forked from red-prig/fpPS4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemit_fetch.pas
613 lines (526 loc) · 13.9 KB
/
emit_fetch.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
unit emit_fetch;
{$mode objfpc}{$H+}
interface
uses
ps4_pssl,
srNode,
srBitcast,
srType,
srTypes,
srConst,
srReg,
srOp,
srOpUtils,
srLayout,
srVertLayout,
srFragLayout,
srVariable,
srInput,
srOutput,
srUniform,
srFlow;
type
TEmitFetch=class(TEmitFlow)
//
function GroupingSharp(src:PPsrRegSlot;rtype:TsrResourceType):PsrDataLayout;
//
procedure PrepTypeSlot(pSlot:PsrRegSlot;rtype:TsrDataType);
function MakeRead(pSlot:PsrRegSlot;rtype:TsrDataType):PsrRegNode;
//
function PrepTypeNode(var node:PsrRegNode;rtype:TsrDataType;relax:Boolean=true):Integer;
function PrepTypeDst(var node:PsrRegNode;rtype:TsrDataType;relax:Boolean=true):Integer;
function PrepTypeParam(node:POpParamNode;rtype:TsrDataType;relax:Boolean=true):Integer;
//
function get_vcc0:PsrRegSlot;
function get_vcc1:PsrRegSlot;
function get_m0:PsrRegSlot;
function get_exec0:PsrRegSlot;
function get_exec1:PsrRegSlot;
function get_scc:PsrRegSlot;
//
function get_sdst7(SDST:Byte):PsrRegSlot;
function get_sdst7_pair(SDST:Byte;dst:PPsrRegSlot):Boolean;
function get_ssrc8(SSRC:Byte):PsrRegSlot;
function get_ssrc9(SSRC:Word):PsrRegSlot;
function get_ssrc9_pair(SSRC:Word;src:PPsrRegSlot):Boolean;
function get_vsrc8(VSRC:Byte):PsrRegSlot;
function get_vdst8(VDST:Byte):PsrRegSlot;
function get_sbase(SBASE,count:Byte;src:PPsrRegSlot):Boolean;
function get_srsrc(SRSRC,count:Byte;src:PPsrRegSlot):Boolean;
//
function get_snapshot:TsrRegsSnapshot;
//
function fetch_ssrc8(SOFFSET:Word;rtype:TsrDataType):PsrRegNode;
function fetch_ssrc9(SSRC:Word;rtype:TsrDataType):PsrRegNode;
function fetch_ssrc9_pair(SSRC:Word;src:PPsrRegNode;rtype:TsrDataType):Boolean;
function fetch_vsrc8(VSRC:Word;rtype:TsrDataType):PsrRegNode;
function fetch_vdst8(VDST:Word;rtype:TsrDataType):PsrRegNode;
procedure fetch_vsrc8_vec2h(VSRC:Word;var dst0,dst1:PsrRegNode);
//
procedure OpCmpV(OpId:DWORD;dst:PsrRegSlot;src0,src1:PsrRegNode);
procedure OpCmpS(OpId:DWORD;dst:PsrRegSlot;src0,src1:PsrRegNode);
procedure OpConvFloatToHalf2(dst:PsrRegSlot;src0,src1:PsrRegNode);
//
function AddInput(dst:PsrRegSlot;rtype:TsrDataType;itype:TpsslInputType;id:Byte=0):PsrRegNode;
function AddAncillary(dst:PsrRegSlot):PsrRegNode;
function AddVertLayout(pLayout:PsrDataLayout;rtype:TsrDataType):PsrRegNode;
function AddFragLayout(itype:TpsslInputType;rtype:TsrDataType;location:DWORD):PsrRegNode;
function FetchLoad(pChain:PsrChain;rtype:TsrDataType):PsrRegNode;
Procedure FetchStore(pChain:PsrChain;src:PsrRegNode);
procedure AddUserdata(dst:PsrRegSlot;offset_dw:Byte);
function FetchChain(grp:PsrDataLayout;lvl_0:PsrChainLvl_0;lvl_1:PsrChainLvl_1):PsrRegNode;
function MakeChain(pSlot:PsrRegSlot;grp:PsrDataLayout;lvl_0:PsrChainLvl_0;lvl_1:PsrChainLvl_1):PsrRegNode;
Procedure AddVecInput(dst:PsrRegSlot;vtype,rtype:TsrDataType;itype:TpsslInputType;id:Byte);
function FetchUniform(src:PsrDataLayout;pType:PsrType):PsrNode;
function FetchImage(src:PsrDataLayout;dtype:TsrDataType;info:TsrTypeImageInfo):PsrNode;
function FetchSampler(src:PsrDataLayout):PsrNode;
function FetchOutput(etype:TpsslExportType;rtype:TsrDataType):PsrVariable;
end;
function GetInputRegNode(node:PsrRegNode):PsrInput;
implementation
function GetInputRegNode(node:PsrRegNode):PsrInput;
var
pSource:PsrNode;
begin
pSource:=GetSourceRegNode(node);
Result:=pSource^.AsType(ntInput);
end;
//
function TEmitFetch.GroupingSharp(src:PPsrRegSlot;rtype:TsrResourceType):PsrDataLayout;
var
chain:TsrChains;
i,n:Byte;
begin
Result:=nil;
chain:=Default(TsrChains);
n:=GetResourceSizeDw(rtype);
For i:=0 to n-1 do
begin
chain[i]:=GetChainRegNode(src[i]^.current);
end;
Result:=DataLayoutList.Grouping(chain,rtype);
end;
//
procedure TEmitFetch.PrepTypeSlot(pSlot:PsrRegSlot;rtype:TsrDataType);
begin
if (pSlot=nil) then Exit;
if (pSlot^.current=nil) then
begin
pSlot^.New(line,rtype); //Unresolve
Exit;
end;
pSlot^.current^.PrepType(ord(rtype));
end;
function TEmitFetch.MakeRead(pSlot:PsrRegSlot;rtype:TsrDataType):PsrRegNode;
var
node:PsrRegNode;
begin
Result:=nil;
if (pSlot=nil) then Exit;
PrepTypeSlot(pSlot,rtype);
node:=pSlot^.current;
if (rtype<>dtUnknow) and (not CompareType(node^.dtype,rtype)) then
begin
Result:=BitcastList.FetchRead(rtype,node);
end else
begin
Result:=node;
end;
if (rtype<>dtUnknow) then
begin
Result^.Weak:=False;
end;
end;
//
function TEmitFetch.PrepTypeNode(var node:PsrRegNode;rtype:TsrDataType;relax:Boolean=true):Integer;
begin
Result:=0;
if (node=nil) then Exit;
if (rtype=dtUnknow) then Exit;
if is_unprep_type(node^.dtype,rtype,node^.Weak) then
begin
node^.PrepType(ord(rtype));
Inc(Result);
end else
begin
Case relax of
True :relax:=CompareType(node^.dtype,rtype);
False:relax:=(node^.dtype=rtype);
end;
if not relax then
begin
node:=BitcastList.FetchRead(rtype,node);
Inc(Result);
end;
end;
end;
function TEmitFetch.PrepTypeDst(var node:PsrRegNode;rtype:TsrDataType;relax:Boolean=true):Integer;
begin
Result:=0;
if (node=nil) then Exit;
if (rtype=dtUnknow) then Exit;
if is_unprep_type(node^.dtype,rtype,node^.Weak) then
begin
node^.PrepType(ord(rtype));
Inc(Result);
end else
begin
Case relax of
True :relax:=CompareType(node^.dtype,rtype);
False:relax:=(node^.dtype=rtype);
end;
if not relax then
begin
node:=BitcastList.FetchDstr(rtype,node);
Inc(Result);
end;
end;
end;
function TEmitFetch.PrepTypeParam(node:POpParamNode;rtype:TsrDataType;relax:Boolean=true):Integer;
var
pReg:PsrRegNode;
begin
Result:=0;
if (node=nil) then Exit;
if (rtype=dtUnknow) then Exit;
if node^.Value^.IsType(ntReg) then
begin
pReg:=node^.AsReg;
Result:=PrepTypeNode(pReg,rtype,relax);
node^.Value:=pReg;
end else
begin
node^.Value^.PrepType(ord(rtype));
end;
end;
//
function TEmitFetch.get_vcc0:PsrRegSlot;
begin
Result:[email protected][0];
end;
function TEmitFetch.get_vcc1:PsrRegSlot;
begin
Result:[email protected][1];
end;
function TEmitFetch.get_m0:PsrRegSlot;
begin
Result:[email protected];
end;
function TEmitFetch.get_exec0:PsrRegSlot;
begin
Result:[email protected][0];
end;
function TEmitFetch.get_exec1:PsrRegSlot;
begin
Result:[email protected][1];
end;
function TEmitFetch.get_scc:PsrRegSlot;
begin
Result:[email protected];
end;
//
function TEmitFetch.get_sdst7(SDST:Byte):PsrRegSlot;
begin
Result:=RegsStory.get_sdst7(SDST);
end;
function TEmitFetch.get_sdst7_pair(SDST:Byte;dst:PPsrRegSlot):Boolean;
begin
Result:=RegsStory.get_sdst7_pair(SDST,dst);
end;
function TEmitFetch.get_ssrc8(SSRC:Byte):PsrRegSlot;
begin
Result:=RegsStory.get_ssrc8(SSRC);
end;
function TEmitFetch.get_ssrc9(SSRC:Word):PsrRegSlot;
begin
Result:=RegsStory.get_ssrc9(SSRC);
end;
function TEmitFetch.get_ssrc9_pair(SSRC:Word;src:PPsrRegSlot):Boolean;
begin
Result:=RegsStory.get_ssrc9_pair(SSRC,src);
end;
function TEmitFetch.get_vsrc8(VSRC:Byte):PsrRegSlot;
begin
Result:=RegsStory.get_vsrc8(VSRC);
end;
function TEmitFetch.get_vdst8(VDST:Byte):PsrRegSlot;
begin
Result:=RegsStory.get_vdst8(VDST);
end;
function TEmitFetch.get_sbase(SBASE,count:Byte;src:PPsrRegSlot):Boolean;
begin
Result:=RegsStory.get_sbase(SBASE,count,src);
end;
function TEmitFetch.get_srsrc(SRSRC,count:Byte;src:PPsrRegSlot):Boolean;
begin
Result:=RegsStory.get_srsrc(SRSRC,count,src);
end;
//
function TEmitFetch.get_snapshot:TsrRegsSnapshot;
begin
Result:=RegsStory.get_snapshot;
end;
//
function TEmitFetch.fetch_ssrc8(SOFFSET:Word;rtype:TsrDataType):PsrRegNode;
Var
src:PsrRegSlot;
pConst:PsrConst;
begin
if is_const_ssrc8(SOFFSET) then
begin
pConst:=ConstList.Fetch_ssrc8_const(SOFFSET,0,rtype);
Result:=NewReg(pConst);
end else
begin
src:=RegsStory.get_ssrc8(SOFFSET);
Result:=MakeRead(src,rtype);
end;
end;
function TEmitFetch.fetch_ssrc9(SSRC:Word;rtype:TsrDataType):PsrRegNode;
Var
src:PsrRegSlot;
pConst:PsrConst;
begin
if is_const_ssrc9(SSRC) then
begin
pConst:=ConstList.Fetch_ssrc9_const(SSRC,FSPI.INLINE32,rtype);
Result:=NewReg(pConst);
end else
begin
src:=RegsStory.get_ssrc9(SSRC);
Result:=MakeRead(src,rtype);
end;
end;
function TEmitFetch.fetch_ssrc9_pair(SSRC:Word;src:PPsrRegNode;rtype:TsrDataType):Boolean;
Var
pSlot:array[0..1] of PsrRegSlot;
begin
if is_const_ssrc9(SSRC) then
begin
src[0]:=NewReg(ConstList.Fetch_ssrc9_const(SSRC,FSPI.INLINE32,rtype));
src[1]:=NewReg(ConstList.Fetch(dtUnknow,0));
Result:=True;
end else
begin
pSlot[0]:=nil;
pSlot[1]:=nil;
Result:=RegsStory.get_ssrc9_pair(SSRC,pSlot);
if not Result then Exit;
src[0]:=MakeRead(pSlot[0],rtype);
src[1]:=MakeRead(pSlot[1],rtype);
end;
end;
function TEmitFetch.fetch_vsrc8(VSRC:Word;rtype:TsrDataType):PsrRegNode;
var
src:PsrRegSlot;
begin
src:=RegsStory.get_vsrc8(VSRC);
Result:=MakeRead(src,rtype);
end;
function TEmitFetch.fetch_vdst8(VDST:Word;rtype:TsrDataType):PsrRegNode;
var
src:PsrRegSlot;
begin
src:=RegsStory.get_vdst8(VDST);
Result:=MakeRead(src,rtype);
end;
procedure TEmitFetch.fetch_vsrc8_vec2h(VSRC:Word;var dst0,dst1:PsrRegNode);
var
pSlot:PsrRegSlot;
dst:PsrRegNode;
begin
pSlot:=RegsStory.get_vsrc8(VSRC);
dst:=MakeRead(pSlot,dtVec2h);
dst0:=NewReg(dtHalf16);
dst1:=NewReg(dtHalf16);;
OpExtract(line,dst0,dst,0);
OpExtract(line,dst1,dst,1);
end;
//
procedure TEmitFetch.OpCmpV(OpId:DWORD;dst:PsrRegSlot;src0,src1:PsrRegNode);
Var
tmp:PsrRegNode;
exc:PsrRegNode;
begin
tmp:=NewReg(dtBool);
_Op2(line,OpId,tmp,src0,src1);
exc:=MakeRead(get_exec0,dtBool);
OpLogicalAnd(dst,tmp,exc);
end;
procedure TEmitFetch.OpCmpS(OpId:DWORD;dst:PsrRegSlot;src0,src1:PsrRegNode);
begin
Op2(OpId,dtBool,dst,src0,src1);
end;
procedure TEmitFetch.OpConvFloatToHalf2(dst:PsrRegSlot;src0,src1:PsrRegNode);
var
rsl:array[0..1] of PsrRegNode;
begin
rsl[0]:=OpFToF(src0,dtHalf16);
rsl[1]:=OpFToF(src1,dtHalf16);
dst^.New(line,dtVec2h);
OpMakeCon(line,dst^.current,@rsl);
end;
//
function TEmitFetch.AddInput(dst:PsrRegSlot;rtype:TsrDataType;itype:TpsslInputType;id:Byte=0):PsrRegNode;
var
i:PsrInput;
v:PsrVariable;
r:PsrRegNode;
begin
i:=InputList.Fetch(rtype,itype,id);
v:=i^.pVar;
r:=i^.pReg;
if (r=nil) then
begin
r:=dst^.New(line,rtype);
i^.pReg :=r;
OpLoad(init_line,r,v); //one in any scope
end else
if (dst^.current<>r) then
begin
MakeCopy(dst,r);
end;
Result:=r;
end;
function TEmitFetch.AddAncillary(dst:PsrRegSlot):PsrRegNode;
var
src:array[0..2] of PsrRegNode;
begin
src[0]:=NewReg_q(dtUInt32,0); //vPrimType
src[1]:=AddInput(@RegsStory.FUnattach,dtUInt32,itSampleId);
src[2]:=AddInput(@RegsStory.FUnattach,dtUInt32,itLayer);
OpPackAnc(dst,src[0],src[1],src[2]);
//Render target array index[26:16], BuiltIn.Layer
//Iterated sample number[11:8], BuiltIn.SampleId
//Primitive type[1:0] 0..3
//IL_REGTYPE_PRIMTYPE vPrimType
//The first component has sign bit = 1, this is a point. Values in other bits are undefined.
//The second component has sign bit = 1, this is a line. Values in other bits are undefined.
//PID ISID RTAI
//01|234567|89AB|CDEF|0123456789A|
//00|000000| |0000| |
Result:=dst^.current;
end;
function TEmitFetch.AddVertLayout(pLayout:PsrDataLayout;rtype:TsrDataType):PsrRegNode;
var
i:PsrVertLayout;
v:PsrVariable;
dst:PsrRegNode;
begin
i:=VertLayoutList.Fetch(pLayout,rtype);
v:=i^.pVar;
dst:=i^.pReg;
if (dst=nil) then
begin
dst:=NewReg(rtype);
i^.pReg:=dst;
OpLoad(init_line,dst,v); //one in any scope
end;
Result:=dst;
end;
function TEmitFetch.AddFragLayout(itype:TpsslInputType;rtype:TsrDataType;location:DWORD):PsrRegNode;
var
i:PsrFragLayout;
v:PsrVariable;
dst:PsrRegNode;
begin
i:=FragLayoutList.Fetch(itype,location,rtype);
v:=i^.pVar;
dst:=i^.pReg;
if (dst=nil) then
begin
dst:=NewReg(rtype);
i^.pReg:=dst;
OpLoad(init_line,dst,v); //one in any scope
end;
Result:=dst;
end;
function TEmitFetch.FetchLoad(pChain:PsrChain;rtype:TsrDataType):PsrRegNode;
begin
Result:=nil;
if (pChain=nil) then Exit;
Result:=NewReg(rtype);
pChain^.FetchLoad(line,Result);
end;
Procedure TEmitFetch.FetchStore(pChain:PsrChain;src:PsrRegNode);
begin
if (pChain=nil) or (src=nil) then Exit;
pChain^.FetchStore(line,src);
end;
procedure TEmitFetch.AddUserdata(dst:PsrRegSlot;offset_dw:Byte);
var
lvl_0:TsrChainLvl_0;
pLayout:PsrDataLayout;
pChain:PsrChain;
pReg:PsrRegNode;
begin
pLayout:=DataLayoutList.pRoot;
lvl_0.offset:=offset_dw*4;
lvl_0.size :=4;
pChain:=pLayout^.Fetch(@lvl_0,nil);
pReg:=FetchLoad(pChain,dtUnknow);
MakeCopy(dst,pReg);
end;
function TEmitFetch.FetchChain(grp:PsrDataLayout;lvl_0:PsrChainLvl_0;lvl_1:PsrChainLvl_1):PsrRegNode;
var
pChain:PsrChain;
begin
pChain:=grp^.Fetch(lvl_0,lvl_1);
Result:=FetchLoad(pChain,dtUnknow);
end;
function TEmitFetch.MakeChain(pSlot:PsrRegSlot;grp:PsrDataLayout;lvl_0:PsrChainLvl_0;lvl_1:PsrChainLvl_1):PsrRegNode;
var
pChain:PsrChain;
pReg:PsrRegNode;
begin
pChain:=grp^.Fetch(lvl_0,lvl_1);
pReg:=FetchLoad(pChain,dtUnknow);
MakeCopy(pSlot,pReg);
Result:=pSlot^.current;
end;
Procedure TEmitFetch.AddVecInput(dst:PsrRegSlot;vtype,rtype:TsrDataType;itype:TpsslInputType;id:Byte);
var
rsl:PsrRegNode;
begin
rsl:=AddInput(@RegsStory.FUnattach,vtype,itype,0);
OpExtract(init_line,dst^.New(line,rtype),rsl,id);
end;
////
function TEmitFetch.FetchUniform(src:PsrDataLayout;pType:PsrType):PsrNode;
var
u:PsrUniform;
v:PsrVariable;
r:PsrRegUniform;
begin
u:=UniformList.Fetch(src,pType);
v:=u^.pVar;
r:=u^.pReg;
if (r^.pLine=nil) then
begin
OpLoad(init_line,pType,r,v); //one in any scope
end;
Result:=r;
end;
function TEmitFetch.FetchImage(src:PsrDataLayout;dtype:TsrDataType;info:TsrTypeImageInfo):PsrNode;
var
pType:PsrType;
begin
pType:=TypeList.Fetch(dtype);
pType:=TypeList.FetchImage(pType,info);
Result:=FetchUniform(src,pType);
end;
function TEmitFetch.FetchSampler(src:PsrDataLayout):PsrNode;
var
pType:PsrType;
begin
pType:=TypeList.Fetch(dtTypeSampler);
Result:=FetchUniform(src,pType);
end;
function TEmitFetch.FetchOutput(etype:TpsslExportType;rtype:TsrDataType):PsrVariable;
var
o:PsrOutput;
begin
o:=OutputList.Fetch(etype,rtype);
Result:=o^.pVar;
end;
end.