opengl: Move members to variables

This commit is contained in:
Hajime Hoshi 2016-07-03 18:11:37 +09:00
parent fbc1a4238c
commit 86144a1cd6
10 changed files with 110 additions and 94 deletions

View File

@ -26,12 +26,12 @@ const (
FilterLinear // linear filter
)
func glFilter(c *opengl.Context, filter Filter) opengl.Filter {
func glFilter(filter Filter) opengl.Filter {
switch filter {
case FilterNearest:
return c.Nearest
return opengl.Nearest
case FilterLinear:
return c.Linear
return opengl.Linear
}
panic("not reach")
}

View File

@ -23,7 +23,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
"github.com/hajimehoshi/ebiten/internal/ui"
)
type images struct {
@ -79,7 +78,7 @@ func (i *images) restorePixels(context *opengl.Context) error {
}
}
for img := range i.images {
if err := img.restorePixels(context); err != nil {
if err := img.restorePixels(); err != nil {
return err
}
}
@ -210,7 +209,7 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
}
imageM.Lock()
defer imageM.Unlock()
image.image, err = graphics.NewImage(width, height, glFilter(ui.GLContext(), filter))
image.image, err = graphics.NewImage(width, height, glFilter(filter))
if err != nil {
return nil, err
}
@ -251,7 +250,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
imageM.Lock()
defer imageM.Unlock()
var err error
img.image, err = graphics.NewImageFromImage(rgbaImg, glFilter(ui.GLContext(), filter))
img.image, err = graphics.NewImageFromImage(rgbaImg, glFilter(filter))
if err != nil {
// TODO: texture should be removed here?
return nil, err

View File

@ -133,7 +133,7 @@ func (i *imageImpl) savePixels(context *opengl.Context) error {
return nil
}
func (i *imageImpl) restorePixels(context *opengl.Context) error {
func (i *imageImpl) restorePixels() error {
if i.screen {
return nil
}
@ -146,14 +146,14 @@ func (i *imageImpl) restorePixels(context *opengl.Context) error {
copy(img.Pix[j*img.Stride:], i.pixels[j*i.width*4:(j+1)*i.width*4])
}
var err error
i.image, err = graphics.NewImageFromImage(img, glFilter(context, i.filter))
i.image, err = graphics.NewImageFromImage(img, glFilter(i.filter))
if err != nil {
return err
}
return nil
}
var err error
i.image, err = graphics.NewImage(i.width, i.height, glFilter(context, i.filter))
i.image, err = graphics.NewImage(i.width, i.height, glFilter(i.filter))
if err != nil {
return err
}

View File

@ -52,7 +52,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error {
}
}
if 0 < len(vertices) {
context.BufferSubData(context.ArrayBuffer, vertices)
context.BufferSubData(opengl.ArrayBuffer, vertices)
}
// NOTE: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far.
// Let's use them to compare to len(quads) in the future.
@ -122,7 +122,7 @@ func (c *drawImageCommand) Exec(context *opengl.Context) error {
defer p.end()
// TODO: We should call glBindBuffer here?
// The buffer is already bound at begin() but it is counterintuitive.
context.DrawElements(context.Triangles, 6*n, theCommandQueue.indexOffsetInBytes)
context.DrawElements(opengl.Triangles, 6*n, theCommandQueue.indexOffsetInBytes)
theCommandQueue.indexOffsetInBytes += 6 * n * 2
return nil
}

View File

@ -14,7 +14,7 @@
package opengl
type Context struct {
var (
Nearest Filter
Linear Filter
VertexShader ShaderType
@ -25,12 +25,16 @@ type Context struct {
StaticDraw BufferUsage
Triangles Mode
Lines Mode
zero operation
one operation
srcAlpha operation
dstAlpha operation
oneMinusSrcAlpha operation
oneMinusDstAlpha operation
)
type Context struct {
locationCache *locationCache
screenFramebuffer Framebuffer // This might not be the default frame buffer '0' (e.g. iOS).
lastFramebuffer Framebuffer

View File

@ -43,28 +43,32 @@ func (p Program) id() programID {
return programID(p)
}
func init() {
Nearest = gl.NEAREST
Linear = gl.LINEAR
VertexShader = gl.VERTEX_SHADER
FragmentShader = gl.FRAGMENT_SHADER
ArrayBuffer = gl.ARRAY_BUFFER
ElementArrayBuffer = gl.ELEMENT_ARRAY_BUFFER
DynamicDraw = gl.DYNAMIC_DRAW
StaticDraw = gl.STATIC_DRAW
Triangles = gl.TRIANGLES
Lines = gl.LINES
zero = gl.ZERO
one = gl.ONE
srcAlpha = gl.SRC_ALPHA
dstAlpha = gl.DST_ALPHA
oneMinusSrcAlpha = gl.ONE_MINUS_SRC_ALPHA
oneMinusDstAlpha = gl.ONE_MINUS_DST_ALPHA
}
type context struct {
funcs chan func()
}
func NewContext() (*Context, error) {
c := &Context{
Nearest: gl.NEAREST,
Linear: gl.LINEAR,
VertexShader: gl.VERTEX_SHADER,
FragmentShader: gl.FRAGMENT_SHADER,
ArrayBuffer: gl.ARRAY_BUFFER,
ElementArrayBuffer: gl.ELEMENT_ARRAY_BUFFER,
DynamicDraw: gl.DYNAMIC_DRAW,
StaticDraw: gl.STATIC_DRAW,
Triangles: gl.TRIANGLES,
Lines: gl.LINES,
zero: gl.ZERO,
one: gl.ONE,
srcAlpha: gl.SRC_ALPHA,
dstAlpha: gl.DST_ALPHA,
oneMinusSrcAlpha: gl.ONE_MINUS_SRC_ALPHA,
oneMinusDstAlpha: gl.ONE_MINUS_DST_ALPHA,
locationCache: newLocationCache(),
lastCompositeMode: CompositeModeUnknown,
}
@ -145,7 +149,7 @@ func (c *Context) BlendFunc(mode CompositeMode) {
return nil
}
c.lastCompositeMode = mode
s, d := c.operations(mode)
s, d := operations(mode)
gl.BlendFunc(uint32(s), uint32(d))
return nil
})

View File

@ -58,6 +58,27 @@ func (p Program) id() programID {
return programID(p.Get("__ebiten_programId").Int())
}
func init() {
c := js.Global.Get("WebGLRenderingContext")
Nearest = Filter(c.Get("NEAREST").Int())
Linear = Filter(c.Get("LINEAR").Int())
VertexShader = ShaderType(c.Get("VERTEX_SHADER").Int())
FragmentShader = ShaderType(c.Get("FRAGMENT_SHADER").Int())
ArrayBuffer = BufferType(c.Get("ARRAY_BUFFER").Int())
ElementArrayBuffer = BufferType(c.Get("ELEMENT_ARRAY_BUFFER").Int())
DynamicDraw = BufferUsage(c.Get("DYNAMIC_DRAW").Int())
StaticDraw = BufferUsage(c.Get("STATIC_DRAW").Int())
Triangles = Mode(c.Get("TRIANGLES").Int())
Lines = Mode(c.Get("LINES").Int())
zero = operation(c.Get("ZERO").Int())
one = operation(c.Get("ONE").Int())
srcAlpha = operation(c.Get("SRC_ALPHA").Int())
dstAlpha = operation(c.Get("DST_ALPHA").Int())
oneMinusSrcAlpha = operation(c.Get("ONE_MINUS_SRC_ALPHA").Int())
oneMinusDstAlpha = operation(c.Get("ONE_MINUS_DST_ALPHA").Int())
}
type context struct {
gl *webgl.Context
lastProgramID programID
@ -89,22 +110,6 @@ func NewContext() (*Context, error) {
}
c := &Context{
Nearest: Filter(gl.NEAREST),
Linear: Filter(gl.LINEAR),
VertexShader: ShaderType(gl.VERTEX_SHADER),
FragmentShader: ShaderType(gl.FRAGMENT_SHADER),
ArrayBuffer: BufferType(gl.ARRAY_BUFFER),
ElementArrayBuffer: BufferType(gl.ELEMENT_ARRAY_BUFFER),
DynamicDraw: BufferUsage(gl.DYNAMIC_DRAW),
StaticDraw: BufferUsage(gl.STATIC_DRAW),
Triangles: Mode(gl.TRIANGLES),
Lines: Mode(gl.LINES),
zero: operation(gl.ZERO),
one: operation(gl.ONE),
srcAlpha: operation(gl.SRC_ALPHA),
dstAlpha: operation(gl.DST_ALPHA),
oneMinusSrcAlpha: operation(gl.ONE_MINUS_SRC_ALPHA),
oneMinusDstAlpha: operation(gl.ONE_MINUS_DST_ALPHA),
locationCache: newLocationCache(),
lastCompositeMode: CompositeModeUnknown,
}
@ -141,7 +146,7 @@ func (c *Context) BlendFunc(mode CompositeMode) {
return
}
c.lastCompositeMode = mode
s, d := c.operations(mode)
s, d := operations(mode)
gl := c.gl
gl.BlendFunc(int(s), int(d))
}

View File

@ -40,6 +40,26 @@ func (p Program) id() programID {
return programID(p.Value)
}
func init() {
Nearest = mgl.NEAREST
Linear = mgl.LINEAR
VertexShader = mgl.VERTEX_SHADER
FragmentShader = mgl.FRAGMENT_SHADER
ArrayBuffer = mgl.ARRAY_BUFFER
ElementArrayBuffer = mgl.ELEMENT_ARRAY_BUFFER
DynamicDraw = mgl.DYNAMIC_DRAW
StaticDraw = mgl.STATIC_DRAW
Triangles = mgl.TRIANGLES
Lines = mgl.LINES
zero = mgl.ZERO
one = mgl.ONE
srcAlpha = mgl.SRC_ALPHA
dstAlpha = mgl.DST_ALPHA
oneMinusSrcAlpha = mgl.ONE_MINUS_SRC_ALPHA
oneMinusDstAlpha = mgl.ONE_MINUS_DST_ALPHA
}
type context struct {
gl mgl.Context
worker mgl.Worker
@ -48,22 +68,6 @@ type context struct {
func NewContext() (*Context, error) {
c := &Context{
Nearest: mgl.NEAREST,
Linear: mgl.LINEAR,
VertexShader: mgl.VERTEX_SHADER,
FragmentShader: mgl.FRAGMENT_SHADER,
ArrayBuffer: mgl.ARRAY_BUFFER,
ElementArrayBuffer: mgl.ELEMENT_ARRAY_BUFFER,
DynamicDraw: mgl.DYNAMIC_DRAW,
StaticDraw: mgl.STATIC_DRAW,
Triangles: mgl.TRIANGLES,
Lines: mgl.LINES,
zero: mgl.ZERO,
one: mgl.ONE,
srcAlpha: mgl.SRC_ALPHA,
dstAlpha: mgl.DST_ALPHA,
oneMinusSrcAlpha: mgl.ONE_MINUS_SRC_ALPHA,
oneMinusDstAlpha: mgl.ONE_MINUS_DST_ALPHA,
locationCache: newLocationCache(),
lastCompositeMode: CompositeModeUnknown,
}
@ -112,7 +116,7 @@ func (c *Context) BlendFunc(mode CompositeMode) {
return
}
c.lastCompositeMode = mode
s, d := c.operations(mode)
s, d := operations(mode)
gl.BlendFunc(mgl.Enum(s), mgl.Enum(d))
}

View File

@ -40,34 +40,34 @@ const (
CompositeModeUnknown
)
func (c *Context) operations(mode CompositeMode) (src operation, dst operation) {
func operations(mode CompositeMode) (src operation, dst operation) {
switch mode {
case CompositeModeSourceOver:
return c.one, c.oneMinusSrcAlpha
return one, oneMinusSrcAlpha
case CompositeModeClear:
return c.zero, c.zero
return zero, zero
case CompositeModeCopy:
return c.one, c.zero
return one, zero
case CompositeModeDestination:
return c.zero, c.one
return zero, one
case CompositeModeDestinationOver:
return c.oneMinusDstAlpha, c.one
return oneMinusDstAlpha, one
case CompositeModeSourceIn:
return c.dstAlpha, c.zero
return dstAlpha, zero
case CompositeModeDestinationIn:
return c.zero, c.srcAlpha
return zero, srcAlpha
case CompositeModeSourceOut:
return c.oneMinusDstAlpha, c.zero
return oneMinusDstAlpha, zero
case CompositeModeDestinationOut:
return c.zero, c.oneMinusSrcAlpha
return zero, oneMinusSrcAlpha
case CompositeModeSourceAtop:
return c.dstAlpha, c.oneMinusSrcAlpha
return dstAlpha, oneMinusSrcAlpha
case CompositeModeDestinationAtop:
return c.oneMinusDstAlpha, c.srcAlpha
return oneMinusDstAlpha, srcAlpha
case CompositeModeXor:
return c.oneMinusDstAlpha, c.oneMinusSrcAlpha
return oneMinusDstAlpha, oneMinusSrcAlpha
case CompositeModeLighter:
return c.one, c.one
return one, one
default:
panic("not reach")
}

View File

@ -61,13 +61,13 @@ func (s *openGLState) initialize(c *opengl.Context) error {
s.lastColorMatrixTranslation = nil
s.lastTexture = zeroTexture
shaderVertexModelviewNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertexModelview))
shaderVertexModelviewNative, err := c.NewShader(opengl.VertexShader, shader(c, shaderVertexModelview))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
}
defer c.DeleteShader(shaderVertexModelviewNative)
shaderFragmentTextureNative, err := c.NewShader(c.FragmentShader, shader(c, shaderFragmentTexture))
shaderFragmentTextureNative, err := c.NewShader(opengl.FragmentShader, shader(c, shaderFragmentTexture))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
}
@ -82,7 +82,7 @@ func (s *openGLState) initialize(c *opengl.Context) error {
}
const stride = 8 // (2 [vertices] + 2 [texels]) * 2 [sizeof(int16)/bytes]
c.NewBuffer(c.ArrayBuffer, 4*stride*MaxQuads, c.DynamicDraw)
c.NewBuffer(opengl.ArrayBuffer, 4*stride*MaxQuads, opengl.DynamicDraw)
indices := make([]uint16, 6*MaxQuads)
for i := uint16(0); i < MaxQuads; i++ {
@ -93,7 +93,7 @@ func (s *openGLState) initialize(c *opengl.Context) error {
indices[6*i+4] = 4*i + 2
indices[6*i+5] = 4*i + 3
}
s.indexBufferQuads = c.NewBuffer(c.ElementArrayBuffer, indices, c.StaticDraw)
s.indexBufferQuads = c.NewBuffer(opengl.ElementArrayBuffer, indices, opengl.StaticDraw)
return nil
}