-
Notifications
You must be signed in to change notification settings - Fork 866
Expand file tree
/
Copy pathRenderGraphResourceTexture.cs
More file actions
685 lines (622 loc) · 30 KB
/
RenderGraphResourceTexture.cs
File metadata and controls
685 lines (622 loc) · 30 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
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEngine.Rendering.RenderGraphModule
{
internal readonly struct TextureAccess
{
public readonly TextureHandle textureHandle;
public readonly int mipLevel;
public readonly int depthSlice;
public readonly AccessFlags flags;
public TextureAccess(in TextureHandle handle, AccessFlags flags, int mipLevel, int depthSlice)
{
this.textureHandle = handle;
this.flags = flags;
this.mipLevel = mipLevel;
this.depthSlice = depthSlice;
}
public TextureAccess(in TextureAccess access, in TextureHandle handle)
{
this.textureHandle = handle;
this.flags = access.flags;
this.mipLevel = access.mipLevel;
this.depthSlice = access.depthSlice;
}
}
/// <summary>
/// Represents the origin of UV coordinates for a texture. It represents how Unity stores the content,
/// independent of the active graphics API. The UV coordinate (0,0) in the shader will either sample
/// the bottom left pixel of the image, or the top left pixel (flipped).
/// </summary>
public enum TextureUVOrigin
{
/// <summary>
/// The UV coordinate (0,0) in a shader will sample the BOTTOM left texel of the texture. This matched the OpenGL standard, which is also the Unity standard for textures.
/// To ensure this behavior, Unity will store the content for texture upside down (flipped) on modern graphics APIs.
/// </summary>
BottomLeft,
/// <summary>
/// The UV coordinate (0,0) in a shader will sample the TOP left texel of the texture. This matches the standard of modern graphics APIs (Vulkan, DX, Metal,...).
/// The actual backbuffer will have a TopLeft orientation when a modern graphics API is active.
/// </summary>
TopLeft
}
/// <summary>
/// Represents the origin of UV coordinates for a texture. It represents how Unity stores the content,
/// independent of the active graphics API. The UV coordinate (0,0) in the shader will either sample
/// the bottom left pixel of the image, or the top left pixel (flipped).
/// </summary>
internal enum TextureUVOriginSelection
{
/// <summary>
/// The UV coordinate (0,0) in a shader will sample the BOTTOM left texel of the texture. This matched the OpenGL standard, which is also the Unity standard for textures.
/// To ensure this behavior, Unity will store the content for texture upside down (flipped) on modern graphics APIs.
/// </summary>
BottomLeft,
/// <summary>
/// The UV coordinate (0,0) in a shader will sample the TOP left texel of the texture. This matches the standard of modern graphics APIs (Vulkan, DX, Metal,...).
/// The actual backbuffer will have a TopLeft orientation when a modern graphics API is active.
/// </summary>
TopLeft,
/// <summary>
/// The orientation has not been assigned yet.
/// </summary>
Unknown
}
/// <summary>
/// An abstract handle representing a texture resource as known by one particular record + execute of the render graph.
/// TextureHandles should not be used outside of the context of a render graph execution.
///
/// A render graph needs to do additional state tracking on texture resources (lifetime, how is it used,...) to enable
/// this all textures relevant to the render graph need to be make known to it. A texture handle specifies such a texture as
/// known to the render graph.
///
/// It is important to understand that a render graph texture handle does not necessarily represent an actual texture. For example
/// textures could be created the render graph that are only referenced by passes that are later culled when executing the graph.
/// Such textures would never be allocated as actual RenderTextures.
///
/// Texture handles are only relevant to one particular record+execute phase of the render graph. After execution all texture
/// handles are invalidated. The system will catch texture handles from a different execution of the render graph but still
/// users should be careful to avoid keeping texture handles around from other render graph executions.
///
/// Texture handles do not need to be disposed/freed (they are auto-invalidated at the end of graph execution). The RenderTextures they represent
/// are either freed by the render graph internally (when the handle was acquired through RenderGraph.CreateTexture) or explicitly managed by
/// some external system (when acquired through RenderGraph.ImportTexture).
///
/// </summary>
[DebuggerDisplay("Texture ({handle.index})")]
[MovedFrom(true, "UnityEngine.Experimental.Rendering.RenderGraphModule", "UnityEngine.Rendering.RenderGraphModule")]
public readonly struct TextureHandle : IEquatable<TextureHandle>
{
private static TextureHandle s_NullHandle = new TextureHandle();
/// <summary>
/// Returns a null texture handle
/// </summary>
/// <value>A null texture handle.</value>
public static TextureHandle nullHandle { get { return s_NullHandle; } }
internal readonly ResourceHandle handle;
private readonly bool builtin;
internal TextureHandle(in ResourceHandle h)
{
handle = h;
builtin = false;
}
internal TextureHandle(int handle, bool shared = false, bool builtin = false)
{
this.handle = new ResourceHandle(handle, RenderGraphResourceType.Texture, shared);
this.builtin = builtin;
}
/// <summary>
/// Cast to RenderTargetIdentifier
/// </summary>
/// <param name="texture">Input TextureHandle.</param>
/// <returns>Resource as a RenderTargetIdentifier.</returns>
public static implicit operator RenderTargetIdentifier(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : default(RenderTargetIdentifier);
/// <summary>
/// Cast to Texture
/// </summary>
/// <param name="texture">Input TextureHandle.</param>
/// <returns>Resource as a Texture.</returns>
public static implicit operator Texture(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
/// <summary>
/// Cast to RenderTexture
/// </summary>
/// <param name="texture">Input TextureHandle.</param>
/// <returns>Resource as a RenderTexture.</returns>
public static implicit operator RenderTexture(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
/// <summary>
/// Cast to RTHandle
/// </summary>
/// <param name="texture">Input TextureHandle.</param>
/// <returns>Resource as a RTHandle.</returns>
public static implicit operator RTHandle(TextureHandle texture) => texture.IsValid() ? RenderGraphResourceRegistry.current.GetTexture(texture) : null;
/// <summary>
/// Determines whether this instance and another specified <see cref="TextureHandle"/> object have the same underlying resource handle.
/// </summary>
/// <param name="other">The texture handle to compare with the current instance.</param>
/// <returns>
/// True if both texture handles reference the same underlying resource; otherwise, false.
/// </returns>
public bool Equals(TextureHandle other) => handle.Equals(other.handle);
/// <summary>
/// Determines whether the specified object is equal to the current <see cref="TextureHandle"/>.
/// </summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns>
/// True if the specified object is a <see cref="TextureHandle"/> and references the same underlying resource; otherwise, false.
/// </returns>
public override bool Equals(object obj) => obj is TextureHandle other && Equals(other);
/// <summary>
/// Returns the hash code for this <see cref="TextureHandle"/>.
/// </summary>
/// <returns>
/// The hash code of the current <see cref="TextureHandle"/>.
/// </returns>
public override int GetHashCode() => handle.GetHashCode();
/// <summary>
/// Determines whether two <see cref="TextureHandle"/> instances reference the same underlying resource.
/// </summary>
/// <param name="lhs">The first texture handle to compare.</param>
/// <param name="rhs">The second texture handle to compare.</param>
/// <returns>
/// True if both handles reference the same underlying resource; otherwise, false.
/// </returns>
public static bool operator ==(TextureHandle lhs, TextureHandle rhs) => lhs.handle.Equals(rhs.handle);
/// <summary>
/// Determines whether two <see cref="TextureHandle"/> instances reference different underlying resources.
/// </summary>
/// <param name="lhs">The first texture handle to compare.</param>
/// <param name="rhs">The second texture handle to compare.</param>
/// <returns>
/// True if the handles reference different underlying resources; otherwise, false.
/// </returns>
public static bool operator !=(TextureHandle lhs, TextureHandle rhs) => !lhs.handle.Equals(rhs.handle);
/// <summary>
/// Return true if the handle is valid.
/// </summary>
/// <returns>True if the handle is valid.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsValid() => handle.IsValid();
/// <summary>
/// Return true if the handle is a builtin handle managed by RenderGraph internally.
/// </summary>
/// <returns>True if the handle is a builtin handle.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool IsBuiltin() => this.builtin;
/// <summary>
/// Get the Descriptor of the texture. This simply calls RenderGraph.GetTextureDesc but is more easily discoverable through auto complete.
/// </summary>
/// <param name="renderGraph">The rendergraph instance that was used to create the texture on. Texture handles are a lightweight object, all information is stored on the RenderGraph itself.</param>
/// <returns>The texture descriptor for the given texture handle.</returns>
public TextureDesc GetDescriptor(RenderGraph renderGraph) { return renderGraph.GetTextureDesc(this); }
public int GetResourceId() { return handle.index; }
}
/// <summary>
/// The mode that determines the size of a Texture.
/// </summary>
public enum TextureSizeMode
{
///<summary>Explicit size.</summary>
Explicit,
///<summary>Size automatically scaled by a Vector.</summary>
Scale,
///<summary>Size automatically scaled by a Functor.</summary>
Functor
}
#if UNITY_2020_2_OR_NEWER
/// <summary>
/// Subset of the texture desc containing information for fast memory allocation (when platform supports it)
/// </summary>
public struct FastMemoryDesc
{
///<summary>Whether the texture will be in fast memory.</summary>
public bool inFastMemory;
///<summary>Flag to determine what parts of the render target is spilled if not fully resident in fast memory.</summary>
public FastMemoryFlags flags;
///<summary>How much of the render target is to be switched into fast memory (between 0 and 1).</summary>
public float residencyFraction;
}
#endif
/// <summary>
/// Descriptor used to create texture resources
/// </summary>
public struct TextureDesc
{
///<summary>Texture sizing mode.</summary>
public TextureSizeMode sizeMode;
///<summary>Texture width.</summary>
public int width;
///<summary>Texture height.</summary>
public int height;
///<summary>Number of texture slices.</summary>
public int slices;
///<summary>Texture scale.</summary>
public Vector2 scale;
///<summary>Texture scale function.</summary>
public ScaleFunc func;
///<summary>Color or depth stencil format.</summary>
public GraphicsFormat format;
///<summary>Filtering mode.</summary>
public FilterMode filterMode;
///<summary>Addressing mode.</summary>
public TextureWrapMode wrapMode;
///<summary>Texture dimension.</summary>
public TextureDimension dimension;
///<summary>Enable random UAV read/write on the texture.</summary>
public bool enableRandomWrite;
///<summary>Texture needs mip maps.</summary>
public bool useMipMap;
///<summary>Automatically generate mip maps.</summary>
public bool autoGenerateMips;
///<summary>Texture is a shadow map.</summary>
public bool isShadowMap;
///<summary>Anisotropic filtering level.</summary>
public int anisoLevel;
///<summary>Mip map bias.</summary>
public float mipMapBias;
///<summary>Number of MSAA samples.</summary>
public MSAASamples msaaSamples;
///<summary>Bind texture multi sampled.</summary>
public bool bindTextureMS;
///<summary>[See Dynamic Resolution documentation](https://docs.unity3d.com/Manual/DynamicResolution.html)</summary>
public bool useDynamicScale;
///<summary>[See Dynamic Resolution documentation](https://docs.unity3d.com/Manual/DynamicResolution.html)</summary>
public bool useDynamicScaleExplicit;
///<summary>
///[See Memoryless documentation](https://docs.unity3d.com/ScriptReference/RenderTextureMemoryless.html)
///</summary>
///<remarks>
///If this is a Render Graph created resource only used in a single raster render pass, and not sampled (no UseTexture() usage), Render Graph will automatically set this resource as memoryless.
///</remarks>
public RenderTextureMemoryless memoryless;
///<summary>Special treatment of the VR eye texture used in stereoscopic rendering.</summary>
public VRTextureUsage vrUsage;
/// <summary>
/// Set to true if the texture is to be used as a shading rate image.
/// </summary>
/// <remarks>
/// Width and height are usually in pixels but if enableShadingRate is set to true, width and height are in tiles.
/// See also <a href="https://docs.unity3d.com/Manual/variable-rate-shading">Variable Rate Shading</a>.
/// </remarks>
public bool enableShadingRate;
///<summary>Texture name.</summary>
public string name;
#if UNITY_2020_2_OR_NEWER
///<summary>Descriptor to determine how the texture will be in fast memory on platform that supports it.</summary>
public FastMemoryDesc fastMemoryDesc;
#endif
///<summary>Determines whether the texture will fallback to a black texture if it is read without ever writing to it.</summary>
public bool fallBackToBlackTexture;
///<summary>
///If all passes writing to a texture are culled by Dynamic Render Pass Culling, it will automatically fallback to a similar preallocated texture.
///Set this to true to force the allocation.
///</summary>
public bool disableFallBackToImportedTexture;
// Initial state. Those should not be used in the hash
///<summary>Texture needs to be cleared on first use.</summary>
public bool clearBuffer;
///<summary>Clear color.</summary>
public Color clearColor;
///<summary>Texture needs to be discarded on last use.</summary>
public bool discardBuffer;
///<summary>Depth buffer bit depth of the format. The setter convert the bits to valid depth stencil format and sets the format. The getter gets the depth bits of the format.</summary>
public DepthBits depthBufferBits
{
get { return (DepthBits)GraphicsFormatUtility.GetDepthBits(format); }
set
{
if (value == DepthBits.None)
{
if( !GraphicsFormatUtility.IsDepthStencilFormat(format) )
return;
else
format = GraphicsFormat.None;
}
else
{
format = GraphicsFormatUtility.GetDepthStencilFormat((int)value);
}
}
}
///<summary>Color format. Sets the format. The getter checks if format is a color format. Returns the format if a color format, otherwise returns GraphicsFormat.None.</summary>
public GraphicsFormat colorFormat
{
get { return GraphicsFormatUtility.IsDepthStencilFormat(format) ? GraphicsFormat.None : format; }
set { format = value; }
}
void InitDefaultValues(bool dynamicResolution, bool xrReady)
{
useDynamicScale = dynamicResolution;
vrUsage = VRTextureUsage.None;
// XR Ready
if (xrReady)
{
slices = TextureXR.slices;
dimension = TextureXR.dimension;
}
else
{
slices = 1;
dimension = TextureDimension.Tex2D;
}
discardBuffer = false;
}
/// <summary>
/// TextureDesc constructor for a texture using explicit size
/// </summary>
/// <param name="width">Texture width</param>
/// <param name="height">Texture height</param>
/// <param name="dynamicResolution">Use dynamic resolution</param>
/// <param name="xrReady">Set this to true if the Texture is a render texture in an XR setting.</param>
public TextureDesc(int width, int height, bool dynamicResolution = false, bool xrReady = false)
: this()
{
// Size related init
sizeMode = TextureSizeMode.Explicit;
this.width = width;
this.height = height;
// Important default values not handled by zero construction in this()
msaaSamples = MSAASamples.None;
InitDefaultValues(dynamicResolution, xrReady);
}
/// <summary>
/// TextureDesc constructor for a texture using a fixed scaling
/// </summary>
/// <param name="scale">RTHandle scale used for this texture</param>
/// <param name="dynamicResolution">Use dynamic resolution</param>
/// <param name="xrReady">Set this to true if the Texture is a render texture in an XR setting.</param>
public TextureDesc(Vector2 scale, bool dynamicResolution = false, bool xrReady = false)
: this()
{
// Size related init
sizeMode = TextureSizeMode.Scale;
this.scale = scale;
// Important default values not handled by zero construction in this()
msaaSamples = MSAASamples.None;
dimension = TextureDimension.Tex2D;
InitDefaultValues(dynamicResolution, xrReady);
}
/// <summary>
/// TextureDesc constructor for a texture using a functor for scaling
/// </summary>
/// <param name="func">Function used to determine the texture size</param>
/// <param name="dynamicResolution">Use dynamic resolution</param>
/// <param name="xrReady">Set this to true if the Texture is a render texture in an XR setting.</param>
public TextureDesc(ScaleFunc func, bool dynamicResolution = false, bool xrReady = false)
: this()
{
// Size related init
sizeMode = TextureSizeMode.Functor;
this.func = func;
// Important default values not handled by zero construction in this()
msaaSamples = MSAASamples.None;
dimension = TextureDimension.Tex2D;
InitDefaultValues(dynamicResolution, xrReady);
}
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="input">The TextureDesc instance to copy from.</param>
public TextureDesc(TextureDesc input)
{
this = input;
}
/// <summary>
/// Do a best effort conversion from a RenderTextureDescriptor to a TextureDesc. This tries to initialize a descriptor to be as close as possible to the given render texture descriptor but there might be subtle differences when creating
/// render graph textures using this TextureDesc due to the underlying RTHandle system.
/// Some parameters of the TextureDesc (like name and filtering modes) are not present in the RenderTextureDescriptor for these the returned TextureDesc will contain plausible default values.
/// </summary>
/// <param name="input">The texture descriptor to create a TextureDesc from</param>
public TextureDesc(RenderTextureDescriptor input)
{
sizeMode = TextureSizeMode.Explicit;
width = input.width;
height = input.height;
slices = input.volumeDepth;
scale = Vector2.one;
func = null;
format = (input.depthStencilFormat != GraphicsFormat.None) ? input.depthStencilFormat : input.graphicsFormat;
filterMode = FilterMode.Bilinear;
wrapMode = TextureWrapMode.Clamp;
dimension = input.dimension;
enableRandomWrite = input.enableRandomWrite;
useMipMap = input.useMipMap;
autoGenerateMips = input.autoGenerateMips;
isShadowMap = (input.shadowSamplingMode != ShadowSamplingMode.None);
anisoLevel = 1;
mipMapBias = 0;
msaaSamples = (MSAASamples)input.msaaSamples;
bindTextureMS = input.bindMS;
useDynamicScale = input.useDynamicScale;
useDynamicScaleExplicit = false;
memoryless = input.memoryless;
vrUsage = input.vrUsage;
name = "UnNamedFromRenderTextureDescriptor";
fastMemoryDesc = new FastMemoryDesc();
fastMemoryDesc.inFastMemory = false;
fallBackToBlackTexture = false;
disableFallBackToImportedTexture = true;
clearBuffer = true;
clearColor = Color.black;
discardBuffer = false;
enableShadingRate = input.enableShadingRate;
}
/// <summary>
/// Do a best effort conversion from a RenderTexture to a TextureDesc. This tries to initialize a descriptor to be as close as possible to the given render texture but there might be subtle differences when creating
/// render graph textures using this TextureDesc due to the underlying RTHandle system.
/// </summary>
/// <param name="input">The texture to create a TextureDesc from</param>
public TextureDesc(RenderTexture input) : this(input.descriptor)
{
filterMode = input.filterMode;
wrapMode = input.wrapMode;
anisoLevel = input.anisoLevel;
mipMapBias = input.mipMapBias;
name = "UnNamedFromRenderTextureDescriptor";
}
/// <summary>
/// Hash function
/// </summary>
/// <returns>The texture descriptor hash.</returns>
public override int GetHashCode()
{
var hashCode = HashFNV1A32.Create();
switch (sizeMode)
{
case TextureSizeMode.Explicit:
hashCode.Append(width);
hashCode.Append(height);
break;
case TextureSizeMode.Functor:
if (func != null)
hashCode.Append(DelegateHashCodeUtils.GetFuncHashCode(func));
break;
case TextureSizeMode.Scale:
hashCode.Append(scale);
break;
}
hashCode.Append(mipMapBias);
hashCode.Append(slices);
hashCode.Append((int) format);
hashCode.Append((int) filterMode);
hashCode.Append((int) wrapMode);
hashCode.Append((int) dimension);
hashCode.Append((int) memoryless);
hashCode.Append((int) vrUsage);
hashCode.Append(anisoLevel);
hashCode.Append(enableRandomWrite);
hashCode.Append(useMipMap);
hashCode.Append(autoGenerateMips);
hashCode.Append(isShadowMap);
hashCode.Append(bindTextureMS);
hashCode.Append(useDynamicScale);
hashCode.Append((int) msaaSamples);
#if UNITY_2020_2_OR_NEWER
hashCode.Append(fastMemoryDesc.inFastMemory);
#endif
hashCode.Append(enableShadingRate);
return hashCode.value;
}
/// <summary>
/// Calculate the final size of the texture descriptor in pixels. This takes into account the sizeMode set for this descriptor.
/// For the automatically scaled sizes the size will be relative to the RTHandle reference size <see cref="RTHandles.SetReferenceSize">SetReferenceSize</see>.
/// </summary>
/// <returns>The calculated size.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the texture descriptor's size mode falls outside the expected range.</exception>
public Vector2Int CalculateFinalDimensions()
{
return sizeMode switch
{
TextureSizeMode.Explicit => new Vector2Int(width, height),
TextureSizeMode.Scale => RTHandles.CalculateDimensions(scale),
TextureSizeMode.Functor => RTHandles.CalculateDimensions(func),
_ => throw new ArgumentOutOfRangeException()
};
}
}
[DebuggerDisplay("TextureResource ({desc.name})")]
class TextureResource : RenderGraphResource<TextureDesc, RTHandle>
{
static int m_TextureCreationIndex;
internal TextureUVOriginSelection textureUVOrigin;
public override string GetName()
{
if (imported && !shared)
return graphicsResource != null ? graphicsResource.name : "null resource";
else
return desc.name;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetDescHashCode() { return desc.GetHashCode(); }
public override void CreateGraphicsResource()
{
var name = GetName();
// Textures are going to be reused under different aliases along the frame so we can't provide a specific name upon creation.
// The name in the desc is going to be used for debugging purpose and render graph visualization.
if (name == "")
name = $"RenderGraphTexture_{m_TextureCreationIndex++}";
RTHandleAllocInfo rtAllocInfo = new RTHandleAllocInfo(name)
{
slices = desc.slices,
format = desc.format,
filterMode = desc.filterMode,
wrapModeU = desc.wrapMode,
wrapModeV = desc.wrapMode,
wrapModeW = desc.wrapMode,
dimension = desc.dimension,
enableRandomWrite = desc.enableRandomWrite,
useMipMap = desc.useMipMap,
autoGenerateMips = desc.autoGenerateMips,
anisoLevel = desc.anisoLevel,
mipMapBias = desc.mipMapBias,
isShadowMap = desc.isShadowMap,
msaaSamples = (MSAASamples)desc.msaaSamples,
bindTextureMS = desc.bindTextureMS,
useDynamicScale = desc.useDynamicScale,
useDynamicScaleExplicit = desc.useDynamicScaleExplicit,
memoryless = desc.memoryless,
vrUsage = desc.vrUsage,
enableShadingRate = desc.enableShadingRate,
};
switch (desc.sizeMode)
{
case TextureSizeMode.Explicit:
graphicsResource = RTHandles.Alloc(desc.width, desc.height, rtAllocInfo);
break;
case TextureSizeMode.Scale:
graphicsResource = RTHandles.Alloc(desc.scale, rtAllocInfo);
break;
case TextureSizeMode.Functor:
graphicsResource = RTHandles.Alloc(desc.func, rtAllocInfo);
break;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void UpdateGraphicsResource()
{
if (graphicsResource != null)
graphicsResource.m_Name = GetName();
}
public override void ReleaseGraphicsResource()
{
if (graphicsResource != null)
graphicsResource.Release();
base.ReleaseGraphicsResource();
}
public override void LogCreation(RenderGraphLogger logger)
{
logger.LogLine($"Created Texture: {desc.name} (Cleared: {desc.clearBuffer})");
}
public override void LogRelease(RenderGraphLogger logger)
{
logger.LogLine($"Released Texture: {desc.name}");
}
}
class TexturePool : RenderGraphResourcePool<RTHandle>
{
protected override void ReleaseInternalResource(RTHandle res)
{
res.Release();
}
protected override string GetResourceName(in RTHandle res)
{
return res.rt.name;
}
protected override long GetResourceSize(in RTHandle res)
{
return Profiling.Profiler.GetRuntimeMemorySizeLong(res.rt);
}
override protected string GetResourceTypeName()
{
return "Texture";
}
override protected int GetSortIndex(RTHandle res)
{
return res.GetInstanceID();
}
}
}