2022-05-31 07:17:14 +00:00
|
|
|
unit emit_alloc;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
sysutils,
|
|
|
|
spirv,
|
2022-09-05 13:30:24 +00:00
|
|
|
srNode,
|
|
|
|
srType,
|
2022-05-31 07:17:14 +00:00
|
|
|
srTypes,
|
|
|
|
srConst,
|
|
|
|
srRefId,
|
|
|
|
srReg,
|
|
|
|
srLayout,
|
|
|
|
srVariable,
|
2022-09-05 13:30:24 +00:00
|
|
|
srInput,
|
|
|
|
srOutput,
|
|
|
|
srVertLayout,
|
|
|
|
srFragLayout,
|
|
|
|
srUniform,
|
|
|
|
srBuffer,
|
|
|
|
srDecorate,
|
2022-05-31 07:17:14 +00:00
|
|
|
srOp,
|
|
|
|
srOpUtils,
|
2022-09-05 13:30:24 +00:00
|
|
|
emit_fetch;
|
2022-05-31 07:17:14 +00:00
|
|
|
|
|
|
|
type
|
2022-09-05 13:30:24 +00:00
|
|
|
TSprvEmit_alloc=class(TEmitFetch)
|
|
|
|
procedure AllocStage;
|
2022-05-31 07:17:14 +00:00
|
|
|
procedure AllocSpirvID(P:PsrRefId);
|
|
|
|
procedure AllocBinding;
|
|
|
|
procedure AllocTypeBinding;
|
|
|
|
procedure AllocEntryPoint;
|
|
|
|
procedure AllocHeader;
|
|
|
|
procedure AllocOpListId(node:PspirvOp);
|
2022-09-05 13:30:24 +00:00
|
|
|
procedure AllocListId(node:PsrNode);
|
2022-05-31 07:17:14 +00:00
|
|
|
procedure AllocFuncId;
|
|
|
|
procedure AllocOpId(node:PSpirvOp);
|
|
|
|
procedure AllocOpBlock(pBlock:PsrOpBlock);
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
procedure TSprvEmit_alloc.AllocStage;
|
2022-05-31 07:17:14 +00:00
|
|
|
begin
|
|
|
|
AllocBinding;
|
2022-09-05 13:30:24 +00:00
|
|
|
|
|
|
|
BufferList.AllocTypeBinding;
|
2022-05-31 07:17:14 +00:00
|
|
|
AllocTypeBinding;
|
|
|
|
|
|
|
|
AllocHeader;
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
//Source Extension
|
|
|
|
DataLayoutList.AllocSourceExtension;
|
|
|
|
DataLayoutList.AllocFuncExt;
|
|
|
|
VertLayoutList.AllocSourceExtension;
|
|
|
|
UniformList .AllocSourceExtension;
|
|
|
|
BufferList .AllocSourceExtension;
|
|
|
|
|
|
|
|
//Decorate Name
|
|
|
|
BufferList .AllocName;
|
|
|
|
VariableList.AllocName;
|
2022-05-31 07:17:14 +00:00
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
//header id
|
|
|
|
AllocOpListId(HeaderList.First);
|
|
|
|
AllocOpListId(DebugInfoList.First);
|
|
|
|
AllocOpListId(DecorateList.First);
|
|
|
|
|
|
|
|
//element id
|
|
|
|
AllocListId(TypeList.First);
|
|
|
|
AllocListId(ConstList.First);
|
|
|
|
AllocListId(VariableList.First);
|
2022-05-31 07:17:14 +00:00
|
|
|
|
|
|
|
AllocFuncId;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocSpirvID(P:PsrRefId);
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
RefIdAlloc.FetchSpirvID(P);
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocBinding;
|
|
|
|
var
|
|
|
|
FBinding:Integer;
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
InputList .AllocBinding;
|
|
|
|
OutputList.AllocBinding;
|
|
|
|
|
|
|
|
VertLayoutList.AllocBinding;
|
|
|
|
FragLayoutList.AllocBinding;
|
2022-05-31 07:17:14 +00:00
|
|
|
|
|
|
|
FBinding:=0;
|
2022-09-05 13:30:24 +00:00
|
|
|
|
|
|
|
UniformList.AllocBinding(FBinding);
|
|
|
|
BufferList .AllocBinding(FBinding);
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocTypeBinding;
|
|
|
|
var
|
|
|
|
node:PsrType;
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=TypeList.First;
|
2022-05-31 07:17:14 +00:00
|
|
|
While (node<>nil) do
|
|
|
|
begin
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
case node^.OpId of
|
2022-05-31 07:17:14 +00:00
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
Op.OpTypeArray,
|
|
|
|
Op.OpTypeRuntimeArray:
|
2022-05-31 07:17:14 +00:00
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
DecorateList.OpDecorate(node,Decoration.ArrayStride,node^.array_stride);
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
Op.OpTypeFloat:
|
|
|
|
begin
|
|
|
|
case node^.dtype.BitSize of
|
|
|
|
16:AddCapability(Capability.Float16);
|
|
|
|
64:AddCapability(Capability.Float64);
|
|
|
|
else;
|
|
|
|
end;
|
|
|
|
end;
|
2022-05-31 07:17:14 +00:00
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
Op.OpTypeInt:
|
2022-05-31 07:17:14 +00:00
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
case node^.dtype.BitSize of
|
|
|
|
8:AddCapability(Capability.Int8);
|
|
|
|
16:AddCapability(Capability.Int16);
|
|
|
|
64:AddCapability(Capability.Int64);
|
|
|
|
else;
|
|
|
|
end;
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
else;
|
|
|
|
end;
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=node^.Next;
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocOpListId(node:PspirvOp);
|
|
|
|
begin
|
|
|
|
While (node<>nil) do
|
|
|
|
begin
|
|
|
|
AllocOpId(node);
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=node^.Next;
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocEntryPoint;
|
|
|
|
var
|
|
|
|
node:PSpirvOp;
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpEntryPoint);
|
2022-05-31 07:17:14 +00:00
|
|
|
|
|
|
|
node^.AddLiteral(FExecutionModel,ExecutionModel.GetStr(FExecutionModel));
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
node^.AddParam(Main);
|
|
|
|
node^.AddString(Main^.name);
|
2022-05-31 07:17:14 +00:00
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
InputList .AllocEntryPoint(node);
|
|
|
|
VertLayoutList.AllocEntryPoint(node);
|
|
|
|
FragLayoutList.AllocEntryPoint(node);
|
|
|
|
OutputList .AllocEntryPoint(node);
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocHeader;
|
|
|
|
var
|
|
|
|
node:PSpirvOp;
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpMemoryModel);
|
2022-05-31 07:17:14 +00:00
|
|
|
node^.AddLiteral(AddressingModel.Logical,AddressingModel.GetStr(AddressingModel.Logical));
|
|
|
|
node^.AddLiteral(MemoryModel.GLSL450,MemoryModel.GetStr(MemoryModel.GLSL450));
|
|
|
|
|
|
|
|
AllocEntryPoint;
|
|
|
|
|
|
|
|
Case FExecutionModel of
|
|
|
|
ExecutionModel.Fragment:
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpExecutionMode);
|
|
|
|
node^.AddParam(Main);
|
2022-05-31 07:17:14 +00:00
|
|
|
node^.AddLiteral(ExecutionMode.OriginUpperLeft,ExecutionMode.GetStr(ExecutionMode.OriginUpperLeft));
|
2023-01-08 15:41:13 +00:00
|
|
|
|
|
|
|
if (foDepthReplacing in DecorateList.FfemOpSet) then
|
|
|
|
begin
|
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpExecutionMode);
|
|
|
|
node^.AddParam(Main);
|
|
|
|
node^.AddLiteral(ExecutionMode.DepthReplacing,ExecutionMode.GetStr(ExecutionMode.DepthReplacing));
|
|
|
|
end;
|
|
|
|
|
|
|
|
if (foDepthGreater in DecorateList.FfemOpSet) then
|
|
|
|
begin
|
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpExecutionMode);
|
|
|
|
node^.AddParam(Main);
|
|
|
|
node^.AddLiteral(ExecutionMode.DepthGreater,ExecutionMode.GetStr(ExecutionMode.DepthGreater));
|
|
|
|
end;
|
|
|
|
|
|
|
|
if (foDepthLess in DecorateList.FfemOpSet) then
|
|
|
|
begin
|
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpExecutionMode);
|
|
|
|
node^.AddParam(Main);
|
|
|
|
node^.AddLiteral(ExecutionMode.DepthLess,ExecutionMode.GetStr(ExecutionMode.DepthLess));
|
|
|
|
end;
|
|
|
|
|
|
|
|
if (foDepthUnchanged in DecorateList.FfemOpSet) then
|
|
|
|
begin
|
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpExecutionMode);
|
|
|
|
node^.AddParam(Main);
|
|
|
|
node^.AddLiteral(ExecutionMode.DepthUnchanged,ExecutionMode.GetStr(ExecutionMode.DepthUnchanged));
|
|
|
|
end;
|
|
|
|
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
ExecutionModel.GLCompute:
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=HeaderList.AddSpirvOp(Op.OpExecutionMode);
|
|
|
|
node^.AddParam(Main);
|
2022-05-31 07:17:14 +00:00
|
|
|
node^.AddLiteral(ExecutionMode.LocalSize,ExecutionMode.GetStr(ExecutionMode.LocalSize));
|
|
|
|
node^.AddLiteral(FLocalSize.x);
|
|
|
|
node^.AddLiteral(FLocalSize.y);
|
|
|
|
node^.AddLiteral(FLocalSize.z);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
procedure TSprvEmit_alloc.AllocListId(node:PsrNode);
|
2022-05-31 07:17:14 +00:00
|
|
|
begin
|
|
|
|
While (node<>nil) do
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
AllocSpirvID(node^.GetRef);
|
|
|
|
node:=node^.Next;
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocFuncId;
|
|
|
|
var
|
|
|
|
pFunc:PSpirvFunc;
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
pFunc:=FuncList.First;
|
2022-05-31 07:17:14 +00:00
|
|
|
While (pFunc<>nil) do
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
AllocOpBlock(pFunc^.pTop);
|
|
|
|
pFunc:=pFunc^.Next;
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocOpId(node:PSpirvOp);
|
|
|
|
var
|
|
|
|
Param:POpParamNode;
|
|
|
|
Info:Op.TOpInfo;
|
2022-07-15 10:12:45 +00:00
|
|
|
pReg:PsrRegNode;
|
2022-05-31 07:17:14 +00:00
|
|
|
begin
|
|
|
|
if (node=nil) then Exit;
|
|
|
|
|
|
|
|
Info:=Op.GetInfo(node^.OpId);
|
|
|
|
|
|
|
|
if Info.result then //dst
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
Assert(node^.pDst<>nil,'AllocOp$1');
|
|
|
|
if (node^.pDst<>nil) then
|
|
|
|
begin
|
|
|
|
AllocSpirvID(node^.pDst^.GetRef);
|
|
|
|
end;
|
2022-05-31 07:17:14 +00:00
|
|
|
end else
|
|
|
|
begin //no dst
|
2022-09-05 13:30:24 +00:00
|
|
|
if (node^.pDst<>nil) then
|
2022-05-31 07:17:14 +00:00
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
AllocSpirvID(node^.pDst^.GetRef);
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if Info.rstype then //dst type
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
if (node^.pType=nil) then
|
2022-07-15 10:12:45 +00:00
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
pReg:=node^.pDst^.AsType(ntReg);
|
|
|
|
Assert(pReg<>nil,'AllocOp$2');
|
|
|
|
Assert(pReg^.dtype<>dtUnknow,'AllocOp$3');
|
|
|
|
if (pReg<>nil) then
|
|
|
|
begin
|
|
|
|
node^.pType:=TypeList.Fetch(pReg^.dtype);
|
|
|
|
end;
|
2022-07-15 10:12:45 +00:00
|
|
|
end;
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
|
2022-09-05 13:30:24 +00:00
|
|
|
Param:=node^.ParamFirst;
|
2022-05-31 07:17:14 +00:00
|
|
|
While (Param<>nil) do
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
AllocSpirvID(Param^.Value^.GetRef);
|
|
|
|
Param:=Param^.Next;
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSprvEmit_alloc.AllocOpBlock(pBlock:PsrOpBlock);
|
|
|
|
var
|
|
|
|
node:PSpirvOp;
|
|
|
|
begin
|
|
|
|
if (pBlock=nil) then Exit;
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=pBlock^.First;
|
2022-05-31 07:17:14 +00:00
|
|
|
While (node<>nil) do
|
|
|
|
begin
|
2022-09-05 13:30:24 +00:00
|
|
|
if node^.IsType(ntOp) then
|
2022-05-31 07:17:14 +00:00
|
|
|
begin
|
|
|
|
AllocOpId(node);
|
|
|
|
end;
|
2022-09-05 13:30:24 +00:00
|
|
|
node:=flow_down_next_up(node);
|
2022-05-31 07:17:14 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
end.
|
|
|
|
|