-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVrs.cs
More file actions
397 lines (345 loc) · 19.6 KB
/
Copy pathVrs.cs
File metadata and controls
397 lines (345 loc) · 19.6 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
using System;
using UnityEngine.Rendering.RenderGraphModule;
namespace UnityEngine.Rendering
{
/// <summary>
/// Encapsulates variable shading rate support (VRS) and texture conversion to shading rate image
/// </summary>
public static class Vrs
{
class ConversionPassData
{
public TextureHandle sriTextureHandle;
public TextureHandle mainTexHandle;
public TextureDimension mainTexDimension;
public BufferHandle mainTexLutHandle;
public BufferHandle validatedShadingRateFragmentSizeHandle;
public ComputeShader computeShader;
public int kernelIndex;
public Vector4 scaleBias;
public Vector2Int dispatchSize;
public bool yFlip;
}
class VisualizationPassData
{
public Material material;
public TextureHandle source;
public BufferHandle lut;
public TextureHandle dummy;
public Vector4 visualizationParams;
}
internal static readonly int shadingRateFragmentSizeCount = Enum.GetNames(typeof(ShadingRateFragmentSize)).Length;
static VrsResources s_VrsResources;
/// <summary>
/// Check if conversion of color texture to shading rate image is supported.
/// Convenience to abstract all capabilities checks.
///
/// The `IsColorMaskTextureConversionSupported` method checks for the following:
///- VRS hardware support through [ShadingRateInfo.supportsPerImageTile](xref:UnityEngine.Rendering.ShadingRateInfo.supportsPerImageTile). The `supportsPerImageTile` property determines whether your GPU can define different quality levels to different tiles.
///- Compute shader support through [SystemInfo.supportsComputeShaders](xref:UnityEngine.Device.SystemInfo.supportsComputeShaders)
///- Proper initialization of VRS utility functions and compute shaders required for converting color textures to shading rate image (SRI). This is automatically handled by the render pipeline. However, for custom implementations, you must call <see cref="InitializeResources"/> manually.
/// </summary>
/// <returns>Returns true if conversion of color texture to shading rate image is supported, false otherwise.</returns>
public static bool IsColorMaskTextureConversionSupported()
{
return SystemInfo.supportsComputeShaders &&
ShadingRateInfo.supportsPerImageTile &&
IsInitialized();
}
/// <summary>
/// Checks if VRS resources are initialized.
/// Initialization may fail due to platform restrictions.
/// </summary>
/// <returns>Returns true if the Vrs resources are initialized.</returns>
public static bool IsInitialized()
{
return s_VrsResources != null &&
s_VrsResources.textureComputeShader != null &&
s_VrsResources.textureReduceKernel != -1 &&
s_VrsResources.textureCopyKernel != -1;
}
/// <summary>
/// Preprocess resources found in VrsRenderPipelineRuntimeResources for use at runtime.
/// </summary>
public static void InitializeResources()
{
// GLES3.0/WebGL2 and older GL platforms do not support compute shaders (for conversion).
// Unity does not implement VRS for OpenGL.
bool isOpenGL = SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLCore ||
SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLES3;
// NOTE: should match "#pragma exclude_renderers" in shaders.
bool isPlatformSupported = !isOpenGL;
// VRS resources are initialized even on platforms that do not support VRS to allow debugging Color<->VRS conversion.
// For example when you are building on a non-VRS platform to a VRS platform.
// VRS conversion requires compute shader support.
// NOTE: Init might fail.
if (SystemInfo.supportsComputeShaders &&
isPlatformSupported)
{
var pipelineRuntimeResources = GraphicsSettings.GetRenderPipelineSettings<VrsRenderPipelineRuntimeResources>();
s_VrsResources = new VrsResources(pipelineRuntimeResources);
}
}
/// <summary>
/// Cleanup resources.
/// </summary>
public static void DisposeResources()
{
s_VrsResources?.Dispose();
s_VrsResources = null;
}
/// <summary>
/// Converts a color mask texture to a shading rate image.
/// </summary>
/// <param name="renderGraph">Render graph to record conversion commands</param>
/// <param name="sriRtHandle">Shading rate images to convert to.</param>
/// <param name="colorMaskRtHandle">Texture to convert from.</param>
/// <param name="yFlip">True if shading rate image should be generated flipped.</param>
/// <returns>Shading rate image texture handle created.</returns>
/// <remarks>
/// sriRtHandle and colorMaskRtHandle are imported with renderGraph before doing the conversion.
/// </remarks>
public static TextureHandle ColorMaskTextureToShadingRateImage(RenderGraph renderGraph, RTHandle sriRtHandle, RTHandle colorMaskRtHandle, bool yFlip)
{
if (renderGraph == null || sriRtHandle == null || colorMaskRtHandle == null)
{
Debug.LogError($"TextureToShadingRateImage: invalid argument.");
return TextureHandle.nullHandle;
}
var sriTextureHandle = renderGraph.ImportShadingRateImageTexture(sriRtHandle);
var colorMaskHandle = renderGraph.ImportTexture(colorMaskRtHandle);
return ColorMaskTextureToShadingRateImage(renderGraph,
sriTextureHandle,
colorMaskHandle,
((Texture)colorMaskRtHandle).dimension,
yFlip);
}
/// <summary>
/// Converts a color mask texture to a shading rate image.
/// </summary>
/// <param name="renderGraph">Render graph to record conversion commands</param>
/// <param name="sriTextureHandle">Shading rate images to convert to.</param>
/// <param name="colorMaskHandle">Texture to convert from.</param>
/// <param name="colorMaskDimension">Texture's dimension.</param>
/// <param name="yFlip">True if shading rate image should be generated flipped.</param>
/// <returns>Shading rate image texture handle created.</returns>
/// <remarks>
/// sriRtHandle and colorMaskHandle are expected to be imported by renderGraph prior to this call.
/// </remarks>
public static TextureHandle ColorMaskTextureToShadingRateImage(RenderGraph renderGraph, TextureHandle sriTextureHandle, TextureHandle colorMaskHandle, TextureDimension colorMaskDimension, bool yFlip)
{
if (!IsColorMaskTextureConversionSupported())
{
Debug.LogError($"ColorMaskTextureToShadingRateImage: conversion not supported.");
return TextureHandle.nullHandle;
}
var sriDesc = sriTextureHandle.GetDescriptor(renderGraph);
if (sriDesc.dimension != TextureDimension.Tex2D)
{
Debug.LogError($"ColorMaskTextureToShadingRateImage: Vrs image not a texture 2D.");
return TextureHandle.nullHandle;
}
if (colorMaskDimension != TextureDimension.Tex2D && colorMaskDimension != TextureDimension.Tex2DArray)
{
Debug.LogError($"ColorMaskTextureToShadingRateImage: Input texture dimension not supported.");
return TextureHandle.nullHandle;
}
using (var builder = renderGraph.AddComputePass<ConversionPassData>("TextureToShadingRateImage", out var outerPassData, s_VrsResources.conversionProfilingSampler))
{
outerPassData.sriTextureHandle = sriTextureHandle;
outerPassData.mainTexHandle = colorMaskHandle;
outerPassData.mainTexDimension = colorMaskDimension;
outerPassData.mainTexLutHandle = renderGraph.ImportBuffer(s_VrsResources.conversionLutBuffer);
outerPassData.validatedShadingRateFragmentSizeHandle = renderGraph.ImportBuffer(s_VrsResources.validatedShadingRateFragmentSizeBuffer);
outerPassData.computeShader = s_VrsResources.textureComputeShader;
outerPassData.kernelIndex = s_VrsResources.textureReduceKernel;
outerPassData.scaleBias = new Vector4()
{
x = 1.0f / (sriDesc.width * s_VrsResources.tileSize.x),
y = 1.0f / (sriDesc.height * s_VrsResources.tileSize.y),
z = sriDesc.width,
w = sriDesc.height,
};
outerPassData.dispatchSize = new Vector2Int(sriDesc.width, sriDesc.height);
outerPassData.yFlip = yFlip;
builder.UseTexture(outerPassData.sriTextureHandle, AccessFlags.Write);
builder.UseTexture(outerPassData.mainTexHandle);
builder.UseBuffer(outerPassData.mainTexLutHandle);
builder.AllowGlobalStateModification(true);
builder.SetRenderFunc(static (ConversionPassData innerPassData, ComputeGraphContext context) =>
{
ConversionDispatch(context.cmd, innerPassData);
});
return outerPassData.sriTextureHandle;
}
}
/// <summary>
/// Converts a shading rate image to a color texture for visualization.
/// </summary>
/// <param name="renderGraph">Render graph to record conversion commands</param>
/// <param name="sriTextureHandle">Texture to convert from.</param>
/// <param name="colorMaskHandle">Output of conversion.</param>
public static void ShadingRateImageToColorMaskTexture(RenderGraph renderGraph, in TextureHandle sriTextureHandle, in TextureHandle colorMaskHandle)
{
if (s_VrsResources == null)
{
Debug.LogError($"ShadingRateImageToColorMaskTexture: VRS not initialized.");
return;
}
if (!colorMaskHandle.IsValid())
{
Debug.LogError($"ShadingRateImageToColorMaskTexture: Output target handle is not valid.");
return;
}
using (var builder = renderGraph.AddRasterRenderPass<VisualizationPassData>("ShadingRateImageToTexture", out var outerPassData, s_VrsResources.visualizationProfilingSampler))
{
outerPassData.material = s_VrsResources.visualizationMaterial;
if (sriTextureHandle.IsValid())
outerPassData.source = sriTextureHandle;
else
outerPassData.source = renderGraph.defaultResources.blackTexture;
outerPassData.lut = renderGraph.ImportBuffer(s_VrsResources.visualizationLutBuffer);
outerPassData.dummy = renderGraph.defaultResources.blackTexture;
outerPassData.visualizationParams = new Vector4(
1.0f / s_VrsResources.tileSize.x,
1.0f / s_VrsResources.tileSize.y,
0,
0);;
builder.UseTexture(outerPassData.source);
builder.UseBuffer(outerPassData.lut);
builder.UseTexture(outerPassData.dummy);
builder.SetRenderAttachment(colorMaskHandle, 0);
builder.AllowPassCulling(false);
builder.SetRenderFunc(static (VisualizationPassData innerPassData, RasterGraphContext context) =>
{
// must setup blitter source via the material: it's a typed texture (uint)
innerPassData.material.SetTexture(VrsShaders.s_ShadingRateImage, innerPassData.source);
innerPassData.material.SetBuffer(VrsShaders.s_VisualizationLut, innerPassData.lut);
innerPassData.material.SetVector(VrsShaders.s_VisualizationParams, innerPassData.visualizationParams);
Blitter.BlitTexture(context.cmd,
innerPassData.dummy,
new Vector4(1, 1, 0, 0),
innerPassData.material,
0);
});
}
}
static void ConversionDispatch(ComputeCommandBuffer cmd, ConversionPassData conversionPassData)
{
var disableTexture2dXArray = new LocalKeyword(conversionPassData.computeShader, VrsShaders.k_DisableTexture2dXArray);
if (conversionPassData.mainTexDimension == TextureDimension.Tex2DArray)
cmd.DisableKeyword(conversionPassData.computeShader, disableTexture2dXArray);
else
cmd.EnableKeyword(conversionPassData.computeShader, disableTexture2dXArray);
var yFlip = new LocalKeyword(conversionPassData.computeShader, VrsShaders.k_YFlip);
if (conversionPassData.yFlip)
cmd.EnableKeyword(conversionPassData.computeShader, yFlip);
else
cmd.DisableKeyword(conversionPassData.computeShader, yFlip);
cmd.SetComputeTextureParam(conversionPassData.computeShader, conversionPassData.kernelIndex, VrsShaders.s_MainTex, conversionPassData.mainTexHandle);
cmd.SetComputeBufferParam(conversionPassData.computeShader, conversionPassData.kernelIndex, VrsShaders.s_MainTexLut, conversionPassData.mainTexLutHandle);
cmd.SetComputeBufferParam(conversionPassData.computeShader, conversionPassData.kernelIndex, VrsShaders.s_ShadingRateNativeValues, conversionPassData.validatedShadingRateFragmentSizeHandle);
cmd.SetComputeTextureParam(conversionPassData.computeShader, conversionPassData.kernelIndex, VrsShaders.s_ShadingRateImage, conversionPassData.sriTextureHandle);
cmd.SetComputeVectorParam(conversionPassData.computeShader, VrsShaders.s_ScaleBias, conversionPassData.scaleBias);
cmd.DispatchCompute(conversionPassData.computeShader, conversionPassData.kernelIndex, conversionPassData.dispatchSize.x, conversionPassData.dispatchSize.y, 1);
}
/// <summary>
/// Converts a color mask texture to a shading rate image.
/// Use this function to perform the conversion without the RenderGraph.
/// </summary>
/// <param name="cmd">CommandBuffer used for the compute dispatch.</param>
/// <param name="sriDestination">Shading rate images to convert to.</param>
/// <param name="colorMaskSource">Texture to convert from.</param>
/// <param name="yFlip">True if shading rate image should be generated flipped.</param>
public static void ColorMaskTextureToShadingRateImageDispatch(CommandBuffer cmd, RTHandle sriDestination, Texture colorMaskSource, bool yFlip = true)
{
if (sriDestination == null)
{
Debug.LogError("ColorMaskTextureToShadingRateImageDispatch: VRS destination shading rate texture is null.");
return;
}
if (colorMaskSource == null)
{
Debug.LogError("ColorMaskTextureToShadingRateImageDispatch: VRS source color texture is null.");
return;
}
if (!IsInitialized())
{
Debug.LogError("ColorMaskTextureToShadingRateImageDispatch: VRS is not initialized.");
return;
}
var computeShader = s_VrsResources.textureComputeShader;
var kernelIndex = s_VrsResources.textureReduceKernel;
var colorLutBuffer = s_VrsResources.conversionLutBuffer;
var validatedShadingRateFragmentSizeBuffer = s_VrsResources.validatedShadingRateFragmentSizeBuffer;
int sriDestWidth = sriDestination.rt.width;
int sriDestHeight = sriDestination.rt.height;
var scaleBias = new Vector4()
{
x = 1.0f / (sriDestWidth * s_VrsResources.tileSize.x),
y = 1.0f / (sriDestHeight * s_VrsResources.tileSize.y),
z = sriDestWidth,
w = sriDestHeight,
};
var dispatchSize = new Vector2Int(sriDestWidth, sriDestHeight);
var disableTexture2dXArray = new LocalKeyword(computeShader, VrsShaders.k_DisableTexture2dXArray);
if (colorMaskSource?.dimension == TextureDimension.Tex2DArray)
cmd.DisableKeyword(computeShader, disableTexture2dXArray);
else
cmd.EnableKeyword(computeShader, disableTexture2dXArray);
var yFlipKeyword = new LocalKeyword(computeShader, VrsShaders.k_YFlip);
if (yFlip)
cmd.EnableKeyword(computeShader, yFlipKeyword);
else
cmd.DisableKeyword(computeShader, yFlipKeyword);
cmd.SetComputeTextureParam(computeShader, kernelIndex, VrsShaders.s_MainTex, colorMaskSource);
cmd.SetComputeBufferParam(computeShader, kernelIndex, VrsShaders.s_MainTexLut, colorLutBuffer);
cmd.SetComputeBufferParam(computeShader, kernelIndex, VrsShaders.s_ShadingRateNativeValues, validatedShadingRateFragmentSizeBuffer);
cmd.SetComputeTextureParam(computeShader, kernelIndex, VrsShaders.s_ShadingRateImage, sriDestination);
cmd.SetComputeVectorParam(computeShader, VrsShaders.s_ScaleBias, scaleBias);
cmd.DispatchCompute(computeShader, kernelIndex, dispatchSize.x, dispatchSize.y, 1);
}
/// <summary>
/// Converts a shading rate image to a color mask texture.
/// Use this function to perform the conversion without the RenderGraph.
/// </summary>
/// <param name="cmd">CommandBuffer used for the compute dispatch.</param>
/// <param name="sriSource">Shading rate images to convert from.</param>
/// <param name="colorMaskDestination">Texture to convert to.</param>
public static void ShadingRateImageToColorMaskTextureBlit(CommandBuffer cmd, RTHandle sriSource, RTHandle colorMaskDestination)
{
if (sriSource == null)
{
Debug.LogError("ShadingRateImageToColorMaskTextureBlit: VRS source shading rate texture is null.");
return;
}
if (colorMaskDestination == null)
{
Debug.LogError("ShadingRateImageToColorMaskTextureBlit: VRS destination color texture is null.");
return;
}
if(!IsInitialized())
{
Debug.LogError("ShadingRateImageToColorMaskTextureBlit: VRS is not initialized.");
return;
}
RTHandle source = sriSource;
RTHandle destination = colorMaskDestination;
var material = s_VrsResources.visualizationMaterial;
var lut = s_VrsResources.visualizationLutBuffer;
Vector4 visualizationParams = new Vector4(
1.0f / s_VrsResources.tileSize.x,
1.0f / s_VrsResources.tileSize.y,
0,
0);
material.SetTexture(VrsShaders.s_ShadingRateImage, source);
material.SetBuffer(VrsShaders.s_VisualizationLut, lut);
material.SetVector(VrsShaders.s_VisualizationParams, visualizationParams);
CoreUtils.SetRenderTarget(cmd, destination);
Blitter.BlitTexture(cmd,
new Vector4(1, 1, 0, 0),
material,
0);
}
}
}