fpPS4/spirv/srBuffer.pas

1106 lines
21 KiB
ObjectPascal
Raw Normal View History

2022-05-31 07:17:14 +00:00
unit srBuffer;
{$mode ObjFPC}{$H+}
interface
uses
2022-09-05 13:30:24 +00:00
sysutils,
spirv,
ginodes,
srNode,
srType,
srTypes,
2022-09-07 18:06:19 +00:00
srReg,
2022-09-05 13:30:24 +00:00
srVariable,
srLayout,
srDecorate,
srConfig;
2022-05-31 07:17:14 +00:00
type
2022-09-05 13:30:24 +00:00
ntBuffer=class(ntDescriptor)
2022-09-07 18:06:19 +00:00
class Procedure add_read (node,src:PsrNode); override;
class Procedure rem_read (node,src:PsrNode); override;
//
2022-09-05 13:30:24 +00:00
class Function pwrite_count (node:PsrNode):PDWORD; override;
class function GetStorageName(node:PsrNode):RawByteString; override;
end;
2022-05-31 07:17:14 +00:00
PsrBuffer=^TsrBuffer;
PsrField=^TsrField;
TsrFieldValue=(frNotFit,frIdent,frVectorAsValue,frValueInVector,frValueInArray);
TFieldFetchValue=record
fValue:TsrFieldValue;
pField:PsrField;
end;
TFieldEnumCb=procedure(pField:PsrField) of object;
TsrField=object
type
TFieldFetch=specialize TNodeFetch<PsrField,TsrField>;
var
pLeft,pRight:PsrField;
//----
pBuffer:PsrBuffer;
2022-09-05 13:30:24 +00:00
pParent:PsrField;
2022-05-31 07:17:14 +00:00
offset:PtrUint;
size:PtrUint;
stride:PtrUint;
FCount:PtrUint;
dtype:TsrDataType;
2022-11-15 12:54:47 +00:00
sType,pType:PsrType;
2022-05-31 07:17:14 +00:00
FID:Integer; //alloc late
//
FList:TFieldFetch;
function c(n1,n2:PsrField):Integer; static;
function Cross(o,s:PtrUint):Boolean;
function Find_be(o:PtrUint):PsrField;
function Find_le(o:PtrUint):PsrField;
function First:PsrField;
function Last:PsrField;
function Next(p:PsrField):PsrField;
function Prev(p:PsrField):PsrField;
function Fetch(o:PtrUint):PsrField;
function FetchValue(_offset,_size:PtrUint;_dtype:TsrDataType):TFieldFetchValue;
function FetchRuntimeArray(_offset,_stride:PtrUint):TFieldFetchValue;
function IsStructUsedRuntimeArray:Boolean;
2022-11-15 12:54:47 +00:00
function IsStructNotUsed:Boolean;
function IsTop:Boolean;
2022-05-31 07:17:14 +00:00
function GetStructDecorate:DWORD;
2022-09-05 13:30:24 +00:00
procedure UpdateSize;
function GetSize:PtrUint;
2022-05-31 07:17:14 +00:00
procedure FillNode(o,s:PtrUint);
function FillSpace:Integer;
end;
TsrBufferType=(btStorageBuffer,btUniformBuffer,btPushConstant);
2022-09-05 13:30:24 +00:00
TsrBuffer=packed object(TsrDescriptor)
2022-05-31 07:17:14 +00:00
pLeft,pRight:PsrBuffer;
//----
2022-09-05 13:30:24 +00:00
fwrite_count:DWORD;
//
align_offset:DWORD;
2022-05-31 07:17:14 +00:00
2022-09-05 13:30:24 +00:00
FEmit:TCustomEmit;
2022-09-07 18:06:19 +00:00
FDList:TRegDNodeList;
2022-05-31 07:17:14 +00:00
bType:TsrBufferType;
2022-09-05 13:30:24 +00:00
key:packed record
pLayout:PsrDataLayout;
AliasId:PtrInt;
end;
2022-05-31 07:17:14 +00:00
FTop:TsrField;
2022-09-05 13:30:24 +00:00
2022-05-31 07:17:14 +00:00
function c(n1,n2:PsrBuffer):Integer; static;
2022-09-05 13:30:24 +00:00
Procedure Init(Emit:TCustomEmit); inline;
2022-09-07 18:06:19 +00:00
Procedure AddDep(t:PsrNode);
Procedure RemDep(t:PsrNode);
function chain_read :DWORD;
function chain_write:DWORD;
2022-09-05 13:30:24 +00:00
function GetStorageName:RawByteString;
2022-05-31 07:17:14 +00:00
function GetTypeChar:Char;
function GetString:RawByteString;
function GetStructName:RawByteString;
function GetSize:PtrUint;
procedure EnumAllField(cb:TFieldEnumCb);
procedure ShiftOffset(Offset:PtrUint);
end;
2022-09-05 13:30:24 +00:00
PsrBufferList=^TsrBufferList;
2022-05-31 07:17:14 +00:00
TsrBufferList=object
type
TNodeFetch=specialize TNodeFetch<PsrBuffer,TsrBuffer>;
var
2022-09-05 13:30:24 +00:00
FEmit:TCustomEmit;
2022-05-31 07:17:14 +00:00
FNTree:TNodeFetch;
FPushConstant:PsrBuffer;
2022-09-05 13:30:24 +00:00
procedure Init(Emit:TCustomEmit); inline;
2022-05-31 07:17:14 +00:00
function Fetch(s:PsrDataLayout;n:PtrInt):PsrBuffer;
2022-09-05 13:30:24 +00:00
function NextAlias(buf:PsrBuffer):PsrBuffer;
2022-05-31 07:17:14 +00:00
Function First:PsrBuffer;
Function Next(node:PsrBuffer):PsrBuffer;
procedure EnumAllField(cb:TFieldEnumCb);
procedure OnFillSpace(node:PsrField);
procedure FillSpace;
2022-09-05 13:30:24 +00:00
procedure OnAllocID(pField:PsrField);
2022-05-31 07:17:14 +00:00
procedure AllocID;
2022-09-05 13:30:24 +00:00
procedure AllocBinding(Var FBinding:Integer);
procedure AllocSourceExtension;
function FindUserDataBuf:PsrBuffer;
2022-05-31 07:17:14 +00:00
procedure ApplyBufferType;
procedure AlignOffset(node:PsrBuffer;offset:PtrUint);
procedure AlignOffset;
2022-09-05 13:30:24 +00:00
procedure OnAllocTypeBinding(pField:PsrField);
procedure AllocTypeBinding;
procedure AllocName;
2022-05-31 07:17:14 +00:00
end;
implementation
2022-09-07 18:06:19 +00:00
class Procedure ntBuffer.add_read(node,src:PsrNode);
begin
inherited;
if src^.IsType(ntChain) then
begin
PsrBuffer(node)^.AddDep(src);
end;
end;
class Procedure ntBuffer.rem_read(node,src:PsrNode);
begin
inherited;
if src^.IsType(ntChain) then
begin
PsrBuffer(node)^.RemDep(src);
end;
end;
//
2022-09-05 13:30:24 +00:00
class Function ntBuffer.pwrite_count(node:PsrNode):PDWORD;
begin
Result:=@PsrBuffer(node)^.fwrite_count;
end;
class function ntBuffer.GetStorageName(node:PsrNode):RawByteString;
begin
Result:=PsrBuffer(node)^.GetStorageName;
end;
2022-05-31 07:17:14 +00:00
//---
function TsrField.c(n1,n2:PsrField):Integer;
begin
Result:=Integer(n1^.offset>n2^.offset)-Integer(n1^.offset<n2^.offset);
end;
function TsrField.Cross(o,s:PtrUint):Boolean;
begin
Result:=((o>=offset) and (o<offset+size)) or
((offset>=o) and (offset<o+s));
end;
function TsrField.Find_be(o:PtrUint):PsrField;
var
node:TsrField;
begin
node:=Default(TsrField);
node.offset:=o;
Result:=FList.Find_be(@node);
end;
function TsrField.Find_le(o:PtrUint):PsrField;
var
node:TsrField;
begin
node:=Default(TsrField);
node.offset:=o;
Result:=FList.Find_le(@node);
end;
function TsrField.First:PsrField;
begin
Result:=FList.Min;
end;
function TsrField.Last:PsrField;
begin
Result:=FList.Max;
end;
function TsrField.Next(p:PsrField):PsrField;
begin
Result:=FList.Next(p);
end;
function TsrField.Prev(p:PsrField):PsrField;
begin
Result:=FList.Prev(p);
end;
function TsrField.Fetch(o:PtrUint):PsrField;
var
node:TsrField;
begin
node:=Default(TsrField);
node.offset:=o;
Result:=FList.Find(@node);
if (Result=nil) then
begin
2022-09-05 13:30:24 +00:00
Assert(pBuffer^.FEmit<>nil);
Result:=pBuffer^.FEmit.Alloc(SizeOf(TsrField));
2022-05-31 07:17:14 +00:00
Result^.pBuffer:=pBuffer;
2022-09-05 13:30:24 +00:00
Result^.pParent:=@Self;
Result^.offset :=o;
Result^.FID :=-1;
2022-05-31 07:17:14 +00:00
FList.Insert(Result);
Inc(FCount);
end;
end;
function TsrField.FetchValue(_offset,_size:PtrUint;_dtype:TsrDataType):TFieldFetchValue;
var
node:PsrField;
_stride:PtrUint;
begin
Result:=Default(TFieldFetchValue);
_stride:=0;
2022-09-05 13:30:24 +00:00
if _dtype.isVector then
2022-05-31 07:17:14 +00:00
begin
2022-09-05 13:30:24 +00:00
_stride:=_dtype.Child.BitSize div 8;
2022-05-31 07:17:14 +00:00
end;
2022-11-15 12:54:47 +00:00
Assert(_size=(_dtype.BitSize div 8));
2022-05-31 07:17:14 +00:00
node:=Find_le(_offset);
if (node<>nil) then
begin
if not node^.Cross(_offset,_size) then
begin
node:=Next(node);
if (node<>nil) then
begin
if not node^.Cross(_offset,_size) then
begin
node:=nil;
end;
end;
end;
end;
if (node=nil) then
begin
//new
node:=Fetch(_offset);
node^.size :=_size;
node^.stride:=_stride;
node^.dtype :=_dtype;
Result.fValue:=frIdent;
Result.pField:=node;
end else
Case node^.dtype of
dtTypeArray:
begin
if (node^.offset>_offset) then Exit; //ident or big than
if (node^.offset+node^.size<_offset+_size) then Exit;
if (node^.stride<_size) then Exit; //ident or min stride
Result.fValue:=frValueInArray;
Result.pField:=node;
end;
dtTypeRuntimeArray:
begin
if (node^.offset>_offset) then Exit; //ident or big than
if (node^.stride<_size) then Exit; //ident or min stride
Result.fValue:=frValueInArray;
Result.pField:=node;
2022-11-05 14:48:13 +00:00
Result.fValue:=frNotFit;
2022-05-31 07:17:14 +00:00
end;
else
begin
2022-09-05 13:30:24 +00:00
if node^.dtype.isVector then
2022-05-31 07:17:14 +00:00
begin //ftVector
2022-09-05 13:30:24 +00:00
if _dtype.isVector then
2022-05-31 07:17:14 +00:00
begin
if (node^.offset=_offset) and
(node^.size =_size) and
(node^.stride=_stride) then
begin
Result.fValue:=frIdent; //ident
Result.pField:=node;
end;
end else
begin
if (node^.offset>_offset) then Exit; //ident or big than
_offset:=_offset-node^.offset;
if (node^.stride=_size) and
(_offset mod node^.stride=0) then
begin
Result.fValue:=frValueInVector; //down to vector
Result.pField:=node;
end;
end;
end else
begin //ftValue
2022-09-05 13:30:24 +00:00
if _dtype.isVector then
2022-05-31 07:17:14 +00:00
begin
if (node^.offset=_offset) and
(node^.size =_size) then
begin
Result.fValue:=frVectorAsValue; //vector as value?
Result.pField:=node;
end;
end else
begin
if (node^.offset=_offset) and
(node^.size =_size) then
begin
Result.fValue:=frIdent; //ident
Result.pField:=node;
end;
end;
end;
end;
end;
end;
function TsrField.FetchRuntimeArray(_offset,_stride:PtrUint):TFieldFetchValue;
var
node:PsrField;
begin
Result:=Default(TFieldFetchValue);
2022-11-05 14:48:13 +00:00
node:=Find_be(_offset); //RA only last item
if (node<>nil) then
begin
if (node^.offset>_offset) or
(node^.dtype<>dtTypeRuntimeArray) or
(node^.stride<>_stride) then
begin
Result.fValue:=frNotFit;
Result.pField:=nil;
Exit;
end;
end;
2022-05-31 07:17:14 +00:00
node:=Find_le(_offset);
if (node=nil) then
begin
//new
node:=Fetch(_offset);
node^.size :=High(PtrUint)-_offset;
node^.stride :=_stride;
node^.dtype :=dtTypeRuntimeArray;
Result.fValue:=frValueInArray;
Result.pField:=node;
end else
if (node^.stride=_stride) and
(node^.dtype =dtTypeRuntimeArray) and
(node^.offset<=_offset) then //ident or big than
begin
Result.fValue:=frValueInArray;
Result.pField:=node;
end;
end;
function TsrField.IsStructUsedRuntimeArray:Boolean;
var
node:PsrField;
begin
Result:=False;
if (dtype=dtTypeStruct) then
begin
node:=FList.Max;
if (node<>nil) then
begin
size:=node^.offset+node^.size;
end;
Result:=(size=High(PtrUint));
end;
end;
2022-11-15 12:54:47 +00:00
function TsrField.IsStructNotUsed:Boolean;
var
child:PsrField;
2022-05-31 07:17:14 +00:00
begin
2022-11-15 12:54:47 +00:00
Result:=False;
if IsTop then Exit;
if (FCount>1) then Exit;
child:=First;
if (child=nil) then Exit;
if (child^.offset<>0) then Exit;
if (child^.size<>stride) then Exit;
Result:=True;
2022-09-05 13:30:24 +00:00
end;
2022-11-15 12:54:47 +00:00
function TsrField.IsTop:Boolean;
2022-09-05 13:30:24 +00:00
begin
Result:=(pParent=nil);
2022-05-31 07:17:14 +00:00
end;
function TsrField.GetStructDecorate:DWORD;
begin
Result:=DWORD(-1); //dont use
2022-09-05 13:30:24 +00:00
if IsTop then
2022-05-31 07:17:14 +00:00
if (dtype=dtTypeStruct) then //is struct
begin
if (pBuffer^.FStorage<>StorageClass.StorageBuffer) and
(pBuffer^.bType=btStorageBuffer) then
begin
Result:=Decoration.BufferBlock;
end else
begin
Result:=Decoration.Block;
end;
end;
end;
2022-09-05 13:30:24 +00:00
procedure TsrField.UpdateSize;
var
node:PsrField;
begin
node:=FList.Max;
if (node<>nil) then
begin
size:=node^.offset+node^.size;
end;
end;
function TsrField.GetSize:PtrUint;
begin
UpdateSize;
Result:=size;
end;
2022-05-31 07:17:14 +00:00
procedure TsrField.FillNode(o,s:PtrUint);
procedure _Pad(p,v:PtrUint;_dtype:TsrDataType); //inline;
var
node:PsrField;
begin
if (o mod p<>0) and (s>=v) then
begin
node:=Fetch(o);
Assert(node^.dtype=dtUnknow,'WTF');
node^.size:=v;
node^.dtype:=_dtype;
o:=o+v;
s:=s-v;
end;
end;
procedure _Fill(v:PtrUint;_dtype:TsrDataType); //inline;
var
count:PtrUint;
node:PsrField;
begin
count:=s div v;
While (count<>0) do
begin
node:=Fetch(o);
Assert(node^.dtype=dtUnknow,'WTF');
node^.size:=v;
node^.dtype:=_dtype;
o:=o+v;
s:=s-v;
Dec(count);
end;
end;
begin
if (s=0) then Exit;
_Pad ( 2,1,dtUint8);
_Pad ( 4,2,dtHalf16);
_Pad ( 8,4,dtFloat32);
_Pad (16,8,dtVec2f);
_Fill(16,dtVec4f);
_Fill(8 ,dtVec2f);
_Fill(4 ,dtFloat32);
_Fill(2 ,dtHalf16);
_Fill(1 ,dtUint8);
end;
function TsrField.FillSpace:Integer;
var
pNode:PsrField;
Foffset,Fsize:PtrUint;
begin
Result:=0;
pNode:=First;
if (pNode=nil) then Exit;
Foffset:=0;
While (pNode<>nil) do
begin
2022-07-09 16:13:52 +00:00
if (pNode^.dtype=dtUnknow) then
begin
Case pNode^.size of
1:pNode^.dtype:=dtUint8;
2:pNode^.dtype:=dtHalf16;
4:pNode^.dtype:=dtFloat32;
8:pNode^.dtype:=dtVec2f;
16:pNode^.dtype:=dtVec4f;
else
Assert(false);
end;
end;
2022-05-31 07:17:14 +00:00
if (Foffset<pNode^.offset) then
begin
Fsize:=pNode^.offset-Foffset;
FillNode(Foffset,Fsize);
Inc(Result);
end;
Foffset:=pNode^.offset+pNode^.size;
pNode:=Next(pNode);
end;
if (stride<>0) and (dtype in [dtTypeArray,dtTypeRuntimeArray]) then
begin
pNode:=FList.Max;
if (pNode<>nil) then
begin
Foffset:=pNode^.offset+pNode^.size;
Assert(Foffset<=stride);
if (Foffset<stride) then
begin
Fsize:=stride-Foffset;
FillNode(Foffset,Fsize);
Inc(Result);
end;
end;
end;
end;
//--
function TsrBuffer.c(n1,n2:PsrBuffer):Integer;
begin
//first pLayout
Result:=Integer(n1^.key.pLayout>n2^.key.pLayout)-Integer(n1^.key.pLayout<n2^.key.pLayout);
if (Result<>0) then Exit;
//second CastNum
2022-09-05 13:30:24 +00:00
Result:=Integer(n1^.key.AliasId>n2^.key.AliasId)-Integer(n1^.key.AliasId<n2^.key.AliasId);
end;
Procedure TsrBuffer.Init(Emit:TCustomEmit); inline;
begin
FEmit:=Emit;
fntype :=ntBuffer;
bType :=btStorageBuffer;
FStorage:=StorageClass.Uniform;
FBinding:=-1;
FTop.FID:=-1;
FTop.dtype:=dtTypeStruct;
end;
2022-09-07 18:06:19 +00:00
Procedure TsrBuffer.AddDep(t:PsrNode);
var
pRegsStory:PsrRegsStory;
node:PRegDNode;
begin
if (t=nil) or (@Self=nil) then Exit;
pRegsStory:=FEmit.GetRegsStory;
node:=pRegsStory^.AllocDep;
node^.pNode:=t;
FDList.Push_head(node);
end;
Procedure TsrBuffer.RemDep(t:PsrNode);
var
pRegsStory:PsrRegsStory;
node,_prev:PRegDNode;
begin
if (t=nil) or (@Self=nil) then Exit;
node:=FDList.pHead;
_prev:=nil;
While (node<>nil) do
begin
if (node^.pNode=t) then
begin
if (_prev=nil) then
begin
FDList.pHead:=node^.pNext;
end else
begin
_prev^.pNext:=node^.pNext;
end;
pRegsStory:=FEmit.GetRegsStory;
pRegsStory^.FreeDep(node);
Exit;
end;
_prev:=node;
node:=node^.pNext;
end;
Assert(false,'not found!');
end;
function TsrBuffer.chain_read:DWORD;
var
node:PRegDNode;
begin
Result:=0;
node:=FDList.pHead;
While (node<>nil) do
begin
if node^.pNode^.IsType(ntChain) then
begin
Result:=Result+PsrChain(node^.pNode)^.read_count;
end;
node:=node^.pNext;
end;
end;
function TsrBuffer.chain_write:DWORD;
var
node:PRegDNode;
begin
Result:=0;
node:=FDList.pHead;
While (node<>nil) do
begin
if node^.pNode^.IsType(ntChain) then
begin
Result:=Result+PsrChain(node^.pNode)^.write_count;
end;
node:=node^.pNext;
end;
end;
2022-09-05 13:30:24 +00:00
function TsrBuffer.GetStorageName:RawByteString;
begin
Result:='';
Case bType of
btStorageBuffer:Result:='sBuf'+IntToStr(FBinding);
btUniformBuffer:Result:='uBuf'+IntToStr(FBinding);
btPushConstant :Result:='cBuf';
end;
2022-05-31 07:17:14 +00:00
end;
function TsrBuffer.GetTypeChar:Char;
begin
Result:=#0;
Case bType of
btStorageBuffer:Result:='S';
btUniformBuffer:Result:='U';
btPushConstant :Result:='P';
end;
end;
function TsrBuffer.GetString:RawByteString;
var
PID:DWORD;
begin
PID:=0;
if (key.pLayout<>nil) then
begin
PID:=key.pLayout^.FID;
end;
Result:='B'+GetTypeChar+
';PID='+HexStr(PID,8)+
';BND='+HexStr(FBinding,8)+
';LEN='+HexStr(GetSize,8)+
';OFS='+HexStr(align_offset,8);
end;
function TsrBuffer.GetStructName:RawByteString;
begin
Result:='TD'+HexStr(FBinding,8);
end;
2022-09-05 13:30:24 +00:00
function TsrBuffer.GetSize:PtrUint;
2022-05-31 07:17:14 +00:00
begin
2022-09-05 13:30:24 +00:00
Result:=FTop.GetSize;
2022-05-31 07:17:14 +00:00
end;
procedure TsrBuffer.EnumAllField(cb:TFieldEnumCb);
var
curr,node:PsrField;
begin
if (cb=nil) then Exit;
curr:=@FTop;
node:=curr^.First;
repeat
While (node<>nil) do
begin
2022-09-05 13:30:24 +00:00
if (node^.First<>nil) then //child exist
2022-05-31 07:17:14 +00:00
begin
curr:=node;
node:=curr^.First; //down
end else
begin
cb(node);
node:=curr^.Next(node);
end;
end;
cb(curr);
2022-09-05 13:30:24 +00:00
node:=curr;
curr:=curr^.pParent; //up
2022-05-31 07:17:14 +00:00
if (curr=nil) then Break;
node:=curr^.Next(node);
until false;
end;
procedure TsrBuffer.ShiftOffset(Offset:PtrUint);
var
node:PsrField;
begin
if (Offset=0) then Exit;
node:=FTop.Last;
While (node<>nil) do
begin
if (node^.offset+node^.size=High(PtrUint)) then
begin
node^.size:=node^.size-Offset;
end;
node^.offset:=node^.offset+Offset;
node:=FTop.Prev(node);
end;
end;
2022-09-05 13:30:24 +00:00
procedure TsrBufferList.Init(Emit:TCustomEmit); inline;
2022-05-31 07:17:14 +00:00
begin
2022-09-05 13:30:24 +00:00
FEmit:=Emit;
2022-05-31 07:17:14 +00:00
end;
function TsrBufferList.Fetch(s:PsrDataLayout;n:PtrInt):PsrBuffer;
var
node:TsrBuffer;
begin
node:=Default(TsrBuffer);
2022-09-05 13:30:24 +00:00
node.Init(FEmit);
2022-05-31 07:17:14 +00:00
node.key.pLayout:=s;
2022-09-05 13:30:24 +00:00
node.key.AliasId:=n;
2022-05-31 07:17:14 +00:00
Result:=FNTree.Find(@node);
if (Result=nil) then
begin
2022-09-05 13:30:24 +00:00
Result:=FEmit.Alloc(SizeOf(TsrBuffer));
Move(node,Result^,SizeOf(TsrBuffer));
2022-05-31 07:17:14 +00:00
Result^.FTop.pBuffer:=Result;
2022-09-05 13:30:24 +00:00
//
Result^.InitVar(FEmit);
//
2022-05-31 07:17:14 +00:00
FNTree.Insert(Result);
end;
end;
2022-09-05 13:30:24 +00:00
function TsrBufferList.NextAlias(buf:PsrBuffer):PsrBuffer;
2022-05-31 07:17:14 +00:00
begin
Result:=nil;
if (buf=nil) then Exit;
2022-09-05 13:30:24 +00:00
Result:=Fetch(buf^.key.pLayout,buf^.key.AliasId+1);
2022-05-31 07:17:14 +00:00
end;
Function TsrBufferList.First:PsrBuffer;
begin
Result:=FNTree.Min;
end;
Function TsrBufferList.Next(node:PsrBuffer):PsrBuffer;
begin
Result:=FNTree.Next(node);
end;
procedure TsrBufferList.EnumAllField(cb:TFieldEnumCb);
var
node:PsrBuffer;
begin
if (cb=nil) then Exit;
node:=First;
While (node<>nil) do
begin
2022-09-05 13:30:24 +00:00
if node^.IsUsed then
begin
node^.EnumAllField(cb);
end;
2022-05-31 07:17:14 +00:00
node:=Next(node);
end;
end;
procedure TsrBufferList.OnFillSpace(node:PsrField);
begin
node^.FillSpace;
end;
procedure TsrBufferList.FillSpace;
begin
EnumAllField(@OnFillSpace);
end;
2022-09-05 13:30:24 +00:00
procedure TsrBufferList.OnAllocID(pField:PsrField);
var
node:PsrField;
ID:Integer;
2022-05-31 07:17:14 +00:00
begin
2022-09-05 13:30:24 +00:00
ID:=0;
node:=pField^.First;
While (node<>nil) do
begin
if pField^.dtype.IsVector then
begin
ID:=node^.offset div pField^.stride;
node^.FID:=ID;
end else
begin
node^.FID:=ID;
Inc(ID);
end;
node:=pField^.Next(node);
end;
2022-05-31 07:17:14 +00:00
end;
procedure TsrBufferList.AllocID;
begin
EnumAllField(@OnAllocID);
end;
2022-09-05 13:30:24 +00:00
procedure TsrBufferList.AllocBinding(Var FBinding:Integer);
2022-05-31 07:17:14 +00:00
var
2022-09-05 13:30:24 +00:00
pConfig:PsrConfig;
pDecorateList:PsrDecorateList;
2022-05-31 07:17:14 +00:00
node:PsrBuffer;
pVar:PsrVariable;
begin
2022-09-05 13:30:24 +00:00
pConfig:=FEmit.GetConfig;
pDecorateList:=FEmit.GetDecorateList;
2022-05-31 07:17:14 +00:00
node:=First;
While (node<>nil) do
begin
pVar:=node^.pVar;
2022-09-05 13:30:24 +00:00
if (pVar<>nil) and node^.IsUsed then
if (node^.bType<>btPushConstant) then
if (node^.FBinding=-1) then //alloc
2022-05-31 07:17:14 +00:00
begin
2022-09-05 13:30:24 +00:00
pDecorateList^.OpDecorate(pVar,Decoration.Binding,FBinding);
pDecorateList^.OpDecorate(pVar,Decoration.DescriptorSet,pConfig^.DescriptorSet);
node^.FBinding:=FBinding;
Inc(FBinding);
2022-05-31 07:17:14 +00:00
end;
node:=Next(node);
end;
end;
2022-09-05 13:30:24 +00:00
procedure TsrBufferList.AllocSourceExtension;
2022-05-31 07:17:14 +00:00
var
2022-09-05 13:30:24 +00:00
pDebugInfoList:PsrDebugInfoList;
2022-05-31 07:17:14 +00:00
node:PsrBuffer;
pVar:PsrVariable;
begin
2022-09-05 13:30:24 +00:00
pDebugInfoList:=FEmit.GetDebugInfoList;
2022-05-31 07:17:14 +00:00
node:=First;
While (node<>nil) do
begin
pVar:=node^.pVar;
2022-09-05 13:30:24 +00:00
if (pVar<>nil) and node^.IsUsed then
2022-05-31 07:17:14 +00:00
begin
2022-11-15 12:54:47 +00:00
pDebugInfoList^.OpSource(node^.GetString);
2022-05-31 07:17:14 +00:00
end;
node:=Next(node);
end;
end;
2022-09-05 13:30:24 +00:00
function TsrBufferList.FindUserDataBuf:PsrBuffer;
var
node:PsrBuffer;
2022-05-31 07:17:14 +00:00
begin
2022-09-05 13:30:24 +00:00
Result:=nil;
node:=First;
While (node<>nil) do
begin
if node^.IsUsed then
begin
if node^.key.pLayout^.IsUserData then
begin
Exit(node);
end;
end;
node:=Next(node);
end;
2022-05-31 07:17:14 +00:00
end;
procedure TsrBufferList.ApplyBufferType;
var
2022-09-05 13:30:24 +00:00
pConfig:PsrConfig;
2022-05-31 07:17:14 +00:00
node:PsrBuffer;
2022-09-07 18:06:19 +00:00
fchain_write:DWORD;
2022-05-31 07:17:14 +00:00
begin
2022-09-05 13:30:24 +00:00
pConfig:=FEmit.GetConfig;
node:=FindUserDataBuf;
if (node<>nil) and (FPushConstant=nil) then
if (node^.write_count=0) and
(node^.GetSize<=pConfig^.maxPushConstantsSize) then
begin
node^.bType :=btPushConstant;
node^.FStorage:=StorageClass.PushConstant;
FPushConstant :=node;
end;
2022-05-31 07:17:14 +00:00
node:=First;
While (node<>nil) do
begin
2022-09-05 13:30:24 +00:00
if node^.IsUsed and (node^.bType=btStorageBuffer) then
2022-05-31 07:17:14 +00:00
begin
2022-09-07 18:06:19 +00:00
fchain_write:=node^.chain_write;
2022-05-31 07:17:14 +00:00
if (FPushConstant=nil) and
2022-09-07 18:06:19 +00:00
(fchain_write=0) and
2022-09-05 13:30:24 +00:00
(node^.GetSize<=pConfig^.maxPushConstantsSize) then
2022-05-31 07:17:14 +00:00
begin
node^.bType :=btPushConstant;
node^.FStorage:=StorageClass.PushConstant;
2022-09-05 13:30:24 +00:00
FPushConstant :=node;
2022-05-31 07:17:14 +00:00
end else
2022-09-07 18:06:19 +00:00
if (fchain_write=0) and
2022-09-05 13:30:24 +00:00
(node^.GetSize<=pConfig^.maxUniformBufferRange) then
2022-05-31 07:17:14 +00:00
begin
node^.bType :=btUniformBuffer;
node^.FStorage:=StorageClass.Uniform;
end else
2022-09-05 13:30:24 +00:00
if pConfig^.CanUseStorageBufferClass then
2022-05-31 07:17:14 +00:00
begin
node^.FStorage:=StorageClass.StorageBuffer;
end else
begin
node^.FStorage:=StorageClass.Uniform;
end;
end;
node:=Next(node);
end;
end;
function AlignShift(addr:Pointer;alignment:PtrUInt):PtrUInt; inline;
begin
if (alignment>1) then
begin
Result:=(PtrUInt(addr) mod alignment);
end else
begin
Result:=0;
end;
end;
procedure TsrBufferList.AlignOffset(node:PsrBuffer;offset:PtrUint);
var
P:Pointer;
begin
P:=node^.key.pLayout^.GetData;
offset:=AlignShift(P,offset);
node^.align_offset:=offset;
node^.ShiftOffset(offset);
end;
procedure TsrBufferList.AlignOffset;
var
2022-09-05 13:30:24 +00:00
pConfig:PsrConfig;
2022-05-31 07:17:14 +00:00
node:PsrBuffer;
begin
2022-09-05 13:30:24 +00:00
pConfig:=FEmit.GetConfig;
2022-05-31 07:17:14 +00:00
node:=First;
While (node<>nil) do
begin
2022-09-05 13:30:24 +00:00
if node^.IsUsed then
begin
Case node^.bType of
btStorageBuffer:
begin
AlignOffset(node,pConfig^.minStorageBufferOffsetAlignment);
end;
btUniformBuffer:
begin
AlignOffset(node,pConfig^.minUniformBufferOffsetAlignment);
end;
btPushConstant:
begin
node^.align_offset:=pConfig^.PushConstantsOffset;
node^.ShiftOffset(pConfig^.PushConstantsOffset);
end;
end;
end;
node:=Next(node);
end;
end;
procedure TsrBufferList.OnAllocTypeBinding(pField:PsrField);
var
pDecorateList:PsrDecorateList;
node:PsrField;
SD:DWORD;
begin
2022-11-15 12:54:47 +00:00
if (pField^.sType=nil) then Exit;
if (pField^.sType^.dtype<>dtTypeStruct) then Exit;
2022-09-05 13:30:24 +00:00
pDecorateList:=FEmit.GetDecorateList;
SD:=pField^.GetStructDecorate;
if (SD<>DWORD(-1)) then
begin
2022-11-15 12:54:47 +00:00
pDecorateList^.OpDecorate(pField^.sType,SD,0);
2022-09-05 13:30:24 +00:00
end;
node:=pField^.First;
While (node<>nil) do
begin
2022-11-15 12:54:47 +00:00
pDecorateList^.OpMember(pField^.sType,node^.FID,node^.offset);
2022-09-05 13:30:24 +00:00
node:=pField^.Next(node);
end;
end;
procedure TsrBufferList.AllocTypeBinding;
var
pDecorateList:PsrDecorateList;
node:PsrBuffer;
begin
EnumAllField(@OnAllocTypeBinding);
//
pDecorateList:=FEmit.GetDecorateList;
//
node:=First;
While (node<>nil) do
begin
if node^.IsUsed and (node^.pVar<>nil) then
if (node^.bType=btStorageBuffer) then
begin
2022-09-07 18:06:19 +00:00
if (node^.chain_read=0) then
2022-09-05 13:30:24 +00:00
begin
pDecorateList^.OpDecorate(node^.pVar,Decoration.NonReadable,0);
end;
2022-09-07 18:06:19 +00:00
if (node^.chain_write=0) then
2022-09-05 13:30:24 +00:00
begin
pDecorateList^.OpDecorate(node^.pVar,Decoration.NonWritable,0);
end;
end;
node:=Next(node);
end;
end;
procedure TsrBufferList.AllocName;
var
FDebugInfo:PsrDebugInfoList;
node:PsrBuffer;
begin
FDebugInfo:=FEmit.GetDebugInfoList;
node:=First;
While (node<>nil) do
begin
if node^.IsUsed and (node^.FTop.pType<>nil) then
begin
FDebugInfo^.OpName(node^.FTop.pType,node^.GetStructName);
2022-05-31 07:17:14 +00:00
end;
node:=Next(node);
end;
end;
end.