浏览代码

Added mip debugging support

(Partially disabled pending c++ support for debug data upload.)
/feature-ReflectionProbeFit
lyndon homewood 7 年前
当前提交
430ca284
共有 9 个文件被更改,包括 202 次插入7 次删除
  1. 15
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs
  2. 36
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/MaterialDebug.cs
  3. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/MaterialDebug.cs.hlsl
  4. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  5. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LitProperties.hlsl
  6. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Unlit/UnlitProperties.hlsl
  7. 126
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/FragInputs.hlsl
  8. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassForward.hlsl
  9. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassForwardUnlit.hlsl

15
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using System;

lightingDebugSettings.debugLightingMode = DebugLightingMode.None;
materialDebugSettings.SetDebugViewProperties(value);
}
public void SetDebugViewTexture(Attributes.DebugViewTexture value)
{
if (value != 0)
lightingDebugSettings.debugLightingMode = DebugLightingMode.None;
materialDebugSettings.SetDebugViewTexture(value);
}
public void SetDebugViewGBuffer(int value)
{

lightingDebugSettings.debugLightingMode = value;
}
public void UpdateMaterials()
{
//if (materialDebugSettings.debugViewTexture != 0)
// Texture.SetStreamingTextureMaterialDebugProperties();
}
public void RegisterDebug()
{
DebugMenuManager.instance.AddDebugItem<float>("Display Stats", "Frame Rate", () => 1.0f / Time.smoothDeltaTime, null, DebugItemFlag.DynamicDisplay);

DebugMenuManager.instance.AddDebugItem<int>("Material", "Engine",() => materialDebugSettings.debugViewEngine, (value) => SetDebugViewEngine((int)value), DebugItemFlag.None, new DebugItemHandlerIntEnum(MaterialDebugSettings.debugViewEngineStrings, MaterialDebugSettings.debugViewEngineValues));
DebugMenuManager.instance.AddDebugItem<Attributes.DebugViewVarying>("Material", "Attributes",() => materialDebugSettings.debugViewVarying, (value) => SetDebugViewVarying((Attributes.DebugViewVarying)value));
DebugMenuManager.instance.AddDebugItem<Attributes.DebugViewProperties>("Material", "Properties", () => materialDebugSettings.debugViewProperties, (value) => SetDebugViewProperties((Attributes.DebugViewProperties)value));
DebugMenuManager.instance.AddDebugItem<Attributes.DebugViewTexture>("Material", "Texture", () => materialDebugSettings.debugViewTexture, (value) => SetDebugViewTexture((Attributes.DebugViewTexture)value));
DebugMenuManager.instance.AddDebugItem<int>("Material", "GBuffer",() => materialDebugSettings.debugViewGBuffer, (value) => SetDebugViewGBuffer((int)value), DebugItemFlag.None, new DebugItemHandlerIntEnum(MaterialDebugSettings.debugViewMaterialGBufferStrings, MaterialDebugSettings.debugViewMaterialGBufferValues));
DebugMenuManager.instance.AddDebugItem<LightingDebugPanel, ShadowMapDebugMode>(kShadowDebugMode, () => lightingDebugSettings.shadowDebugMode, (value) => lightingDebugSettings.shadowDebugMode = (ShadowMapDebugMode)value);

36
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/MaterialDebug.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using System;

TessellationDisplacement,
DepthOffset,
Lightmap,
Last,
}
// Number must be contiguous
[GenerateHLSL]
public enum DebugViewTexture
{
None = 0,
ShowMipRatio = DebugViewProperties.Last,
MaxMip,
StreamingMips,
Last,
}
}

public static int[] debugViewMaterialVaryingValues = null;
public static GUIContent[] debugViewMaterialPropertiesStrings = null;
public static int[] debugViewMaterialPropertiesValues = null;
public static GUIContent[] debugViewMaterialTextureStrings = null;
public static int[] debugViewMaterialTextureValues = null;
public static GUIContent[] debugViewMaterialGBufferStrings = null;
public static int[] debugViewMaterialGBufferValues = null;

