-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathCoreUtils.cs
More file actions
1633 lines (1477 loc) · 85 KB
/
CoreUtils.cs
File metadata and controls
1633 lines (1477 loc) · 85 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 UnityEngine.Experimental.Rendering;
namespace UnityEngine.Rendering
{
using UnityObject = UnityEngine.Object;
/// <summary>
/// Set of utility functions for the Core Scriptable Render Pipeline Library
/// </summary>
public static class CoreUtils
{
/// <summary>
/// List of look at matrices for cubemap faces.
/// Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/bb204881(v=vs.85).aspx
/// </summary>
static public readonly Vector3[] lookAtList =
{
new Vector3(1.0f, 0.0f, 0.0f),
new Vector3(-1.0f, 0.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, -1.0f, 0.0f),
new Vector3(0.0f, 0.0f, 1.0f),
new Vector3(0.0f, 0.0f, -1.0f),
};
/// <summary>
/// List of up vectors for cubemap faces.
/// Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/bb204881(v=vs.85).aspx
/// </summary>
static public readonly Vector3[] upVectorList =
{
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, 0.0f, -1.0f),
new Vector3(0.0f, 0.0f, 1.0f),
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f),
};
/// <summary>
/// Class to store the menu sections
/// </summary>
public static class Sections
{
/// <summary>Menu section 1</summary>
public const int section1 = 10000;
/// <summary>Menu section 2</summary>
public const int section2 = 20000;
/// <summary>Menu section 3</summary>
public const int section3 = 30000;
/// <summary>Menu section 4</summary>
public const int section4 = 40000;
/// <summary>Menu section 5</summary>
public const int section5 = 50000;
/// <summary>Menu section 6</summary>
public const int section6 = 60000;
/// <summary>Menu section 7</summary>
public const int section7 = 70000;
/// <summary>Menu section 8</summary>
public const int section8 = 80000;
}
/// <summary>
/// Class to store the menu priorities on each top level menu
/// </summary>
public static class Priorities
{
/// <summary>Assets > Create > Shader priority</summary>
public const int assetsCreateShaderMenuPriority = 83;
/// <summary>Assets > Create > Rendering priority</summary>
public const int assetsCreateRenderingMenuPriority = 308;
/// <summary>Edit Menu base priority</summary>
public const int editMenuPriority = 320;
/// <summary>Game Object Menu priority</summary>
public const int gameObjectMenuPriority = 10;
/// <summary>Lens Flare Priority</summary>
public const int srpLensFlareMenuPriority = 303;
}
const string obsoletePriorityMessage = "Use CoreUtils.Priorities instead";
/// <summary>Edit Menu priority 1</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int editMenuPriority1 = 320;
/// <summary>Edit Menu priority 2</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int editMenuPriority2 = 331;
/// <summary>Edit Menu priority 3</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int editMenuPriority3 = 342;
/// <summary>Edit Menu priority 4</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int editMenuPriority4 = 353;
/// <summary>Asset Create Menu priority 1</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int assetCreateMenuPriority1 = 230;
/// <summary>Asset Create Menu priority 2</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int assetCreateMenuPriority2 = 241;
/// <summary>Asset Create Menu priority 3</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int assetCreateMenuPriority3 = 300;
/// <summary>Game Object Menu priority</summary>
[Obsolete(obsoletePriorityMessage, false)]
public const int gameObjectMenuPriority = 10;
static Cubemap m_BlackCubeTexture;
/// <summary>
/// Black cubemap texture.
/// </summary>
public static Cubemap blackCubeTexture
{
get
{
if (m_BlackCubeTexture == null)
{
m_BlackCubeTexture = new Cubemap(1, GraphicsFormat.R8G8B8A8_SRGB, TextureCreationFlags.None);
for (int i = 0; i < 6; ++i)
m_BlackCubeTexture.SetPixel((CubemapFace)i, 0, 0, Color.black);
m_BlackCubeTexture.Apply();
}
return m_BlackCubeTexture;
}
}
static Cubemap m_MagentaCubeTexture;
/// <summary>
/// Magenta cubemap texture.
/// </summary>
public static Cubemap magentaCubeTexture
{
get
{
if (m_MagentaCubeTexture == null)
{
m_MagentaCubeTexture = new Cubemap(1, GraphicsFormat.R8G8B8A8_SRGB, TextureCreationFlags.None);
for (int i = 0; i < 6; ++i)
m_MagentaCubeTexture.SetPixel((CubemapFace)i, 0, 0, Color.magenta);
m_MagentaCubeTexture.Apply();
}
return m_MagentaCubeTexture;
}
}
static CubemapArray m_MagentaCubeTextureArray;
/// <summary>
/// Black cubemap array texture.
/// </summary>
public static CubemapArray magentaCubeTextureArray
{
get
{
if (m_MagentaCubeTextureArray == null)
{
m_MagentaCubeTextureArray = new CubemapArray(1, 1, GraphicsFormat.R32G32B32A32_SFloat, TextureCreationFlags.None);
for (int i = 0; i < 6; ++i)
{
Color[] colors = { Color.magenta };
m_MagentaCubeTextureArray.SetPixels(colors, (CubemapFace)i, 0);
}
m_MagentaCubeTextureArray.Apply();
}
return m_MagentaCubeTextureArray;
}
}
static Cubemap m_WhiteCubeTexture;
/// <summary>
/// White cubemap texture.
/// </summary>
public static Cubemap whiteCubeTexture
{
get
{
if (m_WhiteCubeTexture == null)
{
m_WhiteCubeTexture = new Cubemap(1, GraphicsFormat.R8G8B8A8_SRGB, TextureCreationFlags.None);
for (int i = 0; i < 6; ++i)
m_WhiteCubeTexture.SetPixel((CubemapFace)i, 0, 0, Color.white);
m_WhiteCubeTexture.Apply();
}
return m_WhiteCubeTexture;
}
}
static RenderTexture m_EmptyUAV;
/// <summary>
/// Empty 1x1 texture usable as a dummy UAV.
/// </summary>
public static RenderTexture emptyUAV
{
get
{
if (m_EmptyUAV == null)
{
m_EmptyUAV = new RenderTexture(1, 1, 0);
m_EmptyUAV.enableRandomWrite = true;
m_EmptyUAV.Create();
}
return m_EmptyUAV;
}
}
static Texture3D m_BlackVolumeTexture;
/// <summary>
/// Black 3D texture.
/// </summary>
public static Texture3D blackVolumeTexture
{
get
{
if (m_BlackVolumeTexture == null)
{
Color[] colors = { Color.black };
m_BlackVolumeTexture = new Texture3D(1, 1, 1, GraphicsFormat.R8G8B8A8_SRGB, TextureCreationFlags.None);
m_BlackVolumeTexture.SetPixels(colors, 0);
m_BlackVolumeTexture.Apply();
}
return m_BlackVolumeTexture;
}
}
/// <summary>
/// Clear the currently bound render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="clearFlag">Specify how the render texture should be cleared.</param>
/// <param name="clearColor">Specify with which color the render texture should be cleared.</param>
public static void ClearRenderTarget(CommandBuffer cmd, ClearFlag clearFlag, Color clearColor)
{
if (clearFlag != ClearFlag.None)
cmd.ClearRenderTarget((RTClearFlags)clearFlag, clearColor, 1.0f, 0x00);
}
// We use -1 as a default value because when doing SPI for XR, it will bind the full texture array by default (and has no effect on 2D textures)
// Unfortunately, for cubemaps, passing -1 does not work for faces other than the first one, so we fall back to 0 in this case.
private static int FixupDepthSlice(int depthSlice, RTHandle buffer)
{
// buffer.rt can be null in case the RTHandle is constructed from a RenderTextureIdentifier.
if (depthSlice == -1 && buffer.rt?.dimension == TextureDimension.Cube)
depthSlice = 0;
return depthSlice;
}
private static int FixupDepthSlice(int depthSlice, CubemapFace cubemapFace)
{
if (depthSlice == -1 && cubemapFace != CubemapFace.Unknown)
depthSlice = 0;
return depthSlice;
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">RenderTargetIdentifier of the render texture.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier buffer, ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
depthSlice = FixupDepthSlice(depthSlice, cubemapFace);
cmd.SetRenderTarget(buffer, miplevel, cubemapFace, depthSlice);
ClearRenderTarget(cmd, clearFlag, clearColor);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">RenderTargetIdentifier of the render texture.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier buffer, ClearFlag clearFlag = ClearFlag.None, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
SetRenderTarget(cmd, buffer, clearFlag, Color.clear, miplevel, cubemapFace, depthSlice);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">RenderTargetIdentifier of the color render texture.</param>
/// <param name="depthBuffer">RenderTargetIdentifier of the depth render texture.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
SetRenderTarget(cmd, colorBuffer, depthBuffer, ClearFlag.None, Color.clear, miplevel, cubemapFace, depthSlice);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">RenderTargetIdentifier of the color render texture.</param>
/// <param name="depthBuffer">RenderTargetIdentifier of the depth render texture.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
SetRenderTarget(cmd, colorBuffer, depthBuffer, clearFlag, Color.clear, miplevel, cubemapFace, depthSlice);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">RenderTargetIdentifier of the color render texture.</param>
/// <param name="depthBuffer">RenderTargetIdentifier of the depth render texture.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
depthSlice = FixupDepthSlice(depthSlice, cubemapFace);
cmd.SetRenderTarget(colorBuffer, depthBuffer, miplevel, cubemapFace, depthSlice);
ClearRenderTarget(cmd, clearFlag, clearColor);
}
/// <summary>
/// Set the current multiple render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color render textures.</param>
/// <param name="depthBuffer">RenderTargetIdentifier of the depth render texture.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer)
{
SetRenderTarget(cmd, colorBuffers, depthBuffer, ClearFlag.None, Color.clear);
}
/// <summary>
/// Set the current multiple render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color render textures.</param>
/// <param name="depthBuffer">RenderTargetIdentifier of the depth render texture.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag = ClearFlag.None)
{
SetRenderTarget(cmd, colorBuffers, depthBuffer, clearFlag, Color.clear);
}
/// <summary>
/// Set the current multiple render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color render textures.</param>
/// <param name="depthBuffer">RenderTargetIdentifier of the depth render texture.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor)
{
cmd.SetRenderTarget(colorBuffers, depthBuffer, 0, CubemapFace.Unknown, -1);
ClearRenderTarget(cmd, clearFlag, clearColor);
}
// Explicit load and store actions
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="loadAction">Load action.</param>
/// <param name="storeAction">Store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier buffer, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, ClearFlag clearFlag, Color clearColor)
{
cmd.SetRenderTarget(buffer, loadAction, storeAction);
ClearRenderTarget(cmd, clearFlag, clearColor);
}
// Explicit load and store actions
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="loadAction">Load action.</param>
/// <param name="storeAction">Store action.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier buffer, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction,
int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
depthSlice = FixupDepthSlice(depthSlice, cubemapFace);
buffer = new RenderTargetIdentifier(buffer, miplevel, cubemapFace, depthSlice);
cmd.SetRenderTarget(buffer, loadAction, storeAction);
}
// Explicit load and store actions
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="loadAction">Load action.</param>
/// <param name="storeAction">Store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier buffer, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction,
ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
depthSlice = FixupDepthSlice(depthSlice, cubemapFace);
buffer = new RenderTargetIdentifier(buffer, miplevel, cubemapFace, depthSlice);
SetRenderTarget(cmd, buffer, loadAction, storeAction, clearFlag, clearColor);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="loadAction">Load action.</param>
/// <param name="storeAction">Store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier buffer, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, ClearFlag clearFlag)
{
SetRenderTarget(cmd, buffer, loadAction, storeAction, clearFlag, Color.clear);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="colorLoadAction">Color buffer load action.</param>
/// <param name="colorStoreAction">Color buffer store action.</param>
/// <param name="depthBuffer">Depth buffer RenderTargetIdentifier.</param>
/// <param name="depthLoadAction">Depth buffer load action.</param>
/// <param name="depthStoreAction">Depth buffer store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier colorBuffer, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RenderTargetIdentifier depthBuffer, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction,
ClearFlag clearFlag, Color clearColor)
{
cmd.SetRenderTarget(colorBuffer, colorLoadAction, colorStoreAction, depthBuffer, depthLoadAction, depthStoreAction);
ClearRenderTarget(cmd, clearFlag, clearColor);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="colorLoadAction">Color buffer load action.</param>
/// <param name="colorStoreAction">Color buffer store action.</param>
/// <param name="depthBuffer">Depth buffer RenderTargetIdentifier.</param>
/// <param name="depthLoadAction">Depth buffer load action.</param>
/// <param name="depthStoreAction">Depth buffer store action.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier colorBuffer, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RenderTargetIdentifier depthBuffer, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction,
int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
depthSlice = FixupDepthSlice(depthSlice, cubemapFace);
colorBuffer = new RenderTargetIdentifier(colorBuffer, miplevel, cubemapFace, depthSlice);
depthBuffer = new RenderTargetIdentifier(depthBuffer, miplevel, cubemapFace, depthSlice);
cmd.SetRenderTarget(colorBuffer, colorLoadAction, colorStoreAction, depthBuffer, depthLoadAction, depthStoreAction);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="colorLoadAction">Color buffer load action.</param>
/// <param name="colorStoreAction">Color buffer store action.</param>
/// <param name="depthBuffer">Depth buffer RenderTargetIdentifier.</param>
/// <param name="depthLoadAction">Depth buffer load action.</param>
/// <param name="depthStoreAction">Depth buffer store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier colorBuffer, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RenderTargetIdentifier depthBuffer, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction,
ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
depthSlice = FixupDepthSlice(depthSlice, cubemapFace);
colorBuffer = new RenderTargetIdentifier(colorBuffer, miplevel, cubemapFace, depthSlice);
depthBuffer = new RenderTargetIdentifier(depthBuffer, miplevel, cubemapFace, depthSlice);
SetRenderTarget(cmd, colorBuffer, colorLoadAction, colorStoreAction, depthBuffer, depthLoadAction, depthStoreAction, clearFlag, clearColor);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">RenderTargetIdentifier of the render texture.</param>
/// <param name="colorLoadAction">Color buffer load action.</param>
/// <param name="colorStoreAction">Color buffer store action.</param>
/// <param name="depthLoadAction">Depth buffer load action.</param>
/// <param name="depthStoreAction">Depth buffer store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier buffer, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction, ClearFlag clearFlag, Color clearColor)
{
cmd.SetRenderTarget(buffer, colorLoadAction, colorStoreAction, depthLoadAction, depthStoreAction);
ClearRenderTarget(cmd, clearFlag, clearColor);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">Color buffer RenderTargetIdentifier.</param>
/// <param name="colorLoadAction">Color buffer load action.</param>
/// <param name="colorStoreAction">Color buffer store action.</param>
/// <param name="depthBuffer">Depth buffer RenderTargetIdentifier.</param>
/// <param name="depthLoadAction">Depth buffer load action.</param>
/// <param name="depthStoreAction">Depth buffer store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier colorBuffer, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RenderTargetIdentifier depthBuffer, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction,
ClearFlag clearFlag)
{
SetRenderTarget(cmd, colorBuffer, colorLoadAction, colorStoreAction, depthBuffer, depthLoadAction, depthStoreAction, clearFlag, Color.clear);
}
private static void SetViewportAndClear(CommandBuffer cmd, RTHandle buffer, ClearFlag clearFlag, Color clearColor)
{
// Clearing a partial viewport currently does not go through the hardware clear.
// Instead it goes through a quad rendered with a specific shader.
// When enabling wireframe mode in the scene view, unfortunately it overrides this shader thus breaking every clears.
// That's why in the editor we don't set the viewport before clearing (it's set to full screen by the previous SetRenderTarget) but AFTER so that we benefit from un-bugged hardware clear.
// We consider that the small loss in performance is acceptable in the editor.
// A refactor of wireframe is needed before we can fix this properly (with not doing anything!)
#if !UNITY_EDITOR
SetViewport(cmd, buffer);
#endif
CoreUtils.ClearRenderTarget(cmd, clearFlag, clearColor);
#if UNITY_EDITOR
SetViewport(cmd, buffer);
#endif
}
// This set of RenderTarget management methods is supposed to be used when rendering RTHandle render texture.
// This will automatically set the viewport based on the RTHandle System reference size and the RTHandle scaling info.
/// <summary>
/// Setup the current render texture using an RTHandle
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands</param>
/// <param name="buffer">Color buffer RTHandle</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RTHandle buffer, ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
depthSlice = FixupDepthSlice(depthSlice, buffer);
cmd.SetRenderTarget(buffer.nameID, miplevel, cubemapFace, depthSlice);
SetViewportAndClear(cmd, buffer, clearFlag, clearColor);
}
/// <summary>
/// Setup the current render texture using an RTHandle
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands</param>
/// <param name="buffer">Color buffer RTHandle</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RTHandle buffer, ClearFlag clearFlag = ClearFlag.None, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
=> SetRenderTarget(cmd, buffer, clearFlag, Color.clear, miplevel, cubemapFace, depthSlice);
/// <summary>
/// Setup the current render texture using an RTHandle
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands</param>
/// <param name="colorBuffer">Color buffer RTHandle</param>
/// <param name="depthBuffer">Depth buffer RTHandle</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RTHandle colorBuffer, RTHandle depthBuffer, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
if (colorBuffer.rt != null && depthBuffer.rt != null)
{
int cw = colorBuffer.rt.width;
int ch = colorBuffer.rt.height;
int dw = depthBuffer.rt.width;
int dh = depthBuffer.rt.height;
Debug.Assert(cw == dw && ch == dh);
}
SetRenderTarget(cmd, colorBuffer, depthBuffer, ClearFlag.None, Color.clear, miplevel, cubemapFace, depthSlice);
}
/// <summary>
/// Setup the current render texture using an RTHandle
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands</param>
/// <param name="colorBuffer">Color buffer RTHandle</param>
/// <param name="depthBuffer">Depth buffer RTHandle</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RTHandle colorBuffer, RTHandle depthBuffer, ClearFlag clearFlag, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
if (colorBuffer.rt != null && depthBuffer.rt != null)
{
int cw = colorBuffer.rt.width;
int ch = colorBuffer.rt.height;
int dw = depthBuffer.rt.width;
int dh = depthBuffer.rt.height;
Debug.Assert(cw == dw && ch == dh);
}
SetRenderTarget(cmd, colorBuffer, depthBuffer, clearFlag, Color.clear, miplevel, cubemapFace, depthSlice);
}
/// <summary>
/// Setup the current render texture using an RTHandle
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands</param>
/// <param name="colorBuffer">Color buffer RTHandle</param>
/// <param name="depthBuffer">Depth buffer RTHandle</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RTHandle colorBuffer, RTHandle depthBuffer, ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
if (colorBuffer.rt != null && depthBuffer.rt != null)
{
int cw = colorBuffer.rt.width;
int ch = colorBuffer.rt.height;
int dw = depthBuffer.rt.width;
int dh = depthBuffer.rt.height;
Debug.Assert(cw == dw && ch == dh);
}
SetRenderTarget(cmd, colorBuffer.nameID, depthBuffer.nameID, miplevel, cubemapFace, depthSlice);
SetViewportAndClear(cmd, colorBuffer, clearFlag, clearColor);
}
// Explicit load and store actions
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="buffer">Color buffer RTHandleRTR.</param>
/// <param name="loadAction">Load action.</param>
/// <param name="storeAction">Store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RTHandle buffer, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
SetRenderTarget(cmd, buffer.nameID, loadAction, storeAction, miplevel, cubemapFace, depthSlice);
SetViewportAndClear(cmd, buffer, clearFlag, clearColor);
}
/// <summary>
/// Set the current render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffer">Color buffer RTHandle.</param>
/// <param name="colorLoadAction">Color buffer load action.</param>
/// <param name="colorStoreAction">Color buffer store action.</param>
/// <param name="depthBuffer">Depth buffer RTHandle.</param>
/// <param name="depthLoadAction">Depth buffer load action.</param>
/// <param name="depthStoreAction">Depth buffer store action.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
/// <param name="miplevel">Mip level that should be bound as a render texture if applicable.</param>
/// <param name="cubemapFace">Cubemap face that should be bound as a render texture if applicable.</param>
/// <param name="depthSlice">Depth slice that should be bound as a render texture if applicable.</param>
public static void SetRenderTarget(CommandBuffer cmd, RTHandle colorBuffer, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
RTHandle depthBuffer, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction,
ClearFlag clearFlag, Color clearColor, int miplevel = 0, CubemapFace cubemapFace = CubemapFace.Unknown, int depthSlice = -1)
{
if (colorBuffer.rt != null && depthBuffer.rt != null)
{
int cw = colorBuffer.rt.width;
int ch = colorBuffer.rt.height;
int dw = depthBuffer.rt.width;
int dh = depthBuffer.rt.height;
Debug.Assert(cw == dw && ch == dh);
}
SetRenderTarget(cmd, colorBuffer.nameID, colorLoadAction, colorStoreAction, depthBuffer.nameID, depthLoadAction, depthStoreAction, miplevel, cubemapFace, depthSlice);
SetViewportAndClear(cmd, colorBuffer, clearFlag, clearColor);
}
/// <summary>
/// Set the current multiple render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color render textures.</param>
/// <param name="depthBuffer">Depth Buffer RTHandle.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier[] colorBuffers, RTHandle depthBuffer)
{
SetRenderTarget(cmd, colorBuffers, depthBuffer.nameID, ClearFlag.None, Color.clear);
SetViewport(cmd, depthBuffer);
}
/// <summary>
/// Set the current multiple render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color render textures.</param>
/// <param name="depthBuffer">Depth Buffer RTHandle.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier[] colorBuffers, RTHandle depthBuffer, ClearFlag clearFlag = ClearFlag.None)
{
SetRenderTarget(cmd, colorBuffers, depthBuffer.nameID); // Don't clear here, viewport needs to be set before we do.
SetViewportAndClear(cmd, depthBuffer, clearFlag, Color.clear);
}
/// <summary>
/// Set the current multiple render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color render textures.</param>
/// <param name="depthBuffer">Depth Buffer RTHandle.</param>
/// <param name="clearFlag">If not set to ClearFlag.None, specifies how to clear the render target after setup.</param>
/// <param name="clearColor">If applicable, color with which to clear the render texture after setup.</param>
public static void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier[] colorBuffers, RTHandle depthBuffer, ClearFlag clearFlag, Color clearColor)
{
cmd.SetRenderTarget(colorBuffers, depthBuffer.nameID, 0, CubemapFace.Unknown, -1);
SetViewportAndClear(cmd, depthBuffer, clearFlag, clearColor);
}
// Scaling viewport is done for auto-scaling render targets.
// In the context of SRP, every auto-scaled RT is scaled against the maximum RTHandles reference size (that can only grow).
// When we render using a camera whose viewport is smaller than the RTHandles reference size (and thus smaller than the RT actual size), we need to set it explicitly (otherwise, native code will set the viewport at the size of the RT)
// For auto-scaled RTs (like for example a half-resolution RT), we need to scale this viewport accordingly.
// For non scaled RTs we just do nothing, the native code will set the viewport at the size of the RT anyway.
/// <summary>
/// Setup the viewport to the size of the provided RTHandle.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="target">RTHandle from which to compute the proper viewport.</param>
public static void SetViewport(CommandBuffer cmd, RTHandle target)
{
if (target.useScaling)
{
Vector2Int scaledViewportSize = target.GetScaledSize(target.rtHandleProperties.currentViewportSize);
cmd.SetViewport(new Rect(0.0f, 0.0f, scaledViewportSize.x, scaledViewportSize.y));
}
}
/// <summary>
/// Generate a name based on render texture parameters.
/// </summary>
/// <param name="width">With of the texture.</param>
/// <param name="height">Height of the texture.</param>
/// <param name="depth">Depth of the texture.</param>
/// <param name="format">Format of the render texture.</param>
/// <param name="name">Base name of the texture.</param>
/// <param name="mips">True if the texture has mip maps.</param>
/// <param name="enableMSAA">True if the texture is multisampled.</param>
/// <param name="msaaSamples">Number of MSAA samples.</param>
/// <returns>Generated names bassed on the provided parameters.</returns>
public static string GetRenderTargetAutoName(int width, int height, int depth, RenderTextureFormat format, string name, bool mips = false, bool enableMSAA = false, MSAASamples msaaSamples = MSAASamples.None)
=> GetRenderTargetAutoName(width, height, depth, format.ToString(), TextureDimension.None, name, mips, enableMSAA, msaaSamples, dynamicRes: false);
/// <summary>
/// Generate a name based on render texture parameters.
/// </summary>
/// <param name="width">With of the texture.</param>
/// <param name="height">Height of the texture.</param>
/// <param name="depth">Depth of the texture.</param>
/// <param name="format">Graphics format of the render texture.</param>
/// <param name="name">Base name of the texture.</param>
/// <param name="mips">True if the texture has mip maps.</param>
/// <param name="enableMSAA">True if the texture is multisampled.</param>
/// <param name="msaaSamples">Number of MSAA samples.</param>
/// <returns>Generated names bassed on the provided parameters.</returns>
public static string GetRenderTargetAutoName(int width, int height, int depth, GraphicsFormat format, string name, bool mips = false, bool enableMSAA = false, MSAASamples msaaSamples = MSAASamples.None)
=> GetRenderTargetAutoName(width, height, depth, format.ToString(), TextureDimension.None, name, mips, enableMSAA, msaaSamples, dynamicRes: false);
/// <summary>
/// Generate a name based on render texture parameters.
/// </summary>
/// <param name="width">With of the texture.</param>
/// <param name="height">Height of the texture.</param>
/// <param name="depth">Depth of the texture.</param>
/// <param name="format">Graphics format of the render texture.</param>
/// <param name="dim">Dimension of the texture.</param>
/// <param name="name">Base name of the texture.</param>
/// <param name="mips">True if the texture has mip maps.</param>
/// <param name="enableMSAA">True if the texture is multisampled.</param>
/// <param name="msaaSamples">Number of MSAA samples.</param>
/// <param name="dynamicRes">True if the texture uses dynamic resolution.</param>
/// <returns>Generated names bassed on the provided parameters.</returns>
public static string GetRenderTargetAutoName(int width, int height, int depth, GraphicsFormat format, TextureDimension dim, string name, bool mips = false, bool enableMSAA = false, MSAASamples msaaSamples = MSAASamples.None, bool dynamicRes = false)
=> GetRenderTargetAutoName(width, height, depth, format.ToString(), dim, name, mips, enableMSAA, msaaSamples, dynamicRes);
static string GetRenderTargetAutoName(int width, int height, int depth, string format, TextureDimension dim, string name, bool mips, bool enableMSAA, MSAASamples msaaSamples, bool dynamicRes)
{
string result = string.Format("{0}_{1}x{2}", name, width, height);
if (depth > 1)
result = string.Format("{0}x{1}", result, depth);
if (mips)
result = string.Format("{0}_{1}", result, "Mips");
result = string.Format("{0}_{1}", result, format);
if (dim != TextureDimension.None)
result = string.Format("{0}_{1}", result, dim);
if (enableMSAA)
result = string.Format("{0}_{1}", result, msaaSamples.ToString());
if (dynamicRes)
result = string.Format("{0}_{1}", result, "dynamic");
return result;
}
/// <summary>
/// Generate a name based on texture parameters.
/// </summary>
/// <param name="width">With of the texture.</param>
/// <param name="height">Height of the texture.</param>
/// <param name="format">Format of the texture.</param>
/// <param name="dim">Dimension of the texture.</param>
/// <param name="name">Base name of the texture.</param>
/// <param name="mips">True if the texture has mip maps.</param>
/// <param name="depth">Depth of the texture.</param>
/// <returns>Generated names based on the provided parameters.</returns>
public static string GetTextureAutoName(int width, int height, TextureFormat format, TextureDimension dim = TextureDimension.None, string name = "", bool mips = false, int depth = 0)
=> GetTextureAutoName(width, height, format.ToString(), dim, name, mips, depth);
/// <summary>
/// Generate a name based on texture parameters.
/// </summary>
/// <param name="width">With of the texture.</param>
/// <param name="height">Height of the texture.</param>
/// <param name="format">Graphics format of the texture.</param>
/// <param name="dim">Dimension of the texture.</param>
/// <param name="name">Base name of the texture.</param>
/// <param name="mips">True if the texture has mip maps.</param>
/// <param name="depth">Depth of the texture.</param>
/// <returns>Generated names based on the provided parameters.</returns>
public static string GetTextureAutoName(int width, int height, GraphicsFormat format, TextureDimension dim = TextureDimension.None, string name = "", bool mips = false, int depth = 0)
=> GetTextureAutoName(width, height, format.ToString(), dim, name, mips, depth);
static string GetTextureAutoName(int width, int height, string format, TextureDimension dim = TextureDimension.None, string name = "", bool mips = false, int depth = 0)
{
string temp;
if (depth == 0)
temp = string.Format("{0}x{1}{2}_{3}", width, height, mips ? "_Mips" : "", format);
else
temp = string.Format("{0}x{1}x{2}{3}_{4}", width, height, depth, mips ? "_Mips" : "", format);
temp = String.Format("{0}_{1}_{2}", name?.Length == 0 ? "Texture" : name, (dim == TextureDimension.None) ? "" : dim.ToString(), temp);
return temp;
}
/// <summary>
/// Clear a cubemap render texture.
/// </summary>
/// <param name="cmd">CommandBuffer used for rendering commands.</param>
/// <param name="renderTexture">Cubemap render texture that needs to be cleared.</param>
/// <param name="clearColor">Color used for clearing.</param>
/// <param name="clearMips">Set to true to clear the mip maps of the render texture.</param>
public static void ClearCubemap(CommandBuffer cmd, RenderTexture renderTexture, Color clearColor, bool clearMips = false)
{
int mipCount = 1;
if (renderTexture.useMipMap && clearMips)
{
mipCount = (int)Mathf.Log((float)renderTexture.width, 2.0f) + 1;
}
for (int i = 0; i < 6; ++i)
{
for (int mip = 0; mip < mipCount; ++mip)
{
SetRenderTarget(cmd, new RenderTargetIdentifier(renderTexture), ClearFlag.Color, clearColor, mip, (CubemapFace)i);
}
}
}
/// <summary>
/// Draws a full screen triangle.
/// </summary>
/// <param name="commandBuffer">CommandBuffer used for rendering commands.</param>
/// <param name="material">Material used on the full screen triangle.</param>
/// <param name="properties">Optional material property block for the provided material.</param>
/// <param name="shaderPassId">Index of the material pass.</param>
public static void DrawFullScreen(CommandBuffer commandBuffer, Material material,
MaterialPropertyBlock properties = null, int shaderPassId = 0)
{
commandBuffer.DrawProcedural(Matrix4x4.identity, material, shaderPassId, MeshTopology.Triangles, 3, 1, properties);
}
/// <summary>
/// Draws a full screen triangle.
/// </summary>
/// <param name="commandBuffer">CommandBuffer used for rendering commands.</param>
/// <param name="material">Material used on the full screen triangle.</param>
/// <param name="colorBuffer">RenderTargetIdentifier of the color buffer that needs to be set before drawing the full screen triangle.</param>
/// <param name="properties">Optional material property block for the provided material.</param>
/// <param name="shaderPassId">Index of the material pass.</param>
public static void DrawFullScreen(CommandBuffer commandBuffer, Material material,
RenderTargetIdentifier colorBuffer,
MaterialPropertyBlock properties = null, int shaderPassId = 0)
{
commandBuffer.SetRenderTarget(colorBuffer, 0, CubemapFace.Unknown, -1);
commandBuffer.DrawProcedural(Matrix4x4.identity, material, shaderPassId, MeshTopology.Triangles, 3, 1, properties);
}
/// <summary>
/// Draws a full screen triangle.
/// </summary>
/// <param name="commandBuffer">CommandBuffer used for rendering commands.</param>
/// <param name="material">Material used on the full screen triangle.</param>
/// <param name="colorBuffer">RenderTargetIdentifier of the color buffer that needs to be set before drawing the full screen triangle.</param>
/// <param name="depthStencilBuffer">RenderTargetIdentifier of the depth buffer that needs to be set before drawing the full screen triangle.</param>
/// <param name="properties">Optional material property block for the provided material.</param>
/// <param name="shaderPassId">Index of the material pass.</param>
public static void DrawFullScreen(CommandBuffer commandBuffer, Material material,
RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthStencilBuffer,
MaterialPropertyBlock properties = null, int shaderPassId = 0)
{
commandBuffer.SetRenderTarget(colorBuffer, depthStencilBuffer, 0, CubemapFace.Unknown, -1);
commandBuffer.DrawProcedural(Matrix4x4.identity, material, shaderPassId, MeshTopology.Triangles, 3, 1, properties);
}
/// <summary>
/// Draws a full screen triangle.
/// </summary>
/// <param name="commandBuffer">CommandBuffer used for rendering commands.</param>
/// <param name="material">Material used on the full screen triangle.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color buffers that needs to be set before drawing the full screen triangle.</param>
/// <param name="depthStencilBuffer">RenderTargetIdentifier of the depth buffer that needs to be set before drawing the full screen triangle.</param>
/// <param name="properties">Optional material property block for the provided material.</param>
/// <param name="shaderPassId">Index of the material pass.</param>
public static void DrawFullScreen(CommandBuffer commandBuffer, Material material,
RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthStencilBuffer,
MaterialPropertyBlock properties = null, int shaderPassId = 0)
{
commandBuffer.SetRenderTarget(colorBuffers, depthStencilBuffer, 0, CubemapFace.Unknown, -1);
commandBuffer.DrawProcedural(Matrix4x4.identity, material, shaderPassId, MeshTopology.Triangles, 3, 1, properties);
}
// Important: the first RenderTarget must be created with 0 depth bits!
/// <summary>
/// Draws a full screen triangle.
/// </summary>
/// <param name="commandBuffer">CommandBuffer used for rendering commands.</param>
/// <param name="material">Material used on the full screen triangle.</param>
/// <param name="colorBuffers">RenderTargetIdentifier array of the color buffers that needs to be set before drawing the full screen triangle.</param>
/// <param name="properties">Optional material property block for the provided material.</param>
/// <param name="shaderPassId">Index of the material pass.</param>
public static void DrawFullScreen(CommandBuffer commandBuffer, Material material,
RenderTargetIdentifier[] colorBuffers,
MaterialPropertyBlock properties = null, int shaderPassId = 0)
{
// It is currently not possible to have MRT without also setting a depth target.
// To work around this deficiency of the CommandBuffer.SetRenderTarget() API,