浏览代码

HDRenderLoop: Move GBuffer description to Lit.cs

- So a material can define easily what kind of GBuffer it use without
having to dig into HDRenderLoop code
/main
Sebastien Lagarde 8 年前
当前提交
698d1646
共有 10 个文件被更改,包括 81 次插入29 次删除
  1. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset.meta
  2. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  3. 8
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoopInspector.cs
  4. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/BuiltinData.cs
  5. 45
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs
  6. 5
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs.hlsl
  7. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.hlsl
  8. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl
  9. 2
      Assets/ShaderGenerator/CSharpToHLSL.cs
  10. 20
      Assets/ShaderGenerator/ShaderTypeGeneration.cs

2
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset.meta


fileFormatVersion: 2
guid: 2400b74f5ce370c4481e5dc417d03703
timeCreated: 1476466770
timeCreated: 1476649858
licenseType: Pro
NativeFormatImporter:
userData:

12
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


{
ClearComputeBuffers();
// See Lit.hlsl for details
gbufferManager.gbufferCount = 4;
gbufferManager.SetBufferDescription(0, "_CameraGBufferTexture0", RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
gbufferManager.SetBufferDescription(1, "_CameraGBufferTexture1", RenderTextureFormat.ARGB2101010, RenderTextureReadWrite.Linear);
gbufferManager.SetBufferDescription(2, "_CameraGBufferTexture2", RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
gbufferManager.SetBufferDescription(3, "_CameraGBufferTexture3", RenderTextureFormat.RGB111110Float, RenderTextureReadWrite.Linear);
// Init lit gbuffer
gbufferManager.gbufferCount = Lit.GBufferDescription.s_gbufferCount;
for (int gbufferIndex = 0; gbufferIndex < gbufferManager.gbufferCount; ++gbufferIndex)
{
gbufferManager.SetBufferDescription(gbufferIndex, "_CameraGBufferTexture" + gbufferIndex, Lit.GBufferDescription.RTFormat[gbufferIndex], Lit.GBufferDescription.RTReadWrite[gbufferIndex]);
}
s_CameraColorBuffer = Shader.PropertyToID("_CameraColorTexture");
s_CameraDepthBuffer = Shader.PropertyToID("_CameraDepthTexture");

8
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoopInspector.cs


object[] attributes = type.GetCustomAttributes(true);
// Get attribute to get the start number of the value for the enum
GenerateHLSL attr = attributes[0] as GenerateHLSL;
if (!attr.needParamDefines)
{
return ;
}
FieldInfo[] fields = type.GetFields();
string subNamespace = type.Namespace.Substring(type.Namespace.LastIndexOf((".")) + 1);

name = (isBSDFData ? "Engine/" : "") + subNamespace + "/" + name;
debugViewMaterialStrings[index] = new GUIContent(name);
debugViewMaterialValues[index] = attr.debugCounterStart + (int)localIndex;
debugViewMaterialValues[index] = attr.paramDefinesStart + (int)localIndex;
index++;
localIndex++;
}

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/BuiltinData.cs


// and are independent from the BSDF parametrization.
// Note: These parameters can be store in GBuffer if the writer wants
//-----------------------------------------------------------------------------
[GenerateHLSL(PackingRules.Exact, true, 100)]
[GenerateHLSL(PackingRules.Exact, false, true, 100)]
public struct BuiltinData
{
[SurfaceDataAttributes("Opacity")]

45
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs


//-----------------------------------------------------------------------------
// Main structure that store the user data (i.e user input of master node in material graph)
[GenerateHLSL(PackingRules.Exact, true, 1000)]
[GenerateHLSL(PackingRules.Exact, false, true, 1000)]
public struct SurfaceData
{
[SurfaceDataAttributes("Base Color")]

// BSDFData
//-----------------------------------------------------------------------------
[GenerateHLSL(PackingRules.Exact, true, 1030)]
[GenerateHLSL(PackingRules.Exact, false, true, 1030)]
public struct BSDFData
{
public Vector3 diffuseColor;

// SpecColor
// fold into fresnel0
};
//-----------------------------------------------------------------------------
// GBuffer management
//-----------------------------------------------------------------------------
[GenerateHLSL(PackingRules.Exact)]
public enum GBufferMaterial
{
Count = 3
};
struct GBufferDescription
{
#if (VELOCITY_IN_GBUFFER)
public const int s_gbufferCount = (int)GBufferMaterial.Count + 2; // +1 for emissive buffer
#else
public const int s_gbufferCount = (int)GBufferMaterial.Count + 1;
#endif
public static RenderTextureFormat[] RTFormat =
{
RenderTextureFormat.ARGB32,
RenderTextureFormat.ARGB2101010,
RenderTextureFormat.ARGB32,
#if (VELOCITY_IN_GBUFFER)
RenderTextureFormat.RGHalf,
#endif
RenderTextureFormat.RGB111110Float
};
public static RenderTextureReadWrite[] RTReadWrite =
{
RenderTextureReadWrite.sRGB,
RenderTextureReadWrite.Linear,
RenderTextureReadWrite.Linear,
#if (VELOCITY_IN_GBUFFER)
RenderTextureReadWrite.Linear,
#endif
RenderTextureReadWrite.Linear
};
}
}
}

5
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.cs.hlsl


#define DEBUGVIEW_LIT_BSDFDATA_COAT_NORMAL_WS (1045)
#define DEBUGVIEW_LIT_BSDFDATA_COAT_ROUGHNESS (1046)
//
// UnityEngine.ScriptableRenderLoop.Lit.GBufferMaterial: static fields
//
#define GBUFFERMATERIAL_COUNT (3)
// Generated from UnityEngine.ScriptableRenderLoop.Lit.SurfaceData
// PackingRules = Exact
struct SurfaceData

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit.hlsl


// conversion function for deferred
//-----------------------------------------------------------------------------
#define GBUFFER_MATERIAL_COUNT 3
// Encode SurfaceData (BSDF parameters) into GBuffer
// Must be in sync with RT declared in HDRenderLoop.cs ::Rebuild
void EncodeIntoGBuffer( SurfaceData surfaceData,

12
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Material.hlsl


// Define for GBuffer
//-----------------------------------------------------------------------------
#ifdef GBUFFER_MATERIAL_COUNT
#ifdef GBUFFERMATERIAL_COUNT
#if GBUFFER_MATERIAL_COUNT == 3
#if GBUFFERMATERIAL_COUNT == 3
#define OUTPUT_GBUFFER(NAME) \
out float4 MERGE_NAME(NAME, 0) : SV_Target0, \

#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 3)
#endif
#elif GBUFFER_MATERIAL_COUNT == 4
#elif GBUFFERMATERIAL_COUNT == 4
#define OUTPUT_GBUFFER(NAME) \
out float4 MERGE_NAME(NAME, 0) : SV_Target0, \

#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 4)
#endif
#elif GBUFFER_MATERIAL_COUNT == 5
#elif GBUFFERMATERIAL_COUNT == 5
#define OUTPUT_GBUFFER(NAME) \
out float4 MERGE_NAME(NAME, 0) : SV_Target0, \

#define GBUFFER_BAKE_LIGHTING_TARGET(TARGET) MERGE_NAME(TARGET, 5)
#endif
#endif // #if GBUFFER_MATERIAL_COUNT == 3
#endif // #if GBUFFERMATERIAL_COUNT == 3
// Generic whatever the number of GBuffer
#ifdef VELOCITY_IN_GBUFFER

#define FETCH_BAKE_LIGHTING_GBUFFER(NAME, TEX, UV) float4 GBUFFER_BAKE_LIGHTING_NAME(NAME) = GBUFFER_BAKE_LIGHTING_NAME(TEX).Load(uint3(UV, 0));
#define DECODE_BAKE_LIGHTING_FROM_GBUFFER(NAME) DecodeBakedDiffuseLigthingFromGBuffer(GBUFFER_BAKE_LIGHTING_NAME(NAME))
#endif // #ifdef GBUFFER_MATERIAL_COUNT
#endif // #ifdef GBUFFERMATERIAL_COUNT
// Decode velocity need to be accessible in both forward and deferred
#ifdef VELOCITY_IN_GBUFFER

2
Assets/ShaderGenerator/CSharpToHLSL.cs


foreach (var gen in it.Value)
{
if (gen.hasFields && !gen.IsSimple())
if (gen.hasFields && gen.needAccessors())
{
writer.Write(gen.EmitAccessors() + "\n");
}

20
Assets/ShaderGenerator/ShaderTypeGeneration.cs


public class GenerateHLSL : System.Attribute
{
public PackingRules packingRules;
public bool simple;
public int debugCounterStart;
public GenerateHLSL(PackingRules rules = PackingRules.Exact, bool simple = false, int debugCounterStart = 1)
public bool needAccessors; // Whether or not to generate the accessors
public bool needParamDefines; // Wheter or not to generate define for each parameters of the struc
public int paramDefinesStart; // Start of the generated define
public GenerateHLSL(PackingRules rules = PackingRules.Exact, bool needAccessors = true, bool needParamDefines = false, int paramDefinesStart = 1)
this.simple = simple;
this.debugCounterStart = debugCounterStart;
this.needAccessors = needAccessors;
this.needParamDefines = needParamDefines;
this.paramDefinesStart = paramDefinesStart;
}
}

continue;
}
if (attr.simple)
if (attr.needParamDefines)
statics[("DEBUGVIEW_" + subNamespace + "_" + type.Name + "_" + name).ToUpper()] = Convert.ToString(attr.debugCounterStart + debugCounter++);
statics[("DEBUGVIEW_" + subNamespace + "_" + type.Name + "_" + name).ToUpper()] = Convert.ToString(attr.paramDefinesStart + debugCounter++);
}
if (field.FieldType.IsPrimitive)

get { return statics.Count > 0; }
}
public bool IsSimple()
public bool needAccessors()
return attr.simple;
return attr.needAccessors;
}
public Type type;

正在加载...
取消
保存