Skip to content

Commit cbe351c

Browse files
committed
go constants inserted into generated glsl
makes code clearer and less likely to be wrong
1 parent 7d38c1c commit cbe351c

File tree

5 files changed

+122
-31
lines changed

5 files changed

+122
-31
lines changed

gui/crt/shaders/constants.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This file is part of Gopher2600.
2+
//
3+
// Gopher2600 is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Gopher2600 is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
15+
16+
package shaders
17+
18+
// These constants are used by both Go and GLSL. It is inserted into the GLSL
19+
// by then "go generate" process
20+
21+
// List of valid ImageTypes.
22+
const (
23+
GUI int32 = iota
24+
DebugScr
25+
Overlay
26+
PlayScr
27+
PrefsCRT
28+
)
29+
30+
// List of valid DrawModes.
31+
const (
32+
NoCursor int32 = iota
33+
Cursor
34+
LateCursor
35+
)

gui/crt/shaders/generator/fragment.glsl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
// this file requires some constant values to be defined above this line before
2+
// being compiled. should be included during the "go generate" process
3+
14
uniform int ImageType;
2-
uniform int DrawMode; // 0 == running; 1 = show drawing; 2 = "goto coords"
5+
uniform int DrawMode;
36
uniform int Cropped; // false <= 0; true > 0
47
uniform vec2 ScreenDim;
58
uniform vec2 CropScreenDim;
@@ -52,13 +55,13 @@ float gold_noise(in vec2 xy){
5255
void main()
5356
{
5457
// imgui texture
55-
if (ImageType == 0) {
58+
if (ImageType == GUI) {
5659
Out_Color = vec4(Frag_Color.rgb, Frag_Color.a * texture(Texture, Frag_UV.st).r);
5760
return;
5861
}
5962

6063
// if this is the overlay texture then we're done
61-
if (ImageType == 2) {
64+
if (ImageType == Overlay) {
6265
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
6366
return;
6467
}
@@ -77,7 +80,7 @@ void main()
7780
float texelY;
7881

7982
// debug tv screen texture
80-
if (ImageType == 1) {
83+
if (ImageType == DebugScr) {
8184
if (Cropped > 0) {
8285
texelX = ScalingX / CropScreenDim.x;
8386
texelY = ScalingY / CropScreenDim.y;
@@ -120,9 +123,8 @@ void main()
120123
}
121124
}
122125

123-
// when DrawMode is greater than 0 then there's some additional image
124-
// processing we need to perform
125-
if (DrawMode == 1) {
126+
// when DrawMode is Cursor then there is some additional processing we need to perform
127+
if (DrawMode == Cursor) {
126128
// draw cursor if pixel is at the last x/y position
127129
if (lastY >= 0 && lastX >= 0) {
128130
if (isNearEqual(coords.y, lastY+texelY, cursorSize*texelY) && isNearEqual(coords.x, lastX+texelX, cursorSize*texelX/2)) {
@@ -200,7 +202,7 @@ void main()
200202
// special handling for "Goto Coords" mode. the effect we want is for
201203
// the selected coords to be obvious immediately. we don't want to see
202204
// any screen drawing but we do want the alpha fade.
203-
if (DrawMode == 2) {
205+
if (DrawMode == LateCursor) {
204206
if (coords.y > lastY+texelY || (isNearEqual(coords.y, lastY+texelY, texelY) && coords.x > lastX+texelX)) {
205207
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
206208
Out_Color.a = 0.5;
@@ -217,7 +219,7 @@ void main()
217219
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
218220

219221
// if pixel-perfect rendering is selected then there's nothing much more to do
220-
if (CRT == 0 && ImageType != 4) {
222+
if (CRT == 0 && ImageType != PrefsCRT) {
221223
return;
222224
}
223225

gui/crt/shaders/generator/generate.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package main
1919

2020
import (
2121
"fmt"
22+
"go/ast"
23+
"go/parser"
24+
"go/token"
2225
"io/ioutil"
2326
"os"
2427
"strings"
@@ -44,7 +47,7 @@ func read(filename string) (string, error) {
4447
return string(s), nil
4548
}
4649

47-
func generate() (rb bool) {
50+
func generate(constants constants) (rb bool) {
4851
// open file
4952
vs, err := read(vertexShader)
5053
if err != nil {
@@ -76,7 +79,7 @@ func generate() (rb bool) {
7679
output.WriteString("// Code generated by hardware/gui/sdlimgui/shaders/generate.go DO NOT EDIT\n")
7780
output.WriteString("\npackage shaders\n\n")
7881
output.WriteString(fmt.Sprintf("const Vertex = %s + `\n%s`\n\n", glslVersion, vs))
79-
output.WriteString(fmt.Sprintf("const Fragment = %s + `\n%s`\n", glslVersion, fs))
82+
output.WriteString(fmt.Sprintf("const Fragment = %s + `\n%s\n%s`\n", glslVersion, constants.String(), fs))
8083

8184
_, err = f.WriteString(output.String())
8285
if err != nil {
@@ -89,8 +92,48 @@ func generate() (rb bool) {
8992
return true
9093
}
9194

95+
type constants map[string]int
96+
97+
func (c *constants) String() string {
98+
s := strings.Builder{}
99+
for k, v := range *c {
100+
s.WriteString(fmt.Sprintf("const int %s = %d;\n", k, v))
101+
}
102+
return s.String()
103+
}
104+
105+
func getConstants() (constants, error) {
106+
fset := token.NewFileSet()
107+
p, err := parser.ParseFile(fset, "../constants.go", nil, 0)
108+
if err != nil {
109+
fmt.Printf("finding constants: %s\n", err)
110+
return nil, err
111+
}
112+
113+
m := make(constants)
114+
115+
ast.Inspect(p, func(n ast.Node) bool {
116+
switch n := n.(type) {
117+
case *ast.Ident:
118+
if n.IsExported() {
119+
if n.Obj.Kind == ast.Con {
120+
m[n.Obj.Name] = n.Obj.Data.(int)
121+
}
122+
}
123+
}
124+
return true
125+
})
126+
127+
return m, nil
128+
}
129+
92130
func main() {
93-
if !generate() {
131+
constants, err := getConstants()
132+
if err != nil {
133+
os.Exit(10)
134+
}
135+
136+
if !generate(constants) {
94137
os.Exit(10)
95138
}
96139
}

gui/crt/shaders/shaders.go

Lines changed: 20 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gui/sdlimgui/glsl.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,19 @@ func (rnd *glsl) render(displaySize [2]float32, framebufferSize [2]float32, draw
257257
// set DrawMode according to emulation state
258258
switch rnd.img.state {
259259
case gui.StatePaused:
260-
gl.Uniform1i(rnd.attribDrawMode, 1)
260+
gl.Uniform1i(rnd.attribDrawMode, shaders.Cursor)
261261
case gui.StateRunning:
262262
// if FPS is low enough then show screen draw even though
263263
// emulation is running
264264
if rnd.img.lz.TV.ReqFPS < 3.0 {
265-
gl.Uniform1i(rnd.attribDrawMode, 1)
265+
gl.Uniform1i(rnd.attribDrawMode, shaders.NoCursor)
266266
} else {
267-
gl.Uniform1i(rnd.attribDrawMode, 0)
267+
gl.Uniform1i(rnd.attribDrawMode, shaders.Cursor)
268268
}
269269
case gui.StateRewinding:
270-
gl.Uniform1i(rnd.attribDrawMode, 0)
270+
gl.Uniform1i(rnd.attribDrawMode, shaders.NoCursor)
271271
case gui.StateGotoCoords:
272-
gl.Uniform1i(rnd.attribDrawMode, 2)
272+
gl.Uniform1i(rnd.attribDrawMode, shaders.LateCursor)
273273
}
274274

275275
if rnd.img.wm.dbgScr.cropped {
@@ -305,15 +305,15 @@ func (rnd *glsl) render(displaySize [2]float32, framebufferSize [2]float32, draw
305305
textureID := uint32(cmd.TextureID())
306306
switch textureID {
307307
case rnd.img.wm.dbgScr.screenTexture:
308-
gl.Uniform1i(rnd.attribImageType, 1)
308+
gl.Uniform1i(rnd.attribImageType, shaders.DebugScr)
309309
case rnd.img.wm.dbgScr.overlayTexture:
310-
gl.Uniform1i(rnd.attribImageType, 2)
310+
gl.Uniform1i(rnd.attribImageType, shaders.Overlay)
311311
case rnd.img.wm.playScr.screenTexture:
312-
gl.Uniform1i(rnd.attribImageType, 3)
312+
gl.Uniform1i(rnd.attribImageType, shaders.PlayScr)
313313
case rnd.img.wm.crtPrefs.crtTexture:
314-
gl.Uniform1i(rnd.attribImageType, 4)
314+
gl.Uniform1i(rnd.attribImageType, shaders.PrefsCRT)
315315
default:
316-
gl.Uniform1i(rnd.attribImageType, 0)
316+
gl.Uniform1i(rnd.attribImageType, shaders.GUI)
317317
}
318318

319319
// clipping

0 commit comments

Comments
 (0)