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
|
use bufio;
use gl::*;
use io;
use os;
use image::png;
use math;
use strings;
use trace;
type Texture = struct {
Object,
width: u32,
height: u32,
pack: nullable *Pack,
gl_texture: uint,
};
let TEXTURES = OBJREG_EMPTY;
let TEXTURES_MAX_WIDTH = 0u16;
fn textures_find(name: str) nullable *Texture =
objreg_find(&TEXTURES, name): nullable *Texture;
fn textures_register(tex: *Texture) void =
objreg_register(&TEXTURES, tex);
type TexturesIterator = struct {
inner: ObjectRegistryIterator,
};
fn textures_iter() TexturesIterator =
TexturesIterator {
inner = objreg_iter(&TEXTURES),
};
fn textures_next(it: *TexturesIterator) nullable *Texture =
objreg_next(it): nullable *Texture;
fn load_textures(assets: *Pack) void = {
const tr = &trace::root;
trace::info(tr, "loading textures...");
let max_width = 0i32;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_width);
assert(max_width > 0);
// highest power of 2 representable in 16 bits
if (max_width > 1 << 15) {
max_width = 1 << 15;
};
TEXTURES_MAX_WIDTH = max_width: u16;
textures_register_missingno(MISSINGNO);
const results = resource_search(assets, "textures", ".png");
for (let i = 0z; i < len(results); i += 1) {
const (ident, ext) = results[i];
defer free(ident);
const tr = trace::ctx(tr, "load texture info for {}", ident);
// TODO: better error handling, obviously
match (load_texture_info(assets, ident, &tr)) {
case void => void;
case trace::failed =>
textures_register_missingno(ident);
};
};
let ntextures = 0z;
let npixels = 0z;
let it = textures_iter();
for (true) match (textures_next(&it)) {
case let tex: *Texture =>
ntextures += 1;
npixels += tex.width * tex.height;
case null => break;
};
trace::debug(tr, "loaded {} textures / {} pixels / {} bytes",
ntextures, npixels, npixels * 4);
};
fn textures_register_missingno(name: str) void = {
textures_register(alloc(Texture {
name = strings::dup(name),
width = 16,
height = 16,
pack = null,
gl_texture = 0,
}));
};
fn load_texture_info(
assets: *Pack,
ident: str,
tr: *trace::tracer,
) (void | trace::failed) = {
const f = resource_open(assets, "textures", ident, ".png", tr)?;
let fclosed = false;
defer if (!fclosed) io::close(f): void;
const reader = match (png::newreader(f)) {
case let reader: png::reader =>
yield reader;
case let err: png::error =>
return trace::error(tr, "png error: {}", png::strerror(err));
};
match (png::nextchunk(&reader)) {
case let ctype: u32 =>
if (ctype != png::IHDR) {
return trace::error(tr, "Expected IHDR");
};
case io::EOF =>
return trace::error(tr, "Expected IHDR");
case let err: png::error =>
return trace::error(tr, "png error: {}", png::strerror(err));
};
const ihdr = match (png::ihdr_read(&reader)) {
case let ihdr: png::ihdr =>
yield ihdr;
case let err: png::error =>
return trace::error(tr, "png error: {}", png::strerror(err));
};
const res = io::close(f);
fclosed = true;
match (res) {
case void => void;
case let err: io::error =>
return trace::error(tr, "close: {}", io::strerror(err));
};
match (textures_find(ident)) {
case let tex: *Texture =>
abort("todo stacked assets");
case null =>
textures_register(alloc(Texture {
name = strings::dup(ident),
width = ihdr.width,
height = ihdr.height,
pack = assets,
gl_texture = 0,
}));
};
};
fn finish_textures() void = {
let it = textures_iter();
for (true) match (textures_next(&it)) {
case let tex: *Texture =>
free(tex.name);
case null => break;
};
objreg_clear(&TEXTURES);
};
fn texture_upload(tex: *Texture, tr: *trace::tracer) uint = {
const tr = trace::ctx(tr, "upload texture {}", tex.name);
if (tex.gl_texture != 0) {
return tex.gl_texture;
};
glGenTextures(1, &tex.gl_texture);
glBindTexture(GL_TEXTURE_2D, tex.gl_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_LINEAR: i32);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST: i32);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4);
const po2width = 1 << math::bit_size_u32(tex.width - 1): i32;
const po2height = 1 << math::bit_size_u32(tex.height - 1): i32;
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8: i32,
po2width, po2height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, null);
let pbo = 0u;
glGenBuffers(1, &pbo);
defer glDeleteBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
const bufsize = tex.width * tex.height * 4;
for (true) {
glBufferData(GL_PIXEL_UNPACK_BUFFER,
bufsize: uintptr, null, GL_STREAM_DRAW);
const pixels = glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
0, bufsize: uintptr, GL_MAP_WRITE_BIT) as *opaque;
texture_load(tex, pixels: *[*]u8, tex.width * 4, &tr);
if (glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER) == GL_TRUE) {
break;
};
trace::warn(&tr,
"glUnmapBuffer returned false; retrying upload");
};
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
tex.width: i32, tex.height: i32,
GL_RGBA, GL_UNSIGNED_BYTE, null);
glGenerateMipmap(GL_TEXTURE_2D);
return tex.gl_texture;
};
fn texture_load(tex: *Texture, out: *[*]u8, stride: size, tr: *trace::tracer) void = {
match (texture_try_load(tex, out, stride, tr)) {
case void => void;
case trace::failed =>
texture_load_missingno(tex.width, tex.height, out, stride);
};
};
fn texture_try_load(
tex: *Texture,
out: *[*]u8,
stride: size,
tr: *trace::tracer,
) (void | trace::failed) = {
const assets = match (tex.pack) {
case let assets: *Pack =>
yield assets;
case null =>
return trace::failed;
};
const f = resource_open(assets, "textures", tex.name, ".png", tr)?;
let fclosed = false;
defer if (!fclosed) io::close(f): void;
let rbuf: [os::BUFSZ]u8 = [0...];
let buf = bufio::init(f, rbuf, []);
// TODO: write directly to output buffer.
const png = match (png::load(&buf)) {
case let image: png::image =>
yield image;
case let err: png::error =>
return trace::error(tr, "png::load: {}", png::strerror(err));
};
defer png::image_finish(&png);
const width = png.ihdr.width;
const height = png.ihdr.height;
if (width != tex.width || height != tex.height) {
return trace::error(tr, "Header changed while loading");
};
const end = stride * height;
const rowpad = stride - (width << 2);
const irowpad = 8 - width * png.ihdr.bitdepth & 7;
switch (png.ihdr.colortype) {
case png::colortype::GRAYSCALE =>
if (png.ihdr.bitdepth != 8) {
return trace::error(tr,
"Bit depths other than 8 are not supported");
};
let i = 0z;
let o = 0z;
for (o < end) {
const rowend = o + (width << 2);
for (o < rowend) {
// TODO: out[o + n] (...) o += 4; might actually
// be faster due to pipelining weirdness...
const v = png.pixels[i];
i += 1;
out[o] = v; o += 1;
out[o] = v; o += 1;
out[o] = v; o += 1;
out[o] = 255; o += 1;
};
o += rowpad;
};
case png::colortype::RGB =>
if (png.ihdr.bitdepth != 8) {
return trace::error(tr,
"Bit depths other than 8 are not supported");
};
let i = 0z;
let o = 0z;
for (o < end) {
const rowend = o + (width << 2);
for (o < rowend) {
out[o] = png.pixels[i]; i += 1; o += 1;
out[o] = png.pixels[i]; i += 1; o += 1;
out[o] = png.pixels[i]; i += 1; o += 1;
out[o] = 255; o += 1;
};
o += rowpad;
};
case png::colortype::PLTE =>
const mask = (1 << png.ihdr.bitdepth) - 1;
let i = 0z;
let o = 0z;
for (o < end) {
const rowend = o + (width << 2);
for (o < rowend) {
const index = png.pixels[i >> 3]
>> (i & 7) & mask;
const p = png.palette[index];
i += png.ihdr.bitdepth;
out[o] = (p >> 24): u8; o += 1;
out[o] = (p >> 16): u8; o += 1;
out[o] = (p >> 8): u8; o += 1;
out[o] = 255; o += 1;
};
i += irowpad;
o += rowpad;
};
case png::colortype::GRAYALPHA =>
if (png.ihdr.bitdepth != 8) {
return trace::error(tr,
"Bit depths other than 8 are not supported");
};
let i = 0z;
let o = 0z;
for (o < end) {
const rowend = o + (width << 2);
for (o < rowend) {
const v = png.pixels[i];
i += 1;
out[o] = v; o += 1;
out[o] = v; o += 1;
out[o] = v; o += 1;
out[o] = png.pixels[i]; i += 1; o += 1;
};
o += rowpad;
};
case png::colortype::RGBA =>
if (png.ihdr.bitdepth != 8) {
return trace::error(tr,
"Bit depths other than 8 are not supported");
};
let i = 0z;
let o = 0z;
for (o < end) {
out[o..o + (width << 2)] =
png.pixels[i..i + (width << 2)];
i += width << 2;
o += stride;
};
case =>
return trace::error(tr, "Unknown color type {}",
png.ihdr.colortype: u8);
};
const res = io::close(f);
fclosed = true;
match (res) {
case void => void;
case let err: io::error =>
return trace::error(tr, "close: {}", io::strerror(err));
};
return;
};
fn texture_load_missingno(width: u32, height: u32, out: *[*]u8, stride: size)
void = {
for (let y = 0u32; y < height; y += 1)
for (let x = 0u32; x < width; x += 1) {
const i = (x: size << 2) + y * stride;
if (x < width >> 1 == y < height >> 1) {
out[i] = 0;
out[i + 1] = 0;
out[i + 2] = 0;
} else {
out[i] = 255;
out[i + 1] = 0;
out[i + 2] = 255;
};
out[i + 3] = 255;
};
};
|