graphicsdriver/opengl: Unexport Context

This commit is contained in:
Hajime Hoshi 2018-11-11 01:14:26 +09:00
parent 241716d0e6
commit 7e363a6f3b
5 changed files with 119 additions and 120 deletions

View File

@ -55,8 +55,7 @@ func convertOperation(op graphics.Operation) operation {
} }
} }
// TODO: Unexport this type context struct {
type Context struct {
locationCache *locationCache locationCache *locationCache
screenFramebuffer framebufferNative // This might not be the default frame buffer '0' (e.g. iOS). screenFramebuffer framebufferNative // This might not be the default frame buffer '0' (e.g. iOS).
lastFramebuffer framebufferNative lastFramebuffer framebufferNative
@ -65,12 +64,12 @@ type Context struct {
lastViewportHeight int lastViewportHeight int
lastCompositeMode graphics.CompositeMode lastCompositeMode graphics.CompositeMode
maxTextureSize int maxTextureSize int
context contextImpl
} }
var theContext Context var theContext context
func (c *Context) bindTexture(t textureNative) { func (c *context) bindTexture(t textureNative) {
if c.lastTexture == t { if c.lastTexture == t {
return return
} }
@ -78,7 +77,7 @@ func (c *Context) bindTexture(t textureNative) {
c.lastTexture = t c.lastTexture = t
} }
func (c *Context) bindFramebuffer(f framebufferNative) { func (c *context) bindFramebuffer(f framebufferNative) {
if c.lastFramebuffer == f { if c.lastFramebuffer == f {
return return
} }
@ -86,7 +85,7 @@ func (c *Context) bindFramebuffer(f framebufferNative) {
c.lastFramebuffer = f c.lastFramebuffer = f
} }
func (c *Context) setViewport(f *framebuffer) { func (c *context) setViewport(f *framebuffer) {
c.bindFramebuffer(f.native) c.bindFramebuffer(f.native)
if c.lastViewportWidth != f.width || c.lastViewportHeight != f.height { if c.lastViewportWidth != f.width || c.lastViewportHeight != f.height {
c.setViewportImpl(f.width, f.height) c.setViewportImpl(f.width, f.height)
@ -103,11 +102,11 @@ func (c *Context) setViewport(f *framebuffer) {
} }
} }
func (c *Context) getScreenFramebuffer() framebufferNative { func (c *context) getScreenFramebuffer() framebufferNative {
return c.screenFramebuffer return c.screenFramebuffer
} }
func (c *Context) getMaxTextureSize() int { func (c *context) getMaxTextureSize() int {
if c.maxTextureSize == 0 { if c.maxTextureSize == 0 {
c.maxTextureSize = c.maxTextureSizeImpl() c.maxTextureSize = c.maxTextureSizeImpl()
} }

View File

@ -74,11 +74,11 @@ func init() {
initializeArrayBuferLayout() initializeArrayBuferLayout()
} }
type context struct { type contextImpl struct {
init bool init bool
} }
func (c *Context) reset() error { func (c *context) reset() error {
if err := mainthread.Run(func() error { if err := mainthread.Run(func() error {
if c.init { if c.init {
return nil return nil
@ -112,7 +112,7 @@ func (c *Context) reset() error {
return nil return nil
} }
func (c *Context) blendFunc(mode graphics.CompositeMode) { func (c *context) blendFunc(mode graphics.CompositeMode) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return nil return nil
@ -125,7 +125,7 @@ func (c *Context) blendFunc(mode graphics.CompositeMode) {
}) })
} }
func (c *Context) newTexture(width, height int) (textureNative, error) { func (c *context) newTexture(width, height int) (textureNative, error) {
var texture textureNative var texture textureNative
if err := mainthread.Run(func() error { if err := mainthread.Run(func() error {
var t uint32 var t uint32
@ -152,14 +152,14 @@ func (c *Context) newTexture(width, height int) (textureNative, error) {
return texture, nil return texture, nil
} }
func (c *Context) bindFramebufferImpl(f framebufferNative) { func (c *context) bindFramebufferImpl(f framebufferNative) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.BindFramebufferEXT(gl.FRAMEBUFFER, uint32(f)) gl.BindFramebufferEXT(gl.FRAMEBUFFER, uint32(f))
return nil return nil
}) })
} }
func (c *Context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) { func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) {
var pixels []byte var pixels []byte
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.Flush() gl.Flush()
@ -180,14 +180,14 @@ func (c *Context) framebufferPixels(f *framebuffer, width, height int) ([]byte,
return pixels, nil return pixels, nil
} }
func (c *Context) bindTextureImpl(t textureNative) { func (c *context) bindTextureImpl(t textureNative) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.BindTexture(gl.TEXTURE_2D, uint32(t)) gl.BindTexture(gl.TEXTURE_2D, uint32(t))
return nil return nil
}) })
} }
func (c *Context) deleteTexture(t textureNative) { func (c *context) deleteTexture(t textureNative) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
tt := uint32(t) tt := uint32(t)
if !gl.IsTexture(tt) { if !gl.IsTexture(tt) {
@ -201,7 +201,7 @@ func (c *Context) deleteTexture(t textureNative) {
}) })
} }
func (c *Context) isTexture(t textureNative) bool { func (c *context) isTexture(t textureNative) bool {
r := false r := false
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
r = gl.IsTexture(uint32(t)) r = gl.IsTexture(uint32(t))
@ -210,7 +210,7 @@ func (c *Context) isTexture(t textureNative) bool {
return r return r
} }
func (c *Context) texSubImage2D(t textureNative, p []byte, x, y, width, height int) { func (c *context) texSubImage2D(t textureNative, p []byte, x, y, width, height int) {
c.bindTexture(t) c.bindTexture(t)
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.TexSubImage2D(gl.TEXTURE_2D, 0, int32(x), int32(y), int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p)) gl.TexSubImage2D(gl.TEXTURE_2D, 0, int32(x), int32(y), int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
@ -218,7 +218,7 @@ func (c *Context) texSubImage2D(t textureNative, p []byte, x, y, width, height i
}) })
} }
func (c *Context) newFramebuffer(texture textureNative) (framebufferNative, error) { func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
var framebuffer framebufferNative var framebuffer framebufferNative
var f uint32 var f uint32
if err := mainthread.Run(func() error { if err := mainthread.Run(func() error {
@ -252,14 +252,14 @@ func (c *Context) newFramebuffer(texture textureNative) (framebufferNative, erro
return framebuffer, nil return framebuffer, nil
} }
func (c *Context) setViewportImpl(width, height int) { func (c *context) setViewportImpl(width, height int) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.Viewport(0, 0, int32(width), int32(height)) gl.Viewport(0, 0, int32(width), int32(height))
return nil return nil
}) })
} }
func (c *Context) deleteFramebuffer(f framebufferNative) { func (c *context) deleteFramebuffer(f framebufferNative) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
ff := uint32(f) ff := uint32(f)
if !gl.IsFramebufferEXT(ff) { if !gl.IsFramebufferEXT(ff) {
@ -275,7 +275,7 @@ func (c *Context) deleteFramebuffer(f framebufferNative) {
}) })
} }
func (c *Context) newShader(shaderType shaderType, source string) (shader, error) { func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
var sh shader var sh shader
if err := mainthread.Run(func() error { if err := mainthread.Run(func() error {
s := gl.CreateShader(uint32(shaderType)) s := gl.CreateShader(uint32(shaderType))
@ -306,14 +306,14 @@ func (c *Context) newShader(shaderType shaderType, source string) (shader, error
return sh, nil return sh, nil
} }
func (c *Context) deleteShader(s shader) { func (c *context) deleteShader(s shader) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.DeleteShader(uint32(s)) gl.DeleteShader(uint32(s))
return nil return nil
}) })
} }
func (c *Context) newProgram(shaders []shader) (program, error) { func (c *context) newProgram(shaders []shader) (program, error) {
var pr program var pr program
if err := mainthread.Run(func() error { if err := mainthread.Run(func() error {
p := gl.CreateProgram() p := gl.CreateProgram()
@ -338,14 +338,14 @@ func (c *Context) newProgram(shaders []shader) (program, error) {
return pr, nil return pr, nil
} }
func (c *Context) useProgram(p program) { func (c *context) useProgram(p program) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.UseProgram(uint32(p)) gl.UseProgram(uint32(p))
return nil return nil
}) })
} }
func (c *Context) deleteProgram(p program) { func (c *context) deleteProgram(p program) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
if !gl.IsProgram(uint32(p)) { if !gl.IsProgram(uint32(p)) {
return nil return nil
@ -355,7 +355,7 @@ func (c *Context) deleteProgram(p program) {
}) })
} }
func (c *Context) getUniformLocationImpl(p program, location string) uniformLocation { func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
l, free := gl.Strs(location + "\x00") l, free := gl.Strs(location + "\x00")
uniform := uniformLocation(gl.GetUniformLocation(uint32(p), *l)) uniform := uniformLocation(gl.GetUniformLocation(uint32(p), *l))
free() free()
@ -365,7 +365,7 @@ func (c *Context) getUniformLocationImpl(p program, location string) uniformLoca
return uniform return uniform
} }
func (c *Context) uniformInt(p program, location string, v int) { func (c *context) uniformInt(p program, location string, v int) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
gl.Uniform1i(l, int32(v)) gl.Uniform1i(l, int32(v))
@ -373,7 +373,7 @@ func (c *Context) uniformInt(p program, location string, v int) {
}) })
} }
func (c *Context) uniformFloat(p program, location string, v float32) { func (c *context) uniformFloat(p program, location string, v float32) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
gl.Uniform1f(l, v) gl.Uniform1f(l, v)
@ -381,7 +381,7 @@ func (c *Context) uniformFloat(p program, location string, v float32) {
}) })
} }
func (c *Context) uniformFloats(p program, location string, v []float32) { func (c *context) uniformFloats(p program, location string, v []float32) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
switch len(v) { switch len(v) {
@ -398,7 +398,7 @@ func (c *Context) uniformFloats(p program, location string, v []float32) {
}) })
} }
func (c *Context) getAttribLocationImpl(p program, location string) attribLocation { func (c *context) getAttribLocationImpl(p program, location string) attribLocation {
l, free := gl.Strs(location + "\x00") l, free := gl.Strs(location + "\x00")
attrib := attribLocation(gl.GetAttribLocation(uint32(p), *l)) attrib := attribLocation(gl.GetAttribLocation(uint32(p), *l))
free() free()
@ -408,7 +408,7 @@ func (c *Context) getAttribLocationImpl(p program, location string) attribLocati
return attrib return attrib
} }
func (c *Context) vertexAttribPointer(p program, location string, size int, dataType dataType, stride int, offset int) { func (c *context) vertexAttribPointer(p program, location string, size int, dataType dataType, stride int, offset int) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.VertexAttribPointer(uint32(l), int32(size), uint32(dataType), false, int32(stride), gl.PtrOffset(offset)) gl.VertexAttribPointer(uint32(l), int32(size), uint32(dataType), false, int32(stride), gl.PtrOffset(offset))
@ -416,7 +416,7 @@ func (c *Context) vertexAttribPointer(p program, location string, size int, data
}) })
} }
func (c *Context) enableVertexAttribArray(p program, location string) { func (c *context) enableVertexAttribArray(p program, location string) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.EnableVertexAttribArray(uint32(l)) gl.EnableVertexAttribArray(uint32(l))
@ -424,7 +424,7 @@ func (c *Context) enableVertexAttribArray(p program, location string) {
}) })
} }
func (c *Context) disableVertexAttribArray(p program, location string) { func (c *context) disableVertexAttribArray(p program, location string) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.DisableVertexAttribArray(uint32(l)) gl.DisableVertexAttribArray(uint32(l))
@ -432,7 +432,7 @@ func (c *Context) disableVertexAttribArray(p program, location string) {
}) })
} }
func (c *Context) newArrayBuffer(size int) buffer { func (c *context) newArrayBuffer(size int) buffer {
var bf buffer var bf buffer
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
var b uint32 var b uint32
@ -445,7 +445,7 @@ func (c *Context) newArrayBuffer(size int) buffer {
return bf return bf
} }
func (c *Context) newElementArrayBuffer(size int) buffer { func (c *context) newElementArrayBuffer(size int) buffer {
var bf buffer var bf buffer
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
var b uint32 var b uint32
@ -458,28 +458,28 @@ func (c *Context) newElementArrayBuffer(size int) buffer {
return bf return bf
} }
func (c *Context) bindBuffer(bufferType bufferType, b buffer) { func (c *context) bindBuffer(bufferType bufferType, b buffer) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.BindBuffer(uint32(bufferType), uint32(b)) gl.BindBuffer(uint32(bufferType), uint32(b))
return nil return nil
}) })
} }
func (c *Context) arrayBufferSubData(data []float32) { func (c *context) arrayBufferSubData(data []float32) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.BufferSubData(uint32(arrayBuffer), 0, len(data)*4, gl.Ptr(data)) gl.BufferSubData(uint32(arrayBuffer), 0, len(data)*4, gl.Ptr(data))
return nil return nil
}) })
} }
func (c *Context) elementArrayBufferSubData(data []uint16) { func (c *context) elementArrayBufferSubData(data []uint16) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.BufferSubData(uint32(elementArrayBuffer), 0, len(data)*2, gl.Ptr(data)) gl.BufferSubData(uint32(elementArrayBuffer), 0, len(data)*2, gl.Ptr(data))
return nil return nil
}) })
} }
func (c *Context) deleteBuffer(b buffer) { func (c *context) deleteBuffer(b buffer) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
bb := uint32(b) bb := uint32(b)
gl.DeleteBuffers(1, &bb) gl.DeleteBuffers(1, &bb)
@ -487,14 +487,14 @@ func (c *Context) deleteBuffer(b buffer) {
}) })
} }
func (c *Context) drawElements(len int, offsetInBytes int) { func (c *context) drawElements(len int, offsetInBytes int) {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.DrawElements(gl.TRIANGLES, int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(offsetInBytes)) gl.DrawElements(gl.TRIANGLES, int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(offsetInBytes))
return nil return nil
}) })
} }
func (c *Context) maxTextureSizeImpl() int { func (c *context) maxTextureSizeImpl() int {
size := 0 size := 0
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
s := int32(0) s := int32(0)
@ -505,7 +505,7 @@ func (c *Context) maxTextureSizeImpl() int {
return size return size
} }
func (c *Context) flush() { func (c *context) flush() {
_ = mainthread.Run(func() error { _ = mainthread.Run(func() error {
gl.Flush() gl.Flush()
return nil return nil

View File

@ -113,13 +113,13 @@ func init() {
initializeArrayBuferLayout() initializeArrayBuferLayout()
} }
type context struct { type contextImpl struct {
gl js.Value gl js.Value
loseContext js.Value loseContext js.Value
lastProgramID programID lastProgramID programID
} }
func (c *Context) ensureGL() { func (c *context) ensureGL() {
if c.gl != (js.Value{}) { if c.gl != (js.Value{}) {
return return
} }
@ -147,7 +147,7 @@ func (c *Context) ensureGL() {
c.loseContext = gl.Call("getExtension", "WEBGL_lose_context") c.loseContext = gl.Call("getExtension", "WEBGL_lose_context")
} }
func (c *Context) reset() error { func (c *context) reset() error {
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = textureNative(js.Null()) c.lastTexture = textureNative(js.Null())
c.lastFramebuffer = framebufferNative(js.Null()) c.lastFramebuffer = framebufferNative(js.Null())
@ -164,7 +164,7 @@ func (c *Context) reset() error {
return nil return nil
} }
func (c *Context) blendFunc(mode graphics.CompositeMode) { func (c *context) blendFunc(mode graphics.CompositeMode) {
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return return
} }
@ -176,7 +176,7 @@ func (c *Context) blendFunc(mode graphics.CompositeMode) {
gl.Call("blendFunc", int(s2), int(d2)) gl.Call("blendFunc", int(s2), int(d2))
} }
func (c *Context) newTexture(width, height int) (textureNative, error) { func (c *context) newTexture(width, height int) (textureNative, error) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
t := gl.Call("createTexture") t := gl.Call("createTexture")
@ -203,13 +203,13 @@ func (c *Context) newTexture(width, height int) (textureNative, error) {
return textureNative(t), nil return textureNative(t), nil
} }
func (c *Context) bindFramebufferImpl(f framebufferNative) { func (c *context) bindFramebufferImpl(f framebufferNative) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("bindFramebuffer", framebuffer_, js.Value(f)) gl.Call("bindFramebuffer", framebuffer_, js.Value(f))
} }
func (c *Context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) { func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
@ -226,13 +226,13 @@ func (c *Context) framebufferPixels(f *framebuffer, width, height int) ([]byte,
return pixels, nil return pixels, nil
} }
func (c *Context) bindTextureImpl(t textureNative) { func (c *context) bindTextureImpl(t textureNative) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("bindTexture", texture2d, js.Value(t)) gl.Call("bindTexture", texture2d, js.Value(t))
} }
func (c *Context) deleteTexture(t textureNative) { func (c *context) deleteTexture(t textureNative) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
if !gl.Call("isTexture", js.Value(t)).Bool() { if !gl.Call("isTexture", js.Value(t)).Bool() {
@ -244,13 +244,13 @@ func (c *Context) deleteTexture(t textureNative) {
gl.Call("deleteTexture", js.Value(t)) gl.Call("deleteTexture", js.Value(t))
} }
func (c *Context) isTexture(t textureNative) bool { func (c *context) isTexture(t textureNative) bool {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
return gl.Call("isTexture", js.Value(t)).Bool() return gl.Call("isTexture", js.Value(t)).Bool()
} }
func (c *Context) texSubImage2D(t textureNative, pixels []byte, x, y, width, height int) { func (c *context) texSubImage2D(t textureNative, pixels []byte, x, y, width, height int) {
c.bindTexture(t) c.bindTexture(t)
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
@ -262,7 +262,7 @@ func (c *Context) texSubImage2D(t textureNative, pixels []byte, x, y, width, hei
p.Release() p.Release()
} }
func (c *Context) newFramebuffer(t textureNative) (framebufferNative, error) { func (c *context) newFramebuffer(t textureNative) (framebufferNative, error) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
f := gl.Call("createFramebuffer") f := gl.Call("createFramebuffer")
@ -276,13 +276,13 @@ func (c *Context) newFramebuffer(t textureNative) (framebufferNative, error) {
return framebufferNative(f), nil return framebufferNative(f), nil
} }
func (c *Context) setViewportImpl(width, height int) { func (c *context) setViewportImpl(width, height int) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("viewport", 0, 0, width, height) gl.Call("viewport", 0, 0, width, height)
} }
func (c *Context) deleteFramebuffer(f framebufferNative) { func (c *context) deleteFramebuffer(f framebufferNative) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
if !gl.Call("isFramebuffer", js.Value(f)).Bool() { if !gl.Call("isFramebuffer", js.Value(f)).Bool() {
@ -299,7 +299,7 @@ func (c *Context) deleteFramebuffer(f framebufferNative) {
gl.Call("deleteFramebuffer", js.Value(f)) gl.Call("deleteFramebuffer", js.Value(f))
} }
func (c *Context) newShader(shaderType shaderType, source string) (shader, error) { func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
s := gl.Call("createShader", int(shaderType)) s := gl.Call("createShader", int(shaderType))
@ -317,13 +317,13 @@ func (c *Context) newShader(shaderType shaderType, source string) (shader, error
return shader(s), nil return shader(s), nil
} }
func (c *Context) deleteShader(s shader) { func (c *context) deleteShader(s shader) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("deleteShader", js.Value(s)) gl.Call("deleteShader", js.Value(s))
} }
func (c *Context) newProgram(shaders []shader) (program, error) { func (c *context) newProgram(shaders []shader) (program, error) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
v := gl.Call("createProgram") v := gl.Call("createProgram")
@ -347,13 +347,13 @@ func (c *Context) newProgram(shaders []shader) (program, error) {
}, nil }, nil
} }
func (c *Context) useProgram(p program) { func (c *context) useProgram(p program) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("useProgram", p.value) gl.Call("useProgram", p.value)
} }
func (c *Context) deleteProgram(p program) { func (c *context) deleteProgram(p program) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
if !gl.Call("isProgram", p.value).Bool() { if !gl.Call("isProgram", p.value).Bool() {
@ -362,20 +362,20 @@ func (c *Context) deleteProgram(p program) {
gl.Call("deleteProgram", p.value) gl.Call("deleteProgram", p.value)
} }
func (c *Context) getUniformLocationImpl(p program, location string) uniformLocation { func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
return uniformLocation(gl.Call("getUniformLocation", p.value, location)) return uniformLocation(gl.Call("getUniformLocation", p.value, location))
} }
func (c *Context) uniformInt(p program, location string, v int) { func (c *context) uniformInt(p program, location string, v int) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location) l := c.locationCache.GetUniformLocation(c, p, location)
gl.Call("uniform1i", js.Value(l), v) gl.Call("uniform1i", js.Value(l), v)
} }
func (c *Context) uniformFloat(p program, location string, v float32) { func (c *context) uniformFloat(p program, location string, v float32) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location) l := c.locationCache.GetUniformLocation(c, p, location)
@ -386,7 +386,7 @@ var (
float32Array = js.Global().Get("Float32Array") float32Array = js.Global().Get("Float32Array")
) )
func (c *Context) uniformFloats(p program, location string, v []float32) { func (c *context) uniformFloats(p program, location string, v []float32) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location) l := c.locationCache.GetUniformLocation(c, p, location)
@ -404,34 +404,34 @@ func (c *Context) uniformFloats(p program, location string, v []float32) {
} }
} }
func (c *Context) getAttribLocationImpl(p program, location string) attribLocation { func (c *context) getAttribLocationImpl(p program, location string) attribLocation {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
return attribLocation(gl.Call("getAttribLocation", p.value, location).Int()) return attribLocation(gl.Call("getAttribLocation", p.value, location).Int())
} }
func (c *Context) vertexAttribPointer(p program, location string, size int, dataType dataType, stride int, offset int) { func (c *context) vertexAttribPointer(p program, location string, size int, dataType dataType, stride int, offset int) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("vertexAttribPointer", int(l), size, int(dataType), false, stride, offset) gl.Call("vertexAttribPointer", int(l), size, int(dataType), false, stride, offset)
} }
func (c *Context) enableVertexAttribArray(p program, location string) { func (c *context) enableVertexAttribArray(p program, location string) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("enableVertexAttribArray", int(l)) gl.Call("enableVertexAttribArray", int(l))
} }
func (c *Context) disableVertexAttribArray(p program, location string) { func (c *context) disableVertexAttribArray(p program, location string) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("disableVertexAttribArray", int(l)) gl.Call("disableVertexAttribArray", int(l))
} }
func (c *Context) newArrayBuffer(size int) buffer { func (c *context) newArrayBuffer(size int) buffer {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
b := gl.Call("createBuffer") b := gl.Call("createBuffer")
@ -440,7 +440,7 @@ func (c *Context) newArrayBuffer(size int) buffer {
return buffer(b) return buffer(b)
} }
func (c *Context) newElementArrayBuffer(size int) buffer { func (c *context) newElementArrayBuffer(size int) buffer {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
b := gl.Call("createBuffer") b := gl.Call("createBuffer")
@ -449,13 +449,13 @@ func (c *Context) newElementArrayBuffer(size int) buffer {
return buffer(b) return buffer(b)
} }
func (c *Context) bindBuffer(bufferType bufferType, b buffer) { func (c *context) bindBuffer(bufferType bufferType, b buffer) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("bindBuffer", int(bufferType), js.Value(b)) gl.Call("bindBuffer", int(bufferType), js.Value(b))
} }
func (c *Context) arrayBufferSubData(data []float32) { func (c *context) arrayBufferSubData(data []float32) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
arr := js.TypedArrayOf(data) arr := js.TypedArrayOf(data)
@ -463,7 +463,7 @@ func (c *Context) arrayBufferSubData(data []float32) {
arr.Release() arr.Release()
} }
func (c *Context) elementArrayBufferSubData(data []uint16) { func (c *context) elementArrayBufferSubData(data []uint16) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
arr := js.TypedArrayOf(data) arr := js.TypedArrayOf(data)
@ -471,37 +471,37 @@ func (c *Context) elementArrayBufferSubData(data []uint16) {
arr.Release() arr.Release()
} }
func (c *Context) deleteBuffer(b buffer) { func (c *context) deleteBuffer(b buffer) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("deleteBuffer", js.Value(b)) gl.Call("deleteBuffer", js.Value(b))
} }
func (c *Context) drawElements(len int, offsetInBytes int) { func (c *context) drawElements(len int, offsetInBytes int) {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("drawElements", triangles, len, unsignedShort, offsetInBytes) gl.Call("drawElements", triangles, len, unsignedShort, offsetInBytes)
} }
func (c *Context) maxTextureSizeImpl() int { func (c *context) maxTextureSizeImpl() int {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
return gl.Call("getParameter", maxTextureSize).Int() return gl.Call("getParameter", maxTextureSize).Int()
} }
func (c *Context) flush() { func (c *context) flush() {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
gl.Call("flush") gl.Call("flush")
} }
func (c *Context) isContextLost() bool { func (c *context) isContextLost() bool {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
return gl.Call("isContextLost").Bool() return gl.Call("isContextLost").Bool()
} }
func (c *Context) restoreContext() { func (c *context) restoreContext() {
if c.loseContext != js.Null() { if c.loseContext != js.Null() {
c.loseContext.Call("restoreContext") c.loseContext.Call("restoreContext")
} }

View File

@ -70,7 +70,7 @@ func init() {
initializeArrayBuferLayout() initializeArrayBuferLayout()
} }
type context struct { type contextImpl struct {
gl mgl.Context gl mgl.Context
worker mgl.Worker worker mgl.Worker
} }
@ -83,7 +83,7 @@ func InitWithContext(context mgl.Context) {
theContext.gl = context theContext.gl = context
} }
func (c *Context) doWork(chError <-chan error, chDone <-chan struct{}) error { func (c *context) doWork(chError <-chan error, chDone <-chan struct{}) error {
if c.worker == nil { if c.worker == nil {
panic("not reached") panic("not reached")
} }
@ -103,7 +103,7 @@ loop:
return nil return nil
} }
func (c *Context) reset() error { func (c *context) reset() error {
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = invalidTexture c.lastTexture = invalidTexture
c.lastFramebuffer = invalidFramebuffer c.lastFramebuffer = invalidFramebuffer
@ -118,7 +118,7 @@ func (c *Context) reset() error {
return nil return nil
} }
func (c *Context) blendFunc(mode graphics.CompositeMode) { func (c *context) blendFunc(mode graphics.CompositeMode) {
gl := c.gl gl := c.gl
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return return
@ -129,7 +129,7 @@ func (c *Context) blendFunc(mode graphics.CompositeMode) {
gl.BlendFunc(mgl.Enum(s2), mgl.Enum(d2)) gl.BlendFunc(mgl.Enum(s2), mgl.Enum(d2))
} }
func (c *Context) newTexture(width, height int) (textureNative, error) { func (c *context) newTexture(width, height int) (textureNative, error) {
gl := c.gl gl := c.gl
t := gl.CreateTexture() t := gl.CreateTexture()
if t.Value <= 0 { if t.Value <= 0 {
@ -147,12 +147,12 @@ func (c *Context) newTexture(width, height int) (textureNative, error) {
return textureNative(t), nil return textureNative(t), nil
} }
func (c *Context) bindFramebufferImpl(f framebufferNative) { func (c *context) bindFramebufferImpl(f framebufferNative) {
gl := c.gl gl := c.gl
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f)) gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f))
} }
func (c *Context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) { func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) {
gl := c.gl gl := c.gl
gl.Flush() gl.Flush()
@ -166,12 +166,12 @@ func (c *Context) framebufferPixels(f *framebuffer, width, height int) ([]byte,
return pixels, nil return pixels, nil
} }
func (c *Context) bindTextureImpl(t textureNative) { func (c *context) bindTextureImpl(t textureNative) {
gl := c.gl gl := c.gl
gl.BindTexture(mgl.TEXTURE_2D, mgl.Texture(t)) gl.BindTexture(mgl.TEXTURE_2D, mgl.Texture(t))
} }
func (c *Context) deleteTexture(t textureNative) { func (c *context) deleteTexture(t textureNative) {
gl := c.gl gl := c.gl
if !gl.IsTexture(mgl.Texture(t)) { if !gl.IsTexture(mgl.Texture(t)) {
return return
@ -182,18 +182,18 @@ func (c *Context) deleteTexture(t textureNative) {
gl.DeleteTexture(mgl.Texture(t)) gl.DeleteTexture(mgl.Texture(t))
} }
func (c *Context) isTexture(t textureNative) bool { func (c *context) isTexture(t textureNative) bool {
gl := c.gl gl := c.gl
return gl.IsTexture(mgl.Texture(t)) return gl.IsTexture(mgl.Texture(t))
} }
func (c *Context) texSubImage2D(t textureNative, p []byte, x, y, width, height int) { func (c *context) texSubImage2D(t textureNative, p []byte, x, y, width, height int) {
c.bindTexture(t) c.bindTexture(t)
gl := c.gl gl := c.gl
gl.TexSubImage2D(mgl.TEXTURE_2D, 0, x, y, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, p) gl.TexSubImage2D(mgl.TEXTURE_2D, 0, x, y, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, p)
} }
func (c *Context) newFramebuffer(texture textureNative) (framebufferNative, error) { func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
gl := c.gl gl := c.gl
f := gl.CreateFramebuffer() f := gl.CreateFramebuffer()
if f.Value <= 0 { if f.Value <= 0 {
@ -215,12 +215,12 @@ func (c *Context) newFramebuffer(texture textureNative) (framebufferNative, erro
return framebufferNative(f), nil return framebufferNative(f), nil
} }
func (c *Context) setViewportImpl(width, height int) { func (c *context) setViewportImpl(width, height int) {
gl := c.gl gl := c.gl
gl.Viewport(0, 0, width, height) gl.Viewport(0, 0, width, height)
} }
func (c *Context) deleteFramebuffer(f framebufferNative) { func (c *context) deleteFramebuffer(f framebufferNative) {
gl := c.gl gl := c.gl
if !gl.IsFramebuffer(mgl.Framebuffer(f)) { if !gl.IsFramebuffer(mgl.Framebuffer(f)) {
return return
@ -236,7 +236,7 @@ func (c *Context) deleteFramebuffer(f framebufferNative) {
gl.DeleteFramebuffer(mgl.Framebuffer(f)) gl.DeleteFramebuffer(mgl.Framebuffer(f))
} }
func (c *Context) newShader(shaderType shaderType, source string) (shader, error) { func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
gl := c.gl gl := c.gl
s := gl.CreateShader(mgl.Enum(shaderType)) s := gl.CreateShader(mgl.Enum(shaderType))
if s.Value == 0 { if s.Value == 0 {
@ -253,12 +253,12 @@ func (c *Context) newShader(shaderType shaderType, source string) (shader, error
return shader(s), nil return shader(s), nil
} }
func (c *Context) deleteShader(s shader) { func (c *context) deleteShader(s shader) {
gl := c.gl gl := c.gl
gl.DeleteShader(mgl.Shader(s)) gl.DeleteShader(mgl.Shader(s))
} }
func (c *Context) newProgram(shaders []shader) (program, error) { func (c *context) newProgram(shaders []shader) (program, error) {
gl := c.gl gl := c.gl
p := gl.CreateProgram() p := gl.CreateProgram()
if p.Value == 0 { if p.Value == 0 {
@ -276,12 +276,12 @@ func (c *Context) newProgram(shaders []shader) (program, error) {
return program(p), nil return program(p), nil
} }
func (c *Context) useProgram(p program) { func (c *context) useProgram(p program) {
gl := c.gl gl := c.gl
gl.UseProgram(mgl.Program(p)) gl.UseProgram(mgl.Program(p))
} }
func (c *Context) deleteProgram(p program) { func (c *context) deleteProgram(p program) {
gl := c.gl gl := c.gl
if !gl.IsProgram(mgl.Program(p)) { if !gl.IsProgram(mgl.Program(p)) {
return return
@ -289,7 +289,7 @@ func (c *Context) deleteProgram(p program) {
gl.DeleteProgram(mgl.Program(p)) gl.DeleteProgram(mgl.Program(p))
} }
func (c *Context) getUniformLocationImpl(p program, location string) uniformLocation { func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
gl := c.gl gl := c.gl
u := uniformLocation(gl.GetUniformLocation(mgl.Program(p), location)) u := uniformLocation(gl.GetUniformLocation(mgl.Program(p), location))
if u.Value == -1 { if u.Value == -1 {
@ -298,17 +298,17 @@ func (c *Context) getUniformLocationImpl(p program, location string) uniformLoca
return u return u
} }
func (c *Context) uniformInt(p program, location string, v int) { func (c *context) uniformInt(p program, location string, v int) {
gl := c.gl gl := c.gl
gl.Uniform1i(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v) gl.Uniform1i(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
} }
func (c *Context) uniformFloat(p program, location string, v float32) { func (c *context) uniformFloat(p program, location string, v float32) {
gl := c.gl gl := c.gl
gl.Uniform1f(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v) gl.Uniform1f(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
} }
func (c *Context) uniformFloats(p program, location string, v []float32) { func (c *context) uniformFloats(p program, location string, v []float32) {
gl := c.gl gl := c.gl
l := mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)) l := mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location))
switch len(v) { switch len(v) {
@ -323,7 +323,7 @@ func (c *Context) uniformFloats(p program, location string, v []float32) {
} }
} }
func (c *Context) getAttribLocationImpl(p program, location string) attribLocation { func (c *context) getAttribLocationImpl(p program, location string) attribLocation {
gl := c.gl gl := c.gl
a := attribLocation(gl.GetAttribLocation(mgl.Program(p), location)) a := attribLocation(gl.GetAttribLocation(mgl.Program(p), location))
if a.Value == ^uint(0) { if a.Value == ^uint(0) {
@ -332,25 +332,25 @@ func (c *Context) getAttribLocationImpl(p program, location string) attribLocati
return a return a
} }
func (c *Context) vertexAttribPointer(p program, location string, size int, dataType dataType, stride int, offset int) { func (c *context) vertexAttribPointer(p program, location string, size int, dataType dataType, stride 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.Enum(dataType), false, stride, offset) gl.VertexAttribPointer(mgl.Attrib(l), size, mgl.Enum(dataType), false, stride, offset)
} }
func (c *Context) enableVertexAttribArray(p program, location string) { func (c *context) enableVertexAttribArray(p program, location string) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.EnableVertexAttribArray(mgl.Attrib(l)) gl.EnableVertexAttribArray(mgl.Attrib(l))
} }
func (c *Context) disableVertexAttribArray(p program, location string) { func (c *context) disableVertexAttribArray(p program, location string) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.DisableVertexAttribArray(mgl.Attrib(l)) gl.DisableVertexAttribArray(mgl.Attrib(l))
} }
func (c *Context) newArrayBuffer(size int) buffer { func (c *context) newArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.CreateBuffer() b := gl.CreateBuffer()
gl.BindBuffer(mgl.Enum(arrayBuffer), b) gl.BindBuffer(mgl.Enum(arrayBuffer), b)
@ -358,7 +358,7 @@ func (c *Context) newArrayBuffer(size int) buffer {
return buffer(b) return buffer(b)
} }
func (c *Context) newElementArrayBuffer(size int) buffer { func (c *context) newElementArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.CreateBuffer() b := gl.CreateBuffer()
gl.BindBuffer(mgl.Enum(elementArrayBuffer), b) gl.BindBuffer(mgl.Enum(elementArrayBuffer), b)
@ -366,37 +366,37 @@ func (c *Context) newElementArrayBuffer(size int) buffer {
return buffer(b) return buffer(b)
} }
func (c *Context) bindBuffer(bufferType bufferType, b buffer) { func (c *context) bindBuffer(bufferType bufferType, b buffer) {
gl := c.gl gl := c.gl
gl.BindBuffer(mgl.Enum(bufferType), mgl.Buffer(b)) gl.BindBuffer(mgl.Enum(bufferType), mgl.Buffer(b))
} }
func (c *Context) arrayBufferSubData(data []float32) { func (c *context) arrayBufferSubData(data []float32) {
gl := c.gl gl := c.gl
gl.BufferSubData(mgl.Enum(arrayBuffer), 0, float32sToBytes(data)) gl.BufferSubData(mgl.Enum(arrayBuffer), 0, float32sToBytes(data))
} }
func (c *Context) elementArrayBufferSubData(data []uint16) { func (c *context) elementArrayBufferSubData(data []uint16) {
gl := c.gl gl := c.gl
gl.BufferSubData(mgl.Enum(elementArrayBuffer), 0, uint16sToBytes(data)) gl.BufferSubData(mgl.Enum(elementArrayBuffer), 0, uint16sToBytes(data))
} }
func (c *Context) deleteBuffer(b buffer) { func (c *context) deleteBuffer(b buffer) {
gl := c.gl gl := c.gl
gl.DeleteBuffer(mgl.Buffer(b)) gl.DeleteBuffer(mgl.Buffer(b))
} }
func (c *Context) drawElements(len int, offsetInBytes int) { func (c *context) drawElements(len int, offsetInBytes int) {
gl := c.gl gl := c.gl
gl.DrawElements(mgl.TRIANGLES, len, mgl.UNSIGNED_SHORT, offsetInBytes) gl.DrawElements(mgl.TRIANGLES, len, mgl.UNSIGNED_SHORT, offsetInBytes)
} }
func (c *Context) maxTextureSizeImpl() int { func (c *context) maxTextureSizeImpl() int {
gl := c.gl gl := c.gl
return gl.GetInteger(mgl.MAX_TEXTURE_SIZE) return gl.GetInteger(mgl.MAX_TEXTURE_SIZE)
} }
func (c *Context) flush() { func (c *context) flush() {
gl := c.gl gl := c.gl
gl.Flush() gl.Flush()
} }

View File

@ -28,7 +28,7 @@ func newLocationCache() *locationCache {
} }
} }
func (c *locationCache) GetUniformLocation(context *Context, p program, location string) uniformLocation { func (c *locationCache) GetUniformLocation(context *context, p program, location string) uniformLocation {
id := getProgramID(p) id := getProgramID(p)
if _, ok := c.uniformLocationCache[id]; !ok { if _, ok := c.uniformLocationCache[id]; !ok {
c.uniformLocationCache[id] = map[string]uniformLocation{} c.uniformLocationCache[id] = map[string]uniformLocation{}
@ -41,7 +41,7 @@ func (c *locationCache) GetUniformLocation(context *Context, p program, location
return l return l
} }
func (c *locationCache) GetAttribLocation(context *Context, p program, location string) attribLocation { func (c *locationCache) GetAttribLocation(context *context, p program, location string) attribLocation {
id := getProgramID(p) id := getProgramID(p)
if _, ok := c.attribLocationCache[id]; !ok { if _, ok := c.attribLocationCache[id]; !ok {
c.attribLocationCache[id] = map[string]attribLocation{} c.attribLocationCache[id] = map[string]attribLocation{}