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

Updates #2460
This commit is contained in:
Hajime Hoshi 2023-03-22 01:09:55 +09:00
parent 4cd98d512e
commit 13d3a0487b
2 changed files with 36 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

@ -113,22 +113,50 @@ 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
}
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 layouter 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.
@ -184,9 +212,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 {