forked from OBXS/FidelityFX-FSR-Unity-URP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSR1.0-For-URP12.1.4-Patch.patch
4707 lines (4663 loc) · 282 KB
/
FSR1.0-For-URP12.1.4-Patch.patch
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
diff --git a/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Drawers.cs b/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Drawers.cs
index e8c6ae3..c6167e1 100644
--- a/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Drawers.cs
+++ b/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Drawers.cs
@@ -214,7 +214,9 @@ namespace UnityEditor.Rendering.Universal
static void DrawerRenderingRenderPostProcessing(UniversalRenderPipelineSerializedCamera p, Editor owner)
{
- EditorGUILayout.PropertyField(p.renderPostProcessing, Styles.renderPostProcessing);
+ EditorGUILayout.PropertyField(p.renderPostProcessing, Styles.renderPostProcessing);
+ if (p.renderPostProcessing.boolValue)
+ EditorGUILayout.PropertyField(p.AMDFSR, Styles.AMDFSRtext);
}
static void DrawerRenderingPriority(UniversalRenderPipelineSerializedCamera p, Editor owner)
diff --git a/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Skin.cs b/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Skin.cs
index ac65036..82948f6 100644
--- a/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Skin.cs
+++ b/Editor/Camera/UniversalRenderPipelineCameraUI.Rendering.Skin.cs
@@ -12,6 +12,7 @@ namespace UnityEditor.Rendering.Universal
public static GUIContent rendererType = EditorGUIUtility.TrTextContent("Renderer", "The series of operations that translates code into visuals. These have different capabilities and performance characteristics.");
public static GUIContent renderPostProcessing = EditorGUIUtility.TrTextContent("Post Processing", "Enable this to make this camera render post-processing effects.");
+ public static GUIContent AMDFSRtext = EditorGUIUtility.TrTextContent("AMD FSR 1.0", "AMD FidelityFX Super Resolution 1.0 is a cutting edge super-optimized spatial upscaling technology that produces impressive image quality at fast framerates.");
public static GUIContent antialiasing = EditorGUIUtility.TrTextContent("Anti-aliasing", "The method the camera uses to smooth jagged edges.");
public static GUIContent antialiasingQuality = EditorGUIUtility.TrTextContent("Quality", "The quality level to use for the selected anti-aliasing method.");
diff --git a/Editor/Camera/UniversalRenderPipelineSerializedCamera.cs b/Editor/Camera/UniversalRenderPipelineSerializedCamera.cs
index fc41bce..1e7e944 100644
--- a/Editor/Camera/UniversalRenderPipelineSerializedCamera.cs
+++ b/Editor/Camera/UniversalRenderPipelineSerializedCamera.cs
@@ -22,6 +22,7 @@ namespace UnityEditor.Rendering.Universal
public SerializedProperty antialiasing { get; }
// URP specific properties
+ public SerializedProperty AMDFSR { get; }
public SerializedProperty renderShadows { get; }
public SerializedProperty renderDepth { get; }
public SerializedProperty renderOpaque { get; }
@@ -83,6 +84,7 @@ namespace UnityEditor.Rendering.Universal
clearDepth = serializedAdditionalDataObject.FindProperty("m_ClearDepth");
// URP specific properties
+ AMDFSR = serializedAdditionalDataObject.FindProperty("m_AMDFSR");
renderShadows = serializedAdditionalDataObject.FindProperty("m_RenderShadows");
renderDepth = serializedAdditionalDataObject.FindProperty("m_RequiresDepthTextureOption");
renderOpaque = serializedAdditionalDataObject.FindProperty("m_RequiresOpaqueTextureOption");
diff --git a/Runtime/Data/PostProcessData.asset b/Runtime/Data/PostProcessData.asset
index ef442ec..1137794 100644
--- a/Runtime/Data/PostProcessData.asset
+++ b/Runtime/Data/PostProcessData.asset
@@ -14,17 +14,25 @@ MonoBehaviour:
m_EditorClassIdentifier:
shaders:
stopNanPS: {fileID: 4800000, guid: 1121bb4e615ca3c48b214e79e841e823, type: 3}
- subpixelMorphologicalAntialiasingPS: {fileID: 4800000, guid: 63eaba0ebfb82cc43bde059b4a8c65f6, type: 3}
- gaussianDepthOfFieldPS: {fileID: 4800000, guid: 5e7134d6e63e0bc47a1dd2669cedb379, type: 3}
- bokehDepthOfFieldPS: {fileID: 4800000, guid: 2aed67ad60045d54ba3a00c91e2d2631, type: 3}
- cameraMotionBlurPS: {fileID: 4800000, guid: 1edcd131364091c46a17cbff0b1de97a, type: 3}
- paniniProjectionPS: {fileID: 4800000, guid: a15b78cf8ca26ca4fb2090293153c62c, type: 3}
+ subpixelMorphologicalAntialiasingPS: {fileID: 4800000, guid: 63eaba0ebfb82cc43bde059b4a8c65f6,
+ type: 3}
+ gaussianDepthOfFieldPS: {fileID: 4800000, guid: 5e7134d6e63e0bc47a1dd2669cedb379,
+ type: 3}
+ bokehDepthOfFieldPS: {fileID: 4800000, guid: 2aed67ad60045d54ba3a00c91e2d2631,
+ type: 3}
+ cameraMotionBlurPS: {fileID: 4800000, guid: 1edcd131364091c46a17cbff0b1de97a,
+ type: 3}
+ paniniProjectionPS: {fileID: 4800000, guid: a15b78cf8ca26ca4fb2090293153c62c,
+ type: 3}
lutBuilderLdrPS: {fileID: 4800000, guid: 65df88701913c224d95fc554db28381a, type: 3}
lutBuilderHdrPS: {fileID: 4800000, guid: ec9fec698a3456d4fb18cf8bacb7a2bc, type: 3}
bloomPS: {fileID: 4800000, guid: 5f1864addb451f54bae8c86d230f736e, type: 3}
- LensFlareDataDrivenPS: {fileID: 4800000, guid: 6cda457ac28612740adb23da5d39ea92, type: 3}
+ LensFlareDataDrivenPS: {fileID: 4800000, guid: 6cda457ac28612740adb23da5d39ea92,
+ type: 3}
uberPostPS: {fileID: 4800000, guid: e7857e9d0c934dc4f83f270f8447b006, type: 3}
finalPostPassPS: {fileID: 4800000, guid: c49e63ed1bbcb334780a3bd19dfed403, type: 3}
+ easuCS: {fileID: 7200000, guid: cc73f0667c7230142a21eb411ee4cc7d, type: 3}
+ rcasCS: {fileID: 7200000, guid: 458f202028db1d64faa71c7675198a2a, type: 3}
textures:
blueNoise16LTex:
- {fileID: 2800000, guid: 81200413a40918d4d8702e94db29911c, type: 3}
diff --git a/Runtime/Data/PostProcessData.cs b/Runtime/Data/PostProcessData.cs
index 3b9bd5f..10eec31 100644
--- a/Runtime/Data/PostProcessData.cs
+++ b/Runtime/Data/PostProcessData.cs
@@ -74,6 +74,12 @@ namespace UnityEngine.Rendering.Universal
[Reload("Shaders/PostProcessing/FinalPost.shader")]
public Shader finalPostPassPS;
+
+ [Reload("Shaders/PostProcessing/easu.compute")]
+ public ComputeShader easuCS;
+
+ [Reload("Shaders/PostProcessing/rcas.compute")]
+ public ComputeShader rcasCS;
}
[Serializable, ReloadGroup]
diff --git a/Runtime/Passes/PostProcessPass.cs b/Runtime/Passes/PostProcessPass.cs
index 7acb9c1..af07325 100644
--- a/Runtime/Passes/PostProcessPass.cs
+++ b/Runtime/Passes/PostProcessPass.cs
@@ -172,21 +172,35 @@ namespace UnityEngine.Rendering.Universal.Internal
m_UseSwapBuffer = false;
}
- public void SetupFinalPass(in RenderTargetHandle source, bool useSwapBuffer = false)
+ public void SetupFinalPass(in RenderTargetHandle source, in RenderTextureDescriptor finalDesc = new RenderTextureDescriptor(), bool useSwapBuffer = false)
{
m_Source = source.id;
m_Destination = RenderTargetHandle.CameraTarget;
m_IsFinalPass = true;
m_HasFinalPass = false;
m_EnableSRGBConversionIfNeeded = true;
- m_UseSwapBuffer = useSwapBuffer;
+ m_UseSwapBuffer = useSwapBuffer;
+ m_Descriptor = finalDesc;
}
+ ComputeBuffer easuCB;
+ ComputeBuffer rcasCB;
+
/// <inheritdoc/>
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
overrideCameraTarget = true;
+ var enableFSR = renderingData.cameraData.enableFSR;
+ if (enableFSR)
+ {
+ if (easuCB == null)
+ easuCB = new ComputeBuffer(4, sizeof(uint) * 4);
+ if (rcasCB == null)
+ rcasCB = new ComputeBuffer(4, sizeof(uint) * 4);
+ }
+
if (m_Destination == RenderTargetHandle.CameraTarget)
return;
@@ -210,6 +224,18 @@ namespace UnityEngine.Rendering.Universal.Internal
return;
cmd.ReleaseTemporaryRT(m_Destination.id);
+
+ if (easuCB != null)
+ {
+ easuCB.Release();
+ easuCB = null;
+ }
+
+ if (rcasCB != null)
+ {
+ rcasCB.Release();
+ rcasCB = null;
+ }
}
public void ResetHistory()
@@ -275,10 +301,22 @@ namespace UnityEngine.Rendering.Universal.Internal
m_ResetHistory = false;
}
-
+
RenderTextureDescriptor GetCompatibleDescriptor()
=> GetCompatibleDescriptor(m_Descriptor.width, m_Descriptor.height, m_Descriptor.graphicsFormat);
+ RenderTextureDescriptor GetUAVCompatibleDescriptor(int width, int height)
+ {
+ var desc = m_Descriptor;
+ desc.depthBufferBits = 0;
+ desc.msaaSamples = 1;
+ desc.width = width;
+ desc.height = height;
+ desc.enableRandomWrite = true;
+ return desc;
+ }
+
+
RenderTextureDescriptor GetCompatibleDescriptor(int width, int height, GraphicsFormat format, int depthBufferBits = 0)
{
var desc = m_Descriptor;
@@ -540,6 +578,15 @@ namespace UnityEngine.Rendering.Universal.Internal
// Overlay cameras need to output to the target described in the base camera while doing camera stack.
RenderTargetHandle cameraTargetHandle = RenderTargetHandle.GetCameraTarget(cameraData.xr);
RenderTargetIdentifier cameraTarget = (cameraData.targetTexture != null && !cameraData.xr.enabled) ? new RenderTargetIdentifier(cameraData.targetTexture) : cameraTargetHandle.Identifier();
+ var oldCameraTarget = cameraTarget;
+ var enableFSR = cameraData.enableFSR;
+ if (enableFSR)
+ {
+ oldCameraTarget = cameraTarget;
+ cmd.GetTemporaryRT(ShaderConstants._EASUInputTexture, GetCompatibleDescriptor());
+ cameraTarget = ShaderConstants._EASUInputTexture;
+ }
// With camera stacking we not always resolve post to final screen as we might run post-processing in the middle of the stack.
if (m_UseSwapBuffer)
@@ -559,7 +606,7 @@ namespace UnityEngine.Rendering.Universal.Internal
colorLoadAction, RenderBufferStoreAction.Store, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare);
bool isRenderToBackBufferTarget = cameraTarget == cameraData.xr.renderTarget && !cameraData.xr.renderTargetIsRenderTexture;
- if (isRenderToBackBufferTarget)
+ if (isRenderToBackBufferTarget && !enableFSR)
cmd.SetViewport(cameraData.pixelRect);
// We y-flip if
// 1) we are bliting from render texture to back buffer and
@@ -582,7 +629,10 @@ namespace UnityEngine.Rendering.Universal.Internal
scaleBias = new Vector4(1, 1, 0, 0);;
cmd.SetGlobalVector(ShaderPropertyId.scaleBias, scaleBias);
cmd.DrawProcedural(Matrix4x4.identity, m_BlitMaterial, 0, MeshTopology.Quads, 4, 1, null);
- }
+ }
+
+ if (enableFSR)
+ DoFSR(cmd, ref cameraData, oldCameraTarget);
}
else
#endif
@@ -591,7 +641,7 @@ namespace UnityEngine.Rendering.Universal.Internal
cameraData.renderer.ConfigureCameraTarget(cameraTarget, cameraTarget);
cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
- if ((m_Destination == RenderTargetHandle.CameraTarget && !m_UseSwapBuffer) || (m_ResolveToScreen && m_UseSwapBuffer))
+ if ((m_Destination == RenderTargetHandle.CameraTarget && !m_UseSwapBuffer) || (m_ResolveToScreen && m_UseSwapBuffer) && (!enableFSR))
cmd.SetViewport(cameraData.pixelRect);
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_Materials.uber);
@@ -607,7 +657,10 @@ namespace UnityEngine.Rendering.Universal.Internal
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_BlitMaterial);
}
- cmd.SetViewProjectionMatrices(cameraData.camera.worldToCameraMatrix, cameraData.camera.projectionMatrix);
+ cmd.SetViewProjectionMatrices(cameraData.camera.worldToCameraMatrix, cameraData.camera.projectionMatrix);
+
+ if (enableFSR)
+ DoFSR(cmd, ref cameraData, oldCameraTarget);
}
if (m_UseSwapBuffer && !m_ResolveToScreen)
@@ -1435,6 +1488,90 @@ namespace UnityEngine.Rendering.Universal.Internal
#endregion
+ #region EASU
+ void EdgeAdaptiveSpatialUpsampling(CommandBuffer cmd, CameraData cameraData, bool needs_convert_to_srgb)
+ {
+ var easuCS = m_Data.shaders.easuCS;
+ if (needs_convert_to_srgb)
+ {
+ easuCS.EnableKeyword("_AMD_FSR_NEEDS_CONVERT_TO_SRGB");
+ }
+ else
+ {
+ easuCS.DisableKeyword("_AMD_FSR_NEEDS_CONVERT_TO_SRGB");
+ }
+ int viewCount = 1;
+ int kinitialize_idx = easuCS.FindKernel("KInitialize");
+ int kmain_idx = easuCS.FindKernel("KMain");
+ cmd.SetComputeTextureParam(easuCS, kmain_idx, ShaderConstants._EASUInputTexture, ShaderConstants._EASUInputTexture);
+ int srcWidth = m_Descriptor.width;
+ int srcHeight = m_Descriptor.height;
+ int dstWidth = cameraData.pixelWidth;
+ int dstHeight = cameraData.pixelHeight;
+ cmd.SetComputeVectorParam(easuCS, ShaderConstants._EASUViewportSize, new Vector4(srcWidth, srcHeight));
+ cmd.SetComputeVectorParam(easuCS, ShaderConstants._EASUInputImageSize, new Vector4(srcWidth, srcHeight));
+ cmd.GetTemporaryRT(ShaderConstants._EASUOutputTexture, GetUAVCompatibleDescriptor(dstWidth, dstHeight));
+ cmd.SetComputeTextureParam(easuCS, kmain_idx, ShaderConstants._EASUOutputTexture, ShaderConstants._EASUOutputTexture);
+ cmd.SetComputeVectorParam(easuCS, ShaderConstants._EASUOutputSize, new Vector4(dstWidth, dstHeight, 1.0f / dstWidth, 1.0f / dstHeight));
+ cmd.SetComputeBufferParam(easuCS, kinitialize_idx, ShaderConstants._EASUParameters, easuCB);
+ cmd.SetComputeBufferParam(easuCS, kmain_idx, ShaderConstants._EASUParameters, easuCB);
+ cmd.DispatchCompute(easuCS, kinitialize_idx, 1, 1, 1);
+ int DivRoundUp(int x, int y) => (x + y - 1) / y;
+ int dispatchX = DivRoundUp((int)dstWidth, 8);
+ int dispatchY = DivRoundUp((int)dstHeight, 8);
+
+ cmd.DispatchCompute(easuCS, kmain_idx, dispatchX, dispatchY, viewCount);
+ }
+ #endregion
+
+ #region RCAS
+ void RobustContrastAdaptiveSharpening(CommandBuffer cmd, CameraData cameraData, bool needs_convert_to_srgb)
+ {
+ var rcasCS = m_Data.shaders.rcasCS;
+ if (needs_convert_to_srgb)
+ {
+ rcasCS.EnableKeyword("_AMD_FSR_NEEDS_CONVERT_TO_SRGB");
+ }
+ else
+ {
+ rcasCS.DisableKeyword("_AMD_FSR_NEEDS_CONVERT_TO_SRGB");
+ }
+ int viewCount = 1;
+ int kinitialize_idx = rcasCS.FindKernel("KInitialize");
+ int kmain_idx = rcasCS.FindKernel("KMain");
+
+ cmd.SetComputeFloatParam(rcasCS, ShaderConstants._RCASScale, 1.0f);
+ cmd.SetComputeTextureParam(rcasCS, kmain_idx, ShaderConstants._RCASInputTexture, ShaderConstants._EASUOutputTexture);
+ int dstWidth = cameraData.pixelWidth;
+ int dstHeight = cameraData.pixelHeight;
+ cmd.GetTemporaryRT(ShaderConstants._RCASOutputTexture, GetUAVCompatibleDescriptor(dstWidth, dstHeight));
+ cmd.SetComputeTextureParam(rcasCS, kmain_idx, ShaderConstants._RCASOutputTexture, ShaderConstants._RCASOutputTexture);
+ cmd.SetComputeBufferParam(rcasCS, kinitialize_idx, ShaderConstants._RCASParameters, rcasCB);
+ cmd.SetComputeBufferParam(rcasCS, kmain_idx, ShaderConstants._RCASParameters, rcasCB);
+ cmd.DispatchCompute(rcasCS, kinitialize_idx, 1, 1, 1);
+
+ int DivRoundUp(int x, int y) => (x + y - 1) / y;
+ int dispatchX = DivRoundUp((int)dstWidth, 8);
+ int dispatchY = DivRoundUp((int)dstHeight, 8);
+
+ cmd.DispatchCompute(rcasCS, kmain_idx, dispatchX, dispatchY, viewCount);
+ }
+ #endregion
+
+ #region FSR
+ void DoFSR(CommandBuffer cmd, ref CameraData cameraData, RenderTargetIdentifier dst)
+ {
+ bool needs_convert_to_srgb = !(cameraData.isHdrEnabled || QualitySettings.activeColorSpace == ColorSpace.Gamma);
+ using (new ProfilingScope(cmd, ProfilingSampler.Get(URPProfileId.FSR)))
+ {
+ EdgeAdaptiveSpatialUpsampling(cmd, cameraData, needs_convert_to_srgb);
+ RobustContrastAdaptiveSharpening(cmd, cameraData, needs_convert_to_srgb);
+ }
+ cmd.Blit(ShaderConstants._RCASOutputTexture, dst);
+ }
+ #endregion
+
+
#region Internal utilities
class MaterialLibrary
@@ -1550,6 +1687,20 @@ namespace UnityEngine.Rendering.Universal.Internal
public static readonly int _FullscreenProjMat = Shader.PropertyToID("_FullscreenProjMat");
+ // Edge Adaptive Spatial Upsampling
+ public static readonly int _EASUInputTexture = Shader.PropertyToID("_EASUInputTexture");
+ public static readonly int _EASUOutputTexture = Shader.PropertyToID("_EASUOutputTexture");
+ public static readonly int _EASUViewportSize = Shader.PropertyToID("_EASUViewportSize");
+ public static readonly int _EASUInputImageSize = Shader.PropertyToID("_EASUInputImageSize");
+ public static readonly int _EASUOutputSize = Shader.PropertyToID("_EASUOutputSize");
+ public static readonly int _EASUParameters = Shader.PropertyToID("_EASUParameters");
+
+ // Robust Contrast Adaptive Sharpening
+ public static readonly int _RCASInputTexture = Shader.PropertyToID("_RCASInputTexture");
+ public static readonly int _RCASScale = Shader.PropertyToID("_RCASScale");
+ public static readonly int _RCASParameters = Shader.PropertyToID("_RCASParameters");
+ public static readonly int _RCASOutputTexture = Shader.PropertyToID("_RCASOutputTexture");
+
public static int[] _BloomMipUp;
public static int[] _BloomMipDown;
}
diff --git a/Runtime/UniversalAdditionalCameraData.cs b/Runtime/UniversalAdditionalCameraData.cs
index cedebad..c410fed 100644
--- a/Runtime/UniversalAdditionalCameraData.cs
+++ b/Runtime/UniversalAdditionalCameraData.cs
@@ -258,6 +258,17 @@ namespace UnityEngine.Rendering.Universal
[SerializeField] LayerMask m_VolumeLayerMask = 1; // "Default"
[SerializeField] Transform m_VolumeTrigger = null;
+
+ public enum AMDFSR
+ {
+ Disabled = -1,
+ UltraQuality = 0,
+ Quality,
+ Balanced,
+ Performance
+ }
+ [SerializeField] AMDFSR m_AMDFSR = AMDFSR.Disabled;
+
[SerializeField] VolumeFrameworkUpdateMode m_VolumeFrameworkUpdateModeOption = VolumeFrameworkUpdateMode.UsePipelineSettings;
[SerializeField] bool m_RenderPostProcessing = false;
@@ -530,6 +541,12 @@ namespace UnityEngine.Rendering.Universal
set => m_RenderPostProcessing = value;
}
+ public AMDFSR amdFSR
+ {
+ get => m_AMDFSR;
+ set => m_AMDFSR = value;
+ }
+
/// <summary>
/// Returns the current anti-aliasing mode used by this camera.
/// <see cref="AntialiasingMode"/>.
diff --git a/Runtime/UniversalRenderPipeline.cs b/Runtime/UniversalRenderPipeline.cs
index 62de263..a7e7c64 100644
--- a/Runtime/UniversalRenderPipeline.cs
+++ b/Runtime/UniversalRenderPipeline.cs
@@ -532,6 +532,8 @@ namespace UnityEngine.Rendering.Universal
// Update volumeframework before initializing additional camera data
UpdateVolumeFramework(baseCamera, baseCameraAdditionalData);
InitializeCameraData(baseCamera, baseCameraAdditionalData, !isStackedRendering, out var baseCameraData);
+ if (baseCameraAdditionalData?.amdFSR != UniversalAdditionalCameraData.AMDFSR.Disabled && !isStackedRendering)
+ baseCameraData.enableFSR = true;
RenderTextureDescriptor originalTargetDesc = baseCameraData.cameraTargetDescriptor;
#if ENABLE_VR && ENABLE_XR_MODULE
@@ -598,9 +600,11 @@ namespace UnityEngine.Rendering.Universal
VFX.VFXManager.PrepareCamera(currCamera);
#endif
UpdateVolumeFramework(currCamera, currCameraData);
- InitializeAdditionalCameraData(currCamera, currCameraData, lastCamera, ref overlayCameraData);
+ InitializeAdditionalCameraData(currCamera, currCameraData, lastCamera, ref overlayCameraData);
+ if (baseCameraAdditionalData.amdFSR != UniversalAdditionalCameraData.AMDFSR.Disabled && lastCamera)
+ overlayCameraData.enableFSR = true;
#if ENABLE_VR && ENABLE_XR_MODULE
- if (baseCameraData.xr.enabled)
+ if (baseCameraData.xr.enabled)
m_XRSystem.UpdateFromCamera(ref overlayCameraData.xr, overlayCameraData);
#endif
RenderSingleCamera(context, overlayCameraData, anyPostProcessingEnabled);
@@ -749,6 +753,25 @@ namespace UnityEngine.Rendering.Universal
cameraData.isHdrEnabled, msaaSamples, needsAlphaChannel, cameraData.requiresOpaqueTexture);
}
+ private struct AMDFSRSettings
+ {
+ public readonly float m_RenderScale;
+ public readonly float m_MipmapBias;
+ public AMDFSRSettings(in float render_scale, in float mipmap_bias)
+ {
+ m_RenderScale = render_scale;
+ m_MipmapBias = mipmap_bias;
+ }
+ };
+
+ private static readonly AMDFSRSettings[] amdFSRSettingsPreset = new AMDFSRSettings[]
+ {
+ new AMDFSRSettings(.77f, -.38f),
+ new AMDFSRSettings(.67f, -.58f),
+ new AMDFSRSettings(.59f, -.79f),
+ new AMDFSRSettings(.50f, -1.0f)
+ };
+
/// <summary>
/// Initialize camera data settings common for all cameras in the stack. Overlay cameras will inherit
/// settings from base camera.
@@ -929,7 +952,17 @@ namespace UnityEngine.Rendering.Universal
}
cameraData.SetViewAndProjectionMatrix(camera.worldToCameraMatrix, projectionMatrix);
-
+
+ if (additionalCameraData != null)
+ {
+ if (additionalCameraData.amdFSR != UniversalAdditionalCameraData.AMDFSR.Disabled)
+ {
+ settings.msaaSampleCount = 8; // NOTE! You can also use some other AA solutions.
+ var amdFSRSetting = amdFSRSettingsPreset[(int)additionalCameraData.amdFSR];
+ cameraData.renderScale = amdFSRSetting.m_RenderScale;
+ Shader.SetGlobalFloat("amd_fsr_mipmap_bias", amdFSRSetting.m_MipmapBias);
+ }
+ }
cameraData.worldSpaceCameraPos = camera.transform.position;
}
diff --git a/Runtime/UniversalRenderPipelineCore.cs b/Runtime/UniversalRenderPipelineCore.cs
index e6f3b70..358f1f8 100644
--- a/Runtime/UniversalRenderPipelineCore.cs
+++ b/Runtime/UniversalRenderPipelineCore.cs
@@ -207,6 +207,8 @@ namespace UnityEngine.Rendering.Universal
/// When rendering a stack of cameras only the last camera in the stack will resolve to camera target.
/// </summary>
public bool resolveFinalTarget;
+
+ public bool enableFSR;
/// <summary>
/// Camera position in world space.
@@ -863,6 +865,7 @@ namespace UnityEngine.Rendering.Universal
Bloom,
LensFlareDataDriven,
MotionVectors,
+ FSR,
FinalBlit
}
diff --git a/Runtime/UniversalRenderPipelineGlobalSettings.cs b/Runtime/UniversalRenderPipelineGlobalSettings.cs
index 35f4d9b..201d6b8 100644
--- a/Runtime/UniversalRenderPipelineGlobalSettings.cs
+++ b/Runtime/UniversalRenderPipelineGlobalSettings.cs
@@ -115,7 +115,7 @@ namespace UnityEngine.Rendering.Universal
AssetDatabase.CreateFolder("Assets", folderPath);
assetCreated = Create(path);
- Debug.LogWarning("No URP Global Settings Asset is assigned. One will be created for you. If you want to modify it, go to Project Settings > Graphics > URP Settings.");
+ //Debug.LogWarning("No URP Global Settings Asset is assigned. One will be created for you. If you want to modify it, go to Project Settings > Graphics > URP Settings.");
}
else
{
diff --git a/Runtime/UniversalRenderer.cs b/Runtime/UniversalRenderer.cs
index be3144b..dc710b7 100644
--- a/Runtime/UniversalRenderer.cs
+++ b/Runtime/UniversalRenderer.cs
@@ -773,9 +773,11 @@ namespace UnityEngine.Rendering.Universal
EnqueuePass(m_OnRenderObjectCallbackPass);
- bool hasCaptureActions = renderingData.cameraData.captureActions != null && lastCameraInTheStack;
- bool applyFinalPostProcessing = anyPostProcessing && lastCameraInTheStack &&
- renderingData.cameraData.antialiasing == AntialiasingMode.FastApproximateAntialiasing;
+ bool hasCaptureActions = renderingData.cameraData.captureActions != null && lastCameraInTheStack;
+ bool enableFSR = renderingData.cameraData.enableFSR;
+ bool applyFinalPostProcessing = anyPostProcessing && lastCameraInTheStack && (
+ (renderingData.cameraData.antialiasing == AntialiasingMode.FastApproximateAntialiasing) || (enableFSR));
// When post-processing is enabled we can use the stack to resolve rendering to camera target (screen or RT).
// However when there are render passes executing after post we avoid resolving to screen so rendering continues (before sRGBConvertion etc)
@@ -799,7 +801,7 @@ namespace UnityEngine.Rendering.Universal
// Do FXAA or any other final post-processing effect that might need to run after AA.
if (applyFinalPostProcessing)
{
- finalPostProcessPass.SetupFinalPass(sourceForFinalPass, true);
+ finalPostProcessPass.SetupFinalPass(sourceForFinalPass, cameraTargetDescriptor);
EnqueuePass(finalPostProcessPass);
}
diff --git a/ShaderLibrary/SurfaceInput.hlsl b/ShaderLibrary/SurfaceInput.hlsl
index 1073010..849a124 100644
--- a/ShaderLibrary/SurfaceInput.hlsl
+++ b/ShaderLibrary/SurfaceInput.hlsl
@@ -35,7 +35,11 @@ half Alpha(half albedoAlpha, half4 color, half cutoff)
half4 SampleAlbedoAlpha(float2 uv, TEXTURE2D_PARAM(albedoAlphaMap, sampler_albedoAlphaMap))
{
- return half4(SAMPLE_TEXTURE2D(albedoAlphaMap, sampler_albedoAlphaMap, uv));
+#ifdef _AMD_FSR
+ return SAMPLE_TEXTURE2D_BIAS(albedoAlphaMap, sampler_albedoAlphaMap, uv, amd_fsr_mipmap_bias);
+#else
+ return SAMPLE_TEXTURE2D(albedoAlphaMap, sampler_albedoAlphaMap, uv);
+#endif
}
half3 SampleNormal(float2 uv, TEXTURE2D_PARAM(bumpMap, sampler_bumpMap), half scale = half(1.0))
diff --git a/Shaders/PostProcessing/amd_fsr.hlsl b/Shaders/PostProcessing/amd_fsr.hlsl
new file mode 100644
index 0000000..b0d2499
--- /dev/null
+++ b/Shaders/PostProcessing/amd_fsr.hlsl
@@ -0,0 +1,23 @@
+#define A_GPU 1
+#define A_HLSL 1
+
+#ifdef _AMD_FSR_HALF
+#define A_HALF 1
+#define FSR_EASU_H 1
+#define FSR_RCAS_H 1
+#define AREAL half
+#else
+#define FSR_EASU_F 1
+#define FSR_RCAS_F 1
+#define AREAL float
+#endif
+
+#include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/ffx_a.hlsl"
+#include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/ffx_fsr1.hlsl"
+
+
+#ifdef _AMD_FSR_NEEDS_CONVERT_TO_SRGB
+#define AMD_FSR_TO_SRGB(color) AToSrgbF4(color)
+#else
+#define AMD_FSR_TO_SRGB(color) (color)
+#endif
\ No newline at end of file
diff --git a/Shaders/PostProcessing/amd_fsr.hlsl.meta b/Shaders/PostProcessing/amd_fsr.hlsl.meta
new file mode 100644
index 0000000..6415b3d
--- /dev/null
+++ b/Shaders/PostProcessing/amd_fsr.hlsl.meta
@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: a7f973c07e24efd499c532589cf6e49c
+ShaderIncludeImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Shaders/PostProcessing/easu.compute b/Shaders/PostProcessing/easu.compute
new file mode 100644
index 0000000..5eae887
--- /dev/null
+++ b/Shaders/PostProcessing/easu.compute
@@ -0,0 +1,91 @@
+#pragma kernel KMain
+#pragma kernel KInitialize
+
+#pragma multi_compile _ ENABLE_ALPHA _AMD_FSR_HALF _AMD_FSR_NEEDS_CONVERT_TO_SRGB
+#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
+
+#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
+#include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/amd_fsr.hlsl"
+
+// 4 elements:
+// [0] = const0
+// [1] = const1
+// [0] = const2
+// [1] = const3
+// ComputeBuffer is allocated with stride sizeof(int)*4, 4 elements
+RWStructuredBuffer<uint4> _EASUParameters;
+
+float4 _EASUViewportSize;
+float4 _EASUInputImageSize;
+float4 _EASUOutputSize;
+
+SamplerState s_linear_clamp_sampler;
+TEXTURE2D_X(_EASUInputTexture);
+RWTexture2D<float4> _EASUOutputTexture;
+
+#ifdef _AMD_FSR_HALF
+AH4 FsrEasuRH(AF2 p) { return AH4(AMD_FSR_TO_SRGB(GATHER_RED_TEXTURE2D_X(_EASUInputTexture, s_linear_clamp_sampler, p))); }
+AH4 FsrEasuGH(AF2 p){ return AH4(AMD_FSR_TO_SRGB(GATHER_GREEN_TEXTURE2D_X(_EASUInputTexture, s_linear_clamp_sampler, p))); }
+AH4 FsrEasuBH(AF2 p){ return AH4(AMD_FSR_TO_SRGB(GATHER_BLUE_TEXTURE2D_X (_EASUInputTexture, s_linear_clamp_sampler, p))); }
+#else
+AF4 FsrEasuRF(AF2 p){ return AMD_FSR_TO_SRGB(GATHER_RED_TEXTURE2D_X (_EASUInputTexture, s_linear_clamp_sampler, p)); }
+AF4 FsrEasuGF(AF2 p){ return AMD_FSR_TO_SRGB(GATHER_GREEN_TEXTURE2D_X(_EASUInputTexture, s_linear_clamp_sampler, p)); }
+AF4 FsrEasuBF(AF2 p){ return AMD_FSR_TO_SRGB(GATHER_BLUE_TEXTURE2D_X (_EASUInputTexture, s_linear_clamp_sampler, p)); }
+#endif
+
+[numthreads(64, 1, 1)]
+void KMain(uint3 LocalThreadId : SV_GroupThreadID, uint3 WorkGroupId : SV_GroupID, uint3 dispatchThreadId : SV_DispatchThreadID)
+{
+ // Do remapping of local xy in workgroup for a more PS-like swizzle pattern.
+ AU2 gxy = ARmp8x8(LocalThreadId.x) + AU2(WorkGroupId.x<<3u, WorkGroupId.y<<3u);
+
+#ifdef ENABLE_ALPHA
+ AREAL alpha = LOAD_TEXTURE2D_X(_EASUInputTexture, gxy.xy).a;
+#else
+ AREAL alpha = 1.0;
+#endif
+
+ AU4 con0 = _EASUParameters[0];
+ AU4 con1 = _EASUParameters[1];
+ AU4 con2 = _EASUParameters[2];
+ AU4 con3 = _EASUParameters[3];
+ AF4 c;
+
+#ifdef _AMD_FSR_HALF
+ AH3 out_rgb;
+ FsrEasuH(out_rgb, gxy, con0, con1, con2, con3);
+#else
+ AF3 out_rgb;
+ FsrEasuF(out_rgb, gxy, con0, con1, con2, con3);
+#endif
+
+ c.rgb = out_rgb;
+
+ c.a = alpha;
+ _EASUOutputTexture[gxy] = c;
+}
+
+
+/*
+Doing this to avoid having to deal with any CPU side compilation of the headers.
+The FsrRcasCon is doing some extra parameter packing (log space / pows etc) so its better
+to keep this all in the GPU for simplicity sake, and avoid paying this cost for every wave.
+The headers also dont compile for c#, they are meant for c and c++.
+*/
+[numthreads(1,1,1)]
+void KInitialize()
+{
+ AU4 con0 = (AU4)0;
+ AU4 con1 = (AU4)0;
+ AU4 con2 = (AU4)0;
+ AU4 con3 = (AU4)0;
+ FsrEasuCon(con0,con1,con2,con3,
+ _EASUViewportSize.x, _EASUViewportSize.y,
+ _EASUInputImageSize.x,_EASUInputImageSize.y,
+ _EASUOutputSize.x, _EASUOutputSize.y);
+
+ _EASUParameters[0] = con0;
+ _EASUParameters[1] = con1;
+ _EASUParameters[2] = con2;
+ _EASUParameters[3] = con3;
+}
\ No newline at end of file
diff --git a/Shaders/PostProcessing/easu.compute.meta b/Shaders/PostProcessing/easu.compute.meta
new file mode 100644
index 0000000..585d959
--- /dev/null
+++ b/Shaders/PostProcessing/easu.compute.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: cc73f0667c7230142a21eb411ee4cc7d
+ComputeShaderImporter:
+ externalObjects: {}
+ preprocessorOverride: 0
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Shaders/PostProcessing/ffx_a.hlsl b/Shaders/PostProcessing/ffx_a.hlsl
new file mode 100644
index 0000000..9794d27
--- /dev/null
+++ b/Shaders/PostProcessing/ffx_a.hlsl
@@ -0,0 +1,2664 @@
+//==============================================================================================================================
+//
+// [A] SHADER PORTABILITY 1.20210629
+//
+//==============================================================================================================================
+// FidelityFX Super Resolution Sample
+//
+// Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files(the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions :
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//------------------------------------------------------------------------------------------------------------------------------
+// MIT LICENSE
+// ===========
+// Copyright (c) 2014 Michal Drobot (for concepts used in "FLOAT APPROXIMATIONS").
+// -----------
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
+// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+// -----------
+// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+// Software.
+// -----------
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//------------------------------------------------------------------------------------------------------------------------------
+// ABOUT
+// =====
+// Common central point for high-level shading language and C portability for various shader headers.
+//------------------------------------------------------------------------------------------------------------------------------
+// DEFINES
+// =======
+// A_CPU ..... Include the CPU related code.
+// A_GPU ..... Include the GPU related code.
+// A_GLSL .... Using GLSL.
+// A_HLSL .... Using HLSL.
+// A_HLSL_6_2 Using HLSL 6.2 with new 'uint16_t' and related types (requires '-enable-16bit-types').
+// A_NO_16_BIT_CAST Don't use instructions that are not availabe in SPIR-V (needed for running A_HLSL_6_2 on Vulkan)
+// A_GCC ..... Using a GCC compatible compiler (else assume MSVC compatible compiler by default).
+// =======
+// A_BYTE .... Support 8-bit integer.
+// A_HALF .... Support 16-bit integer and floating point.
+// A_LONG .... Support 64-bit integer.
+// A_DUBL .... Support 64-bit floating point.
+// =======
+// A_WAVE .... Support wave-wide operations.
+//------------------------------------------------------------------------------------------------------------------------------
+// To get #include "ffx_a.h" working in GLSL use '#extension GL_GOOGLE_include_directive:require'.
+//------------------------------------------------------------------------------------------------------------------------------
+// SIMPLIFIED TYPE SYSTEM
+// ======================
+// - All ints will be unsigned with exception of when signed is required.
+// - Type naming simplified and shortened "A<type><#components>",
+// - H = 16-bit float (half)
+// - F = 32-bit float (float)
+// - D = 64-bit float (double)
+// - P = 1-bit integer (predicate, not using bool because 'B' is used for byte)
+// - B = 8-bit integer (byte)
+// - W = 16-bit integer (word)
+// - U = 32-bit integer (unsigned)
+// - L = 64-bit integer (long)
+// - Using "AS<type><#components>" for signed when required.
+//------------------------------------------------------------------------------------------------------------------------------
+// TODO
+// ====
+// - Make sure 'ALerp*(a,b,m)' does 'b*m+(-a*m+a)' (2 ops).
+//------------------------------------------------------------------------------------------------------------------------------
+// CHANGE LOG
+// ==========
+// 20200914 - Expanded wave ops and prx code.
+// 20200713 - Added [ZOL] section, fixed serious bugs in sRGB and Rec.709 color conversion code, etc.
+//==============================================================================================================================
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// COMMON
+//==============================================================================================================================
+#define A_2PI 6.28318530718
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+//
+// CPU
+//
+//
+//==============================================================================================================================
+#ifdef A_CPU
+ // Supporting user defined overrides.
+ #ifndef A_RESTRICT
+ #define A_RESTRICT __restrict
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifndef A_STATIC
+ #define A_STATIC static
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ // Same types across CPU and GPU.
+ // Predicate uses 32-bit integer (C friendly bool).
+ typedef uint32_t AP1;
+ typedef float AF1;
+ typedef double AD1;
+ typedef uint8_t AB1;
+ typedef uint16_t AW1;
+ typedef uint32_t AU1;
+ typedef uint64_t AL1;
+ typedef int8_t ASB1;
+ typedef int16_t ASW1;
+ typedef int32_t ASU1;
+ typedef int64_t ASL1;
+//------------------------------------------------------------------------------------------------------------------------------
+ #define AD1_(a) ((AD1)(a))
+ #define AF1_(a) ((AF1)(a))
+ #define AL1_(a) ((AL1)(a))
+ #define AU1_(a) ((AU1)(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ #define ASL1_(a) ((ASL1)(a))
+ #define ASU1_(a) ((ASU1)(a))
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AU1 AU1_AF1(AF1 a){union{AF1 f;AU1 u;}bits;bits.f=a;return bits.u;}
+//------------------------------------------------------------------------------------------------------------------------------
+ #define A_TRUE 1
+ #define A_FALSE 0
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+//
+// CPU/GPU PORTING
+//
+//------------------------------------------------------------------------------------------------------------------------------
+// Get CPU and GPU to share all setup code, without duplicate code paths.
+// This uses a lower-case prefix for special vector constructs.
+// - In C restrict pointers are used.
+// - In the shading language, in/inout/out arguments are used.
+// This depends on the ability to access a vector value in both languages via array syntax (aka color[2]).
+//==============================================================================================================================
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// VECTOR ARGUMENT/RETURN/INITIALIZATION PORTABILITY
+//==============================================================================================================================
+ #define retAD2 AD1 *A_RESTRICT
+ #define retAD3 AD1 *A_RESTRICT
+ #define retAD4 AD1 *A_RESTRICT
+ #define retAF2 AF1 *A_RESTRICT
+ #define retAF3 AF1 *A_RESTRICT
+ #define retAF4 AF1 *A_RESTRICT
+ #define retAL2 AL1 *A_RESTRICT
+ #define retAL3 AL1 *A_RESTRICT
+ #define retAL4 AL1 *A_RESTRICT
+ #define retAU2 AU1 *A_RESTRICT
+ #define retAU3 AU1 *A_RESTRICT
+ #define retAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define inAD2 AD1 *A_RESTRICT
+ #define inAD3 AD1 *A_RESTRICT
+ #define inAD4 AD1 *A_RESTRICT
+ #define inAF2 AF1 *A_RESTRICT
+ #define inAF3 AF1 *A_RESTRICT
+ #define inAF4 AF1 *A_RESTRICT
+ #define inAL2 AL1 *A_RESTRICT
+ #define inAL3 AL1 *A_RESTRICT
+ #define inAL4 AL1 *A_RESTRICT
+ #define inAU2 AU1 *A_RESTRICT
+ #define inAU3 AU1 *A_RESTRICT
+ #define inAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define inoutAD2 AD1 *A_RESTRICT
+ #define inoutAD3 AD1 *A_RESTRICT
+ #define inoutAD4 AD1 *A_RESTRICT
+ #define inoutAF2 AF1 *A_RESTRICT
+ #define inoutAF3 AF1 *A_RESTRICT
+ #define inoutAF4 AF1 *A_RESTRICT
+ #define inoutAL2 AL1 *A_RESTRICT
+ #define inoutAL3 AL1 *A_RESTRICT
+ #define inoutAL4 AL1 *A_RESTRICT
+ #define inoutAU2 AU1 *A_RESTRICT
+ #define inoutAU3 AU1 *A_RESTRICT
+ #define inoutAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define outAD2 AD1 *A_RESTRICT
+ #define outAD3 AD1 *A_RESTRICT
+ #define outAD4 AD1 *A_RESTRICT
+ #define outAF2 AF1 *A_RESTRICT
+ #define outAF3 AF1 *A_RESTRICT
+ #define outAF4 AF1 *A_RESTRICT
+ #define outAL2 AL1 *A_RESTRICT
+ #define outAL3 AL1 *A_RESTRICT
+ #define outAL4 AL1 *A_RESTRICT
+ #define outAU2 AU1 *A_RESTRICT
+ #define outAU3 AU1 *A_RESTRICT
+ #define outAU4 AU1 *A_RESTRICT
+//------------------------------------------------------------------------------------------------------------------------------
+ #define varAD2(x) AD1 x[2]
+ #define varAD3(x) AD1 x[3]
+ #define varAD4(x) AD1 x[4]
+ #define varAF2(x) AF1 x[2]
+ #define varAF3(x) AF1 x[3]
+ #define varAF4(x) AF1 x[4]
+ #define varAL2(x) AL1 x[2]
+ #define varAL3(x) AL1 x[3]
+ #define varAL4(x) AL1 x[4]
+ #define varAU2(x) AU1 x[2]
+ #define varAU3(x) AU1 x[3]
+ #define varAU4(x) AU1 x[4]
+//------------------------------------------------------------------------------------------------------------------------------
+ #define initAD2(x,y) {x,y}
+ #define initAD3(x,y,z) {x,y,z}
+ #define initAD4(x,y,z,w) {x,y,z,w}
+ #define initAF2(x,y) {x,y}
+ #define initAF3(x,y,z) {x,y,z}
+ #define initAF4(x,y,z,w) {x,y,z,w}
+ #define initAL2(x,y) {x,y}
+ #define initAL3(x,y,z) {x,y,z}
+ #define initAL4(x,y,z,w) {x,y,z,w}
+ #define initAU2(x,y) {x,y}
+ #define initAU3(x,y,z) {x,y,z}
+ #define initAU4(x,y,z,w) {x,y,z,w}
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________/\_______________________________________________________________
+//==============================================================================================================================
+// SCALAR RETURN OPS
+//------------------------------------------------------------------------------------------------------------------------------
+// TODO
+// ====
+// - Replace transcendentals with manual versions.
+//==============================================================================================================================
+ #ifdef A_GCC
+ A_STATIC AD1 AAbsD1(AD1 a){return __builtin_fabs(a);}
+ A_STATIC AF1 AAbsF1(AF1 a){return __builtin_fabsf(a);}
+ A_STATIC AU1 AAbsSU1(AU1 a){return AU1_(__builtin_abs(ASU1_(a)));}
+ A_STATIC AL1 AAbsSL1(AL1 a){return AL1_(__builtin_llabs(ASL1_(a)));}
+ #else
+ A_STATIC AD1 AAbsD1(AD1 a){return fabs(a);}
+ A_STATIC AF1 AAbsF1(AF1 a){return fabsf(a);}
+ A_STATIC AU1 AAbsSU1(AU1 a){return AU1_(abs(ASU1_(a)));}
+ A_STATIC AL1 AAbsSL1(AL1 a){return AL1_(labs((long)ASL1_(a)));}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 ACosD1(AD1 a){return __builtin_cos(a);}
+ A_STATIC AF1 ACosF1(AF1 a){return __builtin_cosf(a);}
+ #else
+ A_STATIC AD1 ACosD1(AD1 a){return cos(a);}
+ A_STATIC AF1 ACosF1(AF1 a){return cosf(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 ADotD2(inAD2 a,inAD2 b){return a[0]*b[0]+a[1]*b[1];}
+ A_STATIC AD1 ADotD3(inAD3 a,inAD3 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];}
+ A_STATIC AD1 ADotD4(inAD4 a,inAD4 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3];}
+ A_STATIC AF1 ADotF2(inAF2 a,inAF2 b){return a[0]*b[0]+a[1]*b[1];}
+ A_STATIC AF1 ADotF3(inAF3 a,inAF3 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];}
+ A_STATIC AF1 ADotF4(inAF4 a,inAF4 b){return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3];}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 AExp2D1(AD1 a){return __builtin_exp2(a);}
+ A_STATIC AF1 AExp2F1(AF1 a){return __builtin_exp2f(a);}
+ #else
+ A_STATIC AD1 AExp2D1(AD1 a){return exp2(a);}
+ A_STATIC AF1 AExp2F1(AF1 a){return exp2f(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 AFloorD1(AD1 a){return __builtin_floor(a);}
+ A_STATIC AF1 AFloorF1(AF1 a){return __builtin_floorf(a);}
+ #else
+ A_STATIC AD1 AFloorD1(AD1 a){return floor(a);}
+ A_STATIC AF1 AFloorF1(AF1 a){return floorf(a);}
+ #endif
+//------------------------------------------------------------------------------------------------------------------------------
+ A_STATIC AD1 ALerpD1(AD1 a,AD1 b,AD1 c){return b*c+(-a*c+a);}
+ A_STATIC AF1 ALerpF1(AF1 a,AF1 b,AF1 c){return b*c+(-a*c+a);}
+//------------------------------------------------------------------------------------------------------------------------------
+ #ifdef A_GCC
+ A_STATIC AD1 ALog2D1(AD1 a){return __builtin_log2(a);}