index = 0;
FillWithPropertiesEnum(typeof(Attributes.DebugViewProperties), debugViewMaterialPropertiesStrings, debugViewMaterialPropertiesValues, "", ref index);
// Textures debug
var textureNames = Enum.GetNames(typeof(Attributes.DebugViewTexture));
debugViewMaterialTextureStrings = new GUIContent[textureNames.Length];
debugViewMaterialTextureValues = new int[textureNames.Length];
index = 0;
FillWithPropertiesEnum(typeof(Attributes.DebugViewTexture), debugViewMaterialTextureStrings, debugViewMaterialTextureValues, "", ref index);
// Gbuffer debug
var gbufferNames = Enum.GetNames(typeof(Attributes.DebugViewGbuffer));
debugViewMaterialGBufferStrings = new GUIContent[gbufferNames.Length + bsdfDataDeferredType.GetFields().Length];

public int debugViewEngine { get { return m_DebugViewEngine; } }
public Attributes.DebugViewVarying debugViewVarying { get { return m_DebugViewVarying; } }
public Attributes.DebugViewProperties debugViewProperties { get { return m_DebugViewProperties; } }
public Attributes.DebugViewTexture debugViewTexture { get { return m_DebugViewTexture; } }
public int debugViewGBuffer { get { return m_DebugViewGBuffer; } }
int m_DebugViewMaterial = 0; // No enum there because everything is generated from materials.

Attributes.DebugViewTexture m_DebugViewTexture = Attributes.DebugViewTexture.None;
int m_DebugViewGBuffer = 0; // Can't use GBuffer enum here because the values are actually split between this enum and values from Lit.BSDFData
public int GetDebugMaterialIndex()

// They are all mutually exclusive so return the sum will return the right index.
return m_DebugViewGBuffer + m_DebugViewMaterial + m_DebugViewEngine + (int)m_DebugViewVarying + (int)m_DebugViewProperties;
return m_DebugViewGBuffer + m_DebugViewMaterial + m_DebugViewEngine + (int)m_DebugViewVarying + (int)m_DebugViewProperties + (int)m_DebugViewTexture;
}
public void DisableMaterialDebug()

m_DebugViewVarying = Attributes.DebugViewVarying.None;
m_DebugViewProperties = Attributes.DebugViewProperties.None;
m_DebugViewTexture = Attributes.DebugViewTexture.None;
m_DebugViewGBuffer = 0;
}

DisableMaterialDebug();
m_DebugViewProperties = value;
}
public void SetDebugViewTexture(Attributes.DebugViewTexture value)
{
if (value != 0)
DisableMaterialDebug();
m_DebugViewTexture = value;
}
public void SetDebugViewGBuffer(int value)
{

public bool IsDebugDisplayEnabled()
{
return (m_DebugViewEngine != 0 || m_DebugViewMaterial != 0 || m_DebugViewVarying != Attributes.DebugViewVarying.None || m_DebugViewProperties != Attributes.DebugViewProperties.None || m_DebugViewGBuffer != 0);
return (m_DebugViewEngine != 0 || m_DebugViewMaterial != 0 || m_DebugViewVarying != Attributes.DebugViewVarying.None || m_DebugViewProperties != Attributes.DebugViewProperties.None || m_DebugViewTexture != Attributes.DebugViewTexture.None || m_DebugViewGBuffer != 0);
}
}
}

10
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/MaterialDebug.cs.hlsl


#define DEBUGVIEWPROPERTIES_TESSELLATION_DISPLACEMENT (19)
#define DEBUGVIEWPROPERTIES_DEPTH_OFFSET (20)
#define DEBUGVIEWPROPERTIES_LIGHTMAP (21)
#define DEBUGVIEWPROPERTIES_LAST (22)
//
// UnityEngine.Experimental.Rendering.HDPipeline.Attributes.DebugViewTexture: static fields
//
#define DEBUGVIEWTEXTURE_NONE (0)
#define DEBUGVIEWTEXTURE_SHOW_MIP_RATIO (22)
#define DEBUGVIEWTEXTURE_MAX_MIP (23)
#define DEBUGVIEWTEXTURE_STREAMING_MIPS (24)
#define DEBUGVIEWTEXTURE_LAST (25)
#endif

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine.Rendering;
using System;
using System.Diagnostics;

// Render opaque objects into GBuffer
if (m_CurrentDebugDisplaySettings.IsDebugDisplayEnabled())
{
m_CurrentDebugDisplaySettings.UpdateMaterials();
// When doing debug display, the shader has the clip instruction regardless of the depth prepass so we can use regular depth test.
RenderOpaqueRenderList(cull, camera, renderContext, cmd, HDShaderPassNames.s_GBufferDebugDisplayName, m_currentRendererConfigurationBakedLighting, RenderQueueRange.opaque, m_DepthStateOpaque);
}

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/LitProperties.hlsl


