From cde2102671563d7535702bd697b3a03ba4e10c67 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Tue, 20 Jun 2023 12:56:22 +1200 Subject: [PATCH] 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. --- lib/Graphics/Rendering/Rect/Backgrounds.hs | 42 +++++++++++----------- lib/Graphics/Rendering/Rect/Types.hs | 8 ++--- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/Graphics/Rendering/Rect/Backgrounds.hs b/lib/Graphics/Rendering/Rect/Backgrounds.hs index 41e4059..dd97a0f 100644 --- a/lib/Graphics/Rendering/Rect/Backgrounds.hs +++ b/lib/Graphics/Rendering/Rect/Backgrounds.hs @@ -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);", diff --git a/lib/Graphics/Rendering/Rect/Types.hs b/lib/Graphics/Rendering/Rect/Types.hs index 0c2f322..8c9810c 100644 --- a/lib/Graphics/Rendering/Rect/Types.hs +++ b/lib/Graphics/Rendering/Rect/Types.hs @@ -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 -- 2.30.2