-
Notifications
You must be signed in to change notification settings - Fork 83
/
api.c
486 lines (390 loc) · 11.9 KB
/
api.c
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
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
* Copyright (c) 2011-2018, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2018, Andrei Alexeyev <akari@alienslab.net>.
*/
#include "taisei.h"
#include "api.h"
#include "common/backend.h"
#include "common/matstack.h"
#include "common/sprite_batch.h"
#include "common/models.h"
#include "common/state.h"
#include "util/glm.h"
#define B _r_backend.funcs
static struct {
struct {
ShaderProgram *standard;
ShaderProgram *standardnotex;
} progs;
} R;
void r_init(void) {
_r_backend_init();
}
void r_post_init(void) {
_r_state_init();
B.post_init();
_r_mat_init();
_r_models_init();
_r_sprite_batch_init();
preload_resources(RES_SHADER_PROGRAM, RESF_PERMANENT,
"sprite_default",
"texture_post_load",
"standard",
"standardnotex",
NULL);
R.progs.standard = r_shader_get("standard");
R.progs.standardnotex = r_shader_get("standardnotex");
r_shader_ptr(R.progs.standard);
r_color4(1, 1, 1, 1);
log_info("Rendering subsystem initialized (%s)", _r_backend.name);
}
void r_shutdown(void) {
_r_state_shutdown();
_r_sprite_batch_shutdown();
_r_models_shutdown();
B.shutdown();
}
void r_shader_standard(void) {
r_shader_ptr(R.progs.standard);
}
void r_shader_standard_notex(void) {
r_shader_ptr(R.progs.standardnotex);
}
#ifdef DEBUG
static void attr_unused validate_blend_factor(BlendFactor factor) {
switch(factor) {
case BLENDFACTOR_DST_ALPHA:
case BLENDFACTOR_INV_DST_ALPHA:
case BLENDFACTOR_DST_COLOR:
case BLENDFACTOR_INV_DST_COLOR:
case BLENDFACTOR_INV_SRC_ALPHA:
case BLENDFACTOR_INV_SRC_COLOR:
case BLENDFACTOR_ONE:
case BLENDFACTOR_SRC_ALPHA:
case BLENDFACTOR_SRC_COLOR:
case BLENDFACTOR_ZERO:
return;
default: UNREACHABLE;
}
}
static void attr_unused validate_blend_op(BlendOp op) {
switch(op) {
case BLENDOP_ADD:
case BLENDOP_MAX:
case BLENDOP_MIN:
case BLENDOP_REV_SUB:
case BLENDOP_SUB:
return;
default: UNREACHABLE;
}
}
#else
#define validate_blend_factor(x)
#define validate_blend_op(x)
#endif
BlendMode r_blend_compose(
BlendFactor src_color, BlendFactor dst_color, BlendOp color_op,
BlendFactor src_alpha, BlendFactor dst_alpha, BlendOp alpha_op
) {
validate_blend_factor(src_color);
validate_blend_factor(dst_color);
validate_blend_factor(src_alpha);
validate_blend_factor(dst_alpha);
validate_blend_op(color_op);
validate_blend_op(alpha_op);
return BLENDMODE_COMPOSE(
src_color, dst_color, color_op,
src_alpha, dst_alpha, alpha_op
);
}
uint32_t r_blend_component(BlendMode mode, BlendModeComponent component) {
#ifdef DEBUG
uint32_t result = 0;
switch(component) {
case BLENDCOMP_ALPHA_OP:
case BLENDCOMP_COLOR_OP:
result = BLENDMODE_COMPONENT(mode, component);
validate_blend_op(result);
return result;
case BLENDCOMP_DST_ALPHA:
case BLENDCOMP_DST_COLOR:
case BLENDCOMP_SRC_ALPHA:
case BLENDCOMP_SRC_COLOR:
result = BLENDMODE_COMPONENT(mode, component);
validate_blend_factor(result);
return result;
default: UNREACHABLE;
}
#else
return BLENDMODE_COMPONENT(mode, component);
#endif
}
void r_blend_unpack(BlendMode mode, UnpackedBlendMode *dest) {
dest->color.op = r_blend_component(mode, BLENDCOMP_COLOR_OP);
dest->color.src = r_blend_component(mode, BLENDCOMP_SRC_COLOR);
dest->color.dst = r_blend_component(mode, BLENDCOMP_DST_COLOR);
dest->alpha.op = r_blend_component(mode, BLENDCOMP_ALPHA_OP);
dest->alpha.src = r_blend_component(mode, BLENDCOMP_SRC_ALPHA);
dest->alpha.dst = r_blend_component(mode, BLENDCOMP_DST_ALPHA);
}
const UniformTypeInfo* r_uniform_type_info(UniformType type) {
static UniformTypeInfo uniform_typemap[] = {
[UNIFORM_FLOAT] = { 1, sizeof(float) },
[UNIFORM_VEC2] = { 2, sizeof(float) },
[UNIFORM_VEC3] = { 3, sizeof(float) },
[UNIFORM_VEC4] = { 4, sizeof(float) },
[UNIFORM_INT] = { 1, sizeof(int) },
[UNIFORM_IVEC2] = { 2, sizeof(int) },
[UNIFORM_IVEC3] = { 3, sizeof(int) },
[UNIFORM_IVEC4] = { 4, sizeof(int) },
[UNIFORM_SAMPLER] = { 1, sizeof(int) },
[UNIFORM_MAT3] = { 9, sizeof(float) },
[UNIFORM_MAT4] = { 16, sizeof(float) },
};
assert((uint)type < sizeof(uniform_typemap)/sizeof(UniformTypeInfo));
return uniform_typemap + type;
}
#define VATYPE(type) { sizeof(type), alignof(type) }
static VertexAttribTypeInfo va_type_info[] = {
[VA_FLOAT] = VATYPE(float),
[VA_BYTE] = VATYPE(int8_t),
[VA_UBYTE] = VATYPE(uint8_t),
[VA_SHORT] = VATYPE(int16_t),
[VA_USHORT] = VATYPE(uint16_t),
[VA_INT] = VATYPE(int32_t),
[VA_UINT] = VATYPE(uint32_t),
};
const VertexAttribTypeInfo* r_vertex_attrib_type_info(VertexAttribType type) {
uint idx = type;
assert(idx < sizeof(va_type_info)/sizeof(VertexAttribTypeInfo));
return va_type_info + idx;
}
VertexAttribFormat* r_vertex_attrib_format_interleaved(
size_t nattribs,
VertexAttribSpec specs[nattribs],
VertexAttribFormat formats[nattribs],
uint attachment
) {
size_t stride = 0;
memset(formats, 0, sizeof(VertexAttribFormat) * nattribs);
for(uint i = 0; i < nattribs; ++i) {
VertexAttribFormat *a = formats + i;
memcpy(&a->spec, specs + i, sizeof(a->spec));
const VertexAttribTypeInfo *tinfo = r_vertex_attrib_type_info(a->spec.type);
assert(a->spec.elements > 0 && a->spec.elements <= 4);
size_t al = tinfo->alignment;
size_t misalign = (al - (stride & (al - 1))) & (al - 1);
stride += misalign;
a->offset = stride;
stride += tinfo->size * a->spec.elements;
}
for(uint i = 0; i < nattribs; ++i) {
formats[i].stride = stride;
formats[i].attachment = attachment;
}
return formats + nattribs;
}
// TODO: Maybe implement the state tracker/redundant call elimination here somehow
// instead of burdening the backend with it.
//
// For now though, enjoy this degeneracy since the internal backend interface is
// nearly identical to the public API currently.
//
// A more realisic TODO would be to put assertions/argument validation here.
SDL_Window* r_create_window(const char *title, int x, int y, int w, int h, uint32_t flags) {
return B.create_window(title, x, y, w, h, flags);
}
bool r_supports(RendererFeature feature) {
return B.supports(feature);
}
void r_capability(RendererCapability cap, bool value) {
r_capability_bits_t caps = B.capabilities_current(), newcaps;
if(value) {
newcaps = caps | r_capability_bit(cap);
} else {
newcaps = caps & ~r_capability_bit(cap);
}
if(caps != newcaps) {
_r_state_touch_capabilities();
B.capabilities(newcaps);
}
}
bool r_capability_current(RendererCapability cap) {
return B.capabilities_current() & r_capability_bit(cap);
}
void r_color4(float r, float g, float b, float a) {
_r_state_touch_color();
B.color4(r, g, b, a);
}
Color r_color_current(void) {
return B.color_current();
}
void r_blend(BlendMode mode) {
_r_state_touch_blend_mode();
B.blend(mode);
}
BlendMode r_blend_current(void) {
return B.blend_current();
}
void r_cull(CullFaceMode mode) {
_r_state_touch_cull_mode();
B.cull(mode);
}
CullFaceMode r_cull_current(void) {
return B.cull_current();
}
void r_depth_func(DepthTestFunc func) {
_r_state_touch_depth_func();
B.depth_func(func);
}
DepthTestFunc r_depth_func_current(void) {
return B.depth_func_current();
}
void r_shader_ptr(ShaderProgram *prog) {
_r_state_touch_shader();
B.shader(prog);
}
ShaderProgram* r_shader_current(void) {
return B.shader_current();
}
Uniform* r_shader_uniform(ShaderProgram *prog, const char *uniform_name) {
return B.shader_uniform(prog, uniform_name);
}
void r_uniform_ptr(Uniform *uniform, uint count, const void *data) {
_r_state_touch_uniform(uniform);
B.uniform(uniform, count, data);
}
UniformType r_uniform_type(Uniform *uniform) {
return B.uniform_type(uniform);
}
void r_draw(Primitive prim, uint first, uint count, uint32_t *indices, uint instances, uint base_instance) {
B.draw(prim, first, count, indices, instances, base_instance);
}
void r_texture_create(Texture *tex, const TextureParams *params) {
B.texture_create(tex, params);
}
void r_texture_get_params(Texture *tex, TextureParams *params) {
B.texture_get_params(tex, params);
}
void r_texture_set_filter(Texture *tex, TextureFilterMode fmin, TextureFilterMode fmag) {
B.texture_set_filter(tex, fmin, fmag);
}
void r_texture_set_wrap(Texture *tex, TextureWrapMode ws, TextureWrapMode wt) {
B.texture_set_wrap(tex, ws, wt);
}
void r_texture_fill(Texture *tex, uint mipmap, void *image_data) {
B.texture_fill(tex, mipmap, image_data);
}
void r_texture_fill_region(Texture *tex, uint mipmap, uint x, uint y, uint w, uint h, void *image_data) {
B.texture_fill_region(tex, mipmap, x, y, w, h, image_data);
}
void r_texture_invalidate(Texture *tex) {
B.texture_invalidate(tex);
}
void r_texture_destroy(Texture *tex) {
B.texture_destroy(tex);
}
void r_texture_ptr(uint unit, Texture *tex) {
_r_state_touch_texunit(unit);
B.texture(unit, tex);
}
Texture* r_texture_current(uint unit) {
return B.texture_current(unit);
}
void r_framebuffer_create(Framebuffer *fb) {
B.framebuffer_create(fb);
}
void r_framebuffer_attach(Framebuffer *fb, Texture *tex, uint mipmap, FramebufferAttachment attachment) {
B.framebuffer_attach(fb, tex, mipmap, attachment);
}
Texture* r_framebuffer_get_attachment(Framebuffer *fb, FramebufferAttachment attachment) {
return B.framebuffer_get_attachment(fb, attachment);
}
uint r_framebuffer_get_attachment_mipmap(Framebuffer *fb, FramebufferAttachment attachment) {
return B.framebuffer_get_attachment_mipmap(fb, attachment);
}
void r_framebuffer_viewport(Framebuffer *fb, int x, int y, int w, int h) {
r_framebuffer_viewport_rect(fb, (IntRect) { x, y, w, h });
}
void r_framebuffer_viewport_rect(Framebuffer *fb, IntRect viewport) {
assert(viewport.h > 0);
assert(viewport.w > 0);
B.framebuffer_viewport(fb, viewport);
}
void r_framebuffer_viewport_current(Framebuffer *fb, IntRect *viewport) {
B.framebuffer_viewport_current(fb, viewport);
}
void r_framebuffer_destroy(Framebuffer *fb) {
B.framebuffer_destroy(fb);
}
void r_framebuffer(Framebuffer *fb) {
_r_state_touch_framebuffer();
B.framebuffer(fb);
}
Framebuffer * r_framebuffer_current(void) {
return B.framebuffer_current();
}
void r_vertex_buffer_create(VertexBuffer *vbuf, size_t capacity, void *data) {
B.vertex_buffer_create(vbuf, capacity, data);
}
void r_vertex_buffer_destroy(VertexBuffer *vbuf) {
B.vertex_buffer_destroy(vbuf);
}
void r_vertex_buffer_invalidate(VertexBuffer *vbuf) {
B.vertex_buffer_invalidate(vbuf);
}
void r_vertex_buffer_write(VertexBuffer *vbuf, size_t offset, size_t data_size, void *data) {
B.vertex_buffer_write(vbuf, offset, data_size, data);
}
void r_vertex_buffer_append(VertexBuffer *vbuf, size_t data_size, void *data) {
B.vertex_buffer_append(vbuf, data_size, data);
}
void r_vertex_array_create(VertexArray *varr) {
B.vertex_array_create(varr);
}
void r_vertex_array_destroy(VertexArray *varr) {
B.vertex_array_destroy(varr);
}
void r_vertex_array_attach_buffer(VertexArray *varr, VertexBuffer *vbuf, uint attachment) {
B.vertex_array_attach_buffer(varr, vbuf, attachment);
}
VertexBuffer* r_vertex_array_get_attachment(VertexArray *varr, uint attachment) {
return B.vertex_array_get_attachment(varr, attachment);
}
void r_vertex_array_layout(VertexArray *varr, uint nattribs, VertexAttribFormat attribs[nattribs]) {
B.vertex_array_layout(varr, nattribs, attribs);
}
void r_vertex_array(VertexArray *varr) {
_r_state_touch_vertex_array();
B.vertex_array(varr);
}
VertexArray* r_vertex_array_current(void) {
return B.vertex_array_current();
}
void r_clear(ClearBufferFlags flags) {
B.clear(flags);
}
void r_clear_color4(float r, float g, float b, float a) {
_r_state_touch_clear_color();
B.clear_color4(r, g, b, a);
}
Color r_clear_color_current(void) {
return B.clear_color_current();
}
void r_vsync(VsyncMode mode) {
_r_state_touch_vsync();
B.vsync(mode);
}
VsyncMode r_vsync_current(void) {
return B.vsync_current();
}
void r_swap(SDL_Window *window) {
_r_sprite_batch_end_frame();
B.swap(window);
}
uint8_t* r_screenshot(uint *out_width, uint *out_height) {
return B.screenshot(out_width, out_height);
}