opengl: Change Texture/Framebuffer to js.Value

This commit is contained in:
Hajime Hoshi 2018-05-24 03:08:00 +09:00
parent 48e2c2aba9
commit 17ccf06755

View File

@ -26,12 +26,9 @@ import (
"github.com/hajimehoshi/ebiten/internal/web" "github.com/hajimehoshi/ebiten/internal/web"
) )
// Note that `type Texture *js.Object` doesn't work.
// There is no way to get the internal object in that case.
type ( type (
Texture interface{} Texture js.Value
Framebuffer interface{} Framebuffer js.Value
Shader interface{} Shader interface{}
Program interface{} Program interface{}
Buffer interface{} Buffer interface{}
@ -114,8 +111,8 @@ func Init() error {
func (c *Context) Reset() error { func (c *Context) Reset() error {
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = nil c.lastTexture = Texture(js.Null)
c.lastFramebuffer = nil c.lastFramebuffer = Framebuffer(js.Null)
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
c.lastCompositeMode = CompositeModeUnknown c.lastCompositeMode = CompositeModeUnknown
@ -123,7 +120,7 @@ func (c *Context) Reset() error {
gl.Call("enable", gl.Get("BLEND")) gl.Call("enable", gl.Get("BLEND"))
c.BlendFunc(CompositeModeSourceOver) c.BlendFunc(CompositeModeSourceOver)
f := gl.Call("getParameter", gl.Get("FRAMEBUFFER_BINDING")) f := gl.Call("getParameter", gl.Get("FRAMEBUFFER_BINDING"))
c.screenFramebuffer = f c.screenFramebuffer = Framebuffer(f)
return nil return nil
} }
@ -141,10 +138,10 @@ func (c *Context) NewTexture(width, height int) (Texture, error) {
gl := c.gl gl := c.gl
t := gl.Call("createTexture") t := gl.Call("createTexture")
if t == js.Null { if t == js.Null {
return nil, errors.New("opengl: glGenTexture failed") return Texture(js.Null), errors.New("opengl: glGenTexture failed")
} }
gl.Call("pixelStorei", gl.Get("UNPACK_ALIGNMENT"), 4) gl.Call("pixelStorei", gl.Get("UNPACK_ALIGNMENT"), 4)
c.BindTexture(t) c.BindTexture(Texture(t))
gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_MAG_FILTER"), gl.Get("NEAREST")) gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_MAG_FILTER"), gl.Get("NEAREST"))
gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_MIN_FILTER"), gl.Get("NEAREST")) gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_MIN_FILTER"), gl.Get("NEAREST"))
@ -156,12 +153,12 @@ func (c *Context) NewTexture(width, height int) (Texture, error) {
// GLenum type, ArrayBufferView? pixels); // GLenum type, ArrayBufferView? pixels);
gl.Call("texImage2D", gl.Get("TEXTURE_2D"), 0, gl.Get("RGBA"), width, height, 0, gl.Get("RGBA"), gl.Get("UNSIGNED_BYTE"), nil) gl.Call("texImage2D", gl.Get("TEXTURE_2D"), 0, gl.Get("RGBA"), width, height, 0, gl.Get("RGBA"), gl.Get("UNSIGNED_BYTE"), nil)
return t, nil return Texture(t), nil
} }
func (c *Context) bindFramebufferImpl(f Framebuffer) { func (c *Context) bindFramebufferImpl(f Framebuffer) {
gl := c.gl gl := c.gl
gl.Call("bindFramebuffer", gl.Get("FRAMEBUFFER"), f) gl.Call("bindFramebuffer", gl.Get("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) {
@ -179,23 +176,23 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]byte, e
func (c *Context) bindTextureImpl(t Texture) { func (c *Context) bindTextureImpl(t Texture) {
gl := c.gl gl := c.gl
gl.Call("bindTexture", gl.Get("TEXTURE_2D"), t) gl.Call("bindTexture", gl.Get("TEXTURE_2D"), js.Value(t))
} }
func (c *Context) DeleteTexture(t Texture) { func (c *Context) DeleteTexture(t Texture) {
gl := c.gl gl := c.gl
if !gl.Call("isTexture", t).Bool() { if !gl.Call("isTexture", js.Value(t)).Bool() {
return return
} }
if c.lastTexture == t { if c.lastTexture == t {
c.lastTexture = nil c.lastTexture = Texture(js.Null)
} }
gl.Call("deleteTexture", t) gl.Call("deleteTexture", js.Value(t))
} }
func (c *Context) IsTexture(t Texture) bool { func (c *Context) IsTexture(t Texture) bool {
gl := c.gl gl := c.gl
return gl.Call("isTexture", t).Bool() return gl.Call("isTexture", js.Value(t)).Bool()
} }
func (c *Context) TexSubImage2D(p []byte, x, y, width, height int) { func (c *Context) TexSubImage2D(p []byte, x, y, width, height int) {
@ -209,14 +206,14 @@ func (c *Context) TexSubImage2D(p []byte, x, y, width, height int) {
func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) { func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
gl := c.gl gl := c.gl
f := gl.Call("createFramebuffer") f := gl.Call("createFramebuffer")
c.bindFramebuffer(f) c.bindFramebuffer(Framebuffer(f))
gl.Call("framebufferTexture2D", gl.Get("FRAMEBUFFER"), gl.Get("COLOR_ATTACHMENT0"), gl.Get("TEXTURE_2D"), t, 0) gl.Call("framebufferTexture2D", gl.Get("FRAMEBUFFER"), gl.Get("COLOR_ATTACHMENT0"), gl.Get("TEXTURE_2D"), js.Value(t), 0)
if s := gl.Call("checkFramebufferStatus", gl.Get("FRAMEBUFFER")); s.Int() != gl.Get("FRAMEBUFFER_COMPLETE").Int() { if s := gl.Call("checkFramebufferStatus", gl.Get("FRAMEBUFFER")); s.Int() != gl.Get("FRAMEBUFFER_COMPLETE").Int() {
return nil, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s.Int())) return Framebuffer(js.Null), errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s.Int()))
} }
return f, nil return Framebuffer(f), nil
} }
func (c *Context) setViewportImpl(width, height int) { func (c *Context) setViewportImpl(width, height int) {
@ -226,18 +223,18 @@ func (c *Context) setViewportImpl(width, height int) {
func (c *Context) DeleteFramebuffer(f Framebuffer) { func (c *Context) DeleteFramebuffer(f Framebuffer) {
gl := c.gl gl := c.gl
if !gl.Call("isFramebuffer", f).Bool() { if !gl.Call("isFramebuffer", js.Value(f)).Bool() {
return return
} }
// If a framebuffer to be deleted is bound, a newly bound framebuffer // If a framebuffer to be deleted is bound, a newly bound framebuffer
// will be a default framebuffer. // will be a default framebuffer.
// https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml
if c.lastFramebuffer == f { if c.lastFramebuffer == f {
c.lastFramebuffer = nil c.lastFramebuffer = Framebuffer(js.Null)
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
} }
gl.Call("deleteFramebuffer", 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) {