GeezerVison is showing github.com — Implement a fragment shader GLSL JIT setting · Hydr8gon/3Beans@3b5000a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/core/gpu/gpu.cpp +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,7 +53,7 @@ void Gpu::createRender() { | 53 | 53 | } | 54 | 54 |
| 55 | 55 | // Initialize a new shader of the current type | 56 | | - switch (shaderType = Settings::gpuShader) { | | 56 | + switch (shaderType = (renderType == 1) ? Settings::gpuVtxShader : 0) { | 57 | 57 | default: gpuShader = new GpuShaderInterp(*gpuRender, shdInput); break; | 58 | 58 | case 1: gpuShader = new GpuShaderGlsl(*(GpuRenderOgl*)gpuRender, shdInput); break; | 59 | 59 | } | @@ -81,7 +81,7 @@ void Gpu::syncRender() { | 81 | 81 | } | 82 | 82 |
| 83 | 83 | // 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; | 85 | 85 | destroyRender(); | 86 | 86 | createRender(); | 87 | 87 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/core/gpu/gpu.h +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -233,6 +233,10 @@ struct GpuThreadTask { | 233 | 233 |
| 234 | 234 | class Gpu { | 235 | 235 | public: | | 236 | + GpuRender *gpuRender = nullptr; | | 237 | + GpuShader *gpuShader = nullptr; | | 238 | + int renderType = -1, shaderType = -1; | | 239 | + | 236 | 240 | Gpu(Core &core, std::function<void()> *contextFunc); | 237 | 241 | ~Gpu(); | 238 | 242 |
| @@ -472,10 +476,6 @@ class Gpu { | 472 | 476 | Core &core; | 473 | 477 | std::function<void()> *contextFunc; | 474 | 478 |
| 475 | | - GpuRender *gpuRender = nullptr; | 476 | | - GpuShader *gpuShader = nullptr; | 477 | | - int renderType = -1, shaderType = -1; | 478 | | - | 479 | 479 | static void (Gpu::*cmdWrites[0x400])(uint32_t, uint32_t); | 480 | 480 | static uint32_t maskTable[0x10]; | 481 | 481 |
| |
Collapse file 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 Copy file name to clipboardExpand all lines: src/core/gpu/gpu_render_ogl.h +40-13Lines changed: 40 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,13 +42,25 @@ struct TexCache { | 42 | 42 | bool operator<(const TexCache &t) const { return addr < t.addr; } | 43 | 43 | }; | 44 | 44 |
| | 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 | + | 45 | 56 | class GpuRenderOgl: public GpuRender { | 46 | 57 | public: | 47 | 58 | GpuRenderOgl(Core &core); | 48 | 59 | ~GpuRenderOgl(); | 49 | 60 |
| 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); | 52 | 64 |
| 53 | 65 | void submitInput(float (*input)[4]); | 54 | 66 | void submitVertex(SoftVertex &vertex); | @@ -110,12 +122,15 @@ class GpuRenderOgl: public GpuRender { | 110 | 122 |
| 111 | 123 | private: | 112 | 124 | Core &core; | 113 | | - GLint fragShader; | 114 | | - GLuint softProgram; | 115 | 125 | GLuint vao, vbo; | 116 | 126 | GLuint colBuf, depBuf; | 117 | 127 | GLuint textures[9]; | 118 | 128 |
| | 129 | + GLint vtxShader; | | 130 | + GLint fragShader; | | 131 | + GLint vtxShaderSoft; | | 132 | + GLint fragShaderUber; | | 133 | + | 119 | 134 | GLint posScaleLoc; | 120 | 135 | GLint combSrcsLoc; | 121 | 136 | GLint combOpersLoc; | @@ -141,7 +156,13 @@ class GpuRenderOgl: public GpuRender { | 141 | 156 | GLint lightMapLoc; | 142 | 157 |
| 143 | 158 | 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; | 145 | 166 |
| 146 | 167 | std::vector<VertexInput> vertices; | 147 | 168 | std::vector<TexCache> texCache; | @@ -158,14 +179,17 @@ class GpuRenderOgl: public GpuRender { | 158 | 179 | TexFmt texFmts[3] = {}; | 159 | 180 | GLint texWrapS[3] = {}; | 160 | 181 | 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; | 169 | 193 |
| 170 | 194 | GLfloat lightSpec0[8][3] = {}; | 171 | 195 | GLfloat lightSpec1[8][3] = {}; | @@ -211,4 +235,7 @@ class GpuRenderOgl: public GpuRender { | 211 | 235 | void updateTextures(); | 212 | 236 | void updateLuts(); | 213 | 237 | void updateViewport(); | | 238 | + | | 239 | + std::string getSrc(int i, int j); | | 240 | + void updateFragShader(); | 214 | 241 | }; |
Collapse file Copy file name to clipboardExpand all lines: src/core/gpu/gpu_shader_glsl.cpp +19-34Lines changed: 19 additions & 34 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -86,27 +86,19 @@ GpuShaderGlsl::GpuShaderGlsl(GpuRenderOgl &gpuRender, float (*input)[4]): gpuRen | 86 | 86 | GpuShaderGlsl::~GpuShaderGlsl() { | 87 | 87 | // Clean up resources that were generated | 88 | 88 | for (int i = 0; i < shaderCache.size(); i++) | 89 | | - glDeleteProgram(shaderCache[i].program); | | 89 | + glDeleteShader(shaderCache[i].shader); | 90 | 90 | glDeleteBuffers(1, &vbo); | 91 | 91 | glDeleteVertexArrays(1, &vao); | 92 | 92 | } | 93 | 93 |
| 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) { | 106 | 95 | // 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); | 110 | 102 | } | 111 | 103 |
| 112 | 104 | void GpuShaderGlsl::processVtx(uint32_t idx) { | @@ -118,18 +110,16 @@ void GpuShaderGlsl::processVtx(uint32_t idx) { | 118 | 110 |
| 119 | 111 | // Calculate comparison values for the current shader | 120 | 112 | 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)); | 124 | 116 | s.entryEnd = (vshEntry << 16) | vshEnd; | 125 | 117 |
| 126 | | - // Use a cached shader program if one is found | | 118 | + // Use a cached vertex shader if one is found | 127 | 119 | for (int i = 0; i < shaderCache.size(); i++) { | 128 | 120 | 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); | 133 | 123 | } | 134 | 124 |
| 135 | 125 | // Emit the vertex shader main function | @@ -179,14 +169,9 @@ void GpuShaderGlsl::processVtx(uint32_t idx) { | 179 | 169 |
| 180 | 170 | // Compile and cache a program from the finished vertex code | 181 | 171 | 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); | 187 | 174 | shaderCache.push_back(s); | 188 | | - current = &shaderCache[shaderCache.size() - 1]; | 189 | | - updateUniforms(); | 190 | 175 | } | 191 | 176 |
| 192 | 177 | 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) { | 601 | 586 | // Update one of the vertex shader boolean uniforms | 602 | 587 | gpuRender.flushVertices(); | 603 | 588 | vshBools[i] = value; | 604 | | - if (current) glUniform1i(current->boolsLoc + i, value); | | 589 | + glUniform1i(boolsLoc + i, value); | 605 | 590 | } | 606 | 591 |
| 607 | 592 | void GpuShaderGlsl::setVshInts(int i, uint8_t int0, uint8_t int1, uint8_t int2) { | 608 | 593 | // Update one of the vertex shader integer uniforms | 609 | 594 | gpuRender.flushVertices(); | 610 | 595 | 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); | 612 | 597 | } | 613 | 598 |
| 614 | 599 | void GpuShaderGlsl::setVshFloats(int i, float *floats) { | 615 | 600 | // Update one of the vertex shader float uniforms | 616 | 601 | gpuRender.flushVertices(); | 617 | 602 | memcpy(vshFloats[i], floats, sizeof(float) * 4); | 618 | | - if (current) glUniform4fv(current->floatsLoc + i, 1, floats); | | 603 | + glUniform4fv(floatsLoc + i, 1, floats); | 619 | 604 | } |
Collapse file Copy file name to clipboardExpand all lines: src/core/gpu/gpu_shader_glsl.h +4-6Lines changed: 4 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 28 | 28 | class GpuRenderOgl; | 29 | 29 |
| 30 | 30 | struct ShaderCache { | 31 | | - GLuint program; | 32 | | - GLint floatsLoc, intsLoc, boolsLoc; | | 31 | + GLint shader; | 33 | 32 | uint32_t codeCrc, descCrc, mapCrc; | 34 | 33 | uint32_t entryEnd; | 35 | 34 | }; | @@ -65,6 +64,8 @@ class GpuShaderGlsl: public GpuShader { | 65 | 64 | void setGshInts(int i, uint8_t int0, uint8_t int1, uint8_t int2) {} | 66 | 65 | void setGshFloats(int i, float *floats) {} | 67 | 66 |
| | 67 | + void updateUniforms(GLuint program); | | 68 | + | 68 | 69 | private: | 69 | 70 | GpuRenderOgl &gpuRender; | 70 | 71 | float (*input)[4]; | @@ -77,7 +78,7 @@ class GpuShaderGlsl: public GpuShader { | 77 | 78 | std::vector<ShaderFunc> shaderFuncs; | 78 | 79 | std::vector<uint32_t> ifStack, loopStack, jmpStack; | 79 | 80 |
| 80 | | - ShaderCache *current = nullptr; | | 81 | + GLint floatsLoc, intsLoc, boolsLoc; | 81 | 82 | uint32_t *shdDesc = vshDesc; | 82 | 83 | uint16_t shdPc, shdStop; | 83 | 84 | bool shaderDirty = false; | @@ -91,11 +92,8 @@ class GpuShaderGlsl: public GpuShader { | 91 | 92 | GLint vshInts[4][3] = {}; | 92 | 93 | float vshFloats[96][4] = {}; | 93 | 94 |
| 94 | | - static uint32_t calcCrc32(uint8_t *data, uint32_t size); | 95 | 95 | static std::string getSrc(uint8_t src, uint32_t desc, uint8_t idx = 0); | 96 | 96 | static std::string setDst(uint8_t dst, uint32_t desc, std::string value, bool single = false); | 97 | | - | 98 | | - void updateUniforms(); | 99 | 97 | void emitFuncBody(std::string &code, uint16_t entry, uint16_t end, bool full = true); | 100 | 98 |
| 101 | 99 | void shdAdd(std::string &code, uint32_t opcode); | |
Collapse file Copy file name to clipboardExpand all lines: src/core/settings.cpp +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,8 @@ namespace Settings { | 26 | 26 | int dspBackend = 0; | 27 | 27 | int threadedGpu = 0; | 28 | 28 | int gpuRenderer = 0; | 29 | | - int gpuShader = 0; | | 29 | + int gpuVtxShader = 0; | | 30 | + int gpuFragShader = 0; | 30 | 31 | int unitType = 0; | 31 | 32 |
| 32 | 33 | std::string boot11Path = "boot11.bin"; | @@ -41,7 +42,8 @@ namespace Settings { | 41 | 42 | Setting("dspBackend", &dspBackend, false), | 42 | 43 | Setting("threadedGpu", &threadedGpu, false), | 43 | 44 | Setting("gpuRenderer", &gpuRenderer, false), | 44 | | - Setting("gpuShader", &gpuShader, false), | | 45 | + Setting("gpuVtxShader", &gpuVtxShader, false), | | 46 | + Setting("gpuFragShader", &gpuFragShader, false), | 45 | 47 | Setting("unitType", &unitType, false), | 46 | 48 | Setting("boot11Path", &boot11Path, true), | 47 | 49 | Setting("boot9Path", &boot9Path, true), | |
Collapse file Copy file name to clipboardExpand all lines: src/core/settings.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,8 @@ namespace Settings { | 37 | 37 | extern int dspBackend; | 38 | 38 | extern int threadedGpu; | 39 | 39 | extern int gpuRenderer; | 40 | | - extern int gpuShader; | | 40 | + extern int gpuVtxShader; | | 41 | + extern int gpuFragShader; | 41 | 42 | extern int unitType; | 42 | 43 |
| 43 | 44 | extern std::string boot11Path; | |
|