Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OOShaderUniformMethodType.m
Go to the documentation of this file.
1/*
2
3OOShaderUniformMethodType.m
4
5
6Copyright (C) 2007-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28
29/*
30 For shader uniform binding to work, it is necessary to be able to tell the
31 return type of a method. This is done by comparing the methodReturnType of
32 a method's NSMethodSignature to those of methods in a template class, one
33 method for each supported return type.
34
35 Under OS X, the methodReturnType for a type foo is simply @encode(foo),
36 but under the GNU runtime, it is the @encode() string for the entire
37 method signature. In general, this is platform-defined. In order to
38 maintain an implementation-agnostic approach, we get the signature from a
39 known method of each time at runtime.
40
41 NOTE: the GNU runtime's approach means that the methodReturnType differs
42 between different method signatures with the same return type. For
43 instance, a method -(id)foo:(int) will have a different methodReturnType
44 than a method -(id)foo. As far as I can see this is a bug, but Oolite only
45 supports binding to methods with no parameters, so this is not a problem.
46*/
47
49
50#if OO_SHADERS || !defined(NDEBUG)
51
52
53#import "OOMaths.h"
54
55static BOOL sInited = NO;
56static const char *sTemplates[kOOShaderUniformTypeCount];
57
58static void InitTemplates(void);
59static const char *CopyTemplateForSelector(SEL selector);
60
61
63{
64 unsigned i;
65 const char *typeCode = NULL;
66
67 if (EXPECT_NOT(sInited == NO)) InitTemplates();
68
69 typeCode = [signature methodReturnType];
70 if (EXPECT_NOT(typeCode == NULL)) return kOOShaderUniformTypeInvalid;
71
73 {
74 if (sTemplates[i] != NULL && strcmp(sTemplates[i], typeCode) == 0) return i;
75 }
76
78}
79
80
82
83- (float)floatMethod;
84- (double)doubleMethod;
85- (signed char)signedCharMethod;
86- (unsigned char)unsignedCharMethod;
87- (signed short)signedShortMethod;
88- (unsigned short)unsignedShortMethod;
89- (signed int)signedIntMethod;
90- (unsigned int)unsignedIntMethod;
91- (signed long)signedLongMethod;
92- (unsigned long)unsignedLongMethod;
93- (Vector)vectorMethod;
94- (HPVector)hpvectorMethod;
95- (Quaternion)quaternionMethod;
96- (OOMatrix)matrixMethod;
97- (NSPoint)pointMethod;
98- (id)idMethod;
99
100@end
101
102
128
129
130static const char *CopyTemplateForSelector(SEL selector)
131{
132 NSMethodSignature *signature = nil;
133 const char *typeCode = NULL;
134
135 signature = [OOShaderUniformTypeMethodSignatureTemplateClass instanceMethodSignatureForSelector:selector];
136 typeCode = [signature methodReturnType];
137
138 /* typeCode is *probably* a constant, but this isn't formally guaranteed
139 as far as I'm aware, so we make a copy of it.
140 */
141 return typeCode ? strdup(typeCode) : NULL;
142}
143
144
146
147- (signed char)signedCharMethod
148{
149 return 0;
150}
151
152
153- (unsigned char)unsignedCharMethod
154{
155 return 0;
156}
157
158
159- (signed short)signedShortMethod
160{
161 return 0;
162}
163
164
165- (unsigned short)unsignedShortMethod
166{
167 return 0;
168}
169
170
171- (signed int)signedIntMethod
172{
173 return 0;
174}
175
176
177- (unsigned int)unsignedIntMethod
178{
179 return 0;
180}
181
182
183- (signed long)signedLongMethod
184{
185 return 0;
186}
187
188
189- (unsigned long)unsignedLongMethod
190{
191 return 0;
192}
193
194
195- (float)floatMethod
196{
197 return 0.0f;
198}
199
200
201- (double)doubleMethod
202{
203 return 0.0;
204}
205
206
207- (Vector)vectorMethod
208{
209 Vector v = {0};
210 return v;
211}
212
213
214- (HPVector)hpvectorMethod
215{
216 HPVector v = {0};
217 return v;
218}
219
220
221- (Quaternion)quaternionMethod
222{
223 Quaternion q = {0};
224 return q;
225}
226
227
228- (OOMatrix)matrixMethod
229{
230 return kZeroMatrix;
231}
232
233
234- (NSPoint)pointMethod
235{
236 return NSZeroPoint;
237}
238
239
240- (id)idMethod
241{
242 return nil;
243}
244
245@end
246
247
248long long OOCallIntegerMethod(id object, SEL selector, IMP method, OOShaderUniformType type)
249{
250 switch (type)
251 {
253 return ((CharReturnMsgSend)method)(object, selector);
254
256 return ((UnsignedCharReturnMsgSend)method)(object, selector);
257
259 return ((ShortReturnMsgSend)method)(object, selector);
260
262 return ((UnsignedShortReturnMsgSend)method)(object, selector);
263
265 return ((IntReturnMsgSend)method)(object, selector);
266
268 return ((UnsignedIntReturnMsgSend)method)(object, selector);
269
271 return ((LongReturnMsgSend)method)(object, selector);
272
274 return ((UnsignedLongReturnMsgSend)method)(object, selector);
275
277 return ((LongLongReturnMsgSend)method)(object, selector);
278
280 return ((UnsignedLongLongReturnMsgSend)method)(object, selector);
281
282 default:
283 return 0;
284 }
285}
286
287
288double OOCallFloatMethod(id object, SEL selector, IMP method, OOShaderUniformType type)
289{
290 switch (type)
291 {
293 return ((FloatReturnMsgSend)method)(object, selector);
294
296 return ((DoubleReturnMsgSend)method)(object, selector);
297
298 default:
299 return 0;
300 }
301}
302
303#endif // OO_SHADERS
#define EXPECT_NOT(x)
const OOMatrix kZeroMatrix
Definition OOMatrix.m:38
return nil
int(* IntReturnMsgSend)(id, SEL)
unsigned int(* UnsignedIntReturnMsgSend)(id, SEL)
double(* DoubleReturnMsgSend)(id, SEL)
char(* CharReturnMsgSend)(id, SEL)
long long(* LongLongReturnMsgSend)(id, SEL)
short(* ShortReturnMsgSend)(id, SEL)
unsigned char(* UnsignedCharReturnMsgSend)(id, SEL)
unsigned long(* UnsignedLongReturnMsgSend)(id, SEL)
unsigned short(* UnsignedShortReturnMsgSend)(id, SEL)
@ kOOShaderUniformTypeCount
@ kOOShaderUniformTypeQuaternion
@ kOOShaderUniformTypeMatrix
@ kOOShaderUniformTypeVector
@ kOOShaderUniformTypeLong
@ kOOShaderUniformTypeUnsignedChar
@ kOOShaderUniformTypeShort
@ kOOShaderUniformTypePoint
@ kOOShaderUniformTypeUnsignedLong
@ kOOShaderUniformTypeFloat
@ kOOShaderUniformTypeUnsignedInt
@ kOOShaderUniformTypeUnsignedShort
@ kOOShaderUniformTypeLongLong
@ kOOShaderUniformTypeUnsignedLongLong
@ kOOShaderUniformTypeDouble
@ kOOShaderUniformTypeObject
@ kOOShaderUniformTypeHPVector
@ kOOShaderUniformTypeInt
@ kOOShaderUniformTypeChar
@ kOOShaderUniformTypeInvalid
float(* FloatReturnMsgSend)(id, SEL)
unsigned long long(* UnsignedLongLongReturnMsgSend)(id, SEL)
long(* LongReturnMsgSend)(id, SEL)
long long OOCallIntegerMethod(id object, SEL selector, IMP method, OOShaderUniformType type)
static const char * CopyTemplateForSelector(SEL selector)
static void InitTemplates(void)
static const char * sTemplates[kOOShaderUniformTypeCount]
OOShaderUniformType OOShaderUniformTypeFromMethodSignature(NSMethodSignature *signature)
double OOCallFloatMethod(id object, SEL selector, IMP method, OOShaderUniformType type)
static BOOL sInited
#define GET_TEMPLATE(enumValue, sel)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque