Skip to content

Commit ac6cc77

Browse files
committed
Add GLSL implementation of texture lookup tables (LUT), ported from Wrath
1 parent 4ff05ac commit ac6cc77

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

shader_glsl.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
"// uniform mediump vec4 UserVec4;\n",
250250
"uniform mediump float ColorFringe;\n",
251251
"// uniform highp float ClientTime;\n",
252-
"// uniform sampler2D Texture_LUT;\n",
252+
"uniform sampler2D Texture_LUT;\n",
253253
"uniform mediump vec2 PixelSize;\n",
254254
"\n",
255255
"#ifdef USEFXAA\n",
@@ -299,6 +299,35 @@
299299
"}\n",
300300
"#endif\n",
301301
"\n",
302+
"#ifdef USEPOSTPROCESSING\n",
303+
"// https://github.com/mattdesl/glsl-lut\n",
304+
"vec4 sampleLUT(sampler2D lookupTable, vec3 textureColor) {\n",
305+
" mediump float blueColor = textureColor.b * 63.0;\n",
306+
"\n",
307+
" mediump vec2 quad1;\n",
308+
" quad1.y = floor(floor(blueColor) / 8.0);\n",
309+
" quad1.x = floor(blueColor) - (quad1.y * 8.0);\n",
310+
"\n",
311+
" mediump vec2 quad2;\n",
312+
" quad2.y = floor(ceil(blueColor) / 8.0);\n",
313+
" quad2.x = ceil(blueColor) - (quad2.y * 8.0);\n",
314+
"\n",
315+
" highp vec2 texPos1;\n",
316+
" texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);\n",
317+
" texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);\n",
318+
"\n",
319+
" highp vec2 texPos2;\n",
320+
" texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);\n",
321+
" texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);\n",
322+
"\n",
323+
" lowp vec4 newColor1 = texture2D(lookupTable, texPos1);\n",
324+
" lowp vec4 newColor2 = texture2D(lookupTable, texPos2);\n",
325+
"\n",
326+
" lowp vec4 newColor = mix(newColor1, newColor2, fract(blueColor));\n",
327+
" return newColor;\n",
328+
"}\n",
329+
"#endif\n",
330+
"\n",
302331
"void main(void)\n",
303332
"{\n",
304333
" float fringe = ColorFringe;//.0033f;\n",
@@ -312,6 +341,13 @@
312341
"#endif\n",
313342
"\n",
314343
"#ifdef USEPOSTPROCESSING\n",
344+
" // color grading using LUTs\n",
345+
" vec4 oldColor = dp_texture2D(Texture_First, TexCoord1);\n",
346+
" dp_FragColor.rgb = sampleLUT(Texture_LUT, oldColor.rgb).rgb;\n",
347+
" dp_FragColor.a = oldColor.a;\n",
348+
"#endif\n",
349+
"\n",
350+
"#ifdef USEPOSTPROCESSING\n",
315351
"// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n",
316352
"// 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",
317353
"#if defined(USERVEC1) || defined(USERVEC2)\n",

0 commit comments

Comments
 (0)