-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaingui.pas
More file actions
564 lines (480 loc) · 17.1 KB
/
Copy pathmaingui.pas
File metadata and controls
564 lines (480 loc) · 17.1 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
unit mainGUI;
{$mode objfpc}{$H+}
{
MVP learn
- recursive rays
- implement previous frame reuse
- Closest sdf and dist reuse for tiled rays
- implement hierarchical small world navigation meshSDF and test for fast closest dist traingle sdf
- Anytime rendering/ tiled subset of the pixels are rendered unbiased, then update the screen at intervals with whatever has accumulated so far
- implement AVX/AVX2/SSE raymarcher using pascal intrinsics
- port to C/SDF/OpenGL/CUDA/Vulkan
- adjust grid to a fixed volume and then cell size varies on resolution, better thn start point plus cellsize
- make grid wrap around modular infinite grid
- double hash/LSH/JFA/sort/project large overlapping subgrids
to handle dense cells or degenerate cases, make it robust grid heuristic
- Signed Distance Field (SDF) Grids, Hierarchical Distance Fields
(Instead of storing object lists, store distance values:)
Jump Flooding Algorithm (JFA)
For dynamic updates of distance fields
GPU-based parallel distance field computation
Can rebuild 512³ distance field in ~1ms on modern GPU
void jumpFloodingStep(Texture3D& sdf, int jump_size) {
Each cell looks at neighbors at jump_size distance
// Propagates closest surface information
}
- fix multithread normal lines artifact:
- Port MVP to CUDA
- Port MVP to OpenGL/SDL GLFW/Vulkan/DirectX 12/Metal
- explore this: https://www.shadertoy.com/view/ltfXWr
-multiresGrid, grid cell can have sub grids
V1.0 (CUDA/Vulkan)
Realtime software sdf sculpter capable to handle millions of sdfs and complex scenes
- Flesh out the GUI and tools
- Arena allocator in pascal
- direct mesh sdf sculpt?
- visual debug render: pixel ray steps counter, depth, calls to raymarch, etc
- fix high overhead sending textures every frame (implement Ping-pong PBOs for per-frame streaming)
- booleans
- Layers and Grouping
- Check memory leak and safety
- made the different stages of the render pipeline toggable so they can be independently updated
(adjusting shading/lights won't trigger a full daymarch just shading pass)
- localized updates of the FrameBuffer and screen texture
- symmetry
- space transform operators for sdf evaluators
- advanced sdfs types
- painting/material/texture brushes
- more warp brushes
- BoundingSphereHierarcnhy tree or other spatial acceleration (- Sphere Bounding Volume Tree )
- layers and groups
- materials and textures
- meshing, remeshing algorithms and Assimp export/import
- save/load format
- config save/load
- sdf merging/prunning on filled space
- tube/curves sdf to save on stroke evaluation
- optimize scene definition with proxies or equivalent transforms
- spatial index of sdfs for distance estimation acceleration
- undo/redo
- node based scene editor
- AO and GI
- explore unbiased, progressive accumulating, montecarlo raymarcher for speed?
- explore LSH for Axis Aligned random space split planes creates a bitmask hash (random spatial grids)
Done
- basic GUI
- camera pan,zoom
- camera rotate, dolly
- deferred shading
- screen space normal calculation (for deferred shading)
- move RayBuffer to the camera
- sdf array for scene definition
- generalize march() and calNormal() to use the sdf scene definition
- when rotate, zoom, pan the camera, lower the viewport resolution
- scanline coarse pass to discard raymarch equal values.
- reduce viewport area trick with borders, panels and other windows to improve speed
- random spheres sdf scene for profiling
x software rasterizer? raymarch every n pixels and quad interpolate depth/color/etc
like a surface net for 2D quads
- mip map viewport on camera transforms/realtime changes and gradual refinement on idle
- allocate max FrameBuffer size so resizing won't create/delete new buffers
- make raydir calculation update on demand only
- move to separate units the logic for cleaner scope
- fix inconsistent rotations no matter the orientation (with quaternions)
- multithreading rendering
- multithread update bounds on resize
- tested pure software render to bitmap
- first distance step from camera common for all
- explore direct BitMap render , not worth it
- Cone tracing: Adaptive or fixed (8 px wide optimun)
- gaussian warp brush
- brush sculpt by adding warps
- issue with warps: they deform space, so zBuffer remains unchanged so hitpoint does not build up
- warpedP must be returned also on hit not just dist
- spatial grid acceleration (not worth it)
- Multiresolution spatial grid for faster walking or closest dist linear search (slower)
- bigger cells store bigger sdfs and smaller cells the smaller ones (slower)
- Hierarchical Bounding Sphere Tree (better)
- brush sculpt by adding sphere sdfs
x Expore ranomization algorithms for acceleration (rnd for ray march, rnd for sdf eval etc) not worth it
x Negative space SDF spheres when num scene sdf grows to infinity not worth it
- Port MVP to C++ QtCreator, C++ VStudio 2008/2022
- refactor sdf function pointers to class polymorfism an test for speed
- refactor experimental things to separate unit
- implement trianglesdf
- implement trianglearray sdf (meshSDF) and speed test
- load ply meshes to triangleSDF
- sphere early reject for traingle dist
- make a simple grid, test everything
- triangle sdf/exact triangle sdf
- triangle array/mesh sdf
x explore LSH (Local Sensitivity Hashing random will partition the whole space inclusing the camera.
- Impement Mandelbulb sdf
-% 2^N multi spatial grid where sdf bbox size determines which hash bucket it will go, queries involes testing multi grids (like LSH do)
(levels are dynamically created/removed as sdfs are added/removed
- explore random projection 1D cell sdf sorting for early exit if cells are dense )
}
interface
uses
{$ifdef unix} cthreads, {$endif}
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, ExtCtrls,
OpenGLContext, GL, Math, Matrix, LCLIntf, LCLType, StdCtrls,
SDF, Render;
type
{ TFormMain }
TFormMain = class(TForm)
OpenGLControl1: TOpenGLControl;
StatusBar1: TStatusBar;
ResizeTimer: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure OpenGLControl1KeyPress(Sender: TObject; var Key: char);
procedure OpenGLControl1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure OpenGLControl1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: integer);
procedure OpenGLControl1MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: integer; MousePos: TPoint; var Handled: boolean);
procedure OpenGLControl1Paint(Sender: TObject);
procedure OpenGLControl1Resize(Sender: TObject);
procedure ResizeTimerTimer(Sender: TObject);
private
procedure LowerResolutionOnce;
public
end;
var
FormMain: TFormMain;
origMouseX, origMouseY: integer;
HITMOUSE: Tvector3_single;
// FHD quality max 2:4 medium 3:6 low 4:8
RES: integer = 1; //2;
LOW_RES: integer = 1; //12;
NUMTHREADS: integer = 6; // hw threads -1 for responsiveness
finished: longint;
implementation
{$R *.lfm}
// todo move this to render
function RenderChunkThread(Index: Pointer): PtrInt;
var
idx, dy, startY, endY: integer;
begin
idx := PtrInt(Index);
dy := IMAGEDATA.TEX_HEIGHT div NUMTHREADS;
startY := idx * dy;
endY := startY + dy;
if idx = NUMTHREADS - 1 then
endY := IMAGEDATA.TEX_HEIGHT;
// Do the actual work
renderPipelineDebug(IMAGEDATA, startY, endY);
//renderPipelineCone(IMAGEDATA, startY, endY);
Result := 0;
end;
{ TFormMain }
procedure TFormMain.FormCreate(Sender: TObject);
var
i: integer;
c, v1,v2: Tvector3_single;
r: single;
begin
BGCOLOR.R := 138;
BGCOLOR.G := 171;
BGCOLOR.B := 164;
// One-time setup for OpenGL state.
OpenGLControl1.MakeCurrent;
OpenGLControl1.DoubleBuffered := True;
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
CAM.init;
LOWERES := False;
// Calculate maximum screen dimensions and pre-allocate
IMAGEDATA := TFrameBuffer.Create;
IMAGEDATA.preAllocate(Screen.Width div RES, Screen.Height div RES);
IMAGEDATA.resize(OpenGLControl1.Width div RES, OpenGLControl1.Height div RES);
// lights
LIGHTDIR.init(1, 1, 0);
LIGHTDIR := LIGHTDIR / LIGHTDIR.length;
LIGHTDIR2.init(-1, -1, 0);
LIGHTDIR2 := LIGHTDIR2 / LIGHTDIR2.length;
// scene
SCENE := TSceneSDF.Create();
for i := 0 to 49 do
begin
c.init(-2 + Random * 4,-2 + Random * 4,-2 + Random * 4);
r := 0.1; //0.3 + 0.2 *Random;
SCENE.addSphere(c, r);
//v1.init(-2 + Random * 4,-2 + Random * 4,-2 + Random * 4);
//v2.init(-2 + Random * 4,-2 + Random * 4,-2 + Random * 4);
//SCENE.addTriangle(c,v1,v2);
end; //}
//c.init(0, 0, 0);
//SCENE.addSphere(c, 0.6);
//SCENE.addThorus(c, 1.0, 0.25);
//v1.init(-1,1,0);
//v2.init(1,2,0);
//SCENE.addTriangle(c,v1,v2);
//SCENE.importPLY('bunL.ply'); //bunL.ply
//SCENE.addMandelbulb(c);
//SCENE.fillGrid();
//SCENE.fillMultiGrid();
//SCENE.rebuildBoundTree;
end;
procedure TFormMain.FormDestroy(Sender: TObject);
begin
// Delete the texture from GPU memory.
glDeleteTextures(1, @IMAGEDATA.FTextureID);
IMAGEDATA.dispose;
SCENE.Destroy;
end;
procedure TFormMain.LowerResolutionOnce;
begin
if RES = LOW_RES then Exit;
if not LOWERES then
begin
IMAGEDATA.resize(OpenGLControl1.Width div LOW_RES, OpenGLControl1.Height div
LOW_RES);
LOWERES := True;
DIRTYRAYS := True; // trigger at least once
end;
end;
procedure TFormMain.OpenGLControl1KeyPress(Sender: TObject; var Key: char);
procedure FitCameraToBound(const B: SphereSDF);
var
halfFovRad, distance: single;
begin
CAM.up.init(0, 1, 0);
CAM.target := B.getCenter;
CAM.updateCameraVectors; // now Camera.front points (0,0,-1)
halfFovRad := DegToRad(CAM.zoom) * 0.5;
distance := B.getBoundRadius / Sin(halfFovRad);
CAM.pos := B.getCenter - (CAM.front * distance);
CAM.updateCameraVectors;
end;
begin
if Key in ['f', 'F'] then
begin
FitCameraToBound(SCENE.getBound);
// focus is on main app now, hence we need to be explicit
// calling Repaint on OpenGLControl1
DIRTYRAYS := True;
OpenGLControl1.Invalidate;
end;
end;
// Example with computed surface normal for more realistic push/pull
procedure TFormMain.OpenGLControl1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: integer);
var
tX, tY: integer;
brushCenter, brushDirection, planePoint: TVector3_single;
brushRadius, brushStrength: single;
begin
tX := X div RES;
tY := EnsureRange(IMAGEDATA.TEX_HEIGHT - (Y div RES), 0, IMAGEDATA.TEX_HEIGHT);
// copy the original buffer for original surface sampling
SetLength(IMAGEDATA.z0Buffer, Length(IMAGEDATA.zBuffer));
if Length(IMAGEDATA.zBuffer) > 0 then
Move(IMAGEDATA.zBuffer[0], IMAGEDATA.z0Buffer[0], Length(IMAGEDATA.zBuffer) *
SizeOf(Tvector3_single));
//HITMOUSE := raymarch(CAM.pos, calculateRayDir(IMAGEDATA.TEX_WIDTH, IMAGEDATA.TEX_HEIGHT, x, y));
HITMOUSE := IMAGEDATA.z0Buffer[tY * IMAGEDATA.TEX_WIDTH + tX];
origMouseX := X;
origMouseY := Y;
end;
procedure TFormMain.OpenGLControl1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: integer);
var
dir: Tvector3_single;
tX, tY, dX, dY: integer;
fac, r: single;
qYaw, qPitch: TVector4_single;
pitchAxis: TVector3_single;
localV: TVector3_single;
brushCenter: TVector3_single;
brushRadius: single;
begin
dX := origMouseX - X;
dY := origMouseY - Y;
if ssRight in Shift then
begin
// Shift + RMB Panning
if ssShift in Shift then
begin
fac := 5.0;
CAM.pos := CAM.pos + CAM.right * dX * fac / Width + CAM.up *
dY * fac / Height;
CAM.target := CAM.target + CAM.right * dX * fac / Width + CAM.up *
dY * fac / Height;
LowerResolutionOnce;
ResizeTimer.Enabled := False;
ResizeTimer.Enabled := True;
OpenGLControl1.Invalidate; // Repaint;
origMouseX := X;
origMouseY := Y;
Exit;
end;
// Alt + RMB Dolly
if ssCtrl in Shift then
begin
fac := 3.0;
if Abs(dX) > Abs(dY) then
begin
CAM.pos += CAM.front * fac * dX / Width;
CAM.target += CAM.front * fac * dX / Width;
end
else
begin
CAM.pos -= CAM.front * fac * dY / Height;
CAM.target -= CAM.front * fac * dY / Height;
end;
LowerResolutionOnce;
ResizeTimer.Enabled := False;
ResizeTimer.Enabled := True;
OpenGLControl1.Invalidate; //Repaint macos
origMouseX := X;
origMouseY := Y;
Exit;
end;
// RMB Rotation: todo: tranlsate the camera to the target point
// 0,0,0 transform and translate it back
fac := 0.3;
dir := CAM.pos - CAM.target;
r := dir.length;
// yaw around worldUp
qYaw := makeQuat(CAM.worldUp, DegToRad(dX * fac));
CAM.Q := quatNormalize(QuatMultiply(qYaw, CAM.Q));
// pitch around camera's local right-axis
localV.init(1, 0, 0);
pitchAxis := quatRotateVec(localV, CAM.Q);
qPitch := makeQuat(pitchAxis, DegToRad(-dY * fac));
CAM.Q := quatNormalize(QuatMultiply(qPitch, CAM.Q));
CAM.updateCameraVectors;
CAM.pos := CAM.target - CAM.front * r;
LowerResolutionOnce;
DIRTYRAYS := True;
ResizeTimer.Enabled := False;
ResizeTimer.Enabled := True;
OpenGLControl1.Invalidate;
end;
// sdf sculpt
if ssLeft in Shift then
begin
tX := X div RES;
tY := EnsureRange(IMAGEDATA.TEX_HEIGHT - (Y div RES), 0, IMAGEDATA.TEX_HEIGHT);
if (Abs(dX) < 15) and (Abs(dY) < 15) then Exit;
HITMOUSE := IMAGEDATA.z0Buffer[tY * IMAGEDATA.TEX_WIDTH + tX];
if HITMOUSE.Data[0] = MAX_DIST then Exit;
brushCenter := HITMOUSE;
brushRadius := 0.25; // Smaller brush for more detail
//SCENE.addSphere(brushCenter, brushRadius);
//SCENE.rebuildBoundTree;
OpenGLControl1.Invalidate;
end;
origMouseX := X;
origMouseY := Y;
end;
procedure TFormMain.OpenGLControl1MouseWheel(Sender: TObject;
Shift: TShiftState; WheelDelta: integer; MousePos: TPoint; var Handled: boolean);
var
fac: single;
begin
DIRTYRAYS := True;
ResizeTimer.Enabled := False;
ResizeTimer.Enabled := True;
fac := 0.03;
CAM.zoom := CAM.zoom + WheelDelta * fac;
LowerResolutionOnce;
Repaint;
end;
procedure TFormMain.ResizeTimerTimer(Sender: TObject);
begin
ResizeTimer.Enabled := False;
LOWERES := False;
DIRTYRAYS := True;
IMAGEDATA.resize(OpenGLControl1.Width div RES, OpenGLControl1.Height div RES);
OpenGLControl1.Repaint;
end;
procedure TFormMain.OpenGLControl1Resize(Sender: TObject);
begin
// Reset the timer every time resize happens
// While you're still resizing quickly, the timer
// is constantly reset and never fires.
ResizeTimer.Enabled := False;
ResizeTimer.Enabled := True;
DIRTYRAYS := True;
//OpenGLControl1.MakeCurrent;
glViewport(0, 0, OpenGLControl1.Width, OpenGLControl1.Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, OpenGLControl1.Width, 0, OpenGLControl1.Height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
IMAGEDATA.resize(OpenGLControl1.Width div LOW_RES, OpenGLControl1.Height div LOW_RES);
Invalidate;
end;
{$define SMT}
procedure TFormMain.OpenGLControl1Paint(Sender: TObject);
procedure DrawScreenQuad;
begin
//glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
try
glTexCoord2f(0, 0);
glVertex2i(0, 0);
glTexCoord2f(1, 0);
glVertex2i(OpenGLControl1.Width, 0);
glTexCoord2f(1, 1);
glVertex2i(OpenGLControl1.Width, OpenGLControl1.Height);
glTexCoord2f(0, 1);
glVertex2i(0, OpenGLControl1.Height);
finally
glEnd;
end;
end;
procedure updateDisplayTexture;
begin
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//glBindTexture(GL_TEXTURE_2D, IMAGEDATA.FTextureID);
glTexSubImage2D(
GL_TEXTURE_2D, // Target texture type
0, // Mipmap level
0, 0, // x and y offset
IMAGEDATA.TEX_WIDTH, // Width of the data to update
IMAGEDATA.TEX_HEIGHT, // Height of the data to update
GL_RGB, // Format of the pixel data (Red, Green, Blue)
GL_UNSIGNED_BYTE, // Data type of each color component
@IMAGEDATA.colorBuffer[0] // Pointer to our CPU-side data
);
end;
var
i: integer;
{$ifdef SMT}
threads: array of TThreadID;
{$endif}
startTime, endTime: QWord;
begin
startTime := GetTickCount64();
// OpenGLControl1.MakeCurrent;
//glClearColor(0, 0, 0, 1.0);
//glClear(GL_COLOR_BUFFER_BIT);
ASPECTRATIO := IMAGEDATA.TEX_WIDTH / IMAGEDATA.TEX_HEIGHT;
CAMFACTOR := tan(DegToRad(CAM.zoom * 0.5));
{$ifdef SMT}
SetLength(threads, NUMTHREADS);
for i := 0 to NUMTHREADS - 1 do
threads[i] := BeginThread(@RenderChunkThread, Pointer(i));
for i := 0 to NUMTHREADS - 1 do
WaitForThreadTerminate(threads[i], 0);
{$else}
//renderPipelineCone(IMAGEDATA, 0, IMAGEDATA.TEX_HEIGHT);
renderPipelineDebug(IMAGEDATA, 0, IMAGEDATA.TEX_HEIGHT);
{$endif}
updateDisplayTexture;
DrawScreenQuad;
if DIRTYRAYS then
DIRTYRAYS := False;
OpenGLControl1.SwapBuffers;
endTime := GetTickCount64();
StatusBar1.SimpleText := scene.stats();
//StatusBar1.SimpleText := 'Frametime: ' + IntToStr(endTime - startTime) +
// ' | FPS: ' + IntToStr(1000 div (endTime - startTime + 1));
end;
end.