diff --git a/desmume/src/OGLRender.cpp b/desmume/src/OGLRender.cpp index d1bd178a2..30482745a 100644 --- a/desmume/src/OGLRender.cpp +++ b/desmume/src/OGLRender.cpp @@ -44,7 +44,7 @@ static void ENDGL() { oglrender_endOpenGL(); } -#ifdef _WIN32 && !defined(WXPORT) +#if defined(_WIN32) && !defined(WXPORT) #define WIN32_LEAN_AND_MEAN #include #include @@ -337,6 +337,8 @@ static void OGLReset() } TexCache_Reset(); + if (currTexture) + delete currTexture; currTexture = NULL; // memset(GPU_screenStencil,0,sizeof(GPU_screenStencil)); @@ -565,7 +567,7 @@ static void setTexture(unsigned int format, unsigned int texpal) currTexture->texid = (u64)freeTextureIds.front(); freeTextureIds.pop(); - glBindTexture(GL_TEXTURE_2D,(GLuint)currTexture->texid); + glBindTexture(GL_TEXTURE_2D,(GLuint)currTexture->texid); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -818,17 +820,14 @@ static void OGLRender() if(hasShaders) { - //TODO - maybe this should only happen if the toon table is stale (for a slight speedup) + if (gfx3d.state.invalidateToon) + { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_1D, oglToonTableTextureID); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_1D, oglToonTableTextureID); - - //generate a 8888 toon table from the ds format one and store it in a texture - u32 rgbToonTable[32]; - for(int i=0;i<32;i++) - rgbToonTable[i] = RGB15TO32(gfx3d.state.u16ToonTable[i], 255); - - glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgbToonTable); + glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, &gfx3d.state.rgbToonTable[0]); + gfx3d.state.invalidateToon = false; + } } xglDepthMask(GL_TRUE); diff --git a/desmume/src/gfx3d.cpp b/desmume/src/gfx3d.cpp index 7ab9c2d57..3ed996cb5 100644 --- a/desmume/src/gfx3d.cpp +++ b/desmume/src/gfx3d.cpp @@ -566,6 +566,8 @@ void gfx3d_reset() GFX_PIPEclear(); GFX_FIFOclear(); + + gfx3d.state.invalidateToon = true; } @@ -1613,14 +1615,16 @@ int gfx3d_GetNumVertex() void gfx3d_UpdateToonTable(u8 offset, u16 val) { - gfx3d.state.u16ToonTable[offset] = val; + gfx3d.state.rgbToonTable[offset] = RGB15TO32(val, 255); + gfx3d.state.invalidateToon = true; } void gfx3d_UpdateToonTable(u8 offset, u32 val) { //C.O.P. sets toon table via this method - gfx3d.state.u16ToonTable[offset] = val & 0xFFFF; - gfx3d.state.u16ToonTable[offset+1] = val >> 16; + gfx3d.state.rgbToonTable[offset] = RGB15TO32(val & 0xFFFF, 255); + gfx3d.state.rgbToonTable[offset+1] = RGB15TO32(val >> 16, 255); + gfx3d.state.invalidateToon = true; } s32 gfx3d_GetClipMatrix (unsigned int index) @@ -2205,7 +2209,7 @@ SFORMAT SF_GFX3D[]={ { "GSCD", 4, 1, &gfx3d.state.clearDepth}, { "GSFC", 4, 4, &gfx3d.state.fogColor}, { "GSFO", 4, 1, &gfx3d.state.fogOffset}, - { "GST2", 2, 32, gfx3d.state.u16ToonTable}, + { "GST3", 4, 32, gfx3d.state.rgbToonTable}, { "GSST", 4, 128, &gfx3d.state.shininessTable[0]}, { "GSSI", 4, 1, &shininessInd}, //------------------------ diff --git a/desmume/src/gfx3d.h b/desmume/src/gfx3d.h index b2a54c4bb..3c5120f2e 100644 --- a/desmume/src/gfx3d.h +++ b/desmume/src/gfx3d.h @@ -312,11 +312,13 @@ struct GFX3D_State , fogColor(0) , fogOffset(0) , fogShift(0) + , invalidateToon(true) { - for(u32 i=0;i>5)&0x1F); - toonTable[i].b = GFX3D_5TO6((gfx3d.state.u16ToonTable[i]>>10)&0x1F); + toonTable[i].r = (gfx3d.state.rgbToonTable[i] >> 2) & 0x3F; + toonTable[i].g = (gfx3d.state.rgbToonTable[i] >> 10) & 0x3F; + toonTable[i].b = (gfx3d.state.rgbToonTable[i] >> 18) & 0x3F; } + gfx3d.state.invalidateToon = false; } void SoftRasterizerEngine::updateFogTable() diff --git a/desmume/src/shaders.h b/desmume/src/shaders.h index 4cd2cbf6e..9a3160288 100644 --- a/desmume/src/shaders.h +++ b/desmume/src/shaders.h @@ -17,62 +17,41 @@ const char *fragmentShader = {"\ uniform int hasTexture; \n\ uniform int texBlending; \n\ \n\ - vec4 float_to_6bit(in vec4 color) \n\ - { \n\ - vec4 ret = color * vec4(31.0,31.0,31.0,31.0);\n\ - \n\ - if(ret.r > 0.0) ret.r = (ret.r * 2.0) + 1.0; \n\ - if(ret.g > 0.0) ret.g = (ret.g * 2.0) + 1.0; \n\ - if(ret.b > 0.0) ret.b = (ret.b * 2.0) + 1.0; \n\ - if(ret.a > 0.0) ret.a = (ret.a * 2.0) + 1.0; \n\ - \n\ - return ret; \n\ - } \n\ - \n\ + \ void main() \n\ { \n\ - vec4 vtxColor = float_to_6bit(gl_Color); \n\ - vec4 texColor = float_to_6bit(texture2D(tex2d, gl_TexCoord[0].st)); \n\ - vec3 toonColor = vec3(float_to_6bit(vec4(texture1D(toonTable, gl_Color.r).rgb, 0.0))); \n\ - vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n\ - \n\ - if(hasTexture == 0) \n\ + vec4 texColor = vec4(1.0, 1.0, 1.0, 1.0); \n\ + vec4 flagColor; \n\ + \ + if(hasTexture != 0) \n\ { \n\ - texColor = vec4(63.0, 63.0, 63.0, 63.0); \n\ + texColor = texture2D(tex2d, gl_TexCoord[0].st); \n\ } \n\ - \n\ + flagColor = texColor; \n\ if(texBlending == 0) \n\ { \n\ - fragColor = ((texColor + 1.0) * (vtxColor + 1.0) - 1.0) / 64.0; \n\ + flagColor = gl_Color * texColor; \n\ } \n\ - else if(texBlending == 1) \n\ - { \n\ - if(texColor.a == 0.0 || hasTexture == 0) \n\ + else \n\ + if(texBlending == 1) \n\ { \n\ - fragColor.rgb = vtxColor.rgb; \n\ - } \n\ - else if(texColor.a == 63.0) \n\ - { \n\ - fragColor.rgb = texColor.rgb; \n\ + flagColor.rgb = gl_Color.rgb * (1.0-texColor.a) + texColor.rgb * texColor.a;\n\ + flagColor.a = texColor.a;\n\ } \n\ else \n\ - { \n\ - fragColor.rgb = ((texColor.rgb * texColor.a) + (vtxColor.rgb * (63.0 - texColor.a))) / 64.0; \n\ - } \n\ - \n\ - fragColor.a = vtxColor.a; \n\ - } \n\ - else if(texBlending == 2) \n\ - { \n\ - fragColor.rgb = ((texColor.rgb + 1.0) * (toonColor + 1.0) - 1.0) / 64.0; \n\ - fragColor.a = ((texColor.a + 1.0) * (vtxColor.a + 1.0) - 1.0) / 64.0; \n\ - } \n\ - else if(texBlending == 3) \n\ - { \n\ - fragColor.rgb = min((((texColor.rgb + 1.0) * (toonColor + 1.0) - 1.0) / 64.0) + toonColor, 63.0); \n\ - fragColor.a = ((texColor.a + 1.0) * (vtxColor.a + 1.0) - 1.0) / 64.0; \n\ - } \n\ - \n\ - gl_FragColor = ((fragColor - 1.0) / 2.0) / 31.0; \n\ + if(texBlending == 2) \n\ + { \n\ + vec3 toonColor = vec3(texture1D(toonTable, gl_Color.r).rgb); \n\ + flagColor.rgb = texColor.rgb * toonColor.rgb;\n\ + flagColor.a = texColor.a * gl_Color.a;\n\ + } \n\ + else \n\ + if(texBlending == 3) \n\ + { \n\ + vec3 toonColor = vec3(texture1D(toonTable, gl_Color.r).rgb); \n\ + flagColor.rgb = texColor.rgb * gl_Color.rgb + toonColor.rgb; \n\ + flagColor.a = texColor.a * gl_Color.a; \n\ + } \n\ + gl_FragColor = flagColor; \n\ } \n\ "};