Oolite
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 For shader uniform binding to work, it is necessary to be able to tell the
30 return type of a method. This is done by comparing the methodReturnType of
31 a method's NSMethodSignature to those of methods in a template class, one
32 method for each supported return type.
33
34 Under OS X, the methodReturnType for a type foo is simply @encode(foo),
35 but under the GNU runtime, it is the @encode() string for the entire
36 method signature. In general, this is platform-defined. In order to
37 maintain an implementation-agnostic approach, we get the signature from a
38 known method of each time at runtime.
39
40 NOTE: the GNU runtime's approach means that the methodReturnType differs
41 between different method signatures with the same return type. For
42 instance, a method -(id)foo:(int) will have a different methodReturnType
43 than a method -(id)foo. As far as I can see this is a bug, but Oolite only
44 supports binding to methods with no parameters, so this is not a problem.
45*/
46
48
49#if OO_SHADERS || defined(OO_DEBUG_MONITOR_ENABLED)
50
51#import "OOMaths.h"
52
53static BOOL sInited = NO;
54static const char* sTemplates[kOOShaderUniformTypeCount];
55
56static void InitTemplates(void);
57static const char* CopyTemplateForSelector(SEL selector);
58
59OOShaderUniformType OOShaderUniformTypeFromMethodSignature(NSMethodSignature* signature)
60{
61 unsigned i;
62 const char* typeCode = NULL;
63
64 if (EXPECT_NOT(sInited == NO))
65 InitTemplates();
66
67 typeCode = [signature methodReturnType];
68 if (EXPECT_NOT(typeCode == NULL))
69 return kOOShaderUniformTypeInvalid;
70
71 for (i = kOOShaderUniformTypeInvalid + 1; i != kOOShaderUniformTypeCount; ++i) {
72 if (sTemplates[i] != NULL && strcmp(sTemplates[i], typeCode) == 0)
73 return i;
74 }
75
76 return kOOShaderUniformTypeInvalid;
77}
78
79@interface OOShaderUniformTypeMethodSignatureTemplateClass : NSObject
80
81- (float)floatMethod;
82- (double)doubleMethod;
83- (signed char)signedCharMethod;
84- (unsigned char)unsignedCharMethod;
85- (signed short)signedShortMethod;
86- (unsigned short)unsignedShortMethod;
87- (signed int)signedIntMethod;
88- (unsigned int)unsignedIntMethod;
89- (signed long)signedLongMethod;
90- (unsigned long)unsignedLongMethod;
91- (Vector)vectorMethod;
92- (HPVector)hpvectorMethod;
93- (Quaternion)quaternionMethod;
94- (OOMatrix)matrixMethod;
95- (NSPoint)pointMethod;
96- (id)idMethod;
97
98@end
99
100static void InitTemplates(void)
101{
102#define GET_TEMPLATE(enumValue, sel) \
103 do { \
104 sTemplates[enumValue] = CopyTemplateForSelector(@selector(sel)); \
105 } while (0)
106
107 GET_TEMPLATE(kOOShaderUniformTypeChar, signedCharMethod);
108 GET_TEMPLATE(kOOShaderUniformTypeUnsignedChar, unsignedCharMethod);
109 GET_TEMPLATE(kOOShaderUniformTypeShort, signedShortMethod);
110 GET_TEMPLATE(kOOShaderUniformTypeUnsignedShort, unsignedShortMethod);
111 GET_TEMPLATE(kOOShaderUniformTypeInt, signedIntMethod);
112 GET_TEMPLATE(kOOShaderUniformTypeUnsignedInt, unsignedIntMethod);
113 GET_TEMPLATE(kOOShaderUniformTypeLong, signedLongMethod);
114 GET_TEMPLATE(kOOShaderUniformTypeUnsignedLong, unsignedLongMethod);
115 GET_TEMPLATE(kOOShaderUniformTypeFloat, floatMethod);
116 GET_TEMPLATE(kOOShaderUniformTypeDouble, doubleMethod);
117 GET_TEMPLATE(kOOShaderUniformTypeVector, vectorMethod);
118 GET_TEMPLATE(kOOShaderUniformTypeHPVector, hpvectorMethod);
119 GET_TEMPLATE(kOOShaderUniformTypeQuaternion, quaternionMethod);
120 GET_TEMPLATE(kOOShaderUniformTypeMatrix, matrixMethod);
121 GET_TEMPLATE(kOOShaderUniformTypePoint, pointMethod);
122 GET_TEMPLATE(kOOShaderUniformTypeObject, idMethod);
123
124 sInited = YES;
125}
126
127static const char* CopyTemplateForSelector(SEL selector)
128{
129 NSMethodSignature* signature = nil;
130 const char* typeCode = NULL;
131
132 signature = [OOShaderUniformTypeMethodSignatureTemplateClass instanceMethodSignatureForSelector:selector];
133 typeCode = [signature methodReturnType];
134
135 /* typeCode is *probably* a constant, but this isn't formally guaranteed
136 as far as I'm aware, so we make a copy of it.
137 */
138 return typeCode ? strdup(typeCode) : NULL;
139}
140
141@implementation OOShaderUniformTypeMethodSignatureTemplateClass : NSObject
142
143- (signed char)signedCharMethod
144{
145 return 0;
146}
147
148- (unsigned char)unsignedCharMethod
149{
150 return 0;
151}
152
153- (signed short)signedShortMethod
154{
155 return 0;
156}
157
158- (unsigned short)unsignedShortMethod
159{
160 return 0;
161}
162
163- (signed int)signedIntMethod
164{
165 return 0;
166}
167
168- (unsigned int)unsignedIntMethod
169{
170 return 0;
171}
172
173- (signed long)signedLongMethod
174{
175 return 0;
176}
177
178- (unsigned long)unsignedLongMethod
179{
180 return 0;
181}
182
183- (float)floatMethod
184{
185 return 0.0f;
186}
187
188- (double)doubleMethod
189{
190 return 0.0;
191}
192
193- (Vector)vectorMethod
194{
195 Vector v = { 0 };
196 return v;
197}
198
199- (HPVector)hpvectorMethod
200{
201 HPVector v = { 0 };
202 return v;
203}
204
205- (Quaternion)quaternionMethod
206{
207 Quaternion q = { 0 };
208 return q;
209}
210
211- (OOMatrix)matrixMethod
212{
213 return kZeroMatrix;
214}
215
216- (NSPoint)pointMethod
217{
218 return NSZeroPoint;
219}
220
221- (id)idMethod
222{
223 return nil;
224}
225
226@end
227
228long long OOCallIntegerMethod(id object, SEL selector, IMP method, OOShaderUniformType type)
229{
230 switch (type) {
231 case kOOShaderUniformTypeChar:
232 return ((CharReturnMsgSend)method)(object, selector);
233
234 case kOOShaderUniformTypeUnsignedChar:
235 return ((UnsignedCharReturnMsgSend)method)(object, selector);
236
237 case kOOShaderUniformTypeShort:
238 return ((ShortReturnMsgSend)method)(object, selector);
239
240 case kOOShaderUniformTypeUnsignedShort:
241 return ((UnsignedShortReturnMsgSend)method)(object, selector);
242
243 case kOOShaderUniformTypeInt:
244 return ((IntReturnMsgSend)method)(object, selector);
245
246 case kOOShaderUniformTypeUnsignedInt:
247 return ((UnsignedIntReturnMsgSend)method)(object, selector);
248
249 case kOOShaderUniformTypeLong:
250 return ((LongReturnMsgSend)method)(object, selector);
251
252 case kOOShaderUniformTypeUnsignedLong:
253 return ((UnsignedLongReturnMsgSend)method)(object, selector);
254
255 case kOOShaderUniformTypeLongLong:
256 return ((LongLongReturnMsgSend)method)(object, selector);
257
258 case kOOShaderUniformTypeUnsignedLongLong:
259 return ((UnsignedLongLongReturnMsgSend)method)(object, selector);
260
261 default:
262 return 0;
263 }
264}
265
266double OOCallFloatMethod(id object, SEL selector, IMP method, OOShaderUniformType type)
267{
268 switch (type) {
269 case kOOShaderUniformTypeFloat:
270 return ((FloatReturnMsgSend)method)(object, selector);
271
272 case kOOShaderUniformTypeDouble:
273 return ((DoubleReturnMsgSend)method)(object, selector);
274
275 default:
276 return 0;
277 }
278}
279
280#endif // OO_SHADERS
static BOOL sInited
Definition OOCPUInfo.m:56
#define EXPECT_NOT(x)
struct HPVector HPVector
const OOMatrix kZeroMatrix
Definition OOMatrix.m:33
return nil
struct Quaternion Quaternion
struct Vector Vector
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque