-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathRuntimeUtilities.cs
More file actions
1342 lines (1173 loc) · 54.9 KB
/
RuntimeUtilities.cs
File metadata and controls
1342 lines (1173 loc) · 54.9 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using UnityEngine.Assertions;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Rendering.PostProcessing
{
using SceneManagement;
using UnityObject = UnityEngine.Object;
using LoadAction = RenderBufferLoadAction;
using StoreAction = RenderBufferStoreAction;
/// <summary>
/// A set of runtime utilities used by the post-processing stack.
/// </summary>
public static class RuntimeUtilities
{
#region Textures
static Texture2D m_WhiteTexture;
/// <summary>
/// A 1x1 white texture.
/// </summary>
/// <remarks>
/// This texture is only created once and recycled afterward. You shouldn't modify it.
/// </remarks>
public static Texture2D whiteTexture
{
get
{
if (m_WhiteTexture == null)
{
m_WhiteTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "White Texture" };
m_WhiteTexture.SetPixel(0, 0, Color.white);
m_WhiteTexture.Apply();
}
return m_WhiteTexture;
}
}
static Texture3D m_WhiteTexture3D;
/// <summary>
/// A 1x1x1 white texture.
/// </summary>
/// <remarks>
/// This texture is only created once and recycled afterward. You shouldn't modify it.
/// </remarks>
public static Texture3D whiteTexture3D
{
get
{
if (m_WhiteTexture3D == null)
{
m_WhiteTexture3D = new Texture3D(1, 1, 1, TextureFormat.ARGB32, false) { name = "White Texture 3D" };
m_WhiteTexture3D.SetPixels(new Color[] { Color.white });
m_WhiteTexture3D.Apply();
}
return m_WhiteTexture3D;
}
}
static Texture2D m_BlackTexture;
/// <summary>
/// A 1x1 black texture.
/// </summary>
/// <remarks>
/// This texture is only created once and recycled afterward. You shouldn't modify it.
/// </remarks>
public static Texture2D blackTexture
{
get
{
if (m_BlackTexture == null)
{
m_BlackTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Black Texture" };
m_BlackTexture.SetPixel(0, 0, Color.black);
m_BlackTexture.Apply();
}
return m_BlackTexture;
}
}
static Texture3D m_BlackTexture3D;
/// <summary>
/// A 1x1x1 black texture.
/// </summary>
/// <remarks>
/// This texture is only created once and recycled afterward. You shouldn't modify it.
/// </remarks>
public static Texture3D blackTexture3D
{
get
{
if (m_BlackTexture3D == null)
{
m_BlackTexture3D = new Texture3D(1, 1, 1, TextureFormat.ARGB32, false) { name = "Black Texture 3D" };
m_BlackTexture3D.SetPixels(new Color[] { Color.black });
m_BlackTexture3D.Apply();
}
return m_BlackTexture3D;
}
}
static Texture2D m_TransparentTexture;
/// <summary>
/// A 1x1 transparent texture.
/// </summary>
/// <remarks>
/// This texture is only created once and recycled afterward. You shouldn't modify it.
/// </remarks>
public static Texture2D transparentTexture
{
get
{
if (m_TransparentTexture == null)
{
m_TransparentTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false) { name = "Transparent Texture" };
m_TransparentTexture.SetPixel(0, 0, Color.clear);
m_TransparentTexture.Apply();
}
return m_TransparentTexture;
}
}
static Texture3D m_TransparentTexture3D;
/// <summary>
/// A 1x1x1 transparent texture.
/// </summary>
/// <remarks>
/// This texture is only created once and recycled afterward. You shouldn't modify it.
/// </remarks>
public static Texture3D transparentTexture3D
{
get
{
if (m_TransparentTexture3D == null)
{
m_TransparentTexture3D = new Texture3D(1, 1, 1, TextureFormat.ARGB32, false) { name = "Transparent Texture 3D" };
m_TransparentTexture3D.SetPixels(new Color[] { Color.clear });
m_TransparentTexture3D.Apply();
}
return m_TransparentTexture3D;
}
}
static Dictionary<int, Texture2D> m_LutStrips = new Dictionary<int, Texture2D>();
/// <summary>
/// Gets a 2D lookup table for color grading use. Its size will be <c>width = height * height</c>.
/// </summary>
/// <param name="size">The height of the lookup table</param>
/// <returns>A 2D lookup table</returns>
/// <remarks>
/// Lookup tables are recycled and only created once per size. You shouldn't modify them.
/// </remarks>
public static Texture2D GetLutStrip(int size)
{
Texture2D texture;
if (!m_LutStrips.TryGetValue(size, out texture))
{
int width = size * size;
int height = size;
var pixels = new Color[width * height];
float inv = 1f / (size - 1f);
for (int z = 0; z < size; z++)
{
var offset = z * size;
var b = z * inv;
for (int y = 0; y < size; y++)
{
var g = y * inv;
for (int x = 0; x < size; x++)
{
var r = x * inv;
pixels[y * width + offset + x] = new Color(r, g, b);
}
}
}
var format = TextureFormat.RGBAHalf;
if (!format.IsSupported())
format = TextureFormat.ARGB32;
texture = new Texture2D(size * size, size, format, false, true)
{
name = "Strip Lut" + size,
hideFlags = HideFlags.DontSave,
filterMode = FilterMode.Bilinear,
wrapMode = TextureWrapMode.Clamp,
anisoLevel = 0
};
texture.SetPixels(pixels);
texture.Apply();
m_LutStrips.Add(size, texture);
}
return texture;
}
#endregion
#region Rendering
static PostProcessResources s_Resources;
static Mesh s_FullscreenTriangle;
/// <summary>
/// A fullscreen triangle mesh.
/// </summary>
public static Mesh fullscreenTriangle
{
get
{
if (s_FullscreenTriangle != null)
return s_FullscreenTriangle;
s_FullscreenTriangle = new Mesh { name = "Fullscreen Triangle" };
// Because we have to support older platforms (GLES2/3, DX9 etc) we can't do all of
// this directly in the vertex shader using vertex ids :(
s_FullscreenTriangle.SetVertices(new List<Vector3>
{
new Vector3(-1f, -1f, 0f),
new Vector3(-1f, 3f, 0f),
new Vector3(3f, -1f, 0f)
});
s_FullscreenTriangle.SetIndices(new[] { 0, 1, 2 }, MeshTopology.Triangles, 0, false);
s_FullscreenTriangle.UploadMeshData(false);
return s_FullscreenTriangle;
}
}
static Material s_CopyStdMaterial;
/// <summary>
/// A simple copy material to use with the builtin pipelines.
/// </summary>
public static Material copyStdMaterial
{
get
{
if (s_CopyStdMaterial != null)
return s_CopyStdMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copyStd;
s_CopyStdMaterial = new Material(shader)
{
name = "PostProcess - CopyStd",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyStdMaterial;
}
}
static Material s_CopyStdFromDoubleWideMaterial;
/// <summary>
/// A double-wide copy material to use with VR and the builtin pipelines.
/// </summary>
public static Material copyStdFromDoubleWideMaterial
{
get
{
if (s_CopyStdFromDoubleWideMaterial != null)
return s_CopyStdFromDoubleWideMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copyStdFromDoubleWide;
s_CopyStdFromDoubleWideMaterial = new Material(shader)
{
name = "PostProcess - CopyStdFromDoubleWide",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyStdFromDoubleWideMaterial;
}
}
static Material s_CopyMaterial;
/// <summary>
/// A simple copy material independent from the rendering pipeline.
/// </summary>
public static Material copyMaterial
{
get
{
if (s_CopyMaterial != null)
return s_CopyMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copy;
s_CopyMaterial = new Material(shader)
{
name = "PostProcess - Copy",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyMaterial;
}
}
static Material s_CopyFromTexArrayMaterial;
/// <summary>
/// A copy material with a texture array slice as a source for the builtin pipelines.
/// </summary>
public static Material copyFromTexArrayMaterial
{
get
{
if (s_CopyFromTexArrayMaterial != null)
return s_CopyFromTexArrayMaterial;
Assert.IsNotNull(s_Resources);
var shader = s_Resources.shaders.copyStdFromTexArray;
s_CopyFromTexArrayMaterial = new Material(shader)
{
name = "PostProcess - CopyFromTexArray",
hideFlags = HideFlags.HideAndDontSave
};
return s_CopyFromTexArrayMaterial;
}
}
static PropertySheet s_CopySheet;
/// <summary>
/// A pre-configured <see cref="PropertySheet"/> for <see cref="copyMaterial"/>.
/// </summary>
public static PropertySheet copySheet
{
get
{
if (s_CopySheet == null)
s_CopySheet = new PropertySheet(copyMaterial);
return s_CopySheet;
}
}
static PropertySheet s_CopyFromTexArraySheet;
/// <summary>
/// A pre-configured <see cref="PropertySheet"/> for <see cref="copyFromTexArrayMaterial"/>.
/// </summary>
public static PropertySheet copyFromTexArraySheet
{
get
{
if (s_CopyFromTexArraySheet == null)
s_CopyFromTexArraySheet = new PropertySheet(copyFromTexArrayMaterial);
return s_CopyFromTexArraySheet;
}
}
internal static bool isValidResources()
{
return s_Resources != null;
}
internal static void UpdateResources(PostProcessResources resources)
{
Destroy(s_CopyMaterial);
Destroy(s_CopyStdMaterial);
Destroy(s_CopyFromTexArrayMaterial);
Destroy(s_CopyStdFromDoubleWideMaterial);
s_CopyMaterial = null;
s_CopyStdMaterial = null;
s_CopyFromTexArrayMaterial = null;
s_CopyStdFromDoubleWideMaterial = null;
s_CopySheet = null;
s_CopyFromTexArraySheet = null;
s_Resources = resources;
}
/// <summary>
/// Sets the current render target using specified <see cref="RenderBufferLoadAction"/>.
/// </summary>
/// <param name="cmd">The command buffer to set the render target on</param>
/// <param name="rt">The render target to set</param>
/// <param name="loadAction">The load action</param>
/// <param name="storeAction">The store action</param>
/// <remarks>
/// <see cref="RenderBufferLoadAction"/> are only used on Unity 2018.2 or newer.
/// </remarks>
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd, RenderTargetIdentifier rt, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(rt, loadAction, storeAction);
#else
cmd.SetRenderTarget(rt);
#endif
}
/// <summary>
/// Sets the current render target using specified <see cref="RenderBufferLoadAction"/>.
/// </summary>
/// <param name="cmd">The command buffer to set the render target on</param>
/// <param name="rt">The render target to set</param>
/// <param name="loadAction">The load action</param>
/// <param name="storeAction">The store action</param>
/// <param name="depthLoadAction">The load action for the depth/stencil part of rt</param>
/// <param name="depthStoreAction">The store action for the depth/stencil part of rt</param>
/// <remarks>
/// <see cref="RenderBufferLoadAction"/> are only used on Unity 2018.2 or newer.
/// </remarks>
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd, RenderTargetIdentifier rt,
RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction,
RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(rt, loadAction, storeAction, depthLoadAction, depthStoreAction);
#else
cmd.SetRenderTarget(rt);
#endif
}
/// <summary>
/// Sets the current render target and its depth using specified <see cref="RenderBufferLoadAction"/>.
/// </summary>
/// <param name="cmd">The command buffer to set the render target on</param>
/// <param name="color">The render target to set as color</param>
/// <param name="colorLoadAction">The load action for the color render target</param>
/// <param name="colorStoreAction">The store action for the color render target</param>
/// <param name="depth">The render target to set as depth</param>
/// <param name="depthLoadAction">The load action for the depth render target</param>
/// <param name="depthStoreAction">The store action for the depth render target</param>
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd,
RenderTargetIdentifier color, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RenderTargetIdentifier depth, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(color, colorLoadAction, colorStoreAction, depth, depthLoadAction, depthStoreAction);
#else
cmd.SetRenderTarget(color, depth);
#endif
}
/// <summary>
/// Does a copy of source to destination using a fullscreen triangle.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="clear">Should the destination target be cleared?</param>
/// <param name="viewport">An optional viewport to consider for the blit</param>
/// <param name="preserveDepth">Should the depth buffer be preserved?</param>
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, bool clear = false, Rect? viewport = null, bool preserveDepth = false)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
var colorLoad = viewport == null ? LoadAction.DontCare : LoadAction.Load;
cmd.SetRenderTargetWithLoadStoreAction(destination, colorLoad, StoreAction.Store, preserveDepth ? LoadAction.Load : colorLoad, StoreAction.Store);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, copyMaterial, 0, 0);
}
/// <summary>
/// Blits a fullscreen triangle using a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="loadAction">The load action for this blit</param>
/// <param name="viewport">An optional viewport to consider for the blit</param>
/// <param name="preserveDepth">Should the depth buffer be preserved?</param>
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, RenderBufferLoadAction loadAction, Rect? viewport = null, bool preserveDepth = false)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
#if UNITY_2018_2_OR_NEWER
bool clear = (loadAction == LoadAction.Clear);
if (clear)
loadAction = LoadAction.DontCare;
#else
bool clear = false;
#endif
if (viewport != null)
loadAction = LoadAction.Load;
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, preserveDepth ? LoadAction.Load : loadAction, StoreAction.Store);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
/// <summary>
/// Blits a fullscreen triangle using a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="clear">Should the destination target be cleared?</param>
/// <param name="viewport">An optional viewport to consider for the blit</param>
/// <param name="preserveDepth">Should the depth buffer be preserved?</param>
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false, Rect? viewport = null, bool preserveDepth = false)
{
#if UNITY_2018_2_OR_NEWER
cmd.BlitFullscreenTriangle(source, destination, propertySheet, pass, clear ? LoadAction.Clear : LoadAction.DontCare, viewport, preserveDepth);
#else
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
var loadAction = viewport == null ? LoadAction.DontCare : LoadAction.Load;
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, preserveDepth ? LoadAction.Load : loadAction, StoreAction.Store);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
#endif
}
/// <summary>
/// Blits procedural geometry using a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="instanceCount">The number of instances to render</param>
/// <param name="clear">Should the destination target be cleared?</param>
/// <param name="viewport">An optional viewport to consider for the blit</param>
/// <param name="preserveDepth">Should the depth buffer be preserved?</param>
public static void BlitProcedural(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, int vertexCount, int instanceCount, bool clear = false, Rect? viewport = null, bool preserveDepth = false)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
var loadAction = viewport == null ? LoadAction.DontCare : LoadAction.Load;
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, preserveDepth ? LoadAction.Load : loadAction, StoreAction.Store);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
// TODO: detect which platforms support quads
cmd.DrawProcedural(Matrix4x4.identity, propertySheet.material, pass, MeshTopology.Triangles, vertexCount, instanceCount, propertySheet.properties);
}
/// <summary>
/// Blits a fullscreen triangle from a double-wide source.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="material">The material to use for the blit</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="eye">The target eye</param>
public static void BlitFullscreenTriangleFromDoubleWide(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material material, int pass, int eye)
{
Vector4 uvScaleOffset = new Vector4(0.5f, 1.0f, 0, 0);
if (eye == 1)
uvScaleOffset.z = 0.5f;
cmd.SetGlobalVector(ShaderIDs.UVScaleOffset, uvScaleOffset);
cmd.BuiltinBlit(source, destination, material, pass);
}
/// <summary>
/// Blits a fullscreen triangle to a double-wide destination.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="eye">The target eye</param>
public static void BlitFullscreenTriangleToDoubleWide(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, int eye)
{
Vector4 posScaleOffset = new Vector4(0.5f, 1.0f, -0.5f, 0);
if (eye == 1)
posScaleOffset.z = 0.5f;
propertySheet.EnableKeyword("STEREO_DOUBLEWIDE_TARGET");
propertySheet.properties.SetVector(ShaderIDs.PosScaleOffset, posScaleOffset);
cmd.BlitFullscreenTriangle(source, destination, propertySheet, 0);
}
/// <summary>
/// Blits a fullscreen triangle using a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source texture array</param>
/// <param name="destination">The destination render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="clear">Should the destination target be cleared?</param>
/// <param name="depthSlice">The slice to use for the texture array</param>
public static void BlitFullscreenTriangleFromTexArray(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false, int depthSlice = -1)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
cmd.SetGlobalFloat(ShaderIDs.DepthSlice, depthSlice);
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
/// <summary>
/// Blits a fullscreen triangle using a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="clear">Should the destination target be cleared?</param>
/// <param name="depthSlice">The array slice to consider as a source</param>
public static void BlitFullscreenTriangleToTexArray(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false, int depthSlice = -1)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
cmd.SetGlobalFloat(ShaderIDs.DepthSlice, depthSlice);
cmd.SetRenderTarget(destination, 0, CubemapFace.Unknown, -1);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
/// <summary>
/// Blits a fullscreen triangle using a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="depth">The depth render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="clear">Should the destination target be cleared?</param>
/// <param name="viewport">An optional viewport to consider for the blit</param>
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, RenderTargetIdentifier depth, PropertySheet propertySheet, int pass, bool clear = false, Rect? viewport = null)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
LoadAction loadAction = viewport == null ? LoadAction.DontCare : LoadAction.Load;
if (clear)
{
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, depth, loadAction, StoreAction.Store);
cmd.ClearRenderTarget(true, true, Color.clear);
}
else
{
cmd.SetRenderTargetWithLoadStoreAction(destination, loadAction, StoreAction.Store, depth, LoadAction.Load, StoreAction.Store);
}
if (viewport != null)
cmd.SetViewport(viewport.Value);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
/// <summary>
/// Blits a fullscreen triangle using a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destinations">An array of destinations render targets</param>
/// <param name="depth">The depth render target</param>
/// <param name="propertySheet">The property sheet to use</param>
/// <param name="pass">The pass from the material to use</param>
/// <param name="clear">Should the destination target be cleared?</param>
/// <param name="viewport">An optional viewport to consider for the blit</param>
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier[] destinations, RenderTargetIdentifier depth, PropertySheet propertySheet, int pass, bool clear = false, Rect? viewport = null)
{
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
cmd.SetRenderTarget(destinations, depth);
if (viewport != null)
cmd.SetViewport(viewport.Value);
if (clear)
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
}
/// <summary>
/// Does a copy of source to destination using the builtin blit command.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, RenderTargetIdentifier destination)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
destination = BuiltinRenderTextureType.CurrentActive;
#endif
cmd.Blit(source, destination);
}
/// <summary>
/// Blits a fullscreen quad using the builtin blit command and a given material.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <param name="mat">The material to use for the blit</param>
/// <param name="pass">The pass from the material to use</param>
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, RenderTargetIdentifier destination, Material mat, int pass = 0)
{
#if UNITY_2018_2_OR_NEWER
cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
destination = BuiltinRenderTextureType.CurrentActive;
#endif
cmd.Blit(source, destination, mat, pass);
}
// Fast basic copy texture if available, falls back to blit copy if not
// Assumes that both textures have the exact same type and format
/// <summary>
/// Copies the content of a texture into the other. Both textures must have the same size
/// and format or this method will fail.
/// </summary>
/// <param name="cmd">The command buffer to use</param>
/// <param name="source">The source render target</param>
/// <param name="destination">The destination render target</param>
/// <remarks>
/// If the CopyTexture command isn't supported on the target platform it will revert to a
/// fullscreen blit command instead.
/// </remarks>
public static void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination)
{
if (SystemInfo.copyTextureSupport > CopyTextureSupport.None)
{
cmd.CopyTexture(source, destination);
return;
}
cmd.BlitFullscreenTriangle(source, destination);
}
// TODO: Generalize the GetTemporaryRT and Blit commands in order to support
// RT Arrays for Stereo Instancing/MultiView
#endregion
#region Unity specifics & misc methods
/// <summary>
/// Returns <c>true</c> if a scriptable render pipeline is currently in use, <c>false</c>
/// otherwise.
/// </summary>
public static bool scriptableRenderPipelineActive
{
#if UNITY_2019_3_OR_NEWER
get { return GraphicsSettings.currentRenderPipeline != null; }
#else
get { return GraphicsSettings.renderPipelineAsset != null; }
#endif
}
/// <summary>
/// Returns <c>true</c> if deferred shading is supported on the target platform,
/// <c>false</c> otherwise.
/// </summary>
public static bool supportsDeferredShading
{
get { return scriptableRenderPipelineActive || GraphicsSettings.GetShaderMode(BuiltinShaderType.DeferredShading) != BuiltinShaderMode.Disabled; }
}
/// <summary>
/// Returns <c>true</c> if <see cref="DepthTextureMode.DepthNormals"/> is supported on the
/// target platform, <c>false</c> otherwise.
/// </summary>
public static bool supportsDepthNormals
{
get { return scriptableRenderPipelineActive || GraphicsSettings.GetShaderMode(BuiltinShaderType.DepthNormals) != BuiltinShaderMode.Disabled; }
}
#if UNITY_EDITOR
/// <summary>
/// Returns <c>true</c> if single-pass stereo rendering is selected, <c>false</c> otherwise.
/// </summary>
/// <remarks>
/// This property only works in the editor.
/// </remarks>
public static bool isSinglePassStereoSelected
{
get
{
#if (ENABLE_VR_MODULE && ENABLE_VR) && !UNITY_2020_1_OR_NEWER
return UnityEditorInternal.VR.VREditor.GetVREnabledOnTargetGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget))
&& PlayerSettings.stereoRenderingPath == UnityEditor.StereoRenderingPath.SinglePass;
#else
return false;
#endif
}
}
#endif
/// <summary>
/// Returns <c>true</c> if single-pass stereo rendering is active, <c>false</c> otherwise.
/// </summary>
/// <remarks>
/// This property only works in the editor.
/// </remarks>
// TODO: Check for SPSR support at runtime
public static bool isSinglePassStereoEnabled
{
get
{
#if UNITY_EDITOR
return isSinglePassStereoSelected && Application.isPlaying;
#elif !(ENABLE_VR_MODULE && ENABLE_VR)
return false;
#else
return UnityEngine.XR.XRSettings.eyeTextureDesc.vrUsage == VRTextureUsage.TwoEyes;
#endif
}
}
/// <summary>
/// Returns <c>true</c> if VR is enabled, <c>false</c> otherwise.
/// </summary>
public static bool isVREnabled
{
get
{
#if (ENABLE_VR_MODULE && ENABLE_VR) && UNITY_EDITOR && !UNITY_2020_1_OR_NEWER
return UnityEditorInternal.VR.VREditor.GetVREnabledOnTargetGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget));
#elif UNITY_XBOXONE || !(ENABLE_VR_MODULE && ENABLE_VR)
return false;
#else
return UnityEngine.XR.XRSettings.enabled;
#endif
}
}
/// <summary>
/// Returns <c>true</c> if the target platform is Android and the selected API is OpenGL,
/// <c>false</c> otherwise.
/// </summary>
public static bool isAndroidOpenGL
{
get { return Application.platform == RuntimePlatform.Android && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan; }
}
/// <summary>
/// Returns <c>true</c> if the target platform is WebGL,
/// <c>false</c> otherwise.
/// </summary>
public static bool isWebNonWebGPU
{
get
{
#if UNITY_EDITOR
#if UNITY_WEBGL
#if UNITY_2023_2_OR_NEWER
return PlayerSettings.GetGraphicsAPIs(BuildTarget.WebGL).First() != GraphicsDeviceType.WebGPU;
#else
return true;
#endif
#else
return false;
#endif
#else
return Application.platform == RuntimePlatform.WebGLPlayer
#if UNITY_2023_2_OR_NEWER
&& SystemInfo.graphicsDeviceType != GraphicsDeviceType.WebGPU
#endif
;
#endif
}
}
/// <summary>
/// Gets the default HDR render texture format for the current target platform.
/// </summary>
public static RenderTextureFormat defaultHDRRenderTextureFormat
{
get
{
#if !UNITY_2019_3_OR_NEWER && (UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_EDITOR)
RenderTextureFormat format = RenderTextureFormat.RGB111110Float;
#if UNITY_EDITOR
var target = EditorUserBuildSettings.activeBuildTarget;
if (target != BuildTarget.Android && target != BuildTarget.iOS && target != BuildTarget.tvOS)
return RenderTextureFormat.DefaultHDR;
#endif // UNITY_EDITOR
if (format.IsSupported())
return format;
#endif // #if !UNITY_2019_3_OR_NEWER && (UNITY_ANDROID || UNITY_IPHONE || UNITY_TVOS || UNITY_EDITOR)
return RenderTextureFormat.DefaultHDR;
}
}
/// <summary>
/// Checks if a given render texture format is a floating-point format.
/// </summary>
/// <param name="format">The format to test</param>
/// <returns><c>true</c> if the format is floating-point, <c>false</c> otherwise</returns>
public static bool isFloatingPointFormat(RenderTextureFormat format)
{
return format == RenderTextureFormat.DefaultHDR || format == RenderTextureFormat.ARGBHalf || format == RenderTextureFormat.ARGBFloat ||
format == RenderTextureFormat.RGFloat || format == RenderTextureFormat.RGHalf ||
format == RenderTextureFormat.RFloat || format == RenderTextureFormat.RHalf ||
format == RenderTextureFormat.RGB111110Float;
}
/// <summary>
/// Checks if a given render texture format has an alpha channel.
/// </summary>
/// <param name="format">The format to test</param>
/// <returns><c>true</c> if the format has an alpha channel, <c>false</c> otherwise</returns>
internal static bool hasAlpha(RenderTextureFormat format)
{
UnityEngine.Experimental.Rendering.GraphicsFormat gformat = UnityEngine.Experimental.Rendering.GraphicsFormatUtility.GetGraphicsFormat(format, RenderTextureReadWrite.Default);
return UnityEngine.Experimental.Rendering.GraphicsFormatUtility.HasAlphaChannel(gformat);
}
/// <summary>
/// Properly destroys a given Unity object.
/// </summary>
/// <param name="obj">The object to destroy</param>
public static void Destroy(UnityObject obj)
{
if (obj != null)
{
#if UNITY_EDITOR
if (Application.isPlaying)
UnityObject.Destroy(obj);
else
UnityObject.DestroyImmediate(obj);
#else
UnityObject.Destroy(obj);
#endif
}
}
/// <summary>
/// Returns <c>true</c> if the current color space setting is set to <c>Linear</c>,
/// <c>false</c> otherwise.
/// </summary>
public static bool isLinearColorSpace
{
get { return QualitySettings.activeColorSpace == ColorSpace.Linear; }
}
/// <summary>
/// Checks if resolved depth is available on the current target platform.
/// </summary>
/// <param name="camera">A rendering camera</param>
/// <returns><c>true</c> if resolved depth is available, <c>false</c> otherwise</returns>
public static bool IsResolvedDepthAvailable(Camera camera)
{
// AFAIK resolved depth is only available on D3D11/12 via BuiltinRenderTextureType.ResolvedDepth
// TODO: Is there more proper way to determine this? What about SRPs?
var gtype = SystemInfo.graphicsDeviceType;