From 62907038472cb5dc430b741c9937ec604cb4a39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Thu, 20 Mar 2025 14:26:37 -0400 Subject: [PATCH] Avoid out-of-bounds array access in tilemap shader This breaks the shader on my Android device. When `atIndex` is out of bounds, `atFrames[atIndex]` evaluates to 0 and so `aniIndex / atFrames[atIndex]` results in division by 0. All of the tiles where this division by 0 occurs are blank on my Android device. That probably means the shader pipeline halts when division by 0 occurs. --- shader/tilemap.vert | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/shader/tilemap.vert b/shader/tilemap.vert index 1a88a5e2..df3ce50c 100644 --- a/shader/tilemap.vert +++ b/shader/tilemap.vert @@ -27,12 +27,14 @@ void main() vec2 tex = texCoord; lowp int atIndex = int(tex.y / autotileH); - lowp int pred = int(tex.x <= atAreaW && tex.y <= atAreaH); - lowp int frame = int(aniIndex - atFrames[atIndex] * (aniIndex / atFrames[atIndex])); - lowp int row = frame / 8; - lowp int col = frame - 8 * row; - tex.x += atAniOffsetX * float(col * pred); - tex.y += atAniOffsetY * float(row * pred); + if (atIndex < nAutotiles) { + lowp int pred = int(tex.x <= atAreaW && tex.y <= atAreaH); + lowp int frame = int(aniIndex - atFrames[atIndex] * (aniIndex / atFrames[atIndex])); + lowp int row = frame / 8; + lowp int col = frame - 8 * row; + tex.x += atAniOffsetX * float(col * pred); + tex.y += atAniOffsetY * float(row * pred); + } gl_Position = projMat * vec4(position + translation, 0, 1);