浏览代码

Pipeline specific graph functions and nodes (#1673)

* Refactor GraphFunctions includes to per-pipeline
* Add Scene Color node
- And camera opaque requirements
- Returns 0s in HD
* Add Scene Depth node
* Rename GraphFunctions.hlsl > ShaderGraphFunctions.hlsl
* Update references to ShaderGraphFunctions.hlsl
/main
GitHub 6 年前
当前提交
8908da09
共有 23 个文件被更改,包括 324 次插入16 次删除
  1. 4
      com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDPBRPass.template
  2. 4
      com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDSubShaderUtilities.cs
  3. 4
      com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDUnlitPassForward.template
  4. 7
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightPBRSubShader.cs
  5. 7
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightUnlitSubShader.cs
  6. 4
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightPBRExtraPasses.template
  7. 2
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightPBRForwardPass.template
  8. 4
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightUnlitExtraPasses.template
  9. 2
      com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightUnlitPass.template
  10. 10
      com.unity.shadergraph/Editor/Data/Graphs/ShaderGraphRequirements.cs
  11. 28
      com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl
  12. 19
      com.unity.render-pipelines.high-definition/HDRP/ShaderGraphFunctions.hlsl
  13. 8
      com.unity.render-pipelines.high-definition/HDRP/ShaderGraphFunctions.hlsl.meta
  14. 46
      com.unity.render-pipelines.lightweight/LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl
  15. 9
      com.unity.render-pipelines.lightweight/LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl.meta
  16. 18
      com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireCameraOpaqueTexture.cs
  17. 11
      com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireCameraOpaqueTexture.cs.meta
  18. 18
      com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireDepthTexture.cs
  19. 11
      com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireDepthTexture.cs.meta
  20. 51
      com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneColorNode.cs
  21. 11
      com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneColorNode.cs.meta
  22. 51
      com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneDepthNode.cs
  23. 11
      com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneDepthNode.cs.meta

4
com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDPBRPass.template


#include "CoreRP/ShaderLibrary/NormalSurfaceGradient.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
// define FragInputs structure
#include "HDRP/ShaderPass/FragInputs.hlsl"
#include "HDRP/ShaderPass/ShaderPass.cs.hlsl"

// End Defines
//-------------------------------------------------------------------------------------
#include "ShaderGraphLibrary/Functions.hlsl"
#include "HDRP/ShaderGraphFunctions.hlsl"
#ifdef DEBUG_DISPLAY
#include "HDRP/Debug/DebugDisplay.hlsl"
#endif

4
com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDSubShaderUtilities.cs


// graph requirements describe what the graph itself requires
var pixelRequirements = ShaderGraphRequirements.FromNodes(pixelNodes, ShaderStageCapability.Fragment, false); // TODO: is ShaderStageCapability.Fragment correct?
var vertexRequirements = ShaderGraphRequirements.FromNodes(vertexNodes, ShaderStageCapability.Vertex, false);
var graphRequirements = pixelRequirements.Union(vertexRequirements);
// Function Registry tracks functions to remove duplicates, it wraps a string builder that stores the combined function string
ShaderStringBuilder graphNodeFunctions = new ShaderStringBuilder();

foreach (var define in pass.ExtraDefines)
defines.AddShaderChunk(define);
}
if (graphRequirements.requiresDepthTexture)
defines.AddShaderChunk("#define REQUIRE_DEPTH_TEXTURE");
defines.AddGenerator(interpolatorDefines);
}

4
com.unity.render-pipelines.high-definition/HDRP/Editor/ShaderGraph/HDUnlitPassForward.template


#include "CoreRP/ShaderLibrary/Common.hlsl"
#include "CoreRP/ShaderLibrary/Wind.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
// define FragInputs structure
#include "HDRP/ShaderPass/FragInputs.hlsl"
#include "HDRP/ShaderPass/ShaderPass.cs.hlsl"

// End Defines
//-------------------------------------------------------------------------------------
#include "ShaderGraphLibrary/Functions.hlsl"
#include "HDRP/ShaderGraphFunctions.hlsl"
#ifdef DEBUG_DISPLAY
#include "HDRP/Debug/DebugDisplay.hlsl"
#endif

7
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightPBRSubShader.cs


var vertexRequirements = ShaderGraphRequirements.FromNodes(vertexNodes, ShaderStageCapability.Vertex, false);
var pixelRequirements = ShaderGraphRequirements.FromNodes(pixelNodes, ShaderStageCapability.Fragment);
var graphRequirements = pixelRequirements.Union(vertexRequirements);
var surfaceRequirements = ShaderGraphRequirements.FromNodes(pixelNodes, ShaderStageCapability.Fragment, false);
var modelRequiements = ShaderGraphRequirements.none;

