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
|
// This file is part of the 64k demo project.
// It tests the TextureManager for procedural texture generation and management.
// Tests all public methods with both success and failure cases.
#if !defined(STRIP_ALL) // Test code only - zero size impact on final binary
#include "gpu/texture_manager.h"
#include "procedural/generator.h"
#include "webgpu_test_fixture.h"
#include <cassert>
#include <cstdio>
#include <cstring>
// Test 1: Basic initialization and shutdown
static void test_init_shutdown() {
fprintf(stdout, "Testing init() and shutdown()...\n");
WebGPUTestFixture fixture;
if (!fixture.init()) {
fprintf(stdout, " ⚠ WebGPU unavailable - skipping test\n");
return;
}
TextureManager tm;
// Test init
tm.init(fixture.device(), fixture.queue());
// Test shutdown (should not crash with empty texture map)
tm.shutdown();
fprintf(stdout, " ✓ Init and shutdown OK\n");
}
// Test 2: Create texture from raw data
static void test_create_texture_from_data() {
fprintf(stdout, "Testing create_texture() with raw data...\n");
WebGPUTestFixture fixture;
if (!fixture.init()) {
fprintf(stdout, " ⚠ WebGPU unavailable - skipping test\n");
return;
}
TextureManager tm;
tm.init(fixture.device(), fixture.queue());
// Create 4x4 red texture (RGBA8)
const int width = 4;
const int height = 4;
uint8_t pixels[4 * 4 * 4]; // 4x4 RGBA
for (int i = 0; i < width * height; ++i) {
pixels[i * 4 + 0] = 255; // R
pixels[i * 4 + 1] = 0; // G
pixels[i * 4 + 2] = 0; // B
pixels[i * 4 + 3] = 255; // A
}
tm.create_texture("red_texture", width, height, pixels);
// Verify texture view is valid
WGPUTextureView view = tm.get_texture_view("red_texture");
assert(view != nullptr && "Texture view should be valid");
tm.shutdown();
fprintf(stdout, " ✓ Create texture from raw data OK\n");
}
// Test 3: Create procedural texture
static void test_create_procedural_texture() {
fprintf(stdout, "Testing create_procedural_texture()...\n");
WebGPUTestFixture fixture;
if (!fixture.init()) {
fprintf(stdout, " ⚠ WebGPU unavailable - skipping test\n");
return;
}
TextureManager tm;
tm.init(fixture.device(), fixture.queue());
// Create noise texture using procedural generator
ProceduralTextureDef noise_def;
noise_def.width = 64;
noise_def.height = 64;
noise_def.gen_func = procedural::gen_noise;
noise_def.params = {1234.0f, 1.0f}; // seed, frequency
tm.create_procedural_texture("noise", noise_def);
// Verify texture was created
WGPUTextureView view = tm.get_texture_view("noise");
assert(view != nullptr && "Procedural texture view should be valid");
tm.shutdown();
fprintf(stdout, " ✓ Create procedural texture OK\n");
}
// Test 4: Get texture view for non-existent texture
static void test_get_nonexistent_texture() {
fprintf(stdout, "Testing get_texture_view() for non-existent texture...\n");
WebGPUTestFixture fixture;
if (!fixture.init()) {
fprintf(stdout, " ⚠ WebGPU unavailable - skipping test\n");
return;
}
TextureManager tm;
tm.init(fixture.device(), fixture.queue());
// Try to get non-existent texture
WGPUTextureView view = tm.get_texture_view("does_not_exist");
assert(view == nullptr && "Non-existent texture should return nullptr");
tm.shutdown();
fprintf(stdout, " ✓ Non-existent texture returns nullptr OK\n");
}
// Test 5: Create multiple textures and retrieve them
static void test_multiple_textures() {
fprintf(stdout, "Testing multiple texture creation and retrieval...\n");
WebGPUTestFixture fixture;
if (!fixture.init()) {
fprintf(stdout, " ⚠ WebGPU unavailable - skipping test\n");
return;
}
TextureManager tm;
tm.init(fixture.device(), fixture.queue());
// Create multiple textures
const int size = 32;
uint8_t green_pixels[32 * 32 * 4];
uint8_t blue_pixels[32 * 32 * 4];
// Fill green texture
for (int i = 0; i < size * size; ++i) {
green_pixels[i * 4 + 0] = 0; // R
green_pixels[i * 4 + 1] = 255; // G
green_pixels[i * 4 + 2] = 0; // B
green_pixels[i * 4 + 3] = 255; // A
}
// Fill blue texture
for (int i = 0; i < size * size; ++i) {
blue_pixels[i * 4 + 0] = 0; // R
blue_pixels[i * 4 + 1] = 0; // G
blue_pixels[i * 4 + 2] = 255; // B
blue_pixels[i * 4 + 3] = 255; // A
}
tm.create_texture("green", size, size, green_pixels);
tm.create_texture("blue", size, size, blue_pixels);
// Verify both textures exist
WGPUTextureView green_view = tm.get_texture_view("green");
WGPUTextureView blue_view = tm.get_texture_view("blue");
assert(green_view != nullptr && "Green texture should exist");
assert(blue_view != nullptr && "Blue texture should exist");
assert(green_view != blue_view && "Textures should be different");
tm.shutdown();
fprintf(stdout, " ✓ Multiple textures OK\n");
}
// Test 6: Procedural generation failure handling
static void test_procedural_generation_failure() {
fprintf(stdout, "Testing procedural generation failure handling...\n");
WebGPUTestFixture fixture;
if (!fixture.init()) {
fprintf(stdout, " ⚠ WebGPU unavailable - skipping test\n");
return;
}
TextureManager tm;
tm.init(fixture.device(), fixture.queue());
// Create a generator function that always fails
auto failing_gen = [](uint8_t* buffer, int w, int h, const float* params,
int num_params) -> bool {
(void)buffer;
(void)w;
(void)h;
(void)params;
(void)num_params;
return false; // Simulate failure
};
ProceduralTextureDef failing_def;
failing_def.width = 64;
failing_def.height = 64;
failing_def.gen_func = failing_gen;
failing_def.params = {};
// This should print error message but not crash
tm.create_procedural_texture("failing_texture", failing_def);
// Texture should NOT be created
WGPUTextureView view = tm.get_texture_view("failing_texture");
assert(view == nullptr &&
"Failed procedural generation should not create texture");
tm.shutdown();
fprintf(stdout, " ✓ Procedural generation failure handled OK\n");
}
// Test 7: Shutdown releases all textures
static void test_shutdown_cleanup() {
fprintf(stdout, "Testing shutdown() releases all textures...\n");
WebGPUTestFixture fixture;
if (!fixture.init()) {
fprintf(stdout, " ⚠ WebGPU unavailable - skipping test\n");
return;
}
TextureManager tm;
tm.init(fixture.device(), fixture.queue());
// Create multiple textures
uint8_t pixels[16 * 16 * 4];
memset(pixels, 128, sizeof(pixels));
tm.create_texture("texture1", 16, 16, pixels);
tm.create_texture("texture2", 16, 16, pixels);
tm.create_texture("texture3", 16, 16, pixels);
// Verify textures exist
assert(tm.get_texture_view("texture1") != nullptr);
assert(tm.get_texture_view("texture2") != nullptr);
assert(tm.get_texture_view("texture3") != nullptr);
// Shutdown should release all textures
tm.shutdown();
// After shutdown, textures should be cleared (but we can't query them
// as the TextureManager's internal map is cleared)
fprintf(stdout, " ✓ Shutdown cleanup OK\n");
}
int main() {
fprintf(stdout, "=== TextureManager Tests ===\n");
test_init_shutdown();
test_create_texture_from_data();
test_create_procedural_texture();
test_get_nonexistent_texture();
test_multiple_textures();
test_procedural_generation_failure();
test_shutdown_cleanup();
fprintf(stdout, "=== All TextureManager Tests Passed ===\n");
return 0;
}
#endif /* !defined(STRIP_ALL) */
|