opengl: Refactoring: Remove theOpenGLState

This commit is contained in:
Hajime Hoshi 2018-11-07 01:59:14 +09:00
parent 09c8516545
commit b396b4f88a
3 changed files with 19 additions and 23 deletions

View File

@ -27,10 +27,12 @@ func GetDriver() *Driver {
} }
type Driver struct { type Driver struct {
state openGLState
} }
func (d *Driver) NewImage(width, height int) (*Image, error) { func (d *Driver) NewImage(width, height int) (*Image, error) {
i := &Image{ i := &Image{
driver: d,
width: width, width: width,
height: height, height: height,
} }
@ -48,6 +50,7 @@ func (d *Driver) NewImage(width, height int) (*Image, error) {
func (d *Driver) NewScreenFramebufferImage(width, height int) *Image { func (d *Driver) NewScreenFramebufferImage(width, height int) *Image {
checkSize(width, height) checkSize(width, height)
i := &Image{ i := &Image{
driver: d,
width: width, width: width,
height: height, height: height,
} }
@ -60,7 +63,7 @@ func (d *Driver) NewScreenFramebufferImage(width, height int) *Image {
// Reset resets or initializes the current OpenGL state. // Reset resets or initializes the current OpenGL state.
func (d *Driver) Reset() error { func (d *Driver) Reset() error {
return theOpenGLState.reset() return d.state.reset()
} }
func (d *Driver) BufferSubData(vertices []float32, indices []uint16) { func (d *Driver) BufferSubData(vertices []float32, indices []uint16) {
@ -68,7 +71,7 @@ func (d *Driver) BufferSubData(vertices []float32, indices []uint16) {
} }
func (d *Driver) UseProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error { func (d *Driver) UseProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error {
return useProgram(mode, colorM, filter) return d.state.useProgram(mode, colorM, filter)
} }
func (d *Driver) DrawElements(len int, offsetInBytes int) { func (d *Driver) DrawElements(len int, offsetInBytes int) {

View File

@ -37,6 +37,7 @@ func checkSize(width, height int) {
} }
type Image struct { type Image struct {
driver *Driver
textureNative textureNative textureNative textureNative
framebuffer *framebuffer framebuffer *framebuffer
width int width int
@ -57,7 +58,7 @@ func (i *Image) Delete() {
} }
func (i *Image) SetAsDestination() { func (i *Image) SetAsDestination() {
theOpenGLState.destination = i i.driver.state.destination = i
} }
func (i *Image) setViewport() error { func (i *Image) setViewport() error {
@ -104,5 +105,5 @@ func (i *Image) TexSubImage2D(p []byte, x, y, width, height int) {
} }
func (i *Image) SetAsSource() { func (i *Image) SetAsSource() {
theOpenGLState.source = i i.driver.state.source = i
} }

View File

@ -136,9 +136,6 @@ type openGLState struct {
} }
var ( var (
// theOpenGLState is the OpenGL state in the current process.
theOpenGLState openGLState
zeroBuffer buffer zeroBuffer buffer
zeroProgram program zeroProgram program
) )
@ -261,12 +258,13 @@ func bufferSubData(vertices []float32, indices []uint16) {
theContext.elementArrayBufferSubData(indices) theContext.elementArrayBufferSubData(indices)
} }
func useProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error { // useProgram uses the program (programTexture).
destination := theOpenGLState.destination func (s *openGLState) useProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error {
destination := s.destination
if destination == nil { if destination == nil {
panic("destination image is not set") panic("destination image is not set")
} }
source := theOpenGLState.source source := s.source
if source == nil { if source == nil {
panic("source image is not set") panic("source image is not set")
} }
@ -278,20 +276,10 @@ func useProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graph
return err return err
} }
proj := destination.projectionMatrix() proj := destination.projectionMatrix()
dw, dh := destination.width, destination.height dstW := destination.width
sw, sh := source.width, source.height srcW, srcH := source.width, source.height
theContext.blendFunc(mode) theContext.blendFunc(mode)
theOpenGLState.useProgram(proj, dw, dh, sw, sh, colorM, filter)
theOpenGLState.source = nil
theOpenGLState.destination = nil
return nil
}
// useProgram uses the program (programTexture).
func (s *openGLState) useProgram(proj []float32, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) {
texture := theOpenGLState.source.textureNative
var program program var program program
switch filter { switch filter {
@ -371,5 +359,9 @@ func (s *openGLState) useProgram(proj []float32, dstW, dstH, srcW, srcH int, col
// We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture // 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 // See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml
theContext.bindTexture(texture) theContext.bindTexture(source.textureNative)
s.source = nil
s.destination = nil
return nil
} }