Ignore:
Timestamp:
Nov 26, 2011, 7:36:54 PM (12 years ago)
Author:
sam
Message:

tutorial: smarter register and instruction usage in the Mandelbrot zooming
shader.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/tutorial/tut03.cpp

    r1089 r1090  
    395395                "attribute vec2 a_Vertex;"
    396396                ""
    397                 "varying vec2 A0, A1, A2, A3;"
    398                 "varying vec2 B0, B1, B2, B3;"
     397                "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
    399398                ""
    400399                "void main(void)"
     
    407406                      * size of a texel: the distance from this new point to
    408407                      * the final point will be our error. */
    409                 "    A0 = a_TexCoord * u_ZoomSettings[0][2]"
    410                 "       + vec2(u_ZoomSettings[0][0], -u_ZoomSettings[0][1])"
    411                 "       + 0.5 * u_TexelSize.xy;"
    412                 "    A1 = a_TexCoord * u_ZoomSettings[1][2]"
    413                 "       + vec2(u_ZoomSettings[1][0], -u_ZoomSettings[1][1])"
    414                 "       + -0.5 * u_TexelSize.xy;"
    415                 "    A2 = a_TexCoord * u_ZoomSettings[2][2]"
    416                 "       + vec2(u_ZoomSettings[2][0], -u_ZoomSettings[2][1])"
    417                 "       + vec2(0.5, -0.5) * u_TexelSize.xy;"
    418                 "    A3 = a_TexCoord * u_ZoomSettings[3][2]"
    419                 "       + vec2(u_ZoomSettings[3][0], -u_ZoomSettings[3][1])"
    420                 "       + vec2(-0.5, 0.5) * u_TexelSize.xy;"
     408                "    vec4 offsets = vec4(0.5, -0.5, 0.015625, -0.015625);"
     409                "    vec4 zoomscale = vec4(u_ZoomSettings[0][2],"
     410                "                          u_ZoomSettings[1][2],"
     411                "                          u_ZoomSettings[2][2],"
     412                "                          u_ZoomSettings[3][2]);"
     413                "    vec4 zoomtx = vec4(u_ZoomSettings[0][0],"
     414                "                       u_ZoomSettings[1][0],"
     415                "                       u_ZoomSettings[2][0],"
     416                "                       u_ZoomSettings[3][0]);"
     417                "    vec4 zoomty = vec4(u_ZoomSettings[0][1],"
     418                "                       u_ZoomSettings[1][1],"
     419                "                       u_ZoomSettings[2][1],"
     420                "                       u_ZoomSettings[3][1]);"
     421                     /* Pass all this to the fragment shader */
     422                "    v_CenterX = zoomscale * a_TexCoord.x + zoomtx"
     423                "              + offsets.xyxy * u_TexelSize.x;"
     424                "    v_CenterY = zoomscale * a_TexCoord.y - zoomty"
     425                "              + offsets.xyyx * u_TexelSize.y;"
    421426                     /* Precompute the multiple of one texel where our ideal
    422427                      * point lies. The fragment shader will call floor() on
    423428                      * this value. We add or remove a slight offset to avoid
    424429                      * rounding issues at the image's edges. */
    425                 "    B0 = (A0 * u_TexelSize.zw + vec2(-0.015625, -0.015625));"
    426                 "    B1 = (A1 * u_TexelSize.zw + vec2( 0.015625,  0.015625));"
    427                 "    B2 = (A2 * u_TexelSize.zw + vec2(-0.015625,  0.015625));"
    428                 "    B3 = (A3 * u_TexelSize.zw + vec2( 0.015625, -0.015625));"
     430                "    v_IndexX = v_CenterX * u_TexelSize.z - offsets.zwzw;"
     431                "    v_IndexY = v_CenterY * u_TexelSize.w - offsets.zwwz;"
    429432                "}",
    430433
     
    438441                "uniform sampler2D in_Texture;"
    439442                ""
    440                 "varying vec2 A0, A1, A2, A3;"
    441                 "varying vec2 B0, B1, B2, B3;"
    442                 ""
    443                 "vec2 v05 = vec2(0.5, 0.5);"
    444                 ""
    445                 "float mylen(vec2 p)"
    446                 "{"
    447                 "    p *= u_TexelSize.zw;" /* Correct for aspect ratio */
    448                 //"    return 0.001 + abs(p.x) + abs(p.y);"
    449                 //"    return 0.1 + length(p);"
    450                 "    vec2 q = p * p;"
    451                 "    return 0.01 + q.x + q.y;"
    452                 "}"
    453                 ""
    454                 /* Get the coordinate of the nearest point in slice 0 in xy,
    455                  * and the squared distance to that point in z.
    456                  * return value has the 0.25 Y scaling. */
    457                 "vec3 nearest0()"
    458                 "{"
    459                 "    vec2 r = u_TexelSize.xy * (1.0 + 2.0 * floor(B0));"
    460                 "    vec2 t = step(abs(r - v05), v05);"
    461                 "    float l = t.x * t.y / mylen(r - A0);"
    462                 "    return vec3(r * vec2(1.0, 0.25), l);"
    463                 "}"
    464                 ""
    465                 "vec3 nearest1()"
    466                 "{"
    467                 "    vec2 r = u_TexelSize.xy * (1.0 + 2.0 * floor(B1));"
    468                 "    vec2 t = step(abs(r - v05), v05);"
    469                 "    float l = t.x * t.y / mylen(r - A1);"
    470                 "    return vec3(r * vec2(1.0, 0.25) + vec2(0.0, 0.25), l);"
    471                 "}"
    472                 ""
    473                 "vec3 nearest2()"
    474                 "{"
    475                 "    vec2 r = u_TexelSize.xy * (1.0 + 2.0 * floor(B2));"
    476                 "    vec2 t = step(abs(r - v05), v05);"
    477                 "    float l = t.x * t.y / mylen(r - A2);"
    478                 "    return vec3(r * vec2(1.0, 0.25) + vec2(0.0, 0.50), l);"
    479                 "}"
    480                 ""
    481                 "vec3 nearest3()"
    482                 "{"
    483                 "    vec2 r = u_TexelSize.xy * (1.0 + 2.0 * floor(B3));"
    484                 "    vec2 t = step(abs(r - v05), v05);"
    485                 "    float l = t.x * t.y / mylen(r - A3);"
    486                 "    return vec3(r * vec2(1.0, 0.25) + vec2(0.0, 0.75), l);"
    487                 "}"
     443                "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
    488444                ""
    489445                "void main(void)"
    490446                "{"
    491                      /* Get a pixel coordinate from each slice */
    492                 "    vec3 k0 = nearest0();"
    493                 "    vec3 k1 = nearest1();"
    494                 "    vec3 k2 = nearest2();"
    495                 "    vec3 k3 = nearest3();"
    496                      /* Get the nearest pixel */
    497                 "    float t01 = step(k0.z, k1.z);"
    498                 "    k0 = mix(k0, k1, t01);"
    499                 "    float t23 = step(k2.z, k3.z);"
    500                 "    k2 = mix(k2, k3, t23);"
    501                 "    float t02 = step(k0.z, k2.z);"
    502                 "    k0 = mix(k0, k2, t02);"
    503                 "    gl_FragColor = texture2D(in_Texture, k0.xy);"
     447                     /* Get a pixel coordinate from each slice into rx & ry */
     448                "    vec4 rx = u_TexelSize.x * (1.0 + 2.0 * floor(v_IndexX));"
     449                "    vec4 ry = u_TexelSize.y * (1.0 + 2.0 * floor(v_IndexY));"
     450                     /* Compute distance to expected pixel in dd */
     451                "    vec4 v05 = vec4(0.5, 0.5, 0.5, 0.5);"
     452                "    vec4 t0 = step(abs(rx - v05), v05)"
     453                "            * step(abs(ry - v05), v05);"
     454                "    vec4 dx = rx - v_CenterX;"
     455                "    vec4 dy = ry - v_CenterY;"
     456                //"    vec4 dd = t0 * (abs(dx) + abs(dy));"
     457                //"    vec4 dd = t0 / (0.001 + sqrt((dx * dx) + (dy * dy)));"
     458                "    vec4 dd = t0 / (0.000001 + (dx * dx) + (dy * dy));"
     459                     /* Modify Y coordinate to select proper quarter. */
     460                "    ry = ry * 0.25 + vec4(0.0, 0.25, 0.5, 0.75);"
     461                ""
     462#if 1
     463                     /* Put min(.x,.y) in .x and min(.z,.w) in .z */
     464                "    vec4 t1 = step(dd, dd.yyww);"
     465                "    rx = mix(rx, rx.yyww, t1);"
     466                "    ry = mix(ry, ry.yyww, t1);"
     467                "    dd = mix(dd, dd.yyww, t1);"
     468                     /* Put min(x,z) in x */
     469                "    vec4 t2 = step(dd, dd.zzzz);"
     470                "    rx = mix(rx, rx.zzzz, t2);"
     471                "    ry = mix(ry, ry.zzzz, t2);"
     472                     /* Nearest neighbour */
     473                "    gl_FragColor = texture2D(in_Texture, vec2(rx.x, ry.x));"
     474#else
    504475                     /* Alternate version: some kind of linear interpolation */
    505 //                "    vec4 p0 = texture2D(in_Texture, k0.xy);"
    506 //                "    vec4 p1 = texture2D(in_Texture, k1.xy);"
    507 //                "    vec4 p2 = texture2D(in_Texture, k2.xy);"
    508 //                "    vec4 p3 = texture2D(in_Texture, k3.xy);"
    509 //                "    gl_FragColor = 1.0 / (k0.z + k1.z + k2.z + k3.z)"
    510 //                "          * (k0.z * p0 + k1.z * p1 + k2.z * p2 + k3.z * p3);"
     476                "    vec4 p0 = texture2D(in_Texture, vec2(rx.x, ry.x));"
     477                "    vec4 p1 = texture2D(in_Texture, vec2(rx.y, ry.y));"
     478                "    vec4 p2 = texture2D(in_Texture, vec2(rx.z, ry.z));"
     479                "    vec4 p3 = texture2D(in_Texture, vec2(rx.w, ry.w));"
     480                "    gl_FragColor = 1.0 / (dd.x + dd.y + dd.z + dd.w)"
     481                "          * (dd.x * p0 + dd.y * p1 + dd.z * p2 + dd.w * p3);"
     482#endif
    511483                "}"
    512484#else
Note: See TracChangeset for help on using the changeset viewer.