2018-11-05 19:56:04 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
2018-11-10 19:37:37 +01:00
|
|
|
"fmt"
|
2022-11-13 18:03:36 +01:00
|
|
|
"runtime"
|
2018-11-10 19:37:37 +01:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2022-11-17 05:33:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2018-11-05 19:56:04 +01:00
|
|
|
)
|
|
|
|
|
2021-10-29 17:13:57 +02:00
|
|
|
type activatedTexture struct {
|
|
|
|
textureNative textureNative
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
type Graphics struct {
|
2020-05-17 20:45:58 +02:00
|
|
|
state openGLState
|
|
|
|
context context
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
nextImageID graphicsdriver.ImageID
|
|
|
|
images map[graphicsdriver.ImageID]*Image
|
2019-11-13 17:19:57 +01:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
nextShaderID graphicsdriver.ShaderID
|
|
|
|
shaders map[graphicsdriver.ShaderID]*Shader
|
2020-05-17 20:45:58 +02:00
|
|
|
|
2022-08-07 20:02:12 +02:00
|
|
|
// drawCalled is true just after Draw is called. This holds true until WritePixels is called.
|
2019-11-13 17:19:57 +01:00
|
|
|
drawCalled bool
|
2021-10-29 14:43:40 +02:00
|
|
|
|
|
|
|
uniformVariableNameCache map[int]string
|
2021-10-30 22:40:44 +02:00
|
|
|
textureVariableNameCache map[int]string
|
2021-10-29 17:13:57 +02:00
|
|
|
|
2021-10-29 17:22:28 +02:00
|
|
|
uniformVars []uniformVariable
|
|
|
|
|
2021-10-29 17:13:57 +02:00
|
|
|
// activatedTextures is a set of activated textures.
|
|
|
|
// textureNative cannot be a map key unfortunately.
|
|
|
|
activatedTextures []activatedTexture
|
2018-11-10 19:37:37 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:23:07 +01:00
|
|
|
func (g *Graphics) Begin() error {
|
2019-04-20 08:17:59 +02:00
|
|
|
// Do nothing.
|
2022-03-21 14:23:07 +01:00
|
|
|
return nil
|
2019-04-20 08:17:59 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:23:07 +01:00
|
|
|
func (g *Graphics) End(present bool) error {
|
2019-12-06 00:42:52 +01:00
|
|
|
// Call glFlush to prevent black flicking (especially on Android (#226) and iOS).
|
|
|
|
// TODO: examples/sprites worked without this. Is this really needed?
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.Flush()
|
2022-03-21 14:23:07 +01:00
|
|
|
return nil
|
2019-04-20 08:17:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
func (g *Graphics) SetTransparent(transparent bool) {
|
2019-11-30 16:07:41 +01:00
|
|
|
// Do nothings.
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
func (g *Graphics) checkSize(width, height int) {
|
2018-11-10 19:37:37 +01:00
|
|
|
if width < 1 {
|
2019-02-14 13:04:34 +01:00
|
|
|
panic(fmt.Sprintf("opengl: width (%d) must be equal or more than %d", width, 1))
|
2018-11-10 19:37:37 +01:00
|
|
|
}
|
|
|
|
if height < 1 {
|
2019-02-14 13:04:34 +01:00
|
|
|
panic(fmt.Sprintf("opengl: height (%d) must be equal or more than %d", height, 1))
|
2018-11-10 19:37:37 +01:00
|
|
|
}
|
2020-04-04 10:20:08 +02:00
|
|
|
m := g.context.getMaxTextureSize()
|
2018-11-10 19:37:37 +01:00
|
|
|
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))
|
|
|
|
}
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) genNextImageID() graphicsdriver.ImageID {
|
2020-05-19 16:48:43 +02:00
|
|
|
g.nextImageID++
|
2021-07-01 07:58:55 +02:00
|
|
|
return g.nextImageID
|
2020-05-19 16:48:43 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) genNextShaderID() graphicsdriver.ShaderID {
|
2020-05-17 20:45:58 +02:00
|
|
|
g.nextShaderID++
|
2021-07-01 07:58:55 +02:00
|
|
|
return g.nextShaderID
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) {
|
2018-11-05 19:56:04 +01:00
|
|
|
i := &Image{
|
2020-05-19 16:48:43 +02:00
|
|
|
id: g.genNextImageID(),
|
2020-04-04 10:20:08 +02:00
|
|
|
graphics: g,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
2019-02-14 13:04:34 +01:00
|
|
|
w := graphics.InternalImageSize(width)
|
|
|
|
h := graphics.InternalImageSize(height)
|
2020-04-04 10:20:08 +02:00
|
|
|
g.checkSize(w, h)
|
|
|
|
t, err := g.context.newTexture(w, h)
|
2018-11-05 19:56:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-02 19:41:37 +02:00
|
|
|
i.texture = t
|
2020-05-19 16:48:43 +02:00
|
|
|
g.addImage(i)
|
2018-11-05 19:56:04 +01:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) NewScreenFramebufferImage(width, height int) (graphicsdriver.Image, error) {
|
2020-04-04 10:20:08 +02:00
|
|
|
g.checkSize(width, height)
|
2018-11-05 19:56:04 +01:00
|
|
|
i := &Image{
|
2020-05-19 16:48:43 +02:00
|
|
|
id: g.genNextImageID(),
|
2020-04-04 10:20:08 +02:00
|
|
|
graphics: g,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
screen: true,
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
2020-05-19 16:48:43 +02:00
|
|
|
g.addImage(i)
|
2018-11-11 15:57:23 +01:00
|
|
|
return i, nil
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:48:43 +02:00
|
|
|
func (g *Graphics) addImage(img *Image) {
|
|
|
|
if g.images == nil {
|
2022-02-06 12:41:32 +01:00
|
|
|
g.images = map[graphicsdriver.ImageID]*Image{}
|
2020-05-19 16:48:43 +02:00
|
|
|
}
|
|
|
|
if _, ok := g.images[img.id]; ok {
|
|
|
|
panic(fmt.Sprintf("opengl: image ID %d was already registered", img.id))
|
|
|
|
}
|
|
|
|
g.images[img.id] = img
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) removeImage(img *Image) {
|
|
|
|
delete(g.images, img.id)
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:58:42 +02:00
|
|
|
func (g *Graphics) Initialize() error {
|
|
|
|
return g.state.reset(&g.context)
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:56:04 +01:00
|
|
|
// Reset resets or initializes the current OpenGL state.
|
2020-04-04 10:20:08 +02:00
|
|
|
func (g *Graphics) Reset() error {
|
|
|
|
return g.state.reset(&g.context)
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:23:07 +01:00
|
|
|
func (g *Graphics) SetVertices(vertices []float32, indices []uint16) error {
|
2018-11-11 15:54:58 +01:00
|
|
|
// Note that the vertices passed to BufferSubData is not under GC management
|
|
|
|
// in opengl package due to unsafe-way.
|
|
|
|
// See BufferSubData in context_mobile.go.
|
2020-04-04 10:20:08 +02:00
|
|
|
g.context.arrayBufferSubData(vertices)
|
|
|
|
g.context.elementArrayBufferSubData(indices)
|
2022-03-21 14:23:07 +01:00
|
|
|
return nil
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 14:43:40 +02:00
|
|
|
func (g *Graphics) uniformVariableName(idx int) string {
|
|
|
|
if v, ok := g.uniformVariableNameCache[idx]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
if g.uniformVariableNameCache == nil {
|
|
|
|
g.uniformVariableNameCache = map[int]string{}
|
|
|
|
}
|
|
|
|
name := fmt.Sprintf("U%d", idx)
|
|
|
|
g.uniformVariableNameCache[idx] = name
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2022-11-12 11:02:38 +01:00
|
|
|
func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.ShaderImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms [][]uint32, evenOdd bool) error {
|
2022-10-02 14:26:13 +02:00
|
|
|
if shaderID == graphicsdriver.InvalidShaderID {
|
|
|
|
return fmt.Errorf("opengl: shader ID is invalid")
|
|
|
|
}
|
|
|
|
|
2021-07-01 16:45:23 +02:00
|
|
|
destination := g.images[dstID]
|
2020-05-17 12:12:26 +02:00
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
g.drawCalled = true
|
2020-05-17 12:01:46 +02:00
|
|
|
|
2020-05-17 12:12:26 +02:00
|
|
|
if err := destination.setViewport(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-15 11:58:56 +02:00
|
|
|
g.context.blend(blend)
|
2020-05-17 12:01:46 +02:00
|
|
|
|
2022-10-02 14:26:13 +02:00
|
|
|
shader := g.shaders[shaderID]
|
|
|
|
program := shader.p
|
2020-05-17 12:12:26 +02:00
|
|
|
|
2022-10-30 10:31:06 +01:00
|
|
|
ulen := len(uniforms)
|
2022-10-02 14:26:13 +02:00
|
|
|
if cap(g.uniformVars) < ulen {
|
|
|
|
g.uniformVars = make([]uniformVariable, ulen)
|
2021-07-01 16:45:23 +02:00
|
|
|
} else {
|
2022-10-02 14:26:13 +02:00
|
|
|
g.uniformVars = g.uniformVars[:ulen]
|
|
|
|
}
|
2021-07-01 16:45:23 +02:00
|
|
|
|
2022-10-30 10:31:06 +01:00
|
|
|
for i, v := range uniforms {
|
|
|
|
g.uniformVars[i].name = g.uniformVariableName(i)
|
|
|
|
g.uniformVars[i].value = v
|
|
|
|
g.uniformVars[i].typ = shader.ir.Uniforms[i]
|
2022-10-02 14:26:13 +02:00
|
|
|
}
|
|
|
|
|
2022-10-31 04:59:27 +01:00
|
|
|
// In OpenGL, the NDC's Y direction is upward, so flip the Y direction for the final framebuffer.
|
2022-10-30 10:31:06 +01:00
|
|
|
if destination.screen {
|
2022-10-02 14:26:13 +02:00
|
|
|
const idx = graphics.ProjectionMatrixUniformVariableIndex
|
2022-11-12 11:02:38 +01:00
|
|
|
// Invert the sign bits as float32 values.
|
2022-11-12 13:03:54 +01:00
|
|
|
g.uniformVars[idx].value[1] ^= 1 << 31
|
|
|
|
g.uniformVars[idx].value[5] ^= 1 << 31
|
|
|
|
g.uniformVars[idx].value[9] ^= 1 << 31
|
|
|
|
g.uniformVars[idx].value[13] ^= 1 << 31
|
2020-05-17 13:20:29 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]textureVariable
|
2021-07-01 16:45:23 +02:00
|
|
|
for i, srcID := range srcIDs {
|
2022-02-06 12:41:32 +01:00
|
|
|
if srcID == graphicsdriver.InvalidImageID {
|
2021-07-01 16:45:23 +02:00
|
|
|
continue
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2021-07-01 16:45:23 +02:00
|
|
|
imgs[i].valid = true
|
2021-07-02 19:41:37 +02:00
|
|
|
imgs[i].native = g.images[srcID].texture
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 17:22:28 +02:00
|
|
|
if err := g.useProgram(program, g.uniformVars, imgs); err != nil {
|
2018-11-11 15:53:23 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 12:12:26 +02:00
|
|
|
|
2021-10-29 17:22:28 +02:00
|
|
|
for i := range g.uniformVars {
|
|
|
|
g.uniformVars[i] = uniformVariable{}
|
|
|
|
}
|
|
|
|
g.uniformVars = g.uniformVars[:0]
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
if evenOdd {
|
|
|
|
if err := destination.ensureStencilBuffer(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.Enable(gl.STENCIL_TEST)
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
2022-11-04 08:17:40 +01:00
|
|
|
|
|
|
|
for _, dstRegion := range dstRegions {
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.Scissor(
|
|
|
|
int32(dstRegion.Region.X),
|
|
|
|
int32(dstRegion.Region.Y),
|
|
|
|
int32(dstRegion.Region.Width),
|
|
|
|
int32(dstRegion.Region.Height),
|
2022-11-04 08:17:40 +01:00
|
|
|
)
|
|
|
|
if evenOdd {
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.Clear(gl.STENCIL_BUFFER_BIT)
|
|
|
|
g.context.ctx.StencilFunc(gl.ALWAYS, 0x00, 0xff)
|
|
|
|
g.context.ctx.StencilOp(gl.KEEP, gl.KEEP, gl.INVERT)
|
|
|
|
g.context.ctx.ColorMask(false, false, false, false)
|
|
|
|
|
|
|
|
g.context.ctx.DrawElements(gl.TRIANGLES, int32(dstRegion.IndexCount), gl.UNSIGNED_SHORT, indexOffset*2)
|
|
|
|
|
|
|
|
g.context.ctx.StencilFunc(gl.NOTEQUAL, 0x00, 0xff)
|
|
|
|
g.context.ctx.StencilOp(gl.KEEP, gl.KEEP, gl.KEEP)
|
|
|
|
g.context.ctx.ColorMask(true, true, true, true)
|
2022-11-04 08:17:40 +01:00
|
|
|
}
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.DrawElements(gl.TRIANGLES, int32(dstRegion.IndexCount), gl.UNSIGNED_SHORT, indexOffset*2) // 2 is uint16 size in bytes
|
2022-11-04 08:17:40 +01:00
|
|
|
indexOffset += dstRegion.IndexCount
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
if evenOdd {
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.Disable(gl.STENCIL_TEST)
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 15:53:23 +01:00
|
|
|
return nil
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
func (g *Graphics) SetVsyncEnabled(enabled bool) {
|
2018-11-12 16:00:10 +01:00
|
|
|
// Do nothing
|
2021-08-08 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) SetFullscreen(fullscreen bool) {
|
|
|
|
// Do nothing
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
func (g *Graphics) NeedsRestoring() bool {
|
2022-11-13 18:03:36 +01:00
|
|
|
// Though it is possible to have a logic to restore the graphics data for GPU, do not use it for performance (#1603).
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
return false
|
|
|
|
}
|
2022-11-17 05:33:32 +01:00
|
|
|
return g.context.ctx.IsES()
|
2019-05-26 12:08:46 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 18:43:32 +02:00
|
|
|
func (g *Graphics) NeedsClearingScreen() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
func (g *Graphics) IsGL() bool {
|
2018-11-12 16:00:10 +01:00
|
|
|
return true
|
|
|
|
}
|
2019-06-21 21:47:48 +02:00
|
|
|
|
2022-02-11 12:38:45 +01:00
|
|
|
func (g *Graphics) IsDirectX() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
func (g *Graphics) MaxImageSize() int {
|
|
|
|
return g.context.getMaxTextureSize()
|
2019-07-03 18:10:08 +02:00
|
|
|
}
|
2020-05-17 20:45:58 +02:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader, error) {
|
2020-08-07 17:57:40 +02:00
|
|
|
s, err := newShader(g.genNextShaderID(), g, program)
|
2020-05-17 20:45:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
g.addShader(s)
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) addShader(shader *Shader) {
|
|
|
|
if g.shaders == nil {
|
2022-02-06 12:41:32 +01:00
|
|
|
g.shaders = map[graphicsdriver.ShaderID]*Shader{}
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
|
|
|
if _, ok := g.shaders[shader.id]; ok {
|
|
|
|
panic(fmt.Sprintf("opengl: shader ID %d was already registered", shader.id))
|
|
|
|
}
|
|
|
|
g.shaders[shader.id] = shader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) removeShader(shader *Shader) {
|
|
|
|
delete(g.shaders, shader.id)
|
|
|
|
}
|