-
Notifications
You must be signed in to change notification settings - Fork 4
/
raster-shader-generic-fill.js
368 lines (310 loc) · 15.2 KB
/
raster-shader-generic-fill.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* 2019-2022 Tarpeeksi Hyvae Soft
*
* Software: Retro n-gon renderer
*
*/
// No performance-enhancing assumptions are made, so this is the most compatible filler,
// but also potentially the slowest.
export function generic_fill({
ngon,
ngonIdx,
leftEdges,
rightEdges,
numLeftEdges,
numRightEdges,
pixelBuffer32,
auxiliaryBuffers
})
{
const usePalette = Rngon.internalState.usePalette;
const usePixelShader = Rngon.internalState.usePixelShader;
const fragmentBuffer = Rngon.internalState.fragmentBuffer.data;
const depthBuffer = (Rngon.internalState.useDepthBuffer? Rngon.internalState.depthBuffer.data : null);
const pixelBufferImage = Rngon.internalState.pixelBuffer;
const pixelBufferClamped8 = pixelBufferImage.data;
const pixelBufferWidth = pixelBufferImage.width;
const material = ngon.material;
const texture = (material.texture || null);
let textureMipLevel = null;
let textureMipLevelIdx = 0;
if (texture)
{
const numMipLevels = texture.mipLevels.length;
textureMipLevelIdx = Math.max(0, Math.min((numMipLevels - 1), Math.round((numMipLevels - 1) * ngon.mipLevel)));
textureMipLevel = texture.mipLevels[textureMipLevelIdx];
}
let curLeftEdgeIdx = 0;
let curRightEdgeIdx = 0;
let leftEdge = leftEdges[curLeftEdgeIdx];
let rightEdge = rightEdges[curRightEdgeIdx];
if (!numLeftEdges || !numRightEdges) return;
// Note: We assume the n-gon's vertices to be sorted by increasing Y.
const ngonStartY = leftEdges[0].top;
const ngonEndY = leftEdges[numLeftEdges-1].bottom;
// Rasterize the n-gon in horizontal pixel spans over its height.
for (let y = ngonStartY; y < ngonEndY; y++)
{
const spanStartX = Math.min(pixelBufferWidth, Math.max(0, Math.round(leftEdge.start.x)));
const spanEndX = Math.min(pixelBufferWidth, Math.max(0, Math.ceil(rightEdge.start.x)));
const spanWidth = ((spanEndX - spanStartX) + 1);
if (spanWidth > 0)
{
const deltaDepth = ((rightEdge.start.depth - leftEdge.start.depth) / spanWidth);
let iplDepth = (leftEdge.start.depth - deltaDepth);
const deltaShade = ((rightEdge.start.shade - leftEdge.start.shade) / spanWidth);
let iplShade = (leftEdge.start.shade - deltaShade);
const deltaU = ((rightEdge.start.u - leftEdge.start.u) / spanWidth);
let iplU = (leftEdge.start.u - deltaU);
const deltaV = ((rightEdge.start.v - leftEdge.start.v) / spanWidth);
let iplV = (leftEdge.start.v - deltaV);
const deltaInvW = ((rightEdge.start.invW - leftEdge.start.invW) / spanWidth);
let iplInvW = (leftEdge.start.invW - deltaInvW);
if (usePixelShader)
{
var deltaWorldX = ((rightEdge.start.worldX - leftEdge.start.worldX) / spanWidth);
var iplWorldX = (leftEdge.start.worldX - deltaWorldX);
var deltaWorldY = ((rightEdge.start.worldY - leftEdge.start.worldY) / spanWidth);
var iplWorldY = (leftEdge.start.worldY - deltaWorldY);
var deltaWorldZ = ((rightEdge.start.worldZ - leftEdge.start.worldZ) / spanWidth);
var iplWorldZ = (leftEdge.start.worldZ - deltaWorldZ);
}
// Assumes the depth buffer consists of 1 element per pixel.
let pixelBufferIdx = ((spanStartX + y * pixelBufferWidth) - 1);
// Draw the span into the pixel buffer.
for (let x = spanStartX; x < spanEndX; x++)
{
// Will hold the texture coordinates used if we end up drawing
// a textured pixel at the current x,y screen location.
let u = 0.0, v = 0.0;
// Update values that're interpolated horizontally along the span.
iplDepth += deltaDepth;
iplShade += deltaShade;
iplU += deltaU;
iplV += deltaV;
iplInvW += deltaInvW;
pixelBufferIdx++;
if (usePixelShader)
{
iplWorldX += deltaWorldX;
iplWorldY += deltaWorldY;
iplWorldZ += deltaWorldZ;
}
const depth = (iplDepth / iplInvW);
// Depth test.
if (depthBuffer && (depthBuffer[pixelBufferIdx] <= depth)) continue;
let shade = (material.renderVertexShade? iplShade : 1);
// The color we'll write into the pixel buffer for this pixel; assuming
// it passes the alpha test, the depth test, etc.
let red = 0;
let green = 0;
let blue = 0;
let index = 0;
// Solid fill.
if (!texture)
{
// Note: We assume that the triangle transformer has already culled away
// n-gons whose base color alpha is less than 255; so we don't test for
// material.allowAlphaReject.
if (material.allowAlphaBlend &&
Rngon.baseModules.rasterize.stipple(material.color.alpha, x, y))
{
continue;
}
index = material.color.index;
red = (material.color.red * shade);
green = (material.color.green * shade);
blue = (material.color.blue * shade);
}
// Textured fill.
else
{
switch (material.textureMapping)
{
// Affine mapping for power-of-two textures.
case "affine":
{
u = (iplU / iplInvW);
v = (iplV / iplInvW);
switch (material.uvWrapping)
{
case "clamp":
{
const signU = Math.sign(u);
const signV = Math.sign(v);
const upperLimit = (1 - Number.EPSILON);
u = Math.max(0, Math.min(Math.abs(u), upperLimit));
v = Math.max(0, Math.min(Math.abs(v), upperLimit));
// Negative UV coordinates flip the texture.
if (signU === -1) u = (upperLimit - u);
if (signV === -1) v = (upperLimit - v);
u *= textureMipLevel.width;
v *= textureMipLevel.height;
break;
}
case "repeat":
{
u -= Math.floor(u);
v -= Math.floor(v);
u *= textureMipLevel.width;
v *= textureMipLevel.height;
// Modulo for power-of-two. This will also flip the texture for
// negative UV coordinates.
u = (u & (textureMipLevel.width - 1));
v = (v & (textureMipLevel.height - 1));
break;
}
default: Rngon.$throw("Unrecognized UV wrapping mode."); break;
}
break;
}
// Affine mapping for wrapping non-power-of-two textures.
/// FIXME: This implementation is a bit kludgy.
/// TODO: Add clamped UV wrapping mode (we can just use the one for
/// power-of-two textures).
case "affine-npot":
{
u = (iplU / iplInvW);
v = (iplV / iplInvW);
u *= textureMipLevel.width;
v *= textureMipLevel.height;
// Wrap with repetition.
/// FIXME: Why do we need to test for UV < 0 even when using positive
/// but tiling UV coordinates? Doesn't render properly unless we do.
if ((u < 0) ||
(v < 0) ||
(u >= textureMipLevel.width) ||
(v >= textureMipLevel.height))
{
const uWasNeg = (u < 0);
const vWasNeg = (v < 0);
u = (Math.abs(u) % textureMipLevel.width);
v = (Math.abs(v) % textureMipLevel.height);
if (uWasNeg) u = (textureMipLevel.width - u);
if (vWasNeg) v = (textureMipLevel.height - v);
}
break;
}
// Screen-space UV mapping, as used e.g. in the DOS game Rally-Sport.
case "ortho":
{
const ngonHeight = (ngonEndY - ngonStartY);
// Pixel coordinates relative to the polygon.
const ngonX = (x - spanStartX + 1);
const ngonY = (y - ngonStartY + 1);
u = (ngonX * (textureMipLevel.width / spanWidth));
v = (ngonY * (textureMipLevel.height / ngonHeight));
// The texture image is flipped, so we need to flip V as well.
v = (textureMipLevel.height - v);
break;
}
default: Rngon.$throw("Unknown texture-mapping mode."); break;
}
const texel = textureMipLevel.pixels[(~~u) + (~~v) * textureMipLevel.width];
// Make sure we gracefully exit if accessing the texture out of bounds.
if (!texel)
{
continue;
}
if (material.allowAlphaReject &&
(texel.alpha !== 255))
{
continue;
}
if (material.allowAlphaBlend &&
Rngon.baseModules.rasterize.stipple(material.color.alpha, x, y))
{
continue;
}
index = texel.index;
red = (texel.red * material.color.unitRange.red * shade);
green = (texel.green * material.color.unitRange.green * shade);
blue = (texel.blue * material.color.unitRange.blue * shade);
}
// The pixel passed its alpha test, depth test, etc., and should be drawn
// on screen.
{
if (usePalette)
{
pixelBufferClamped8[pixelBufferIdx] = index;
}
else
{
// If shade is > 1, the color values may exceed 255, in which case we write into
// the clamped 8-bit view to get 'free' clamping.
if (shade > 1)
{
const idx = (pixelBufferIdx * 4);
pixelBufferClamped8[idx+0] = red;
pixelBufferClamped8[idx+1] = green;
pixelBufferClamped8[idx+2] = blue;
pixelBufferClamped8[idx+3] = 255;
}
else
{
pixelBuffer32[pixelBufferIdx] = (
(255 << 24) +
(blue << 16) +
(green << 8) +
red
);
}
}
if (depthBuffer)
{
depthBuffer[pixelBufferIdx] = depth;
}
if (usePixelShader)
{
const fragment = fragmentBuffer[pixelBufferIdx];
fragment.ngonIdx = ngonIdx;
fragment.textureUScaled = ~~u;
fragment.textureVScaled = ~~v;
fragment.depth = (iplDepth / iplInvW);
fragment.shade = iplShade;
fragment.worldX = (iplWorldX / iplInvW);
fragment.worldY = (iplWorldY / iplInvW);
fragment.worldZ = (iplWorldZ / iplInvW);
fragment.w = (1 / iplInvW);
}
for (let b = 0; b < auxiliaryBuffers.length; b++)
{
if (material.auxiliary[auxiliaryBuffers[b].property] !== null)
{
// Buffers are expected to consist of one element per pixel.
auxiliaryBuffers[b].buffer[pixelBufferIdx] = material.auxiliary[auxiliaryBuffers[b].property];
}
}
}
}
}
// Update values that're interpolated vertically along the edges.
{
leftEdge.start.x += leftEdge.delta.x;
leftEdge.start.depth += leftEdge.delta.depth;
leftEdge.start.shade += leftEdge.delta.shade;
leftEdge.start.u += leftEdge.delta.u;
leftEdge.start.v += leftEdge.delta.v;
leftEdge.start.invW += leftEdge.delta.invW;
rightEdge.start.x += rightEdge.delta.x;
rightEdge.start.depth += rightEdge.delta.depth;
rightEdge.start.shade += rightEdge.delta.shade;
rightEdge.start.u += rightEdge.delta.u;
rightEdge.start.v += rightEdge.delta.v;
rightEdge.start.invW += rightEdge.delta.invW;
if (usePixelShader)
{
leftEdge.start.worldX += leftEdge.delta.worldX;
leftEdge.start.worldY += leftEdge.delta.worldY;
leftEdge.start.worldZ += leftEdge.delta.worldZ;
rightEdge.start.worldX += rightEdge.delta.worldX;
rightEdge.start.worldY += rightEdge.delta.worldY;
rightEdge.start.worldZ += rightEdge.delta.worldZ;
}
}
// We can move onto the next edge when we're at the end of the current one.
if (y === (leftEdge.bottom - 1)) leftEdge = leftEdges[++curLeftEdgeIdx];
if (y === (rightEdge.bottom - 1)) rightEdge = rightEdges[++curRightEdgeIdx];
}
return true;
}