]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Add GLSL implementation of texture lookup tables (LUT), ported from Wrath
authorMario <mario@smbclan.net>
Tue, 9 Jun 2020 17:48:05 +0000 (03:48 +1000)
committerMario <mario@smbclan.net>
Tue, 9 Jun 2020 17:48:05 +0000 (03:48 +1000)
shader_glsl.h

index fff9af5fefc2a5d97134b08c14d2af8c81b83c45..ed74055a1daf19a9c2f97c800d45f47408c3a89b 100644 (file)
 "// uniform mediump vec4 UserVec4;\n",
 "uniform mediump float ColorFringe;\n",
 "// uniform highp float ClientTime;\n",
-"// uniform sampler2D Texture_LUT;\n",
+"uniform sampler2D Texture_LUT;\n",
 "uniform mediump vec2 PixelSize;\n",
 "\n",
 "#ifdef USEFXAA\n",
 "}\n",
 "#endif\n",
 "\n",
+"#ifdef USEPOSTPROCESSING\n",
+"// https://github.com/mattdesl/glsl-lut\n",
+"vec4 sampleLUT(sampler2D lookupTable, vec3 textureColor) {\n",
+"      mediump float blueColor = textureColor.b * 63.0;\n",
+"\n",
+"      mediump vec2 quad1;\n",
+"      quad1.y = floor(floor(blueColor) / 8.0);\n",
+"      quad1.x = floor(blueColor) - (quad1.y * 8.0);\n",
+"\n",
+"      mediump vec2 quad2;\n",
+"      quad2.y = floor(ceil(blueColor) / 8.0);\n",
+"      quad2.x = ceil(blueColor) - (quad2.y * 8.0);\n",
+"\n",
+"      highp vec2 texPos1;\n",
+"      texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);\n",
+"      texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);\n",
+"\n",
+"      highp vec2 texPos2;\n",
+"      texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);\n",
+"      texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);\n",
+"\n",
+"      lowp vec4 newColor1 = texture2D(lookupTable, texPos1);\n",
+"      lowp vec4 newColor2 = texture2D(lookupTable, texPos2);\n",
+"\n",
+"      lowp vec4 newColor = mix(newColor1, newColor2, fract(blueColor));\n",
+"      return newColor;\n",
+"}\n",
+"#endif\n",
+"\n",
 "void main(void)\n",
 "{\n",
 "      float fringe = ColorFringe;//.0033f;\n",
 "#endif\n",
 "\n",
 "#ifdef USEPOSTPROCESSING\n",
+"      // color grading using LUTs\n",
+"      vec4 oldColor = dp_texture2D(Texture_First, TexCoord1);\n",
+"      dp_FragColor.rgb = sampleLUT(Texture_LUT, oldColor.rgb).rgb;\n",
+"      dp_FragColor.a = oldColor.a;\n",
+"#endif\n",
+"\n",
+"#ifdef USEPOSTPROCESSING\n",
 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n",
 "// this code does a blur with the radius specified in the first component of r_glsl_postprocess_uservec1 and blends it using the second component\n",
 "#if defined(USERVEC1) || defined(USERVEC2)\n",