-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMundus.Renderer.Worker.pas
180 lines (163 loc) · 4.68 KB
/
Mundus.Renderer.Worker.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
unit Mundus.Renderer.Worker;
interface
uses
Classes,
Graphics,
SyncObjs,
Generics.Collections,
Mundus.DrawCall,
Mundus.Shader.Texture,
Mundus.Math,
Mundus.Types,
Mundus.Diagnostics.StopWatch;
type
TRenderWorker = class(TThread)
private
FDrawCalls: TDrawCalls;
FDone: TEvent;
FStart: TEvent;
FBlockSteps: Integer;
FBlockOffset: Integer;
FResolutionX: Integer;
FResolutionY: Integer;
FMaxResolutionX: Integer;
FMaxResolutionY: Integer;
FHalfResolutionX: Integer;
FHalfResolutionY: Integer;
FWatch: TStopWatch;
FPixelBuffer: TBitmap;
FDepthBuffer: PDepthsBuffer;
procedure SetResolutionX(const Value: Integer);
procedure SetResolutionY(const Value: Integer);
function GetFPS: Integer;
function GetRenderFence: THandle;
protected
procedure Execute; override;
procedure TerminatedSet; override;
public
constructor Create;
destructor Destroy; override;
procedure StartRender;
property DrawCalls: TDrawCalls read FDrawCalls write FDrawCalls;
property BlockSteps: Integer read FBlockSteps write FBlockSteps;
property BlockOffset: Integer read FBlockOffset write FBlockOffset;
property ResolutionX: Integer read FResolutionX write SetResolutionX;
property ResolutionY: Integer read FResolutionY write SetResolutionY;
property PixelBuffer: TBitmap read FPixelBuffer write FPixelBuffer;
property DepthBuffer: PDepthsBuffer read FDepthBuffer write FDepthBuffer;
property FPS: Integer read GetFPS;
property RenderFence: THandle read GetRenderFence;
end;
implementation
uses
Windows,
Mundus.Rasterizer,
Mundus.Shader;
{ TRenderWorker }
constructor TRenderWorker.Create;
begin
inherited Create(True);
FDone := TEvent.Create(nil, False, True, '');
FStart := TEvent.Create(nil, False, False, '');
FWatch := TStopWatch.Create();
end;
destructor TRenderWorker.Destroy;
begin
inherited;
FStart.Free;
FDone.Free;
FWatch.Free;
end;
procedure TRenderWorker.Execute;
var
LCall: PDrawCall;
LTriangle: TTriangle;
i, k: Integer;
LVertexA, LVertexB, LVertexC, LNormal: TFloat4;
LShader: TShader;
LRasterizer: TRasterizer;
LRenderTarget: Pointer;
begin
while not Terminated do
begin
FStart.WaitFor();
FWatch.Start;
if Assigned(FDrawCalls) then
for i := 0 to Pred(FDrawCalls.Count) do
begin
LRenderTarget := FPixelBuffer.ScanLine[0];
LCall := FDrawCalls[i];
LShader := LCall.Shader.Create();
LShader.PixelBuffer := FPixelBuffer;
LShader.BindBuffer(@LCall.Values);
LRasterizer := LCall.Shader.GetRasterizer();
for k := 0 to Pred(LCall.TriangleCount) do
begin
LTriangle := LCall.Triangles[k];
LVertexA := LCall.Vertices[LTriangle.VertexA];
LVertexB := LCall.Vertices[LTriangle.VertexB];
LVertexC := LCall.Vertices[LTriangle.VertexC];
LNormal.CalculateSurfaceNormal(LVertexA, LVertexB, LVertexC);
if (LNormal.Z < 0)then
begin
//denormalize vectors to screenpos
LVertexA.Element[0] := (1-LVertexA.Element[0]) * FHalfResolutionX;//half screen size
LVertexA.Element[1] := (1-LVertexA.Element[1]) * FHalfResolutionY;
LVertexB.Element[0] := (1-LVertexB.Element[0]) * FHalfResolutionX;
LVertexB.Element[1] := (1-LVertexB.Element[1]) * FHalfResolutionY;
LVertexC.Element[0] := (1-LVertexC.Element[0]) * FHalfResolutionX;
LVertexC.Element[1] := (1-LVertexC.Element[1]) * FHalfResolutionY;
LRasterizer(
FMaxResolutionX, FMaxResolutionY,
LVertexA, LVertexB, LVertexC,
LCall.Attributes[LTriangle.VertexA],
LCall.Attributes[LTriangle.VertexB],
LCall.Attributes[LTriangle.VertexC],
LShader,
LRenderTarget,
FDepthBuffer,
FBlockOffset, FBlockSteps);
end;
end;
LShader.Free;
end;
FWatch.Stop;
FDone.SetEvent;
end;
end;
function TRenderWorker.GetFPS: Integer;
var
LMicro: Int64;
begin
LMicro := FWatch.ElapsedMicroseconds;
if LMicro > 0 then
Result := 1000000 div LMicro
else
Result := 1000;
end;
function TRenderWorker.GetRenderFence: THandle;
begin
Result := FDone.Handle;
end;
procedure TRenderWorker.SetResolutionX(const Value: Integer);
begin
FResolutionX := Value;
FMaxResolutionX := Value - 1;
FHalfResolutionX := Value div 2;
end;
procedure TRenderWorker.SetResolutionY(const Value: Integer);
begin
FResolutionY := Value;
FMaxResolutionY := Value - 1;
FHalfResolutionY := Value div 2;
end;
procedure TRenderWorker.StartRender;
begin
FStart.SetEvent;
end;
procedure TRenderWorker.TerminatedSet;
begin
inherited;
FStart.SetEvent;
end;
end.