浏览代码

- Cleaner way describe MasterNode

- First version of suitable template which integrate packing/unpacking between vertex & fragment
N.B.: Refactor is needed in MaterialGraph to move specific shader code generation to ScriptableRenderloop
/scriptablerenderloop-materialgraph
Paul Demeulenaere 8 年前
当前提交
e9b5f5ae
共有 5 个文件被更改,包括 51 次插入112 次删除
  1. 28
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 37
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/Lit.cs
  3. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitData.template
  4. 81
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitShare.template
  5. 8
      Assets/ShaderGenerator/ShaderGeneratorAttributes.cs

28
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
[Serializable]
public abstract class AbstractHDRenderLoopMasterNode : AbstractMasterNode
public abstract class AbstractHDRenderLoopMasterNode : AbstractMasterNode, IGeneratesVertexToFragmentBlock
{
public override void GenerateLightFunction(ShaderGenerator lightFunction)
{

public sealed override void UpdateNodeAfterDeserialization()
{
var surfaceType = GetSurfaceType();
if (surfaceType != null)
{
var fieldsBuiltIn = typeof(Builtin.BuiltinData).GetFields();

var attribute = (SurfaceDataAttributes[])field.GetCustomAttributes(typeof(SurfaceDataAttributes), false);
var materialAttribute = (MaterialIdAttributes_WIP[])field.GetCustomAttributes(typeof(MaterialIdAttributes_WIP), false);
var attributes = (SurfaceDataAttributes[])field.GetCustomAttributes(typeof(SurfaceDataAttributes), false);
var attribute = attributes.Length > 0 ? attributes[0] : new SurfaceDataAttributes();
var valueType = SlotValueType.Dynamic;
var fieldType = field.FieldType;

return new
{
index = index,
displayName = attribute.Length > 0 ? attribute[0].displayName : field.Name,
materialID = materialAttribute.Length > 0 ? materialAttribute[0].materialID : new int[] { },
priority = attribute.priority,
displayName = attribute.displayName,
materialID = attribute.filter,
}).Where(o => o.materialID.Contains(GetMatchingMaterialID())).ToArray();
})
.Where(o => (o.materialID == null || o.materialID.Contains(GetMatchingMaterialID())) && o.valueType != SlotValueType.Dynamic)
.OrderBy(o => o.priority)
.ThenBy(o => o.displayName)
.ToArray();
foreach (var slot in slots)
{

}
}
[AttributeUsage(AttributeTargets.Field)]
public class MaterialIdAttributes_WIP : System.Attribute
{
public int[] materialID;
public MaterialIdAttributes_WIP(int[] materialID)
public void GenerateVertexToFragmentBlock(ShaderFragmentInputGenerator visitor, ShaderGenerator visitor_deprecated, GenerationMode generationMode)
this.materialID = materialID;
visitor.AddInput(OutputPrecision.@float, SlotValueType.Vector3, "positionWS");
}
}

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


[GenerateHLSL(PackingRules.Exact, false, true, 1000)]
public struct SurfaceData
{
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard, (int)MaterialId.LitSSS, (int)MaterialId.LitClearCoat, (int)MaterialId.LitSpecular })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard, (int)MaterialId.LitSSS, (int)MaterialId.LitClearCoat, (int)MaterialId.LitSpecular })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard, (int)MaterialId.LitSSS, (int)MaterialId.LitClearCoat, (int)MaterialId.LitSpecular })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard, (int)MaterialId.LitSSS, (int)MaterialId.LitClearCoat, (int)MaterialId.LitSpecular })]
[SurfaceDataAttributes("Smoothness")]
public float perceptualSmoothness;

[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard, (int)MaterialId.LitSSS, (int)MaterialId.LitClearCoat, (int)MaterialId.LitSpecular })]
// MaterialId dependent attribute
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard })]
[SurfaceDataAttributes("Tangent")]
[SurfaceDataAttributes("Tangent", 0, new int[] { (int)MaterialId.LitStandard })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard })]
[SurfaceDataAttributes("Anisotropy")]
[SurfaceDataAttributes("Anisotropy", 0, new int[] { (int)MaterialId.LitStandard })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard })]
[SurfaceDataAttributes("Metallic")]
[SurfaceDataAttributes("Metallic", 0, new int[] { (int)MaterialId.LitStandard })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitStandard })]
[SurfaceDataAttributes("Specular")]
[SurfaceDataAttributes("Specular", 0, new int[] { (int)MaterialId.LitStandard })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitSSS })]
[SurfaceDataAttributes("SubSurface Radius")]
[SurfaceDataAttributes("SubSurface Radius", 0, new int[] { (int)MaterialId.LitSSS })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitSSS })]
[SurfaceDataAttributes("Thickness")]
[SurfaceDataAttributes("Thickness", 0, new int[] { (int)MaterialId.LitSSS })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitSSS })]
[SurfaceDataAttributes("SubSurface Profile")]
[SurfaceDataAttributes("SubSurface Profile", 0, new int[] { (int)MaterialId.LitSSS })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitClearCoat })]
[SurfaceDataAttributes("Coat Normal")]
[SurfaceDataAttributes("Coat Normal", 0, new int[] { (int)MaterialId.LitClearCoat })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitClearCoat })]
[SurfaceDataAttributes("Coat Smoothness")]
[SurfaceDataAttributes("Coat Smoothness", 0, new int[] { (int)MaterialId.LitClearCoat })]
[MaterialIdAttributes_WIP(new int[] { (int)MaterialId.LitSpecular })]
[SurfaceDataAttributes("Specular Color")]
[SurfaceDataAttributes("Specular Color", 0, new int[] { (int)MaterialId.LitSpecular })]
public Vector3 specularColor;
};

