diff --git a/desmume/src/OGLRender.cpp b/desmume/src/OGLRender.cpp index 144544c02..018399924 100755 --- a/desmume/src/OGLRender.cpp +++ b/desmume/src/OGLRender.cpp @@ -638,7 +638,7 @@ uniform sampler2D texInFragDepth;\n\ uniform sampler2D texInFogAttributes;\n\ uniform bool stateEnableFogAlphaOnly;\n\ uniform vec4 stateFogColor;\n\ -uniform float stateFogDensity[32];\n\ +uniform vec4 stateFogDensity[32];\n\ \n\ void main()\n\ {\n\ @@ -654,135 +654,135 @@ void main()\n\ \n\ if (inFragDepth <= FOG_DEPTH_COMPARE_0)\n\ {\n\ - fogMixWeight = stateFogDensity[0];\n\ + fogMixWeight = stateFogDensity[0].r;\n\ }\n\ else if (inFragDepth >= FOG_DEPTH_COMPARE_31)\n\ {\n\ - fogMixWeight = stateFogDensity[31];\n\ + fogMixWeight = stateFogDensity[31].r;\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_1)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 0], stateFogDensity[ 1], (inFragDepth - FOG_DEPTH_COMPARE_0) * FOG_DEPTH_INVDIFF_1);\n\ + fogMixWeight = mix(stateFogDensity[ 1].g, stateFogDensity[ 1].r, (inFragDepth - FOG_DEPTH_COMPARE_0) * FOG_DEPTH_INVDIFF_1);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_2)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 1], stateFogDensity[ 2], (inFragDepth - FOG_DEPTH_COMPARE_1) * FOG_DEPTH_INVDIFF_2);\n\ + fogMixWeight = mix(stateFogDensity[ 2].g, stateFogDensity[ 2].r, (inFragDepth - FOG_DEPTH_COMPARE_1) * FOG_DEPTH_INVDIFF_2);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_3)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 2], stateFogDensity[ 3], (inFragDepth - FOG_DEPTH_COMPARE_2) * FOG_DEPTH_INVDIFF_3);\n\ + fogMixWeight = mix(stateFogDensity[ 3].g, stateFogDensity[ 3].r, (inFragDepth - FOG_DEPTH_COMPARE_2) * FOG_DEPTH_INVDIFF_3);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_4)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 3], stateFogDensity[ 4], (inFragDepth - FOG_DEPTH_COMPARE_3) * FOG_DEPTH_INVDIFF_4);\n\ + fogMixWeight = mix(stateFogDensity[ 4].g, stateFogDensity[ 4].r, (inFragDepth - FOG_DEPTH_COMPARE_3) * FOG_DEPTH_INVDIFF_4);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_5)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 4], stateFogDensity[ 5], (inFragDepth - FOG_DEPTH_COMPARE_4) * FOG_DEPTH_INVDIFF_5);\n\ + fogMixWeight = mix(stateFogDensity[ 5].g, stateFogDensity[ 5].r, (inFragDepth - FOG_DEPTH_COMPARE_4) * FOG_DEPTH_INVDIFF_5);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_6)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 5], stateFogDensity[ 6], (inFragDepth - FOG_DEPTH_COMPARE_5) * FOG_DEPTH_INVDIFF_6);\n\ + fogMixWeight = mix(stateFogDensity[ 6].g, stateFogDensity[ 6].r, (inFragDepth - FOG_DEPTH_COMPARE_5) * FOG_DEPTH_INVDIFF_6);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_7)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 6], stateFogDensity[ 7], (inFragDepth - FOG_DEPTH_COMPARE_6) * FOG_DEPTH_INVDIFF_7);\n\ + fogMixWeight = mix(stateFogDensity[ 7].g, stateFogDensity[ 7].r, (inFragDepth - FOG_DEPTH_COMPARE_6) * FOG_DEPTH_INVDIFF_7);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_8)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 7], stateFogDensity[ 8], (inFragDepth - FOG_DEPTH_COMPARE_7) * FOG_DEPTH_INVDIFF_8);\n\ + fogMixWeight = mix(stateFogDensity[ 8].g, stateFogDensity[ 8].r, (inFragDepth - FOG_DEPTH_COMPARE_7) * FOG_DEPTH_INVDIFF_8);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_9)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 8], stateFogDensity[ 9], (inFragDepth - FOG_DEPTH_COMPARE_8) * FOG_DEPTH_INVDIFF_9);\n\ + fogMixWeight = mix(stateFogDensity[ 9].g, stateFogDensity[ 9].r, (inFragDepth - FOG_DEPTH_COMPARE_8) * FOG_DEPTH_INVDIFF_9);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_10)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[ 9], stateFogDensity[10], (inFragDepth - FOG_DEPTH_COMPARE_9) * FOG_DEPTH_INVDIFF_10);\n\ + fogMixWeight = mix(stateFogDensity[10].g, stateFogDensity[10].r, (inFragDepth - FOG_DEPTH_COMPARE_9) * FOG_DEPTH_INVDIFF_10);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_11)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[10], stateFogDensity[11], (inFragDepth - FOG_DEPTH_COMPARE_10) * FOG_DEPTH_INVDIFF_11);\n\ + fogMixWeight = mix(stateFogDensity[11].g, stateFogDensity[11].r, (inFragDepth - FOG_DEPTH_COMPARE_10) * FOG_DEPTH_INVDIFF_11);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_12)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[11], stateFogDensity[12], (inFragDepth - FOG_DEPTH_COMPARE_11) * FOG_DEPTH_INVDIFF_12);\n\ + fogMixWeight = mix(stateFogDensity[12].g, stateFogDensity[12].r, (inFragDepth - FOG_DEPTH_COMPARE_11) * FOG_DEPTH_INVDIFF_12);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_13)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[12], stateFogDensity[13], (inFragDepth - FOG_DEPTH_COMPARE_12) * FOG_DEPTH_INVDIFF_13);\n\ + fogMixWeight = mix(stateFogDensity[13].g, stateFogDensity[13].r, (inFragDepth - FOG_DEPTH_COMPARE_12) * FOG_DEPTH_INVDIFF_13);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_14)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[13], stateFogDensity[14], (inFragDepth - FOG_DEPTH_COMPARE_13) * FOG_DEPTH_INVDIFF_14);\n\ + fogMixWeight = mix(stateFogDensity[14].g, stateFogDensity[14].r, (inFragDepth - FOG_DEPTH_COMPARE_13) * FOG_DEPTH_INVDIFF_14);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_15)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[14], stateFogDensity[15], (inFragDepth - FOG_DEPTH_COMPARE_14) * FOG_DEPTH_INVDIFF_15);\n\ + fogMixWeight = mix(stateFogDensity[15].g, stateFogDensity[15].r, (inFragDepth - FOG_DEPTH_COMPARE_14) * FOG_DEPTH_INVDIFF_15);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_16)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[15], stateFogDensity[16], (inFragDepth - FOG_DEPTH_COMPARE_15) * FOG_DEPTH_INVDIFF_16);\n\ + fogMixWeight = mix(stateFogDensity[16].g, stateFogDensity[16].r, (inFragDepth - FOG_DEPTH_COMPARE_15) * FOG_DEPTH_INVDIFF_16);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_17)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[16], stateFogDensity[17], (inFragDepth - FOG_DEPTH_COMPARE_16) * FOG_DEPTH_INVDIFF_17);\n\ + fogMixWeight = mix(stateFogDensity[17].g, stateFogDensity[17].r, (inFragDepth - FOG_DEPTH_COMPARE_16) * FOG_DEPTH_INVDIFF_17);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_18)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[17], stateFogDensity[18], (inFragDepth - FOG_DEPTH_COMPARE_17) * FOG_DEPTH_INVDIFF_18);\n\ + fogMixWeight = mix(stateFogDensity[18].g, stateFogDensity[18].r, (inFragDepth - FOG_DEPTH_COMPARE_17) * FOG_DEPTH_INVDIFF_18);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_19)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[18], stateFogDensity[19], (inFragDepth - FOG_DEPTH_COMPARE_18) * FOG_DEPTH_INVDIFF_19);\n\ + fogMixWeight = mix(stateFogDensity[19].g, stateFogDensity[19].r, (inFragDepth - FOG_DEPTH_COMPARE_18) * FOG_DEPTH_INVDIFF_19);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_20)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[19], stateFogDensity[20], (inFragDepth - FOG_DEPTH_COMPARE_19) * FOG_DEPTH_INVDIFF_20);\n\ + fogMixWeight = mix(stateFogDensity[20].g, stateFogDensity[20].r, (inFragDepth - FOG_DEPTH_COMPARE_19) * FOG_DEPTH_INVDIFF_20);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_21)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[20], stateFogDensity[21], (inFragDepth - FOG_DEPTH_COMPARE_20) * FOG_DEPTH_INVDIFF_21);\n\ + fogMixWeight = mix(stateFogDensity[21].g, stateFogDensity[21].r, (inFragDepth - FOG_DEPTH_COMPARE_20) * FOG_DEPTH_INVDIFF_21);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_22)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[21], stateFogDensity[22], (inFragDepth - FOG_DEPTH_COMPARE_21) * FOG_DEPTH_INVDIFF_22);\n\ + fogMixWeight = mix(stateFogDensity[22].g, stateFogDensity[22].r, (inFragDepth - FOG_DEPTH_COMPARE_21) * FOG_DEPTH_INVDIFF_22);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_23)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[22], stateFogDensity[23], (inFragDepth - FOG_DEPTH_COMPARE_22) * FOG_DEPTH_INVDIFF_23);\n\ + fogMixWeight = mix(stateFogDensity[23].g, stateFogDensity[23].r, (inFragDepth - FOG_DEPTH_COMPARE_22) * FOG_DEPTH_INVDIFF_23);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_24)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[23], stateFogDensity[24], (inFragDepth - FOG_DEPTH_COMPARE_23) * FOG_DEPTH_INVDIFF_24);\n\ + fogMixWeight = mix(stateFogDensity[24].g, stateFogDensity[24].r, (inFragDepth - FOG_DEPTH_COMPARE_23) * FOG_DEPTH_INVDIFF_24);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_25)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[24], stateFogDensity[25], (inFragDepth - FOG_DEPTH_COMPARE_24) * FOG_DEPTH_INVDIFF_25);\n\ + fogMixWeight = mix(stateFogDensity[25].g, stateFogDensity[25].r, (inFragDepth - FOG_DEPTH_COMPARE_24) * FOG_DEPTH_INVDIFF_25);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_26)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[25], stateFogDensity[26], (inFragDepth - FOG_DEPTH_COMPARE_25) * FOG_DEPTH_INVDIFF_26);\n\ + fogMixWeight = mix(stateFogDensity[26].g, stateFogDensity[26].r, (inFragDepth - FOG_DEPTH_COMPARE_25) * FOG_DEPTH_INVDIFF_26);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_27)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[26], stateFogDensity[27], (inFragDepth - FOG_DEPTH_COMPARE_26) * FOG_DEPTH_INVDIFF_27);\n\ + fogMixWeight = mix(stateFogDensity[27].g, stateFogDensity[27].r, (inFragDepth - FOG_DEPTH_COMPARE_26) * FOG_DEPTH_INVDIFF_27);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_28)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[27], stateFogDensity[28], (inFragDepth - FOG_DEPTH_COMPARE_27) * FOG_DEPTH_INVDIFF_28);\n\ + fogMixWeight = mix(stateFogDensity[28].g, stateFogDensity[28].r, (inFragDepth - FOG_DEPTH_COMPARE_27) * FOG_DEPTH_INVDIFF_28);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_29)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[28], stateFogDensity[29], (inFragDepth - FOG_DEPTH_COMPARE_28) * FOG_DEPTH_INVDIFF_29);\n\ + fogMixWeight = mix(stateFogDensity[29].g, stateFogDensity[29].r, (inFragDepth - FOG_DEPTH_COMPARE_28) * FOG_DEPTH_INVDIFF_29);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_30)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[29], stateFogDensity[30], (inFragDepth - FOG_DEPTH_COMPARE_29) * FOG_DEPTH_INVDIFF_30);\n\ + fogMixWeight = mix(stateFogDensity[30].g, stateFogDensity[30].r, (inFragDepth - FOG_DEPTH_COMPARE_29) * FOG_DEPTH_INVDIFF_30);\n\ }\n\ else if (inFragDepth <= FOG_DEPTH_COMPARE_31)\n\ {\n\ - fogMixWeight = mix(stateFogDensity[30], stateFogDensity[31], (inFragDepth - FOG_DEPTH_COMPARE_30) * FOG_DEPTH_INVDIFF_31);\n\ + fogMixWeight = mix(stateFogDensity[31].g, stateFogDensity[31].r, (inFragDepth - FOG_DEPTH_COMPARE_30) * FOG_DEPTH_INVDIFF_31);\n\ }\n\ \n\ newFoggedColor = mix(inFragColor, (stateEnableFogAlphaOnly) ? vec4(inFragColor.rgb, stateFogColor.a) : stateFogColor, fogMixWeight);\n\ @@ -4514,7 +4514,8 @@ Render3DError OpenGLRenderer_1_2::BeginRender(const GFX3D &engine) for (size_t i = 0; i < 32; i++) { - this->_pendingRenderStates.fogDensity[i] = (engine.renderState.fogDensityTable[i] == 127) ? 1.0f : (GLfloat)engine.renderState.fogDensityTable[i] / 128.0f; + this->_pendingRenderStates.fogDensity[i].r = (engine.renderState.fogDensityTable[i] == 127) ? 1.0f : (GLfloat)engine.renderState.fogDensityTable[i] / 128.0f; // Current table value is stored in r + this->_pendingRenderStates.fogDensity[i].g = (i == 0) ? this->_pendingRenderStates.fogDensity[0].r : this->_pendingRenderStates.fogDensity[i-1].r; // Previous table value is stored in g } } @@ -4759,7 +4760,7 @@ Render3DError OpenGLRenderer_1_2::PostprocessFramebuffer() glUseProgram(shaderID.program); glUniform1i(OGLRef.uniformStateEnableFogAlphaOnly, this->_pendingRenderStates.enableFogAlphaOnly); glUniform4fv(OGLRef.uniformStateFogColor, 1, (const GLfloat *)&this->_pendingRenderStates.fogColor); - glUniform1fv(OGLRef.uniformStateFogDensity, 32, (const GLfloat *)this->_pendingRenderStates.fogDensity); + glUniform4fv(OGLRef.uniformStateFogDensity, 32, (const GLfloat *)this->_pendingRenderStates.fogDensity); glDisable(GL_STENCIL_TEST); glDisable(GL_BLEND); @@ -5739,7 +5740,8 @@ Render3DError OpenGLRenderer_2_0::BeginRender(const GFX3D &engine) for (size_t i = 0; i < 32; i++) { - this->_pendingRenderStates.fogDensity[i] = (engine.renderState.fogDensityTable[i] == 127) ? 1.0f : (GLfloat)engine.renderState.fogDensityTable[i] / 128.0f; + this->_pendingRenderStates.fogDensity[i].r = (engine.renderState.fogDensityTable[i] == 127) ? 1.0f : (GLfloat)engine.renderState.fogDensityTable[i] / 128.0f; // Current table value is stored in r + this->_pendingRenderStates.fogDensity[i].g = (i == 0) ? this->_pendingRenderStates.fogDensity[0].r : this->_pendingRenderStates.fogDensity[i-1].r; // Previous table value is stored in g } } diff --git a/desmume/src/OGLRender.h b/desmume/src/OGLRender.h index 86fb084a8..6337b6520 100755 --- a/desmume/src/OGLRender.h +++ b/desmume/src/OGLRender.h @@ -400,7 +400,7 @@ struct OGLRenderStates GLfloat fogOffset; GLfloat fogStep; GLfloat pad_0; // This needs to be here to preserve alignment - GLfloat fogDensity[32]; + GLvec4 fogDensity[32]; GLvec4 fogColor; GLvec4 edgeColor[8]; GLvec4 toonColor[32]; @@ -432,7 +432,7 @@ union OGLPolyStates union OGLGeometryFlags { u8 value; - + #ifndef MSB_FIRST struct { @@ -443,32 +443,32 @@ union OGLGeometryFlags u8 EnableAlphaTest:1; u8 EnableTextureSampling:1; u8 ToonShadingMode:1; - u8 unused:1; - }; - - struct - { - u8 DrawBuffersMode:3; - u8 :5; - }; -#else - struct - { - u8 unused:1; - u8 ToonShadingMode:1; - u8 EnableTextureSampling:1; - u8 EnableAlphaTest:1; - u8 EnableWDepth:1; - u8 OpaqueDrawMode:1; - u8 EnableEdgeMark:1; - u8 EnableFog:1; - }; - - struct - { - u8 :5; - u8 DrawBuffersMode:3; - }; + u8 unused:1; + }; + + struct + { + u8 DrawBuffersMode:3; + u8 :5; + }; +#else + struct + { + u8 unused:1; + u8 ToonShadingMode:1; + u8 EnableTextureSampling:1; + u8 EnableAlphaTest:1; + u8 EnableWDepth:1; + u8 OpaqueDrawMode:1; + u8 EnableEdgeMark:1; + u8 EnableFog:1; + }; + + struct + { + u8 :5; + u8 DrawBuffersMode:3; + }; #endif }; typedef OGLGeometryFlags OGLGeometryFlags; diff --git a/desmume/src/OGLRender_3_2.cpp b/desmume/src/OGLRender_3_2.cpp index d4a93c413..50fea5c03 100755 --- a/desmume/src/OGLRender_3_2.cpp +++ b/desmume/src/OGLRender_3_2.cpp @@ -206,38 +206,7 @@ layout (std140) uniform RenderStates\n\ float fogOffset;\n\ float fogStep;\n\ float pad_0;\n\ - float fogDensity_00;\n\ - float fogDensity_01;\n\ - float fogDensity_02;\n\ - float fogDensity_03;\n\ - float fogDensity_04;\n\ - float fogDensity_05;\n\ - float fogDensity_06;\n\ - float fogDensity_07;\n\ - float fogDensity_08;\n\ - float fogDensity_09;\n\ - float fogDensity_10;\n\ - float fogDensity_11;\n\ - float fogDensity_12;\n\ - float fogDensity_13;\n\ - float fogDensity_14;\n\ - float fogDensity_15;\n\ - float fogDensity_16;\n\ - float fogDensity_17;\n\ - float fogDensity_18;\n\ - float fogDensity_19;\n\ - float fogDensity_20;\n\ - float fogDensity_21;\n\ - float fogDensity_22;\n\ - float fogDensity_23;\n\ - float fogDensity_24;\n\ - float fogDensity_25;\n\ - float fogDensity_26;\n\ - float fogDensity_27;\n\ - float fogDensity_28;\n\ - float fogDensity_29;\n\ - float fogDensity_30;\n\ - float fogDensity_31;\n\ + vec4 fogDensity[32];\n\ vec4 fogColor;\n\ vec4 edgeColor[8];\n\ vec4 toonColor[32];\n\ @@ -436,38 +405,7 @@ layout (std140) uniform RenderStates\n\ float fogOffset;\n\ float fogStep;\n\ float pad_0;\n\ - float fogDensity_00;\n\ - float fogDensity_01;\n\ - float fogDensity_02;\n\ - float fogDensity_03;\n\ - float fogDensity_04;\n\ - float fogDensity_05;\n\ - float fogDensity_06;\n\ - float fogDensity_07;\n\ - float fogDensity_08;\n\ - float fogDensity_09;\n\ - float fogDensity_10;\n\ - float fogDensity_11;\n\ - float fogDensity_12;\n\ - float fogDensity_13;\n\ - float fogDensity_14;\n\ - float fogDensity_15;\n\ - float fogDensity_16;\n\ - float fogDensity_17;\n\ - float fogDensity_18;\n\ - float fogDensity_19;\n\ - float fogDensity_20;\n\ - float fogDensity_21;\n\ - float fogDensity_22;\n\ - float fogDensity_23;\n\ - float fogDensity_24;\n\ - float fogDensity_25;\n\ - float fogDensity_26;\n\ - float fogDensity_27;\n\ - float fogDensity_28;\n\ - float fogDensity_29;\n\ - float fogDensity_30;\n\ - float fogDensity_31;\n\ + vec4 fogDensity[32];\n\ vec4 fogColor;\n\ vec4 edgeColor[8];\n\ vec4 toonColor[32];\n\ @@ -585,38 +523,7 @@ layout (std140) uniform RenderStates\n\ float fogOffset;\n\ float fogStep;\n\ float pad_0;\n\ - float fogDensity_00;\n\ - float fogDensity_01;\n\ - float fogDensity_02;\n\ - float fogDensity_03;\n\ - float fogDensity_04;\n\ - float fogDensity_05;\n\ - float fogDensity_06;\n\ - float fogDensity_07;\n\ - float fogDensity_08;\n\ - float fogDensity_09;\n\ - float fogDensity_10;\n\ - float fogDensity_11;\n\ - float fogDensity_12;\n\ - float fogDensity_13;\n\ - float fogDensity_14;\n\ - float fogDensity_15;\n\ - float fogDensity_16;\n\ - float fogDensity_17;\n\ - float fogDensity_18;\n\ - float fogDensity_19;\n\ - float fogDensity_20;\n\ - float fogDensity_21;\n\ - float fogDensity_22;\n\ - float fogDensity_23;\n\ - float fogDensity_24;\n\ - float fogDensity_25;\n\ - float fogDensity_26;\n\ - float fogDensity_27;\n\ - float fogDensity_28;\n\ - float fogDensity_29;\n\ - float fogDensity_30;\n\ - float fogDensity_31;\n\ + vec4 fogDensity[32];\n\ vec4 fogColor;\n\ vec4 edgeColor[8];\n\ vec4 toonColor[32];\n\ @@ -642,147 +549,26 @@ void main()\n\ outFragColor = texelFetch(texInFragColor, ivec2(gl_FragCoord.xy), 0);\n\ #endif\n\ \n\ + float inFragDepth = texelFetch(texInFragDepth, ivec2(gl_FragCoord.xy), 0).r;\n\ vec4 inFogAttributes = texelFetch(texInFogAttributes, ivec2(gl_FragCoord.xy), 0);\n\ bool polyEnableFog = (inFogAttributes.r > 0.999);\n\ \n\ if (polyEnableFog)\n\ {\n\ - float inFragDepth = texelFetch(texInFragDepth, ivec2(gl_FragCoord.xy), 0).r;\n\ - float fogMixWeight = 0.0;\n\ + int inFragDepthi = int( (inFragDepth * 32767.0) + 0.5 );\n\ + int diffi = inFragDepthi - FOG_OFFSET + (FOG_STEP - 1);\n\ \n\ - if (inFragDepth <= FOG_DEPTH_COMPARE_0)\n\ + float interp = 1.0;\n\ + if ( (inFragDepth > FOG_DEPTH_COMPARE_0) && (inFragDepth < FOG_DEPTH_COMPARE_31) )\n\ {\n\ - fogMixWeight = state.fogDensity_00;\n\ - }\n\ - else if (inFragDepth >= FOG_DEPTH_COMPARE_31)\n\ - {\n\ - fogMixWeight = state.fogDensity_31;\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_1)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_00, state.fogDensity_01, (inFragDepth - FOG_DEPTH_COMPARE_0) * FOG_DEPTH_INVDIFF_1);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_2)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_01, state.fogDensity_02, (inFragDepth - FOG_DEPTH_COMPARE_1) * FOG_DEPTH_INVDIFF_2);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_3)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_02, state.fogDensity_03, (inFragDepth - FOG_DEPTH_COMPARE_2) * FOG_DEPTH_INVDIFF_3);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_4)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_03, state.fogDensity_04, (inFragDepth - FOG_DEPTH_COMPARE_3) * FOG_DEPTH_INVDIFF_4);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_5)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_04, state.fogDensity_05, (inFragDepth - FOG_DEPTH_COMPARE_4) * FOG_DEPTH_INVDIFF_5);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_6)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_05, state.fogDensity_06, (inFragDepth - FOG_DEPTH_COMPARE_5) * FOG_DEPTH_INVDIFF_6);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_7)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_06, state.fogDensity_07, (inFragDepth - FOG_DEPTH_COMPARE_6) * FOG_DEPTH_INVDIFF_7);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_8)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_07, state.fogDensity_08, (inFragDepth - FOG_DEPTH_COMPARE_7) * FOG_DEPTH_INVDIFF_8);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_9)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_08, state.fogDensity_09, (inFragDepth - FOG_DEPTH_COMPARE_8) * FOG_DEPTH_INVDIFF_9);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_10)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_09, state.fogDensity_10, (inFragDepth - FOG_DEPTH_COMPARE_9) * FOG_DEPTH_INVDIFF_10);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_11)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_10, state.fogDensity_11, (inFragDepth - FOG_DEPTH_COMPARE_10) * FOG_DEPTH_INVDIFF_11);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_12)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_11, state.fogDensity_12, (inFragDepth - FOG_DEPTH_COMPARE_11) * FOG_DEPTH_INVDIFF_12);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_13)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_12, state.fogDensity_13, (inFragDepth - FOG_DEPTH_COMPARE_12) * FOG_DEPTH_INVDIFF_13);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_14)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_13, state.fogDensity_14, (inFragDepth - FOG_DEPTH_COMPARE_13) * FOG_DEPTH_INVDIFF_14);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_15)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_14, state.fogDensity_15, (inFragDepth - FOG_DEPTH_COMPARE_14) * FOG_DEPTH_INVDIFF_15);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_16)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_15, state.fogDensity_16, (inFragDepth - FOG_DEPTH_COMPARE_15) * FOG_DEPTH_INVDIFF_16);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_17)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_16, state.fogDensity_17, (inFragDepth - FOG_DEPTH_COMPARE_16) * FOG_DEPTH_INVDIFF_17);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_18)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_17, state.fogDensity_18, (inFragDepth - FOG_DEPTH_COMPARE_17) * FOG_DEPTH_INVDIFF_18);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_19)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_18, state.fogDensity_19, (inFragDepth - FOG_DEPTH_COMPARE_18) * FOG_DEPTH_INVDIFF_19);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_20)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_19, state.fogDensity_20, (inFragDepth - FOG_DEPTH_COMPARE_19) * FOG_DEPTH_INVDIFF_20);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_21)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_20, state.fogDensity_21, (inFragDepth - FOG_DEPTH_COMPARE_20) * FOG_DEPTH_INVDIFF_21);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_22)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_21, state.fogDensity_22, (inFragDepth - FOG_DEPTH_COMPARE_21) * FOG_DEPTH_INVDIFF_22);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_23)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_22, state.fogDensity_23, (inFragDepth - FOG_DEPTH_COMPARE_22) * FOG_DEPTH_INVDIFF_23);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_24)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_23, state.fogDensity_24, (inFragDepth - FOG_DEPTH_COMPARE_23) * FOG_DEPTH_INVDIFF_24);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_25)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_24, state.fogDensity_25, (inFragDepth - FOG_DEPTH_COMPARE_24) * FOG_DEPTH_INVDIFF_25);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_26)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_25, state.fogDensity_26, (inFragDepth - FOG_DEPTH_COMPARE_25) * FOG_DEPTH_INVDIFF_26);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_27)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_26, state.fogDensity_27, (inFragDepth - FOG_DEPTH_COMPARE_26) * FOG_DEPTH_INVDIFF_27);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_28)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_27, state.fogDensity_28, (inFragDepth - FOG_DEPTH_COMPARE_27) * FOG_DEPTH_INVDIFF_28);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_29)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_28, state.fogDensity_29, (inFragDepth - FOG_DEPTH_COMPARE_28) * FOG_DEPTH_INVDIFF_29);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_30)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_29, state.fogDensity_30, (inFragDepth - FOG_DEPTH_COMPARE_29) * FOG_DEPTH_INVDIFF_30);\n\ - }\n\ - else if (inFragDepth <= FOG_DEPTH_COMPARE_31)\n\ - {\n\ - fogMixWeight = mix(state.fogDensity_30, state.fogDensity_31, (inFragDepth - FOG_DEPTH_COMPARE_30) * FOG_DEPTH_INVDIFF_31);\n\ + interp = float( (diffi & FOG_WEIGHT_TRUNCATE_MASK) + FOG_OFFSET - inFragDepthi ) / float(FOG_STEP);\n\ }\n\ \n\ + int idx = (diffi >> FOG_SHIFT_INV) - 1;\n\ + idx = (idx < 1) ? 0 : (idx > 31) ? 31 : idx;\n\ + \n\ + float fogMixWeight = mix(state.fogDensity[idx].r, state.fogDensity[idx].g, interp);\n\ + \n\ #if USE_DUAL_SOURCE_BLENDING\n\ outFogWeight = (state.enableFogAlphaOnly) ? vec4(vec3(0.0), fogMixWeight) : vec4(fogMixWeight);\n\ #else\n\ @@ -1766,43 +1552,13 @@ Render3DError OpenGLRenderer_3_2::CreateFogProgram(const OGLFogProgramKey fogPro return error; } - const u16 fogOffset = fogProgramKey.offset; - const u16 fogShift = (0x0400 >> fogProgramKey.shift); + const u32 fogOffset = fogProgramKey.offset; + const u32 fogStep = (0x0400 >> fogProgramKey.shift); + const u32 fogShiftInv = 10 - fogProgramKey.shift; + const u32 fogWeightTruncateMask = ~(fogStep - 1); - const GLfloat fogDepthCompare[32] = { - std::min((GLfloat)(fogOffset + (fogShift * 1)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 2)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 3)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 4)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 5)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 6)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 7)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 8)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 9)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 10)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 11)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 12)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 13)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 14)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 15)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 16)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 17)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 18)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 19)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 20)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 21)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 22)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 23)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 24)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 25)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 26)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 27)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 28)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 29)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 30)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 31)) / 32767.0f, 1.0f), - std::min((GLfloat)(fogOffset + (fogShift * 32)) / 32767.0f, 1.0f) - }; + const GLfloat fogDepthCompare0 = std::min((GLfloat)(fogOffset + (fogStep * 1)) / 32767.0f, 1.0f); + const GLfloat fogDepthCompare31 = std::min((GLfloat)(fogOffset + (fogStep * 32)) / 32767.0f, 1.0f); std::stringstream shaderHeader; shaderHeader << "#version 150\n"; @@ -1810,71 +1566,12 @@ Render3DError OpenGLRenderer_3_2::CreateFogProgram(const OGLFogProgramKey fogPro shaderHeader << "\n"; std::stringstream fragDepthConstants; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_0 " << fogDepthCompare[ 0] << (((fogDepthCompare[ 0] == 0.0f) || (fogDepthCompare[ 0] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_1 " << fogDepthCompare[ 1] << (((fogDepthCompare[ 1] == 0.0f) || (fogDepthCompare[ 1] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_2 " << fogDepthCompare[ 2] << (((fogDepthCompare[ 2] == 0.0f) || (fogDepthCompare[ 2] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_3 " << fogDepthCompare[ 3] << (((fogDepthCompare[ 3] == 0.0f) || (fogDepthCompare[ 3] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_4 " << fogDepthCompare[ 4] << (((fogDepthCompare[ 4] == 0.0f) || (fogDepthCompare[ 4] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_5 " << fogDepthCompare[ 5] << (((fogDepthCompare[ 5] == 0.0f) || (fogDepthCompare[ 5] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_6 " << fogDepthCompare[ 6] << (((fogDepthCompare[ 6] == 0.0f) || (fogDepthCompare[ 6] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_7 " << fogDepthCompare[ 7] << (((fogDepthCompare[ 7] == 0.0f) || (fogDepthCompare[ 7] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_8 " << fogDepthCompare[ 8] << (((fogDepthCompare[ 8] == 0.0f) || (fogDepthCompare[ 8] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_9 " << fogDepthCompare[ 9] << (((fogDepthCompare[ 9] == 0.0f) || (fogDepthCompare[ 9] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_10 " << fogDepthCompare[10] << (((fogDepthCompare[10] == 0.0f) || (fogDepthCompare[10] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_11 " << fogDepthCompare[11] << (((fogDepthCompare[11] == 0.0f) || (fogDepthCompare[11] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_12 " << fogDepthCompare[12] << (((fogDepthCompare[12] == 0.0f) || (fogDepthCompare[12] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_13 " << fogDepthCompare[13] << (((fogDepthCompare[13] == 0.0f) || (fogDepthCompare[13] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_14 " << fogDepthCompare[14] << (((fogDepthCompare[14] == 0.0f) || (fogDepthCompare[14] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_15 " << fogDepthCompare[15] << (((fogDepthCompare[15] == 0.0f) || (fogDepthCompare[15] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_16 " << fogDepthCompare[16] << (((fogDepthCompare[16] == 0.0f) || (fogDepthCompare[16] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_17 " << fogDepthCompare[17] << (((fogDepthCompare[17] == 0.0f) || (fogDepthCompare[17] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_18 " << fogDepthCompare[18] << (((fogDepthCompare[18] == 0.0f) || (fogDepthCompare[18] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_19 " << fogDepthCompare[19] << (((fogDepthCompare[19] == 0.0f) || (fogDepthCompare[19] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_20 " << fogDepthCompare[20] << (((fogDepthCompare[20] == 0.0f) || (fogDepthCompare[20] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_21 " << fogDepthCompare[21] << (((fogDepthCompare[21] == 0.0f) || (fogDepthCompare[21] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_22 " << fogDepthCompare[22] << (((fogDepthCompare[22] == 0.0f) || (fogDepthCompare[22] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_23 " << fogDepthCompare[23] << (((fogDepthCompare[23] == 0.0f) || (fogDepthCompare[23] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_24 " << fogDepthCompare[24] << (((fogDepthCompare[24] == 0.0f) || (fogDepthCompare[24] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_25 " << fogDepthCompare[25] << (((fogDepthCompare[25] == 0.0f) || (fogDepthCompare[25] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_26 " << fogDepthCompare[26] << (((fogDepthCompare[26] == 0.0f) || (fogDepthCompare[26] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_27 " << fogDepthCompare[27] << (((fogDepthCompare[27] == 0.0f) || (fogDepthCompare[27] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_28 " << fogDepthCompare[28] << (((fogDepthCompare[28] == 0.0f) || (fogDepthCompare[28] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_29 " << fogDepthCompare[29] << (((fogDepthCompare[29] == 0.0f) || (fogDepthCompare[29] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_30 " << fogDepthCompare[30] << (((fogDepthCompare[30] == 0.0f) || (fogDepthCompare[30] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "#define FOG_DEPTH_COMPARE_31 " << fogDepthCompare[31] << (((fogDepthCompare[31] == 0.0f) || (fogDepthCompare[31] == 1.0f)) ? ".0" : "") << "\n"; - fragDepthConstants << "\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_0 0.0\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_1 (1.0 / (FOG_DEPTH_COMPARE_1 - FOG_DEPTH_COMPARE_0))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_2 (1.0 / (FOG_DEPTH_COMPARE_2 - FOG_DEPTH_COMPARE_1))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_3 (1.0 / (FOG_DEPTH_COMPARE_3 - FOG_DEPTH_COMPARE_2))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_4 (1.0 / (FOG_DEPTH_COMPARE_4 - FOG_DEPTH_COMPARE_3))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_5 (1.0 / (FOG_DEPTH_COMPARE_5 - FOG_DEPTH_COMPARE_4))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_6 (1.0 / (FOG_DEPTH_COMPARE_6 - FOG_DEPTH_COMPARE_5))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_7 (1.0 / (FOG_DEPTH_COMPARE_7 - FOG_DEPTH_COMPARE_6))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_8 (1.0 / (FOG_DEPTH_COMPARE_8 - FOG_DEPTH_COMPARE_7))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_9 (1.0 / (FOG_DEPTH_COMPARE_9 - FOG_DEPTH_COMPARE_8))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_10 (1.0 / (FOG_DEPTH_COMPARE_10 - FOG_DEPTH_COMPARE_9))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_11 (1.0 / (FOG_DEPTH_COMPARE_11 - FOG_DEPTH_COMPARE_10))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_12 (1.0 / (FOG_DEPTH_COMPARE_12 - FOG_DEPTH_COMPARE_11))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_13 (1.0 / (FOG_DEPTH_COMPARE_13 - FOG_DEPTH_COMPARE_12))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_14 (1.0 / (FOG_DEPTH_COMPARE_14 - FOG_DEPTH_COMPARE_13))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_15 (1.0 / (FOG_DEPTH_COMPARE_15 - FOG_DEPTH_COMPARE_14))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_16 (1.0 / (FOG_DEPTH_COMPARE_16 - FOG_DEPTH_COMPARE_15))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_17 (1.0 / (FOG_DEPTH_COMPARE_17 - FOG_DEPTH_COMPARE_16))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_18 (1.0 / (FOG_DEPTH_COMPARE_18 - FOG_DEPTH_COMPARE_17))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_19 (1.0 / (FOG_DEPTH_COMPARE_19 - FOG_DEPTH_COMPARE_18))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_20 (1.0 / (FOG_DEPTH_COMPARE_20 - FOG_DEPTH_COMPARE_19))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_21 (1.0 / (FOG_DEPTH_COMPARE_21 - FOG_DEPTH_COMPARE_20))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_22 (1.0 / (FOG_DEPTH_COMPARE_22 - FOG_DEPTH_COMPARE_21))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_23 (1.0 / (FOG_DEPTH_COMPARE_23 - FOG_DEPTH_COMPARE_22))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_24 (1.0 / (FOG_DEPTH_COMPARE_24 - FOG_DEPTH_COMPARE_23))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_25 (1.0 / (FOG_DEPTH_COMPARE_25 - FOG_DEPTH_COMPARE_24))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_26 (1.0 / (FOG_DEPTH_COMPARE_26 - FOG_DEPTH_COMPARE_25))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_27 (1.0 / (FOG_DEPTH_COMPARE_27 - FOG_DEPTH_COMPARE_26))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_28 (1.0 / (FOG_DEPTH_COMPARE_28 - FOG_DEPTH_COMPARE_27))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_29 (1.0 / (FOG_DEPTH_COMPARE_29 - FOG_DEPTH_COMPARE_28))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_30 (1.0 / (FOG_DEPTH_COMPARE_30 - FOG_DEPTH_COMPARE_29))\n"; - fragDepthConstants << "#define FOG_DEPTH_INVDIFF_31 (1.0 / (FOG_DEPTH_COMPARE_31 - FOG_DEPTH_COMPARE_30))\n"; + fragDepthConstants << "#define FOG_DEPTH_COMPARE_0 " << fogDepthCompare0 << (((fogDepthCompare0 == 0.0f) || (fogDepthCompare0 == 1.0f)) ? ".0" : "") << "\n"; + fragDepthConstants << "#define FOG_DEPTH_COMPARE_31 " << fogDepthCompare31 << (((fogDepthCompare31 == 0.0f) || (fogDepthCompare31 == 1.0f)) ? ".0" : "") << "\n"; + fragDepthConstants << "#define FOG_OFFSET " << fogOffset << "\n"; + fragDepthConstants << "#define FOG_STEP " << fogStep << "\n"; + fragDepthConstants << "#define FOG_SHIFT_INV " << fogShiftInv << "\n"; + fragDepthConstants << "#define FOG_WEIGHT_TRUNCATE_MASK " << fogWeightTruncateMask << "\n"; fragDepthConstants << "\n"; std::string vtxShaderCode = shaderHeader.str() + std::string(vtxShaderCString); @@ -2415,7 +2112,8 @@ Render3DError OpenGLRenderer_3_2::BeginRender(const GFX3D &engine) for (size_t i = 0; i < 32; i++) { - this->_pendingRenderStates.fogDensity[i] = (engine.renderState.fogDensityTable[i] == 127) ? 1.0f : (GLfloat)engine.renderState.fogDensityTable[i] / 128.0f; + this->_pendingRenderStates.fogDensity[i].r = (engine.renderState.fogDensityTable[i] == 127) ? 1.0f : (GLfloat)engine.renderState.fogDensityTable[i] / 128.0f; // Current table value is stored in r + this->_pendingRenderStates.fogDensity[i].g = (i == 0) ? this->_pendingRenderStates.fogDensity[0].r : this->_pendingRenderStates.fogDensity[i-1].r; // Previous table value is stored in g } } diff --git a/desmume/src/gfx3d.cpp b/desmume/src/gfx3d.cpp index c02622210..bccb09acc 100644 --- a/desmume/src/gfx3d.cpp +++ b/desmume/src/gfx3d.cpp @@ -3093,7 +3093,7 @@ void gfx3d_parseCurrentDISP3DCNT() gfx3d.state.enableEdgeMarking = (DISP3DCNT.EnableEdgeMarking != 0); gfx3d.state.enableFogAlphaOnly = (DISP3DCNT.FogOnlyAlpha != 0); gfx3d.state.enableFog = (DISP3DCNT.EnableFog != 0); - gfx3d.state.fogShift = DISP3DCNT.FogShiftSHR; + gfx3d.state.fogShift = (DISP3DCNT.FogShiftSHR <= 10) ? DISP3DCNT.FogShiftSHR : 10; gfx3d.state.enableClearImage = (DISP3DCNT.RearPlaneMode != 0); } diff --git a/desmume/src/rasterize.cpp b/desmume/src/rasterize.cpp index bde5e9484..69dda083b 100644 --- a/desmume/src/rasterize.cpp +++ b/desmume/src/rasterize.cpp @@ -2114,67 +2114,58 @@ void SoftRasterizerRenderer::_UpdateEdgeMarkColorTable(const u16 *edgeMarkColorT void SoftRasterizerRenderer::_UpdateFogTable(const u8 *fogDensityTable) { -#if 0 - //TODO - this might be a little slow; - //we might need to hash all the variables and only recompute this when something changes - const int increment = (0x400 >> shift); - for (u32 i = 0; i < 32768; i++) - { - if (i < offset) - { - this->_fogTable[i] = densityTable[0]; - continue; - } - - for (int j = 0; j < 32; j++) - { - u32 value = offset + increment*(j+1); - if (i <= value) - { - if (j == 0) - { - this->_fogTable[i] = densityTable[0]; - goto done; - } - else - { - this->_fogTable[i] = ((value-i)*(densityTable[j-1]) + (increment-(value-i))*(densityTable[j]))/increment; - goto done; - } - } - } - this->_fogTable[i] = (densityTable[31]); - done: ; - } -#else - // this should behave exactly the same as the previous loop, - // except much faster. (because it's not a 2d loop and isn't so branchy either) - // maybe it's fast enough to not need to be cached, now. - const int increment = ((1 << 10) >> this->currentRenderState->fogShift); - const int incrementDivShift = 10 - this->currentRenderState->fogShift; - u32 fogOffset = min(max(this->currentRenderState->fogOffset, 0), 32768); - u32 iMin = min(32768, (( 1 + 1) << incrementDivShift) + fogOffset + 1 - increment); - u32 iMax = min(32768, ((32 + 1) << incrementDivShift) + fogOffset + 1 - increment); + const s32 fogStep = 0x400 >> this->currentRenderState->fogShift; + const s32 fogShiftInv = 10 - this->currentRenderState->fogShift; + const s32 fogOffset = min( max((s32)this->currentRenderState->fogOffset, 0), 32768 ); + const s32 iMin = min( max((( 1 + 1) << fogShiftInv) + fogOffset + 1 - fogStep, 0), 32768); + const s32 iMax = min( max(((32 + 1) << fogShiftInv) + fogOffset + 1 - fogStep, 0), 32768); assert(iMin <= iMax); // If the fog factor is 127, then treat it as 128. - u8 fogFactor = (fogDensityTable[0] == 127) ? 128 : fogDensityTable[0]; - memset(this->_fogTable, fogFactor, iMin); + u8 fogWeight = (fogDensityTable[0] >= 127) ? 128 : fogDensityTable[0]; + memset(this->_fogTable, fogWeight, iMin); - for(u32 i = iMin; i < iMax; i++) + for (s32 depth = iMin; depth < iMax; depth++) { - int num = (i - fogOffset + (increment-1)); - int j = (num >> incrementDivShift) - 1; - u32 value = (num & ~(increment-1)) + fogOffset; - u32 diff = value - i; - assert(j >= 1 && j < 32); - fogFactor = ((diff*(fogDensityTable[j-1]) + (increment-diff)*(fogDensityTable[j])) >> incrementDivShift); - this->_fogTable[i] = (fogFactor == 127) ? 128 : fogFactor; +#if 0 + // TODO: this might be a little slow; + // We might need to hash all the variables and only recompute this when something changes. + // But let's keep a naive version of this loop for the sake of clarity. + if (depth < fogOffset) + { + this->_fogTable[depth] = fogDensityTable[0]; + } + else + { + this->_fogTable[depth] = fogDensityTable[31]; + + for (size_t idx = 0; idx < 32; idx++) + { + s32 value = fogOffset + (fogStep * (idx+1)); + if (depth <= value) + { + this->_fogTable[depth] = (idx == 0) ? fogDensityTable[0] : ((value-depth)*(fogDensityTable[idx-1]) + (fogStep-(value-depth))*(fogDensityTable[idx])) / fogStep; + break; + } + } + } +#else + // this should behave exactly the same as the previous loop, + // except much faster. (because it's not a 2d loop and isn't so branchy either) + // maybe it's fast enough to not need to be cached, now. + const s32 diff = depth - fogOffset + (fogStep - 1); + const s32 interp = (diff & ~(fogStep - 1)) + fogOffset - depth; + + const size_t idx = (diff >> fogShiftInv) - 1; + assert( (idx >= 1) && (idx < 32) ); + + fogWeight = (u8)( ( (interp * fogDensityTable[idx-1]) + ((fogStep - interp) * fogDensityTable[idx]) ) >> fogShiftInv ); + this->_fogTable[depth] = (fogWeight >= 127) ? 128 : fogWeight; +#endif } - fogFactor = (fogDensityTable[31] == 127) ? 128 : fogDensityTable[31]; - memset(this->_fogTable+iMax, fogFactor, 32768-iMax); -#endif + fogWeight = (fogDensityTable[31] >= 127) ? 128 : fogDensityTable[31]; + memset(this->_fogTable+iMax, fogWeight, 32768-iMax); } Render3DError SoftRasterizerRenderer::RenderEdgeMarkingAndFog(const SoftRasterizerPostProcessParams ¶m) @@ -2248,16 +2239,16 @@ Render3DError SoftRasterizerRenderer::RenderEdgeMarkingAndFog(const SoftRasteriz const size_t fogIndex = depth >> 9; assert(fogIndex < 32768); - const u8 fog = (this->_framebufferAttributes->isFogged[i] != 0) ? this->_fogTable[fogIndex] : 0; + const u8 fogWeight = (this->_framebufferAttributes->isFogged[i] != 0) ? this->_fogTable[fogIndex] : 0; if (!param.fogAlphaOnly) { - dstColor.r = ( (128-fog)*dstColor.r + fogColor.r*fog ) >> 7; - dstColor.g = ( (128-fog)*dstColor.g + fogColor.g*fog ) >> 7; - dstColor.b = ( (128-fog)*dstColor.b + fogColor.b*fog ) >> 7; + dstColor.r = ( (128-fogWeight)*dstColor.r + fogColor.r*fogWeight ) >> 7; + dstColor.g = ( (128-fogWeight)*dstColor.g + fogColor.g*fogWeight ) >> 7; + dstColor.b = ( (128-fogWeight)*dstColor.b + fogColor.b*fogWeight ) >> 7; } - dstColor.a = ( (128-fog)*dstColor.a + fogColor.a*fog ) >> 7; + dstColor.a = ( (128-fogWeight)*dstColor.a + fogColor.a*fogWeight ) >> 7; } } }