-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual_parallel_test.go
More file actions
473 lines (402 loc) · 12.1 KB
/
visual_parallel_test.go
File metadata and controls
473 lines (402 loc) · 12.1 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package gg
import (
"image/color"
"testing"
"github.com/gogpu/gg/internal/parallel"
)
// =============================================================================
// Visual Regression Tests for Parallel Rendering
// =============================================================================
//
// These tests compare parallel rendering output with expected results to ensure
// correctness of the parallel rendering implementation.
//
// =============================================================================
// TestVisualRegression_ParallelClear tests that parallel Clear produces
// pixel-perfect results matching a reference clear operation.
func TestVisualRegression_ParallelClear(t *testing.T) {
const (
width = 256
height = 256
)
testColors := []struct {
name string
color color.Color
want [4]byte
}{
{"white", color.White, [4]byte{255, 255, 255, 255}},
{"black", color.Black, [4]byte{0, 0, 0, 255}},
{"red", color.RGBA{R: 255, G: 0, B: 0, A: 255}, [4]byte{255, 0, 0, 255}},
{"green", color.RGBA{R: 0, G: 255, B: 0, A: 255}, [4]byte{0, 255, 0, 255}},
{"blue", color.RGBA{R: 0, G: 0, B: 255, A: 255}, [4]byte{0, 0, 255, 255}},
{"transparent", color.Transparent, [4]byte{0, 0, 0, 0}},
{"semi_transparent", color.RGBA{R: 128, G: 64, B: 32, A: 128}, [4]byte{128, 64, 32, 128}},
}
for _, tc := range testColors {
t.Run(tc.name, func(t *testing.T) {
pr := parallel.NewParallelRasterizer(width, height)
if pr == nil {
t.Fatal("Failed to create parallel rasterizer")
}
defer pr.Close()
// Perform parallel clear
pr.Clear(tc.color)
// Composite to buffer
stride := width * 4
dst := make([]byte, height*stride)
pr.Composite(dst, stride)
// Verify every pixel matches expected color (pixel-perfect, tolerance 0)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
offset := y*stride + x*4
got := [4]byte{dst[offset], dst[offset+1], dst[offset+2], dst[offset+3]}
if got != tc.want {
t.Errorf("Pixel (%d,%d) = %v, want %v", x, y, got, tc.want)
return // Stop on first mismatch
}
}
}
})
}
}
// TestVisualRegression_ParallelFillRect tests that parallel FillRect produces
// pixel-perfect results.
func TestVisualRegression_ParallelFillRect(t *testing.T) {
const (
width = 256
height = 256
)
testCases := []struct {
name string
bgColor color.Color
rectX, rectY, rectW, h int
rectColor color.Color
}{
{
name: "centered_red_rect",
bgColor: color.Black,
rectX: 64,
rectY: 64,
rectW: 128,
h: 128,
rectColor: color.RGBA{R: 255, G: 0, B: 0, A: 255},
},
{
name: "corner_green_rect",
bgColor: color.White,
rectX: 0,
rectY: 0,
rectW: 64,
h: 64,
rectColor: color.RGBA{R: 0, G: 255, B: 0, A: 255},
},
{
name: "spanning_multiple_tiles",
bgColor: color.Black,
rectX: 32,
rectY: 32,
rectW: 192,
h: 192,
rectColor: color.RGBA{R: 0, G: 0, B: 255, A: 255},
},
{
name: "edge_tile_rect",
bgColor: color.White,
rectX: 200,
rectY: 200,
rectW: 56, // Goes to edge
h: 56,
rectColor: color.RGBA{R: 255, G: 255, B: 0, A: 255},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
pr := parallel.NewParallelRasterizer(width, height)
if pr == nil {
t.Fatal("Failed to create parallel rasterizer")
}
defer pr.Close()
// Clear with background color
pr.Clear(tc.bgColor)
// Fill rectangle
pr.FillRect(tc.rectX, tc.rectY, tc.rectW, tc.h, tc.rectColor)
// Composite to buffer
stride := width * 4
dst := make([]byte, height*stride)
pr.Composite(dst, stride)
// Get expected colors
bgR, bgG, bgB, bgA := tc.bgColor.RGBA()
bgExpected := [4]byte{byte(bgR >> 8), byte(bgG >> 8), byte(bgB >> 8), byte(bgA >> 8)}
rectR, rectG, rectB, rectA := tc.rectColor.RGBA()
rectExpected := [4]byte{byte(rectR >> 8), byte(rectG >> 8), byte(rectB >> 8), byte(rectA >> 8)}
// Verify pixels
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
offset := y*stride + x*4
got := [4]byte{dst[offset], dst[offset+1], dst[offset+2], dst[offset+3]}
insideRect := x >= tc.rectX && x < tc.rectX+tc.rectW &&
y >= tc.rectY && y < tc.rectY+tc.h
var want [4]byte
if insideRect {
want = rectExpected
} else {
want = bgExpected
}
if got != want {
t.Errorf("Pixel (%d,%d) inside=%v: got %v, want %v",
x, y, insideRect, got, want)
return
}
}
}
})
}
}
// TestVisualRegression_ParallelComposite tests that Composite correctly
// assembles tiles into a contiguous buffer.
func TestVisualRegression_ParallelComposite(t *testing.T) {
const (
width = 256
height = 256
)
pr := parallel.NewParallelRasterizer(width, height)
if pr == nil {
t.Fatal("Failed to create parallel rasterizer")
}
defer pr.Close()
// Create a gradient pattern: color depends on tile position
tiles := pr.Grid().AllTiles()
for _, tile := range tiles {
tileX, tileY, tileW, tileH := tile.Bounds()
for py := 0; py < tileH; py++ {
for px := 0; px < tileW; px++ {
canvasX := tileX + px
canvasY := tileY + py
offset := (py*tileW + px) * 4
tile.Data[offset] = byte(canvasX) // R = x
tile.Data[offset+1] = byte(canvasY) // G = y
tile.Data[offset+2] = 128 // B = constant
tile.Data[offset+3] = 255 // A = opaque
}
}
}
// Composite to buffer
stride := width * 4
dst := make([]byte, height*stride)
pr.Composite(dst, stride)
// Verify the gradient pattern
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
offset := y*stride + x*4
gotR := dst[offset]
gotG := dst[offset+1]
gotB := dst[offset+2]
gotA := dst[offset+3]
wantR := byte(x)
wantG := byte(y)
wantB := byte(128)
wantA := byte(255)
if gotR != wantR || gotG != wantG || gotB != wantB || gotA != wantA {
t.Errorf("Pixel (%d,%d) = [%d,%d,%d,%d], want [%d,%d,%d,%d]",
x, y, gotR, gotG, gotB, gotA, wantR, wantG, wantB, wantA)
return
}
}
}
}
// TestVisualRegression_ParallelVsSerial compares parallel rendering output
// with a reference serial implementation.
func TestVisualRegression_ParallelVsSerial(t *testing.T) {
const (
width = 256
height = 256
)
// Test case: Clear + FillRect
pr := parallel.NewParallelRasterizer(width, height)
if pr == nil {
t.Fatal("Failed to create parallel rasterizer")
}
defer pr.Close()
// Perform parallel operations
bgColor := color.RGBA{R: 50, G: 100, B: 150, A: 255}
rectColor := color.RGBA{R: 200, G: 50, B: 100, A: 255}
pr.Clear(bgColor)
pr.FillRect(50, 50, 100, 100, rectColor)
// Composite parallel result
stride := width * 4
parallelResult := make([]byte, height*stride)
pr.Composite(parallelResult, stride)
// Generate reference result serially
serialResult := make([]byte, height*stride)
// Serial clear
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
offset := y*stride + x*4
serialResult[offset] = 50
serialResult[offset+1] = 100
serialResult[offset+2] = 150
serialResult[offset+3] = 255
}
}
// Serial FillRect
for y := 50; y < 150; y++ {
for x := 50; x < 150; x++ {
offset := y*stride + x*4
serialResult[offset] = 200
serialResult[offset+1] = 50
serialResult[offset+2] = 100
serialResult[offset+3] = 255
}
}
// Compare results (pixel-perfect, tolerance 0)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
offset := y*stride + x*4
pR := parallelResult[offset]
pG := parallelResult[offset+1]
pB := parallelResult[offset+2]
pA := parallelResult[offset+3]
sR := serialResult[offset]
sG := serialResult[offset+1]
sB := serialResult[offset+2]
sA := serialResult[offset+3]
if pR != sR || pG != sG || pB != sB || pA != sA {
t.Errorf("Pixel (%d,%d) parallel=[%d,%d,%d,%d], serial=[%d,%d,%d,%d]",
x, y, pR, pG, pB, pA, sR, sG, sB, sA)
return
}
}
}
}
// TestVisualRegression_TileBoundaries tests that there are no visible seams
// at tile boundaries.
func TestVisualRegression_TileBoundaries(t *testing.T) {
const (
width = 256
height = 256
)
pr := parallel.NewParallelRasterizer(width, height)
if pr == nil {
t.Fatal("Failed to create parallel rasterizer")
}
defer pr.Close()
// Fill with a solid color
solidColor := color.RGBA{R: 128, G: 128, B: 128, A: 255}
pr.Clear(solidColor)
// Composite to buffer
stride := width * 4
dst := make([]byte, height*stride)
pr.Composite(dst, stride)
// Check tile boundary pixels specifically
tileBoundaries := []int{63, 64, 127, 128, 191, 192}
for _, bx := range tileBoundaries {
for _, by := range tileBoundaries {
if bx >= width || by >= height {
continue
}
offset := by*stride + bx*4
got := [4]byte{dst[offset], dst[offset+1], dst[offset+2], dst[offset+3]}
want := [4]byte{128, 128, 128, 255}
if got != want {
t.Errorf("Tile boundary pixel (%d,%d) = %v, want %v", bx, by, got, want)
}
}
}
}
// TestVisualRegression_EdgeTiles tests that edge tiles (partial tiles at
// canvas boundaries) are rendered correctly.
func TestVisualRegression_EdgeTiles(t *testing.T) {
// Use non-multiple-of-64 dimensions to create edge tiles
const (
width = 200 // Creates 36-pixel-wide edge tile
height = 200 // Creates 36-pixel-tall edge tile
)
pr := parallel.NewParallelRasterizer(width, height)
if pr == nil {
t.Fatal("Failed to create parallel rasterizer")
}
defer pr.Close()
// Fill with gradient based on position
tiles := pr.Grid().AllTiles()
for _, tile := range tiles {
tileX, tileY, tileW, tileH := tile.Bounds()
for py := 0; py < tileH; py++ {
for px := 0; px < tileW; px++ {
canvasX := tileX + px
canvasY := tileY + py
offset := (py*tileW + px) * 4
tile.Data[offset] = byte(canvasX)
tile.Data[offset+1] = byte(canvasY)
tile.Data[offset+2] = 0
tile.Data[offset+3] = 255
}
}
}
// Composite to buffer
stride := width * 4
dst := make([]byte, height*stride)
pr.Composite(dst, stride)
// Verify edge pixels are correct
edgePixels := []struct {
x, y int
}{
{199, 0}, // Right edge
{0, 199}, // Bottom edge
{199, 199}, // Corner
{128, 199}, // Bottom edge past first tile
{199, 128}, // Right edge past first tile
}
for _, ep := range edgePixels {
offset := ep.y*stride + ep.x*4
gotR := dst[offset]
gotG := dst[offset+1]
wantR := byte(ep.x)
wantG := byte(ep.y)
if gotR != wantR || gotG != wantG {
t.Errorf("Edge pixel (%d,%d) R,G = [%d,%d], want [%d,%d]",
ep.x, ep.y, gotR, gotG, wantR, wantG)
}
}
}
// TestVisualRegression_MultipleOperations tests a sequence of operations
// produces correct results.
func TestVisualRegression_MultipleOperations(t *testing.T) {
const (
width = 256
height = 256
)
pr := parallel.NewParallelRasterizer(width, height)
if pr == nil {
t.Fatal("Failed to create parallel rasterizer")
}
defer pr.Close()
// Perform a sequence of operations
pr.Clear(color.Black)
pr.FillRect(0, 0, 128, 128, color.RGBA{R: 255, G: 0, B: 0, A: 255}) // Red top-left
pr.FillRect(128, 0, 128, 128, color.RGBA{R: 0, G: 255, B: 0, A: 255}) // Green top-right
pr.FillRect(0, 128, 128, 128, color.RGBA{R: 0, G: 0, B: 255, A: 255}) // Blue bottom-left
pr.FillRect(128, 128, 128, 128, color.RGBA{R: 255, G: 255, B: 0, A: 255}) // Yellow bottom-right
// Composite to buffer
stride := width * 4
dst := make([]byte, height*stride)
pr.Composite(dst, stride)
// Define expected colors for each quadrant
quadrants := []struct {
name string
testX, testY int
want [4]byte
}{
{"top-left (red)", 64, 64, [4]byte{255, 0, 0, 255}},
{"top-right (green)", 192, 64, [4]byte{0, 255, 0, 255}},
{"bottom-left (blue)", 64, 192, [4]byte{0, 0, 255, 255}},
{"bottom-right (yellow)", 192, 192, [4]byte{255, 255, 0, 255}},
}
for _, q := range quadrants {
offset := q.testY*stride + q.testX*4
got := [4]byte{dst[offset], dst[offset+1], dst[offset+2], dst[offset+3]}
if got != q.want {
t.Errorf("Quadrant %s at (%d,%d) = %v, want %v",
q.name, q.testX, q.testY, got, q.want)
}
}
}