New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/mobile/gl: ctx.BufferData and ctx.BufferSubData with unsafe.Pointer #29720
Comments
For comparison, this is the slow and safe way for converting float32 arrays:
And this is the fast, but unsafe and fragile way:
|
I would say functions should be added that take []float32 for the vertices and, []uint16 for the indexes. Like that no unsafe pointer use will be needed. |
/cc @nigeltao |
Yeah, we probably want new methods, with names such as BufferDataFloat32 since with Go, unlike C++, we can't overload methods. /cc @crawshaw |
@nigeltao are you comfortable saying we should accept this proposal? |
I'm comfortable saying we should accept this proposal, except the parts about adding unsafe.Pointer to the API. |
Based on discussion, marking approved for adding more BufferData/BufferSubData with different type arguments, but not unsafe.Pointer. Note also that Go does not have method overloading, so you'd need to do:
|
Change https://golang.org/cl/247077 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
When working with OpenGL, meshes consist out of vertices and indices that need to be pushed into GPU buffers.
The vertex array is usually of type
[]float32
, the index array is usually of type[]uint16
or[]uint32
.The Context-object in
golang.org/x/mobile/gl
(gl.go
andgldebug.go
) offers the following methods for pushing such buffer data:The problem here is that both functions require byte slices. In order to use them, the vertex/index arrays need to be converted first. If done in a safe way (without relying on go implementation details), the data effectively needs to be copied every time before it can be rendered.
When rendering large amounts of data this is a significant (and unnecessary) performance impact.
The implementation of Buffer(Sub)Data converts the incoming byte slice into an
unsafe.Pointer
- which is the format needed by the underlying OpenGL device. So this whole conversion is only needed to satisfy the function arguments.Using only
[]byte
within the application is rarely an option because vertex/index data needs to be manipulated to create dynamic scenes. This would only move the conversion to a different location.What did you expect to see?
I suggest adding more generic functions for setting buffer data:
This would allow users to write any type of slice into buffers without expensive conversion or unsafe/fragile casting.
Alternatively (if the unsafe package should not face to the user) I suggest adding the following functions:
The text was updated successfully, but these errors were encountered: