-
Notifications
You must be signed in to change notification settings - Fork 8
/
material.d
358 lines (319 loc) · 9.1 KB
/
material.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
module fuji.material;
public import fuji.c.MFMaterial;
import fuji.resource;
import fuji.texture;
import fuji.matrix;
import std.algorithm : canFind;
import std.conv : to;
nothrow:
@nogc:
struct Material
{
alias resource this;
MFMaterial *pMaterial;
nothrow:
@nogc:
this(this) pure
{
addRef();
}
this(ref Resource resource) pure
{
// TODO: should this throw instead?
if(resource.type == MFResourceType.Material)
{
pMaterial = cast(MFMaterial*)resource.handle;
addRef();
}
}
this(const(char)[] name)
{
create(name);
}
this(MFStockMaterials materialIdentifier)
{
createStockMaterial(materialIdentifier);
}
~this()
{
release();
}
void opAssign(MFMaterial *pMaterial)
{
release();
this.pMaterial = pMaterial;
addRef();
}
void opAssign(Material material)
{
release();
pMaterial = material.pMaterial;
addRef();
}
void create(const(char)[] name)
{
release();
auto s = Stringz!(64)(name);
pMaterial = MFMaterial_Create(s);
}
void createExisting(const(char)[] name)
{
release();
auto s = Stringz!(64)(name);
pMaterial = MFMaterial_Find(s);
}
void createStockMaterial(MFStockMaterials materialIdentifier)
{
release();
pMaterial = MFMaterial_GetStockMaterial(materialIdentifier);
}
int release()
{
int rc = 0;
if(pMaterial)
{
rc = MFMaterial_Release(pMaterial);
pMaterial = null;
}
return rc;
}
void setCurrent()
{
MFMaterial_SetMaterial(pMaterial);
}
@property inout(MFMaterial)* handle() inout pure { return pMaterial; }
@property ref inout(Resource) resource() inout pure { return *cast(inout(Resource)*)&this; }
@property const(char)[] name() const pure
{
return MFMaterial_GetMaterialName(pMaterial).toDStr;
}
@property inout(Parameters) parameters() inout
{
return inout(Parameters)(pMaterial, 0, MFMaterial_GetNumParameters(pMaterial));
}
struct Parameter
{
// alias asInt this;
// alias asFloat this;
nothrow:
//@nogc:
this(MFMaterial* pMaterial, size_t index) @nogc
{
this.pMaterial = pMaterial;
pParameterInfo = index != -1 ? MFMaterial_GetParameterInfo(pMaterial, cast(int)index) : null;
if(pParameterInfo)
{
if(pParameterInfo.argIndex.type == MFParamType.Constant)
{
parameterArg = pParameterInfo.argIndex.defaultValue;
bLowordSet = true;
}
if(pParameterInfo.argIndexHigh.type == MFParamType.Constant)
{
parameterArg |= pParameterInfo.argIndexHigh.defaultValue << 16;
bHiwordSet = true;
}
}
}
bool opCast(T)() const pure @nogc if(is(T == bool))
{
return pParameterInfo != null;
}
@property const(char)[] name() const pure @nogc
{
return pParameterInfo.pParameterName.toDStr;
}
@property ref const(MFMaterialParameterInfo) info() const pure @nogc
{
return *pParameterInfo;
}
Parameter opIndex(int i) pure //@nogc
{
Parameter p = this;
if(pParameterInfo.argIndexHigh.type == MFParamType.Enum || pParameterInfo.argIndexHigh.type == MFParamType.Int)
{
assert(pParameterInfo.argIndex.type != MFParamType.Enum && pParameterInfo.argIndex.type != MFParamType.Int, "Parameters with 2 arguments are not (yet) supported >_<");
p.parameterArg = (parameterArg & 0xFFFF) | (i << 16);
p.bHiwordSet = true;
}
else if(pParameterInfo.argIndex.type == MFParamType.Enum)
{
p.parameterArg = (parameterArg & 0xFFFF0000) | i;
p.bLowordSet = true;
}
else
assert(false, "Material parameter '" ~ pParameterInfo.pParameterName.toDStr ~ "' is not indexable.");
return p;
}
Parameter opIndex(const(char)[] s) pure //@nogc
{
Parameter p = this;
bool bHigh;
const(MFEnumKey)* pKey;
if(pParameterInfo.argIndexHigh.type == MFParamType.Enum)
{
assert(pParameterInfo.argIndex.type != MFParamType.Enum, "Parameters with 2 arguments are not (yet) supported >_<");
pKey = pParameterInfo.argIndexHigh.pEnumKeys;
bHigh = true;
}
else if(pParameterInfo.argIndex.type == MFParamType.Enum)
{
pKey = pParameterInfo.argIndex.pEnumKeys;
}
else
assert(false, "Material parameter '" ~ pParameterInfo.pParameterName.toDStr ~ "' does not have an enum index.");
while(pKey && pKey.pKey)
{
// TODO: should this be case insensitive?
if(pKey.pKey[0..s.length] == s[] && pKey.pKey[s.length] == 0)
{
p.parameterArg = bHigh ? (parameterArg & 0xFFFF) | (pKey.value << 16) : (parameterArg & 0xFFFF0000) | pKey.value;
if(bHigh)
p.bHiwordSet = true;
else
p.bLowordSet = true;
return p;
}
++pKey;
}
assert(false, "Invalid enum key '" ~ s ~ "' for material parameter: " ~ pParameterInfo.pParameterName.toDStr);
}
bool ValidateArg(MFParamType[] types ...) const pure //@nogc
{
assert(pParameterInfo.numValues == 1, "Material parameter '" ~ pParameterInfo.pParameterName.toDStr ~ "' is incorrect type: struct");
// FIXME assert(canFind(types, pParameterInfo.pValues[0].type), "Material parameter '" ~ pParameterInfo.pParameterName.toDStr ~ "' is incorrect type: " ~ to!string(pParameterInfo.pValues[0].type));
assert(canFind(types, pParameterInfo.pValues[0].type), "Material parameter '" ~ pParameterInfo.pParameterName.toDStr ~ "' is incorrect type: " ~ to!string(cast(int)pParameterInfo.pValues[0].type));
return true;
}
// getters...
@property int asInt() const //@nogc
{
debug ValidateArg(MFParamType.Int, MFParamType.Enum, MFParamType.Bool);
return cast(int)GetParameter(null);
}
@property float asFloat() const //@nogc
{
debug ValidateArg(MFParamType.Float);
float value;
GetParameter(&value);
return value;
}
@property const(char)[] asString() const //@nogc
{
debug ValidateArg(MFParamType.String, MFParamType.Enum);
if(pParameterInfo.pValues[0].type == MFParamType.String)
{
const(char)* value = cast(const(char)*)GetParameter(null);
return value.toDStr;
}
else// if(pParameterInfo.pValues[0].type == MFParamType.Enum)
{
// TODO: return enum key for value...
assert(false, "TODO");
}
}
@property bool asBool() const //@nogc
{
debug ValidateArg(MFParamType.Bool);
return GetParameter(null) != 0;
}
@property MFVector asVector() const //@nogc
{
debug ValidateArg(MFParamType.Vector3, MFParamType.Vector4);
MFVector v;
GetParameter(&v);
return v;
}
@property MFMatrix asMatrix() const //@nogc
{
debug ValidateArg(MFParamType.Matrix);
MFMatrix m;
GetParameter(&m);
return m;
}
@property Texture asTexture() const //@nogc
{
// HACK: fuji material system needs to be tidied up!
debug ValidateArg(MFParamType.String); // **String?**
size_t t = GetParameter(null);
Texture tex;
tex.pTexture = cast(MFTexture*)t;
tex.addRef();
return tex;
}
// setters...
void opAssign(int i) //@nogc
{
debug ValidateArg(MFParamType.Int, MFParamType.Enum);
SetParameter(i);
}
void opAssign(float f) //@nogc
{
debug ValidateArg(MFParamType.Float);
SetParameter(cast(size_t)&f);
}
void opAssign(const(char)[] s) //@nogc
{
debug ValidateArg(MFParamType.String, MFParamType.Enum);
if(pParameterInfo.pValues[0].type == MFParamType.String)
{
auto str = Stringz!(32)(s);
SetParameter(cast(size_t)str);
}
else// if(pParameterInfo.pValues[0].type == MFParamType.Enum)
{
const(MFEnumKey)* pKey = pParameterInfo.pValues[0].pEnumKeys;
while(pKey && pKey.pKey)
{
// TODO: should this be case insensitive?
if(pKey.pKey[0..s.length] == s[] && pKey.pKey[s.length] == 0)
{
SetParameter(pKey.value);
return;
}
++pKey;
}
assert(false, "Invalid enum key '" ~ s ~ "' for material parameter: " ~ pParameterInfo.pParameterName.toDStr);
}
}
void opAssign(bool b) //@nogc
{
debug ValidateArg(MFParamType.Bool);
SetParameter(b ? 1 : 0);
}
void opAssign(ref in MFVector v) //@nogc
{
debug ValidateArg(MFParamType.Vector3, MFParamType.Vector4);
SetParameter(cast(size_t)&v);
}
void opAssign(ref in MFMatrix m) //@nogc
{
debug ValidateArg(MFParamType.Matrix);
SetParameter(cast(size_t)&m);
}
void opAssign(const Texture t)
{
// HACK: fuji material system needs to be tidied up!
assert(false, "TODO: fuji needs a texture type...");
// SetParameter(cast(size_t)t.pTexture);
}
private:
size_t GetParameter(void* pValue = null) const //@nogc
{
assert(pParameterInfo.argIndex.type == MFParamType.None || bLowordSet, "Argument index not specified!");
assert(pParameterInfo.argIndexHigh.type == MFParamType.None || bHiwordSet, "Argument index not specified!");
return MFMaterial_GetParameter(pMaterial, pParameterInfo.parameterIndex, parameterArg, pValue);
}
void SetParameter(size_t value) //@nogc
{
assert(pParameterInfo.argIndex.type == MFParamType.None || bLowordSet, "Argument index not specified!");
assert(pParameterInfo.argIndexHigh.type == MFParamType.None || bHiwordSet, "Argument index not specified!");
MFMaterial_SetParameter(pMaterial, pParameterInfo.parameterIndex, parameterArg, value);
}
MFMaterial* pMaterial;
const(MFMaterialParameterInfo)* pParameterInfo;
int parameterArg;
bool bLowordSet, bHiwordSet;
}
alias Parameters = ResourceRange!(MFMaterial, Parameter, MFMaterial_GetParameterIndexFromName, true, true);
}