浏览代码

Debug Shader first version

/main
Julien Ignace 8 年前
当前提交
d825657a
共有 5 个文件被更改,包括 265 次插入8 次删除
  1. 89
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 93
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader
  3. 16
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl
  4. 11
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
  5. 64
      Assets/TestScenes/HDTest/EnlightenTest.unity

89
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


// This HDRenderLoop assume linear lighting. Don't work with gamma.
public class HDRenderLoop : ScriptableRenderLoop
{
private static string m_HDRenderLoopPath = "Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset";
public enum MaterialDebugMode
{
None = 0,
DiffuseColor = 1,
Normal = 2,
Depth = 3,
AmbiantOcclusion = 4,
SpecularColor = 5,
SpecularOcclustion = 6,
Smoothness = 7,
MaterialId = 8,
UV0 = 9,
Tangent = 10,
Bitangent = 11
}
public MaterialDebugMode m_MaterialDebugMode = MaterialDebugMode.None;
public MaterialDebugMode materialDebugMode
{
get { return m_MaterialDebugMode; }
set { m_MaterialDebugMode = value; }
}
[MenuItem("Renderloop/CreateHDRenderLoop")]
[MenuItem("Renderloop/HDRenderLoop/CreateHDRenderLoop")]
UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset");
UnityEditor.AssetDatabase.CreateAsset(instance, m_HDRenderLoopPath);
}
[MenuItem("Renderloop/HDRenderLoop/DebugMenu")]
static void DebugMenu()
{
HDRenderLoop renderLoop = AssetDatabase.LoadAssetAtPath(m_HDRenderLoopPath, typeof(HDRenderLoop)) as HDRenderLoop;
if(renderLoop != null)
{
if (renderLoop.materialDebugMode == HDRenderLoop.MaterialDebugMode.None)
renderLoop.materialDebugMode = HDRenderLoop.MaterialDebugMode.Normal;
else
renderLoop.materialDebugMode = HDRenderLoop.MaterialDebugMode.None;
EditorUtility.SetDirty(renderLoop);
}
}
#endif

settings.sorting.sortOptions = SortOptions.SortByMaterialThenMesh;
settings.inputCullingOptions.SetQueuesOpaque();
renderLoop.DrawRenderers(ref settings);
}
void RenderMaterialDebug(CullResults cull, Camera camera, RenderLoop renderLoop)
{
// setup GBuffer for rendering
var cmd = new CommandBuffer();
cmd.name = "Debug Pass";
cmd.GetTemporaryRT(s_CameraColorBuffer, -1, -1, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
cmd.GetTemporaryRT(s_CameraDepthBuffer, -1, -1, 24, FilterMode.Point, RenderTextureFormat.Depth);
cmd.SetRenderTarget(new RenderTargetIdentifier(s_CameraColorBuffer), new RenderTargetIdentifier(s_CameraDepthBuffer));
cmd.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
Shader.SetGlobalInt("g_MaterialDebugMode", (int)m_MaterialDebugMode);
DrawRendererSettings settings = new DrawRendererSettings(cull, camera, new ShaderPassName("Debug"));
settings.sorting.sortOptions = SortOptions.SortByMaterialThenMesh;
settings.inputCullingOptions.SetQueuesOpaque();
renderLoop.DrawRenderers(ref settings);
cmd = new CommandBuffer();
cmd.name = "FinalPass";
cmd.Blit(s_CameraColorBuffer, BuiltinRenderTextureType.CameraTarget, m_FinalPassMaterial, 0);
renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}
Matrix4x4 GetViewProjectionMatrix(Camera camera)

//UpdateLightConstants(cullResults.culledLights /*, ref shadows */);
UpdatePunctualLights(cullResults.culledLights);
if (m_MaterialDebugMode == MaterialDebugMode.None)
{
UpdatePunctualLights(cullResults.culledLights);
InitAndClearBuffer(camera, renderLoop);
InitAndClearBuffer(camera, renderLoop);
RenderGBuffer(cullResults, camera, renderLoop);
RenderGBuffer(cullResults, camera, renderLoop);
RenderDeferredLighting(camera, renderLoop);
RenderDeferredLighting(camera, renderLoop);
// RenderForward(cullResults, camera, renderLoop);
// RenderForward(cullResults, camera, renderLoop);
FinalPass(renderLoop);
FinalPass(renderLoop);
}
else
{
RenderMaterialDebug(cullResults, camera, renderLoop);
}
renderLoop.Submit ();
}

93
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader


