-
Notifications
You must be signed in to change notification settings - Fork 33
/
mtlvolume2.pas
1528 lines (1461 loc) · 58.8 KB
/
mtlvolume2.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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit mtlvolume2;
{$mode objfpc}
{$modeswitch objectivec1}
{$H+}
interface
{$DEFINE GPUGRADIENTS} //Computing volume gradients on the GPU is much faster than using the CPU
{$DEFINE VIEW2D}
{$DEFINE CUBE}
{$DEFINE MATCAP}
{$DEFINE CLRBAR}
{$DEFINE OLDDEPTHPICKER}
{$DEFINE TIMER} //reports GPU gradient time to stdout (Unix only)
{$include opts.inc} //for DEFINE DEPTHPICKER
uses
CocoaAll, MacOSAll,
//CFBase, CGImage, CGDataProvider, CGColorSpace, MetalKit, CocoaAll,
{$IFDEF MATCAP} intfgraphics, graphtype, Graphics, {$ENDIF}
{$IFDEF CUBE} Forms, mtlcube, {$ENDIF}
{$IFDEF CLRBAR} mtlclrbar, {$ENDIF}
{$IFDEF TIMER} DateUtils,{$ENDIF}
mtlfont, VectorMath, MetalPipeline, MetalControl, Metal, MetalUtils,
SysUtils, Math, nifti, niftis, SimdUtils, Classes
{$IFDEF VIEW2D}, drawvolume, slices2D, colorEditor{$ENDIF};
const
kDefaultDistance = 2.25;
kMaxDistance = 40;
kQualityMedium = 3;
kQualityBest = 5;
type
TGPUVolume = class
private
{$IFDEF VIEW2D}
colorEditorVertexBuffer, sliceVertexBuffer, renderVertexBuffer, lineVertexBuffer: MTLBufferProtocol;
{$IFDEF GPUGRADIENTS}blurShader, sobelShader,{$ENDIF}
shader2D, shader2Dn, shaderLines2D: TMetalPipeline;
slices2D: TSlices2D;
colorEditor: TColorEditor;
isSmooth2D, colorEditorVisible: boolean;
txt: TGPUFont;
drawVolTex, drawVolLut: MTLTextureProtocol;
{$ENDIF}
RayCastQuality1to5, maxDim, fAzimuth, fElevation, fPitch: integer;
shaderPrefs: TShaderPrefs;
prefValues: array [1..kMaxUniform] of single;
fDistance, overlayNum: single;
fLightPos, fClipPlane: TVec4;
indexBuffer, vertexBuffer: MTLBufferProtocol;
mvp: TMat4;
viewportXYWH: TVec4;
{$IFDEF LINE3D}
line3DBuffer: MTLBufferProtocol;
{$ENDIF}
{$IFDEF LINE3D}shaderLine3D, shaderLine3Dalpha, {$ENDIF}{$IFDEF OLDDEPTHPICKER}shaderDepth,{$ENDIF} shader3D, shader3Dbetter : TMetalPipeline;
volTex, gradTex, overlayVolTex, overlayGradTex: MTLTextureProtocol;
{$IFDEF MATCAP} matCapTex: MTLTextureProtocol;{$ENDIF}
mtlControl: TMetalControl;
{$IFDEF CLRBAR}clrbar: TGPUClrbar; {$ENDIF}
{$IFDEF CUBE} gCube :TGPUCube; {$ENDIF}
procedure LoadCube();
procedure LoadTexture(var vol: TNIfTI; deferGradients: boolean);
procedure CreateDrawColorTable;//1D texture for drawing
procedure CreateDrawTex(Dim: TVec3i; Vals: TUInt8s);
procedure UpdateDraw(Drawing: TDraw);
procedure CreateOverlayTextures(Dim: TVec3i; volRGBA: TRGBAs);
procedure CreateGradientVolumeGPU(Xsz,Ysz,Zsz: integer; var inTex, grTex: MTLTextureProtocol);
public
ClipThick: single;
renderBitmapWidth: integer;
matcapLoc: integer;
UseDepthShader: boolean;
{$IFDEF VIEW2D}
SelectionRect: TVec4;
property ShowColorEditor: boolean read colorEditorVisible write colorEditorVisible;
property ShowSmooth2D: boolean read isSmooth2D write isSmooth2D;
//function ColorEditorMouseDown(mouseX, mouseY: integer): boolean;
function Slice2Dmm(var vol: TNIfTI; out vox: TVec3i): TVec3;
//function FracVox: TVec3i;
property CE: TColorEditor read colorEditor;
property Slices: Tslices2D read slices2D;
procedure SetSlice2DFrac(frac : TVec3);
function GetSlice2DFrac(mouseX, mouseY: integer; out Orient: integer): TVec3;
function Unproject(mouseX, mouseY, depth: single): TVec3;
function GetSlice2DMaxXY(mouseX, mouseY: integer; var Lo: TPoint): TPoint;
procedure Paint2D(var vol: TNIfTI; Drawing: TDraw; DisplayOrient: integer);
procedure PaintMosaicRender(var vol: TNIfTI; lRender: TMosaicRender);
procedure PaintMosaic2D(var vol: TNIfTI; Drawing: TDraw; MosaicString: string);
{$ENDIF}
procedure UpdateOverlays(vols: TNIfTIs);
property Quality1to5: integer read RayCastQuality1to5 write RayCastQuality1to5;
property ShaderSliders: TShaderPrefs read shaderPrefs write shaderPrefs;
property Azimuth: integer read fAzimuth write fAzimuth;
property Elevation: integer read fElevation write fElevation;
property Pitch: integer read fPitch write fPitch;
property Distance: single read fDistance write fDistance;
property LightPosition: TVec4 read fLightPos write fLightPos;
property ClipPlane: TVec4 read fClipPlane write fClipPlane;
procedure Prepare(shaderName: string);
constructor Create(fromView: TMetalControl);
procedure PaintCrosshair3D(var vol: TNIfTI; rgba: TVec4);
procedure PaintCore(var vol: TNIfTI; widthHeightLeft: TVec3i; clearScreen: boolean = true; isDepthShader: boolean = false);
procedure Paint(var vol: TNIfTI);
procedure PaintDepth(var vol: TNIfTI; isAxCorSagOrient4: boolean = false);
procedure SetShader(shaderName: string; isUpdatePrefs: boolean = true);
procedure SetShaderSlider(idx: integer; newVal: single);
procedure SaveBmp(filename: string; hasAlpha: boolean);
function ReadPixel(x, y: integer): UInt32;
function ReadDepth(x,y: integer): single;
{$IFDEF CLRBAR} procedure SetColorBar(fromColorbar: TGPUClrbar); {$ENDIF}
procedure SetTextContrast(clearclr: TRGBA);
{$IFDEF MATCAP} function SetMatCap(fnm: string): boolean; {$ENDIF}
destructor Destroy; override;
end;
implementation
//uses mainunit;
function gluUnProject(winXYZ: TVec3; mvp: TMat4; viewportXYWH: TVec4): TVec3;
//viewport[0]=x, viewport[1]=y, viewport[2]=width, viewport[3]=height
//return coordinates in object space
(*vec4 v = vec4(2.0*(gl_FragCoord.x-view.x)/view.z-1.0,
2.0*(gl_FragCoord.y-view.y)/view.w-1.0,
2.0*texture2DRect(DepthTex,gl_FragCoord.xy).z-1.0,
1.0 );
v = gl_ModelViewProjectionMatrixInverse * v;
v /= v.w;*)
//https://community.khronos.org/t/converting-gl-fragcoord-to-model-space/57397
//https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluUnProject.xml
//http://nehe.gamedev.net/article/using_gluunproject/16013/
var
v: TVec4;
mvpInv: TMat4;
begin
v := vec4(2.0*(winXYZ.x-viewportXYWH.x)/viewportXYWH.z-1.0,
2.0*(winXYZ.y-viewportXYWH.y)/viewportXYWH.w-1.0,
2.0*winXYZ.z-1.0,
1.0);
mvpInv := mvp.inverse;
v := mvpInv * v;
v := v / v.w;
result := vec3(v.x, v.y, v.z);
end;
function TGPUVolume.Unproject(mouseX, mouseY, depth: single): TVec3;
var
winXYZ: TVec3;
begin
winXYZ := vec3(mouseX, mouseY, depth);
result := gluUnProject(winXYZ, mvp, viewportXYWH);
//printf(format('windowXYZ %g %g %g', [winXYZ.x, winXYZ.y, winXYZ.z]));
//printf(format('objXYZ %g %g %g', [result.x, result.y, result.z]));
end;
type
TVertVertex = record //Each vertex defines a location and color
position: TVec3;
padding: array[0..0] of TScalar;
color: TVec4;
end;
TVertVertexArray = array of TVertVertex;
TVertUniforms = record //Uniforms for vertex shader
modelViewProjectionMatrix: TMat4;
end;
TFragUniforms = record //Uniforms for fragment shader
stepSize, sliceSize, numOverlays, clipThick: TScalar;
backAlpha, pad1, pad2, pad3: TScalar;
rayDir: TVec4;
lightPos: TVec4;
clipPlane: TVec4;
normMatrix, modelViewProjectionMatrix: TMat4;
end;
{$IFDEF LINE3D}
var
gLines3Dv: array of TVertVertex = nil;
gLines3DnIdx: integer = 0;
gLines3DIndexBuffer: MTLBufferProtocol = nil;
{$ENDIF}
destructor TGPUVolume.Destroy;
begin
{$IFDEF VIEW2D}
slices2D.free;
colorEditor.free;
txt.free;
{$ENDIF}
{$IFDEF LINE3D}
gLines3Dv := nil;
gLines3DIndexBuffer := nil;
{$ENDIF}
{$IFDEF CUBE} gCube.free; {$ENDIF}
{$IFDEF CLRBAR} clrbar.free; {$ENDIF}
inherited;
end;
{$IFDEF MATCAP}
procedure FlipVertical (var px: TPicture);
var
p: array of byte;
i, half, b: integer;
LoPtr, HiPtr: PInteger;
begin
if px.Height < 3 then exit;
half := (px.Height div 2);
b := px.Bitmap.RawImage.Description.BytesPerLine;
LoPtr := PInteger(px.Bitmap.RawImage.Data);
HiPtr := PInteger(px.Bitmap.RawImage.Data+ ((px.Height -1) * b));
setlength(p, b);
for i := 1 to half do begin
System.Move(LoPtr^,p[0],b); //(src, dst,sz)
System.Move(HiPtr^,LoPtr^,b); //(src, dst,sz)
System.Move(p[0],HiPtr^,b); //(src, dst,sz)
Inc(PByte(LoPtr), b );
Dec(PByte(HiPtr), b);
end;
end; //FlipVertical()
procedure CreateBmp(var px: TPicture);
Type
TRGBquad = PACKED RECORD
rgbBlue,rgbGreen,rgbRed,rgbAlpha: byte;
end;
TQuadRA = array [1..1] of TRGBQuad;
RGBQuadp = ^TQuadRA;
function rgb2quad(R,G,B: Byte): TRGBquad;
begin
result.rgbRed:= (R);
result.rgbGreen:= (G);
result.rgbBlue:= (B);
result.rgbAlpha:= 0;
end;
const
x = 256;
y = 256;
var
i, j, k: integer;
lBuff: RGBQuadp;
Ptr: pointer;
AImage: TLazIntfImage;
lRawImage: TRawImage;
begin
GetMem(lBuff, x*y* sizeof(TRGBQuad));
k := 1;
for j := 1 to y do
for i := 1 to x do begin
//lBuff^[k] := rgb2quad(256-i,i-1,256-j);
lBuff^[k] := rgb2quad(256-j,256-j,256-j);
k := k + 1;
end;
lRawImage.Init;
//lRawImage.Description.Init_BPP32_B8G8R8A8_BIO_TTB(0,0);
lRawImage.Description.Init_BPP32_R8G8B8A8_BIO_TTB(x,y);
lRawImage.CreateData(true);
AImage := TLazIntfImage.Create(x,y);
AImage.SetRawImage(lRawImage);
AImage.BeginUpdate;
i := 1;
for j := 0 to (y-1) do begin
ptr := AImage.GetDataLineStart(j);
Move(lBuff^[i], Ptr^, x * sizeof(TRGBQuad));
inc(i, x);
end;
AImage.EndUpdate;
px.Bitmap.LoadFromIntfImage(AImage);
FreeMem(lBuff);
end;
function TGPUVolume.SetMatCap(fnm: string): boolean;
var
px: TPicture;
bmpHt, bmpWid: integer;
isPng : boolean;
pngTexDesc: MTLTextureDescriptor;
pngRegion: MTLRegion;
AImage: TLazIntfImage;
lRawImage: TRawImage;
//ifnm, MatCapDir: string;
begin
result := false;
if (not fileexists(fnm)) and (fnm <> '') then begin
//MatCapDir := ExtractFilePath(ShaderDir)+ 'matcap';
fnm := ExtractFilePath(ShaderDir)+ 'matcap'+pathdelim+fnm+'.jpg';
end;
//if (fnm <> '') and (not fileexists(fnm)) then begin
px := TPicture.Create;
if not fileexists(fnm) then begin
if fnm <> '' then
writeln('Unable to find MatCap "'+fnm+'"');
CreateBmp(px)
end else begin
isPng := upcase(ExtractFileExt(fnm)) = '.PNG';
try
if isPng then
px.LoadFromFile(fnm)
else begin
lRawImage.Init;
//lRawImage.Description.Init_BPP32_B8G8R8A8_BIO_TTB(0,0);
lRawImage.Description.Init_BPP32_R8G8B8A8_BIO_TTB(0,0);
lRawImage.CreateData(false);
AImage := TLazIntfImage.Create(0,0);
try
AImage.SetRawImage(lRawImage);
AImage.LoadFromFile(fnm);
px.Bitmap.LoadFromIntfImage(AImage);
finally
AImage.Free;
end;
end;
except
px.Bitmap.Width:=0;
end;
end;
if (px.Bitmap.PixelFormat <> pf32bit ) or (px.Bitmap.Width < 1) or (px.Bitmap.Height < 1) then begin
writeln('Error loading 32-bit MatCap '+fnm);
exit;
end;
FlipVertical(px);
bmpHt := px.Bitmap.Height;
bmpWid := px.Bitmap.Width;
if px.Bitmap.PixelFormat <> pf32bit then
exit; //distance stored in ALPHA field
pngTexDesc := MTLTextureDescriptor.alloc.init.autorelease;
pngTexDesc.setTextureType(MTLTextureType2D);
if isPng then
pngTexDesc.setPixelFormat(MTLPixelFormatBGRA8Unorm)
else
pngTexDesc.setPixelFormat(MTLPixelFormatRGBA8Unorm);
//pngTexDesc.setPixelFormat(MTLPixelFormatBGRA8Unorm);
pngTexDesc.setWidth(bmpWid);
pngTexDesc.setHeight(bmpHt);
pngTexDesc.setDepth(1);
if (matCapTex <> nil) then matCapTex.release;
matCapTex := mtlControl.renderView.device.newTextureWithDescriptor(pngTexDesc);
Fatal(matCapTex = nil, format('mtlfont: newTextureWithDescriptor failed %dx%d', [bmpHt, bmpWid]));
pngRegion := MTLRegionMake3D(0, 0, 0, bmpWid, bmpHt, 1);
matCapTex.replaceRegion_mipmapLevel_withBytes_bytesPerRow(pngRegion, 0, PInteger(px.Bitmap.RawImage.Data), bmpWid*4);
px.Free;
result := true;
end;
{$ENDIF}
procedure TGPUVolume.SetTextContrast(clearclr: TRGBA);
begin
if (clearclr.R + clearclr.G + clearclr.B) > 300 then
txt.FontColor := Vec4(0,0,0,1)
else
txt.FontColor := Vec4(1,1,1,1);
end;
procedure TGPUVolume.SetShaderSlider(idx: integer; newVal: single);
begin
if (idx < 1) or (idx > kMaxUniform) then exit;
prefValues[idx] :=newVal;
end;
function TGPUVolume.ReadPixel(x, y: integer): UInt32;
begin
result := MTLReadPixel(x, y);
end;
function TGPUVolume.ReadDepth(x,y: integer): single;
begin
result := MTLReadDepth(x, y);
end;
procedure TGPUVolume.SaveBmp(filename: string; hasAlpha: boolean);
begin
if filename = '' then
MTLWriteTextureToClipboard(hasAlpha)
else
MTLWriteTextureToFile(pChar(filename), hasAlpha);
end;
procedure TGPUVolume.SetShader(shaderName: string; isUpdatePrefs: boolean = true);
var
options: TMetalPipelineOptions;
i: integer;
//str: NSString;
//Attrib: NSDictionary;
//defaultLibrary: MTLLibraryProtocol;
libraryOptions : TMetalLibraryOptions;
begin
if not isUpdatePrefs then exit; //only for opengl switching between "Better"
options := TMetalPipelineOptions.Default;
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find shader ' + shaderName);
options.pipelineDescriptor := MTLCreatePipelineDescriptor;
options.pipelineDescriptor.colorAttachmentAtIndex(0).setBlendingEnabled(true);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setRgbBlendOperation(MTLBlendOperationAdd);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setAlphaBlendOperation(MTLBlendOperationAdd);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setSourceRGBBlendFactor(MTLBlendFactorSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setSourceAlphaBlendFactor(MTLBlendFactorSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setDestinationRGBBlendFactor(MTLBlendFactorOneMinusSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setDestinationAlphaBlendFactor(MTLBlendFactorOneMinusSourceAlpha);
options.fragmentShader:= 'fragmentShader';
shader3D := MTLCreatePipeline(options);
libraryOptions := TMetalLibraryOptions.Default;
libraryOptions.name := shaderName;
libraryOptions.preprocessorMacros := NSDictionary.dictionaryWithObject_forKey(NSSTR('CUBIC'), NSSTR('CUBIC'));
options.shaderLibrary := MTLCreateLibrary(libraryOptions);
shader3Dbetter := MTLCreatePipeline(options);
//if not isUpdatePrefs then exit;
shaderPrefs := loadShaderPrefs(shaderName);
matcapLoc := -1;
if (shaderPrefs.nUniform > 0) and (shaderPrefs.nUniform <= kMaxUniform) then
for i := 1 to shaderPrefs.nUniform do begin
if (shaderPrefs.Uniform[i].Widget = 3) then begin
matcapLoc := 1;
shaderPrefs.nUniform := shaderPrefs.nUniform - 1;
break;
end;
prefValues[i] := shaderPrefs.Uniform[i].DefaultV;
end;
MTLSetDepthStencil(shader3D, MTLCompareFunctionLess, true);//TQ
end;
procedure TGPUVolume.Prepare(shaderName: string);
var
{$IFDEF VIEW2D}
options: TMetalPipelineOptions;
success: boolean;
{$ENDIF}
begin
if (shaderName = '') or (not fileexists(shaderName)) then
shaderName := ShaderDir+pathdelim+'Default.metal';
SetShader(shaderName);
{$IFDEF VIEW2D}
options := TMetalPipelineOptions.Default;
//default shader
shaderName := ShaderDir+pathdelim+'_Texture2D.metal';
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find ' + shaderName);
options.pipelineDescriptor := MTLCreatePipelineDescriptor;
options.pipelineDescriptor.colorAttachmentAtIndex(0).setBlendingEnabled(true);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setRgbBlendOperation(MTLBlendOperationAdd);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setAlphaBlendOperation(MTLBlendOperationAdd);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setSourceRGBBlendFactor(MTLBlendFactorSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setSourceAlphaBlendFactor(MTLBlendFactorSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setDestinationRGBBlendFactor(MTLBlendFactorOneMinusSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setDestinationAlphaBlendFactor(MTLBlendFactorOneMinusSourceAlpha);
shader2D := MTLCreatePipeline(options);
//nearest neighbor shader
shaderName := ShaderDir+pathdelim+'_Texture2Dn.metal';
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find ' + shaderName);
shader2Dn := MTLCreatePipeline(options);
//line shades
shaderName := ShaderDir+pathdelim+'_Line2D.metal';
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find ' + shaderName);
shaderLines2D := MTLCreatePipeline(options);
MTLSetDepthStencil(shaderLines2D, MTLCompareFunctionLess, false);
//depth picker
{$IFDEF OLDDEPTHPICKER}
UseDepthShader := false;
shaderName := ShaderDir+pathdelim+'_Depth3D.metal';
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find ' + shaderName);
shaderDepth := MTLCreatePipeline(options);
{$ENDIF}
options := TMetalPipelineOptions.Default;
shaderName := ShaderDir+pathdelim+'_Blur3d.metal';
{$IFDEF GPUGRADIENTS}
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find ' + shaderName);
options.kernelFunction := 'sobelKernel'; //blur kernel
sobelShader := MTLCreatePipeline(options);
options.kernelFunction := 'blurKernel'; //blur kernel
blurShader := MTLCreatePipeline(options);
{$ENDIF}
//Create(fnm : string; out success: boolean; var fromView: TMetalControl);
colorEditor := TColorEditor.Create;
//colorEditorVisible := false;
Txt := TGPUFont.Create(ResourcePath('Roboto', 'png'), success, mtlControl); //<-multi-channel channel fonts glmtext
slices2D := TSlices2D.Create(Txt);
{$IFDEF CUBE}
gCube := TGPUCube.Create(mtlControl);
if (Screen.PixelsPerInch < 100) then
gCube.Size:= gCube.Size * 1.5; //Check Darwin Retina vs Windows HiDPI
{$ENDIF}
CreateDrawTex(pti(4,4,4), nil);
CreateDrawColorTable;
{$ENDIF}
{$IFDEF MATCAP}
if matCapTex = nil then
SetMatCap('');
{$ENDIF}
{$IFDEF LINE3D}
options := TMetalPipelineOptions.Default;
shaderName := ShaderDir+pathdelim+'_Line3D.metal';
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find ' + shaderName);
options.pipelineDescriptor := MTLCreatePipelineDescriptor;
options.pipelineDescriptor.colorAttachmentAtIndex(0).setBlendingEnabled(true);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setRgbBlendOperation(MTLBlendOperationAdd);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setAlphaBlendOperation(MTLBlendOperationAdd);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setSourceRGBBlendFactor(MTLBlendFactorSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setSourceAlphaBlendFactor(MTLBlendFactorSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setDestinationRGBBlendFactor(MTLBlendFactorOneMinusSourceAlpha);
options.pipelineDescriptor.colorAttachmentAtIndex(0).setDestinationAlphaBlendFactor(MTLBlendFactorOneMinusSourceAlpha);
shaderLine3D := MTLCreatePipeline(options);
MTLSetDepthStencil(shaderLine3D, MTLCompareFunctionLess, true);//TQ
//alpha shader does not use
shaderName := ShaderDir+pathdelim+'_Line3Dalpha.metal';
options.libraryName := shaderName;
if not fileexists(shaderName) then
writeln('Unable to find ' + shaderName);
shaderLine3Dalpha := MTLCreatePipeline(options);
MTLSetDepthStencil(shaderLine3Dalpha, MTLCompareFunctionGreaterEqual, true);//TQ
line3DBuffer := nil;
{$ENDIF} //LINE3D
end;
{$IFDEF CLRBAR}
procedure TGPUVolume.SetColorBar(fromColorbar: TGPUClrbar);
begin
clrbar := fromColorbar;
end;
{$ENDIF}
function TGPUVolume.Slice2Dmm(var vol: TNIfTI; out vox: TVec3i): TVec3;
begin
if (slices2D = nil) then exit;
result := slices2D.FracMM(vol.Mat, vol.Dim, vox);
end;
procedure TGPUVolume.SetSlice2DFrac(frac : TVec3);
begin
if (slices2D = nil) or (frac.x < 0.0) or (frac.y < 0.0) or (frac.z < 0.0) then exit;
slices2D.sliceFrac := frac;
end;
constructor TGPUVolume.Create(fromView: TMetalControl);
begin
mtlControl := fromView;
shader3Dbetter := nil;
{$IFDEF CLRBAR}clrbar := nil;{$ENDIF}
slices2D := nil;
volTex := nil;
gradTex := nil;
overlayVolTex := nil;
overlayGradTex := nil;
overlayNum := 0;
fDistance := kDefaultDistance;
colorEditorVertexBuffer := nil;
sliceVertexBuffer := nil;
renderVertexBuffer := nil;
lineVertexBuffer := nil;
fAzimuth := 110;
fElevation := 30;
fPitch := 0;
RaycastQuality1to5 := 5;
SelectionRect := Vec4(-1,0,0,0);
fLightPos := Vec4(0, 0.707, 0.707, 0);
//fLightPos := Vec4(0, 0.087, 0.996, 0);
fClipPlane := Vec4(0, 0, 0, -1);
ClipThick := 1.0;
matcapLoc := 1;
//fLightPos := Vec4(0,0.0,0.707, 0.0);
//fClearColor.r := 200;
//fClearColor.g := 200;
//fClearColor.b := 255;
vertexBuffer := nil;
colorEditorVisible := false;
isSmooth2D := true;
shaderPrefs.nUniform:= 0;
{$IFDEF MATCAP}
matCapTex := nil;
//SetMatCap(ResourceFolderPath+pathdelim+'matcap'+pathdelim+'RedPlastic.jpg');
//SetMatCap('');
{$ENDIF}
end;
function VertVertex(x, y, z: TScalar): TVertVertex;
begin
result.position := V3(x,y,z);
result.color := V4(x,y,z, 1);
end;
procedure TGPUVolume.LoadCube();
const
mtlFaces: array[0..13] of uint16 = (1,0,4,3,7,6,4,5,1,6,2,3,1,0);
//mtlFaces: array[0..13] of uint16 = (0,1,3,2,6,1,5,4, 6,7,3, 4, 0, 1);
var
v0,v1,v2, v3,v4,v5, v6, v7:TVertVertex;
vertices: array of TVertVertex;
begin
v0 := VertVertex(0,0,0);
v1 := VertVertex(0,1,0);
v2 := VertVertex(1,1,0);
v3 := VertVertex(1,0,0);
v4 := VertVertex(0,0,1);
v5 := VertVertex(0,1,1);
v6 := VertVertex(1,1,1);
v7 := VertVertex(1,0,1);
vertices := TVertVertexArray.Create(v0,v1,v2,v3,v4,v5,v6,v7);
indexBuffer := mtlControl.renderView.device.newBufferWithBytes_length_options(@mtlFaces[0], sizeof(uint16) * Length(mtlFaces), MTLResourceStorageModeShared);
vertexBuffer := mtlControl.renderView.device.newBufferWithBytes_length_options(@vertices[0], sizeof(TVertVertex) * Length(vertices), MTLResourceStorageModeShared);
end;
procedure TGPUVolume.CreateGradientVolumeGPU(Xsz,Ysz,Zsz: integer; var inTex, grTex: MTLTextureProtocol);
{$IFDEF GPUGRADIENTS}
var
grTexDesc: MTLTextureDescriptor;
threadgroupSize: MTLSize;
threadgroupCount: MTLSize;
tempTex: MTLTextureProtocol;
{$IFDEF TIMER}StartTime: TDateTime;{$ENDIF}
begin
{$IFDEF TIMER}startTime := now;{$ENDIF}
grTexDesc := MTLTextureDescriptor.alloc.init.autorelease;
grTexDesc.setTextureType(MTLTextureType3D);
grTexDesc.setUsage(MTLTextureUsageShaderWrite or MTLTextureUsageShaderRead);
grTexDesc.setPixelFormat(MTLPixelFormatRGBA8Unorm);
grTexDesc.setWidth(Xsz);
grTexDesc.setHeight(Ysz);
grTexDesc.setDepth(Zsz);
//grTex := nil; //789
if (grTex <> nil) then grTex.release;
grTex := mtlControl.renderView.device.newTextureWithDescriptor(grTexDesc);
Fatal(grTex = nil, 'CreateGradientVolumeGPU: newTextureWithDescriptor failed');
tempTex := mtlControl.renderView.device.newTextureWithDescriptor(grTexDesc);
threadgroupSize := MTLSizeMake(8, 8, 8);
threadgroupCount.width := (Xsz + threadgroupSize.width - 1) div threadgroupSize.width;
threadgroupCount.height := (Ysz + threadgroupSize.height - 1) div threadgroupSize.height;
threadgroupCount.depth := (Zsz + threadgroupSize.depth - 1) div threadgroupSize.depth;
MTLBeginCommand;
MTLBeginEncoding(blurShader);
MTLSetTexture(inTex, 0); //in
MTLSetTexture(tempTex, 1);//out
MTLSetDispatchThreadgroups(threadgroupCount, threadgroupSize);
MTLEndEncoding;
MTLBeginEncoding(sobelShader);
MTLSetTexture(tempTex, 0); //in
MTLSetTexture(grTex, 1);//out
MTLSetDispatchThreadgroups(threadgroupCount, threadgroupSize);
MTLEndEncoding;
//MTLEndCommand;
MTLEndCommand(true); //<- syncrhonous: waitUntilCompleted, reduce flicker
tempTex.release;
tempTex := nil;
{$IFDEF TIMER}writeln(format('GPU Gradient time %d',[MilliSecondsBetween(Now,startTime)]));{$ENDIF}
end;
{$ELSE}
begin
//
end;
{$ENDIF}
procedure TGPUVolume.CreateDrawColorTable;//1D texture for drawing
var
volRegion: MTLRegion;
volTexDesc: MTLTextureDescriptor;
i: integer;
colorLut: array [0..255] of TRGBA;
begin
colorLut[0] := setRGBA(0,0,0,0);
colorLut[1] := setRGBA(255,0,0,255);//red
colorLut[2] := setRGBA(0,128,0,255);//green
colorLut[3] := setRGBA(0,0,255,255);//blue
colorLut[4] := setRGBA(255,128,0,255);//orange
colorLut[5] := setRGBA(128,0,255,255);//purple
colorLut[6] := setRGBA(0,200,200,255);//cyan
colorLut[7] := setRGBA(160,48,48,255);//brick
colorLut[8] := setRGBA(32,255,32,255);//lime
colorLut[9] := setRGBA(128,160,230,255);//lightblue
for i := 10 to 255 do
colorLut[i] := colorLut[((i-1) mod 9)+1];
volTexDesc := MTLTextureDescriptor.alloc.init.autorelease;
volTexDesc.setTextureType(MTLTextureType1D);
volTexDesc.setPixelFormat(MTLPixelFormatRGBA8Unorm);//MTLPixelFormatRGBA8Uint
volTexDesc.setWidth(256);
volTexDesc.setHeight(1);
volTexDesc.setDepth(1);
//drawVolLut := nil; //789
if (drawVolLut <> nil) then drawVolLut.release;
drawVolLut := mtlControl.renderView.device.newTextureWithDescriptor(volTexDesc);
Fatal(drawVolLut = nil, 'drawVolume: newTextureWithDescriptor failed');
//i := 256;
//volRegion := MTLRegionMake1D(i);
volRegion := MTLRegionMake1D(0, 256);
drawVolLut.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage(volRegion, 0,0, @colorLut[0], 0, 0);
end;
procedure TGPUVolume.UpdateDraw(Drawing: TDraw);
begin
if not Drawing.NeedsUpdate then exit;
if Drawing.IsOpen then
CreateDrawTex(Drawing.Dim, Drawing.VolRawBytes)
else
CreateDrawTex(pti(4,4,4), nil);
Drawing.NeedsUpdate := false;
end;
procedure TGPUVolume.CreateDrawTex(Dim: TVec3i; Vals: TUInt8s);
var
volRegion: MTLRegion;
volTexDesc: MTLTextureDescriptor;
i, vx: int64;
v: TUInt8s;
begin
volTexDesc := MTLTextureDescriptor.alloc.init.autorelease;
volTexDesc.setTextureType(MTLTextureType3D);
volTexDesc.setPixelFormat(MTLPixelFormatR8Unorm);//MTLPixelFormatRGBA8Uint
volTexDesc.setWidth(Dim.X);
volTexDesc.setHeight(Dim.Y);
volTexDesc.setDepth(Dim.Z);
//drawVolTex := nil; //789
if drawVolTex <> nil then drawVolTex.release;
drawVolTex := mtlControl.renderView.device.newTextureWithDescriptor(volTexDesc);
Fatal(drawVolTex = nil, 'DrawTex: newTextureWithDescriptor failed');
volRegion := MTLRegionMake3D(0, 0, 0, Dim.X, Dim.Y, Dim.Z);
//GLForm1.caption := format('%d %d %d', [Dim.X, Dim.Y, Dim.Z]);
if (Vals = nil) then begin
vx := prod(Dim);
setlength(v,vx);
for i := 0 to (vx -1) do
v[i] := random(3);
drawVolTex.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage(volRegion, 0,0, @v[0], Dim.X, Dim.X*Dim.Y);
v := nil;
end else
drawVolTex.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage(volRegion, 0,0, @Vals[0], Dim.X, Dim.X*Dim.Y);
end;
procedure TGPUVolume.CreateOverlayTextures(Dim: TVec3i; volRGBA: TRGBAs);
var
{$IFNDEF GPUGRADIENTS}
gradRegion: MTLRegion;
gradData: TRGBAs;
{$ENDIF}
volRegion: MTLRegion;
volTexDesc: MTLTextureDescriptor;
begin
if (volRGBA = nil) then begin
Dim := pti(1,1,1);
setlength(volRGBA,1);
volRGBA[0] := setrgba(0,0,0,0);
overlayNum := 0; //perhaps overlays loaded but made transparent
end;
volTexDesc := MTLTextureDescriptor.alloc.init.autorelease;
volTexDesc.setTextureType(MTLTextureType3D);
volTexDesc.setPixelFormat(MTLPixelFormatRGBA8Unorm);//MTLPixelFormatRGBA8Uint
volTexDesc.setWidth(Dim.X);
volTexDesc.setHeight(Dim.Y);
volTexDesc.setDepth(Dim.Z);
//overlayVolTex := nil; //789
if overlayVolTex <> nil then overlayVolTex.release;
overlayVolTex := mtlControl.renderView.device.newTextureWithDescriptor(volTexDesc);
Fatal(overlayVolTex = nil, 'volTex: newTextureWithDescriptor failed');
volRegion := MTLRegionMake3D(0, 0, 0, Dim.X, Dim.Y, Dim.Z);
//GLForm1.caption := format('%d %d %d', [Dim.X, Dim.Y, Dim.Z]);
overlayVolTex.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage(volRegion, 0,0, @volRGBA[0], Dim.X*4, Dim.X*Dim.Y*4);
//if not skipGradientCalc then begin
{$IFDEF GPUGRADIENTS}
CreateGradientVolumeGPU(Dim.X, Dim.Y, Dim.Z, overlayVolTex, overlayGradTex);
{$ELSE}
//Vol.CreateGradientVolume (intensityData, Dim.X, Dim.Y, Dim.Z, gradData);
CreateGradientVolumeX (TUInt8s(volRGBA), Dim.X, Dim.Y, Dim.Z, 1, gradData);
volTexDesc.setUsage(MTLTextureUsageShaderWrite or MTLTextureUsageShaderRead);
if (overlayGradTex <> nil) then overlayGradTex.release;
overlayGradTex := mtlControl.renderView.device.newTextureWithDescriptor(volTexDesc);
Fatal(overlayGradTex = nil, 'newTextureWithDescriptor failed');
gradRegion := MTLRegionMake3D(0, 0, 0, Dim.X, Dim.Y, Dim.Z);
overlayGradTex.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage(gradRegion, 0,0, @gradData[0], Dim.X*4, Dim.X*Dim.Y*4);
gradData := nil;
{$ENDIF}
//end;
volRGBA := nil; //free
end;
procedure TGPUVolume.UpdateOverlays(vols: TNIfTIs);
var
intensityData: TRGBAs;
Vol: TNIfTI;
begin
if not vols.Layer(0,Vol) then exit;
overlayNum := vols.NumLayers -1; //-1 as we ignore background
if (overlayNum < 1) then begin //background only
//GLForm1.LayerBox.Caption := '->>'+inttostr(random(888));
if (overlayGradTex = nil) or (overlayGradTex.width > 1) then //e.g. update texture after user closes overlays
CreateOverlayTextures(Vol.Dim, nil); // <- empty overlay texture
exit; //no overlays
end;
if not vols.OverlaysNeedsUpdate then exit;
intensityData := vols.CreateOverlayVolume;
CreateOverlayTextures(Vol.Dim, intensityData);
end;
procedure TGPUVolume.LoadTexture(var vol: TNIfTI; deferGradients: boolean);
var
volTexDesc: MTLTextureDescriptor;
volRegion: MTLRegion;
{$IFNDEF GPUGRADIENTS}
//gradTexDesc: MTLTextureDescriptor;
gradData: TRGBAs;
gradRegion: MTLRegion;
{$ENDIF}
begin
{$IFDEF GPUGRADIENTS}
//see if gradients were previously deferred but now required
if (Vol.VolRGBA = nil) and (volTex <> nil) and(gradTex = nil) and (not deferGradients) then CreateGradientVolumeGPU(Vol.Dim.X, Vol.Dim.Y, Vol.Dim.Z, volTex, gradTex);
{$ENDIF}
if (Vol.VolRGBA = nil) then exit;
if gradTex <> nil then gradTex.release;
gradTex := nil;
maxDim := max(Vol.Dim.X,max(Vol.Dim.Y,Vol.Dim.Z));
volTexDesc := MTLTextureDescriptor.alloc.init.autorelease;
volTexDesc.setTextureType(MTLTextureType3D);
volTexDesc.setPixelFormat(MTLPixelFormatRGBA8Unorm);//MTLPixelFormatRGBA8Uint
volTexDesc.setWidth(Vol.Dim.X);
volTexDesc.setHeight(Vol.Dim.Y);
volTexDesc.setDepth(Vol.Dim.Z);
//volTex := nil; //789
if volTex <> nil then volTex.release;
volTex := nil;
volTex := mtlControl.renderView.device.newTextureWithDescriptor(volTexDesc);
Fatal(volTex = nil, 'loadTex: newTextureWithDescriptor failed');
volRegion := MTLRegionMake3D(0, 0, 0, Vol.Dim.X, Vol.Dim.Y, Vol.Dim.Z);
volTex.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage(volRegion, 0,0, @Vol.VolRGBA[0], Vol.Dim.X*4, Vol.Dim.X*Vol.Dim.Y*4);
//compute and load gradients
{$IFDEF GPUGRADIENTS}
if (not deferGradients) then
CreateGradientVolumeGPU(Vol.Dim.X, Vol.Dim.Y, Vol.Dim.Z, volTex, gradTex);
{$ELSE}
volTexDesc.setUsage(MTLTextureUsageShaderWrite or MTLTextureUsageShaderRead);
if gradTex <> nil then gradTex.release;
gradTex := mtlControl.renderView.device.newTextureWithDescriptor(volTexDesc);
Fatal(gradTex = nil, 'newTextureWithDescriptor failed');
gradRegion := MTLRegionMake3D(0, 0, 0, Vol.Dim.X, Vol.Dim.Y, Vol.Dim.Z);
gradData := Vol.GenerateGradientVolume;
//CreateGradientVolume (Vol.VolRGBA, Vol.Dim.X, Vol.Dim.Y, Vol.Dim.Z, gradData);
gradTex.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage(gradRegion, 0,0, @gradData[0], Vol.Dim.X*4, Vol.Dim.X*Vol.Dim.Y*4);
// volTex.replaceRegion_mipmapLevel_slice_withBytes_bytesPerRow_bytesPerImage( volRegion, 0,0, @gradData[0], Vol.Dim.X*4, Vol.Dim.X*Vol.Dim.Y*4);
gradData := nil;
{$ENDIF}
Vol.GPULoadDone;
if (overlayVolTex = nil) then CreateOverlayTextures(Vol.Dim, nil); //load blank overlay
end;
procedure addFuzz (var v: TVec4); //avoid shader compile by zero error
const
kEPS = 0.0001;
begin
if (abs(v.x) < kEPS) then v.x := kEPS;
if (abs(v.y) < kEPS) then v.y := kEPS;
if (abs(v.z) < kEPS) then v.z := kEPS;
if (abs(v.w) < kEPS) then v.w := kEPS;
end;
function lerp (p1,p2,frac: single): single;
begin
result := round(p1 + frac * (p2 - p1));
end;//linear interpolation
function ComputeStepSize (Quality1to5, Slices: integer): single;
var
i : integer;
f: single;
begin
//if (i = 0) then i := 2; //dynamic (1=0.4, 2=0.55 ... 5=1.0
i := Quality1to5 - 1;
i := max(i,0);
i := min(i,4); //quality 5 is same as 4 but adds cubic sampling
f := lerp(slices*0.4,slices*1.0, i/4); //0.4..1.0
if f < 10 then
f := 10;
result := 1/f;
end;
{$IFDEF VIEW2D}
type
TVertUniforms2D = record //Uniforms for vertex shader
viewportSize: TVec2;
end;
TFragUniforms2D = record //Uniforms for fragment shader
backAlpha, drawAlpha, pad1, pad2: TScalar;
end;
(*function TGPUVolume.ColorEditorMouseDown(mouseX, mouseY: integer): boolean;
begin
if not colorEditorVisible then exit(false);
result := colorEditor.ColorEditorMouseDown(mouseX, mouseY);
end; *)
function TGPUVolume.GetSlice2DMaxXY(mouseX, mouseY: integer; var Lo: TPoint): TPoint;
begin
result := slices2D.GetSlice2DMaxXY2D(mouseX, mouseY, lo);
end;
function TGPUVolume.GetSlice2DFrac(mouseX, mouseY: integer; out Orient: integer): TVec3;
begin
result := slices2D.GetSlice2DFrac2D(mouseX, mouseY, Orient);
end;
procedure TGPUVolume.Paint2D(var vol: TNIfTI; Drawing: TDraw; DisplayOrient: integer);
var
vertUniforms: TVertUniforms2D;
fragUniforms: TFragUniforms2D;
w,h, scale, rulerPx: single;
i: integer;
begin
if vertexBuffer = nil then // only once
LoadCube();
//if (vol.VolRGBA <> nil) then
LoadTexture(vol, DisplayOrient <> kAxCorSagOrient4);
//LoadTexture(vol, false);
if (volTex = nil) then
exit;
w := mtlControl.clientwidth;
h := mtlControl.clientheight;
{$IFDEF CLRBAR}
if (clrbar <> nil) and (clrbar.PanelFraction < 1.0) and (clrbar.PanelFraction > 0.0) then begin
if (clrbar.isVertical) then
w := round(w * (1.0-clrbar.PanelFraction))
else
h := round(h * (1.0-clrbar.PanelFraction));
scale := slices2D.Update(vol.Scale, w, h, DisplayOrient, mtlControl.clientheight);
w := mtlControl.clientwidth;
h := mtlControl.clientheight;
end else {$ENDIF}
scale := slices2D.Update(vol.Scale, w, h, DisplayOrient);
if SelectionRect.x > 0 then
slices2D.DrawOutLine(SelectionRect.X,h-SelectionRect.Y,SelectionRect.Z,h-SelectionRect.W);
//w := mtlControl.clientwidth;
//h := mtlControl.clientheight;
if slices2D.NumberOfVertices < 3 then exit;
vertUniforms.viewportSize := V2(w, h);
if vol.IsLabels then
fragUniforms.backAlpha := 1
else
fragUniforms.backAlpha := vol.OpacityPercent/100;
UpdateDraw(Drawing);
if (not Drawing.IsOpen) or (Drawing.OpacityFraction <= 0.0) then
fragUniforms.drawAlpha := 0.0
else
fragUniforms.drawAlpha := Drawing.OpacityFraction; //<- loaded!
MTLBeginFrame();
//draw 2D slices
if not isSmooth2D then
MTLSetShader(shader2Dn)
else
MTLSetShader(shader2D);
MTLSetFragmentTexture(volTex, 0);
MTLSetFragmentTexture(overlayVolTex, 1);
MTLSetFragmentTexture(drawVolTex, 2);
MTLSetFragmentTexture(drawVolLut, 3);
if slices2D.HasNewSlices then begin
sliceVertexBuffer := mtlControl.renderView.device.newBufferWithBytes_length_options(@slices2D.SliceVertices[0], slices2D.NumberOfVertices * sizeof(TVertex2D), MTLResourceStorageModeShared);
slices2D.HasNewSlices := false;
end;
MTLSetVertexBuffer(sliceVertexBuffer, 0, 0);
MTLSetVertexBytes(@vertUniforms, sizeof(vertUniforms), 1);
MTLSetFragmentBytes(@fragUniforms, sizeof(fragUniforms), 2);
MTLDraw(MTLPrimitiveTypeTriangle, 0, slices2D.NumberOfVertices);
//draw lines
if slices2D.NumberOfLineVertices > 0 then begin
MTLSetShader(shaderLines2D);
if slices2D.HasNewLines then begin
lineVertexBuffer := mtlControl.renderView.device.newBufferWithBytes_length_options(@slices2D.LineVertices[0], slices2D.NumberOfLineVertices * sizeof(TVertex2D), MTLResourceStorageModeShared);
slices2D.HasNewLines := false;
end;
MTLSetVertexBuffer(lineVertexBuffer, 0, 0);
//MTLSetVertexBytes(@slices2D.LineVertices[0], slices2D.NumberOfLineVertices * sizeof(TVertex2D), 0); //limited to 4096 bytes!
MTLSetVertexBytes(@vertUniforms, sizeof(vertUniforms), 1);
MTLDraw(MTLPrimitiveTypeTriangle, 0, slices2D.NumberOfLineVertices);
end;
//draw inset rendering
if (DisplayOrient = kAxCorSagOrient4) then begin
i := RayCastQuality1to5;
if (i < kQualityMedium) then
RayCastQuality1to5 := kQualityMedium;
PaintCore(vol, slices2D.axCorSagOrient4XY, false, false);
RayCastQuality1to5 := i;
end;
//draw color editor
if colorEditorVisible then begin
colorEditor.Update( w, h, vol);
if colorEditor.NumberOfLineVertices > 2 then begin
MTLSetShader(shaderLines2D);
if colorEditor.HasNewLines then begin
colorEditorVertexBuffer := mtlControl.renderView.device.newBufferWithBytes_length_options(@colorEditor.LineVertices[0], colorEditor.NumberOfLineVertices * sizeof(TVertex2D), MTLResourceStorageModeShared);
colorEditor.HasNewLines := false;
end;
MTLSetVertexBuffer(colorEditorVertexBuffer, 0, 0);
//MTLSetVertexBytes(@colorEditor.LineVertices[0], colorEditor.NumberOfLineVertices * sizeof(TVertex2D), 0); //limited to 4096 bytes!