~alcinnz/Mondrian

cde2102671563d7535702bd697b3a03ba4e10c67 — Adrian Cochrane 10 months ago b49146e
Minor legibility & theoretical-performance optimizations.

Use forward-unrolled loop to lookup the transition to apply, avoiding else-if branches.
Use GL_TRIANGLE_STRIP to avoid the need to upload duplicate vertex data.
2 files changed, 24 insertions(+), 26 deletions(-)

M lib/Graphics/Rendering/Rect/Backgrounds.hs
M lib/Graphics/Rendering/Rect/Types.hs
M lib/Graphics/Rendering/Rect/Backgrounds.hs => lib/Graphics/Rendering/Rect/Backgrounds.hs +21 -21
@@ 48,14 48,14 @@ linearFragmentShader = B8.pack $ unlines [
    "",
    "   int i = 0;",
    -- Workaround for buggy GPU drivers on test machine.
    "   if (1 < nStops - 1 && a > stopPoints[1]) i = 1;",
    "   if (2 < nStops - 1 && a > stopPoints[2]) i = 2;",
    "   if (3 < nStops - 1 && a > stopPoints[3]) i = 3;",
    "   if (4 < nStops - 1 && a > stopPoints[4]) i = 4;",
    "   if (5 < nStops - 1 && a > stopPoints[5]) i = 5;",
    "   if (6 < nStops - 1 && a > stopPoints[6]) i = 6;",
    "   if (7 < nStops - 1 && a > stopPoints[7]) i = 7;",
    "   if (8 < nStops - 1 && a > stopPoints[8]) i = 8;",
    "   else if (7 < nStops - 1 && a > stopPoints[7]) i = 7;",
    "   else if (6 < nStops - 1 && a > stopPoints[6]) i = 6;",
    "   else if (5 < nStops - 1 && a > stopPoints[5]) i = 5;",
    "   else if (4 < nStops - 1 && a > stopPoints[4]) i = 4;",
    "   else if (3 < nStops - 1 && a > stopPoints[3]) i = 3;",
    "   else if (2 < nStops - 1 && a > stopPoints[2]) i = 2;",
    "   else if (1 < nStops - 1 && a > stopPoints[1]) i = 1;",
    "",
    "   a = smoothstep(stopPoints[i], stopPoints[i+1], a);",
    "   fcolour = mix(stops[i], stops[i+1], a);",


@@ 78,14 78,14 @@ radialFragmentShader = B8.pack $ unlines [
    "",
    "   int i = 0;",
    -- Workaround for buggy GPU drivers on test machine.
    "   if (1 < nStops - 1 && a > stopPoints[1]) i = 1;",
    "   if (2 < nStops - 1 && a > stopPoints[2]) i = 2;",
    "   if (3 < nStops - 1 && a > stopPoints[3]) i = 3;",
    "   if (4 < nStops - 1 && a > stopPoints[4]) i = 4;",
    "   if (5 < nStops - 1 && a > stopPoints[5]) i = 5;",
    "   if (6 < nStops - 1 && a > stopPoints[6]) i = 6;",
    "   if (7 < nStops - 1 && a > stopPoints[7]) i = 7;",
    "   if (8 < nStops - 1 && a > stopPoints[8]) i = 8;",
    "   else if (7 < nStops - 1 && a > stopPoints[7]) i = 7;",
    "   else if (6 < nStops - 1 && a > stopPoints[6]) i = 6;",
    "   else if (5 < nStops - 1 && a > stopPoints[5]) i = 5;",
    "   else if (4 < nStops - 1 && a > stopPoints[4]) i = 4;",
    "   else if (3 < nStops - 1 && a > stopPoints[3]) i = 3;",
    "   else if (2 < nStops - 1 && a > stopPoints[2]) i = 2;",
    "   else if (1 < nStops - 1 && a > stopPoints[1]) i = 1;",
    "",
    "   a = smoothstep(stopPoints[i], stopPoints[i+1], a);",
    "   fcolour = mix(stops[i], stops[i+1], a);",


@@ 107,14 107,14 @@ circleFragmentShader = B8.pack $ unlines [
    "",
    "   int i = 0;",
    -- Workaround for buggy GPU drivers on test machine.
    "   if (1 < nStops - 1 && a > stopPoints[1]) i = 1;",
    "   if (2 < nStops - 1 && a > stopPoints[2]) i = 2;",
    "   if (3 < nStops - 1 && a > stopPoints[3]) i = 3;",
    "   if (4 < nStops - 1 && a > stopPoints[4]) i = 4;",
    "   if (5 < nStops - 1 && a > stopPoints[5]) i = 5;",
    "   if (6 < nStops - 1 && a > stopPoints[6]) i = 6;",
    "   if (7 < nStops - 1 && a > stopPoints[7]) i = 7;",
    "   if (8 < nStops - 1 && a > stopPoints[8]) i = 8;",
    "   else if (7 < nStops - 1 && a > stopPoints[7]) i = 7;",
    "   else if (6 < nStops - 1 && a > stopPoints[6]) i = 6;",
    "   else if (5 < nStops - 1 && a > stopPoints[5]) i = 5;",
    "   else if (4 < nStops - 1 && a > stopPoints[4]) i = 4;",
    "   else if (3 < nStops - 1 && a > stopPoints[3]) i = 3;",
    "   else if (2 < nStops - 1 && a > stopPoints[2]) i = 2;",
    "   else if (1 < nStops - 1 && a > stopPoints[1]) i = 1;",
    "",
    "   a = smoothstep(stopPoints[i], stopPoints[i+1], a);",
    "   fcolour = mix(stops[i], stops[i+1], a);",

M lib/Graphics/Rendering/Rect/Types.hs => lib/Graphics/Rendering/Rect/Types.hs +3 -5
@@ 29,10 29,8 @@ data Rect = Rect {
    right :: Float, bottom :: Float
} deriving (Read, Show, Eq, Ord)
rect2geom :: Rect -> UV.Vector (V2 Float)
rect2geom Rect{..} = UV.fromList [tl, tr, br, tl, br, bl]
  where
    (tl, tr) = (V2 left top, V2 right top)
    (bl, br) = (V2 left bottom, V2 right bottom)
rect2geom Rect{..} = UV.fromList
    [V2 left top, V2 right top, V2 left bottom, V2 right bottom]
size :: Rect -> (Float, Float)
size Rect {..} = (right - left, bottom - top)



@@ 123,7 121,7 @@ renderRectWith fragmentShader uniformNames = do

        withBoundTextures (map unTexture textures) $ do
            glBindVertexArray vao
            drawVAO prog vao GL_TRIANGLES 6 -- 2 triangles
            drawVAO prog vao GL_TRIANGLE_STRIP 4
            glBindVertexArray 0

        liftIO $ withArray [pbuf] $ glDeleteBuffers 1