Skip to content

Latest commit

 

History

History
642 lines (533 loc) · 18.4 KB

mojoshader_vulkan.c

File metadata and controls

642 lines (533 loc) · 18.4 KB
 
Jul 1, 2020
Jul 1, 2020
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
#if SUPPORT_PROFILE_SPIRV
Jul 2, 2020
Jul 2, 2020
15
#include "vulkan/vulkan.h"
Jul 1, 2020
Jul 1, 2020
16
17
18
19
20
21
22
#define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
#define VULKAN_DEVICE_FUNCTION(ret, func, params) \
typedef ret (VKAPI_CALL *vkfntype_MOJOSHADER_##func) params;
#include "mojoshader_vulkan_vkfuncs.h"
Jul 6, 2020
Jul 6, 2020
23
#define UBO_BUFFER_SIZE 8000000 // 8MB
Jul 1, 2020
Jul 1, 2020
24
25
26
27
28
29
30
31
32
33
34
35
36
// Internal struct defs...
typedef struct MOJOSHADER_vkShader
{
VkShaderModule shaderModule;
const MOJOSHADER_parseData *parseData;
uint32_t refcount;
} MOJOSHADER_vkShader;
typedef struct MOJOSHADER_vkUniformBuffer
{
VkBuffer buffer;
Jul 6, 2020
Jul 6, 2020
37
VkDeviceMemory deviceMemory;
Jul 1, 2020
Jul 1, 2020
38
39
40
VkDeviceSize bufferSize;
VkDeviceSize dynamicOffset;
VkDeviceSize currentBlockSize;
Jul 6, 2020
Jul 6, 2020
41
uint8_t *mapPointer;
Jul 1, 2020
Jul 1, 2020
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
} MOJOSHADER_vkUniformBuffer;
// Error state...
static char error_buffer[1024] = { '\0' };
static void set_error(const char *str)
{
snprintf(error_buffer, sizeof (error_buffer), "%s", str);
} // set_error
static inline void out_of_memory(void)
{
set_error("out of memory");
} // out_of_memory
Jul 6, 2020
Jul 6, 2020
57
// Max entries for each register file type
Jul 1, 2020
Jul 1, 2020
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
#define MAX_REG_FILE_F 8192
#define MAX_REG_FILE_I 2047
#define MAX_REG_FILE_B 2047
typedef struct MOJOSHADER_vkContext
{
VkInstance *instance;
VkPhysicalDevice *physical_device;
VkDevice *logical_device;
PFN_vkGetInstanceProcAddr instance_proc_lookup;
PFN_vkGetDeviceProcAddr device_proc_lookup;
uint32_t graphics_queue_family_index;
uint32_t maxUniformBufferRange;
uint32_t minUniformBufferOffsetAlignment;
int32_t frames_in_flight;
MOJOSHADER_malloc malloc_fn;
MOJOSHADER_free free_fn;
void *malloc_data;
// The constant register files...
// !!! FIXME: Man, it kills me how much memory this takes...
// !!! FIXME: ... make this dynamically allocated on demand.
float vs_reg_file_f[MAX_REG_FILE_F * 4];
int32_t vs_reg_file_i[MAX_REG_FILE_I * 4];
uint8_t vs_reg_file_b[MAX_REG_FILE_B * 4];
float ps_reg_file_f[MAX_REG_FILE_F * 4];
int32_t ps_reg_file_i[MAX_REG_FILE_I * 4];
uint8_t ps_reg_file_b[MAX_REG_FILE_B * 4];
Jul 6, 2020
Jul 6, 2020
89
90
MOJOSHADER_vkUniformBuffer *vertUboBuffer;
MOJOSHADER_vkUniformBuffer *fragUboBuffer;
Jul 1, 2020
Jul 1, 2020
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
MOJOSHADER_vkShader *vertexShader;
MOJOSHADER_vkShader *pixelShader;
#define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
vkfntype_MOJOSHADER_##func func;
#define VULKAN_DEVICE_FUNCTION(ret, func, params) \
vkfntype_MOJOSHADER_##func func;
#include "mojoshader_vulkan_vkfuncs.h"
} MOJOSHADER_vkContext;
static MOJOSHADER_vkContext *ctx = NULL;
static uint8_t find_memory_type(
MOJOSHADER_vkContext *ctx,
uint32_t typeFilter,
VkMemoryPropertyFlags properties,
uint32_t *result
) {
uint32_t i;
VkPhysicalDeviceMemoryProperties memoryProperties;
ctx->vkGetPhysicalDeviceMemoryProperties(*ctx->physical_device, &memoryProperties);
for (i = 0; i < memoryProperties.memoryTypeCount; i++)
{
if ((typeFilter & (1 << i))
&& (memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
{
*result = i;
return 1;
} // if
} // for
return 0;
} // find_memory_type
static uint32_t next_highest_offset_alignment(uint32_t offset)
{
return (
(offset + ctx->minUniformBufferOffsetAlignment - 1) /
ctx->minUniformBufferOffsetAlignment *
ctx->minUniformBufferOffsetAlignment
);
} // next_highest_offset_alignment
Jul 6, 2020
Jul 6, 2020
136
137
138
static MOJOSHADER_vkUniformBuffer *create_ubo(MOJOSHADER_vkContext *ctx)
{
MOJOSHADER_vkUniformBuffer *result = (MOJOSHADER_vkUniformBuffer *) ctx->malloc_fn(
Jul 1, 2020
Jul 1, 2020
139
sizeof(MOJOSHADER_vkUniformBuffer),
Jul 6, 2020
Jul 6, 2020
140
ctx->malloc_data
Jul 1, 2020
Jul 1, 2020
141
142
143
144
145
);
VkBufferCreateInfo bufferCreateInfo =
{
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
};
Jul 6, 2020
Jul 6, 2020
146
147
148
149
150
VkMemoryRequirements memoryRequirements;
VkMemoryAllocateInfo allocateInfo =
{
VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
};
Jul 1, 2020
Jul 1, 2020
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
bufferCreateInfo.flags = 0;
bufferCreateInfo.size = UBO_BUFFER_SIZE;
bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
bufferCreateInfo.queueFamilyIndexCount = 1;
bufferCreateInfo.pQueueFamilyIndices = &ctx->graphics_queue_family_index;
ctx->vkCreateBuffer(
*ctx->logical_device,
&bufferCreateInfo,
NULL,
&result->buffer
);
Jul 6, 2020
Jul 6, 2020
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
ctx->vkGetBufferMemoryRequirements(
*ctx->logical_device,
result->buffer,
&memoryRequirements
);
allocateInfo.allocationSize = UBO_BUFFER_SIZE;
if (!find_memory_type(ctx,
memoryRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&allocateInfo.memoryTypeIndex))
{
set_error("failed to find suitable memory type for UBO memory");
return NULL;
} // if
ctx->vkAllocateMemory(*ctx->logical_device,
&allocateInfo,
NULL,
&result->deviceMemory
);
ctx->vkBindBufferMemory(*ctx->logical_device,
result->buffer,
result->deviceMemory,
0
);
ctx->vkMapMemory(*ctx->logical_device,
result->deviceMemory,
0,
UBO_BUFFER_SIZE,
0,
(void**) &result->mapPointer
);
Jul 1, 2020
Jul 1, 2020
203
204
205
206
207
208
209
210
211
212
213
result->bufferSize = UBO_BUFFER_SIZE;
result->currentBlockSize = 0;
result->dynamicOffset = 0;
return result;
} // create_ubo
static uint32_t uniform_data_size(MOJOSHADER_vkShader *shader)
{
int32_t i;
int32_t buflen = 0;
Jul 6, 2020
Jul 6, 2020
214
const int32_t uniformSize = 16; // Yes, even the bool registers
Jul 1, 2020
Jul 1, 2020
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
for (i = 0; i < shader->parseData->uniform_count; i++)
{
const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
buflen += (arrayCount ? arrayCount : 1) * uniformSize;
} // for
return buflen;
} // uniform_data_size
static VkBuffer get_uniform_buffer(MOJOSHADER_vkShader *shader)
{
if (shader == NULL || shader->parseData->uniform_count == 0)
return VK_NULL_HANDLE;
if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
Jul 6, 2020
Jul 6, 2020
230
return ctx->vertUboBuffer->buffer;
Jul 1, 2020
Jul 1, 2020
231
else
Jul 6, 2020
Jul 6, 2020
232
return ctx->fragUboBuffer->buffer;
Jul 1, 2020
Jul 1, 2020
233
234
235
236
237
238
239
240
} // get_uniform_buffer
static VkDeviceSize get_uniform_offset(MOJOSHADER_vkShader *shader)
{
if (shader == NULL || shader->parseData->uniform_count == 0)
return 0;
if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
Jul 6, 2020
Jul 6, 2020
241
return ctx->vertUboBuffer->dynamicOffset;
Jul 1, 2020
Jul 1, 2020
242
else
Jul 6, 2020
Jul 6, 2020
243
return ctx->fragUboBuffer->dynamicOffset;
Jul 1, 2020
Jul 1, 2020
244
245
246
247
248
249
250
251
} // get_uniform_offset
static VkDeviceSize get_uniform_size(MOJOSHADER_vkShader *shader)
{
if (shader == NULL || shader->parseData->uniform_count == 0)
return 0;
if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
Jul 6, 2020
Jul 6, 2020
252
return ctx->vertUboBuffer->currentBlockSize;
Jul 1, 2020
Jul 1, 2020
253
else
Jul 6, 2020
Jul 6, 2020
254
return ctx->fragUboBuffer->currentBlockSize;
Jul 1, 2020
Jul 1, 2020
255
256
257
258
} // get_uniform_size
static void update_uniform_buffer(MOJOSHADER_vkShader *shader)
{
Jul 6, 2020
Jul 6, 2020
259
int32_t i, j;
Jul 1, 2020
Jul 1, 2020
260
261
int32_t offset;
uint8_t *contents;
Jul 6, 2020
Jul 6, 2020
262
uint32_t *contentsI;
Jul 1, 2020
Jul 1, 2020
263
264
265
266
267
268
269
270
271
272
273
274
float *regF; int *regI; uint8_t *regB;
MOJOSHADER_vkUniformBuffer *ubo;
if (shader == NULL || shader->parseData->uniform_count == 0)
return;
if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
{
regF = ctx->vs_reg_file_f;
regI = ctx->vs_reg_file_i;
regB = ctx->vs_reg_file_b;
Jul 6, 2020
Jul 6, 2020
275
ubo = ctx->vertUboBuffer;
Jul 1, 2020
Jul 1, 2020
276
277
278
279
280
281
282
} // if
else
{
regF = ctx->ps_reg_file_f;
regI = ctx->ps_reg_file_i;
regB = ctx->ps_reg_file_b;
Jul 6, 2020
Jul 6, 2020
283
ubo = ctx->fragUboBuffer;
Jul 1, 2020
Jul 1, 2020
284
285
286
287
288
289
290
291
} // else
ubo->dynamicOffset += ubo->currentBlockSize;
ubo->currentBlockSize = next_highest_offset_alignment(uniform_data_size(shader));
if (ubo->dynamicOffset + ubo->currentBlockSize >= ubo->bufferSize)
{
Jul 6, 2020
Jul 6, 2020
292
set_error("UBO overflow!!");
Jul 1, 2020
Jul 1, 2020
293
294
} // if
Jul 6, 2020
Jul 6, 2020
295
contents = ubo->mapPointer + ubo->dynamicOffset;
Jul 1, 2020
Jul 1, 2020
296
297
298
299
300
301
302
303
304
305
306
307
offset = 0;
for (i = 0; i < shader->parseData->uniform_count; i++)
{
const int32_t index = shader->parseData->uniforms[i].index;
const int32_t arrayCount = shader->parseData->uniforms[i].array_count;
const int32_t size = arrayCount ? arrayCount : 1;
switch (shader->parseData->uniforms[i].type)
{
case MOJOSHADER_UNIFORM_FLOAT:
memcpy(
Jul 6, 2020
Jul 6, 2020
308
contents + offset,
Jul 1, 2020
Jul 1, 2020
309
310
311
312
313
314
315
&regF[4 * index],
size * 16
);
break;
case MOJOSHADER_UNIFORM_INT:
memcpy(
Jul 6, 2020
Jul 6, 2020
316
contents + offset,
Jul 1, 2020
Jul 1, 2020
317
318
319
320
321
322
&regI[4 * index],
size * 16
);
break;
case MOJOSHADER_UNIFORM_BOOL:
Jul 6, 2020
Jul 6, 2020
323
324
325
contentsI = (uint32_t *) (contents + offset);
for (j = 0; j < size; j++)
contentsI[j * 4] = regB[index + j];
Jul 1, 2020
Jul 1, 2020
326
327
328
329
330
331
332
333
334
335
break;
default:
set_error(
"SOMETHING VERY WRONG HAPPENED WHEN UPDATING UNIFORMS"
);
assert(0);
break;
} // switch
Jul 6, 2020
Jul 6, 2020
336
offset += size * 16;
Jul 1, 2020
Jul 1, 2020
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
} // for
} // update_uniform_buffer
static void lookup_entry_points(MOJOSHADER_vkContext *ctx)
{
#define VULKAN_INSTANCE_FUNCTION(ret, func, params) \
ctx->func = (vkfntype_MOJOSHADER_##func) ctx->instance_proc_lookup(*ctx->instance, #func);
#define VULKAN_DEVICE_FUNCTION(ret, func, params) \
ctx->func = (vkfntype_MOJOSHADER_##func) ctx->device_proc_lookup(*ctx->logical_device, #func);
#include "mojoshader_vulkan_vkfuncs.h"
} // lookup_entry_points
static int shader_bytecode_len(MOJOSHADER_vkShader *shader)
{
return shader->parseData->output_len - sizeof(SpirvPatchTable);
} // shader_bytecode_len
static void delete_shader(
VkShaderModule shaderModule
) {
ctx->vkDestroyShaderModule(
*ctx->logical_device,
shaderModule,
NULL
);
} // delete_shader
// Public API
MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(
VkInstance *instance,
VkPhysicalDevice *physical_device,
VkDevice *logical_device,
int frames_in_flight,
PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
PFN_MOJOSHADER_vkGetDeviceProcAddr device_lookup,
unsigned int graphics_queue_family_index,
unsigned int max_uniform_buffer_range,
unsigned int min_uniform_buffer_offset_alignment,
MOJOSHADER_malloc m, MOJOSHADER_free f,
void *malloc_d
) {
MOJOSHADER_vkContext* resultCtx;
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;
resultCtx = (MOJOSHADER_vkContext *) m(sizeof(MOJOSHADER_vkContext), malloc_d);
if (resultCtx == NULL)
{
out_of_memory();
goto init_fail;
Jul 6, 2020
Jul 6, 2020
390
} // if
Jul 1, 2020
Jul 1, 2020
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
memset(resultCtx, '\0', sizeof(MOJOSHADER_vkContext));
resultCtx->malloc_fn = m;
resultCtx->free_fn = f;
resultCtx->malloc_data = malloc_d;
resultCtx->instance = (VkInstance*) instance;
resultCtx->physical_device = (VkPhysicalDevice*) physical_device;
resultCtx->logical_device = (VkDevice*) logical_device;
resultCtx->instance_proc_lookup = (PFN_vkGetInstanceProcAddr) instance_lookup;
resultCtx->device_proc_lookup = (PFN_vkGetDeviceProcAddr) device_lookup;
resultCtx->frames_in_flight = frames_in_flight;
resultCtx->graphics_queue_family_index = graphics_queue_family_index;
resultCtx->maxUniformBufferRange = max_uniform_buffer_range;
resultCtx->minUniformBufferOffsetAlignment = min_uniform_buffer_offset_alignment;
lookup_entry_points(resultCtx);
Jul 6, 2020
Jul 6, 2020
409
410
resultCtx->vertUboBuffer = create_ubo(resultCtx);
resultCtx->fragUboBuffer = create_ubo(resultCtx);
Jul 1, 2020
Jul 1, 2020
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
return resultCtx;
init_fail:
if (resultCtx != NULL)
f(resultCtx, malloc_d);
return NULL;
} // MOJOSHADER_vkCreateContext
void MOJOSHADER_vkMakeContextCurrent(MOJOSHADER_vkContext *_ctx)
{
ctx = _ctx;
} // MOJOSHADER_vkMakeContextCurrent
void MOJOSHADER_vkDestroyContext()
{
Jul 6, 2020
Jul 6, 2020
427
428
429
ctx->vkDestroyBuffer(*ctx->logical_device,
ctx->vertUboBuffer->buffer,
NULL);
Jul 1, 2020
Jul 1, 2020
430
Jul 6, 2020
Jul 6, 2020
431
432
433
ctx->vkDestroyBuffer(*ctx->logical_device,
ctx->fragUboBuffer->buffer,
NULL);
Jul 1, 2020
Jul 1, 2020
434
Jul 6, 2020
Jul 6, 2020
435
436
437
ctx->vkFreeMemory(*ctx->logical_device,
ctx->vertUboBuffer->deviceMemory,
NULL);
Jul 1, 2020
Jul 1, 2020
438
Jul 6, 2020
Jul 6, 2020
439
440
441
ctx->vkFreeMemory(*ctx->logical_device,
ctx->fragUboBuffer->deviceMemory,
NULL);
Jul 1, 2020
Jul 1, 2020
442
Jul 6, 2020
Jul 6, 2020
443
444
ctx->free_fn(ctx->vertUboBuffer, ctx->malloc_data);
ctx->free_fn(ctx->fragUboBuffer, ctx->malloc_data);
Jul 1, 2020
Jul 1, 2020
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
ctx->free_fn(ctx, ctx->malloc_data);
} // MOJOSHADER_vkDestroyContext
MOJOSHADER_vkShader *MOJOSHADER_vkCompileShader(
const char *mainfn,
const unsigned char *tokenbuf,
const unsigned int bufsize,
const MOJOSHADER_swizzle *swiz,
const unsigned int swizcount,
const MOJOSHADER_samplerMap *smap,
const unsigned int smapcount
) {
VkResult result;
VkShaderModuleCreateInfo shaderModuleCreateInfo =
{
VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
};
MOJOSHADER_vkShader *shader;
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(
"spirv", mainfn,
tokenbuf, bufsize,
swiz, swizcount,
smap, smapcount,
ctx->malloc_fn,
ctx->free_fn,
ctx->malloc_data
);
if (pd->error_count > 0)
{
set_error(pd->errors[0].error);
goto compile_shader_fail;
} // if
shader = (MOJOSHADER_vkShader *) ctx->malloc_fn(sizeof(MOJOSHADER_vkShader), ctx->malloc_data);
if (shader == NULL)
{
out_of_memory();
goto compile_shader_fail;
} // if
shader->parseData = pd;
shader->refcount = 1;
shaderModuleCreateInfo.flags = 0;
shaderModuleCreateInfo.codeSize = shader_bytecode_len(shader);
shaderModuleCreateInfo.pCode = (uint32_t*) pd->output;
result = ctx->vkCreateShaderModule(
*ctx->logical_device,
&shaderModuleCreateInfo,
NULL,
&shader->shaderModule
);
if (result != VK_SUCCESS)
{
// FIXME: should display VK error code
set_error("Error when creating VkShaderModule");
goto compile_shader_fail;
} // if
return shader;
compile_shader_fail:
MOJOSHADER_freeParseData(pd);
if (shader != NULL)
{
delete_shader(shader->shaderModule);
ctx->free_fn(shader, ctx->malloc_data);
} // if
return NULL;
} // MOJOSHADER_vkMakeContextCurrent
void MOJOSHADER_vkShaderAddRef(MOJOSHADER_vkShader *shader)
{
if (shader != NULL)
shader->refcount++;
} // MOJOShader_vkShaderAddRef
void MOJOSHADER_vkDeleteShader(MOJOSHADER_vkShader *shader)
{
if (shader != NULL)
{
if (shader->refcount > 1)
shader->refcount--;
else
{
delete_shader(shader->shaderModule);
MOJOSHADER_freeParseData(shader->parseData);
ctx->free_fn(shader, ctx->malloc_data);
} // else
} // if
} // MOJOSHADER_vkDeleteShader
const MOJOSHADER_parseData *MOJOSHADER_vkGetShaderParseData(
MOJOSHADER_vkShader *shader
) {
return (shader != NULL) ? shader->parseData : NULL;
} // MOJOSHADER_vkGetShaderParseData
void MOJOSHADER_vkBindShaders(MOJOSHADER_vkShader *vshader,
MOJOSHADER_vkShader *pshader)
{
Jul 6, 2020
Jul 6, 2020
552
// NOOP if shader is null
Jul 1, 2020
Jul 1, 2020
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
if (vshader != NULL)
ctx->vertexShader = vshader;
if (pshader != NULL)
ctx->pixelShader = pshader;
} // MOJOSHADER_vkBindShaders
void MOJOSHADER_vkGetBoundShaders(MOJOSHADER_vkShader **vshader,
MOJOSHADER_vkShader **pshader)
{
*vshader = ctx->vertexShader;
*pshader = ctx->pixelShader;
} // MOJOSHADER_vkGetBoundShaders
void MOJOSHADER_vkMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
float **psf, int **psi, unsigned char **psb)
{
*vsf = ctx->vs_reg_file_f;
*vsi = ctx->vs_reg_file_i;
*vsb = ctx->vs_reg_file_b;
*psf = ctx->ps_reg_file_f;
*psi = ctx->ps_reg_file_i;
*psb = ctx->ps_reg_file_b;
} // MOJOSHADER_vkMapUniformBufferMemory
void MOJOSHADER_vkUnmapUniformBufferMemory()
{
/* Why is this function named unmap instead of update?
* the world may never know...
*/
update_uniform_buffer(ctx->vertexShader);
update_uniform_buffer(ctx->pixelShader);
} // MOJOSHADER_vkUnmapUniformBufferMemory
void MOJOSHADER_vkGetUniformBuffers(VkBuffer *vbuf, unsigned long long *voff, unsigned long long *vsize,
VkBuffer *pbuf, unsigned long long *poff, unsigned long long *psize)
{
*vbuf = get_uniform_buffer(ctx->vertexShader);
*voff = get_uniform_offset(ctx->vertexShader);
*vsize = get_uniform_size(ctx->vertexShader);
*pbuf = get_uniform_buffer(ctx->pixelShader);
*poff = get_uniform_offset(ctx->pixelShader);
*psize = get_uniform_size(ctx->pixelShader);
} // MOJOSHADER_vkGetUniformBuffers
void MOJOSHADER_vkEndFrame()
{
Jul 6, 2020
Jul 6, 2020
601
602
603
604
ctx->vertUboBuffer->dynamicOffset = 0;
ctx->vertUboBuffer->currentBlockSize = 0;
ctx->fragUboBuffer->dynamicOffset = 0;
ctx->fragUboBuffer->currentBlockSize = 0;
Jul 1, 2020
Jul 1, 2020
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
} // MOJOSHADER_VkEndFrame
int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
MOJOSHADER_usage usage, int index)
{
int32_t i;
if (vert == NULL)
return -1;
for (i = 0; i < vert->parseData->attribute_count; i++)
{
if (vert->parseData->attributes[i].usage == usage &&
vert->parseData->attributes[i].index == index)
{
return i;
} // if
} // for
// failure
return -1;
} //MOJOSHADER_vkGetVertexAttribLocation
unsigned long long MOJOSHADER_vkGetShaderModule(MOJOSHADER_vkShader *shader)
{
if (shader == NULL)
return 0;
return (unsigned long long) shader->shaderModule;
} //MOJOSHADER_vkGetShaderModule
const char *MOJOSHADER_vkGetError(void)
{
return error_buffer;
} // MOJOSHADER_vkGetError
#endif /* SUPPORT_PROFILE_SPIRV */
// end of mojoshader_vulkan.c ...