9
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitData.template


struct FragInput
{
float4 unPositionSS;
float3 positionWS;
float2 meshUV0;
float2 meshUV1;
float2 meshUV2;
float3 tangentToWorld[3];
bool isFrontFace;
${VaryingAttributes}
};
void GetSurfaceAndBuiltinData(FragInput IN, out SurfaceData o, out BuiltinData builtinData)

float3 vertexNormalWS = IN.tangentToWorld[2].xyz;
o.normalWS = vertexNormalWS;
${PixelShaderBody}
}

81
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LitShare.template


//-------------------------------------------------------------------------------------
struct Attributes
{
float3 positionOS : POSITION;
float3 normalOS : NORMAL;
float2 uv0 : TEXCOORD0;
float2 uv1 : TEXCOORD1;
float2 uv2 : TEXCOORD2;
float4 tangentOS : TANGENT;
float3 positionOS : POSITION;
${VertexAttributes}
float4 positionHS;
float3 positionWS;
float2 meshUV0;
float2 meshUV1;
float2 meshUV2;
float3 tangentToWorld[3];
float4 positionHS;
${VaryingAttributes}
float4 positionHS : SV_Position;
float4 interpolators[5] : TEXCOORD0;
float4 positionHS : SV_Position;
${PackedVaryingAttributes}
PackedVaryings output;
output.positionHS = input.positionHS;
output.interpolators[0].xyz = input.positionWS.xyz;
output.interpolators[1].xyz = input.tangentToWorld[0];
output.interpolators[2].xyz = input.tangentToWorld[1];
output.interpolators[3].xyz = input.tangentToWorld[2];
output.interpolators[0].w = input.meshUV0.x;
output.interpolators[1].w = input.meshUV0.y;
output.interpolators[2].w = input.meshUV1.x;
output.interpolators[3].w = input.meshUV1.y;
output.interpolators[4] = float4(input.meshUV1.xy, 0.0, 0.0);
return output;
PackedVaryings output;
output.positionHS = input.positionHS;
${PackingVaryingCode}
return output;
FragInput output;
ZERO_INITIALIZE(FragInput, output);
FragInput output;
ZERO_INITIALIZE(FragInput, output);
output.unPositionSS = input.positionHS;
output.positionWS.xyz = input.interpolators[0].xyz;
output.tangentToWorld[0] = input.interpolators[1].xyz;
output.tangentToWorld[1] = input.interpolators[2].xyz;
output.tangentToWorld[2] = input.interpolators[3].xyz;
output.meshUV0.xy = float2(input.interpolators[0].w, input.interpolators[1].w);
output.meshUV1.xy = float2(input.interpolators[2].w, input.interpolators[3].w);
output.meshUV2 = input.interpolators[4].xy;
return output;
output.unPositionSS = input.positionHS;
${UnpackVaryingCode}
return output;
// TODO: Here we will also have all the vertex deformation (GPU skinning, vertex animation, morph target...) or we will need to generate a compute shaders instead (better! but require work to deal with unpacking like fp16)
Varyings output;
output.positionWS = TransformObjectToWorld(input.positionOS);
output.positionHS = TransformWorldToHClip(output.positionWS);
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
output.meshUV0 = input.uv0;
output.meshUV1 = input.uv1;
output.meshUV2 = input.uv2;
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
Varyings output;
float3x3 tangentToWorld = CreateTangentToWorld(normalWS, tangentWS.xyz, tangentWS.w);
output.tangentToWorld[0] = tangentToWorld[0];
output.tangentToWorld[1] = tangentToWorld[1];
output.tangentToWorld[2] = tangentToWorld[2];
output.positionWS = TransformObjectToWorld(input.positionOS);
output.positionHS = TransformWorldToHClip(output.positionWS);
${VertexShaderBody}
return PackVaryings(output);
return PackVaryings(output);
}

8
Assets/ShaderGenerator/ShaderGeneratorAttributes.cs


[AttributeUsage(AttributeTargets.Field)]
public class SurfaceDataAttributes : System.Attribute
{
public string displayName;
public string displayName { get; private set; }
public int priority { get; private set; }
public int[] filter { get; private set; }
public SurfaceDataAttributes(string displayName = "")
public SurfaceDataAttributes(string displayName = "", int priority = 0, int[] filter = null)
this.priority = priority;
this.filter = filter;
}
}
}
正在加载...
取消
保存