graphicsdriver/opengl: Use a new package gles

Updates #1387
This commit is contained in:
Hajime Hoshi 2020-10-18 18:09:10 +09:00
parent a781391752
commit 55af18a178
2 changed files with 101 additions and 45 deletions

View File

@ -20,9 +20,11 @@ import (
"errors" "errors"
"fmt" "fmt"
// TODO: Remove this dependency (#1387)
mgl "golang.org/x/mobile/gl" mgl "golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
) )
@ -78,21 +80,21 @@ func getProgramID(p program) programID {
} }
const ( const (
vertexShader = shaderType(mgl.VERTEX_SHADER) vertexShader = shaderType(gles.VERTEX_SHADER)
fragmentShader = shaderType(mgl.FRAGMENT_SHADER) fragmentShader = shaderType(gles.FRAGMENT_SHADER)
arrayBuffer = bufferType(mgl.ARRAY_BUFFER) arrayBuffer = bufferType(gles.ARRAY_BUFFER)
elementArrayBuffer = bufferType(mgl.ELEMENT_ARRAY_BUFFER) elementArrayBuffer = bufferType(gles.ELEMENT_ARRAY_BUFFER)
dynamicDraw = bufferUsage(mgl.DYNAMIC_DRAW) dynamicDraw = bufferUsage(gles.DYNAMIC_DRAW)
short = dataType(mgl.SHORT) short = dataType(gles.SHORT)
float = dataType(mgl.FLOAT) float = dataType(gles.FLOAT)
zero = operation(mgl.ZERO) zero = operation(gles.ZERO)
one = operation(mgl.ONE) one = operation(gles.ONE)
srcAlpha = operation(mgl.SRC_ALPHA) srcAlpha = operation(gles.SRC_ALPHA)
dstAlpha = operation(mgl.DST_ALPHA) dstAlpha = operation(gles.DST_ALPHA)
oneMinusSrcAlpha = operation(mgl.ONE_MINUS_SRC_ALPHA) oneMinusSrcAlpha = operation(gles.ONE_MINUS_SRC_ALPHA)
oneMinusDstAlpha = operation(mgl.ONE_MINUS_DST_ALPHA) oneMinusDstAlpha = operation(gles.ONE_MINUS_DST_ALPHA)
dstColor = operation(mgl.DST_COLOR) dstColor = operation(gles.DST_COLOR)
) )
type contextImpl struct { type contextImpl struct {
@ -106,9 +108,9 @@ func (c *context) reset() error {
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
c.lastCompositeMode = driver.CompositeModeUnknown c.lastCompositeMode = driver.CompositeModeUnknown
c.gl.Enable(mgl.BLEND) c.gl.Enable(gles.BLEND)
c.blendFunc(driver.CompositeModeSourceOver) c.blendFunc(driver.CompositeModeSourceOver)
f := c.gl.GetInteger(mgl.FRAMEBUFFER_BINDING) f := c.gl.GetInteger(gles.FRAMEBUFFER_BINDING)
c.screenFramebuffer = framebufferNative(mgl.Framebuffer{uint32(f)}) c.screenFramebuffer = framebufferNative(mgl.Framebuffer{uint32(f)})
// TODO: Need to update screenFramebufferWidth/Height? // TODO: Need to update screenFramebufferWidth/Height?
return nil return nil
@ -131,21 +133,21 @@ func (c *context) newTexture(width, height int) (textureNative, error) {
if t.Value <= 0 { if t.Value <= 0 {
return textureNative{}, errors.New("opengl: creating texture failed") return textureNative{}, errors.New("opengl: creating texture failed")
} }
gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gles.UNPACK_ALIGNMENT, 4)
c.bindTexture(textureNative(t)) c.bindTexture(textureNative(t))
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, mgl.NEAREST) gl.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_MAG_FILTER, gles.NEAREST)
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, mgl.NEAREST) gl.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_MIN_FILTER, gles.NEAREST)
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_WRAP_S, mgl.CLAMP_TO_EDGE) gl.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_WRAP_S, gles.CLAMP_TO_EDGE)
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_WRAP_T, mgl.CLAMP_TO_EDGE) gl.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_WRAP_T, gles.CLAMP_TO_EDGE)
gl.TexImage2D(mgl.TEXTURE_2D, 0, mgl.RGBA, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, nil) gl.TexImage2D(gles.TEXTURE_2D, 0, gles.RGBA, width, height, gles.RGBA, gles.UNSIGNED_BYTE, nil)
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(gles.FRAMEBUFFER, mgl.Framebuffer(f))
} }
func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte { func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte {
@ -155,18 +157,18 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte {
c.bindFramebuffer(f.native) c.bindFramebuffer(f.native)
pixels := make([]byte, 4*width*height) pixels := make([]byte, 4*width*height)
gl.ReadPixels(pixels, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE) gl.ReadPixels(pixels, 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE)
return pixels return pixels
} }
func (c *context) activeTexture(idx int) { func (c *context) activeTexture(idx int) {
gl := c.gl gl := c.gl
gl.ActiveTexture(mgl.Enum(mgl.TEXTURE0 + idx)) gl.ActiveTexture(mgl.Enum(gles.TEXTURE0 + idx))
} }
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(gles.TEXTURE_2D, mgl.Texture(t))
} }
func (c *context) deleteTexture(t textureNative) { func (c *context) deleteTexture(t textureNative) {
@ -193,13 +195,13 @@ func (c *context) newFramebuffer(texture textureNative) (framebufferNative, erro
} }
c.bindFramebuffer(framebufferNative(f)) c.bindFramebuffer(framebufferNative(f))
gl.FramebufferTexture2D(mgl.FRAMEBUFFER, mgl.COLOR_ATTACHMENT0, mgl.TEXTURE_2D, mgl.Texture(texture), 0) gl.FramebufferTexture2D(gles.FRAMEBUFFER, gles.COLOR_ATTACHMENT0, gles.TEXTURE_2D, mgl.Texture(texture), 0)
s := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER) s := gl.CheckFramebufferStatus(gles.FRAMEBUFFER)
if s != mgl.FRAMEBUFFER_COMPLETE { if s != gles.FRAMEBUFFER_COMPLETE {
if s != 0 { if s != 0 {
return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: %v", s) return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: %v", s)
} }
if e := gl.GetError(); e != mgl.NO_ERROR { if e := gl.GetError(); e != gles.NO_ERROR {
return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e) return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
} }
return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: unknown error") return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: unknown error")
@ -237,8 +239,8 @@ func (c *context) newShader(shaderType shaderType, source string) (shader, error
gl.ShaderSource(s, source) gl.ShaderSource(s, source)
gl.CompileShader(s) gl.CompileShader(s)
v := gl.GetShaderi(s, mgl.COMPILE_STATUS) v := gl.GetShaderi(s, gles.COMPILE_STATUS)
if v == mgl.FALSE { if v == gles.FALSE {
log := gl.GetShaderInfoLog(s) log := gl.GetShaderInfoLog(s)
return shader{}, fmt.Errorf("opengl: shader compile failed: %s", log) return shader{}, fmt.Errorf("opengl: shader compile failed: %s", log)
} }
@ -266,8 +268,8 @@ func (c *context) newProgram(shaders []shader, attributes []string) (program, er
} }
gl.LinkProgram(p) gl.LinkProgram(p)
v := gl.GetProgrami(p, mgl.LINK_STATUS) v := gl.GetProgrami(p, gles.LINK_STATUS)
if v == mgl.FALSE { if v == gles.FALSE {
info := gl.GetProgramInfoLog(p) info := gl.GetProgramInfoLog(p)
return program{}, fmt.Errorf("opengl: program error: %s", info) return program{}, fmt.Errorf("opengl: program error: %s", info)
} }
@ -399,17 +401,17 @@ func (c *context) deleteBuffer(b buffer) {
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(gles.TRIANGLES, len, gles.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(gles.MAX_TEXTURE_SIZE)
} }
func (c *context) getShaderPrecisionFormatPrecision() int { func (c *context) getShaderPrecisionFormatPrecision() int {
gl := c.gl gl := c.gl
_, _, p := gl.GetShaderPrecisionFormat(mgl.FRAGMENT_SHADER, mgl.HIGH_FLOAT) _, _, p := gl.GetShaderPrecisionFormat(gles.FRAGMENT_SHADER, gles.HIGH_FLOAT)
return p return p
} }
@ -432,16 +434,16 @@ func (c *context) texSubImage2D(t textureNative, width, height int, args []*driv
c.bindTexture(t) c.bindTexture(t)
gl := c.gl gl := c.gl
for _, a := range args { for _, a := range args {
gl.TexSubImage2D(mgl.TEXTURE_2D, 0, a.X, a.Y, a.Width, a.Height, mgl.RGBA, mgl.UNSIGNED_BYTE, a.Pixels) gl.TexSubImage2D(gles.TEXTURE_2D, 0, a.X, a.Y, a.Width, a.Height, gles.RGBA, gles.UNSIGNED_BYTE, a.Pixels)
} }
} }
func (c *context) newPixelBufferObject(width, height int) buffer { func (c *context) newPixelBufferObject(width, height int) buffer {
gl := c.gl gl := c.gl
b := gl.CreateBuffer() b := gl.CreateBuffer()
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, b) gl.BindBuffer(gles.PIXEL_UNPACK_BUFFER, b)
gl.BufferInit(mgl.PIXEL_UNPACK_BUFFER, 4*width*height, mgl.STREAM_DRAW) gl.BufferInit(gles.PIXEL_UNPACK_BUFFER, 4*width*height, gles.STREAM_DRAW)
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, mgl.Buffer{0}) gl.BindBuffer(gles.PIXEL_UNPACK_BUFFER, mgl.Buffer{0})
return buffer(b) return buffer(b)
} }
@ -450,16 +452,16 @@ func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, he
c.bindTexture(t) c.bindTexture(t)
gl := c.gl gl := c.gl
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, mgl.Buffer(buffer)) gl.BindBuffer(gles.PIXEL_UNPACK_BUFFER, mgl.Buffer(buffer))
stride := 4 * width stride := 4 * width
for _, a := range args { for _, a := range args {
offset := 4 * (a.Y*width + a.X) offset := 4 * (a.Y*width + a.X)
for j := 0; j < a.Height; j++ { for j := 0; j < a.Height; j++ {
gl.BufferSubData(mgl.PIXEL_UNPACK_BUFFER, offset+stride*j, a.Pixels[4*a.Width*j:4*a.Width*(j+1)]) gl.BufferSubData(gles.PIXEL_UNPACK_BUFFER, offset+stride*j, a.Pixels[4*a.Width*j:4*a.Width*(j+1)])
} }
} }
gl.TexSubImage2D(mgl.TEXTURE_2D, 0, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, nil) gl.TexSubImage2D(gles.TEXTURE_2D, 0, 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, nil)
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, mgl.Buffer{0}) gl.BindBuffer(gles.PIXEL_UNPACK_BUFFER, mgl.Buffer{0})
} }

