diff --git a/desmume/src/OGLRender.cpp b/desmume/src/OGLRender.cpp index 61017eed0..1335a9206 100644 --- a/desmume/src/OGLRender.cpp +++ b/desmume/src/OGLRender.cpp @@ -372,19 +372,12 @@ static const char *EdgeMarkFragShader_100 = {"\ \n\ void main()\n\ {\n\ - vec4 inPolyIDAttributes[5];\n\ - inPolyIDAttributes[0] = texture2D(texInPolyID, texCoord[0]);\n\ - inPolyIDAttributes[1] = texture2D(texInPolyID, texCoord[1]);\n\ - inPolyIDAttributes[2] = texture2D(texInPolyID, texCoord[2]);\n\ - inPolyIDAttributes[3] = texture2D(texInPolyID, texCoord[3]);\n\ - inPolyIDAttributes[4] = texture2D(texInPolyID, texCoord[4]);\n\ - \n\ int polyID[5];\n\ - polyID[0] = int((inPolyIDAttributes[0].r * 63.0) + 0.5);\n\ - polyID[1] = int((inPolyIDAttributes[1].r * 63.0) + 0.5);\n\ - polyID[2] = int((inPolyIDAttributes[2].r * 63.0) + 0.5);\n\ - polyID[3] = int((inPolyIDAttributes[3].r * 63.0) + 0.5);\n\ - polyID[4] = int((inPolyIDAttributes[4].r * 63.0) + 0.5);\n\ + polyID[0] = int((texture2D(texInPolyID, texCoord[0]).r * 63.0) + 0.5);\n\ + polyID[1] = int((texture2D(texInPolyID, texCoord[1]).r * 63.0) + 0.5);\n\ + polyID[2] = int((texture2D(texInPolyID, texCoord[2]).r * 63.0) + 0.5);\n\ + polyID[3] = int((texture2D(texInPolyID, texCoord[3]).r * 63.0) + 0.5);\n\ + polyID[4] = int((texture2D(texInPolyID, texCoord[4]).r * 63.0) + 0.5);\n\ \n\ float depth[5];\n\ depth[0] = unpackFloatFromVec3(texture2D(texInFragDepth, texCoord[0]).rgb);\n\ diff --git a/desmume/src/OGLRender.h b/desmume/src/OGLRender.h index 910178147..4cf7c4a33 100644 --- a/desmume/src/OGLRender.h +++ b/desmume/src/OGLRender.h @@ -353,6 +353,7 @@ struct OGLRenderStates GLvec4 fogColor; GLvec4 fogDensity[32]; // Array of floats need to be padded as vec4 GLvec4 edgeColor[8]; + GLvec4 toonColor[32]; }; struct OGLPolyStates @@ -499,7 +500,7 @@ extern GPU3DInterface gpu3Dgl_3_2; extern const GLenum RenderDrawList[4]; extern CACHE_ALIGN const GLfloat divide5bitBy31_LUT[32]; -extern const GLfloat PostprocessVtxBuffer[16];; +extern const GLfloat PostprocessVtxBuffer[16]; extern const GLubyte PostprocessElementBuffer[6]; extern void texDeleteCallback(TexCacheItem *item); diff --git a/desmume/src/OGLRender_3_2.cpp b/desmume/src/OGLRender_3_2.cpp index 8f1ac31a7..4e26ddb74 100644 --- a/desmume/src/OGLRender_3_2.cpp +++ b/desmume/src/OGLRender_3_2.cpp @@ -178,10 +178,10 @@ static const char *GeometryFragShader_150 = {"\ vec4 fogColor;\n\ float fogDensity[32];\n\ vec4 edgeColor[8];\n\ + vec4 toonColor[32];\n\ } state;\n\ \n\ uniform sampler2D texRenderObject; \n\ - uniform sampler1D texToonTable; \n\ uniform usamplerBuffer PolyStates;\n\ uniform int polyIndex;\n\ \n\ @@ -209,8 +209,8 @@ static const char *GeometryFragShader_150 = {"\ } \n\ else if (polyMode == 2u) \n\ { \n\ - vec3 toonColor = vec3(texture(texToonTable, vtxColor.r).rgb); \n\ - newFragColor.rgb = (state.toonShadingMode == 0) ? mainTexColor.rgb * toonColor.rgb : min((mainTexColor.rgb * vtxColor.rgb) + toonColor.rgb, 1.0); \n\ + vec3 newToonColor = state.toonColor[int((vtxColor.r * 31.0) + 0.5)].rgb;\n\ + newFragColor.rgb = (state.toonShadingMode == 0) ? mainTexColor.rgb * newToonColor.rgb : min((mainTexColor.rgb * vtxColor.rgb) + newToonColor.rgb, 1.0); \n\ } \n\ else if (polyMode == 3u) \n\ { \n\ @@ -258,6 +258,7 @@ static const char *EdgeMarkVtxShader_150 = {"\ vec4 fogColor;\n\ float fogDensity[32];\n\ vec4 edgeColor[8];\n\ + vec4 toonColor[32];\n\ } state;\n\ \n\ out vec2 texCoord[5];\n\ @@ -298,6 +299,7 @@ static const char *EdgeMarkFragShader_150 = {"\ vec4 fogColor;\n\ float fogDensity[32];\n\ vec4 edgeColor[8];\n\ + vec4 toonColor[32];\n\ } state;\n\ \n\ uniform sampler2D texInFragDepth;\n\ @@ -313,19 +315,12 @@ static const char *EdgeMarkFragShader_150 = {"\ \n\ void main()\n\ {\n\ - vec4 inPolyIDAttributes[5];\n\ - inPolyIDAttributes[0] = texture(texInPolyID, texCoord[0]);\n\ - inPolyIDAttributes[1] = texture(texInPolyID, texCoord[1]);\n\ - inPolyIDAttributes[2] = texture(texInPolyID, texCoord[2]);\n\ - inPolyIDAttributes[3] = texture(texInPolyID, texCoord[3]);\n\ - inPolyIDAttributes[4] = texture(texInPolyID, texCoord[4]);\n\ - \n\ int polyID[5];\n\ - polyID[0] = int((inPolyIDAttributes[0].r * 63.0) + 0.5);\n\ - polyID[1] = int((inPolyIDAttributes[1].r * 63.0) + 0.5);\n\ - polyID[2] = int((inPolyIDAttributes[2].r * 63.0) + 0.5);\n\ - polyID[3] = int((inPolyIDAttributes[3].r * 63.0) + 0.5);\n\ - polyID[4] = int((inPolyIDAttributes[4].r * 63.0) + 0.5);\n\ + polyID[0] = int((texture(texInPolyID, texCoord[0]).r * 63.0) + 0.5);\n\ + polyID[1] = int((texture(texInPolyID, texCoord[1]).r * 63.0) + 0.5);\n\ + polyID[2] = int((texture(texInPolyID, texCoord[2]).r * 63.0) + 0.5);\n\ + polyID[3] = int((texture(texInPolyID, texCoord[3]).r * 63.0) + 0.5);\n\ + polyID[4] = int((texture(texInPolyID, texCoord[4]).r * 63.0) + 0.5);\n\ \n\ float depth[5];\n\ depth[0] = unpackFloatFromVec3(texture(texInFragDepth, texCoord[0]).rgb);\n\ @@ -386,6 +381,7 @@ static const char *FogFragShader_150 = {"\ vec4 fogColor;\n\ float fogDensity[32];\n\ vec4 edgeColor[8];\n\ + vec4 toonColor[32];\n\ } state;\n\ \n\ uniform sampler2D texInFragColor;\n\ @@ -944,10 +940,8 @@ Render3DError OpenGLRenderer_3_2::InitGeometryProgramShaderLocations() glActiveTexture(GL_TEXTURE0); OGLRef.uniformTexRenderObject = glGetUniformLocation(OGLRef.programGeometryID, "texRenderObject"); - OGLRef.uniformTexToonTable = glGetUniformLocation(OGLRef.programGeometryID, "texToonTable"); OGLRef.uniformTexBufferPolyStates = glGetUniformLocation(OGLRef.programGeometryID, "PolyStates"); glUniform1i(OGLRef.uniformTexRenderObject, 0); - glUniform1i(OGLRef.uniformTexToonTable, OGLTextureUnitID_ToonTable); glUniform1i(OGLRef.uniformTexBufferPolyStates, OGLTextureUnitID_PolyStates); OGLRef.uniformPolyStateIndex = glGetUniformLocation(OGLRef.programGeometryID, "polyIndex"); @@ -1091,6 +1085,14 @@ Render3DError OpenGLRenderer_3_2::BeginRender(const GFX3D &engine) state->edgeColor[i].a = edgeColorAlpha; } + for (size_t i = 0; i < 32; i++) + { + state->toonColor[i].r = divide5bitBy31_LUT[(engine.renderState.u16ToonTable[i] ) & 0x001F]; + state->toonColor[i].g = divide5bitBy31_LUT[(engine.renderState.u16ToonTable[i] >> 5) & 0x001F]; + state->toonColor[i].b = divide5bitBy31_LUT[(engine.renderState.u16ToonTable[i] >> 10) & 0x001F]; + state->toonColor[i].a = 1.0f; + } + glUnmapBuffer(GL_UNIFORM_BUFFER); // Do per-poly setup @@ -1205,6 +1207,24 @@ Render3DError OpenGLRenderer_3_2::RenderFog(const u8 *densityTable, const u32 co return OGLERROR_NOERR; } +Render3DError OpenGLRenderer_3_2::CreateToonTable() +{ + // Do nothing. The toon table is updated in the render states UBO. + return OGLERROR_NOERR; +} + +Render3DError OpenGLRenderer_3_2::DestroyToonTable() +{ + // Do nothing. The toon table is updated in the render states UBO. + return OGLERROR_NOERR; +} + +Render3DError OpenGLRenderer_3_2::UpdateToonTable(const u16 *toonTableBuffer) +{ + // Do nothing. The toon table is updated in the render states UBO. + return OGLERROR_NOERR; +} + Render3DError OpenGLRenderer_3_2::ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer) { OGLRenderRef &OGLRef = *this->ref; diff --git a/desmume/src/OGLRender_3_2.h b/desmume/src/OGLRender_3_2.h index 45370e23e..d047077b6 100644 --- a/desmume/src/OGLRender_3_2.h +++ b/desmume/src/OGLRender_3_2.h @@ -85,6 +85,9 @@ protected: virtual Render3DError RenderEdgeMarking(const u16 *colorTable, const bool useAntialias); virtual Render3DError RenderFog(const u8 *densityTable, const u32 color, const u32 offset, const u8 shift, const bool alphaOnly); + virtual Render3DError CreateToonTable(); + virtual Render3DError DestroyToonTable(); + virtual Render3DError UpdateToonTable(const u16 *toonTableBuffer); virtual Render3DError ClearUsingImage(const u16 *__restrict colorBuffer, const u32 *__restrict depthBuffer, const bool *__restrict fogBuffer, const u8 *__restrict polyIDBuffer); virtual Render3DError ClearUsingValues(const FragmentColor &clearColor, const FragmentAttributes &clearAttributes) const;