mkxp-z/shader/tilemap.vert
刘皓 6290703847
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.
2025-03-20 14:26:37 -04:00

42 lines
1.1 KiB
GLSL

uniform mat4 projMat;
uniform vec2 texSizeInv;
uniform vec2 translation;
uniform highp int aniIndex;
attribute vec2 position;
attribute vec2 texCoord;
varying vec2 v_texCoord;
const int nAutotiles = 7;
const float tileW = 32.0;
const float tileH = 32.0;
const float autotileW = 3.0*tileW;
const float autotileH = 4.0*tileW;
const float atAreaW = autotileW;
const float atAreaH = autotileH*float(nAutotiles);
const float atAniOffsetX = 3.0*tileW;
const float atAniOffsetY = tileH;
uniform lowp int atFrames[nAutotiles];
void main()
{
vec2 tex = texCoord;
lowp int atIndex = int(tex.y / autotileH);
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);
v_texCoord = tex * texSizeInv;
}