if (masterNode.surfaceType == SurfaceType.Transparent && masterNode.alphaMode == AlphaMode.Premultiply)
defines.AppendLine("#define _ALPHAPREMULTIPLY_ON 1");
if (graphRequirements.requiresDepthTexture)
defines.AppendLine("#define REQUIRE_DEPTH_TEXTURE");
if (graphRequirements.requiresCameraOpaqueTexture)
defines.AppendLine("#define REQUIRE_OPAQUE_TEXTURE");
// ----------------------------------------------------- //
// START VERTEX DESCRIPTION //

7
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/LightWeightUnlitSubShader.cs


var vertexRequirements = ShaderGraphRequirements.FromNodes(vertexNodes, ShaderStageCapability.Vertex, false);
var pixelRequirements = ShaderGraphRequirements.FromNodes(pixelNodes, ShaderStageCapability.Fragment);
var graphRequirements = pixelRequirements.Union(vertexRequirements);
var surfaceRequirements = ShaderGraphRequirements.FromNodes(pixelNodes, ShaderStageCapability.Fragment, false);
var modelRequiements = ShaderGraphRequirements.none;

if (masterNode.surfaceType == SurfaceType.Transparent && masterNode.alphaMode == AlphaMode.Premultiply)
defines.AppendLine("#define _ALPHAPREMULTIPLY_ON 1");
if (graphRequirements.requiresDepthTexture)
defines.AppendLine("#define REQUIRE_DEPTH_TEXTURE");
if (graphRequirements.requiresCameraOpaqueTexture)
defines.AppendLine("#define REQUIRE_OPAQUE_TEXTURE");
// ----------------------------------------------------- //
// START VERTEX DESCRIPTION //

4
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightPBRExtraPasses.template


${Defines}
#include "LWRP/ShaderLibrary/Core.hlsl"
#include "LWRP/ShaderLibrary/Lighting.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
#include "LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl"
#include "CoreRP/ShaderLibrary/Color.hlsl"
${Graph}

${Defines}
#include "LWRP/ShaderLibrary/Core.hlsl"
#include "LWRP/ShaderLibrary/Lighting.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
#include "LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl"
#include "CoreRP/ShaderLibrary/Color.hlsl"
${Graph}

2
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightPBRForwardPass.template


#include "LWRP/ShaderLibrary/Lighting.hlsl"
#include "CoreRP/ShaderLibrary/Color.hlsl"
#include "CoreRP/ShaderLibrary/UnityInstancing.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
#include "LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl"
${Graph}
struct GraphVertexOutput

4
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightUnlitExtraPasses.template


${Defines}
#include "LWRP/ShaderLibrary/Core.hlsl"
#include "LWRP/ShaderLibrary/Lighting.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
#include "LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl"
#include "CoreRP/ShaderLibrary/Color.hlsl"
${Graph}

${Defines}
#include "LWRP/ShaderLibrary/Core.hlsl"
#include "LWRP/ShaderLibrary/Lighting.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
#include "LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl"
#include "CoreRP/ShaderLibrary/Color.hlsl"
${Graph}

2
com.unity.render-pipelines.lightweight/LWRP/Editor/ShaderGraph/lightweightUnlitPass.template


#include "LWRP/ShaderLibrary/Lighting.hlsl"
#include "CoreRP/ShaderLibrary/Color.hlsl"
#include "LWRP/ShaderLibrary/InputSurfaceUnlit.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
#include "LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl"
${Graph}
struct GraphVertexOutput

10
com.unity.shadergraph/Editor/Data/Graphs/ShaderGraphRequirements.cs


public bool requiresVertexColor;
public bool requiresFaceSign;
public List<UVChannel> requiresMeshUVs;
public bool requiresDepthTexture;
public bool requiresCameraOpaqueTexture;
public static ShaderGraphRequirements none
{

newReqs.requiresScreenPosition = other.requiresScreenPosition | requiresScreenPosition;
newReqs.requiresVertexColor = other.requiresVertexColor | requiresVertexColor;
newReqs.requiresFaceSign = other.requiresFaceSign | requiresFaceSign;
newReqs.requiresDepthTexture = other.requiresDepthTexture | requiresDepthTexture;
newReqs.requiresCameraOpaqueTexture = other.requiresCameraOpaqueTexture | requiresCameraOpaqueTexture;
newReqs.requiresMeshUVs = new List<UVChannel>();
if (requiresMeshUVs != null)

bool requiresScreenPosition = nodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
bool requiresVertexColor = nodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
bool requiresFaceSign = nodes.OfType<IMayRequireFaceSign>().Any(x => x.RequiresFaceSign());
bool requiresDepthTexture = nodes.OfType<IMayRequireDepthTexture>().Any(x => x.RequiresDepthTexture());
bool requiresCameraOpaqueTexture = nodes.OfType<IMayRequireCameraOpaqueTexture>().Any(x => x.RequiresCameraOpaqueTexture());
var meshUV = new List<UVChannel>();
for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex)

