From 2f95c0613a7d8962d19b862695e13007295265ca Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Tue, 23 Dec 2014 19:00:14 +0100 Subject: [PATCH] Shaders: Prefer arithmetic conditionals to branching --- shader/tilemap.vert | 5 +++-- shader/tilemapvx.vert | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/shader/tilemap.vert b/shader/tilemap.vert index 52847c4..05c5f72 100644 --- a/shader/tilemap.vert +++ b/shader/tilemap.vert @@ -18,8 +18,9 @@ const float atAniOffset = 32.0*3.0; void main() { vec2 tex = texCoord; - if (tex.x <= atAreaW && tex.y <= atAreaH) - tex.x += aniIndex * atAniOffset; + + lowp float pred = float(tex.x <= atAreaW && tex.y <= atAreaH); + tex.x += aniIndex * atAniOffset * pred; gl_Position = projMat * vec4(position + translation, 0, 1); diff --git a/shader/tilemapvx.vert b/shader/tilemapvx.vert index 5ad87c2..b744d21 100644 --- a/shader/tilemapvx.vert +++ b/shader/tilemapvx.vert @@ -18,14 +18,15 @@ const float atAreaCW = 4.0*32.0; void main() { vec2 tex = texCoord; + lowp float pred; /* Type A autotiles shift horizontally */ - if (tex.x <= atAreaA.x && tex.y <= atAreaA.y) - tex.x += aniOffset.x; + pred = float(tex.x <= atAreaA.x && tex.y <= atAreaA.y); + tex.x += aniOffset.x * pred; /* Type C autotiles shift vertically */ - if (tex.x >= atAreaCX && tex.x <= (atAreaCX+atAreaCW) && tex.y <= atAreaA.y) - tex.y += aniOffset.y; + pred = float(tex.x >= atAreaCX && tex.x <= (atAreaCX+atAreaCW) && tex.y <= atAreaA.y); + tex.y += aniOffset.y * pred; gl_Position = projMat * vec4(position + translation, 0, 1);