mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
internal/graphicsdriver/opengl: remove the built-in shaders
Updates #2369
This commit is contained in:
parent
5f7db485f2
commit
033997d928
@ -1,251 +0,0 @@
|
|||||||
// Copyright 2014 Hajime Hoshi
|
|
||||||
//
|
|
||||||
// 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 (
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
||||||
)
|
|
||||||
|
|
||||||
// glslReservedKeywords is a set of reserved keywords that cannot be used as an indentifier on some environments.
|
|
||||||
// See https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf.
|
|
||||||
var glslReservedKeywords = map[string]struct{}{
|
|
||||||
"common": {}, "partition": {}, "active": {},
|
|
||||||
"asm": {},
|
|
||||||
"class": {}, "union": {}, "enum": {}, "typedef": {}, "template": {}, "this": {},
|
|
||||||
"resource": {},
|
|
||||||
"goto": {},
|
|
||||||
"inline": {}, "noinline": {}, "public": {}, "static": {}, "extern": {}, "external": {}, "interface": {},
|
|
||||||
"long": {}, "short": {}, "half": {}, "fixed": {}, "unsigned": {}, "superp": {},
|
|
||||||
"input": {}, "output": {},
|
|
||||||
"hvec2": {}, "hvec3": {}, "hvec4": {}, "fvec2": {}, "fvec3": {}, "fvec4": {},
|
|
||||||
"filter": {},
|
|
||||||
"sizeof": {}, "cast": {},
|
|
||||||
"namespace": {}, "using": {},
|
|
||||||
"sampler3DRect": {},
|
|
||||||
}
|
|
||||||
|
|
||||||
var glslIdentifier = regexp.MustCompile(`[_a-zA-Z][_a-zA-Z0-9]*`)
|
|
||||||
|
|
||||||
func checkGLSL(src string) {
|
|
||||||
for _, l := range strings.Split(src, "\n") {
|
|
||||||
if strings.Contains(l, "//") {
|
|
||||||
l = l[:strings.Index(l, "//")]
|
|
||||||
}
|
|
||||||
for _, token := range glslIdentifier.FindAllString(l, -1) {
|
|
||||||
if _, ok := glslReservedKeywords[token]; ok {
|
|
||||||
panic(fmt.Sprintf("opengl: %q is a reserved keyword", token))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func vertexShaderStr() string {
|
|
||||||
src := shaderStrVertex
|
|
||||||
checkGLSL(src)
|
|
||||||
return src
|
|
||||||
}
|
|
||||||
|
|
||||||
func fragmentShaderStr(useColorM bool, filter graphicsdriver.Filter, address graphicsdriver.Address) string {
|
|
||||||
replaces := map[string]string{
|
|
||||||
"{{.AddressClampToZero}}": fmt.Sprintf("%d", graphicsdriver.AddressClampToZero),
|
|
||||||
"{{.AddressRepeat}}": fmt.Sprintf("%d", graphicsdriver.AddressRepeat),
|
|
||||||
"{{.AddressUnsafe}}": fmt.Sprintf("%d", graphicsdriver.AddressUnsafe),
|
|
||||||
}
|
|
||||||
src := shaderStrFragment
|
|
||||||
for k, v := range replaces {
|
|
||||||
src = strings.Replace(src, k, v, -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
var defs []string
|
|
||||||
|
|
||||||
if useColorM {
|
|
||||||
defs = append(defs, "#define USE_COLOR_MATRIX")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch filter {
|
|
||||||
case graphicsdriver.FilterNearest:
|
|
||||||
defs = append(defs, "#define FILTER_NEAREST")
|
|
||||||
case graphicsdriver.FilterLinear:
|
|
||||||
defs = append(defs, "#define FILTER_LINEAR")
|
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("opengl: invalid filter: %d", filter))
|
|
||||||
}
|
|
||||||
|
|
||||||
switch address {
|
|
||||||
case graphicsdriver.AddressClampToZero:
|
|
||||||
defs = append(defs, "#define ADDRESS_CLAMP_TO_ZERO")
|
|
||||||
case graphicsdriver.AddressRepeat:
|
|
||||||
defs = append(defs, "#define ADDRESS_REPEAT")
|
|
||||||
case graphicsdriver.AddressUnsafe:
|
|
||||||
defs = append(defs, "#define ADDRESS_UNSAFE")
|
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("opengl: invalid address: %d", address))
|
|
||||||
}
|
|
||||||
|
|
||||||
src = strings.Replace(src, "{{.Definitions}}", strings.Join(defs, "\n"), -1)
|
|
||||||
|
|
||||||
checkGLSL(src)
|
|
||||||
return src
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
shaderStrVertex = `
|
|
||||||
uniform vec2 viewport_size;
|
|
||||||
attribute vec2 A0;
|
|
||||||
attribute vec2 A1;
|
|
||||||
attribute vec4 A2;
|
|
||||||
varying vec2 varying_tex;
|
|
||||||
varying vec4 varying_color_scale;
|
|
||||||
|
|
||||||
void main(void) {
|
|
||||||
varying_tex = A1;
|
|
||||||
varying_color_scale = A2;
|
|
||||||
|
|
||||||
mat4 projection_matrix = mat4(
|
|
||||||
vec4(2.0 / viewport_size.x, 0, 0, 0),
|
|
||||||
vec4(0, 2.0 / viewport_size.y, 0, 0),
|
|
||||||
vec4(0, 0, 1, 0),
|
|
||||||
vec4(-1, -1, 0, 1)
|
|
||||||
);
|
|
||||||
gl_Position = projection_matrix * vec4(A0, 0, 1);
|
|
||||||
}
|
|
||||||
`
|
|
||||||
shaderStrFragment = `
|
|
||||||
#if defined(GL_ES)
|
|
||||||
precision mediump float;
|
|
||||||
#else
|
|
||||||
#define lowp
|
|
||||||
#define mediump
|
|
||||||
#define highp
|
|
||||||
#endif
|
|
||||||
|
|
||||||
{{.Definitions}}
|
|
||||||
|
|
||||||
uniform sampler2D T0;
|
|
||||||
uniform vec4 source_region;
|
|
||||||
|
|
||||||
#if defined(USE_COLOR_MATRIX)
|
|
||||||
uniform mat4 color_matrix_body;
|
|
||||||
uniform vec4 color_matrix_translation;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uniform highp vec2 source_size;
|
|
||||||
|
|
||||||
varying highp vec2 varying_tex;
|
|
||||||
varying highp vec4 varying_color_scale;
|
|
||||||
|
|
||||||
highp vec2 adjustTexelByAddress(highp vec2 p, highp vec4 source_region) {
|
|
||||||
#if defined(ADDRESS_CLAMP_TO_ZERO)
|
|
||||||
return p;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(ADDRESS_REPEAT)
|
|
||||||
highp vec2 o = vec2(source_region[0], source_region[1]);
|
|
||||||
highp vec2 size = vec2(source_region[2] - source_region[0], source_region[3] - source_region[1]);
|
|
||||||
return mod((p - o), size) + o;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(ADDRESS_UNSAFE)
|
|
||||||
return p;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void main(void) {
|
|
||||||
highp vec2 pos = varying_tex;
|
|
||||||
|
|
||||||
#if defined(FILTER_NEAREST)
|
|
||||||
vec4 color;
|
|
||||||
# if defined(ADDRESS_UNSAFE)
|
|
||||||
color = texture2D(T0, pos);
|
|
||||||
# else
|
|
||||||
pos = adjustTexelByAddress(pos, source_region);
|
|
||||||
if (source_region[0] <= pos.x &&
|
|
||||||
source_region[1] <= pos.y &&
|
|
||||||
pos.x < source_region[2] &&
|
|
||||||
pos.y < source_region[3]) {
|
|
||||||
color = texture2D(T0, pos);
|
|
||||||
} else {
|
|
||||||
color = vec4(0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
# endif // defined(ADDRESS_UNSAFE)
|
|
||||||
#endif // defined(FILTER_NEAREST)
|
|
||||||
|
|
||||||
#if defined(FILTER_LINEAR)
|
|
||||||
vec4 color;
|
|
||||||
highp vec2 texel_size = 1.0 / source_size;
|
|
||||||
|
|
||||||
// Shift 1/512 [texel] to avoid the tie-breaking issue (#1212).
|
|
||||||
// As all the vertex positions are aligned to 1/16 [pixel], this shiting should work in most cases.
|
|
||||||
highp vec2 p0 = pos - (texel_size) / 2.0 + (texel_size / 512.0);
|
|
||||||
highp vec2 p1 = pos + (texel_size) / 2.0 + (texel_size / 512.0);
|
|
||||||
|
|
||||||
# if !defined(ADDRESS_UNSAFE)
|
|
||||||
p0 = adjustTexelByAddress(p0, source_region);
|
|
||||||
p1 = adjustTexelByAddress(p1, source_region);
|
|
||||||
# endif // defined(ADDRESS_UNSAFE)
|
|
||||||
|
|
||||||
vec4 c0 = texture2D(T0, p0);
|
|
||||||
vec4 c1 = texture2D(T0, vec2(p1.x, p0.y));
|
|
||||||
vec4 c2 = texture2D(T0, vec2(p0.x, p1.y));
|
|
||||||
vec4 c3 = texture2D(T0, p1);
|
|
||||||
# if !defined(ADDRESS_UNSAFE)
|
|
||||||
if (p0.x < source_region[0]) {
|
|
||||||
c0 = vec4(0, 0, 0, 0);
|
|
||||||
c2 = vec4(0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
if (p0.y < source_region[1]) {
|
|
||||||
c0 = vec4(0, 0, 0, 0);
|
|
||||||
c1 = vec4(0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
if (source_region[2] <= p1.x) {
|
|
||||||
c1 = vec4(0, 0, 0, 0);
|
|
||||||
c3 = vec4(0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
if (source_region[3] <= p1.y) {
|
|
||||||
c2 = vec4(0, 0, 0, 0);
|
|
||||||
c3 = vec4(0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
# endif // defined(ADDRESS_UNSAFE)
|
|
||||||
|
|
||||||
vec2 rate = fract(p0 * source_size);
|
|
||||||
color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
|
|
||||||
#endif // defined(FILTER_LINEAR)
|
|
||||||
|
|
||||||
# if defined(USE_COLOR_MATRIX)
|
|
||||||
// Un-premultiply alpha.
|
|
||||||
// When the alpha is 0, 1.0 - sign(alpha) is 1.0, which means division does nothing.
|
|
||||||
color.rgb /= color.a + (1.0 - sign(color.a));
|
|
||||||
// Apply the color matrix or scale.
|
|
||||||
color = (color_matrix_body * color) + color_matrix_translation;
|
|
||||||
// Premultiply alpha
|
|
||||||
color.rgb *= color.a;
|
|
||||||
// Apply color scale.
|
|
||||||
color *= varying_color_scale;
|
|
||||||
// Clamp the output.
|
|
||||||
color.rgb = min(color.rgb, color.a);
|
|
||||||
# else
|
|
||||||
// Apply color scale.
|
|
||||||
color *= varying_color_scale;
|
|
||||||
// No clamping needed as the color matrix shader is used then.
|
|
||||||
# endif // defined(USE_COLOR_MATRIX)
|
|
||||||
|
|
||||||
gl_FragColor = color;
|
|
||||||
}
|
|
||||||
`
|
|
||||||
)
|
|
@ -181,6 +181,10 @@ func (g *Graphics) uniformVariableName(idx int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.ShaderImageCount]graphicsdriver.ImageID, offsets [graphics.ShaderImageCount - 1][2]float32, shaderID graphicsdriver.ShaderID, indexLen int, indexOffset int, mode graphicsdriver.CompositeMode, colorM graphicsdriver.ColorM, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, uniforms [][]float32, evenOdd bool) error {
|
func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.ShaderImageCount]graphicsdriver.ImageID, offsets [graphics.ShaderImageCount - 1][2]float32, shaderID graphicsdriver.ShaderID, indexLen int, indexOffset int, mode graphicsdriver.CompositeMode, colorM graphicsdriver.ColorM, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, uniforms [][]float32, evenOdd bool) error {
|
||||||
|
if shaderID == graphicsdriver.InvalidShaderID {
|
||||||
|
return fmt.Errorf("opengl: shader ID is invalid")
|
||||||
|
}
|
||||||
|
|
||||||
destination := g.images[dstID]
|
destination := g.images[dstID]
|
||||||
|
|
||||||
g.drawCalled = true
|
g.drawCalled = true
|
||||||
@ -196,145 +200,95 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.
|
|||||||
)
|
)
|
||||||
g.context.blendFunc(mode)
|
g.context.blendFunc(mode)
|
||||||
|
|
||||||
var program program
|
shader := g.shaders[shaderID]
|
||||||
if shaderID == graphicsdriver.InvalidShaderID {
|
program := shader.p
|
||||||
program = g.state.programs[programKey{
|
|
||||||
useColorM: !colorM.IsIdentity(),
|
|
||||||
filter: filter,
|
|
||||||
address: address,
|
|
||||||
}]
|
|
||||||
|
|
||||||
dw, dh := destination.framebufferSize()
|
ulen := graphics.PreservedUniformVariablesCount + len(uniforms)
|
||||||
g.uniformVars = append(g.uniformVars, uniformVariable{
|
if cap(g.uniformVars) < ulen {
|
||||||
name: "viewport_size",
|
g.uniformVars = make([]uniformVariable, ulen)
|
||||||
value: []float32{float32(dw), float32(dh)},
|
|
||||||
typ: shaderir.Type{Main: shaderir.Vec2},
|
|
||||||
}, uniformVariable{
|
|
||||||
name: "source_region",
|
|
||||||
value: []float32{
|
|
||||||
srcRegion.X,
|
|
||||||
srcRegion.Y,
|
|
||||||
srcRegion.X + srcRegion.Width,
|
|
||||||
srcRegion.Y + srcRegion.Height,
|
|
||||||
},
|
|
||||||
typ: shaderir.Type{Main: shaderir.Vec4},
|
|
||||||
})
|
|
||||||
|
|
||||||
if !colorM.IsIdentity() {
|
|
||||||
// ColorM's elements are immutable. It's OK to hold the reference without copying.
|
|
||||||
var esBody [16]float32
|
|
||||||
var esTranslate [4]float32
|
|
||||||
colorM.Elements(esBody[:], esTranslate[:])
|
|
||||||
g.uniformVars = append(g.uniformVars, uniformVariable{
|
|
||||||
name: "color_matrix_body",
|
|
||||||
value: esBody[:],
|
|
||||||
typ: shaderir.Type{Main: shaderir.Mat4},
|
|
||||||
}, uniformVariable{
|
|
||||||
name: "color_matrix_translation",
|
|
||||||
value: esTranslate[:],
|
|
||||||
typ: shaderir.Type{Main: shaderir.Vec4},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if filter != graphicsdriver.FilterNearest {
|
|
||||||
sw, sh := g.images[srcIDs[0]].framebufferSize()
|
|
||||||
g.uniformVars = append(g.uniformVars, uniformVariable{
|
|
||||||
name: "source_size",
|
|
||||||
value: []float32{float32(sw), float32(sh)},
|
|
||||||
typ: shaderir.Type{Main: shaderir.Vec2},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
shader := g.shaders[shaderID]
|
g.uniformVars = g.uniformVars[:ulen]
|
||||||
program = shader.p
|
}
|
||||||
|
|
||||||
ulen := graphics.PreservedUniformVariablesCount + len(uniforms)
|
|
||||||
if cap(g.uniformVars) < ulen {
|
|
||||||
g.uniformVars = make([]uniformVariable, ulen)
|
|
||||||
} else {
|
|
||||||
g.uniformVars = g.uniformVars[:ulen]
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
const idx = graphics.TextureDestinationSizeUniformVariableIndex
|
|
||||||
w, h := destination.framebufferSize()
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = []float32{float32(w), float32(h)}
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
{
|
|
||||||
sizes := make([]float32, 2*len(srcIDs))
|
|
||||||
for i, srcID := range srcIDs {
|
|
||||||
if img := g.images[srcID]; img != nil {
|
|
||||||
w, h := img.framebufferSize()
|
|
||||||
sizes[2*i] = float32(w)
|
|
||||||
sizes[2*i+1] = float32(h)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
{
|
||||||
|
const idx = graphics.TextureDestinationSizeUniformVariableIndex
|
||||||
|
w, h := destination.framebufferSize()
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = []float32{float32(w), float32(h)}
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
{
|
||||||
|
sizes := make([]float32, 2*len(srcIDs))
|
||||||
|
for i, srcID := range srcIDs {
|
||||||
|
if img := g.images[srcID]; img != nil {
|
||||||
|
w, h := img.framebufferSize()
|
||||||
|
sizes[2*i] = float32(w)
|
||||||
|
sizes[2*i+1] = float32(h)
|
||||||
}
|
}
|
||||||
const idx = graphics.TextureSourceSizesUniformVariableIndex
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = sizes
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
dw, dh := destination.framebufferSize()
|
|
||||||
{
|
|
||||||
origin := []float32{float32(dstRegion.X) / float32(dw), float32(dstRegion.Y) / float32(dh)}
|
|
||||||
const idx = graphics.TextureDestinationRegionOriginUniformVariableIndex
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = origin
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
{
|
|
||||||
size := []float32{float32(dstRegion.Width) / float32(dw), float32(dstRegion.Height) / float32(dh)}
|
|
||||||
const idx = graphics.TextureDestinationRegionSizeUniformVariableIndex
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = size
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
{
|
|
||||||
voffsets := make([]float32, 2*len(offsets))
|
|
||||||
for i, o := range offsets {
|
|
||||||
voffsets[2*i] = o[0]
|
|
||||||
voffsets[2*i+1] = o[1]
|
|
||||||
}
|
|
||||||
const idx = graphics.TextureSourceOffsetsUniformVariableIndex
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = voffsets
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
{
|
|
||||||
origin := []float32{float32(srcRegion.X), float32(srcRegion.Y)}
|
|
||||||
const idx = graphics.TextureSourceRegionOriginUniformVariableIndex
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = origin
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
{
|
|
||||||
size := []float32{float32(srcRegion.Width), float32(srcRegion.Height)}
|
|
||||||
const idx = graphics.TextureSourceRegionSizeUniformVariableIndex
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = size
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
{
|
|
||||||
const idx = graphics.ProjectionMatrixUniformVariableIndex
|
|
||||||
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
|
||||||
g.uniformVars[idx].value = []float32{
|
|
||||||
2 / float32(dw), 0, 0, 0,
|
|
||||||
0, 2 / float32(dh), 0, 0,
|
|
||||||
0, 0, 1, 0,
|
|
||||||
-1, -1, 0, 1,
|
|
||||||
}
|
|
||||||
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, v := range uniforms {
|
|
||||||
const offset = graphics.PreservedUniformVariablesCount
|
|
||||||
g.uniformVars[i+offset].name = g.uniformVariableName(i + offset)
|
|
||||||
g.uniformVars[i+offset].value = v
|
|
||||||
g.uniformVars[i+offset].typ = shader.ir.Uniforms[i+offset]
|
|
||||||
}
|
}
|
||||||
|
const idx = graphics.TextureSourceSizesUniformVariableIndex
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = sizes
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
dw, dh := destination.framebufferSize()
|
||||||
|
{
|
||||||
|
origin := []float32{float32(dstRegion.X) / float32(dw), float32(dstRegion.Y) / float32(dh)}
|
||||||
|
const idx = graphics.TextureDestinationRegionOriginUniformVariableIndex
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = origin
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
{
|
||||||
|
size := []float32{float32(dstRegion.Width) / float32(dw), float32(dstRegion.Height) / float32(dh)}
|
||||||
|
const idx = graphics.TextureDestinationRegionSizeUniformVariableIndex
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = size
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
{
|
||||||
|
voffsets := make([]float32, 2*len(offsets))
|
||||||
|
for i, o := range offsets {
|
||||||
|
voffsets[2*i] = o[0]
|
||||||
|
voffsets[2*i+1] = o[1]
|
||||||
|
}
|
||||||
|
const idx = graphics.TextureSourceOffsetsUniformVariableIndex
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = voffsets
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
{
|
||||||
|
origin := []float32{float32(srcRegion.X), float32(srcRegion.Y)}
|
||||||
|
const idx = graphics.TextureSourceRegionOriginUniformVariableIndex
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = origin
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
{
|
||||||
|
size := []float32{float32(srcRegion.Width), float32(srcRegion.Height)}
|
||||||
|
const idx = graphics.TextureSourceRegionSizeUniformVariableIndex
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = size
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const idx = graphics.ProjectionMatrixUniformVariableIndex
|
||||||
|
g.uniformVars[idx].name = g.uniformVariableName(idx)
|
||||||
|
g.uniformVars[idx].value = []float32{
|
||||||
|
2 / float32(dw), 0, 0, 0,
|
||||||
|
0, 2 / float32(dh), 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
-1, -1, 0, 1,
|
||||||
|
}
|
||||||
|
g.uniformVars[idx].typ = shader.ir.Uniforms[idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, v := range uniforms {
|
||||||
|
const offset = graphics.PreservedUniformVariablesCount
|
||||||
|
g.uniformVars[i+offset].name = g.uniformVariableName(i + offset)
|
||||||
|
g.uniformVars[i+offset].value = v
|
||||||
|
g.uniformVars[i+offset].typ = shader.ir.Uniforms[i+offset]
|
||||||
}
|
}
|
||||||
|
|
||||||
var imgs [graphics.ShaderImageCount]textureVariable
|
var imgs [graphics.ShaderImageCount]textureVariable
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -114,12 +113,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type programKey struct {
|
|
||||||
useColorM bool
|
|
||||||
filter graphicsdriver.Filter
|
|
||||||
address graphicsdriver.Address
|
|
||||||
}
|
|
||||||
|
|
||||||
// openGLState is a state for
|
// openGLState is a state for
|
||||||
type openGLState struct {
|
type openGLState struct {
|
||||||
// arrayBuffer is OpenGL's array buffer (vertices data).
|
// arrayBuffer is OpenGL's array buffer (vertices data).
|
||||||
@ -128,9 +121,6 @@ type openGLState struct {
|
|||||||
// elementArrayBuffer is OpenGL's element array buffer (indices data).
|
// elementArrayBuffer is OpenGL's element array buffer (indices data).
|
||||||
elementArrayBuffer buffer
|
elementArrayBuffer buffer
|
||||||
|
|
||||||
// programs is OpenGL's program for rendering a texture.
|
|
||||||
programs map[programKey]program
|
|
||||||
|
|
||||||
lastProgram program
|
lastProgram program
|
||||||
lastUniforms map[string][]float32
|
lastUniforms map[string][]float32
|
||||||
lastActiveTexture int
|
lastActiveTexture int
|
||||||
@ -153,18 +143,6 @@ func (s *openGLState) reset(context *context) error {
|
|||||||
delete(s.lastUniforms, key)
|
delete(s.lastUniforms, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// When context lost happens, deleting programs or buffers is not necessary.
|
|
||||||
// However, it is not assumed that reset is called only when context lost happens.
|
|
||||||
// Let's delete them explicitly.
|
|
||||||
if s.programs == nil {
|
|
||||||
s.programs = map[programKey]program{}
|
|
||||||
} else {
|
|
||||||
for k, p := range s.programs {
|
|
||||||
context.deleteProgram(p)
|
|
||||||
delete(s.programs, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// On browsers (at least Chrome), buffers are already detached from the context
|
// On browsers (at least Chrome), buffers are already detached from the context
|
||||||
// and must not be deleted by DeleteBuffer.
|
// and must not be deleted by DeleteBuffer.
|
||||||
if runtime.GOOS != "js" {
|
if runtime.GOOS != "js" {
|
||||||
@ -176,46 +154,6 @@ func (s *openGLState) reset(context *context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shaderVertexModelviewNative, err := context.newVertexShader(vertexShaderStr())
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
|
|
||||||
}
|
|
||||||
defer context.deleteShader(shaderVertexModelviewNative)
|
|
||||||
|
|
||||||
for _, c := range []bool{false, true} {
|
|
||||||
for _, a := range []graphicsdriver.Address{
|
|
||||||
graphicsdriver.AddressClampToZero,
|
|
||||||
graphicsdriver.AddressRepeat,
|
|
||||||
graphicsdriver.AddressUnsafe,
|
|
||||||
} {
|
|
||||||
for _, f := range []graphicsdriver.Filter{
|
|
||||||
graphicsdriver.FilterNearest,
|
|
||||||
graphicsdriver.FilterLinear,
|
|
||||||
} {
|
|
||||||
shaderFragmentColorMatrixNative, err := context.newFragmentShader(fragmentShaderStr(c, f, a))
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
|
|
||||||
}
|
|
||||||
defer context.deleteShader(shaderFragmentColorMatrixNative)
|
|
||||||
|
|
||||||
program, err := context.newProgram([]shader{
|
|
||||||
shaderVertexModelviewNative,
|
|
||||||
shaderFragmentColorMatrixNative,
|
|
||||||
}, theArrayBufferLayout.names())
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s.programs[programKey{
|
|
||||||
useColorM: c,
|
|
||||||
filter: f,
|
|
||||||
address: a,
|
|
||||||
}] = program
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s.arrayBuffer = theArrayBufferLayout.newArrayBuffer(context)
|
s.arrayBuffer = theArrayBufferLayout.newArrayBuffer(context)
|
||||||
|
|
||||||
// Note that the indices passed to NewElementArrayBuffer is not under GC management
|
// Note that the indices passed to NewElementArrayBuffer is not under GC management
|
||||||
|
Loading…
Reference in New Issue
Block a user