opengl: Refactoring: Hide projection matrix inside opengl package

This commit is contained in:
Hajime Hoshi 2018-11-06 03:44:43 +09:00
parent 57c8c089e9
commit ed961505d2
3 changed files with 48 additions and 16 deletions

View File

@ -227,20 +227,16 @@ func (c *drawImageCommand) String() string {
// Exec executes the drawImageCommand. // Exec executes the drawImageCommand.
func (c *drawImageCommand) Exec(indexOffsetInBytes int) error { func (c *drawImageCommand) Exec(indexOffsetInBytes int) error {
// On some environments, viewport size must be within the framebuffer size. // TODO: Is it ok not to bind any framebuffer here?
// e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691).
// Use the same size of the framebuffer here.
if err := c.dst.image.SetViewport(); err != nil {
return err
}
if c.nindices == 0 { if c.nindices == 0 {
return nil return nil
} }
proj := c.dst.image.ProjectionMatrix()
dw, dh := c.dst.Size() c.dst.image.SetAsDestination()
sw, sh := c.src.Size() c.src.image.SetAsSource()
opengl.UseProgram(c.mode, proj, c.src.image, dw, dh, sw, sh, c.color, c.filter) if err := opengl.UseProgram(c.mode, c.color, c.filter); err != nil {
return err
}
opengl.GetContext().DrawElements(c.nindices, indexOffsetInBytes) opengl.GetContext().DrawElements(c.nindices, indexOffsetInBytes)
// glFlush() might be necessary at least on MacBook Pro (a smilar problem at #419), // glFlush() might be necessary at least on MacBook Pro (a smilar problem at #419),

View File

@ -85,7 +85,11 @@ func (i *Image) Delete() {
} }
} }
func (i *Image) SetViewport() error { func (i *Image) SetAsDestination() {
theOpenGLState.destination = i
}
func (i *Image) setViewport() error {
if err := i.ensureFramebuffer(); err != nil { if err := i.ensureFramebuffer(); err != nil {
return err return err
} }
@ -104,7 +108,7 @@ func (i *Image) Pixels() ([]byte, error) {
return p, nil return p, nil
} }
func (i *Image) ProjectionMatrix() []float32 { func (i *Image) projectionMatrix() []float32 {
if i.framebuffer == nil { if i.framebuffer == nil {
panic("not reached") panic("not reached")
} }
@ -127,3 +131,7 @@ 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.textureNative, p, x, y, width, height) theContext.texSubImage2D(i.textureNative, p, x, y, width, height)
} }
func (i *Image) SetAsSource() {
theOpenGLState.source = i
}

View File

@ -130,6 +130,9 @@ type openGLState struct {
lastColorMatrixTranslation []float32 lastColorMatrixTranslation []float32
lastSourceWidth int lastSourceWidth int
lastSourceHeight int lastSourceHeight int
source *Image
destination *Image
} }
var ( var (
@ -265,15 +268,40 @@ func BufferSubData(vertices []float32, indices []uint16) {
c.elementArrayBufferSubData(indices) c.elementArrayBufferSubData(indices)
} }
func UseProgram(mode graphics.CompositeMode, proj []float32, src *Image, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) { func UseProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error {
destination := theOpenGLState.destination
if destination == nil {
panic("destination image is not set")
}
source := theOpenGLState.source
if source == nil {
panic("source image is not set")
}
// On some environments, viewport size must be within the framebuffer size.
// e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691).
// Use the same size of the framebuffer here.
if err := destination.setViewport(); err != nil {
return err
}
proj := destination.projectionMatrix()
dw, dh := destination.width, destination.height
sw, sh := source.width, source.height
GetContext().blendFunc(mode) GetContext().blendFunc(mode)
theOpenGLState.useProgram(proj, src.textureNative, dstW, dstH, srcW, srcH, colorM, filter) theOpenGLState.useProgram(proj, dw, dh, sw, sh, colorM, filter)
theOpenGLState.source = nil
theOpenGLState.destination = nil
return nil
} }
// useProgram uses the program (programTexture). // useProgram uses the program (programTexture).
func (s *openGLState) useProgram(proj []float32, texture textureNative, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) { func (s *openGLState) useProgram(proj []float32, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) {
c := GetContext() c := GetContext()
texture := theOpenGLState.source.textureNative
var program program var program program
switch filter { switch filter {
case graphics.FilterNearest: case graphics.FilterNearest: