graphics: Refactoring: Introduce arrayBufferLayout

This commit is contained in:
Hajime Hoshi 2016-10-17 10:03:25 +09:00
parent c1b4624890
commit 8ee859df31
4 changed files with 48 additions and 15 deletions

View File

@ -432,10 +432,10 @@ func (c *Context) getAttribLocationImpl(p Program, location string) attribLocati
return attrib return attrib
} }
func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, v int) { func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, offset int) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.VertexAttribPointer(uint32(l), int32(size), gl.SHORT, normalize, int32(stride), gl.PtrOffset(v)) gl.VertexAttribPointer(uint32(l), int32(size), gl.SHORT, normalize, int32(stride), gl.PtrOffset(offset))
return nil return nil
}) })
} }

View File

@ -354,10 +354,10 @@ func (c *Context) getAttribLocationImpl(p Program, location string) attribLocati
return attribLocation(gl.GetAttribLocation(p.Object, location)) return attribLocation(gl.GetAttribLocation(p.Object, location))
} }
func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, v int) { func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, offset int) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.VertexAttribPointer(int(l), size, gl.SHORT, normalize, stride, v) gl.VertexAttribPointer(int(l), size, gl.SHORT, normalize, stride, offset)
} }
func (c *Context) EnableVertexAttribArray(p Program, location string) { func (c *Context) EnableVertexAttribArray(p Program, location string) {

View File

@ -336,10 +336,10 @@ func (c *Context) getAttribLocationImpl(p Program, location string) attribLocati
return a return a
} }
func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, v int) { func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, offset int) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.VertexAttribPointer(mgl.Attrib(l), size, mgl.SHORT, normalize, stride, v) gl.VertexAttribPointer(mgl.Attrib(l), size, mgl.SHORT, normalize, stride, offset)
} }
func (c *Context) EnableVertexAttribArray(p Program, location string) { func (c *Context) EnableVertexAttribArray(p Program, location string) {

View File

@ -20,6 +20,42 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/graphics/opengl"
) )
type arrayBufferLayout struct {
}
func (a *arrayBufferLayout) enable(c *opengl.Context, program opengl.Program) {
c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "tex_coord")
// TODO: Argument order doesn't match with glVertexAttribPointer: Fix them.
c.VertexAttribPointer(program, "vertex", false, a.totalBytes(), a.vertexNum(), 0)
c.VertexAttribPointer(program, "tex_coord", true, a.totalBytes(), a.texCoordNum(), a.vertexBytes())
}
func (a *arrayBufferLayout) disable(c *opengl.Context, program opengl.Program) {
c.DisableVertexAttribArray(program, "tex_coord")
c.DisableVertexAttribArray(program, "vertex")
}
func (a *arrayBufferLayout) vertexNum() int {
return 2
}
func (a *arrayBufferLayout) vertexBytes() int {
return int16Size * a.vertexNum()
}
func (a *arrayBufferLayout) texCoordNum() int {
return 2
}
func (a *arrayBufferLayout) texCoordBytes() int {
return int16Size * a.texCoordNum()
}
func (a *arrayBufferLayout) totalBytes() int {
return a.vertexBytes() + a.texCoordBytes()
}
type openGLState struct { type openGLState struct {
arrayBuffer opengl.Buffer arrayBuffer opengl.Buffer
indexBufferQuads opengl.Buffer indexBufferQuads opengl.Buffer
@ -34,6 +70,7 @@ type openGLState struct {
var ( var (
theOpenGLState openGLState theOpenGLState openGLState
theArrayBufferLayout = arrayBufferLayout{}
zeroBuffer opengl.Buffer zeroBuffer opengl.Buffer
zeroProgram opengl.Program zeroProgram opengl.Program
@ -94,8 +131,8 @@ func (s *openGLState) reset(context *opengl.Context) error {
return err return err
} }
const stride = 8 // (2 [vertices] + 2 [texels]) * 2 [sizeof(int16)/bytes] stride := theArrayBufferLayout.totalBytes()
s.arrayBuffer = context.NewBuffer(opengl.ArrayBuffer, 4*stride*maxQuads, opengl.DynamicDraw) s.arrayBuffer = context.NewBuffer(opengl.ArrayBuffer, stride*4*maxQuads, opengl.DynamicDraw)
indices := make([]uint16, 6*maxQuads) indices := make([]uint16, 6*maxQuads)
for i := uint16(0); i < maxQuads; i++ { for i := uint16(0); i < maxQuads; i++ {
@ -138,13 +175,9 @@ func (p *programContext) begin() error {
if p.state.lastProgram != p.program { if p.state.lastProgram != p.program {
c.UseProgram(p.program) c.UseProgram(p.program)
if p.state.lastProgram != zeroProgram { if p.state.lastProgram != zeroProgram {
c.DisableVertexAttribArray(p.state.lastProgram, "tex_coord") theArrayBufferLayout.disable(c, p.state.lastProgram)
c.DisableVertexAttribArray(p.state.lastProgram, "vertex")
} }
c.EnableVertexAttribArray(p.program, "vertex") theArrayBufferLayout.enable(c, p.program)
c.EnableVertexAttribArray(p.program, "tex_coord")
c.VertexAttribPointer(p.program, "vertex", false, int16Size*4, 2, int16Size*0)
c.VertexAttribPointer(p.program, "tex_coord", true, int16Size*4, 2, int16Size*2)
p.state.lastProgram = p.state.programTexture p.state.lastProgram = p.state.programTexture
p.state.lastProjectionMatrix = nil p.state.lastProjectionMatrix = nil