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.
|
|
|
|
|
2023-12-19 17:52:03 +01:00
|
|
|
//go:build !playstation5
|
|
|
|
|
2018-11-05 19:56:04 +01:00
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
2018-11-10 19:37:37 +01:00
|
|
|
"fmt"
|
2023-11-03 18:45:16 +01:00
|
|
|
"unsafe"
|
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
|
2023-12-19 18:38:09 +01:00
|
|
|
vsync bool
|
2020-05-17 20:45:58 +02:00
|
|
|
|
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
|
|
|
|
2024-04-10 18:57:55 +02:00
|
|
|
uniformVariableNameCache map[int]string
|
|
|
|
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
|
2023-12-23 11:17:50 +01:00
|
|
|
|
|
|
|
graphicsPlatform
|
2018-11-10 19:37:37 +01:00
|
|
|
}
|
|
|
|
|
2023-04-21 15:52:49 +02:00
|
|
|
func newGraphics(ctx gl.Context) *Graphics {
|
2023-12-19 18:38:09 +01:00
|
|
|
g := &Graphics{
|
|
|
|
vsync: true,
|
|
|
|
}
|
2023-04-21 15:52:49 +02:00
|
|
|
if isDebug {
|
|
|
|
g.context.ctx = &gl.DebugContext{Context: ctx}
|
|
|
|
} else {
|
|
|
|
g.context.ctx = ctx
|
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
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()
|
2023-01-03 17:36:02 +01:00
|
|
|
|
2023-12-17 17:44:25 +01:00
|
|
|
// The last uniforms must be reset before swapping the buffer (#2517).
|
2023-01-03 17:36:02 +01:00
|
|
|
if present {
|
|
|
|
g.state.resetLastUniforms()
|
2023-12-19 18:38:09 +01:00
|
|
|
if err := g.swapBuffers(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-03 17:36:02 +01:00
|
|
|
}
|
2023-12-17 17:09:05 +01:00
|
|
|
|
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) {
|
2023-01-28 11:06:38 +01:00
|
|
|
// Do nothing.
|
2019-11-30 16:07:41 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-12-23 11:28:17 +01:00
|
|
|
if err := g.makeContextCurrent(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-17 17:09:05 +01:00
|
|
|
if err := g.state.reset(&g.context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-22 09:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets or initializes the current OpenGL state.
|
|
|
|
func (g *Graphics) Reset() error {
|
|
|
|
return g.state.reset(&g.context)
|
2018-11-05 19:56:04 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 18:45:16 +01:00
|
|
|
func (g *Graphics) SetVertices(vertices []float32, indices []uint32) error {
|
2023-03-20 05:22:51 +01:00
|
|
|
g.state.setVertices(&g.context, vertices, 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
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:39:14 +02:00
|
|
|
func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcIDs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error {
|
2022-10-02 14:26:13 +02:00
|
|
|
if shaderID == graphicsdriver.InvalidShaderID {
|
|
|
|
return fmt.Errorf("opengl: shader ID is invalid")
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
g.drawCalled = true
|
2024-04-10 21:08:03 +02:00
|
|
|
targetCount := 0
|
|
|
|
firstTarget := -1
|
2024-04-10 18:57:55 +02:00
|
|
|
var dsts [graphics.ShaderDstImageCount]*Image
|
2024-04-05 20:39:14 +02:00
|
|
|
for i, dstID := range dstIDs {
|
|
|
|
if dstID == graphicsdriver.InvalidImageID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dst := g.images[dstIDs[i]]
|
|
|
|
if dst == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-10 21:08:03 +02:00
|
|
|
if firstTarget == -1 {
|
|
|
|
firstTarget = i
|
|
|
|
}
|
2024-04-10 21:11:32 +02:00
|
|
|
if err := dst.ensureFramebuffer(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-10 18:57:55 +02:00
|
|
|
dsts[i] = dst
|
2024-04-10 21:08:03 +02:00
|
|
|
targetCount++
|
2020-05-17 12:12:26 +02:00
|
|
|
}
|
2024-04-10 18:57:55 +02:00
|
|
|
|
2024-04-10 21:08:03 +02:00
|
|
|
f := uint32(dsts[firstTarget].framebuffer.native)
|
|
|
|
// If the number of targets is more than one, or if the only target is the first one, then
|
|
|
|
// it is safe to assume that MRT is used.
|
|
|
|
// Also, it only matters in order to specify empty targets/viewports when not all slots are
|
|
|
|
// being filled.
|
|
|
|
usesMRT := firstTarget > 0 || targetCount > 1
|
|
|
|
if usesMRT {
|
|
|
|
f = uint32(g.context.mrtFramebuffer)
|
|
|
|
// Create the initial MRT framebuffer
|
|
|
|
if f == 0 {
|
2024-04-10 18:57:55 +02:00
|
|
|
f = g.context.ctx.CreateFramebuffer()
|
|
|
|
if f <= 0 {
|
|
|
|
return fmt.Errorf("opengl: creating framebuffer failed: the returned value is not positive but %d", f)
|
|
|
|
}
|
|
|
|
g.context.mrtFramebuffer = framebufferNative(f)
|
|
|
|
}
|
2024-04-05 20:39:14 +02:00
|
|
|
|
2024-04-10 18:57:55 +02:00
|
|
|
g.context.bindFramebuffer(framebufferNative(f))
|
|
|
|
|
|
|
|
// Reset color attachments
|
|
|
|
if s := g.context.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s == gl.FRAMEBUFFER_COMPLETE {
|
2024-04-10 18:59:21 +02:00
|
|
|
g.context.ctx.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
|
2024-04-05 20:39:14 +02:00
|
|
|
}
|
2024-04-10 18:57:55 +02:00
|
|
|
for i, dst := range dsts {
|
|
|
|
if dst == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
g.context.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+uint32(i), gl.TEXTURE_2D, uint32(dst.texture), 0)
|
|
|
|
}
|
|
|
|
if s := g.context.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE {
|
|
|
|
if s != 0 {
|
|
|
|
return fmt.Errorf("opengl: creating framebuffer failed: %v", s)
|
|
|
|
}
|
|
|
|
if e := g.context.ctx.GetError(); e != gl.NO_ERROR {
|
|
|
|
return fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("opengl: creating framebuffer failed: unknown error")
|
|
|
|
}
|
|
|
|
// Color attachments
|
|
|
|
var attached []uint32
|
|
|
|
for i, dst := range dsts {
|
|
|
|
if dst == nil {
|
|
|
|
attached = append(attached, gl.NONE)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attached = append(attached, uint32(gl.COLOR_ATTACHMENT0+i))
|
|
|
|
}
|
|
|
|
g.context.ctx.DrawBuffers(attached)
|
|
|
|
} else {
|
|
|
|
g.context.bindFramebuffer(framebufferNative(f))
|
2024-04-05 20:39:14 +02:00
|
|
|
}
|
2024-04-10 18:57:55 +02:00
|
|
|
|
2024-04-10 21:08:03 +02:00
|
|
|
w, h := dsts[firstTarget].viewportSize() //.framebuffer.viewportWidth, dsts[firstTarget].framebuffer.viewportHeight
|
|
|
|
g.context.setViewport(w, h, dsts[firstTarget].screen)
|
2024-04-05 20:39:14 +02:00
|
|
|
|
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-12-03 08:11:26 +01:00
|
|
|
ulen := len(shader.ir.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-12-03 08:11:26 +01:00
|
|
|
var idx int
|
|
|
|
for i, typ := range shader.ir.Uniforms {
|
|
|
|
n := typ.Uint32Count()
|
2022-10-30 10:31:06 +01:00
|
|
|
g.uniformVars[i].name = g.uniformVariableName(i)
|
2022-12-03 08:11:26 +01:00
|
|
|
g.uniformVars[i].value = uniforms[idx : idx+n]
|
|
|
|
g.uniformVars[i].typ = typ
|
|
|
|
idx += n
|
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.
|
2024-04-10 21:08:03 +02:00
|
|
|
if !usesMRT && dsts[firstTarget].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
|
|
|
}
|
|
|
|
|
2024-04-05 20:39:14 +02:00
|
|
|
var imgs [graphics.ShaderSrcImageCount]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]
|
|
|
|
|
2024-06-08 04:54:44 +02:00
|
|
|
if fillRule != graphicsdriver.FillRuleFillAll {
|
2024-04-10 21:08:03 +02:00
|
|
|
if err := dsts[firstTarget].ensureStencilBuffer(framebufferNative(f)); err != nil {
|
|
|
|
return err
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
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(
|
2023-09-28 07:29:55 +02:00
|
|
|
int32(dstRegion.Region.Min.X),
|
|
|
|
int32(dstRegion.Region.Min.Y),
|
|
|
|
int32(dstRegion.Region.Dx()),
|
|
|
|
int32(dstRegion.Region.Dy()),
|
2022-11-04 08:17:40 +01:00
|
|
|
)
|
2024-04-05 20:39:14 +02:00
|
|
|
|
2023-11-06 01:18:08 +01:00
|
|
|
switch fillRule {
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleNonZero:
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.Clear(gl.STENCIL_BUFFER_BIT)
|
|
|
|
g.context.ctx.StencilFunc(gl.ALWAYS, 0x00, 0xff)
|
2023-11-06 01:18:08 +01:00
|
|
|
g.context.ctx.StencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.INCR_WRAP)
|
|
|
|
g.context.ctx.StencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.DECR_WRAP)
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.ColorMask(false, false, false, false)
|
2024-04-05 20:39:14 +02:00
|
|
|
|
2023-11-03 18:45:16 +01:00
|
|
|
g.context.ctx.DrawElements(gl.TRIANGLES, int32(dstRegion.IndexCount), gl.UNSIGNED_INT, indexOffset*int(unsafe.Sizeof(uint32(0))))
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleEvenOdd:
|
2023-11-06 01:18:08 +01:00
|
|
|
g.context.ctx.Clear(gl.STENCIL_BUFFER_BIT)
|
|
|
|
g.context.ctx.StencilFunc(gl.ALWAYS, 0x00, 0xff)
|
|
|
|
g.context.ctx.StencilOpSeparate(gl.FRONT_AND_BACK, gl.KEEP, gl.KEEP, gl.INVERT)
|
|
|
|
g.context.ctx.ColorMask(false, false, false, false)
|
2022-11-17 05:33:32 +01:00
|
|
|
|
2023-11-06 01:18:08 +01:00
|
|
|
g.context.ctx.DrawElements(gl.TRIANGLES, int32(dstRegion.IndexCount), gl.UNSIGNED_INT, indexOffset*int(unsafe.Sizeof(uint32(0))))
|
|
|
|
}
|
2024-06-08 04:54:44 +02:00
|
|
|
if fillRule != graphicsdriver.FillRuleFillAll {
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.StencilFunc(gl.NOTEQUAL, 0x00, 0xff)
|
2023-11-06 01:18:08 +01:00
|
|
|
g.context.ctx.StencilOpSeparate(gl.FRONT_AND_BACK, gl.KEEP, gl.KEEP, gl.KEEP)
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.ColorMask(true, true, true, true)
|
2022-11-04 08:17:40 +01:00
|
|
|
}
|
2023-11-03 18:45:16 +01:00
|
|
|
g.context.ctx.DrawElements(gl.TRIANGLES, int32(dstRegion.IndexCount), gl.UNSIGNED_INT, indexOffset*int(unsafe.Sizeof(uint32(0))))
|
2022-11-04 08:17:40 +01:00
|
|
|
indexOffset += dstRegion.IndexCount
|
|
|
|
}
|
|
|
|
|
2024-06-08 04:54:44 +02:00
|
|
|
if fillRule != graphicsdriver.FillRuleFillAll {
|
2022-11-17 05:33:32 +01:00
|
|
|
g.context.ctx.Disable(gl.STENCIL_TEST)
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
|
|
|
|
2024-04-10 18:57:55 +02:00
|
|
|
// Detach existing color attachments
|
|
|
|
//g.context.bindFramebuffer(fb)
|
|
|
|
//TODO:
|
|
|
|
|
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) {
|
2023-12-19 18:38:09 +01:00
|
|
|
g.vsync = enabled
|
2021-08-08 08:38:41 +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) 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)
|
|
|
|
}
|