GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Implement a fragment shader GLSL JIT setting · Hydr8gon/3Beans@3b5000a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • core
      • gpu
        • gpu.cpp
        • gpu.h
        • gpu_render_ogl.cpp
        • gpu_render_ogl.h
        • gpu_shader_glsl.cpp
        • gpu_shader_glsl.h
      • settings.cpp
      • settings.h
    • desktop
      • b3_frame.cpp
      • b3_frame.h
      • gpu_dialog.cpp
      • gpu_dialog.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/core/gpu/gpu.cpp‎

Copy file name to clipboardExpand all lines: src/core/gpu/gpu.cpp
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void Gpu::createRender() {
5353
}
5454

5555
// Initialize a new shader of the current type
56-
switch (shaderType = Settings::gpuShader) {
56+
switch (shaderType = (renderType == 1) ? Settings::gpuVtxShader : 0) {
5757
default: gpuShader = new GpuShaderInterp(*gpuRender, shdInput); break;
5858
case 1: gpuShader = new GpuShaderGlsl(*(GpuRenderOgl*)gpuRender, shdInput); break;
5959
}
@@ -81,7 +81,7 @@ void Gpu::syncRender() {
8181
}
8282

8383
// Reset the renderer if it was changed
84-
if (renderType == Settings::gpuRenderer && shaderType == Settings::gpuShader) return;
84+
if (renderType == Settings::gpuRenderer && shaderType == Settings::gpuVtxShader) return;
8585
destroyRender();
8686
createRender();
8787

Collapse file

‎src/core/gpu/gpu.h‎

Copy file name to clipboardExpand all lines: src/core/gpu/gpu.h
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ struct GpuThreadTask {
233233

234234
class Gpu {
235235
public:
236+
GpuRender *gpuRender = nullptr;
237+
GpuShader *gpuShader = nullptr;
238+
int renderType = -1, shaderType = -1;
239+
236240
Gpu(Core &core, std::function<void()> *contextFunc);
237241
~Gpu();
238242

@@ -472,10 +476,6 @@ class Gpu {
472476
Core &core;
473477
std::function<void()> *contextFunc;
474478

475-
GpuRender *gpuRender = nullptr;
476-
GpuShader *gpuShader = nullptr;
477-
int renderType = -1, shaderType = -1;
478-
479479
static void (Gpu::*cmdWrites[0x400])(uint32_t, uint32_t);
480480
static uint32_t maskTable[0x10];
481481

Collapse file

‎src/core/gpu/gpu_render_ogl.cpp‎

Copy file name to clipboardExpand all lines: src/core/gpu/gpu_render_ogl.cpp
+224-68Lines changed: 224 additions & 68 deletions
Large diffs are not rendered by default.
Collapse file

‎src/core/gpu/gpu_render_ogl.h‎

Copy file name to clipboardExpand all lines: src/core/gpu/gpu_render_ogl.h
+40-13Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,25 @@ struct TexCache {
4242
bool operator<(const TexCache &t) const { return addr < t.addr; }
4343
};
4444

45+
struct ProgramCache {
46+
GLuint program;
47+
GLint vtxShader;
48+
GLint fragShader;
49+
};
50+
51+
struct FragCache {
52+
GLint shader;
53+
uint32_t dataCrc;
54+
};
55+
4556
class GpuRenderOgl: public GpuRender {
4657
public:
4758
GpuRenderOgl(Core &core);
4859
~GpuRenderOgl();
4960

50-
GLuint makeProgram(const char *vtxCode);
51-
void setProgram(GLuint program);
61+
static uint32_t calcCrc32(uint8_t *data, uint32_t size);
62+
static GLint makeShader(const char *code, bool frag);
63+
void setShader(GLint shader, bool frag);
5264

5365
void submitInput(float (*input)[4]);
5466
void submitVertex(SoftVertex &vertex);
@@ -110,12 +122,15 @@ class GpuRenderOgl: public GpuRender {
110122

111123
private:
112124
Core &core;
113-
GLint fragShader;
114-
GLuint softProgram;
115125
GLuint vao, vbo;
116126
GLuint colBuf, depBuf;
117127
GLuint textures[9];
118128

129+
GLint vtxShader;
130+
GLint fragShader;
131+
GLint vtxShaderSoft;
132+
GLint fragShaderUber;
133+
119134
GLint posScaleLoc;
120135
GLint combSrcsLoc;
121136
GLint combOpersLoc;
@@ -141,7 +156,13 @@ class GpuRenderOgl: public GpuRender {
141156
GLint lightMapLoc;
142157

143158
static const char *vtxCodeSoft;
144-
static const char *fragCode;
159+
static const char *fragBase;
160+
static const char *fragBodyUber;
161+
162+
std::vector<ProgramCache> programCache;
163+
std::vector<FragCache> fragCache;
164+
bool fragDirty = false;
165+
bool useLights = false;
145166

146167
std::vector<VertexInput> vertices;
147168
std::vector<TexCache> texCache;
@@ -158,14 +179,17 @@ class GpuRenderOgl: public GpuRender {
158179
TexFmt texFmts[3] = {};
159180
GLint texWrapS[3] = {};
160181
GLint texWrapT[3] = {};
161-
GLint combSrcs[12][3] = {};
162-
GLint combOpers[12][3] = {};
163-
GLint combModes[6][2] = {};
164-
GLfloat combColors[6][4] = {};
165-
GLfloat combBufColor[4] = {};
166-
uint8_t combBufMask = 0;
167-
TestFunc alphaFunc = TEST_AL;
168-
float alphaValue = 0;
182+
183+
struct CombData {
184+
GLint combSrcs[12][3] = {};
185+
GLint combOpers[12][3] = {};
186+
GLint combModes[6][2] = {};
187+
GLfloat combColors[6][4] = {};
188+
GLfloat combBufColor[4] = {};
189+
uint8_t combBufMask = 0;
190+
TestFunc alphaFunc = TEST_AL;
191+
float alphaValue = 0;
192+
} cd;
169193

170194
GLfloat lightSpec0[8][3] = {};
171195
GLfloat lightSpec1[8][3] = {};
@@ -211,4 +235,7 @@ class GpuRenderOgl: public GpuRender {
211235
void updateTextures();
212236
void updateLuts();
213237
void updateViewport();
238+
239+
std::string getSrc(int i, int j);
240+
void updateFragShader();
214241
};
Collapse file

‎src/core/gpu/gpu_shader_glsl.cpp‎

Copy file name to clipboardExpand all lines: src/core/gpu/gpu_shader_glsl.cpp
+19-34Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,19 @@ GpuShaderGlsl::GpuShaderGlsl(GpuRenderOgl &gpuRender, float (*input)[4]): gpuRen
8686
GpuShaderGlsl::~GpuShaderGlsl() {
8787
// Clean up resources that were generated
8888
for (int i = 0; i < shaderCache.size(); i++)
89-
glDeleteProgram(shaderCache[i].program);
89+
glDeleteShader(shaderCache[i].shader);
9090
glDeleteBuffers(1, &vbo);
9191
glDeleteVertexArrays(1, &vao);
9292
}
9393

94-
uint32_t GpuShaderGlsl::calcCrc32(uint8_t *data, uint32_t size) {
95-
// Calculate a CRC32 for the given data
96-
uint32_t crc = 0xFFFFFFFF;
97-
for (int i = 0; i < size; i++) {
98-
crc ^= data[i];
99-
for (int j = 0; j < 8; j++)
100-
crc = (crc & 0x1) ? ((crc >> 1) ^ 0xEDB88320) : (crc >> 1);
101-
}
102-
return crc;
103-
}
104-
105-
void GpuShaderGlsl::updateUniforms() {
94+
void GpuShaderGlsl::updateUniforms(GLuint program) {
10695
// Update all uniforms when the shader changes
107-
glUniform4fv(current->floatsLoc, 96, vshFloats[0]);
108-
glUniform3iv(current->intsLoc, 4, vshInts[0]);
109-
glUniform1iv(current->boolsLoc, 16, vshBools);
96+
floatsLoc = glGetUniformLocation(program, "floats");
97+
intsLoc = glGetUniformLocation(program, "ints");
98+
boolsLoc = glGetUniformLocation(program, "bools");
99+
glUniform4fv(floatsLoc, 96, vshFloats[0]);
100+
glUniform3iv(intsLoc, 4, vshInts[0]);
101+
glUniform1iv(boolsLoc, 16, vshBools);
110102
}
111103

112104
void GpuShaderGlsl::processVtx(uint32_t idx) {
@@ -118,18 +110,16 @@ void GpuShaderGlsl::processVtx(uint32_t idx) {
118110

119111
// Calculate comparison values for the current shader
120112
ShaderCache s;
121-
s.codeCrc = calcCrc32((uint8_t*)vshCode, sizeof(vshCode));
122-
s.descCrc = calcCrc32((uint8_t*)vshDesc, sizeof(vshDesc));
123-
s.mapCrc = calcCrc32((uint8_t*)outMap, sizeof(outMap));
113+
s.codeCrc = gpuRender.calcCrc32((uint8_t*)vshCode, sizeof(vshCode));
114+
s.descCrc = gpuRender.calcCrc32((uint8_t*)vshDesc, sizeof(vshDesc));
115+
s.mapCrc = gpuRender.calcCrc32((uint8_t*)outMap, sizeof(outMap));
124116
s.entryEnd = (vshEntry << 16) | vshEnd;
125117

126-
// Use a cached shader program if one is found
118+
// Use a cached vertex shader if one is found
127119
for (int i = 0; i < shaderCache.size(); i++) {
128120
ShaderCache &c = shaderCache[i];
129-
if (c.codeCrc != s.codeCrc || c.descCrc != s.descCrc || c.mapCrc != s.mapCrc || c.entryEnd != s.entryEnd)
130-
continue;
131-
gpuRender.setProgram((current = &c)->program);
132-
return updateUniforms();
121+
if (c.codeCrc == s.codeCrc && c.descCrc == s.descCrc && c.mapCrc == s.mapCrc && c.entryEnd == s.entryEnd)
122+
return gpuRender.setShader(c.shader, false);
133123
}
134124

135125
// Emit the vertex shader main function
@@ -179,14 +169,9 @@ void GpuShaderGlsl::processVtx(uint32_t idx) {
179169

180170
// Compile and cache a program from the finished vertex code
181171
LOG_INFO("Caching GLSL shader with CRCs 0x%X, 0x%X, and 0x%X\n", s.codeCrc, s.descCrc, s.mapCrc);
182-
s.program = gpuRender.makeProgram(vtxCode.c_str());
183-
gpuRender.setProgram(s.program);
184-
s.floatsLoc = glGetUniformLocation(s.program, "floats");
185-
s.intsLoc = glGetUniformLocation(s.program, "ints");
186-
s.boolsLoc = glGetUniformLocation(s.program, "bools");
172+
s.shader = gpuRender.makeShader(vtxCode.c_str(), false);
173+
gpuRender.setShader(s.shader, false);
187174
shaderCache.push_back(s);
188-
current = &shaderCache[shaderCache.size() - 1];
189-
updateUniforms();
190175
}
191176

192177
void GpuShaderGlsl::emitFuncBody(std::string &code, uint16_t entry, uint16_t end, bool full) {
@@ -601,19 +586,19 @@ void GpuShaderGlsl::setVshBool(int i, bool value) {
601586
// Update one of the vertex shader boolean uniforms
602587
gpuRender.flushVertices();
603588
vshBools[i] = value;
604-
if (current) glUniform1i(current->boolsLoc + i, value);
589+
glUniform1i(boolsLoc + i, value);
605590
}
606591

607592
void GpuShaderGlsl::setVshInts(int i, uint8_t int0, uint8_t int1, uint8_t int2) {
608593
// Update one of the vertex shader integer uniforms
609594
gpuRender.flushVertices();
610595
vshInts[i][0] = int0, vshInts[i][1] = int0, vshInts[i][2] = int2;
611-
if (current) glUniform3i(current->intsLoc + i, int0, int1, int2);
596+
glUniform3i(intsLoc + i, int0, int1, int2);
612597
}
613598

614599
void GpuShaderGlsl::setVshFloats(int i, float *floats) {
615600
// Update one of the vertex shader float uniforms
616601
gpuRender.flushVertices();
617602
memcpy(vshFloats[i], floats, sizeof(float) * 4);
618-
if (current) glUniform4fv(current->floatsLoc + i, 1, floats);
603+
glUniform4fv(floatsLoc + i, 1, floats);
619604
}
Collapse file

‎src/core/gpu/gpu_shader_glsl.h‎

Copy file name to clipboardExpand all lines: src/core/gpu/gpu_shader_glsl.h
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
class GpuRenderOgl;
2929

3030
struct ShaderCache {
31-
GLuint program;
32-
GLint floatsLoc, intsLoc, boolsLoc;
31+
GLint shader;
3332
uint32_t codeCrc, descCrc, mapCrc;
3433
uint32_t entryEnd;
3534
};
@@ -65,6 +64,8 @@ class GpuShaderGlsl: public GpuShader {
6564
void setGshInts(int i, uint8_t int0, uint8_t int1, uint8_t int2) {}
6665
void setGshFloats(int i, float *floats) {}
6766

67+
void updateUniforms(GLuint program);
68+
6869
private:
6970
GpuRenderOgl &gpuRender;
7071
float (*input)[4];
@@ -77,7 +78,7 @@ class GpuShaderGlsl: public GpuShader {
7778
std::vector<ShaderFunc> shaderFuncs;
7879
std::vector<uint32_t> ifStack, loopStack, jmpStack;
7980

80-
ShaderCache *current = nullptr;
81+
GLint floatsLoc, intsLoc, boolsLoc;
8182
uint32_t *shdDesc = vshDesc;
8283
uint16_t shdPc, shdStop;
8384
bool shaderDirty = false;
@@ -91,11 +92,8 @@ class GpuShaderGlsl: public GpuShader {
9192
GLint vshInts[4][3] = {};
9293
float vshFloats[96][4] = {};
9394

94-
static uint32_t calcCrc32(uint8_t *data, uint32_t size);
9595
static std::string getSrc(uint8_t src, uint32_t desc, uint8_t idx = 0);
9696
static std::string setDst(uint8_t dst, uint32_t desc, std::string value, bool single = false);
97-
98-
void updateUniforms();
9997
void emitFuncBody(std::string &code, uint16_t entry, uint16_t end, bool full = true);
10098

10199
void shdAdd(std::string &code, uint32_t opcode);
Collapse file

‎src/core/settings.cpp‎

Copy file name to clipboardExpand all lines: src/core/settings.cpp
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ namespace Settings {
2626
int dspBackend = 0;
2727
int threadedGpu = 0;
2828
int gpuRenderer = 0;
29-
int gpuShader = 0;
29+
int gpuVtxShader = 0;
30+
int gpuFragShader = 0;
3031
int unitType = 0;
3132

3233
std::string boot11Path = "boot11.bin";
@@ -41,7 +42,8 @@ namespace Settings {
4142
Setting("dspBackend", &dspBackend, false),
4243
Setting("threadedGpu", &threadedGpu, false),
4344
Setting("gpuRenderer", &gpuRenderer, false),
44-
Setting("gpuShader", &gpuShader, false),
45+
Setting("gpuVtxShader", &gpuVtxShader, false),
46+
Setting("gpuFragShader", &gpuFragShader, false),
4547
Setting("unitType", &unitType, false),
4648
Setting("boot11Path", &boot11Path, true),
4749
Setting("boot9Path", &boot9Path, true),
Collapse file

‎src/core/settings.h‎

Copy file name to clipboardExpand all lines: src/core/settings.h
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ namespace Settings {
3737
extern int dspBackend;
3838
extern int threadedGpu;
3939
extern int gpuRenderer;
40-
extern int gpuShader;
40+
extern int gpuVtxShader;
41+
extern int gpuFragShader;
4142
extern int unitType;
4243

4344
extern std::string boot11Path;

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.