Guest User

Presorted

a guest
Jun 8th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.12 KB | None | 0 0
  1. function TFL3dCobject.Init:Boolean;
  2. var
  3. I      : Integer;
  4. J      : Integer;
  5. K      : Integer;
  6. M      : Integer;
  7. N      : Integer;
  8. Num    : UINT;
  9. Mat    : UINT;
  10. Vms    : PVms;
  11. Idx    : array of UINT;
  12. begin
  13. for I := 0 to High(F3dbs) do
  14.   with F3dbs[I] do
  15.     for J := 0 to High(Levels) do
  16.       with Levels[J] do
  17.       begin
  18.         Vms := Lib.Data;
  19.         if (Ref.MeshCount > 0) and
  20.           (Ref.IndexStart < Vms.Data.IndexCount) and
  21.           (Ref.VertexStart < Vms.Data.VertexCount) and
  22.           (UINT(Ref.MeshStart) + UINT(Ref.MeshCount) <= UINT(Vms.Data.MeshesCount)) then
  23.           begin
  24.             Num := 0;
  25.             if Length(Idx) < Ref.MeshCount then SetLength(Idx, Ref.MeshCount);
  26.             for M := 0 to Ref.MeshCount-1 do
  27.               with Vms.Meshes[Ref.MeshStart + M] do
  28.               begin
  29.                 Idx[M] := Ref.IndexStart + Num;
  30.                 Inc(Num,VNum);
  31.               end;
  32.             if Num > 0 then
  33.             begin
  34.               N := 0;
  35.               Num := 0;
  36.               SetLength(_3d, Ref.MeshCount);
  37.               for K := 0 to High(Vms.Mats) do
  38.               begin
  39.                 Mat := Vms.Mats[K];
  40.                 for M := 0 to Ref.MeshCount-1 do
  41.                   if Mat = Vms.Meshes[Ref.MeshStart + M].MaterialID then
  42.                   begin
  43.                     for N := M to Ref.MeshCount-1 do
  44.                       with Vms.Meshes[Ref.MeshStart + N] do
  45.                         if Mat = MaterialID then
  46.                         begin
  47.                           _3d[Num].IStart := Idx[N] + Vms.Buffer.IB;
  48.                           _3d[Num].ICount := VNum;
  49.                           _3d[Num].VStart := Ref.VertexStart + VStart + Vms.Buffer.VB;
  50.                           _3d[Num].VCount := VEnd - VStart;
  51.                           _3d[Num].Material := MaterialID;
  52.                           Inc(Num);
  53.                         end;
  54.                     Break;
  55.                   end;
  56.               end;
  57.             end;
  58.           end;
  59.       end;
  60. Result := True;
  61. end;
  62.  
  63.  
  64. //Shader expects that matrices buffer is bound at slot #7.
  65. //Precalculated matrices of all instances (typical static geometry) gives +25% boost VS updating matrices each frame.
  66. procedure TFL3dCobject.DrawInstanced(const Context:ID3D11DeviceContext;const Count,Start:UINT;const Level:Byte=0);
  67. var
  68. DC    : ID3D11DeviceContext;
  69. P     : Integer;
  70. C     : TD3DXColor;
  71. Cnt   : UINT;
  72. Clr   : Single;
  73. Add   : Single;
  74. M     : Integer;
  75. MatID : UINT;
  76. Buff  : ID3DX11Buffer;
  77. //  Z: Integer;
  78. begin
  79. if not Eval or (Count = 0) then Exit;
  80. if Context = nil then
  81.   FL.Device.GetImmediateContext(DC) else
  82.   DC := Context;
  83. DC.IASetInputLayout(FL.Layout);
  84. DC.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  85. MatID := 0;
  86. for P := 0 to High(FParts) do
  87.   with FParts[P] do
  88.     if Model >= 0 then
  89.       with F3dbs[Model] do
  90.         if UINT(Level) < UINT(Length(Levels)) then
  91.           with Levels[Level], Lib.Data^, Buffer do
  92.             if Value <> nil then
  93.             begin
  94.               if Buff <> Value then
  95.               begin
  96.                 Buff := Value;
  97.                 if Failed(Buff.Pipeline(nil,Map)) then
  98.                   Exit;
  99.               end;
  100.               FL.Effect.GetVariableByName('Model').AsMatrix.SetMatrix(@Mat);
  101.               for M := 0 to High(_3d) do
  102.                 with _3d[M] do
  103.                 begin
  104.                   if MatID <> Material then
  105.                   begin
  106.                     MatID := Material;
  107.                     Clr := Clr + Add;
  108.                     C := D3DXColorToUINT(MIHSV2RGB(DWORD(Round(Clr) shl 16) or $0000FFFF));
  109.                     FL.Effect.GetVariableByName('Dc').AsVector.SetFloatVector(@C);
  110.  
  111.                     //This should called as less as possible. It kills performance.
  112.                     FL.Effect.GetTechniqueByIndex(0).GetPassByIndex(0).Apply(0,DC);
  113.                   end;
  114.                   //6000 meshes: fps ~2000-2700
  115.                   //for Z := 0 to 100 do //6000 meshes x100 (~600000 calls) fps ~43
  116.                   DC.DrawIndexedInstanced(ICount, Count, IStart, VStart, Start);
  117.                 end;
  118.             end;
  119. end;
Add Comment
Please, Sign In to add comment