Skip to content

Commit 989cf70

Browse files
[URP] Port HDRP Mip Bias Logic to URP (#6229)
* Port HDRP Mip Bias Logic to URP This change integrates the mip bias logic from HDRP into URP. This logic ensures that shaders select their mips based on the final screen resolution rather than the current render resolution. In cases where aggressive upscaling is taking place, this can significantly improve texture detail in the final image. * Combined Mip Bias Constants Into Vector This change combines the mip bias shader constants into a single vector rather than having two separate scalar values. This may improve performance on older mobile devices and shouldn't hurt performance on modern ones.
1 parent 9c9b7ab commit 989cf70

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ void SetPerCameraShaderVariables(CommandBuffer cmd, ref CameraData cameraData)
217217

218218
cmd.SetGlobalVector(ShaderPropertyId.screenSize, new Vector4(cameraWidth, cameraHeight, 1.0f / cameraWidth, 1.0f / cameraHeight));
219219

220+
// Calculate a bias value which corrects the mip lod selection logic when image scaling is active.
221+
// We clamp this value to 0.0 or less to make sure we don't end up reducing image detail in the downsampling case.
222+
float mipBias = Math.Min((float)-Math.Log(cameraWidth / scaledCameraWidth, 2.0f), 0.0f);
223+
cmd.SetGlobalVector(ShaderPropertyId.globalMipBias, new Vector2(mipBias, Mathf.Pow(2.0f, mipBias)));
224+
220225
//Set per camera matrices.
221226
SetCameraMatrices(cmd, ref cameraData, true);
222227
}

com.unity.render-pipelines.universal/Runtime/UniversalRenderPipelineCore.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ internal static class ShaderPropertyId
339339
public static readonly int projectionParams = Shader.PropertyToID("_ProjectionParams");
340340
public static readonly int zBufferParams = Shader.PropertyToID("_ZBufferParams");
341341
public static readonly int orthoParams = Shader.PropertyToID("unity_OrthoParams");
342+
public static readonly int globalMipBias = Shader.PropertyToID("_GlobalMipBias");
342343

343344
public static readonly int screenSize = Shader.PropertyToID("_ScreenSize");
344345

com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,77 @@
103103
#define GATHER_BLUE_TEXTURE2D_X(textureName, samplerName, coord2) GATHER_BLUE_TEXTURE2D(textureName, samplerName, coord2)
104104
#endif
105105

