-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimdUtils.pas
300 lines (271 loc) · 6.59 KB
/
SimdUtils.pas
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
unit SimdUtils;
interface
{$mode objfpc}
{$IFDEF Darwin}
//{$mode objfpc}
{$modeswitch objectivec1}
{$ENDIF}
uses
{$IFDEF Darwin} CocoaAll, MacOSAll, {$ENDIF}
//dialogs
sysutils, VectorMath;
const
kMaxUniform = 12;//for Metal: divisible by 4
type
TUniform = record
Name,Hint: string;
Widget: integer;
Min,DefaultV,Max: single;
Bool: boolean;
end;
TShaderPrefs = record
nUniform: integer;
Uniform: array [1..kMaxUniform] of TUniform;
end;
TUInt32s = array of uint32;
TInt32s = array of int32;
TUInt16s = array of uint16;
TInt16s = array of int16;
TUInt8s = array of uint8;
TInt8s = array of int8;
TFloat32s = array of single;
TFloat64s = array of double;
TRGB = packed record //red,green,blue
R,G,B : byte;
end;
TRGBs = array of TRGB;
TRGBA = packed record //red,green,blue,alpha
R,G,B,A : byte;
end;
TRGBAs = array of TRGBA;
TLUT = array [0..255] of TRGBA; //Color Lookup Table
TVec3i = packed record
case integer of
0: (v: array[0..2] of int32);
1: (x, y, z: int32);
end;
TVec4i = packed record
case integer of
0: (v: array[0..3] of int32);
1: (x, y, z, t: int32);
end;
TVec6i = packed record
case integer of
0: (v: array[0..5] of int32);
1: (xLo, yLo, zLo, xHi, yHi, zHi: int32);
end;
TVec6 = packed record
case integer of
0: (v: array[0..5] of single);
1: (xLo, yLo, zLo, xHi, yHi, zHi: single);
end;
function prod(v:TVec3i): int64;
function SetRGBA(r,g,b,a: byte): TRGBA; overload;
function SetRGBA(v:TVec4): TRGBA; overload;
function pti(x,y,z: integer): TVec3i; //create integer vector
procedure SortVec3i(var lo, hi: TVec3i);
function loadShaderPrefs(shaderName: string): TShaderPrefs;
function ResourceDir (): string;
function ScriptDir (): string;
function ResourceFile (name: pchar; ofType: pchar): string;
function ShaderDir (): string;
function Vec6 (xLo, yLo, zLo, xHi, yHi, zHi: single): TVec6;
implementation
function Vec6 (xLo, yLo, zLo, xHi, yHi, zHi: single): TVec6;
begin
result.xLo := xLo;
result.yLo := yLo;
result.zLo := zLo;
result.xHi := xHi;
result.yHi := yHi;
result.zHi := zHi;
end;
{$IFDEF Darwin}
function ResourceDir (): string;
begin
result := NSBundle.mainBundle.resourcePath.UTF8String;
end;
function ResourceURL (name: pchar; ofType: pchar): NSURL;
begin
result := NSBundle.mainBundle.URLForResource_withExtension(NSSTR(name), NSSTR(ofType));
end;
function ResourceFile (name: pchar; ofType: pchar): string;
var
url: NSURL;
begin
url := ResourceURL(name, ofType);
result := url.relativePath.UTF8String;
end;
{$ELSE}
function ResourceDir (): string;
begin
result := extractfilepath(paramstr(0))+'Resources';
end;
function ResourceFile (name: pchar; ofType: pchar): string;
begin
result := ResourceDir + pathdelim + name +'.'+ ofType;
end;
{$ENDIF}
function ScriptDir (): string;
begin
result := ResourceDir + pathdelim + 'script';
end;
function ShaderDir (): string;
begin
result := ResourceDir + pathdelim + 'shader';
end;
const
kError = 666;
kNote = 777;
kBool = 0;
kInt = 1;
kFloat = 2;
kSet = 3;
function StrToUniform(lS: string): TUniform;
var
lV: string;
lC: char;
lLen,lP,lN: integer;
begin
result.Name := '';
result.Hint := '';
result.Widget := kError;
lLen := length(lS);
//read values
lV := '';
lP := 1;
lN := 0;
while (lP <= lLen) do begin
if lS[lP] = '/' then begin
inc(lP);
continue;
end;
if lS[lP] <> '|' then
lV := lV + lS[lP];
if (lS[lP] = '|') or (lP = lLen) then begin
inc(lN);
case lN of
1: result.Name := lV;
2: begin
lC := upcase (lV[1]);
case lC of
'S' : result.Widget := kSet;
'B' : result.Widget := kBool;
'I' : result.Widget := kInt;
'F' : result.Widget := kFloat;
'N' : begin
result.Widget := kNote;
exit;
end;
else
writeln('Unkown uniform type :'+lV);
exit;
end;
end;
3: begin
if (result.Widget = kBool) {or (result.Widget = kSet)} then begin
result.bool := upcase (lV[1]) = 'T';
end else
result.min := strtofloatdef(lV,0);
end;
4: result.defaultv := strtofloatdef(lV,0);
5: result.max := strtofloatdef(lV,0);
6: result.Hint := lV;
end;
lV := '';
end;
inc(lP);
end;
end;
function loadShaderPrefs(shaderName: string): TShaderPrefs;
//load custom shader uniforms, between "//pref" and "//frag" tag
//pref
//ambient|float|0.0|1.0|2
//diffuse|float|0.0|0.3|2
//specular|float|0.0|0.25|2
//vert
//... rest of shader
const
knone=0;
kpref=1;
kvert = 2;
kfrag = 3;
var
mode: integer;
F : TextFile;
S: string;
U: TUniform;
begin
result.nUniform := 0;
if shaderName = '' then exit;
if not fileexists(shaderName) then begin
writeln('ShaderPrefs Unable to find '+ shaderName);
exit;
end;
mode := knone;
FileMode := fmOpenRead;
AssignFile(F,shaderName);
Reset(F);
while (not Eof(F)) and (mode <> kvert) and (mode <> kfrag) do begin
ReadLn(F, S);
if S = '//pref' then
mode := kpref
else if S = '//frag' then
mode := kfrag
else if S = '//vert' then
mode := kvert
else if mode = kpref then begin
//mode := kpref
//writeln('>>>>>>>>>>'+S);
U := StrToUniform(S);
if (U.Widget = kFloat)then begin
if (result.nUniform < kMaxUniform) then begin
inc(result.nUniform);
result.Uniform[result.nUniform] := U;
end else
writeln('Too many preferences');
end ;
end;
end;//EOF
CloseFile(F);
end;
procedure LoHi(var lo,hi: integer);
var
s: integer;
begin
if lo <= hi then exit;
s := hi;
hi := lo;
lo := s;
end;
procedure SortVec3i(var lo, hi: TVec3i);
begin
LoHi(lo.X, hi.X);
LoHi(lo.Y, hi.Y);
LoHi(lo.Z, hi.Z);
end;
function prod(v:TVec3i): int64;
begin
result := v.x * v.y * v.z;
end;
function pti(x,y,z: integer): TVec3i; //create integer vector
begin
result.X := x;
result.Y := y;
result.Z := z;
end; // pti()
function SetRGBA(r,g,b,a: byte): TRGBA; overload;
begin
result.r := r;
result.g := g;
result.b := b;
result.a := a;
end;
function SetRGBA(v:TVec4): TRGBA; overload;
begin
result.r := round(v.r *255);
result.g := round(v.g *255);
result.b := round(v.b *255);
result.a := round(v.a *255);
end;
end.