internal/graphicsdriver/opengl: automatically adjust the array buffer layout

Updates #2640
This commit is contained in:
Hajime Hoshi 2024-08-12 03:56:47 +09:00
parent 9e208eee81
commit 435c8b75eb

View File

@ -53,14 +53,14 @@ func (a *arrayBufferLayout) names() []string {
return ns return ns
} }
// totalBytes returns the size in bytes for one element of the array buffer. // float32Count returns the total float32 count for one element of the array buffer.
func (a *arrayBufferLayout) totalBytes() int { func (a *arrayBufferLayout) float32Count() int {
if a.total != 0 { if a.total != 0 {
return a.total return a.total
} }
t := 0 t := 0
for _, p := range a.parts { for _, p := range a.parts {
t += floatSizeInBytes * p.num t += p.num
} }
a.total = t a.total = t
return a.total return a.total
@ -71,10 +71,10 @@ func (a *arrayBufferLayout) enable(context *context) {
for i := range a.parts { for i := range a.parts {
context.ctx.EnableVertexAttribArray(uint32(i)) context.ctx.EnableVertexAttribArray(uint32(i))
} }
total := a.totalBytes() total := a.float32Count()
offset := 0 offset := 0
for i, p := range a.parts { for i, p := range a.parts {
context.ctx.VertexAttribPointer(uint32(i), int32(p.num), gl.FLOAT, false, int32(total), offset) context.ctx.VertexAttribPointer(uint32(i), int32(p.num), gl.FLOAT, false, int32(floatSizeInBytes*total), offset)
offset += floatSizeInBytes * p.num offset += floatSizeInBytes * p.num
} }
} }
@ -88,7 +88,10 @@ func (a *arrayBufferLayout) disable(context *context) {
} }
// theArrayBufferLayout is the array buffer layout for Ebitengine. // theArrayBufferLayout is the array buffer layout for Ebitengine.
var theArrayBufferLayout = arrayBufferLayout{ var theArrayBufferLayout arrayBufferLayout
func init() {
theArrayBufferLayout = arrayBufferLayout{
// Note that GL_MAX_VERTEX_ATTRIBS is at least 16. // Note that GL_MAX_VERTEX_ATTRIBS is at least 16.
parts: []arrayBufferLayoutPart{ parts: []arrayBufferLayoutPart{
{ {
@ -104,12 +107,20 @@ var theArrayBufferLayout = arrayBufferLayout{
num: 4, num: 4,
}, },
}, },
} }
n := theArrayBufferLayout.float32Count()
func init() { if n > graphics.VertexFloatCount {
vertexFloatCount := theArrayBufferLayout.totalBytes() / floatSizeInBytes panic("opengl: the array buffer layout is too large")
if graphics.VertexFloatCount != vertexFloatCount { }
panic(fmt.Sprintf("vertex float num must be %d but %d", graphics.VertexFloatCount, vertexFloatCount)) if n < graphics.VertexFloatCount {
d := graphics.VertexFloatCount - n
if d > 4 {
panic("opengl: the array buffer layout is too small")
}
theArrayBufferLayout.parts = append(theArrayBufferLayout.parts, arrayBufferLayoutPart{
name: "A3",
num: d,
})
} }
} }