Skip to content

Latest commit

 

History

History
688 lines (586 loc) · 20.9 KB

mojoshader_metal.c

File metadata and controls

688 lines (586 loc) · 20.9 KB
 
Jan 12, 2020
Jan 12, 2020
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 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.
*/
#if (defined(__APPLE__) && defined(__MACH__))
#define PLATFORM_APPLE 1
#include "TargetConditionals.h"
#include <objc/message.h>
Apr 14, 2020
Apr 14, 2020
14
15
16
17
18
#define msg ((void* (*)(void*, void*))objc_msgSend)
#define msg_s ((void* (*)(void*, void*, const char*))objc_msgSend)
#define msg_p ((void* (*)(void*, void*, void*))objc_msgSend)
#define msg_ip ((void* (*)(void*, void*, int, void*))objc_msgSend)
#define msg_ppp ((void* (*)(void*, void*, void*, void*, void*))objc_msgSend)
Jan 12, 2020
Jan 12, 2020
19
20
21
22
23
24
25
26
27
28
#endif /* (defined(__APPLE__) && defined(__MACH__)) */
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
typedef struct MOJOSHADER_mtlUniformBuffer MOJOSHADER_mtlUniformBuffer;
typedef struct MOJOSHADER_mtlShader
{
const MOJOSHADER_parseData *parseData;
MOJOSHADER_mtlUniformBuffer *ubo;
Apr 24, 2020
Apr 24, 2020
29
uint32 refcount;
Jan 12, 2020
Jan 12, 2020
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
void *library; // MTLLibrary*
} MOJOSHADER_mtlShader;
// 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
// profile-specific implementations...
#if SUPPORT_PROFILE_METAL && PLATFORM_APPLE
#ifdef MOJOSHADER_EFFECT_SUPPORT
/* Structs */
typedef struct MOJOSHADER_mtlUniformBuffer
{
int bufferSize;
void **internalBuffers; // MTLBuffer*
int internalBufferSize;
int internalOffset;
int currentFrame;
Apr 24, 2020
Apr 24, 2020
60
int inUse;
Jan 12, 2020
Jan 12, 2020
61
62
63
64
65
66
67
} MOJOSHADER_mtlUniformBuffer;
// Max entries for each register file type...
#define MAX_REG_FILE_F 8192
#define MAX_REG_FILE_I 2047
#define MAX_REG_FILE_B 2047
Apr 24, 2020
Apr 24, 2020
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
typedef struct MOJOSHADER_mtlContext
{
// Allocators...
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];
int vs_reg_file_i[MAX_REG_FILE_I * 4];
uint8 vs_reg_file_b[MAX_REG_FILE_B];
float ps_reg_file_f[MAX_REG_FILE_F * 4];
int ps_reg_file_i[MAX_REG_FILE_I * 4];
uint8 ps_reg_file_b[MAX_REG_FILE_B];
// Pointer to the active MTLDevice.
void* device;
// The maximum number of frames in flight.
int framesInFlight;
// Array of UBOs that are being used in the current frame.
MOJOSHADER_mtlUniformBuffer **buffersInUse;
// The current capacity of the uniform buffer array.
int bufferArrayCapacity;
// The actual number of UBOs used in the current frame.
int numBuffersInUse;
// The currently bound shaders.
MOJOSHADER_mtlShader *vertexShader;
MOJOSHADER_mtlShader *pixelShader;
// Objective-C Selectors
void* classNSString;
void* selAlloc;
void* selInitWithUTF8String;
void* selUTF8String;
void* selLength;
void* selContents;
void* selNewBufferWithLength;
void* selRelease;
void* selNewLibraryWithSource;
void* selLocalizedDescription;
void* selNewFunctionWithName;
void* selRetain;
} MOJOSHADER_mtlContext;
static MOJOSHADER_mtlContext *ctx = NULL;
Jan 12, 2020
Jan 12, 2020
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Uniform buffer utilities */
static inline int next_highest_alignment(int n)
{
#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_SIMULATOR
int align = 16;
#else
int align = 256;
#endif
return align * ((n + align - 1) / align);
} // next_highest_alignment
Apr 24, 2020
Apr 24, 2020
134
135
static void* create_ubo_backing_buffer(MOJOSHADER_mtlUniformBuffer *ubo,
int frame)
Jan 12, 2020
Jan 12, 2020
136
{
Apr 24, 2020
Apr 24, 2020
137
void *oldBuffer = ubo->internalBuffers[frame];
Apr 14, 2020
Apr 14, 2020
138
void *newBuffer = msg_ip(
Apr 24, 2020
Apr 24, 2020
139
140
ctx->device,
ctx->selNewBufferWithLength,
Jan 12, 2020
Jan 12, 2020
141
142
143
144
145
146
147
ubo->internalBufferSize,
NULL
);
if (oldBuffer != NULL)
{
// Copy over data from old buffer
memcpy(
Apr 24, 2020
Apr 24, 2020
148
149
150
msg(newBuffer, ctx->selContents),
msg(oldBuffer, ctx->selContents),
(int) msg(oldBuffer, ctx->selLength)
Jan 12, 2020
Jan 12, 2020
151
152
153
);
// Free the old buffer
Apr 24, 2020
Apr 24, 2020
154
msg(oldBuffer, ctx->selRelease);
Jan 12, 2020
Jan 12, 2020
155
156
157
} //if
return newBuffer;
Apr 24, 2020
Apr 24, 2020
158
} // create_ubo_backing_buffer
Jan 12, 2020
Jan 12, 2020
159
Apr 24, 2020
Apr 24, 2020
160
static void predraw_ubo(MOJOSHADER_mtlUniformBuffer *ubo)
Jan 12, 2020
Jan 12, 2020
161
{
Apr 24, 2020
Apr 24, 2020
162
if (!ubo->inUse)
Jan 12, 2020
Jan 12, 2020
163
{
Apr 24, 2020
Apr 24, 2020
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
ubo->inUse = 1;
ctx->buffersInUse[ctx->numBuffersInUse++] = ubo;
// Double the array size if we run out of room
if (ctx->numBuffersInUse >= ctx->bufferArrayCapacity)
{
int oldlen = ctx->bufferArrayCapacity;
ctx->bufferArrayCapacity *= 2;
MOJOSHADER_mtlUniformBuffer **tmp;
tmp = (MOJOSHADER_mtlUniformBuffer**) ctx->malloc_fn(
ctx->bufferArrayCapacity * sizeof(MOJOSHADER_mtlUniformBuffer *),
ctx->malloc_data
);
memcpy(tmp, ctx->buffersInUse, oldlen * sizeof(MOJOSHADER_mtlUniformBuffer *));
ctx->free_fn(ctx->buffersInUse, ctx->malloc_data);
ctx->buffersInUse = tmp;
}
Jan 12, 2020
Jan 12, 2020
181
182
183
184
185
return;
} // if
ubo->internalOffset += ubo->bufferSize;
Apr 24, 2020
Apr 24, 2020
186
187
188
189
int buflen = (int) msg(
ubo->internalBuffers[ubo->currentFrame],
ctx->selLength
);
Jan 12, 2020
Jan 12, 2020
190
191
192
193
194
195
196
if (ubo->internalOffset >= buflen)
{
// Double capacity when we're out of room
if (ubo->internalOffset >= ubo->internalBufferSize)
ubo->internalBufferSize *= 2;
ubo->internalBuffers[ubo->currentFrame] =
Apr 24, 2020
Apr 24, 2020
197
create_ubo_backing_buffer(ubo, ubo->currentFrame);
Jan 12, 2020
Jan 12, 2020
198
} //if
Apr 24, 2020
Apr 24, 2020
199
} // predraw_ubo
Jan 12, 2020
Jan 12, 2020
200
Apr 24, 2020
Apr 24, 2020
201
202
static MOJOSHADER_mtlUniformBuffer* create_ubo(MOJOSHADER_mtlShader *shader,
MOJOSHADER_malloc m, void* d)
Jan 12, 2020
Jan 12, 2020
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{
int uniformCount = shader->parseData->uniform_count;
if (uniformCount == 0)
return NULL;
// Calculate how big we need to make the buffer
int buflen = 0;
for (int i = 0; i < uniformCount; i += 1)
{
int arrayCount = shader->parseData->uniforms[i].array_count;
int uniformSize = 16;
if (shader->parseData->uniforms[i].type == MOJOSHADER_UNIFORM_BOOL)
uniformSize = 1;
buflen += (arrayCount ? arrayCount : 1) * uniformSize;
} // for
Apr 24, 2020
Apr 24, 2020
219
220
221
222
223
224
225
226
227
228
229
230
// Allocate the UBO
MOJOSHADER_mtlUniformBuffer *retval;
retval = (MOJOSHADER_mtlUniformBuffer *) m(sizeof(MOJOSHADER_mtlUniformBuffer), d);
retval->bufferSize = next_highest_alignment(buflen);
retval->internalBufferSize = retval->bufferSize * 16; // pre-allocate some extra room!
retval->internalBuffers = m(ctx->framesInFlight * sizeof(void*), d);
retval->internalOffset = 0;
retval->inUse = 0;
retval->currentFrame = 0;
// Create the backing buffers
for (int i = 0; i < ctx->framesInFlight; i++)
Jan 12, 2020
Jan 12, 2020
231
{
Apr 24, 2020
Apr 24, 2020
232
233
retval->internalBuffers[i] = NULL; // basically a memset('\0')
retval->internalBuffers[i] = create_ubo_backing_buffer(retval, i);
Jan 12, 2020
Jan 12, 2020
234
235
} // for
Apr 24, 2020
Apr 24, 2020
236
return retval;
Jan 12, 2020
Jan 12, 2020
237
238
239
240
241
242
243
244
245
} // create_ubo
static void dealloc_ubo(MOJOSHADER_mtlShader *shader,
MOJOSHADER_free f,
void* d)
{
if (shader->ubo == NULL)
return;
Apr 24, 2020
Apr 24, 2020
246
for (int i = 0; i < ctx->framesInFlight; i++)
Jan 12, 2020
Jan 12, 2020
247
{
Apr 24, 2020
Apr 24, 2020
248
msg(shader->ubo->internalBuffers[i], ctx->selRelease);
Jan 12, 2020
Jan 12, 2020
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
shader->ubo->internalBuffers[i] = NULL;
} // for
f(shader->ubo->internalBuffers, d);
f(shader->ubo, d);
} // dealloc_ubo
static void *get_uniform_buffer(MOJOSHADER_mtlShader *shader)
{
if (shader == NULL || shader->ubo == NULL)
return NULL;
return shader->ubo->internalBuffers[shader->ubo->currentFrame];
} // get_uniform_buffer
static int get_uniform_offset(MOJOSHADER_mtlShader *shader)
{
if (shader == NULL || shader->ubo == NULL)
return 0;
return shader->ubo->internalOffset;
} // get_uniform_offset
static void update_uniform_buffer(MOJOSHADER_mtlShader *shader)
{
if (shader == NULL || shader->ubo == NULL)
return;
float *regF; int *regI; uint8 *regB;
if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX)
{
Apr 24, 2020
Apr 24, 2020
280
281
282
regF = ctx->vs_reg_file_f;
regI = ctx->vs_reg_file_i;
regB = ctx->vs_reg_file_b;
Jan 12, 2020
Jan 12, 2020
283
284
285
} // if
else
{
Apr 24, 2020
Apr 24, 2020
286
287
288
regF = ctx->ps_reg_file_f;
regI = ctx->ps_reg_file_i;
regB = ctx->ps_reg_file_b;
Jan 12, 2020
Jan 12, 2020
289
290
} // else
Apr 24, 2020
Apr 24, 2020
291
predraw_ubo(shader->ubo);
Jan 12, 2020
Jan 12, 2020
292
void *buf = shader->ubo->internalBuffers[shader->ubo->currentFrame];
Apr 24, 2020
Apr 24, 2020
293
void *contents = msg(buf, ctx->selContents) + shader->ubo->internalOffset;
Jan 12, 2020
Jan 12, 2020
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
int offset = 0;
for (int i = 0; i < shader->parseData->uniform_count; i++)
{
int idx = shader->parseData->uniforms[i].index;
int arrayCount = shader->parseData->uniforms[i].array_count;
int size = arrayCount ? arrayCount : 1;
switch (shader->parseData->uniforms[i].type)
{
case MOJOSHADER_UNIFORM_FLOAT:
memcpy(
contents + (offset * 16),
&regF[4 * idx],
size * 16
);
break;
case MOJOSHADER_UNIFORM_INT:
// !!! FIXME: Need a test case
memcpy(
contents + (offset * 16),
&regI[4 * idx],
size * 16
);
break;
case MOJOSHADER_UNIFORM_BOOL:
// !!! FIXME: Need a test case
memcpy(
contents + offset,
&regB[idx],
size
);
break;
default:
assert(0); // This should never happen.
break;
} // switch
offset += size;
} // for
} // update_uniform_buffer
/* Public API */
Apr 24, 2020
Apr 24, 2020
341
342
343
int MOJOSHADER_mtlCreateContext(void* mtlDevice, int framesInFlight,
MOJOSHADER_malloc m, MOJOSHADER_free f,
void *malloc_d)
Jan 12, 2020
Jan 12, 2020
344
{
Apr 24, 2020
Apr 24, 2020
345
346
347
348
349
350
351
assert(ctx == NULL);
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;
ctx = (MOJOSHADER_mtlContext *) m(sizeof(MOJOSHADER_mtlContext), malloc_d);
if (ctx == NULL)
Jan 12, 2020
Jan 12, 2020
352
353
{
out_of_memory();
Apr 24, 2020
Apr 24, 2020
354
goto init_fail;
Jan 12, 2020
Jan 12, 2020
355
} // if
Apr 24, 2020
Apr 24, 2020
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
memset(ctx, '\0', sizeof (MOJOSHADER_mtlContext));
ctx->malloc_fn = m;
ctx->free_fn = f;
ctx->malloc_data = malloc_d;
// Initialize the Metal state
ctx->device = mtlDevice;
ctx->framesInFlight = framesInFlight;
// Allocate the uniform buffer object array
ctx->bufferArrayCapacity = 32; // arbitrary!
ctx->buffersInUse = ctx->malloc_fn(
ctx->bufferArrayCapacity * sizeof(MOJOSHADER_mtlUniformBuffer *),
ctx->malloc_data
);
// Grab references to Objective-C selectors
ctx->classNSString = objc_getClass("NSString");
ctx->selAlloc = sel_registerName("alloc");
ctx->selInitWithUTF8String = sel_registerName("initWithUTF8String:");
ctx->selUTF8String = sel_registerName("UTF8String");
ctx->selLength = sel_registerName("length");
ctx->selContents = sel_registerName("contents");
ctx->selNewBufferWithLength = sel_registerName("newBufferWithLength:options:");
ctx->selRelease = sel_registerName("release");
ctx->selNewLibraryWithSource = sel_registerName("newLibraryWithSource:options:error:");
ctx->selLocalizedDescription = sel_registerName("localizedDescription");
ctx->selNewFunctionWithName = sel_registerName("newFunctionWithName:");
ctx->selRetain = sel_registerName("retain");
return 0;
init_fail:
if (ctx != NULL)
f(ctx, malloc_d);
return -1;
} // MOJOSHADER_mtlCreateContext
void MOJOSHADER_mtlDestroyContext(void)
{
ctx->free_fn(ctx->buffersInUse, ctx->malloc_data);
ctx->free_fn(ctx, ctx->malloc_data);
May 2, 2020
May 2, 2020
399
ctx = NULL;
Apr 24, 2020
Apr 24, 2020
400
401
402
403
404
405
406
407
408
409
410
411
412
413
} // MOJOSHADER_mtlDestroyContext
void *MOJOSHADER_mtlCompileLibrary(MOJOSHADER_effect *effect)
{
MOJOSHADER_malloc m = ctx->malloc_fn;
MOJOSHADER_free f = ctx->free_fn;
void *d = ctx->malloc_data;
int i, src_len, src_pos, output_len;
char *shader_source, *ptr;
const char *repl;
MOJOSHADER_effectObject *object;
MOJOSHADER_mtlShader *shader;
void *retval, *compileError, *shader_source_ns;
Jan 12, 2020
Jan 12, 2020
414
415
// Count the number of shaders before allocating
Apr 24, 2020
Apr 24, 2020
416
src_len = 0;
Jan 12, 2020
Jan 12, 2020
417
418
for (i = 0; i < effect->object_count; i++)
{
Apr 24, 2020
Apr 24, 2020
419
object = &effect->objects[i];
Jan 12, 2020
Jan 12, 2020
420
421
422
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
{
Apr 24, 2020
Apr 24, 2020
423
if (!object->shader.is_preshader)
Jan 12, 2020
Jan 12, 2020
424
{
Apr 24, 2020
Apr 24, 2020
425
426
427
shader = (MOJOSHADER_mtlShader*) object->shader.shader;
src_len += shader->parseData->output_len;
} // if
Jan 12, 2020
Jan 12, 2020
428
429
430
} // if
} // for
Apr 24, 2020
Apr 24, 2020
431
432
// Allocate shader source buffer
shader_source = (char *) m(src_len + 1, d);
Jan 12, 2020
Jan 12, 2020
433
memset(shader_source, '\0', src_len + 1);
Apr 24, 2020
Apr 24, 2020
434
src_pos = 0;
Jan 12, 2020
Jan 12, 2020
435
436
437
438
// Copy all the source text into the buffer
for (i = 0; i < effect->object_count; i++)
{
Apr 24, 2020
Apr 24, 2020
439
object = &effect->objects[i];
Jan 12, 2020
Jan 12, 2020
440
441
442
443
444
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
{
if (!object->shader.is_preshader)
{
Apr 24, 2020
Apr 24, 2020
445
446
447
448
shader = (MOJOSHADER_mtlShader*) object->shader.shader;
memcpy(&shader_source[src_pos], shader->parseData->output,
shader->parseData->output_len);
src_pos += shader->parseData->output_len;
Jan 12, 2020
Jan 12, 2020
449
450
451
452
453
454
455
456
457
} // if
} // if
} // for
// Handle texcoord0 -> point_coord conversion
if (strstr(shader_source, "[[point_size]]"))
{
// !!! FIXME: This assumes all texcoord0 attributes in the effect are
// !!! FIXME: actually point coords! It ain't necessarily so! -caleb
Apr 24, 2020
Apr 24, 2020
458
repl = "[[ point_coord ]]";
Jan 12, 2020
Jan 12, 2020
459
460
461
462
while ((ptr = strstr(shader_source, "[[user(texcoord0)]]")))
{
memcpy(ptr, repl, strlen(repl));
Apr 24, 2020
Apr 24, 2020
463
// "float4" -> "float2"
Jan 12, 2020
Jan 12, 2020
464
465
466
467
468
469
470
471
472
int spaces = 0;
while (spaces < 2)
if (*(ptr--) == ' ')
spaces++;
memcpy(ptr, "2", sizeof(char));
} // while
} // if
// Compile the source into a library
Apr 24, 2020
Apr 24, 2020
473
474
475
476
477
compileError = NULL;
shader_source_ns = msg_s(
msg(ctx->classNSString, ctx->selAlloc),
ctx->selInitWithUTF8String,
shader_source
Jan 12, 2020
Jan 12, 2020
478
);
Apr 24, 2020
Apr 24, 2020
479
480
retval = msg_ppp(ctx->device, ctx->selNewLibraryWithSource,
shader_source_ns, NULL, &compileError);
Jan 12, 2020
Jan 12, 2020
481
f(shader_source, d);
Apr 24, 2020
Apr 24, 2020
482
msg(shader_source_ns, ctx->selRelease);
Jan 12, 2020
Jan 12, 2020
483
Apr 24, 2020
Apr 24, 2020
484
if (retval == NULL)
Jan 12, 2020
Jan 12, 2020
485
{
Apr 24, 2020
Apr 24, 2020
486
487
488
compileError = msg(compileError, ctx->selLocalizedDescription);
set_error((char*) msg(compileError, ctx->selUTF8String));
return NULL;
Jan 12, 2020
Jan 12, 2020
489
490
} // if
Apr 24, 2020
Apr 24, 2020
491
// Run through the shaders again, setting the library reference
Jan 12, 2020
Jan 12, 2020
492
493
for (i = 0; i < effect->object_count; i++)
{
Apr 24, 2020
Apr 24, 2020
494
object = &effect->objects[i];
Jan 12, 2020
Jan 12, 2020
495
496
497
498
499
500
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
{
if (object->shader.is_preshader)
continue;
Apr 24, 2020
Apr 24, 2020
501
((MOJOSHADER_mtlShader*) object->shader.shader)->library = retval;
Jan 12, 2020
Jan 12, 2020
502
503
504
505
} // if
} // for
return retval;
Apr 24, 2020
Apr 24, 2020
506
} // MOJOSHADER_mtlCompileLibrary
Jan 12, 2020
Jan 12, 2020
507
Apr 24, 2020
Apr 24, 2020
508
void MOJOSHADER_mtlDeleteLibrary(void *library)
Jan 12, 2020
Jan 12, 2020
509
{
Apr 24, 2020
Apr 24, 2020
510
511
msg(library, ctx->selRelease);
} // MOJOSHADER_mtlDeleteLibrary
Jan 12, 2020
Jan 12, 2020
512
Apr 24, 2020
Apr 24, 2020
513
514
515
516
517
518
519
MOJOSHADER_mtlShader *MOJOSHADER_mtlCompileShader(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)
Jan 12, 2020
Jan 12, 2020
520
{
Apr 24, 2020
Apr 24, 2020
521
522
523
MOJOSHADER_malloc m = ctx->malloc_fn;
MOJOSHADER_free f = ctx->free_fn;
void *d = ctx->malloc_data;
Jan 12, 2020
Jan 12, 2020
524
Apr 24, 2020
Apr 24, 2020
525
526
527
528
const MOJOSHADER_parseData *pd = MOJOSHADER_parse("metal", mainfn, tokenbuf,
bufsize, swiz, swizcount,
smap, smapcount, m, f, d);
if (pd->error_count > 0)
Jan 12, 2020
Jan 12, 2020
529
{
Apr 24, 2020
Apr 24, 2020
530
531
532
533
// !!! FIXME: put multiple errors in the buffer? Don't use
// !!! FIXME: MOJOSHADER_mtlGetError() for this?
set_error(pd->errors[0].error);
goto compile_shader_fail;
Jan 12, 2020
Jan 12, 2020
534
535
} // if
Apr 24, 2020
Apr 24, 2020
536
537
538
MOJOSHADER_mtlShader *retval = (MOJOSHADER_mtlShader *) m(sizeof(MOJOSHADER_mtlShader), d);
if (retval == NULL)
goto compile_shader_fail;
Jan 12, 2020
Jan 12, 2020
539
Apr 24, 2020
Apr 24, 2020
540
541
542
543
retval->parseData = pd;
retval->refcount = 1;
retval->ubo = create_ubo(retval, m, d);
retval->library = NULL; // populated by MOJOSHADER_mtlCompileLibrary
Jan 12, 2020
Jan 12, 2020
544
Apr 24, 2020
Apr 24, 2020
545
return retval;
Jan 12, 2020
Jan 12, 2020
546
Apr 24, 2020
Apr 24, 2020
547
548
549
550
551
compile_shader_fail:
MOJOSHADER_freeParseData(retval->parseData);
f(retval, d);
return NULL;
} // MOJOSHADER_mtlCompileShader
Jan 12, 2020
Jan 12, 2020
552
Apr 24, 2020
Apr 24, 2020
553
554
555
556
557
void MOJOSHADER_mtlShaderAddRef(MOJOSHADER_mtlShader *shader)
{
if (shader != NULL)
shader->refcount++;
} // MOJOSHADER_mtlShaderAddRef
Jan 12, 2020
Jan 12, 2020
558
Apr 24, 2020
Apr 24, 2020
559
void MOJOSHADER_mtlDeleteShader(MOJOSHADER_mtlShader *shader)
Jan 12, 2020
Jan 12, 2020
560
{
Apr 24, 2020
Apr 24, 2020
561
if (shader != NULL)
Jan 12, 2020
Jan 12, 2020
562
{
Apr 24, 2020
Apr 24, 2020
563
564
565
if (shader->refcount > 1)
shader->refcount--;
else
Jan 12, 2020
Jan 12, 2020
566
{
Apr 24, 2020
Apr 24, 2020
567
dealloc_ubo(shader, ctx->free_fn, ctx->malloc_data);
Apr 26, 2020
Apr 26, 2020
568
MOJOSHADER_freeParseData(shader->parseData);
Apr 24, 2020
Apr 24, 2020
569
570
ctx->free_fn(shader, ctx->malloc_data);
} // else
Jan 12, 2020
Jan 12, 2020
571
} // if
Apr 24, 2020
Apr 24, 2020
572
} // MOJOSHADER_mtlDeleteShader
Jan 12, 2020
Jan 12, 2020
573
Apr 24, 2020
Apr 24, 2020
574
575
576
577
578
const MOJOSHADER_parseData *MOJOSHADER_mtlGetShaderParseData(
MOJOSHADER_mtlShader *shader)
{
return (shader != NULL) ? shader->parseData : NULL;
} // MOJOSHADER_mtlGetParseData
Jan 12, 2020
Jan 12, 2020
579
Apr 24, 2020
Apr 24, 2020
580
581
582
583
584
585
void MOJOSHADER_mtlBindShaders(MOJOSHADER_mtlShader *vshader,
MOJOSHADER_mtlShader *pshader)
{
// Use the last bound shaders in case of NULL
if (vshader != NULL)
ctx->vertexShader = vshader;
Jan 12, 2020
Jan 12, 2020
586
Apr 24, 2020
Apr 24, 2020
587
588
589
if (pshader != NULL)
ctx->pixelShader = pshader;
} // MOJOSHADER_mtlBindShaders
Jan 12, 2020
Jan 12, 2020
590
Apr 24, 2020
Apr 24, 2020
591
592
void MOJOSHADER_mtlGetBoundShaders(MOJOSHADER_mtlShader **vshader,
MOJOSHADER_mtlShader **pshader)
Jan 12, 2020
Jan 12, 2020
593
{
Apr 24, 2020
Apr 24, 2020
594
595
596
*vshader = ctx->vertexShader;
*pshader = ctx->pixelShader;
} // MOJOSHADER_mtlGetBoundShaders
Jan 12, 2020
Jan 12, 2020
597
Apr 24, 2020
Apr 24, 2020
598
599
600
601
602
603
604
605
606
607
void MOJOSHADER_mtlMapUniformBufferMemory(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_mtlMapUniformBufferMemory
Jan 12, 2020
Jan 12, 2020
608
Apr 24, 2020
Apr 24, 2020
609
void MOJOSHADER_mtlUnmapUniformBufferMemory()
Jan 12, 2020
Jan 12, 2020
610
{
Apr 24, 2020
Apr 24, 2020
611
612
613
614
615
616
617
/* This has nothing to do with unmapping memory
* and everything to do with updating uniform
* buffers with the latest parameter contents.
*/
update_uniform_buffer(ctx->vertexShader);
update_uniform_buffer(ctx->pixelShader);
} // MOJOSHADER_mtlUnmapUniformBufferMemory
Jan 12, 2020
Jan 12, 2020
618
Apr 24, 2020
Apr 24, 2020
619
620
621
622
623
624
625
626
void MOJOSHADER_mtlGetUniformBuffers(void **vbuf, int *voff,
void **pbuf, int *poff)
{
*vbuf = get_uniform_buffer(ctx->vertexShader);
*voff = get_uniform_offset(ctx->vertexShader);
*pbuf = get_uniform_buffer(ctx->pixelShader);
*poff = get_uniform_offset(ctx->pixelShader);
} // MOJOSHADER_mtlGetUniformBuffers
Jan 12, 2020
Jan 12, 2020
627
628
629
630
631
632
void *MOJOSHADER_mtlGetFunctionHandle(MOJOSHADER_mtlShader *shader)
{
if (shader == NULL)
return NULL;
Apr 24, 2020
Apr 24, 2020
633
634
635
636
637
void *fnname = msg_s(
msg(ctx->classNSString, ctx->selAlloc),
ctx->selInitWithUTF8String,
shader->parseData->mainfn
);
Apr 14, 2020
Apr 14, 2020
638
void *ret = msg_p(
Jan 12, 2020
Jan 12, 2020
639
shader->library,
Apr 24, 2020
Apr 24, 2020
640
ctx->selNewFunctionWithName,
Jan 12, 2020
Jan 12, 2020
641
642
fnname
);
Apr 24, 2020
Apr 24, 2020
643
644
msg(fnname, ctx->selRelease);
msg(ret, ctx->selRetain);
Jan 12, 2020
Jan 12, 2020
645
646
647
648
649
650
return ret;
} // MOJOSHADER_mtlGetFunctionHandle
void MOJOSHADER_mtlEndFrame()
{
Apr 24, 2020
Apr 24, 2020
651
for (int i = 0; i < ctx->numBuffersInUse; i += 1)
Jan 12, 2020
Jan 12, 2020
652
{
Apr 24, 2020
Apr 24, 2020
653
654
655
656
657
658
MOJOSHADER_mtlUniformBuffer *buf = ctx->buffersInUse[i];
buf->internalOffset = 0;
buf->currentFrame = (buf->currentFrame + 1) % ctx->framesInFlight;
buf->inUse = 0;
} // for
ctx->numBuffersInUse = 0;
Jan 12, 2020
Jan 12, 2020
659
660
661
662
663
664
665
666
667
668
} // MOJOSHADER_mtlEndFrame
int MOJOSHADER_mtlGetVertexAttribLocation(MOJOSHADER_mtlShader *vert,
MOJOSHADER_usage usage, int index)
{
if (vert == NULL)
return -1;
for (int i = 0; i < vert->parseData->attribute_count; i++)
{
Apr 24, 2020
Apr 24, 2020
669
670
if (vert->parseData->attributes[i].usage == usage &&
vert->parseData->attributes[i].index == index)
Jan 12, 2020
Jan 12, 2020
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
{
return i;
} // if
} // for
// failure, couldn't find requested attribute
return -1;
} // MOJOSHADER_mtlGetVertexAttribLocation
const char *MOJOSHADER_mtlGetError(void)
{
return error_buffer;
} // MOJOSHADER_mtlGetError
#endif /* MOJOSHADER_EFFECT_SUPPORT */
#endif /* SUPPORT_PROFILE_METAL && PLATFORM_APPLE */
// end of mojoshader_metal.c ...