internal/graphicsdriver/opengl: reland: refactoring: remove usages of graphics.IndicesCount

Updates #2460
This commit is contained in:
Hajime Hoshi 2023-03-22 01:09:55 +09:00
parent 61ee811579
commit 034f9c2db8
2 changed files with 38 additions and 11 deletions

View File

@ -48,7 +48,7 @@ const (
)
const (
IndicesCount = (1 << 16) / 3 * 3 // Adjust num for triangles.
IndicesCount = (1 << 16) / 3 * 3 // Adjust num for triangles. TODO: Remove this (#2460).
VertexFloatCount = 8
)

View File

@ -114,9 +114,13 @@ type openGLState struct {
// arrayBuffer is OpenGL's array buffer (vertices data).
arrayBuffer buffer
arrayBufferSizeInBytes int
// elementArrayBuffer is OpenGL's element array buffer (indices data).
elementArrayBuffer buffer
elementArrayBufferSizeInBytes int
lastProgram program
lastUniforms map[string][]uint32
lastActiveTexture int
@ -145,19 +149,45 @@ func (s *openGLState) reset(context *context) error {
}
}
s.arrayBuffer = 0
s.arrayBufferSizeInBytes = 0
s.elementArrayBuffer = 0
s.elementArrayBufferSizeInBytes = 0
return nil
}
func (s *openGLState) setVertices(context *context, vertices []float32, indices []uint16) {
if s.arrayBuffer == 0 {
s.arrayBuffer = context.newArrayBuffer(graphics.IndicesCount * graphics.VertexFloatCount * floatSizeInBytes)
context.ctx.BindBuffer(gl.ARRAY_BUFFER, uint32(s.arrayBuffer))
func pow2(x int) int {
p2 := 1
for p2 < x {
p2 *= 2
}
if s.elementArrayBuffer == 0 {
s.elementArrayBuffer = context.newElementArrayBuffer(graphics.IndicesCount * 2)
context.ctx.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(s.elementArrayBuffer))
return p2
}
func (s *openGLState) setVertices(context *context, vertices []float32, indices []uint16) {
if size := len(vertices) * 4; s.arrayBufferSizeInBytes < size {
if s.arrayBuffer != 0 {
context.ctx.DeleteBuffer(uint32(s.arrayBuffer))
}
newSize := pow2(size)
// newArrayBuffer calls BindBuffer.
s.arrayBuffer = context.newArrayBuffer(newSize)
s.arrayBufferSizeInBytes = newSize
// Reenable the array buffer layout explicitly after resetting the array buffer.
theArrayBufferLayout.enable(context)
}
if size := len(indices) * 2; s.elementArrayBufferSizeInBytes < size {
if s.elementArrayBuffer != 0 {
context.ctx.DeleteBuffer(uint32(s.elementArrayBuffer))
}
newSize := pow2(size)
// newElementArrayBuffer calls BindBuffer.
s.elementArrayBuffer = context.newElementArrayBuffer(newSize)
s.elementArrayBufferSizeInBytes = newSize
}
// Note that the vertices and the indices passed to BufferSubData is not under GC management in the gl package.
@ -213,9 +243,6 @@ func (g *Graphics) textureVariableName(idx int) string {
func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textures [graphics.ShaderImageCount]textureVariable) error {
if g.state.lastProgram != program {
g.context.ctx.UseProgram(uint32(program))
if g.state.lastProgram == 0 {
theArrayBufferLayout.enable(&g.context)
}
g.state.lastProgram = program
for k := range g.state.lastUniforms {