ENDCG
}
// ------------------------------------------------------------------
// Debug pass
Pass
{
Name "Debug"
Tags { "LightMode" = "Debug" }
Cull[_CullMode]
CGPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
#pragma vertex VertDefault
#pragma fragment FragDebug
int g_MaterialDebugMode;
#include "Assets/ScriptableRenderLoop/ShaderLibrary/Color.hlsl"
#include "TemplateDisneyGGX.hlsl"
float4 FragDebug( PackedVaryings packedInput ) : SV_Target
{
Varyings input = UnpackVaryings(packedInput);
SurfaceData surfaceData = GetSurfaceData(input);
float3 result = float3(1.0, 1.0, 0.0);
bool outputIsLinear = false;
if(g_MaterialDebugMode == DebugDiffuseColor)
{
result = surfaceData.diffuseColor;
}
else if (g_MaterialDebugMode == DebugNormal)
{
result = surfaceData.normalWS * 0.5 + 0.5;
outputIsLinear = true;
}
else if (g_MaterialDebugMode == DebugDepth)
{
float linearDepth = frac(LinearEyeDepth(input.positionHS.z, _ZBufferParams) * 0.1);
result = linearDepth.xxx;
outputIsLinear = true;
}
else if (g_MaterialDebugMode == DebugAO)
{
result = surfaceData.ambientOcclusion.xxx;
outputIsLinear = true;
}
else if (g_MaterialDebugMode == DebugSpecularColor)
{
result = surfaceData.specularColor;
}
else if (g_MaterialDebugMode == DebugSpecularOcclusion)
{
result = surfaceData.specularOcclusion.xxx;
outputIsLinear = true;
}
else if (g_MaterialDebugMode == DebugSmoothness)
{
result = surfaceData.perceptualSmoothness.xxx;
outputIsLinear = true;
}
else if (g_MaterialDebugMode == DebugMaterialId)
{
result = surfaceData.materialId.xxx;
}
else if (g_MaterialDebugMode == DebugUV0)
{
result = float3(input.texCoord0, 0.0);
}
else if (g_MaterialDebugMode == DebugTangent)
{
result = input.tangentToWorld[0].xyz * 0.5 + 0.5;
outputIsLinear = true;
}
else if (g_MaterialDebugMode == DebugBitangent)
{
result = input.tangentToWorld[1].xyz * 0.5 + 0.5;
outputIsLinear = true;
}
// For now, the final blit in the backbuffer performs an sRGB write
// So in the meantime we apply the inverse transform to linear data to compensate.
if(outputIsLinear)
result = SRGBToLinear(result);
return float4(result, 0.0);
}
ENDCG
}
}
CustomEditor "DisneyGGXGUI"

16
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl


// SurfaceData and BSDFData
//-----------------------------------------------------------------------------
// Main structure that store the user data (i.e user input of master node in material graph)
struct SurfaceData
{

// System
float3 diffuseLightingAndEmissive;
};
// List of debug modes. Keep in sync with HDRenderLoop.DebugMode
#define DebugNone 0,
#define DebugDiffuseColor 1
#define DebugNormal 2
#define DebugDepth 3
#define DebugAO 4
#define DebugSpecularColor 5
#define DebugSpecularOcclusion 6
#define DebugSmoothness 7
#define DebugMaterialId 8
#define DebugUV0 9
#define DebugTangent 10
#define DebugBitangent 11
//-----------------------------------------------------------------------------
// conversion function for forward and deferred

11
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


return hpositionWS.xyz / hpositionWS.w;
}
// Z buffer to linear 0..1 depth
float Linear01Depth(float depth, float4 zBufferParam)
{
return 1.0 / (zBufferParam.x * depth + zBufferParam.y);
}
// Z buffer to linear depth
float LinearEyeDepth(float depth, float4 zBufferParam)
{
return 1.0 / (zBufferParam.z * depth + zBufferParam.w);
}
#endif // UNITY_COMMON_INCLUDED

64
Assets/TestScenes/HDTest/EnlightenTest.unity


cellSize: 0.16666667
accuratePlacement: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &96510370
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 96510373}
- component: {fileID: 96510372}
- component: {fileID: 96510371}
m_Layer: 0
m_Name: EventSystem
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &96510371
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 96510370}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1077351063, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_HorizontalAxis: Horizontal
m_VerticalAxis: Vertical
m_SubmitButton: Submit
m_CancelButton: Cancel
m_InputActionsPerSecond: 10
m_RepeatDelay: 0.5
m_ForceModuleActive: 0
--- !u!114 &96510372
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 96510370}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -619905303, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_FirstSelected: {fileID: 0}
m_sendNavigationEvents: 1
m_DragThreshold: 5
--- !u!4 &96510373
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 96510370}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &124873432
GameObject:
m_ObjectHideFlags: 0

m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 1
m_AllowMSAA: 1
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022

正在加载...
取消
保存