-
Notifications
You must be signed in to change notification settings - Fork 0
/
glemu.h
442 lines (385 loc) · 12.6 KB
/
glemu.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
//NOTE(Ray):Please refer to the readme before inclusion into your project
#if !defined(OPENGLEMU_H)
#define GLEMU_DEBUG 0
#define METALIZER_INSERT_DEBUGSIGNPOST 0
#define METALIZER_DEBUG_OUTPUT 1
//glemu constant defines
#define GLEMU_MAX_ATLAS_PER_SPRITE_BATCH 10
#define GLEMU_MAX_PSO_STATES 4000
#define GLEMU_DEFAULT_TEXTURE_DELETE_COUNT 50
#define CLEAR_COLOR_BIT (1 << 1)
#define CLEAR_STENCIL_BIT (1 << 2)
#define CLEAR_DEPTH_BIT (1 << 3)
//TODO(Ray):The naming of this is a leftover needs more thought
struct MatrixPassInParams
{
RenderCommandEncoder re;
Drawable current_drawable;
float4 viewport;
bool is_s_rect;
ScissorRect s_rect;
SamplerState sampler_state;
RenderPipelineState pipeline_state;
TripleGPUBuffer* vertexbuffer;
};
struct GLProgramKey
{
uint64_t v;
uint64_t f;
};
struct UniformBindingTableEntry
{
uint32_t call_index;
uint32_t v_size;
uint32_t f_size;
void* v_data;
void* f_data;
};
struct TextureBindingTableEntry
{
uint32_t vertex_or_fragment;
uint32_t call_index;
uint32_t size;
void* texture_ptr;
};
enum BufferBindTarget
{
ArrayBuffer = 0,
IndexBuffer = 1
};
struct BufferBindingTableEntry
{
uint32_t index;//The index to bind to on the shader
uint32_t offset;
//GPUBuffer buffer;//the binding id for the buffer in glemu
uint64_t key;
};
struct FragmentShaderTextureBindingTableEntry
{
uint32_t sampler_index;
uint32_t tex_index;
GLTexture texture;
};
enum GLEMUBufferState
{
glemu_bufferstate_none,//defualt0k
glemu_bufferstate_start,//start means we are drawing NOW!1
glemu_bufferstate_viewport_change,//means we are changin viewport size/location2
glemu_bufferstate_blend_change,//3
glemu_bufferstate_scissor_rect_change,//4
glemu_bufferstate_scissor_test_enable,//5
glemu_bufferstate_scissor_test_disable,//if disable default is full viewport settings.//6
glemu_bufferstate_shader_program_change,//7
glemu_bufferstate_bindbuffer,//8
glemu_bufferstate_set_uniforms,//this is a lil different than opengl but lets you pass some data easily without creating an intermediate buffer9
//limited to 4kb< most uniforms are under that amount
// glemu_bufferstate_binduniform_buffer,//Note implmenented because have no need for it yet.
//Depth and stencils
glemu_bufferstate_stencil_enable,//10
glemu_bufferstate_stencil_disable,//11
glemu_bufferstate_stencil_mask,//12
glemu_bufferstate_stencil_mask_sep,//13
glemu_bufferstate_stencil_func,//14
glemu_bufferstate_stencil_func_sep,//15
glemu_bufferstate_stencil_op,//16
glemu_bufferstate_stencil_op_sep,//17
glemu_bufferstate_stencil_func_and_op,//18
glemu_bufferstate_stencil_func_and_op_sep,//19
glemu_bufferstate_clear_start,//20
glemu_bufferstate_clear_end,//21
glemu_bufferstate_clear_stencil_value,//22
glemu_bufferstate_clear_color_value,//23
glemu_bufferstate_clear_color_and_stencil_value,//24
//Debug stuff
glemu_bufferstate_debug_signpost,//25
glemu_bufferstate_draw_arrays,//26
glemu_bufferstate_end//27
};
/*
struct VertexShaderTextureBindingTableEntry
{
uint32_t index;//The index to bind to on the shader
uint32_t offset;
GPUBuffer buffer;//the binding id for the buffer in glemu
BufferBindTarget bind_target;
};
struct IndexBufferBindingTableEntry
{
uint32_t index;//The index to bind to on the shader
uint32_t offset;
GPUBuffer buffer;//the binding id for the buffer in glemu
BufferBindTarget bind_target;
};
*/
//TODO(Ray):Could do multiple of these for multithreading and join after done ...
//perhaps something for another day. For now single thread only.
struct DrawCallTables
{
YoyoVector uniform_binding_table;
YoyoVector texture_binding_table;
YoyoVector buffer_binding_table;
};
struct GLTextureKey
{
//void* api_internal_ptr;
PixelFormat format;
uint32_t width;
uint32_t height;
uint32_t sample_count;
StorageMode storage_mode;
// bool allowGPUOptimizedContents;
uint64_t gl_tex_id;
};
//NOTE(Ray):The number we delete on this will be a deffered deletion scheme such that.
//anytime release is called on a texture it is added to the table and everytime a frame passes the count increases.
//So deletion of textures ON THE GPU are deffered until a safe time. Typically at least one frame later but could be more.
//Also the client can request to check and know without a doubt if the texture has been deleted for sure and if its scheduled
//to be deleted by simply checking the count.
struct ReleasedTextureEntry
{
GLTextureKey tex_key;//Create a tex id from some backing store.
uint32_t delete_count;
uint32_t current_count;
memory_index thread_id;
bool is_free;//TODO(Ray):Implement the meta data free list iterator scheme in AnythingCacheCode to get rid of this bool
};
struct UsedButReleasedEntry
{
uint64_t texture_key;
memory_index thread_id;
};
struct ResourceManagementTables
{
YoyoVector used_but_released_table;
AnythingCache released_textures_table;
};
//Command types
struct GLEMURenderCommandList
{
MemoryArena buffer;
uint32_t count;
};
struct GLEMUCommandHeader
{
GLEMUBufferState type;
uint32_t pad;
};
struct GLEMUBlendCommand
{
BlendFactor sourceRGBBlendFactor;
BlendFactor destinationRGBBlendFactor;
};
struct GLEMUUseProgramCommand
{
GLProgram program;
};
struct GLEMUScissorTestCommand
{
bool is_enable;
};
struct GLEMUScissorRectCommand
{
ScissorRect s_rect;
};
struct GLEMUViewportChangeCommand
{
float4 viewport;
};
struct GLEMUFramebufferStart
{
Texture texture;
};
struct GLEMUFramebufferEnd
{
uint32_t placeholder;
//nothing fo now
};
struct GLEMUStencilStateCommand
{
bool is_enable;
};
struct GLEMUStencilMaskCommand
{
uint32_t write_mask_value;
};
struct GLEMUStencilMaskSepCommand
{
uint32_t front_or_back;
uint32_t write_mask_value;
};
struct GLEMUStencilFunCommand
{
CompareFunc compareFunction;
uint32_t mask_value;
uint32_t write_mask_value;
};
struct GLEMUStencilFunSepCommand
{
uint32_t front_or_back;
CompareFunc compareFunction;
uint32_t mask_value;
uint32_t write_mask_value;
};
struct GLEMUStencilOpCommand
{
StencilOp stencil_fail_op;
StencilOp depth_fail_op;
StencilOp depth_stencil_pass_op;
};
struct GLEMUStencilOpSepCommand
{
uint32_t front_or_back;
StencilOp stencil_fail_op;
StencilOp depth_fail_op;
StencilOp depth_stencil_pass_op;
};
struct GLEMUStencilFuncAndOpCommand
{
CompareFunc compareFunction;
uint32_t write_mask_value;
StencilOp stencil_fail_op;
StencilOp depth_fail_op;
StencilOp depth_stencil_pass_op;
uint32_t mask_value;
};
struct GLEMUStencilFuncAndOpSepCommand
{
uint32_t front_or_back;
CompareFunc compareFunction;
uint32_t write_mask_value;
StencilOp stencil_fail_op;
StencilOp depth_fail_op;
StencilOp depth_stencil_pass_op;
uint32_t mask_value;
};
struct GLEMUClearBufferCommand
{
bool is_start;
uint32_t write_mask_value;
};
struct GLEMUClearStencilCommand
{
uint32_t write_mask_value;
};
struct GLEMUClearColorCommand
{
float4 clear_color;
};
struct GLEMUClearColorAndStencilCommand
{
float4 clear_color;
uint32_t write_mask_value;
};
struct GLEMUAddDebugSignPostCommand
{
char* string;
};
struct GLEMUDrawArraysCommand
{
bool is_from_to;
bool is_primitive_triangles;
uint32_t uniform_table_index;
float2 buffer_range;
float2 texture_buffer_range;
uint32_t current_count;
PrimitiveType primitive_type;
};
namespace OpenGLEmu
{
//Variables that have been exposed or should possiblly exposed are here.
extern uint32_t current_buffer_index;
extern uint32_t buffer_count;
//NOTE(RAY):Temporary way to change the default buffer size of bound buffer
//TODO(Ray):Probably just make this a parameter for CreateBufferAtBinding API. Still on the fence.
extern memory_index default_buffer_size;
void Init();
//CPU BUFFER
void CreateCPUBufferAtBinding(uint64_t bindkey,memory_index size);
void AddCPUBindingToBuffer(uint64_t buffer_key,uint64_t key);
CPUBuffer* GetCPUBufferAtBinding(uint64_t bindkey);
YoyoVector GetCPUBufferList();
//UNIFORM API
UniformBindResult AddUniformDataAtBinding(uint64_t bindkey,void* uniform_data,memory_index size);
BufferOffsetResult GetUniformAtBinding(uint64_t bindkey,uint32_t index);
//TEST API
void APInit();
void Execute(void* pass_in_c_buffer = nullptr);
void PreFrameSetup();
SamplerState GetSamplerStateWithDescriptor(SamplerDescriptor desc);
SamplerDescriptor GetSamplerDescriptor();
SamplerDescriptor GetDefaultDescriptor();
DepthStencilDescription GetDefaultDepthStencilDescriptor();
SamplerState GetDefaultSampler();
GLProgram AddProgramFromSource(const char* v_s,const char* vs_name,const char* f_s,const char* fs_name,VertexDescriptor vd);
GLProgram AddProgramFromMainLibrary(const char* vs_name,const char* fs_name,VertexDescriptor vd);
GLProgram GetDefaultProgram();
void CreateBufferAtBinding(uint64_t bindkey);
void AddBindingToBuffer(uint64_t buffer_key,uint64_t key);
TripleGPUBuffer* GetBufferAtBinding(uint64_t bindkey);
YoyoVector GetBufferList();
YoyoVector GetProgramList();
GLProgram GetProgram(GLProgramKey key);
GLProgram* GetProgramPtr(GLProgramKey key);
void GLBlending(uint64_t gl_src,uint64_t gl_dst);
void UseProgram(GLProgram gl_program);
void EnableScissorTest();
void DisableScissorTest();
void ScissorTest(int x,int y,int width,int height);
void ScissorTest(float4 rect);
void Viewport(int x, int y, int width, int height);
void Viewport(float4 vp);
void BindFrameBufferStart(GLTexture texture);
void BindFrameBufferEnd();
void DrawArrays(uint32_t current_count,uint32_t unit_size,PrimitiveType primitive_type = primitive_type_triangle);
void DrawArrayPrimitives(uint32_t current_count,uint32_t unit_size);
GLTexture TexImage2D(void* texels,float2 dim,PixelFormat format,SamplerDescriptor sd,TextureUsage usage);
GLTexture TexImage2D(void* texels,float2 dim,PixelFormat format,TextureUsage usage);
void AddFragTextureBinding(GLTexture texture,uint32_t index);
void AddFragTextureBinding(GLTexture texture,uint32_t tex_index,uint32_t sampler_index);
void AddBufferBinding(uint64_t bind_key,uint64_t index,uint64_t offset);
#define SetUniformsFragment(Type) (Type*)OpenGLEmu::SetUniformsFragment_(sizeof(Type))
void* SetUniformsFragment_(memory_index size);
#define SetUniformsVertex(Type) (Type*)OpenGLEmu::SetUniformsVertex_(sizeof(Type))
void* SetUniformsVertex_(memory_index size);
UniformBindingTableEntry GetUniEntryForDrawCall(uint32_t index);
//NOTE(Ray):THis is actually worse than what I had before... need to do either binding model or pass in model
SamplerState SamplerParameter(SamplerMinMagFilter min,SamplerMinMagFilter mag,SamplerAddressMode s,SamplerAddressMode t);
//note tested yet
void EnableStencilTest();
void DisableStencilTest();
void StencilMask(uint32_t mask);
void StencilMaskSep(uint32_t front_or_back,uint32_t mask);
void StencilFunc(CompareFunc func,uint32_t ref,uint32_t mask);
void StencilFuncSep(uint32_t front_or_back,CompareFunc func,uint32_t ref,uint32_t mask);
void StencilOperation(StencilOp sten_fail,StencilOp dpfail,StencilOp dppass);
void StencilOperationSep(uint32_t front_or_back,StencilOp sten_fail,StencilOp dpfail,StencilOp dppass);
void StencilFuncAndOp(CompareFunc func,uint32_t ref,uint32_t mask,StencilOp sten_fail,StencilOp dpfail,StencilOp dppass);
void StencilFuncAndOpSep(uint32_t front_or_back,CompareFunc func,uint32_t ref,uint32_t mask,StencilOp sten_fail,StencilOp dpfail,StencilOp dppass);
DepthStencilState GetOrCreateDepthStencilState(DepthStencilDescription desc);
void ClearBuffer(uint32_t buffer_bits);
void ClearStencil(uint32_t value);
void ClearColor(float4 value);
void ClearColorAndStencil(float4 color,uint32_t stencil);
DepthStencilDescription GetCurrentDepthStencilState();
uint32_t GetCurrentStencilReferenceValue();
//Stats
uint32_t GetDepthStencilStateCount();
//Debug
void AddDebugSignPost(char* str);
//Resource Management
void GLDeleteTexture(GLTexture* texture);
bool GLIsValidTexture(GLTexture texture);
};
//NOTE(Ray):Probably a better place fo rthis is in the openglemu file
namespace RenderProgramCache
{
void Init(uint32_t max_hash_states);
bool DoesProgramExist(RenderShader* render_shader);
//Returns key for later retrival
uint64_t AddProgram(RenderShader* shaders,GLProgram* glp);
GLProgram GetProgram(RenderShader* shaders);
GLProgram GetSamplerStateByKey(uint64_t hash_key);
};
#include "glemu.cpp"
#define OPENGLEMU_H
#endif