-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenesis.h
491 lines (420 loc) · 13.6 KB
/
genesis.h
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
487
488
489
490
491
#ifndef GENESIS_H
#define GENESIS_H
#ifdef __cplusplus
extern "C"
{
#endif
#define GS_VERSION_MAJOR 0
#define GS_VERSION_MINOR 1
#define GS_VERSION_PATCH 0
#define GS_MAX_VERTEX_LAYOUT_ITEMS 128
#define GS_MAX_TEXTURE_SLOTS 16
#define GS_MAX_COMMAND_LIST_ITEMS 4096
#define GS_MAX_COMMAND_SUBMISSIONS 128
#define GS_MALLOC(size) malloc(size)
#define GS_ALLOC_MULTIPLE(obj, count) (obj*)malloc(sizeof(obj) * count)
#define GS_ASSERT(cond) if(!(cond)) { printf("Assertion failed: %s, file: %s, line: %d\n", #cond, __FILE__, __LINE__); exit(1); }
#define GS_MEMSET(ptr, value, size) memset(ptr, value, size)
#define GS_ALLOC(obj) GS_ALLOC_MULTIPLE(obj, 1)
#define GS_FREE(obj) free(obj)
#define GS_BOOL int
#define GS_TRUE 1
#define GS_FALSE 0
typedef enum {
GS_BACKEND_OPENGL
} GsBackendType;
typedef enum {
GS_CAPABILITY_RENDERER = 1 << 0,
} GsCapability;
typedef enum {
GS_ATTRIB_TYPE_UINT8,
GS_ATTRIB_TYPE_INT16,
GS_ATTRIB_TYPE_FLOAT
} GsVtxAttribType;
typedef enum {
GS_COMMAND_NONE,
GS_COMMAND_CLEAR,
GS_COMMAND_SET_VIEWPORT,
GS_COMMAND_USE_PIPELINE,
GS_COMMAND_USE_BUFFER,
GS_COMMAND_USE_TEXTURE,
GS_COMMAND_DRAW_ARRAYS,
GS_COMMAND_DRAW_INDEXED,
GS_COMMAND_SET_SCISSOR,
GS_COMMAND_SET_UNIFORM_INT,
GS_COMMAND_SET_UNIFORM_FLOAT,
GS_COMMAND_SET_UNIFORM_VEC2,
GS_COMMAND_SET_UNIFORM_VEC3,
GS_COMMAND_SET_UNIFORM_VEC4,
GS_COMMAND_SET_UNIFORM_MAT4
} GsCommandType;
typedef enum {
GS_CLEAR_COLOR = 1,
GS_CLEAR_DEPTH = 2,
GS_CLEAR_STENCIL = 4
} GsClearFlags;
typedef enum {
GS_BUFFER_TYPE_VERTEX,
GS_BUFFER_TYPE_INDEX
} GsBufferType;
typedef enum {
GS_BUFFER_INTENT_DRAW_STATIC,
GS_BUFFER_INTENT_DRAW_DYNAMIC,
GS_BUFFER_INTENT_DRAW_STREAM,
GS_BUFFER_INTENT_READ_STATIC,
GS_BUFFER_INTENT_READ_DYNAMIC,
GS_BUFFER_INTENT_READ_STREAM,
GS_BUFFER_INTENT_COPY_STATIC,
GS_BUFFER_INTENT_COPY_DYNAMIC,
GS_BUFFER_INTENT_COPY_STREAM
} GsBufferIntent;
typedef enum {
GS_SHADER_TYPE_VERTEX,
GS_SHADER_TYPE_FRAGMENT
} GsShaderType;
typedef enum {
GS_TEXTURE_FORMAT_RGB8,
GS_TEXTURE_FORMAT_RGBA8,
GS_TEXTURE_FORMAT_RGB16F,
GS_TEXTURE_FORMAT_RGBA16F,
GS_TEXTURE_FORMAT_DEPTH24_STENCIL8,
GS_TEXTURE_FORMAT_DEPTH32F
} GsTextureFormat;
typedef enum {
GS_TEXTURE_WRAP_REPEAT,
GS_TEXTURE_WRAP_CLAMP,
GS_TEXTURE_WRAP_MIRROR
} GsTextureWrap;
typedef enum {
GS_TEXTURE_FILTER_NEAREST,
GS_TEXTURE_FILTER_LINEAR,
GS_TEXTURE_FILTER_MIPMAP_NEAREST,
GS_TEXTURE_FILTER_MIPMAP_LINEAR
} GsTextureFilter;
typedef enum {
GS_TEXTURE_TYPE_2D,
GS_TEXTURE_TYPE_CUBEMAP
} GsTextureType;
typedef enum {
GS_CUBEMAP_FACE_UP,
GS_CUBEMAP_FACE_DOWN,
GS_CUBEMAP_FACE_LEFT,
GS_CUBEMAP_FACE_RIGHT,
GS_CUBEMAP_FACE_FRONT,
GS_CUBEMAP_FACE_BACK,
GS_CUBEMAP_FACE_NONE
} GsCubemapFace;
typedef enum {
GS_BLEND_FACTOR_ZERO,
GS_BLEND_FACTOR_ONE,
GS_BLEND_FACTOR_SRC_COLOR,
GS_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
GS_BLEND_FACTOR_DST_COLOR,
GS_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
GS_BLEND_FACTOR_SRC_ALPHA,
GS_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
GS_BLEND_FACTOR_DST_ALPHA,
GS_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
GS_BLEND_FACTOR_CONSTANT_COLOR,
GS_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
GS_BLEND_FACTOR_CONSTANT_ALPHA,
GS_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA,
GS_BLEND_FACTOR_SRC_ALPHA_SATURATE
} GsBlendFactor;
typedef enum {
GS_BLEND_OP_ADD,
GS_BLEND_OP_SUBTRACT,
GS_BLEND_OP_REVERSE_SUBTRACT,
GS_BLEND_OP_MIN,
GS_BLEND_OP_MAX
} GsBlendOp;
typedef int GsUniformLocation;
typedef struct GsBackend GsBackend;
typedef struct GsConfig GsConfig;
typedef struct GsVtxLayout GsVtxLayout;
typedef struct GsVtxLayoutItem GsVtxLayoutItem;
typedef struct GsCommandList GsCommandList;
typedef struct GsCommandListItem GsCommandListItem;
typedef struct GsPipeline GsPipeline;
typedef struct GsShader GsShader;
typedef struct GsProgram GsProgram;
typedef struct GsBuffer GsBuffer;
typedef struct GsTexture GsTexture;
typedef struct GsUnmanagedBufferData GsUnmanagedBufferData;
typedef struct GsClearCommand GsClearCommand;
typedef struct GsViewportCommand GsViewportCommand;
typedef struct GsPipelineCommand GsPipelineCommand;
typedef struct GsTextureCommand GsTextureCommand;
typedef struct GsUseBufferCommand GsUseBufferCommand;
typedef struct GsDrawArraysCommand GsDrawArraysCommand;
typedef struct GsDrawIndexedCommand GsDrawIndexedCommand;
typedef struct GsScissorCommand GsScissorCommand;
typedef struct GsUniformIntCommand GsUniformIntCommand;
typedef struct GsUniformFloatCommand GsUniformFloatCommand;
typedef struct GsUniformVec2Command GsUniformVec2Command;
typedef struct GsUniformVec3Command GsUniformVec3Command;
typedef struct GsUniformVec4Command GsUniformVec4Command;
typedef struct GsUniformMat4Command GsUniformMat4Command;
typedef struct GsConfig {
// config
GsBackend *backend;
void *window;
// state
GsCommandList *command_lists[GS_MAX_COMMAND_SUBMISSIONS];
int command_list_count;
} GsConfig;
typedef struct GsBackend {
GsBackendType type;
GsCapability capabilities;
// core
GS_BOOL (*init)(GsBackend *backend, GsConfig *config);
void (*shutdown)(GsBackend *backend);
void (*submit)(GsBackend *backend, GsCommandList *list);
// buffer
void (*create_buffer_handle)(GsBuffer *buffer);
void (*set_buffer_data)(GsBuffer *buffer, void *data, int size);
void (*set_buffer_partial_data)(GsBuffer *buffer, void *data, int size, int offset);
void (*destroy_buffer_handle)(GsBuffer *buffer);
// shader
void (*create_shader_handle)(GsShader *shader, const char *source);
void (*destroy_shader_handle)(GsShader *shader);
// program,
void (*create_program_handle)(GsProgram *program);
GsUniformLocation (*get_uniform_location)(GsProgram *program, const char *name);
void (*destroy_program_handle)(GsProgram *program);
// layout
void (*create_layout_handle)(GsVtxLayout *layout);
void (*destroy_layout_handle)(GsVtxLayout *layout);
// texture
void (*create_texture_handle)(GsTexture *texture);
void (*set_texture_data)(GsTexture *texture, GsCubemapFace face, void *data);
void (*generate_mipmaps)(GsTexture *texture);
void (*destroy_texture_handle)(GsTexture *texture);
} GsBackend;
typedef struct GsVtxLayoutItem {
int index;
int offset;
int size_total; // size_per_item * components
int size_per_item; // sizeof(type)
int components;
GsVtxAttribType type;
GS_BOOL normalized;
} GsVtxLayoutItem;
typedef struct GsVtxLayout {
GsVtxLayoutItem items[GS_MAX_VERTEX_LAYOUT_ITEMS];
int count;
int components;
int stride;
void *handle;
GS_BOOL completed;
} GsVtxLayout;
typedef struct GsCommandListItem {
void *data;
int size;
GsCommandType type;
} GsCommandListItem;
typedef struct GsCommandList {
GsCommandListItem items[GS_MAX_COMMAND_LIST_ITEMS];
GsPipeline *pipeline;
int count;
} GsCommandList;
typedef struct GsPipeline {
GsVtxLayout *layout;
GsProgram *program;
GsBlendFactor blend_src;
GsBlendFactor blend_dst;
GsBlendOp blend_op;
GS_BOOL blend_enabled;
int msaa_samples;
} GsPipeline;
typedef struct GsBuffer {
GsBufferType type;
GsBufferIntent intent;
void *handle;
} GsBuffer;
typedef struct GsUnmanagedBufferData {
void *data;
int size;
} GsUnmanagedBufferData;
typedef struct GsClearCommand {
float r;
float g;
float b;
float a;
GsClearFlags flags;
} GsClearCommand;
typedef struct GsViewportCommand {
int x;
int y;
int width;
int height;
} GsViewportCommand;
typedef struct GsUniformIntCommand {
GsUniformLocation location;
int value;
} GsUniformIntCommand;
typedef struct GsUniformFloatCommand {
GsUniformLocation location;
float value;
} GsUniformFloatCommand;
typedef struct GsUniformVec2Command {
GsUniformLocation location;
float x;
float y;
} GsUniformVec2Command;
typedef struct GsUniformVec3Command {
GsUniformLocation location;
float x;
float y;
float z;
} GsUniformVec3Command;
typedef struct GsUniformVec4Command {
GsUniformLocation location;
float x;
float y;
float z;
float w;
} GsUniformVec4Command;
typedef struct GsUniformMat4Command {
GsUniformLocation location;
float m00;
float m01;
float m02;
float m03;
float m10;
float m11;
float m12;
float m13;
float m20;
float m21;
float m22;
float m23;
float m30;
float m31;
float m32;
float m33;
} GsUniformMat4Command;
typedef struct GsPipelineCommand {
GsPipeline *pipeline;
} GsPipelineCommand;
typedef struct GsTextureCommand {
GsTexture *texture;
int slot;
} GsTextureCommand;
typedef struct GsUseBufferCommand {
GsBuffer *buffer;
} GsUseBufferCommand;
typedef struct GsDrawArraysCommand {
int start;
int count;
} GsDrawArraysCommand;
typedef struct GsDrawIndexedCommand {
int count;
} GsDrawIndexedCommand;
typedef struct GsScissorCommand {
int x;
int y;
int width;
int height;
GS_BOOL enable;
} GsScissorCommand;
typedef struct GsShader {
GsShaderType type;
void *handle;
} GsShader;
typedef struct GsProgram {
GsShader *vertex;
GsShader *fragment;
GS_BOOL completed;
void *handle;
} GsProgram;
typedef struct GsTexture {
int width;
int height;
float lodBias;
GsTextureFormat format;
GsTextureWrap wrap_s;
GsTextureWrap wrap_t;
GsTextureWrap wrap_r; // cubemap
GsTextureFilter min;
GsTextureFilter mag;
GsTextureType type;
void *handle;
} GsTexture;
// Textures
GsTexture *gs_create_texture(int width, int height, GsTextureFormat format, GsTextureWrap wrap_s, GsTextureWrap wrap_t, GsTextureFilter min, GsTextureFilter mag);
GsTexture *gs_create_cubemap(int width, int height, GsTextureFormat format, GsTextureWrap wrap_s, GsTextureWrap wrap_t, GsTextureWrap wrap_r, GsTextureFilter min, GsTextureFilter mag);
void gs_texture_set_data(GsTexture *texture, void *data);
void gs_texture_set_face_data(GsTexture *texture, GsCubemapFace face, void *data);
void gs_texture_generate_mipmaps(GsTexture *texture);
void gs_destroy_texture(GsTexture *texture);
// Shaders
GsShader *gs_create_shader(GsShaderType type, const char *source);
void gs_destroy_shader(GsShader *shader);
// Programs
GsProgram *gs_create_program();
void gs_program_attach_shader(GsProgram *program, GsShader *shader);
void gs_program_build(GsProgram *program);
GsUniformLocation gs_get_uniform_location(GsProgram *program, const char *name);
void gs_destroy_program(GsProgram *program);
// Pipeline
GsPipeline *gs_create_pipeline();
void gs_destroy_pipeline(GsPipeline *pipeline);
void gs_pipeline_set_layout(GsPipeline *pipeline, GsVtxLayout *layout);
// Buffers
GsBuffer *gs_create_buffer(GsBufferType type, GsBufferIntent intent);
void gs_destroy_buffer(GsBuffer *buffer);
void gs_buffer_set_data(GsBuffer *buffer, void *data, int size);
void gs_buffer_set_partial_data(GsBuffer *buffer, void *data, int size, int offset);
GsUnmanagedBufferData *gs_buffer_get_data(GsBuffer *buffer, int offset, int size);
void gs_destroy_unmanaged_buffer_data(GsUnmanagedBufferData *data);
// Command list
GsCommandList *gs_create_command_list();
void gs_command_list_begin(GsCommandList *list);
void gs_command_list_add(GsCommandList *list, GsCommandType type, void *data, int size);
void gs_command_list_clear(GsCommandList *list);
void gs_clear(GsCommandList *list, GsClearFlags flags, float r, float g, float b, float a);
void gs_set_viewport(GsCommandList *list, int x, int y, int width, int height);
void gs_use_pipeline(GsCommandList *list, GsPipeline *pipeline);
void gs_use_buffer(GsCommandList *list, GsBuffer *buffer);
void gs_use_texture(GsCommandList *list, GsTexture *texture, int slot);
void gs_set_scissor(GsCommandList *list, int x, int y, int width, int height);
void gs_disable_scissor(GsCommandList *list);
void gs_draw_arrays(GsCommandList *list, int start, int count);
void gs_draw_indexed(GsCommandList *list, int count);
void gs_uniform_set_int(GsCommandList *list, GsUniformLocation location, int value);
void gs_uniform_set_float(GsCommandList *list, GsUniformLocation location, float value);
void gs_uniform_set_vec2(GsCommandList *list, GsUniformLocation location, float x, float y);
void gs_uniform_set_vec3(GsCommandList *list, GsUniformLocation location, float x, float y, float z);
void gs_uniform_set_vec4(GsCommandList *list, GsUniformLocation location, float x, float y, float z, float w);
void gs_uniform_set_mat4(GsCommandList *list, GsUniformLocation location, float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33);
void gs_command_list_end(GsCommandList *list);
void gs_command_list_submit(GsCommandList *list);
void gs_destroy_command_list(GsCommandList *list);
// Vertex Layout
GS_BOOL gs_layout_add(GsVtxLayout *layout, int index, GsVtxAttribType type, int count);
GsVtxLayout *gs_create_layout();
void gs_destroy_layout(GsVtxLayout *layout);
void gs_layout_build(GsVtxLayout *layout);
// Global
GS_BOOL gs_init(GsConfig *config);
void gs_handle_internal_command(GsCommandListItem item);
void gs_shutdown();
void gs_discard_frame();
void gs_frame();
// Config
void gs_destroy_config(GsConfig *config);
GsConfig *gs_create_config();
// Backend
GsBackend *gs_create_backend(GsBackendType type);
void gs_destroy_backend(GsBackend *backend);
GsBackendType gs_get_optimal_backend_type();
// caps
GS_BOOL gs_has_capability(GsCapability capability);
// optional mainloop wrapper
void gs_create_mainloop(void (*mainloop)());
void gs_stop_mainloop();
#ifdef __cplusplus
}
#endif
#endif //GENESIS_H