forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgracoordpool3d.pas
470 lines (417 loc) · 13 KB
/
bgracoordpool3d.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
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRACoordPool3D;
{$i bgrabitmap.inc}{$H+}
{.$POINTERMATH ON}
interface
uses
Classes, SysUtils, BGRATypes, BGRABitmapTypes, BGRASSE, BGRAMatrix3D;
type
PBGRACoordData3D = ^TBGRACoordData3D;
TBGRACoordData3D = packed record
{0} sceneCoord: TPoint3D_128;
{16} viewCoord: TPoint3D_128;
{32} projectedCoord: TPointF;
{40} InvZ: single;
{44} used: wordbool; customNormalUsed: wordbool;
{48} viewNormal: TPoint3D_128;
{64} customNormal: TPoint3D_128;
end; {80}
PBGRANormalData3D = ^TBGRANormalData3D;
TBGRANormalData3D = packed record
{0} customNormal: TPoint3D_128;
{16} viewNormal: TPoint3D_128;
{32} used: longbool;
{36} filler1,filler2,filler3: BGRALongWord;
end; {48}
{ TBGRAGenericPool }
TBGRAGenericPool = class
private
FFirstFree: integer;
FNbElements,FCapacity: integer;
FElementSize: BGRAPtrInt;
FUsedCapacity : integer;
function GetElement(AIndex: integer): Pointer;
procedure SetCapacity(ACapacity: integer);
protected
FPoolData: TMemoryBlockAlign128;
function GetUsed({%H-}AElement: integer): boolean; virtual;
procedure SetUsed({%H-}AElement: integer; {%H-}AUsed: boolean); virtual;
procedure Remove(AIndex: integer); //does not work if GetUsed/SetUsed are not implemented
public
constructor Create(ACapacity: integer; AElementSize: integer);
destructor Destroy; override;
function Add: integer;
property Element[AIndex: integer]: Pointer read GetElement;
property Capacity: integer read FCapacity;
property UsedCapacity: integer read FUsedCapacity;
end;
{ TBGRACoordPool3D }
TBGRACoordPool3D = class(TBGRAGenericPool)
private
function GetCoordData(AIndex: integer): PBGRACoordData3D;
protected
function GetUsed(AElement: integer): boolean; override;
procedure SetUsed(AElement: integer; AUsed: boolean); override;
public
procedure Remove(AIndex: integer);
constructor Create(ACapacity: integer);
procedure ComputeWithMatrix(const AMatrix: TMatrix3D; const AProjection: TProjection3D);
property CoordData[AIndex: integer]: PBGRACoordData3D read GetCoordData;
end;
{ TBGRANormalPool3D }
TBGRANormalPool3D = class(TBGRAGenericPool)
private
function GetNormalData(AIndex: integer): PBGRANormalData3D;
protected
function GetUsed(AElement: integer): boolean; override;
procedure SetUsed(AElement: integer; AUsed: boolean); override;
public
procedure Remove(AIndex: integer);
constructor Create(ACapacity: integer);
procedure ComputeWithMatrix(const AMatrix: TMatrix3D);
property NormalData[AIndex: integer]: PBGRANormalData3D read GetNormalData;
end;
implementation
{ TBGRAGenericPool }
function TBGRAGenericPool.GetElement(AIndex: integer): Pointer;
begin
result := Pointer(PByte(FPoolData.Data)+AIndex*FElementSize);
end;
procedure TBGRAGenericPool.SetCapacity(ACapacity: integer);
var NewPoolData: TMemoryBlockAlign128;
begin
if FCapacity <> ACapacity then
begin
if ACapacity = 0 then
FreeAndNil(FPoolData)
else
begin
NewPoolData := TMemoryBlockAlign128.Create(ACapacity*FElementSize);
if FCapacity <> 0 then
begin
//previous block is smaller
if FCapacity < ACapacity then
begin
move(FPoolData.Data^, NewPoolData.Data^, FCapacity*FElementSize);
//pad with zeros
fillchar((pbyte(NewPoolData.Data)+FCapacity*FElementSize)^,(ACapacity-FCapacity)*FElementSize,0);
end
else //previous block is greater or equal
move(FPoolData.Data^, NewPoolData.Data^, ACapacity*FElementSize);
FreeAndNil(FPoolData);
end else
//clear new block
fillchar(pbyte(NewPoolData.Data)^,ACapacity*FElementSize,0);
FPoolData := NewPoolData;
end;
FCapacity:= ACapacity;
end;
end;
function TBGRAGenericPool.GetUsed(AElement: integer): boolean;
begin
result := false;
end;
procedure TBGRAGenericPool.SetUsed(AElement: integer; AUsed: boolean);
begin
//nothing
end;
constructor TBGRAGenericPool.Create(ACapacity: integer; AElementSize: integer);
begin
FCapacity := 0;
FPoolData := nil;
FNbElements:= 0;
FFirstFree := 0;
FUsedCapacity := 0;
FElementSize:= AElementSize;
SetCapacity(ACapacity);
end;
destructor TBGRAGenericPool.Destroy;
begin
FreeAndNil(FPoolData);
FCapacity := 0;
FNbElements:= 0;
FFirstFree := 0;
FUsedCapacity := 0;
inherited Destroy;
end;
procedure TBGRAGenericPool.Remove(AIndex: integer);
begin
if (AIndex < 0) or (AIndex >= FUsedCapacity) then
raise ERangeError.Create('Index out of bounds');
if GetUsed(AIndex) then
begin
SetUsed(AIndex, false);
if AIndex < FFirstFree then FFirstFree := AIndex;
if AIndex = FUsedCapacity-1 then
begin
while (FUsedCapacity > 0) and not GetUsed(FUsedCapacity-1) do
dec(FUsedCapacity);
end;
end;
end;
function TBGRAGenericPool.Add: integer;
begin
//check for free space
while FFirstFree < FCapacity do
begin
if not GetUsed(FFirstFree) then
begin
SetUsed(FFirstFree,True);
result := FFirstFree;
inc(FFirstFree);
if FFirstFree > FUsedCapacity then
FUsedCapacity := FFirstFree;
exit;
end;
inc(FFirstFree);
end;
//no free space
SetCapacity(FCapacity*2+8);
SetUsed(FFirstFree, true);
result := FFirstFree;
inc(FFirstFree);
if FFirstFree > FUsedCapacity then
FUsedCapacity := FFirstFree;
end;
{ TBGRACoordPool3D }
constructor TBGRACoordPool3D.Create(ACapacity: integer);
begin
inherited Create(ACapacity,SizeOf(TBGRACoordData3D));
end;
procedure TBGRACoordPool3D.ComputeWithMatrix(const AMatrix: TMatrix3D;
const AProjection: TProjection3D);
var
P: PBGRACoordData3D;
I: BGRANativeInt;
begin
if UsedCapacity = 0 then exit;
P := PBGRACoordData3D(FPoolData.Data);
{$IFDEF CPUI386}
{$IFDEF BGRASSE_AVAILABLE}
{$asmmode intel}
if UseSSE then
begin
Matrix3D_SSE_Load(AMatrix);
asm
mov eax,[AProjection]
movups xmm4,[eax]
xorps xmm1,xmm1
end;
i := UsedCapacity;
if UseSSE3 then
begin
while i > 0 do
with P^ do
begin
if used then
begin
MatrixMultiplyVect3D_SSE3_Aligned(sceneCoord,viewCoord);
if viewCoord.z > 0 then
begin
asm
mov eax, P
movaps xmm3, [eax+16] //viewCoord
movaps xmm2,xmm3
shufps xmm2,xmm3,2+8+32+128
rcpps xmm2,xmm2 //xmm2 = InvZ
movss [eax+40],xmm2 //-> InvZ
mulps xmm3,xmm4 //xmm3 *= Projection.Zoom
mulps xmm3,xmm2 //xmm3 *= InvZ
movhlps xmm0,xmm4 //xmm0 = Projection.Center
addps xmm3,xmm0 //xmm3 += Projection.Center
movlps [eax+32],xmm3 //->projectedCoord
movaps [eax+48],xmm1 //->normal
end;
end else
asm
mov eax, P
movlps [eax+32],xmm1 //0->projectedCoord
movaps [eax+48],xmm1 //->normal
end;
if customNormalUsed then
MatrixMultiplyVect3DWithoutTranslation_SSE3_Aligned(customNormal,viewNormal);
end;
dec(i);
inc(p);
end;
end else
begin
while i > 0 do
with P^ do
begin
if used then
begin
MatrixMultiplyVect3D_SSE_Aligned(sceneCoord,viewCoord);
if viewCoord.z > 0 then
begin
asm
mov eax, P
movaps xmm3, [eax+16] //viewCoord
movaps xmm2,xmm3
shufps xmm2,xmm3,2+8+32+128
rcpps xmm2,xmm2 //xmm2 = InvZ
movss [eax+40],xmm2 //-> InvZ
mulps xmm3,xmm4 //xmm3 *= Projection.Zoom
mulps xmm3,xmm2 //xmm3 *= InvZ
movhlps xmm0,xmm4 //xmm0 = Projection.Center
addps xmm3,xmm0 //xmm3 += Projection.Center
movlps [eax+32],xmm3 //->projectedCoord
movaps [eax+48],xmm1 //->normal
end;
end else
asm
mov eax, P
movlps [eax+32],xmm1 //0 ->projectedCoord
movaps [eax+48],xmm1 //->normal
end;
if customNormalUsed then
MatrixMultiplyVect3DWithoutTranslation_SSE_Aligned(customNormal,viewNormal);
end;
dec(i);
inc(p);
end;
end;
end
else
{$ENDIF}
{$ENDIF}
begin
i := UsedCapacity;
while i > 0 do
with P^ do
begin
if used then
begin
viewCoord := MatrizMult(AMatrix,sceneCoord);
if customNormalUsed then
viewNormal := MultiplyVect3DWithoutTranslation(AMatrix,customNormal)
else
ClearPoint3D_128(viewNormal);
if viewCoord.z > 0 then
begin
InvZ := 1/viewCoord.z;
projectedCoord := PointF(viewCoord.x*InvZ*AProjection.Zoom.x + AProjection.Center.x,
viewCoord.y*InvZ*AProjection.Zoom.Y + AProjection.Center.y);
end else
projectedCoord := PointF(0,0);
end;
dec(i);
inc(p);
end;
end;
end;
function TBGRACoordPool3D.GetCoordData(AIndex: integer): PBGRACoordData3D;
begin
result := PBGRACoordData3D(FPoolData.Data)+AIndex;
end;
function TBGRACoordPool3D.GetUsed(AElement: integer): boolean;
begin
Result:= CoordData[AElement]^.used;
end;
procedure TBGRACoordPool3D.SetUsed(AElement: integer; AUsed: boolean);
begin
CoordData[AElement]^.used := AUsed;
end;
procedure TBGRACoordPool3D.Remove(AIndex: integer);
begin
inherited Remove(AIndex);
end;
{ TBGRANormalPool3D }
function TBGRANormalPool3D.GetNormalData(AIndex: integer): PBGRANormalData3D;
begin
result := PBGRANormalData3D(FPoolData.Data)+AIndex;
end;
function TBGRANormalPool3D.GetUsed(AElement: integer): boolean;
begin
Result:= NormalData[AElement]^.used;
end;
procedure TBGRANormalPool3D.SetUsed(AElement: integer; AUsed: boolean);
begin
NormalData[AElement]^.used := AUsed;
end;
procedure TBGRANormalPool3D.Remove(AIndex: integer);
begin
inherited Remove(AIndex);
end;
constructor TBGRANormalPool3D.Create(ACapacity: integer);
begin
inherited Create(ACapacity,SizeOf(TBGRANormalData3D));
end;
procedure TBGRANormalPool3D.ComputeWithMatrix(const AMatrix: TMatrix3D);
var
P: PBGRANormalData3D;
I: BGRANativeInt;
begin
if UsedCapacity = 0 then exit;
P := PBGRANormalData3D(FPoolData.Data);
{$IFDEF CPUI386}
{$IFDEF BGRASSE_AVAILABLE}
{$asmmode intel}
if UseSSE then
begin
Matrix3D_SSE_Load(AMatrix);
i := UsedCapacity;
if UseSSE3 then
begin
while i > 0 do
with P^ do
begin
if used then
MatrixMultiplyVect3DWithoutTranslation_SSE3_Aligned(customNormal,viewNormal);
dec(i);
inc(p);
end;
end else
begin
while i > 0 do
with P^ do
begin
if used then
MatrixMultiplyVect3DWithoutTranslation_SSE_Aligned(customNormal,viewNormal);
dec(i);
inc(p);
end;
end;
end
else
{$ENDIF}
{$ENDIF}
begin
i := UsedCapacity;
while i > 0 do
with P^ do
begin
if used then
viewNormal := MultiplyVect3DWithoutTranslation(AMatrix,customNormal);
dec(i);
inc(p);
end;
end;
end;
end.