106+
///
107+
/// Texture Sampling Macro Overrides for Scaling
108+
///
109+
/// When mip bias is supported by the underlying platform, the following section redefines all 2d texturing operations to support a global mip bias feature.
110+
/// This feature is used to improve rendering quality when image scaling is active. It achieves this by adding a bias value to the standard mip lod calculation
111+
/// which allows us to select the mip level based on the final image resolution rather than the current rendering resolution.
112+
113+
#ifdef PLATFORM_SAMPLE_TEXTURE2D_BIAS
114+
#ifdef SAMPLE_TEXTURE2D
115+
#undef SAMPLE_TEXTURE2D
116+
#define SAMPLE_TEXTURE2D(textureName, samplerName, coord2) \
117+
PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, _GlobalMipBias.x)
118+
#endif
119+
#ifdef SAMPLE_TEXTURE2D_BIAS
120+
#undef SAMPLE_TEXTURE2D_BIAS
121+
#define SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, bias) \
122+
PLATFORM_SAMPLE_TEXTURE2D_BIAS(textureName, samplerName, coord2, (bias + _GlobalMipBias.x))
123+
#endif
124+
#endif
125+
126+
#ifdef PLATFORM_SAMPLE_TEXTURE2D_GRAD
127+
#ifdef SAMPLE_TEXTURE2D_GRAD
128+
#undef SAMPLE_TEXTURE2D_GRAD
129+
#define SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, dpdx, dpdy) \
130+
PLATFORM_SAMPLE_TEXTURE2D_GRAD(textureName, samplerName, coord2, (dpdx * _GlobalMipBias.y), (dpdy * _GlobalMipBias.y))
131+
#endif
132+
#endif
133+
134+
#ifdef PLATFORM_SAMPLE_TEXTURE2D_ARRAY_BIAS
135+
#ifdef SAMPLE_TEXTURE2D_ARRAY
136+
#undef SAMPLE_TEXTURE2D_ARRAY
137+
#define SAMPLE_TEXTURE2D_ARRAY(textureName, samplerName, coord2, index) \
138+
PLATFORM_SAMPLE_TEXTURE2D_ARRAY_BIAS(textureName, samplerName, coord2, index, _GlobalMipBias.x)
139+
#endif
140+
#ifdef SAMPLE_TEXTURE2D_ARRAY_BIAS
141+
#undef SAMPLE_TEXTURE2D_ARRAY_BIAS
142+
#define SAMPLE_TEXTURE2D_ARRAY_BIAS(textureName, samplerName, coord2, index, bias) \
143+
PLATFORM_SAMPLE_TEXTURE2D_ARRAY_BIAS(textureName, samplerName, coord2, index, (bias + _GlobalMipBias.x))
144+
#endif
145+
#endif
146+
147+
#ifdef PLATFORM_SAMPLE_TEXTURECUBE_BIAS
148+
#ifdef SAMPLE_TEXTURECUBE
149+
#undef SAMPLE_TEXTURECUBE
150+
#define SAMPLE_TEXTURECUBE(textureName, samplerName, coord3) \
151+
PLATFORM_SAMPLE_TEXTURECUBE_BIAS(textureName, samplerName, coord3, _GlobalMipBias.x)
152+
#endif
153+
#ifdef SAMPLE_TEXTURECUBE_BIAS
154+
#undef SAMPLE_TEXTURECUBE_BIAS
155+
#define SAMPLE_TEXTURECUBE_BIAS(textureName, samplerName, coord3, bias) \
156+
PLATFORM_SAMPLE_TEXTURECUBE_BIAS(textureName, samplerName, coord3, (bias + _GlobalMipBias.x))
157+
#endif
158+
#endif
159+
160+
#ifdef PLATFORM_SAMPLE_TEXTURECUBE_ARRAY_BIAS
161+
162+
#ifdef SAMPLE_TEXTURECUBE_ARRAY
163+
#undef SAMPLE_TEXTURECUBE_ARRAY
164+
#define SAMPLE_TEXTURECUBE_ARRAY(textureName, samplerName, coord3, index)\
165+
PLATFORM_SAMPLE_TEXTURECUBE_ARRAY_BIAS(textureName, samplerName, coord3, index, _GlobalMipBias.x)
166+
#endif
167+
168+
#ifdef SAMPLE_TEXTURECUBE_ARRAY_BIAS
169+
#undef SAMPLE_TEXTURECUBE_ARRAY_BIAS
170+
#define SAMPLE_TEXTURECUBE_ARRAY_BIAS(textureName, samplerName, coord3, index, bias)\
171+
PLATFORM_SAMPLE_TEXTURECUBE_ARRAY_BIAS(textureName, samplerName, coord3, index, (bias + _GlobalMipBias.x))
172+
#endif
173+
#endif
174+
175+
#define VT_GLOBAL_MIP_BIAS_MULTIPLIER (_GlobalMipBias.y)
176+
106177
// Structs
107178
struct VertexPositionInputs
108179
{

com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ float4 _ProjectionParams;
5959
// w = 1 + 1.0/height
6060
float4 _ScreenParams;
6161

62+
// x = Mip Bias
63+
// y = 2.0 ^ [Mip Bias]
64+
float2 _GlobalMipBias;
65+
6266
// Values used to linearize the Z buffer (http://www.humus.name/temp/Linearize%20depth.txt)
6367
// x = 1-far/near
6468
// y = far/near

0 commit comments

Comments
 (0)