forked from melonDS-emu/melonDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2DSpriteFS.glsl
More file actions
283 lines (235 loc) · 6.99 KB
/
Copy path2DSpriteFS.glsl
File metadata and controls
283 lines (235 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#version 140
uniform sampler2D SpriteTex;
uniform sampler2DArray Capture128Tex;
uniform sampler2DArray Capture256Tex;
uniform int uSpriteFilterMode;
struct sOAM
{
ivec2 Position;
bvec2 Flip;
ivec2 Size;
ivec2 BoundSize;
int OBJMode;
int Type;
int PalOffset;
int TileOffset;
int TileStride;
int Rotscale;
int BGPrio;
bool Mosaic;
};
layout(std140) uniform ubSpriteConfig
{
int uVRAMMask;
ivec4 uRotscale[32];
sOAM uOAM[128];
};
layout(std140) uniform ubSpriteScanlineConfig
{
ivec4 uMosaicLine[48];
};
uniform bool uRenderTransparent;
flat in int fSpriteIndex;
smooth in vec2 fPosition;
smooth in vec2 fTexcoord;
out vec4 oColor;
out vec4 oFlags;
out vec4 oCoverage;
const int SpriteFilter_Nearest = 0;
const int SpriteFilter_Spline36 = 1;
float Spline36Weight(float x)
{
x = abs(x);
if (x < 1.0)
return (((13.0 / 11.0 * x - 453.0 / 209.0) * x - 3.0 / 209.0) * x + 1.0);
else if (x < 2.0)
return (((-6.0 / 11.0 * x + 612.0 / 209.0) * x - 1038.0 / 209.0) * x + 540.0 / 209.0);
else if (x < 3.0)
return (((1.0 / 11.0 * x - 159.0 / 209.0) * x + 434.0 / 209.0) * x - 384.0 / 209.0);
return 0.0;
}
vec4 FetchSpriteTexel(int sprite, ivec2 coord)
{
ivec2 basecoord = ivec2((sprite & 0xF) * 64, (sprite >> 4) * 64);
ivec2 size = uOAM[sprite].Size;
if (any(lessThan(coord, ivec2(0))) || any(greaterThanEqual(coord, size)))
return vec4(0.0);
return texelFetch(SpriteTex, basecoord + coord, 0);
}
vec4 GetSpritePixelNearest(int sprite, vec2 coord)
{
return FetchSpriteTexel(sprite, ivec2(coord));
}
vec4 GetSpritePixelCoverage(int sprite, vec2 coord, out float coverage)
{
vec4 nearestColor = GetSpritePixelNearest(sprite, coord);
if (nearestColor.a <= 0.0)
{
coverage = 0.0;
return vec4(0.0);
}
vec2 srcCoord = coord - vec2(0.5);
ivec2 baseCoord = ivec2(floor(srcCoord));
vec2 frac = fract(srcCoord);
float wx0 = 1.0 - frac.x;
float wx1 = frac.x;
float wy0 = 1.0 - frac.y;
float wy1 = frac.y;
coverage = 0.0;
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 2; x++)
{
vec4 sampleColor = FetchSpriteTexel(sprite, baseCoord + ivec2(x, y));
if (sampleColor.a <= 0.0)
continue;
float weight = (x == 0 ? wx0 : wx1) * (y == 0 ? wy0 : wy1);
coverage += weight;
}
}
return nearestColor;
}
vec4 GetSpritePixelSpline36(int sprite, vec2 coord)
{
vec4 nearestColor = GetSpritePixelNearest(sprite, coord);
if (nearestColor.a <= 0.0)
return vec4(0.0);
vec2 srcCoord = coord - vec2(0.5);
ivec2 baseCoord = ivec2(floor(srcCoord));
vec2 frac = fract(srcCoord);
float wx[6];
float wy[6];
float wxsum = 0.0;
float wysum = 0.0;
for (int i = 0; i < 6; i++)
{
wx[i] = Spline36Weight(float(i - 2) - frac.x);
wy[i] = Spline36Weight(float(i - 2) - frac.y);
wxsum += wx[i];
wysum += wy[i];
}
vec3 accum = vec3(0.0);
vec3 minColor = vec3(1.0);
vec3 maxColor = vec3(0.0);
float totalWeight = 0.0;
for (int y = 0; y < 6; y++)
{
float wyNorm = wy[y] / wysum;
for (int x = 0; x < 6; x++)
{
float weight = (wx[x] / wxsum) * wyNorm;
vec4 sampleColor = FetchSpriteTexel(sprite, baseCoord + ivec2(x - 2, y - 2));
if (sampleColor.a <= 0.0)
continue;
accum += sampleColor.rgb * weight;
minColor = min(minColor, sampleColor.rgb);
maxColor = max(maxColor, sampleColor.rgb);
totalWeight += weight;
}
}
if (totalWeight <= 0.00001)
return nearestColor;
vec3 color = clamp(accum / totalWeight, minColor, maxColor);
return vec4(clamp(color, 0.0, 1.0), nearestColor.a);
}
vec4 GetSpritePixel(int sprite, vec2 coord)
{
if (uSpriteFilterMode == SpriteFilter_Spline36)
return GetSpritePixelSpline36(sprite, coord);
return GetSpritePixelNearest(sprite, coord);
}
void main()
{
vec4 col, flags = vec4(0);
float coverage = 0.0;
vec2 coord = fTexcoord;
if (uOAM[fSpriteIndex].Mosaic)
{
int line = int(fPosition.y);
int mosline = uMosaicLine[line>>2][line&0x3];
float ymin = 0;
if (uOAM[fSpriteIndex].Rotscale != -1)
ymin = -float(uOAM[fSpriteIndex].Size.y) / 2.0;
float mosy = coord.y - (line - mosline);
if (coord.y >= ymin)
coord.y = max(mosy, ymin);
}
if (uOAM[fSpriteIndex].Rotscale != -1)
{
// rotscale sprite
// fTexcoord is based on the sprite center
vec2 sprsize = vec2(uOAM[fSpriteIndex].Size);
vec4 rotscale = vec4(uRotscale[uOAM[fSpriteIndex].Rotscale]) / 256;
mat2 rsmatrix = mat2(rotscale.xy, rotscale.zw);
coord = (coord * rsmatrix) + (sprsize / 2);
if (any(lessThan(coord, vec2(0)))) discard;
if (any(greaterThanEqual(coord, sprsize))) discard;
}
if (uRenderTransparent)
{
// set BG priority and mosaic flags for transparent pixels
if (uOAM[fSpriteIndex].Mosaic)
flags.g = 1;
flags.a = float(uOAM[fSpriteIndex].BGPrio) / 255;
oColor = vec4(0);
oFlags = flags;
oCoverage = vec4(0);
return;
}
if (uOAM[fSpriteIndex].Type == 3)
{
coord += (ivec2(uOAM[fSpriteIndex].TileOffset) >> ivec2(1, 8));
coord *= (1.0/128.0);
col = texture(Capture256Tex, vec3(fract(coord), uOAM[fSpriteIndex].TileStride));
coverage = col.a;
}
else if (uOAM[fSpriteIndex].Type == 4)
{
coord += (ivec2(uOAM[fSpriteIndex].TileOffset) >> ivec2(1, 9));
coord *= (1.0/256.0);
col = texture(Capture256Tex, vec3(fract(coord), uOAM[fSpriteIndex].TileStride));
coverage = col.a;
}
else
{
col = GetSpritePixelCoverage(fSpriteIndex, coord, coverage);
if (uSpriteFilterMode == SpriteFilter_Spline36)
{
vec4 filteredColor = GetSpritePixelSpline36(fSpriteIndex, coord);
if (filteredColor.a > 0.0)
col = filteredColor;
}
}
if (col.a == 0) discard;
// oFlags:
// r = sprite blending flag
// g = mosaic flag
// b = OBJ window flag
// a = BG prio
if (uOAM[fSpriteIndex].OBJMode == 2)
{
// OBJ window
// OBJ mosaic doesn't apply to "OBJ window" sprites
flags.b = 1;
}
else
{
if (uOAM[fSpriteIndex].OBJMode == 1)
{
// semi-transparent sprite
flags.r = 1.0 / 255;
}
else if (uOAM[fSpriteIndex].OBJMode == 3)
{
// bitmap sprite
col.a = float(uOAM[fSpriteIndex].PalOffset) / 31;
flags.r = 2.0 / 255;
}
if (uOAM[fSpriteIndex].Mosaic)
flags.g = 1;
flags.a = float(uOAM[fSpriteIndex].BGPrio) / 255;
}
oColor = col;
oFlags = flags;
oCoverage = vec4(coverage, 0.0, 0.0, 0.0);
}