requiresScreenPosition = requiresScreenPosition,
requiresVertexColor = requiresVertexColor,
requiresFaceSign = requiresFaceSign,
requiresMeshUVs = meshUV
requiresMeshUVs = meshUV,
requiresDepthTexture = requiresDepthTexture,
requiresCameraOpaqueTexture = requiresCameraOpaqueTexture
};
return reqs;

28
com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl


// UNITY_SHADER_NO_UPGRADE
#ifndef UNITY_SHADER_GRAPH_INCLUDED
#define UNITY_SHADER_GRAPH_INCLUDED
#ifndef UNITY_GRAPHFUNCTIONS_INCLUDED
#define UNITY_GRAPHFUNCTIONS_INCLUDED
// ----------------------------------------------------------------------------
// Included in generated graph shaders
// ----------------------------------------------------------------------------
bool IsGammaSpace()
{

float2 alphas[8];
};
#endif // UNITY_SHADER_GRAPH_INCLUDED
#ifndef SHADERGRAPH_SAMPLE_SCENE_DEPTH
#define SHADERGRAPH_SAMPLE_SCENE_DEPTH(uv) shadergraph_SampleSceneDepth(uv);
#endif
#ifndef SHADERGRAPH_SAMPLE_SCENE_COLOR
#define SHADERGRAPH_SAMPLE_SCENE_COLOR(uv) shadergraph_SampleSceneColor(uv);
#endif
float shadergraph_SampleSceneDepth(float2 uv)
{
return 1;
}
float3 shadergraph_SampleSceneColor(float2 uv)
{
return 0;
}
#endif // UNITY_GRAPHFUNCTIONS_INCLUDED

19
com.unity.render-pipelines.high-definition/HDRP/ShaderGraphFunctions.hlsl


#ifndef UNITY_GRAPHFUNCTIONS_HD_INCLUDED
#define UNITY_GRAPHFUNCTIONS_HD_INCLUDED
#define SHADERGRAPH_SAMPLE_SCENE_DEPTH(uv) shadergraph_HDSampleSceneDepth(uv);
float shadergraph_HDSampleSceneDepth(float2 uv)
{
#if defined(REQUIRE_DEPTH_TEXTURE)
float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv);
return Linear01Depth(rawDepth, _ZBufferParams);
#endif
return 0;
}
// Always include Shader Graph version
// Always include last to avoid double macros
#include "ShaderGraphLibrary/Functions.hlsl"
#endif // UNITY_GRAPHFUNCTIONS_HD_INCLUDED

8
com.unity.render-pipelines.high-definition/HDRP/ShaderGraphFunctions.hlsl.meta


fileFormatVersion: 2
guid: 609118ca59c7543f59f7b5967e96b463
timeCreated: 1472205826
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

46
com.unity.render-pipelines.lightweight/LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl


#ifndef UNITY_GRAPHFUNCTIONS_LW_INCLUDED
#define UNITY_GRAPHFUNCTIONS_LW_INCLUDED
#define SHADERGRAPH_SAMPLE_SCENE_DEPTH(uv) shadergraph_LWSampleSceneDepth(uv);
#define SHADERGRAPH_SAMPLE_SCENE_COLOR(uv) shadergraph_LWSampleSceneColor(uv);
#if defined(REQUIRE_DEPTH_TEXTURE)
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
TEXTURE2D_ARRAY(_CameraDepthTexture);
#else
TEXTURE2D(_CameraDepthTexture);
#endif
SAMPLER(sampler_CameraDepthTexture);
#endif // REQUIRE_DEPTH_TEXTURE
#if defined(REQUIRE_OPAQUE_TEXTURE)
TEXTURE2D(_CameraOpaqueTexture);
SAMPLER(sampler_CameraOpaqueTexture);
#endif // REQUIRE_OPAQUE_TEXTURE
float shadergraph_LWSampleSceneDepth(float2 uv)
{
#if defined(REQUIRE_DEPTH_TEXTURE)
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
float rawDepth = SAMPLE_TEXTURE2D_ARRAY(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord.xy, unity_StereoEyeIndex).r;
#else
float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv);
#endif
return Linear01Depth(rawDepth, _ZBufferParams);
#endif // REQUIRE_DEPTH_TEXTURE
return 0;
}
float3 shadergraph_LWSampleSceneColor(float2 uv)
{
#if defined(REQUIRE_OPAQUE_TEXTURE)
return SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, uv);
#endif
return 0;
}
// Always include Shader Graph version
// Always include last to avoid double macros
#include "ShaderGraphLibrary/Functions.hlsl"
#endif // UNITY_GRAPHFUNCTIONS_LW_INCLUDED