// Set of users variables
float4 _BaseColor;
float4 _BaseColorMap_ST;
float4 _BaseColorMap_TexelSize;
float4 _BaseColorMap_MipInfo;
float _Metallic;
float _Smoothness;

float4 _BaseColorMap1_ST;
float4 _BaseColorMap2_ST;
float4 _BaseColorMap3_ST;
float4 _BaseColorMap0_TexelSize;
float4 _BaseColorMap0_MipInfo;
PROP_DECL(float, _Metallic);
PROP_DECL(float, _Smoothness);

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Unlit/UnlitProperties.hlsl


TEXTURE2D(_UnlitColorMap);
SAMPLER(sampler_UnlitColorMap);
float4 _UnlitColorMap_ST;
float4 _UnlitColorMap_TexelSize;
float4 _UnlitColorMap_MipInfo;
TEXTURE2D(_DistortionVectorMap);
SAMPLER(sampler_DistortionVectorMap);

126
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/FragInputs.hlsl


break;
}
}
uint GetMipCount(Texture2D tex)
{
uint width, height, depth, mipCount;
width = height = depth = mipCount = 0;
tex.GetDimensions(width, height, depth, mipCount);
return mipCount;
}
float4 GetStreamingMipColour(uint mipCount, float4 mipInfo)
{
// mipInfo :
// x = quality setings minStreamingMipLevel
// y = original mip count for texture
// z = desired on screen mip level
// w = 0
uint originalTextureMipCount = uint(mipInfo.y);
// If material/shader mip info (original mip level) has not been set its not a streamed texture
if (originalTextureMipCount == 0)
return float4(1.0f, 1.0f, 1.0f, 0.0f);
uint desiredMipLevel = uint(mipInfo.z);
uint mipCountDesired = uint(originalTextureMipCount) - uint(desiredMipLevel);
if (mipCount < mipCountDesired)
{
// red tones when not at the desired mip level (reduction due to budget). Brighter is further from original, alpha 0 when at desired
float ratioToDesired = float(mipCount) / float(mipCountDesired);
return float4(0.5f + (0.5f * ratioToDesired), 0.0f, 0.0f, 1.0f - ratioToDesired);
}
else if (mipCount >= originalTextureMipCount)
{
// original colour when at (or beyond) original mip count
return float4(1.0f, 1.0f, 1.0f, 0.0f);
}
else
{
// green tones when not at the original mip level. Brighter is closer to original, alpha 0 when at original
float ratioToOriginal = float(mipCount) / float(originalTextureMipCount);
return float4(0.0f, 0.5f + (0.5f * ratioToOriginal), 0.0f, 1.0f - ratioToOriginal);
}
}
float4 GetSimpleMipCountColour(uint mipCount)
{
// 0-4, red tones, 4-7 green tones, 8-11 blue tones
float4 color = float4(
mipCount >= 0 && mipCount < 4 ? 0.5f + (float)(mipCount) / 6.0f : 0.0f,
mipCount >= 4 && mipCount < 8 ? 0.5f + (float)(mipCount - 4) / 6.0f : 0.0f,
mipCount >= 8 && mipCount < 12 ? 0.5f + (float)(mipCount - 8) / 6.0f : 0.0f,
0.5f
);
return mipCount >= 12 ? float4(1.0f, 1.0f, 1.0f, 0.0f) : color;
}
float GetMipLevel(float2 uv, float2 textureSize)
{
float2 dx = ddx(uv * textureSize.x);
float2 dy = ddy(uv * textureSize.y);
float d = max(dot(dx, dx), dot(dy, dy));
return 0.5f * log2(d);
}
float4 GetMipLevelColour(float2 iUV, float2 iTextureSize)
{
float mipLevel = GetMipLevel(iUV, iTextureSize);
mipLevel = clamp(mipLevel, 0.0f, 6.0f - 0.0001f);
float4 colours[6] = {
float4(0.0f, 0.0f, 1.0f, 0.8f),
float4(0.0f, 0.5f, 1.0f, 0.4f),
float4(1.0f, 1.0f, 1.0f, 0.0f), // optimal level
float4(1.0f, 0.7f, 0.0f, 0.2f),
float4(1.0f, 0.3f, 0.0f, 0.6f),
float4(1.0f, 0.0f, 0.0f, 0.8f)
};
int mipLevelInt = floor(mipLevel);
float t = frac(mipLevel);
float4 a = colours[mipLevelInt];
float4 b = colours[mipLevelInt + 1];
float4 color = lerp(a, b, t);
return color;
}
float3 GetDebugMipColour(float3 originalColour, Texture2D tex, float4 texelSize, float2 uv)
{
// https://aras-p.info/blog/2011/05/03/a-way-to-visualize-mip-levels/
float4 mipColour = GetMipLevelColour(uv, texelSize.zw); // /8 to push down 2 mip levels (4 * uv difference)
return lerp(originalColour, mipColour.rgb, mipColour.a);
}
float3 GetDebugMaxMipColour(float3 originalColour, Texture2D tex)
{
uint mipCount = GetMipCount(tex);
float4 mipColour = GetSimpleMipCountColour(mipCount);
return lerp(originalColour, mipColour.rgb, mipColour.a);
}
float3 GetDebugStreamingMipColour(float3 originalColour, Texture2D tex, float4 mipInfo)
{
uint mipCount = GetMipCount(tex);
float4 mipColour = GetStreamingMipColour(mipCount, mipInfo);
return lerp(originalColour, mipColour.rgb, mipColour.a);
}
void GetTextureDataDebug(uint paramId, FragInputs input, Texture2D tex, float4 texelSize, float4 mipInfo, inout float3 result, inout bool needLinearToSRGB)
{
switch (paramId)
{
case DEBUGVIEWTEXTURE_SHOW_MIP_RATIO:
result = GetDebugMipColour(result, tex, texelSize, input.texCoord0.xy);
break;
case DEBUGVIEWTEXTURE_MAX_MIP:
result = GetDebugMaxMipColour(result, tex);
break;
case DEBUGVIEWTEXTURE_STREAMING_MIPS:
result = GetDebugStreamingMipColour(result, tex, mipInfo);
break;
}
}

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassForward.hlsl