View File

@ -0,0 +1,54 @@
// +build android ios
// Package gles implements Go bindings to OpenGL ES.
package gles
const (
VERTEX_SHADER = 0x8B31
FRAGMENT_SHADER = 0x8B30
ARRAY_BUFFER = 0x8892
ELEMENT_ARRAY_BUFFER = 0x8893
DYNAMIC_DRAW = 0x88E8
STREAM_DRAW = 0x88E0
PIXEL_UNPACK_BUFFER = 0x88EC
SHORT = 0x1402
FLOAT = 0x1406
ZERO = 0
ONE = 1
SRC_ALPHA = 0x0302
DST_ALPHA = 0x0304
ONE_MINUS_SRC_ALPHA = 0x0303
ONE_MINUS_DST_ALPHA = 0x0305
DST_COLOR = 0x0306
FALSE = 0
TRUE = 1
BLEND = 0x0BE2
CLAMP_TO_EDGE = 0x812F
COLOR_ATTACHMENT0 = 0x8CE0
COMPILE_STATUS = 0x8B81
FRAMEBUFFER = 0x8D40
FRAMEBUFFER_BINDING = 0x8CA6
FRAMEBUFFER_COMPLETE = 0x8CD5
HIGH_FLOAT = 0x8DF2
INFO_LOG_LENGTH = 0x8B84
LINK_STATUS = 0x8B82
MAX_TEXTURE_SIZE = 0x0D33
NEAREST = 0x2600
NO_ERROR = 0
READ_WRITE = 0x88BA
RGBA = 0x1908
TEXTURE0 = 0x84C0
TEXTURE_2D = 0x0DE1
TEXTURE_MAG_FILTER = 0x2800
TEXTURE_MIN_FILTER = 0x2801
TEXTURE_WRAP_S = 0x2802
TEXTURE_WRAP_T = 0x2803
TRIANGLES = 0x0004
UNPACK_ALIGNMENT = 0x0CF5
UNSIGNED_BYTE = 0x1401
UNSIGNED_SHORT = 0x1403
WRITE_ONLY = 0x88B9
)