From 0a669f9c4933d32850c08a87e406bdaaf05d76c1 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 25 Nov 2011 18:24:19 +0000 Subject: [PATCH] tutorial: make the GLSL Mandelbrot shader completely branchless. --- test/tutorial/tut03.cpp | 44 +++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/test/tutorial/tut03.cpp b/test/tutorial/tut03.cpp index b786c26f..9ea174cc 100644 --- a/test/tutorial/tut03.cpp +++ b/test/tutorial/tut03.cpp @@ -391,6 +391,8 @@ public: "uniform mat4 in_ZoomSettings;" "uniform sampler2D in_Texture;" "" + "vec2 v05 = vec2(0.5, 0.5);" + "" "float mylen(vec2 p)" "{" " p *= in_TexelSize.zw;" /* Correct for aspect ratio */ @@ -424,8 +426,8 @@ public: * so that we know the error between the pixel we get * and the pixel we wanted. */ " vec2 r = q - 0.5 * in_TexelSize.xy;" - " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)" - " ? 1.0 / mylen(r - p) : 0.0;" + " vec2 t = step(abs(q - v05), v05);" + " float l = t.x * t.y / mylen(r - p);" /* Step 4: return final texel coordinates and * corresponding error value. */ " return vec3(q * vec2(1.0, 0.25), l);" @@ -441,8 +443,9 @@ public: " q = in_TexelSize.xy" " * (1.0 + 2.0 * floor(q * 0.5 * in_TexelSize.zw));" " vec2 r = q - -0.5 * in_TexelSize.xy;" - " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)" - " ? 1.0 / mylen(r - p) : 0.0;" + "" + " vec2 t = step(abs(q - v05), v05);" + " float l = t.x * t.y / mylen(r - p);" " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.25), l);" "}" "" @@ -456,8 +459,9 @@ public: " q = in_TexelSize.xy" " * (1.0 + 2.0 * floor(q * 0.5 * in_TexelSize.zw));" " vec2 r = q - vec2(0.5, -0.5) * in_TexelSize.xy;" - " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)" - " ? 1.0 / mylen(r - p) : 0.0;" + "" + " vec2 t = step(abs(q - v05), v05);" + " float l = t.x * t.y / mylen(r - p);" " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.50), l);" "}" "" @@ -471,8 +475,9 @@ public: " q = in_TexelSize.xy" " * (1.0 + 2.0 * floor(q * 0.5 * in_TexelSize.zw));" " vec2 r = q - vec2(-0.5, 0.5) * in_TexelSize.xy;" - " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)" - " ? 1.0 / mylen(r - p) : 0.0;" + "" + " vec2 t = step(abs(q - v05), v05);" + " float l = t.x * t.y / mylen(r - p);" " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.75), l);" "}" "" @@ -483,19 +488,24 @@ public: #else " vec2 coord = gl_TexCoord[0].xy;" #endif - /* Get a pixel from each slice */ + /* Get a pixel coordinate from each slice */ " vec3 k0 = nearest0(coord);" " vec3 k1 = nearest1(coord);" " vec3 k2 = nearest2(coord);" " vec3 k3 = nearest3(coord);" - " vec4 p0 = texture2D(in_Texture, k0.xy);" - " vec4 p1 = texture2D(in_Texture, k1.xy);" - " vec4 p2 = texture2D(in_Texture, k2.xy);" - " vec4 p3 = texture2D(in_Texture, k3.xy);" -"if (k0.z >= k1.z && k0.z >= k2.z && k0.z >= k3.z) gl_FragColor = p0;" -"if (k1.z >= k0.z && k1.z >= k2.z && k1.z >= k3.z) gl_FragColor = p1;" -"if (k2.z >= k0.z && k2.z >= k1.z && k2.z >= k3.z) gl_FragColor = p2;" -"if (k3.z >= k0.z && k3.z >= k1.z && k3.z >= k2.z) gl_FragColor = p3;" + /* Get the nearest pixel */ + " float t01 = step(k0.z, k1.z);" + " k0 = mix(k0, k1, t01);" + " float t23 = step(k2.z, k3.z);" + " k2 = mix(k2, k3, t23);" + " float t02 = step(k0.z, k2.z);" + " k0 = mix(k0, k2, t02);" + " gl_FragColor = texture2D(in_Texture, k0.xy);" + /* Alternate version: some kind of linear interpolation */ +// " vec4 p0 = texture2D(in_Texture, k0.xy);" +// " vec4 p1 = texture2D(in_Texture, k1.xy);" +// " vec4 p2 = texture2D(in_Texture, k2.xy);" +// " vec4 p3 = texture2D(in_Texture, k3.xy);" // " gl_FragColor = 1.0 / (k0.z + k1.z + k2.z + k3.z)" // " * (k0.z * p0 + k1.z * p1 + k2.z * p2 + k3.z * p3);" "}"