// Same code in ShaderPassForwardUnlit.shader
if (_DebugViewMaterial != 0)
{
float3 result = float3(1.0, 0.0, 1.0);
float3 result = (_DebugViewMaterial >= DEBUGVIEWTEXTURE_SHOW_MIP_RATIO && _DebugViewMaterial < DEBUGVIEWTEXTURE_LAST) ? ApplyBlendMode(bsdfData.diffuseColor, builtinData.opacity).xyz : float3(1.0, 0.0, 1.0);
bool needLinearToSRGB = false;
GetPropertiesDataDebug(_DebugViewMaterial, result, needLinearToSRGB);

GetBSDFDataDebug(_DebugViewMaterial, bsdfData, result, needLinearToSRGB); // TODO: This required to initialize all field from BSDFData...
#ifdef LAYERED_LIT_SHADER
GetTextureDataDebug(_DebugViewMaterial, input, _BaseColorMap0, _BaseColorMap0_TexelSize, _BaseColorMap0_MipInfo, result, needLinearToSRGB);
#else
GetTextureDataDebug(_DebugViewMaterial, input, _BaseColorMap, _BaseColorMap_TexelSize, _BaseColorMap_MipInfo, result, needLinearToSRGB);
#endif
// TEMP!
// For now, the final blit in the backbuffer performs an sRGB write

3
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassForwardUnlit.hlsl


// Same code in ShaderPassForward.shader
if (_DebugViewMaterial != 0)
{
float3 result = float3(1.0, 0.0, 1.0);
float3 result = (_DebugViewMaterial >= DEBUGVIEWTEXTURE_SHOW_MIP_RATIO && _DebugViewMaterial < DEBUGVIEWTEXTURE_LAST) ? outColor.rgb : float3(1.0, 0.0, 1.0);
bool needLinearToSRGB = false;
GetPropertiesDataDebug(_DebugViewMaterial, result, needLinearToSRGB);

GetTextureDataDebug(_DebugViewMaterial, input, _UnlitColorMap, _UnlitColorMap_TexelSize, _UnlitColorMap_MipInfo, result, needLinearToSRGB);
// TEMP!
// For now, the final blit in the backbuffer performs an sRGB write

正在加载...
取消
保存