graphicsdriver/opengl: Remove theContext

This commit is contained in:
Hajime Hoshi 2018-11-11 03:37:37 +09:00
parent 2f692d98c7
commit 734aeabc8d
5 changed files with 122 additions and 122 deletions

View File

@ -67,8 +67,6 @@ type context struct {
contextImpl
}
var theContext context
func (c *context) bindTexture(t textureNative) {
if c.lastTexture == t {
return

View File

@ -15,6 +15,8 @@
package opengl
import (
"fmt"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver"
@ -28,7 +30,24 @@ func GetDriver() *Driver {
}
type Driver struct {
state openGLState
state openGLState
context context
}
func (d *Driver) checkSize(width, height int) {
if width < 1 {
panic(fmt.Sprintf("opengl: width (%d) must be equal or more than 1.", width))
}
if height < 1 {
panic(fmt.Sprintf("opengl: height (%d) must be equal or more than 1.", height))
}
m := d.context.getMaxTextureSize()
if width > m {
panic(fmt.Sprintf("opengl: width (%d) must be less than or equal to %d", width, m))
}
if height > m {
panic(fmt.Sprintf("opengl: height (%d) must be less than or equal to %d", height, m))
}
}
func (d *Driver) NewImage(width, height int) (graphicsdriver.Image, error) {
@ -39,8 +58,8 @@ func (d *Driver) NewImage(width, height int) (graphicsdriver.Image, error) {
}
w := math.NextPowerOf2Int(width)
h := math.NextPowerOf2Int(height)
checkSize(w, h)
t, err := theContext.newTexture(w, h)
d.checkSize(w, h)
t, err := d.context.newTexture(w, h)
if err != nil {
return nil, err
}
@ -49,7 +68,7 @@ func (d *Driver) NewImage(width, height int) (graphicsdriver.Image, error) {
}
func (d *Driver) NewScreenFramebufferImage(width, height int) graphicsdriver.Image {
checkSize(width, height)
d.checkSize(width, height)
i := &Image{
driver: d,
width: width,
@ -58,25 +77,25 @@ func (d *Driver) NewScreenFramebufferImage(width, height int) graphicsdriver.Ima
// The (default) framebuffer size can't be converted to a power of 2.
// On browsers, c.width and c.height are used as viewport size and
// Edge can't treat a bigger viewport than the drawing area (#71).
i.framebuffer = newScreenFramebuffer(width, height)
i.framebuffer = newScreenFramebuffer(&d.context, width, height)
return i
}
// Reset resets or initializes the current OpenGL state.
func (d *Driver) Reset() error {
return d.state.reset()
return d.state.reset(&d.context)
}
func (d *Driver) BufferSubData(vertices []float32, indices []uint16) {
bufferSubData(vertices, indices)
bufferSubData(&d.context, vertices, indices)
}
func (d *Driver) UseProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error {
return d.state.useProgram(mode, colorM, filter)
return d.useProgram(mode, colorM, filter)
}
func (d *Driver) DrawElements(len int, offsetInBytes int) {
theContext.drawElements(len, offsetInBytes)
d.context.drawElements(len, offsetInBytes)
// glFlush() might be necessary at least on MacBook Pro (a smilar problem at #419),
// but basically this pass the tests (esp. TestImageTooManyFill).
// As glFlush() causes performance problems, this should be avoided as much as possible.
@ -84,9 +103,9 @@ func (d *Driver) DrawElements(len int, offsetInBytes int) {
}
func (d *Driver) Flush() {
theContext.flush()
d.context.flush()
}
func (d *Driver) MaxImageSize() int {
return theContext.getMaxTextureSize()
return d.context.getMaxTextureSize()
}

View File

@ -16,6 +16,7 @@ package opengl
// framebuffer is a wrapper of OpenGL's framebuffer.
type framebuffer struct {
driver *Driver
native framebufferNative
proMatrix []float32
width int
@ -23,8 +24,8 @@ type framebuffer struct {
}
// newFramebufferFromTexture creates a framebuffer from the given texture.
func newFramebufferFromTexture(texture textureNative, width, height int) (*framebuffer, error) {
native, err := theContext.newFramebuffer(texture)
func newFramebufferFromTexture(context *context, texture textureNative, width, height int) (*framebuffer, error) {
native, err := context.newFramebuffer(texture)
if err != nil {
return nil, err
}
@ -36,9 +37,9 @@ func newFramebufferFromTexture(texture textureNative, width, height int) (*frame
}
// newScreenFramebuffer creates a framebuffer for the screen.
func newScreenFramebuffer(width, height int) *framebuffer {
func newScreenFramebuffer(context *context, width, height int) *framebuffer {
return &framebuffer{
native: theContext.getScreenFramebuffer(),
native: context.getScreenFramebuffer(),
width: width,
height: height,
}
@ -57,8 +58,8 @@ func (f *framebuffer) projectionMatrix() []float32 {
return f.proMatrix
}
func (f *framebuffer) delete() {
if f.native != theContext.getScreenFramebuffer() {
theContext.deleteFramebuffer(f.native)
func (f *framebuffer) delete(context *context) {
if f.native != context.getScreenFramebuffer() {
context.deleteFramebuffer(f.native)
}
}

View File

@ -15,27 +15,9 @@
package opengl
import (
"fmt"
"github.com/hajimehoshi/ebiten/internal/math"
)
func checkSize(width, height int) {
if width < 1 {
panic(fmt.Sprintf("opengl: width (%d) must be equal or more than 1.", width))
}
if height < 1 {
panic(fmt.Sprintf("opengl: height (%d) must be equal or more than 1.", height))
}
m := theContext.getMaxTextureSize()
if width > m {
panic(fmt.Sprintf("opengl: width (%d) must be less than or equal to %d", width, m))
}
if height > m {
panic(fmt.Sprintf("opengl: height (%d) must be less than or equal to %d", height, m))
}
}
type Image struct {
driver *Driver
textureNative textureNative
@ -45,15 +27,15 @@ type Image struct {
}
func (i *Image) IsInvalidated() bool {
return !theContext.isTexture(i.textureNative)
return !i.driver.context.isTexture(i.textureNative)
}
func (i *Image) Delete() {
if i.framebuffer != nil {
i.framebuffer.delete()
i.framebuffer.delete(&i.driver.context)
}
if i.textureNative != *new(textureNative) {
theContext.deleteTexture(i.textureNative)
i.driver.context.deleteTexture(i.textureNative)
}
}
@ -65,7 +47,7 @@ func (i *Image) setViewport() error {
if err := i.ensureFramebuffer(); err != nil {
return err
}
theContext.setViewport(i.framebuffer)
i.driver.context.setViewport(i.framebuffer)
return nil
}
@ -73,7 +55,7 @@ func (i *Image) Pixels() ([]byte, error) {
if err := i.ensureFramebuffer(); err != nil {
return nil, err
}
p, err := theContext.framebufferPixels(i.framebuffer, i.width, i.height)
p, err := i.driver.context.framebufferPixels(i.framebuffer, i.width, i.height)
if err != nil {
return nil, err
}
@ -92,7 +74,7 @@ func (i *Image) ensureFramebuffer() error {
return nil
}
w, h := i.width, i.height
f, err := newFramebufferFromTexture(i.textureNative, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
f, err := newFramebufferFromTexture(&i.driver.context, i.textureNative, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
if err != nil {
return err
}
@ -103,8 +85,8 @@ func (i *Image) ensureFramebuffer() error {
func (i *Image) ReplacePixels(p []byte, x, y, width, height int) {
// glFlush is necessary on Android.
// glTexSubImage2D didn't work without this hack at least on Nexus 5x and NuAns NEO [Reloaded] (#211).
theContext.flush()
theContext.texSubImage2D(i.textureNative, p, x, y, width, height)
i.driver.context.flush()
i.driver.context.texSubImage2D(i.textureNative, p, x, y, width, height)
}
func (i *Image) SetAsSource() {

View File

@ -53,28 +53,28 @@ func (a *arrayBufferLayout) totalBytes() int {
}
// newArrayBuffer creates OpenGL's buffer object for the array buffer.
func (a *arrayBufferLayout) newArrayBuffer() buffer {
return theContext.newArrayBuffer(a.totalBytes() * graphics.IndicesNum)
func (a *arrayBufferLayout) newArrayBuffer(context *context) buffer {
return context.newArrayBuffer(a.totalBytes() * graphics.IndicesNum)
}
// enable binds the array buffer the given program to use the array buffer.
func (a *arrayBufferLayout) enable(program program) {
func (a *arrayBufferLayout) enable(context *context, program program) {
for _, p := range a.parts {
theContext.enableVertexAttribArray(program, p.name)
context.enableVertexAttribArray(program, p.name)
}
total := a.totalBytes()
offset := 0
for _, p := range a.parts {
theContext.vertexAttribPointer(program, p.name, p.num, float, total, offset)
context.vertexAttribPointer(program, p.name, p.num, float, total, offset)
offset += float.SizeInBytes() * p.num
}
}
// disable stops using the array buffer.
func (a *arrayBufferLayout) disable(program program) {
func (a *arrayBufferLayout) disable(context *context, program program) {
// TODO: Disabling should be done in reversed order?
for _, p := range a.parts {
theContext.disableVertexAttribArray(program, p.name)
context.disableVertexAttribArray(program, p.name)
}
}
@ -146,8 +146,8 @@ const (
)
// reset resets or initializes the OpenGL state.
func (s *openGLState) reset() error {
if err := theContext.reset(); err != nil {
func (s *openGLState) reset(context *context) error {
if err := context.reset(); err != nil {
return err
}
@ -162,51 +162,51 @@ func (s *openGLState) reset() error {
// However, it is not assumed that reset is called only when context lost happens.
// Let's delete them explicitly.
if s.programNearest != zeroProgram {
theContext.deleteProgram(s.programNearest)
context.deleteProgram(s.programNearest)
}
if s.programLinear != zeroProgram {
theContext.deleteProgram(s.programLinear)
context.deleteProgram(s.programLinear)
}
if s.programScreen != zeroProgram {
theContext.deleteProgram(s.programScreen)
context.deleteProgram(s.programScreen)
}
// On browsers (at least Chrome), buffers are already detached from the context
// and must not be deleted by DeleteBuffer.
if !web.IsBrowser() {
if s.arrayBuffer != zeroBuffer {
theContext.deleteBuffer(s.arrayBuffer)
context.deleteBuffer(s.arrayBuffer)
}
if s.elementArrayBuffer != zeroBuffer {
theContext.deleteBuffer(s.elementArrayBuffer)
context.deleteBuffer(s.elementArrayBuffer)
}
}
shaderVertexModelviewNative, err := theContext.newShader(vertexShader, shaderStr(shaderVertexModelview))
shaderVertexModelviewNative, err := context.newShader(vertexShader, shaderStr(shaderVertexModelview))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
}
defer theContext.deleteShader(shaderVertexModelviewNative)
defer context.deleteShader(shaderVertexModelviewNative)
shaderFragmentNearestNative, err := theContext.newShader(fragmentShader, shaderStr(shaderFragmentNearest))
shaderFragmentNearestNative, err := context.newShader(fragmentShader, shaderStr(shaderFragmentNearest))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
}
defer theContext.deleteShader(shaderFragmentNearestNative)
defer context.deleteShader(shaderFragmentNearestNative)
shaderFragmentLinearNative, err := theContext.newShader(fragmentShader, shaderStr(shaderFragmentLinear))
shaderFragmentLinearNative, err := context.newShader(fragmentShader, shaderStr(shaderFragmentLinear))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
}
defer theContext.deleteShader(shaderFragmentLinearNative)
defer context.deleteShader(shaderFragmentLinearNative)
shaderFragmentScreenNative, err := theContext.newShader(fragmentShader, shaderStr(shaderFragmentScreen))
shaderFragmentScreenNative, err := context.newShader(fragmentShader, shaderStr(shaderFragmentScreen))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
}
defer theContext.deleteShader(shaderFragmentScreenNative)
defer context.deleteShader(shaderFragmentScreenNative)
s.programNearest, err = theContext.newProgram([]shader{
s.programNearest, err = context.newProgram([]shader{
shaderVertexModelviewNative,
shaderFragmentNearestNative,
})
@ -214,7 +214,7 @@ func (s *openGLState) reset() error {
return err
}
s.programLinear, err = theContext.newProgram([]shader{
s.programLinear, err = context.newProgram([]shader{
shaderVertexModelviewNative,
shaderFragmentLinearNative,
})
@ -222,7 +222,7 @@ func (s *openGLState) reset() error {
return err
}
s.programScreen, err = theContext.newProgram([]shader{
s.programScreen, err = context.newProgram([]shader{
shaderVertexModelviewNative,
shaderFragmentScreenNative,
})
@ -230,12 +230,12 @@ func (s *openGLState) reset() error {
return err
}
s.arrayBuffer = theArrayBufferLayout.newArrayBuffer()
s.arrayBuffer = theArrayBufferLayout.newArrayBuffer(context)
// Note that the indices passed to NewElementArrayBuffer is not under GC management
// in opengl package due to unsafe-way.
// See NewElementArrayBuffer in context_mobile.go.
s.elementArrayBuffer = theContext.newElementArrayBuffer(graphics.IndicesNum * 2)
s.elementArrayBuffer = context.newElementArrayBuffer(graphics.IndicesNum * 2)
return nil
}
@ -253,18 +253,18 @@ func areSameFloat32Array(a, b []float32) bool {
return true
}
func bufferSubData(vertices []float32, indices []uint16) {
theContext.arrayBufferSubData(vertices)
theContext.elementArrayBufferSubData(indices)
func bufferSubData(context *context, vertices []float32, indices []uint16) {
context.arrayBufferSubData(vertices)
context.elementArrayBufferSubData(indices)
}
// useProgram uses the program (programTexture).
func (s *openGLState) useProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error {
destination := s.destination
func (d *Driver) useProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error {
destination := d.state.destination
if destination == nil {
panic("destination image is not set")
}
source := s.source
source := d.state.source
if source == nil {
panic("source image is not set")
}
@ -279,89 +279,89 @@ func (s *openGLState) useProgram(mode graphics.CompositeMode, colorM *affine.Col
dstW := destination.width
srcW, srcH := source.width, source.height
theContext.blendFunc(mode)
d.context.blendFunc(mode)
var program program
switch filter {
case graphics.FilterNearest:
program = s.programNearest
program = d.state.programNearest
case graphics.FilterLinear:
program = s.programLinear
program = d.state.programLinear
case graphics.FilterScreen:
program = s.programScreen
program = d.state.programScreen
default:
panic("not reached")
}
if s.lastProgram != program {
theContext.useProgram(program)
if s.lastProgram != zeroProgram {
theArrayBufferLayout.disable(s.lastProgram)
if d.state.lastProgram != program {
d.context.useProgram(program)
if d.state.lastProgram != zeroProgram {
theArrayBufferLayout.disable(&d.context, d.state.lastProgram)
}
theArrayBufferLayout.enable(program)
theArrayBufferLayout.enable(&d.context, program)
if s.lastProgram == zeroProgram {
theContext.bindBuffer(arrayBuffer, s.arrayBuffer)
theContext.bindBuffer(elementArrayBuffer, s.elementArrayBuffer)
theContext.uniformInt(program, "texture", 0)
if d.state.lastProgram == zeroProgram {
d.context.bindBuffer(arrayBuffer, d.state.arrayBuffer)
d.context.bindBuffer(elementArrayBuffer, d.state.elementArrayBuffer)
d.context.uniformInt(program, "texture", 0)
}
s.lastProgram = program
s.lastProjectionMatrix = nil
s.lastColorMatrix = nil
s.lastColorMatrixTranslation = nil
s.lastSourceWidth = 0
s.lastSourceHeight = 0
d.state.lastProgram = program
d.state.lastProjectionMatrix = nil
d.state.lastColorMatrix = nil
d.state.lastColorMatrixTranslation = nil
d.state.lastSourceWidth = 0
d.state.lastSourceHeight = 0
}
if !areSameFloat32Array(s.lastProjectionMatrix, proj) {
theContext.uniformFloats(program, "projection_matrix", proj)
if s.lastProjectionMatrix == nil {
s.lastProjectionMatrix = make([]float32, 16)
if !areSameFloat32Array(d.state.lastProjectionMatrix, proj) {
d.context.uniformFloats(program, "projection_matrix", proj)
if d.state.lastProjectionMatrix == nil {
d.state.lastProjectionMatrix = make([]float32, 16)
}
// (*framebuffer).projectionMatrix is always same for the same framebuffer.
// It's OK to hold the reference without copying.
s.lastProjectionMatrix = proj
d.state.lastProjectionMatrix = proj
}
esBody, esTranslate := colorM.UnsafeElements()
if !areSameFloat32Array(s.lastColorMatrix, esBody) {
theContext.uniformFloats(program, "color_matrix_body", esBody)
if s.lastColorMatrix == nil {
s.lastColorMatrix = make([]float32, 16)
if !areSameFloat32Array(d.state.lastColorMatrix, esBody) {
d.context.uniformFloats(program, "color_matrix_body", esBody)
if d.state.lastColorMatrix == nil {
d.state.lastColorMatrix = make([]float32, 16)
}
// ColorM's elements are immutable. It's OK to hold the reference without copying.
s.lastColorMatrix = esBody
d.state.lastColorMatrix = esBody
}
if !areSameFloat32Array(s.lastColorMatrixTranslation, esTranslate) {
theContext.uniformFloats(program, "color_matrix_translation", esTranslate)
if s.lastColorMatrixTranslation == nil {
s.lastColorMatrixTranslation = make([]float32, 4)
if !areSameFloat32Array(d.state.lastColorMatrixTranslation, esTranslate) {
d.context.uniformFloats(program, "color_matrix_translation", esTranslate)
if d.state.lastColorMatrixTranslation == nil {
d.state.lastColorMatrixTranslation = make([]float32, 4)
}
// ColorM's elements are immutable. It's OK to hold the reference without copying.
s.lastColorMatrixTranslation = esTranslate
d.state.lastColorMatrixTranslation = esTranslate
}
sw := emath.NextPowerOf2Int(srcW)
sh := emath.NextPowerOf2Int(srcH)
if s.lastSourceWidth != sw || s.lastSourceHeight != sh {
theContext.uniformFloats(program, "source_size", []float32{float32(sw), float32(sh)})
s.lastSourceWidth = sw
s.lastSourceHeight = sh
if d.state.lastSourceWidth != sw || d.state.lastSourceHeight != sh {
d.context.uniformFloats(program, "source_size", []float32{float32(sw), float32(sh)})
d.state.lastSourceWidth = sw
d.state.lastSourceHeight = sh
}
if program == s.programScreen {
if program == d.state.programScreen {
scale := float32(dstW) / float32(srcW)
theContext.uniformFloat(program, "scale", scale)
d.context.uniformFloat(program, "scale", scale)
}
// We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture
// See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml
theContext.bindTexture(source.textureNative)
d.context.bindTexture(source.textureNative)
s.source = nil
s.destination = nil
d.state.source = nil
d.state.destination = nil
return nil
}