9
com.unity.render-pipelines.lightweight/LWRP/ShaderLibrary/ShaderGraphFunctions.hlsl.meta


fileFormatVersion: 2
guid: 9adc879d74aec4400bbdce89ff664a05
timeCreated: 1488965025
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

18
com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireCameraOpaqueTexture.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public interface IMayRequireCameraOpaqueTexture
{
bool RequiresCameraOpaqueTexture(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireCameraOpaqueTextureExtensions
{
public static bool RequiresCameraOpaqueTexture(this ISlot slot)
{
var mayRequireCameraOpaqueTexture = slot as IMayRequireCameraOpaqueTexture;
return mayRequireCameraOpaqueTexture != null && mayRequireCameraOpaqueTexture.RequiresCameraOpaqueTexture();
}
}
}

11
com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireCameraOpaqueTexture.cs.meta


fileFormatVersion: 2
guid: 93fdbd040dca943cbbd42c33416450a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

18
com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireDepthTexture.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public interface IMayRequireDepthTexture
{
bool RequiresDepthTexture(ShaderStageCapability stageCapability = ShaderStageCapability.All);
}
public static class MayRequireDepthTextureExtensions
{
public static bool RequiresDepthTexture(this ISlot slot)
{
var mayRequireDepthTexture = slot as IMayRequireDepthTexture;
return mayRequireDepthTexture != null && mayRequireDepthTexture.RequiresDepthTexture();
}
}
}

11
com.unity.shadergraph/Editor/Data/Interfaces/IMayRequireDepthTexture.cs.meta


fileFormatVersion: 2
guid: 6f00cea5e23a84b5bbe1ac581ebfc64e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

51
com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneColorNode.cs


using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Scene", "Scene Color")]
public sealed class SceneColorNode : CodeFunctionNode, IMayRequireCameraOpaqueTexture
{
const string kScreenPositionSlotName = "UV";
const string kOutputSlotName = "Out";
public const int ScreenPositionSlotId = 0;
public const int OutputSlotId = 1;
public SceneColorNode()
{
name = "Scene Color";
UpdateNodeAfterDeserialization();
}
public override bool hasPreview { get { return false; } }
public override string documentationURL
{
get { return "https://github.com/Unity-Technologies/ShaderGraph/wiki/Scene-Color-Node"; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_SceneColor", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_SceneColor(
[Slot(0, Binding.ScreenPosition)] Vector3 UV,
[Slot(1, Binding.None, ShaderStageCapability.Fragment)] out Vector3 Out)
{
Out = Vector3.one;
return
@"
{
Out = SHADERGRAPH_SAMPLE_SCENE_COLOR(UV);
}
";
}
public bool RequiresCameraOpaqueTexture(ShaderStageCapability stageCapability)
{
return true;
}
}
}

11
com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneColorNode.cs.meta


fileFormatVersion: 2
guid: c4ba33edde21e400685cf0fb5c8f2551
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

51
com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneDepthNode.cs


using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Scene", "Scene Depth")]
public sealed class SceneDepthNode : CodeFunctionNode, IMayRequireDepthTexture
{
const string kScreenPositionSlotName = "UV";
const string kOutputSlotName = "Out";
public const int ScreenPositionSlotId = 0;
public const int OutputSlotId = 1;
public SceneDepthNode()
{
name = "Scene Depth";
UpdateNodeAfterDeserialization();
}
public override bool hasPreview { get { return false; } }
public override string documentationURL
{
get { return "https://github.com/Unity-Technologies/ShaderGraph/wiki/Scene-Depth-Node"; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_SceneDepth", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_SceneDepth(
[Slot(0, Binding.ScreenPosition)] Vector3 UV,
[Slot(1, Binding.None, ShaderStageCapability.Fragment)] out Vector3 Out)
{
Out = Vector3.one;
return
@"
{
Out = SHADERGRAPH_SAMPLE_SCENE_DEPTH(UV);
}
";
}
public bool RequiresDepthTexture(ShaderStageCapability stageCapability)
{
return true;
}
}
}

11
com.unity.shadergraph/Editor/Data/Nodes/Input/Scene/SceneDepthNode.cs.meta


fileFormatVersion: 2
guid: f8e0a75d065844105a06ca48151adb7b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存