author | Ethan Lee <flibitijibibo@flibitijibibo.com> |
Tue, 07 Jul 2020 17:19:45 -0400 | |
changeset 1277 | da61410edbc9 |
parent 1252 | ed07dd994125 |
child 1279 | b1f51cfa8933 |
permissions | -rw-r--r-- |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
1 |
/** |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
2 |
* MojoShader; generate shader programs from bytecode of compiled |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
3 |
* Direct3D shaders. |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
4 |
* |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
5 |
* Please see the file LICENSE.txt in the source's root directory. |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
6 |
* |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
7 |
* This file written by Ryan C. Gordon. |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
8 |
*/ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
9 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
10 |
#if (defined(__APPLE__) && defined(__MACH__)) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
11 |
#define PLATFORM_APPLE 1 |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
12 |
#include "TargetConditionals.h" |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
13 |
#include <objc/message.h> |
1241 | 14 |
#define msg ((void* (*)(void*, void*))objc_msgSend) |
15 |
#define msg_s ((void* (*)(void*, void*, const char*))objc_msgSend) |
|
16 |
#define msg_p ((void* (*)(void*, void*, void*))objc_msgSend) |
|
17 |
#define msg_ip ((void* (*)(void*, void*, int, void*))objc_msgSend) |
|
18 |
#define msg_ppp ((void* (*)(void*, void*, void*, void*, void*))objc_msgSend) |
|
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
19 |
#endif /* (defined(__APPLE__) && defined(__MACH__)) */ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
20 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
21 |
#define __MOJOSHADER_INTERNAL__ 1 |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
22 |
#include "mojoshader_internal.h" |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
23 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
24 |
typedef struct MOJOSHADER_mtlUniformBuffer MOJOSHADER_mtlUniformBuffer; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
25 |
typedef struct MOJOSHADER_mtlShader |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
26 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
27 |
const MOJOSHADER_parseData *parseData; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
28 |
MOJOSHADER_mtlUniformBuffer *ubo; |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
29 |
uint32 refcount; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
30 |
void *library; // MTLLibrary* |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
31 |
} MOJOSHADER_mtlShader; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
32 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
33 |
// Error state... |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
34 |
static char error_buffer[1024] = { '\0' }; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
35 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
36 |
static void set_error(const char *str) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
37 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
38 |
snprintf(error_buffer, sizeof (error_buffer), "%s", str); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
39 |
} // set_error |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
40 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
41 |
static inline void out_of_memory(void) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
42 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
43 |
set_error("out of memory"); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
44 |
} // out_of_memory |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
45 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
46 |
// profile-specific implementations... |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
47 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
48 |
#if SUPPORT_PROFILE_METAL && PLATFORM_APPLE |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
49 |
#ifdef MOJOSHADER_EFFECT_SUPPORT |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
50 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
51 |
/* Structs */ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
52 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
53 |
typedef struct MOJOSHADER_mtlUniformBuffer |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
54 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
55 |
int bufferSize; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
56 |
void **internalBuffers; // MTLBuffer* |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
57 |
int internalBufferSize; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
58 |
int internalOffset; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
59 |
int currentFrame; |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
60 |
int inUse; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
61 |
} MOJOSHADER_mtlUniformBuffer; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
62 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
63 |
// Max entries for each register file type... |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
64 |
#define MAX_REG_FILE_F 8192 |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
65 |
#define MAX_REG_FILE_I 2047 |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
66 |
#define MAX_REG_FILE_B 2047 |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
67 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
68 |
typedef struct MOJOSHADER_mtlContext |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
69 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
70 |
// Allocators... |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
71 |
MOJOSHADER_malloc malloc_fn; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
72 |
MOJOSHADER_free free_fn; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
73 |
void *malloc_data; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
74 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
75 |
// The constant register files... |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
76 |
// !!! FIXME: Man, it kills me how much memory this takes... |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
77 |
// !!! FIXME: ... make this dynamically allocated on demand. |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
78 |
float vs_reg_file_f[MAX_REG_FILE_F * 4]; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
79 |
int vs_reg_file_i[MAX_REG_FILE_I * 4]; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
80 |
uint8 vs_reg_file_b[MAX_REG_FILE_B]; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
81 |
float ps_reg_file_f[MAX_REG_FILE_F * 4]; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
82 |
int ps_reg_file_i[MAX_REG_FILE_I * 4]; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
83 |
uint8 ps_reg_file_b[MAX_REG_FILE_B]; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
84 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
85 |
// Pointer to the active MTLDevice. |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
86 |
void* device; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
87 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
88 |
// The maximum number of frames in flight. |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
89 |
int framesInFlight; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
90 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
91 |
// Array of UBOs that are being used in the current frame. |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
92 |
MOJOSHADER_mtlUniformBuffer **buffersInUse; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
93 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
94 |
// The current capacity of the uniform buffer array. |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
95 |
int bufferArrayCapacity; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
96 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
97 |
// The actual number of UBOs used in the current frame. |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
98 |
int numBuffersInUse; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
99 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
100 |
// The currently bound shaders. |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
101 |
MOJOSHADER_mtlShader *vertexShader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
102 |
MOJOSHADER_mtlShader *pixelShader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
103 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
104 |
// Objective-C Selectors |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
105 |
void* classNSString; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
106 |
void* selAlloc; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
107 |
void* selInitWithUTF8String; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
108 |
void* selUTF8String; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
109 |
void* selLength; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
110 |
void* selContents; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
111 |
void* selNewBufferWithLength; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
112 |
void* selRelease; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
113 |
void* selNewLibraryWithSource; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
114 |
void* selLocalizedDescription; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
115 |
void* selNewFunctionWithName; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
116 |
void* selRetain; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
117 |
} MOJOSHADER_mtlContext; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
118 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
119 |
static MOJOSHADER_mtlContext *ctx = NULL; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
120 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
121 |
/* Uniform buffer utilities */ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
122 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
123 |
static inline int next_highest_alignment(int n) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
124 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
125 |
#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_SIMULATOR |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
126 |
int align = 16; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
127 |
#else |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
128 |
int align = 256; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
129 |
#endif |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
130 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
131 |
return align * ((n + align - 1) / align); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
132 |
} // next_highest_alignment |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
133 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
134 |
static void* create_ubo_backing_buffer(MOJOSHADER_mtlUniformBuffer *ubo, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
135 |
int frame) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
136 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
137 |
void *oldBuffer = ubo->internalBuffers[frame]; |
1241 | 138 |
void *newBuffer = msg_ip( |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
139 |
ctx->device, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
140 |
ctx->selNewBufferWithLength, |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
141 |
ubo->internalBufferSize, |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
142 |
NULL |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
143 |
); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
144 |
if (oldBuffer != NULL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
145 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
146 |
// Copy over data from old buffer |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
147 |
memcpy( |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
148 |
msg(newBuffer, ctx->selContents), |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
149 |
msg(oldBuffer, ctx->selContents), |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
150 |
(int) msg(oldBuffer, ctx->selLength) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
151 |
); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
152 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
153 |
// Free the old buffer |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
154 |
msg(oldBuffer, ctx->selRelease); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
155 |
} //if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
156 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
157 |
return newBuffer; |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
158 |
} // create_ubo_backing_buffer |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
159 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
160 |
static void predraw_ubo(MOJOSHADER_mtlUniformBuffer *ubo) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
161 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
162 |
if (!ubo->inUse) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
163 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
164 |
ubo->inUse = 1; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
165 |
ctx->buffersInUse[ctx->numBuffersInUse++] = ubo; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
166 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
167 |
// Double the array size if we run out of room |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
168 |
if (ctx->numBuffersInUse >= ctx->bufferArrayCapacity) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
169 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
170 |
int oldlen = ctx->bufferArrayCapacity; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
171 |
ctx->bufferArrayCapacity *= 2; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
172 |
MOJOSHADER_mtlUniformBuffer **tmp; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
173 |
tmp = (MOJOSHADER_mtlUniformBuffer**) ctx->malloc_fn( |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
174 |
ctx->bufferArrayCapacity * sizeof(MOJOSHADER_mtlUniformBuffer *), |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
175 |
ctx->malloc_data |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
176 |
); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
177 |
memcpy(tmp, ctx->buffersInUse, oldlen * sizeof(MOJOSHADER_mtlUniformBuffer *)); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
178 |
ctx->free_fn(ctx->buffersInUse, ctx->malloc_data); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
179 |
ctx->buffersInUse = tmp; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
180 |
} |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
181 |
return; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
182 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
183 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
184 |
ubo->internalOffset += ubo->bufferSize; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
185 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
186 |
int buflen = (int) msg( |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
187 |
ubo->internalBuffers[ubo->currentFrame], |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
188 |
ctx->selLength |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
189 |
); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
190 |
if (ubo->internalOffset >= buflen) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
191 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
192 |
// Double capacity when we're out of room |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
193 |
if (ubo->internalOffset >= ubo->internalBufferSize) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
194 |
ubo->internalBufferSize *= 2; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
195 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
196 |
ubo->internalBuffers[ubo->currentFrame] = |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
197 |
create_ubo_backing_buffer(ubo, ubo->currentFrame); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
198 |
} //if |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
199 |
} // predraw_ubo |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
200 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
201 |
static MOJOSHADER_mtlUniformBuffer* create_ubo(MOJOSHADER_mtlShader *shader, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
202 |
MOJOSHADER_malloc m, void* d) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
203 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
204 |
int uniformCount = shader->parseData->uniform_count; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
205 |
if (uniformCount == 0) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
206 |
return NULL; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
207 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
208 |
// Calculate how big we need to make the buffer |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
209 |
int buflen = 0; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
210 |
for (int i = 0; i < uniformCount; i += 1) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
211 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
212 |
int arrayCount = shader->parseData->uniforms[i].array_count; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
213 |
int uniformSize = 16; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
214 |
if (shader->parseData->uniforms[i].type == MOJOSHADER_UNIFORM_BOOL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
215 |
uniformSize = 1; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
216 |
buflen += (arrayCount ? arrayCount : 1) * uniformSize; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
217 |
} // for |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
218 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
219 |
// Allocate the UBO |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
220 |
MOJOSHADER_mtlUniformBuffer *retval; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
221 |
retval = (MOJOSHADER_mtlUniformBuffer *) m(sizeof(MOJOSHADER_mtlUniformBuffer), d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
222 |
retval->bufferSize = next_highest_alignment(buflen); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
223 |
retval->internalBufferSize = retval->bufferSize * 16; // pre-allocate some extra room! |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
224 |
retval->internalBuffers = m(ctx->framesInFlight * sizeof(void*), d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
225 |
retval->internalOffset = 0; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
226 |
retval->inUse = 0; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
227 |
retval->currentFrame = 0; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
228 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
229 |
// Create the backing buffers |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
230 |
for (int i = 0; i < ctx->framesInFlight; i++) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
231 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
232 |
retval->internalBuffers[i] = NULL; // basically a memset('\0') |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
233 |
retval->internalBuffers[i] = create_ubo_backing_buffer(retval, i); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
234 |
} // for |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
235 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
236 |
return retval; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
237 |
} // create_ubo |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
238 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
239 |
static void dealloc_ubo(MOJOSHADER_mtlShader *shader, |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
240 |
MOJOSHADER_free f, |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
241 |
void* d) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
242 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
243 |
if (shader->ubo == NULL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
244 |
return; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
245 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
246 |
for (int i = 0; i < ctx->framesInFlight; i++) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
247 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
248 |
msg(shader->ubo->internalBuffers[i], ctx->selRelease); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
249 |
shader->ubo->internalBuffers[i] = NULL; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
250 |
} // for |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
251 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
252 |
f(shader->ubo->internalBuffers, d); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
253 |
f(shader->ubo, d); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
254 |
} // dealloc_ubo |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
255 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
256 |
static void *get_uniform_buffer(MOJOSHADER_mtlShader *shader) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
257 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
258 |
if (shader == NULL || shader->ubo == NULL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
259 |
return NULL; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
260 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
261 |
return shader->ubo->internalBuffers[shader->ubo->currentFrame]; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
262 |
} // get_uniform_buffer |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
263 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
264 |
static int get_uniform_offset(MOJOSHADER_mtlShader *shader) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
265 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
266 |
if (shader == NULL || shader->ubo == NULL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
267 |
return 0; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
268 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
269 |
return shader->ubo->internalOffset; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
270 |
} // get_uniform_offset |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
271 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
272 |
static void update_uniform_buffer(MOJOSHADER_mtlShader *shader) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
273 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
274 |
if (shader == NULL || shader->ubo == NULL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
275 |
return; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
276 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
277 |
float *regF; int *regI; uint8 *regB; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
278 |
if (shader->parseData->shader_type == MOJOSHADER_TYPE_VERTEX) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
279 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
280 |
regF = ctx->vs_reg_file_f; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
281 |
regI = ctx->vs_reg_file_i; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
282 |
regB = ctx->vs_reg_file_b; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
283 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
284 |
else |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
285 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
286 |
regF = ctx->ps_reg_file_f; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
287 |
regI = ctx->ps_reg_file_i; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
288 |
regB = ctx->ps_reg_file_b; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
289 |
} // else |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
290 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
291 |
predraw_ubo(shader->ubo); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
292 |
void *buf = shader->ubo->internalBuffers[shader->ubo->currentFrame]; |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
293 |
void *contents = msg(buf, ctx->selContents) + shader->ubo->internalOffset; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
294 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
295 |
int offset = 0; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
296 |
for (int i = 0; i < shader->parseData->uniform_count; i++) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
297 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
298 |
int idx = shader->parseData->uniforms[i].index; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
299 |
int arrayCount = shader->parseData->uniforms[i].array_count; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
300 |
int size = arrayCount ? arrayCount : 1; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
301 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
302 |
switch (shader->parseData->uniforms[i].type) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
303 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
304 |
case MOJOSHADER_UNIFORM_FLOAT: |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
305 |
memcpy( |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
306 |
contents + (offset * 16), |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
307 |
®F[4 * idx], |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
308 |
size * 16 |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
309 |
); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
310 |
break; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
311 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
312 |
case MOJOSHADER_UNIFORM_INT: |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
313 |
// !!! FIXME: Need a test case |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
314 |
memcpy( |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
315 |
contents + (offset * 16), |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
316 |
®I[4 * idx], |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
317 |
size * 16 |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
318 |
); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
319 |
break; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
320 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
321 |
case MOJOSHADER_UNIFORM_BOOL: |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
322 |
// !!! FIXME: Need a test case |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
323 |
memcpy( |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
324 |
contents + offset, |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
325 |
®B[idx], |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
326 |
size |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
327 |
); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
328 |
break; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
329 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
330 |
default: |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
331 |
assert(0); // This should never happen. |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
332 |
break; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
333 |
} // switch |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
334 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
335 |
offset += size; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
336 |
} // for |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
337 |
} // update_uniform_buffer |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
338 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
339 |
/* Public API */ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
340 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
341 |
int MOJOSHADER_mtlCreateContext(void* mtlDevice, int framesInFlight, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
342 |
MOJOSHADER_malloc m, MOJOSHADER_free f, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
343 |
void *malloc_d) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
344 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
345 |
assert(ctx == NULL); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
346 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
347 |
if (m == NULL) m = MOJOSHADER_internal_malloc; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
348 |
if (f == NULL) f = MOJOSHADER_internal_free; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
349 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
350 |
ctx = (MOJOSHADER_mtlContext *) m(sizeof(MOJOSHADER_mtlContext), malloc_d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
351 |
if (ctx == NULL) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
352 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
353 |
out_of_memory(); |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
354 |
goto init_fail; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
355 |
} // if |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
356 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
357 |
memset(ctx, '\0', sizeof (MOJOSHADER_mtlContext)); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
358 |
ctx->malloc_fn = m; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
359 |
ctx->free_fn = f; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
360 |
ctx->malloc_data = malloc_d; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
361 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
362 |
// Initialize the Metal state |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
363 |
ctx->device = mtlDevice; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
364 |
ctx->framesInFlight = framesInFlight; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
365 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
366 |
// Allocate the uniform buffer object array |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
367 |
ctx->bufferArrayCapacity = 32; // arbitrary! |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
368 |
ctx->buffersInUse = ctx->malloc_fn( |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
369 |
ctx->bufferArrayCapacity * sizeof(MOJOSHADER_mtlUniformBuffer *), |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
370 |
ctx->malloc_data |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
371 |
); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
372 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
373 |
// Grab references to Objective-C selectors |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
374 |
ctx->classNSString = objc_getClass("NSString"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
375 |
ctx->selAlloc = sel_registerName("alloc"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
376 |
ctx->selInitWithUTF8String = sel_registerName("initWithUTF8String:"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
377 |
ctx->selUTF8String = sel_registerName("UTF8String"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
378 |
ctx->selLength = sel_registerName("length"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
379 |
ctx->selContents = sel_registerName("contents"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
380 |
ctx->selNewBufferWithLength = sel_registerName("newBufferWithLength:options:"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
381 |
ctx->selRelease = sel_registerName("release"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
382 |
ctx->selNewLibraryWithSource = sel_registerName("newLibraryWithSource:options:error:"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
383 |
ctx->selLocalizedDescription = sel_registerName("localizedDescription"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
384 |
ctx->selNewFunctionWithName = sel_registerName("newFunctionWithName:"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
385 |
ctx->selRetain = sel_registerName("retain"); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
386 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
387 |
return 0; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
388 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
389 |
init_fail: |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
390 |
if (ctx != NULL) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
391 |
f(ctx, malloc_d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
392 |
return -1; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
393 |
} // MOJOSHADER_mtlCreateContext |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
394 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
395 |
void MOJOSHADER_mtlDestroyContext(void) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
396 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
397 |
ctx->free_fn(ctx->buffersInUse, ctx->malloc_data); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
398 |
ctx->free_fn(ctx, ctx->malloc_data); |
1252
ed07dd994125
metal: Set ctx to NULL after free
Caleb Cornett <caleb.cornett@outlook.com>
parents:
1250
diff
changeset
|
399 |
ctx = NULL; |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
400 |
} // MOJOSHADER_mtlDestroyContext |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
401 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
402 |
void *MOJOSHADER_mtlCompileLibrary(MOJOSHADER_effect *effect) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
403 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
404 |
MOJOSHADER_malloc m = ctx->malloc_fn; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
405 |
MOJOSHADER_free f = ctx->free_fn; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
406 |
void *d = ctx->malloc_data; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
407 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
408 |
int i, src_len, src_pos, output_len; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
409 |
char *shader_source, *ptr; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
410 |
const char *repl; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
411 |
MOJOSHADER_effectObject *object; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
412 |
MOJOSHADER_mtlShader *shader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
413 |
void *retval, *compileError, *shader_source_ns; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
414 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
415 |
// Count the number of shaders before allocating |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
416 |
src_len = 0; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
417 |
for (i = 0; i < effect->object_count; i++) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
418 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
419 |
object = &effect->objects[i]; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
420 |
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
421 |
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
422 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
423 |
if (!object->shader.is_preshader) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
424 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
425 |
shader = (MOJOSHADER_mtlShader*) object->shader.shader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
426 |
src_len += shader->parseData->output_len; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
427 |
} // if |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
428 |
} // if |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
429 |
} // for |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
430 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
431 |
// Allocate shader source buffer |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
432 |
shader_source = (char *) m(src_len + 1, d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
433 |
memset(shader_source, '\0', src_len + 1); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
434 |
src_pos = 0; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
435 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
436 |
// Copy all the source text into the buffer |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
437 |
for (i = 0; i < effect->object_count; i++) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
438 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
439 |
object = &effect->objects[i]; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
440 |
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
441 |
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
442 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
443 |
if (!object->shader.is_preshader) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
444 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
445 |
shader = (MOJOSHADER_mtlShader*) object->shader.shader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
446 |
memcpy(&shader_source[src_pos], shader->parseData->output, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
447 |
shader->parseData->output_len); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
448 |
src_pos += shader->parseData->output_len; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
449 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
450 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
451 |
} // for |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
452 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
453 |
// Handle texcoord0 -> point_coord conversion |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
454 |
if (strstr(shader_source, "[[point_size]]")) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
455 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
456 |
// !!! FIXME: This assumes all texcoord0 attributes in the effect are |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
457 |
// !!! FIXME: actually point coords! It ain't necessarily so! -caleb |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
458 |
repl = "[[ point_coord ]]"; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
459 |
while ((ptr = strstr(shader_source, "[[user(texcoord0)]]"))) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
460 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
461 |
memcpy(ptr, repl, strlen(repl)); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
462 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
463 |
// "float4" -> "float2" |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
464 |
int spaces = 0; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
465 |
while (spaces < 2) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
466 |
if (*(ptr--) == ' ') |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
467 |
spaces++; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
468 |
memcpy(ptr, "2", sizeof(char)); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
469 |
} // while |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
470 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
471 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
472 |
// Compile the source into a library |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
473 |
compileError = NULL; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
474 |
shader_source_ns = msg_s( |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
475 |
msg(ctx->classNSString, ctx->selAlloc), |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
476 |
ctx->selInitWithUTF8String, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
477 |
shader_source |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
478 |
); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
479 |
retval = msg_ppp(ctx->device, ctx->selNewLibraryWithSource, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
480 |
shader_source_ns, NULL, &compileError); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
481 |
f(shader_source, d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
482 |
msg(shader_source_ns, ctx->selRelease); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
483 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
484 |
if (retval == NULL) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
485 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
486 |
compileError = msg(compileError, ctx->selLocalizedDescription); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
487 |
set_error((char*) msg(compileError, ctx->selUTF8String)); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
488 |
return NULL; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
489 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
490 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
491 |
// Run through the shaders again, setting the library reference |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
492 |
for (i = 0; i < effect->object_count; i++) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
493 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
494 |
object = &effect->objects[i]; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
495 |
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
496 |
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
497 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
498 |
if (object->shader.is_preshader) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
499 |
continue; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
500 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
501 |
((MOJOSHADER_mtlShader*) object->shader.shader)->library = retval; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
502 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
503 |
} // for |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
504 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
505 |
return retval; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
506 |
} // MOJOSHADER_mtlCompileLibrary |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
507 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
508 |
void MOJOSHADER_mtlDeleteLibrary(void *library) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
509 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
510 |
msg(library, ctx->selRelease); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
511 |
} // MOJOSHADER_mtlDeleteLibrary |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
512 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
513 |
MOJOSHADER_mtlShader *MOJOSHADER_mtlCompileShader(const char *mainfn, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
514 |
const unsigned char *tokenbuf, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
515 |
const unsigned int bufsize, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
516 |
const MOJOSHADER_swizzle *swiz, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
517 |
const unsigned int swizcount, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
518 |
const MOJOSHADER_samplerMap *smap, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
519 |
const unsigned int smapcount) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
520 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
521 |
MOJOSHADER_malloc m = ctx->malloc_fn; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
522 |
MOJOSHADER_free f = ctx->free_fn; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
523 |
void *d = ctx->malloc_data; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
524 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
525 |
const MOJOSHADER_parseData *pd = MOJOSHADER_parse("metal", mainfn, tokenbuf, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
526 |
bufsize, swiz, swizcount, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
527 |
smap, smapcount, m, f, d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
528 |
if (pd->error_count > 0) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
529 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
530 |
// !!! FIXME: put multiple errors in the buffer? Don't use |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
531 |
// !!! FIXME: MOJOSHADER_mtlGetError() for this? |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
532 |
set_error(pd->errors[0].error); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
533 |
goto compile_shader_fail; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
534 |
} // if |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
535 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
536 |
MOJOSHADER_mtlShader *retval = (MOJOSHADER_mtlShader *) m(sizeof(MOJOSHADER_mtlShader), d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
537 |
if (retval == NULL) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
538 |
goto compile_shader_fail; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
539 |
|
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
540 |
retval->parseData = pd; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
541 |
retval->refcount = 1; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
542 |
retval->ubo = create_ubo(retval, m, d); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
543 |
retval->library = NULL; // populated by MOJOSHADER_mtlCompileLibrary |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
544 |
|
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
545 |
return retval; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
546 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
547 |
compile_shader_fail: |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
548 |
MOJOSHADER_freeParseData(retval->parseData); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
549 |
f(retval, d); |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
550 |
return NULL; |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
551 |
} // MOJOSHADER_mtlCompileShader |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
552 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
553 |
void MOJOSHADER_mtlShaderAddRef(MOJOSHADER_mtlShader *shader) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
554 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
555 |
if (shader != NULL) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
556 |
shader->refcount++; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
557 |
} // MOJOSHADER_mtlShaderAddRef |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
558 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
559 |
void MOJOSHADER_mtlDeleteShader(MOJOSHADER_mtlShader *shader) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
560 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
561 |
if (shader != NULL) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
562 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
563 |
if (shader->refcount > 1) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
564 |
shader->refcount--; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
565 |
else |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
566 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
567 |
dealloc_ubo(shader, ctx->free_fn, ctx->malloc_data); |
1250
30f8b128b906
Fix memory leak in mtlDeleteShader
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1245
diff
changeset
|
568 |
MOJOSHADER_freeParseData(shader->parseData); |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
569 |
ctx->free_fn(shader, ctx->malloc_data); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
570 |
} // else |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
571 |
} // if |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
572 |
} // MOJOSHADER_mtlDeleteShader |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
573 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
574 |
const MOJOSHADER_parseData *MOJOSHADER_mtlGetShaderParseData( |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
575 |
MOJOSHADER_mtlShader *shader) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
576 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
577 |
return (shader != NULL) ? shader->parseData : NULL; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
578 |
} // MOJOSHADER_mtlGetParseData |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
579 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
580 |
void MOJOSHADER_mtlBindShaders(MOJOSHADER_mtlShader *vshader, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
581 |
MOJOSHADER_mtlShader *pshader) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
582 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
583 |
// Use the last bound shaders in case of NULL |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
584 |
if (vshader != NULL) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
585 |
ctx->vertexShader = vshader; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
586 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
587 |
if (pshader != NULL) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
588 |
ctx->pixelShader = pshader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
589 |
} // MOJOSHADER_mtlBindShaders |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
590 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
591 |
void MOJOSHADER_mtlGetBoundShaders(MOJOSHADER_mtlShader **vshader, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
592 |
MOJOSHADER_mtlShader **pshader) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
593 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
594 |
*vshader = ctx->vertexShader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
595 |
*pshader = ctx->pixelShader; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
596 |
} // MOJOSHADER_mtlGetBoundShaders |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
597 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
598 |
void MOJOSHADER_mtlMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
599 |
float **psf, int **psi, unsigned char **psb) |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
600 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
601 |
*vsf = ctx->vs_reg_file_f; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
602 |
*vsi = ctx->vs_reg_file_i; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
603 |
*vsb = ctx->vs_reg_file_b; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
604 |
*psf = ctx->ps_reg_file_f; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
605 |
*psi = ctx->ps_reg_file_i; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
606 |
*psb = ctx->ps_reg_file_b; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
607 |
} // MOJOSHADER_mtlMapUniformBufferMemory |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
608 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
609 |
void MOJOSHADER_mtlUnmapUniformBufferMemory() |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
610 |
{ |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
611 |
/* This has nothing to do with unmapping memory |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
612 |
* and everything to do with updating uniform |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
613 |
* buffers with the latest parameter contents. |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
614 |
*/ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
615 |
update_uniform_buffer(ctx->vertexShader); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
616 |
update_uniform_buffer(ctx->pixelShader); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
617 |
} // MOJOSHADER_mtlUnmapUniformBufferMemory |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
618 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
619 |
void MOJOSHADER_mtlGetUniformBuffers(void **vbuf, int *voff, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
620 |
void **pbuf, int *poff) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
621 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
622 |
*vbuf = get_uniform_buffer(ctx->vertexShader); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
623 |
*voff = get_uniform_offset(ctx->vertexShader); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
624 |
*pbuf = get_uniform_buffer(ctx->pixelShader); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
625 |
*poff = get_uniform_offset(ctx->pixelShader); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
626 |
} // MOJOSHADER_mtlGetUniformBuffers |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
627 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
628 |
void *MOJOSHADER_mtlGetFunctionHandle(MOJOSHADER_mtlShader *shader) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
629 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
630 |
if (shader == NULL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
631 |
return NULL; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
632 |
|
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
633 |
void *fnname = msg_s( |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
634 |
msg(ctx->classNSString, ctx->selAlloc), |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
635 |
ctx->selInitWithUTF8String, |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
636 |
shader->parseData->mainfn |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
637 |
); |
1241 | 638 |
void *ret = msg_p( |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
639 |
shader->library, |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
640 |
ctx->selNewFunctionWithName, |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
641 |
fnname |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
642 |
); |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
643 |
msg(fnname, ctx->selRelease); |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
644 |
msg(ret, ctx->selRetain); |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
645 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
646 |
return ret; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
647 |
} // MOJOSHADER_mtlGetFunctionHandle |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
648 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
649 |
void MOJOSHADER_mtlEndFrame() |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
650 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
651 |
for (int i = 0; i < ctx->numBuffersInUse; i += 1) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
652 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
653 |
MOJOSHADER_mtlUniformBuffer *buf = ctx->buffersInUse[i]; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
654 |
buf->internalOffset = 0; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
655 |
buf->currentFrame = (buf->currentFrame + 1) % ctx->framesInFlight; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
656 |
buf->inUse = 0; |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
657 |
} // for |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
658 |
ctx->numBuffersInUse = 0; |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
659 |
} // MOJOSHADER_mtlEndFrame |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
660 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
661 |
int MOJOSHADER_mtlGetVertexAttribLocation(MOJOSHADER_mtlShader *vert, |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
662 |
MOJOSHADER_usage usage, int index) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
663 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
664 |
if (vert == NULL) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
665 |
return -1; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
666 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
667 |
for (int i = 0; i < vert->parseData->attribute_count; i++) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
668 |
{ |
1245
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
669 |
if (vert->parseData->attributes[i].usage == usage && |
ad9a16c8b023
Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents:
1241
diff
changeset
|
670 |
vert->parseData->attributes[i].index == index) |
1229
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
671 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
672 |
return i; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
673 |
} // if |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
674 |
} // for |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
675 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
676 |
// failure, couldn't find requested attribute |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
677 |
return -1; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
678 |
} // MOJOSHADER_mtlGetVertexAttribLocation |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
679 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
680 |
const char *MOJOSHADER_mtlGetError(void) |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
681 |
{ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
682 |
return error_buffer; |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
683 |
} // MOJOSHADER_mtlGetError |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
684 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
685 |
#endif /* MOJOSHADER_EFFECT_SUPPORT */ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
686 |
#endif /* SUPPORT_PROFILE_METAL && PLATFORM_APPLE */ |
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
687 |
|
3ecd29a50d9d
Metal Effect support!
Caleb Cornett <caleb.cornett@outlook.com>
parents:
diff
changeset
|
688 |
// end of mojoshader_metal.c ... |