opengl: Unexport Texture

This commit is contained in:
Hajime Hoshi 2018-11-04 19:46:20 +09:00
parent 786b349579
commit 7bbc32ce0e
7 changed files with 50 additions and 50 deletions

View File

@ -66,7 +66,7 @@ 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
lastTexture Texture lastTexture textureNative
lastViewportWidth int lastViewportWidth int
lastViewportHeight int lastViewportHeight int
lastCompositeMode graphics.CompositeMode lastCompositeMode graphics.CompositeMode
@ -80,7 +80,7 @@ func GetContext() *Context {
return theContext return theContext
} }
func (c *Context) bindTexture(t Texture) { func (c *Context) bindTexture(t textureNative) {
if c.lastTexture == t { if c.lastTexture == t {
return return
} }

View File

@ -29,14 +29,14 @@ import (
) )
type ( type (
Texture uint32 textureNative uint32
framebufferNative uint32 framebufferNative uint32
shader uint32 shader uint32
program uint32 program uint32
buffer uint32 buffer uint32
) )
var InvalidTexture Texture var InvalidTexture textureNative
type ( type (
uniformLocation int32 uniformLocation int32
@ -135,8 +135,8 @@ func (c *Context) BlendFunc(mode graphics.CompositeMode) {
}) })
} }
func (c *Context) newTexture(width, height int) (Texture, error) { func (c *Context) newTexture(width, height int) (textureNative, error) {
var texture Texture var texture textureNative
if err := c.runOnContextThread(func() error { if err := c.runOnContextThread(func() error {
var t uint32 var t uint32
gl.GenTextures(1, &t) gl.GenTextures(1, &t)
@ -145,7 +145,7 @@ func (c *Context) newTexture(width, height int) (Texture, error) {
return errors.New("opengl: creating texture failed") return errors.New("opengl: creating texture failed")
} }
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
texture = Texture(t) texture = textureNative(t)
return nil return nil
}); err != nil { }); err != nil {
return 0, err return 0, err
@ -190,14 +190,14 @@ func (c *Context) framebufferPixels(f *Framebuffer, width, height int) ([]byte,
return pixels, nil return pixels, nil
} }
func (c *Context) bindTextureImpl(t Texture) { func (c *Context) bindTextureImpl(t textureNative) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(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 Texture) { func (c *Context) deleteTexture(t textureNative) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
tt := uint32(t) tt := uint32(t)
if !gl.IsTexture(tt) { if !gl.IsTexture(tt) {
@ -211,7 +211,7 @@ func (c *Context) deleteTexture(t Texture) {
}) })
} }
func (c *Context) isTexture(t Texture) bool { func (c *Context) isTexture(t textureNative) bool {
r := false r := false
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
r = gl.IsTexture(uint32(t)) r = gl.IsTexture(uint32(t))
@ -220,7 +220,7 @@ func (c *Context) isTexture(t Texture) bool {
return r return r
} }
func (c *Context) texSubImage2D(t Texture, 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)
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(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))
@ -232,7 +232,7 @@ func (c *Context) BeforeSwapping() {
c.bindFramebuffer(c.screenFramebuffer) c.bindFramebuffer(c.screenFramebuffer)
} }
func (c *Context) newFramebuffer(texture Texture) (framebufferNative, error) { func (c *Context) newFramebuffer(texture textureNative) (framebufferNative, error) {
var framebuffer framebufferNative var framebuffer framebufferNative
var f uint32 var f uint32
if err := c.runOnContextThread(func() error { if err := c.runOnContextThread(func() error {

View File

@ -26,7 +26,7 @@ import (
) )
type ( type (
Texture js.Value textureNative js.Value
framebufferNative js.Value framebufferNative js.Value
shader js.Value shader js.Value
buffer js.Value buffer js.Value
@ -40,7 +40,7 @@ type (
} }
) )
var InvalidTexture = Texture(js.Null()) var InvalidTexture = textureNative(js.Null())
func getProgramID(p program) programID { func getProgramID(p program) programID {
return p.id return p.id
@ -149,7 +149,7 @@ func Init() error {
func (c *Context) reset() error { func (c *Context) reset() error {
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = Texture(js.Null()) c.lastTexture = textureNative(js.Null())
c.lastFramebuffer = framebufferNative(js.Null()) c.lastFramebuffer = framebufferNative(js.Null())
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
@ -173,14 +173,14 @@ 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) (Texture, error) { func (c *Context) newTexture(width, height int) (textureNative, error) {
gl := c.gl gl := c.gl
t := gl.Call("createTexture") t := gl.Call("createTexture")
if t == js.Null() { if t == js.Null() {
return Texture(js.Null()), errors.New("opengl: glGenTexture failed") return textureNative(js.Null()), errors.New("opengl: glGenTexture failed")
} }
gl.Call("pixelStorei", unpackAlignment, 4) gl.Call("pixelStorei", unpackAlignment, 4)
c.bindTexture(Texture(t)) c.bindTexture(textureNative(t))
gl.Call("texParameteri", texture2d, textureMagFilter, nearest) gl.Call("texParameteri", texture2d, textureMagFilter, nearest)
gl.Call("texParameteri", texture2d, textureMinFilter, nearest) gl.Call("texParameteri", texture2d, textureMinFilter, nearest)
@ -196,7 +196,7 @@ func (c *Context) newTexture(width, height int) (Texture, error) {
// avoided. // avoided.
gl.Call("texImage2D", texture2d, 0, rgba, width, height, 0, rgba, unsignedByte, nil) gl.Call("texImage2D", texture2d, 0, rgba, width, height, 0, rgba, unsignedByte, nil)
return Texture(t), nil return textureNative(t), nil
} }
func (c *Context) bindFramebufferImpl(f framebufferNative) { func (c *Context) bindFramebufferImpl(f framebufferNative) {
@ -220,28 +220,28 @@ func (c *Context) framebufferPixels(f *Framebuffer, width, height int) ([]byte,
return pixels, nil return pixels, nil
} }
func (c *Context) bindTextureImpl(t Texture) { func (c *Context) bindTextureImpl(t textureNative) {
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 Texture) { func (c *Context) deleteTexture(t textureNative) {
gl := c.gl gl := c.gl
if !gl.Call("isTexture", js.Value(t)).Bool() { if !gl.Call("isTexture", js.Value(t)).Bool() {
return return
} }
if c.lastTexture == t { if c.lastTexture == t {
c.lastTexture = Texture(js.Null()) c.lastTexture = textureNative(js.Null())
} }
gl.Call("deleteTexture", js.Value(t)) gl.Call("deleteTexture", js.Value(t))
} }
func (c *Context) isTexture(t Texture) bool { func (c *Context) isTexture(t textureNative) bool {
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 Texture, 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)
gl := c.gl gl := c.gl
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, // void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
@ -252,7 +252,7 @@ func (c *Context) texSubImage2D(t Texture, pixels []byte, x, y, width, height in
p.Release() p.Release()
} }
func (c *Context) newFramebuffer(t Texture) (framebufferNative, error) { func (c *Context) newFramebuffer(t textureNative) (framebufferNative, error) {
gl := c.gl gl := c.gl
f := gl.Call("createFramebuffer") f := gl.Call("createFramebuffer")
c.bindFramebuffer(framebufferNative(f)) c.bindFramebuffer(framebufferNative(f))

View File

@ -26,14 +26,14 @@ import (
) )
type ( type (
Texture mgl.Texture textureNative mgl.Texture
framebufferNative mgl.Framebuffer framebufferNative mgl.Framebuffer
shader mgl.Shader shader mgl.Shader
program mgl.Program program mgl.Program
buffer mgl.Buffer buffer mgl.Buffer
) )
var InvalidTexture Texture var InvalidTexture textureNative
type ( type (
uniformLocation mgl.Uniform uniformLocation mgl.Uniform
@ -43,7 +43,7 @@ type (
type programID uint32 type programID uint32
var ( var (
invalidTexture = Texture(mgl.Texture{}) invalidTexture = textureNative(mgl.Texture{})
invalidFramebuffer = framebufferNative(mgl.Framebuffer{(1 << 32) - 1}) invalidFramebuffer = framebufferNative(mgl.Framebuffer{(1 << 32) - 1})
) )
@ -133,14 +133,14 @@ 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) (Texture, 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 {
return Texture{}, errors.New("opengl: creating texture failed") return textureNative{}, errors.New("opengl: creating texture failed")
} }
gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4)
c.bindTexture(Texture(t)) c.bindTexture(textureNative(t))
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, mgl.NEAREST) gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, mgl.NEAREST)
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, mgl.NEAREST) gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, mgl.NEAREST)
@ -148,7 +148,7 @@ func (c *Context) newTexture(width, height int) (Texture, error) {
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_WRAP_T, mgl.CLAMP_TO_EDGE) gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_WRAP_T, mgl.CLAMP_TO_EDGE)
gl.TexImage2D(mgl.TEXTURE_2D, 0, mgl.RGBA, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, nil) gl.TexImage2D(mgl.TEXTURE_2D, 0, mgl.RGBA, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, nil)
return Texture(t), nil return textureNative(t), nil
} }
func (c *Context) bindFramebufferImpl(f framebufferNative) { func (c *Context) bindFramebufferImpl(f framebufferNative) {
@ -170,12 +170,12 @@ func (c *Context) framebufferPixels(f *Framebuffer, width, height int) ([]byte,
return pixels, nil return pixels, nil
} }
func (c *Context) bindTextureImpl(t Texture) { 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 Texture) { 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
@ -186,18 +186,18 @@ func (c *Context) deleteTexture(t Texture) {
gl.DeleteTexture(mgl.Texture(t)) gl.DeleteTexture(mgl.Texture(t))
} }
func (c *Context) isTexture(t Texture) 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 Texture, 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 Texture) (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 {

View File

@ -25,7 +25,7 @@ type Framebuffer struct {
} }
// newFramebufferFromTexture creates a framebuffer from the given texture. // newFramebufferFromTexture creates a framebuffer from the given texture.
func newFramebufferFromTexture(texture Texture, width, height int) (*Framebuffer, error) { func newFramebufferFromTexture(texture textureNative, width, height int) (*Framebuffer, error) {
native, err := theContext.newFramebuffer(texture) native, err := theContext.newFramebuffer(texture)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -37,10 +37,10 @@ func checkSize(width, height int) {
} }
type Image struct { type Image struct {
Texture Texture textureNative textureNative
Framebuffer *Framebuffer Framebuffer *Framebuffer
width int width int
height int height int
} }
func NewImage(width, height int) (*Image, error) { func NewImage(width, height int) (*Image, error) {
@ -55,7 +55,7 @@ func NewImage(width, height int) (*Image, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
i.Texture = t i.textureNative = t
return i, nil return i, nil
} }
@ -77,15 +77,15 @@ func (i *Image) Size() (int, int) {
} }
func (i *Image) IsInvalidated() bool { func (i *Image) IsInvalidated() bool {
return !theContext.isTexture(i.Texture) return !theContext.isTexture(i.textureNative)
} }
func (i *Image) Delete() { func (i *Image) Delete() {
if i.Framebuffer != nil { if i.Framebuffer != nil {
i.Framebuffer.delete() i.Framebuffer.delete()
} }
if i.Texture != *new(Texture) { if i.textureNative != *new(textureNative) {
theContext.deleteTexture(i.Texture) theContext.deleteTexture(i.textureNative)
} }
} }
@ -121,7 +121,7 @@ func (i *Image) ensureFramebuffer() error {
return nil return nil
} }
w, h := i.Size() w, h := i.Size()
f, err := newFramebufferFromTexture(i.Texture, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h)) f, err := newFramebufferFromTexture(i.textureNative, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
if err != nil { if err != nil {
return err return err
} }
@ -130,5 +130,5 @@ func (i *Image) ensureFramebuffer() error {
} }
func (i *Image) TexSubImage2D(p []byte, x, y, width, height int) { func (i *Image) TexSubImage2D(p []byte, x, y, width, height int) {
theContext.texSubImage2D(i.Texture, p, x, y, width, height) theContext.texSubImage2D(i.textureNative, p, x, y, width, height)
} }

View File

@ -267,11 +267,11 @@ func BufferSubData(vertices []float32, indices []uint16) {
} }
func UseProgram(proj []float32, src *Image, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) { func UseProgram(proj []float32, src *Image, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) {
theOpenGLState.useProgram(proj, src.Texture, dstW, dstH, srcW, srcH, colorM, filter) theOpenGLState.useProgram(proj, src.textureNative, dstW, dstH, srcW, srcH, colorM, filter)
} }
// useProgram uses the program (programTexture). // useProgram uses the program (programTexture).
func (s *openGLState) useProgram(proj []float32, texture Texture, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) { func (s *openGLState) useProgram(proj []float32, texture textureNative, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) {
c := GetContext() c := GetContext()
var program program var program program