Skip to content

Commit

Permalink
feat(hdrp): add sample Lit shader (#37)
Browse files Browse the repository at this point in the history
feat: lower shader target level to 4.5

fix(urp): don't setup urp buffers if screen properties are not yet present

fix: prevent asset import errors by adding appropriate shader package requirements and fallbacks

refactor: merge render shaders into a single shader asset
  • Loading branch information
happy-turtle authored Jun 20, 2024
1 parent 45a0672 commit 975b5bb
Show file tree
Hide file tree
Showing 32 changed files with 13,293 additions and 283 deletions.
2 changes: 1 addition & 1 deletion HDRP/Runtime/OitRenderPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OitRenderPass : CustomPass

protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
orderIndependentTransparency ??= new OitLinkedList("OitRenderHDRP");
orderIndependentTransparency ??= new OitLinkedList("OitRender");
}

protected override void Execute(CustomPassContext ctx)
Expand Down
8 changes: 8 additions & 0 deletions HDRP/Shaders.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11,974 changes: 11,974 additions & 0 deletions HDRP/Shaders/OitLitHDRP.shader

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

267 changes: 267 additions & 0 deletions HDRP/Shaders/OitShaderPassForwardHDRP.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
#if SHADERPASS != SHADERPASS_FORWARD
#error SHADERPASS_is_not_correctly_define
#endif

#include "Packages/org.happy-turtle.order-independent-transparency/Shaders/LinkedListCreation.hlsl"

#ifdef _WRITE_TRANSPARENT_MOTION_VECTOR
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/MotionVectorVertexShaderCommon.hlsl"

PackedVaryingsType Vert(AttributesMesh inputMesh, AttributesPass inputPass)
{
VaryingsType varyingsType;
#ifdef HAVE_VFX_MODIFICATION
AttributesElement inputElement;
varyingsType.vmesh = VertMesh(inputMesh, inputElement);
return MotionVectorVS(varyingsType, inputMesh, inputPass, inputElement);
#else
varyingsType.vmesh = VertMesh(inputMesh);
return MotionVectorVS(varyingsType, inputMesh, inputPass);
#endif
}

#ifdef TESSELLATION_ON

PackedVaryingsToPS VertTesselation(VaryingsToDS input)
{
VaryingsToPS output;
output.vmesh = VertMeshTesselation(input.vmesh);
return MotionVectorTessellation(output, input);
}

#endif // TESSELLATION_ON

#else // _WRITE_TRANSPARENT_MOTION_VECTOR

#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl"

PackedVaryingsType Vert(AttributesMesh inputMesh)
{
VaryingsType varyingsType;

#if defined(HAVE_RECURSIVE_RENDERING)
// If we have a recursive raytrace object, we will not render it.
// As we don't want to rely on renderqueue to exclude the object from the list,
// we cull it by settings position to NaN value.
// TODO: provide a solution to filter dyanmically recursive raytrace object in the DrawRenderer
if (_EnableRecursiveRayTracing && _RayTracing > 0.0)
{
ZERO_INITIALIZE(VaryingsType, varyingsType); // Divide by 0 should produce a NaN and thus cull the primitive.
}
else
#endif
{
varyingsType.vmesh = VertMesh(inputMesh);
}

return PackVaryingsType(varyingsType);
}

#ifdef TESSELLATION_ON

PackedVaryingsToPS VertTesselation(VaryingsToDS input)
{
VaryingsToPS output;
output.vmesh = VertMeshTesselation(input.vmesh);

return PackVaryingsToPS(output);
}

#endif // TESSELLATION_ON

#endif // _WRITE_TRANSPARENT_MOTION_VECTOR


#ifdef TESSELLATION_ON
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/TessellationShare.hlsl"
#endif

#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplayMaterial.hlsl"

//NOTE: some shaders set target1 to be
// Blend 1 SrcAlpha OneMinusSrcAlpha
//The reason for this blend mode is to let virtual texturing alpha dither work.
//Anything using Target1 should write 1.0 or 0.0 in alpha to write / not write into the target.

#ifdef UNITY_VIRTUAL_TEXTURING
#ifdef OUTPUT_SPLIT_LIGHTING
#define DIFFUSE_LIGHTING_TARGET SV_Target2
#define SSS_BUFFER_TARGET SV_Target3
#elif defined(_WRITE_TRANSPARENT_MOTION_VECTOR)
#define MOTION_VECTOR_TARGET SV_Target2
#endif
#if defined(SHADER_API_PSSL)
//For exact packing on pssl, we want to write exact 16 bit unorm (respect exact bit packing).
//In some sony platforms, the default is FMT_16_ABGR, which would incur in loss of precision.
//Thus, when VT is enabled, we force FMT_32_ABGR
#pragma PSSL_target_output_format(target 1 FMT_32_ABGR)
#endif
#else
#ifdef OUTPUT_SPLIT_LIGHTING
#define DIFFUSE_LIGHTING_TARGET SV_Target1
#define SSS_BUFFER_TARGET SV_Target2
#elif defined(_WRITE_TRANSPARENT_MOTION_VECTOR)
#define MOTION_VECTOR_TARGET SV_Target1
#endif
#endif

[earlydepthstencil]
void Frag(PackedVaryingsToPS packedInput
, out float4 outColor : SV_Target0 // outSpecularLighting when outputting split lighting
#ifdef UNITY_VIRTUAL_TEXTURING
, out float4 outVTFeedback : SV_Target1
#endif
#ifdef OUTPUT_SPLIT_LIGHTING
, out float4 outDiffuseLighting : DIFFUSE_LIGHTING_TARGET
, OUTPUT_SSSBUFFER(outSSSBuffer) : SSS_BUFFER_TARGET
#elif defined(_WRITE_TRANSPARENT_MOTION_VECTOR)
, out float4 outMotionVec : MOTION_VECTOR_TARGET
#endif
#ifdef _DEPTHOFFSET_ON
, out float outputDepth : DEPTH_OFFSET_SEMANTIC
#endif
, uint uSampleIdx : SV_SampleIndex
)
{
#ifdef _WRITE_TRANSPARENT_MOTION_VECTOR
// Init outMotionVector here to solve compiler warning (potentially unitialized variable)
// It is init to the value of forceNoMotion (with 2.0)
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
// motion vector expected output format is RG16
outMotionVec = float4(2.0, 0.0, 0.0, 1.0);
#endif

UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput);
FragInputs input = UnpackVaryingsToFragInputs(packedInput);

AdjustFragInputsToOffScreenRendering(input, _OffScreenRendering > 0, _OffScreenDownsampleFactor);

uint2 tileIndex = uint2(input.positionSS.xy) / GetTileSize();

// input.positionSS is SV_Position
PositionInputs posInput = GetPositionInput(input.positionSS.xy, _ScreenSize.zw, input.positionSS.z, input.positionSS.w, input.positionRWS.xyz, tileIndex);

#ifdef VARYINGS_NEED_POSITION_WS
float3 V = GetWorldSpaceNormalizeViewDir(input.positionRWS);
#else
// Unused
float3 V = float3(1.0, 1.0, 1.0); // Avoid the division by 0
#endif

SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);

BSDFData bsdfData = ConvertSurfaceDataToBSDFData(input.positionSS.xy, surfaceData);

PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);

outColor = float4(0.0, 0.0, 0.0, 0.0);

// We need to skip lighting when doing debug pass because the debug pass is done before lighting so some buffers may not be properly initialized potentially causing crashes on PS4.

#ifdef DEBUG_DISPLAY
// Init in debug display mode to quiet warning
#ifdef OUTPUT_SPLIT_LIGHTING
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
// Diffuse output is expected to be RGB10, so alpha must always be 1 to ensure it is written.
outDiffuseLighting = float4(0, 0, 0, 1);
ENCODE_INTO_SSSBUFFER(surfaceData, posInput.positionSS, outSSSBuffer);
#endif

bool viewMaterial = GetMaterialDebugColor(outColor, input, builtinData, posInput, surfaceData, bsdfData);

if (!viewMaterial)
{
if (_DebugFullScreenMode == FULLSCREENDEBUGMODE_VALIDATE_DIFFUSE_COLOR || _DebugFullScreenMode == FULLSCREENDEBUGMODE_VALIDATE_SPECULAR_COLOR)
{
float3 result = float3(0.0, 0.0, 0.0);

GetPBRValidatorDebug(surfaceData, result);

outColor = float4(result, 1.0f);
}
else if (_DebugFullScreenMode == FULLSCREENDEBUGMODE_TRANSPARENCY_OVERDRAW)
{
float4 result = _DebugTransparencyOverdrawWeight * float4(TRANSPARENCY_OVERDRAW_COST, TRANSPARENCY_OVERDRAW_COST, TRANSPARENCY_OVERDRAW_COST, TRANSPARENCY_OVERDRAW_A);
outColor = result;
}
else
#endif
{
#ifdef _SURFACE_TYPE_TRANSPARENT
uint featureFlags = LIGHT_FEATURE_MASK_FLAGS_TRANSPARENT;
#else
uint featureFlags = LIGHT_FEATURE_MASK_FLAGS_OPAQUE;
#endif
LightLoopOutput lightLoopOutput;
LightLoop(V, posInput, preLightData, bsdfData, builtinData, featureFlags, lightLoopOutput);

// Alias
float3 diffuseLighting = lightLoopOutput.diffuseLighting;
float3 specularLighting = lightLoopOutput.specularLighting;

diffuseLighting *= GetCurrentExposureMultiplier();
specularLighting *= GetCurrentExposureMultiplier();

#ifdef OUTPUT_SPLIT_LIGHTING
if (_EnableSubsurfaceScattering != 0 && ShouldOutputSplitLighting(bsdfData))
{
outColor = float4(specularLighting, 1.0);
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
// Diffuse output is expected to be RGB10, so alpha must always be 1 to ensure it is written.
outDiffuseLighting = float4(TagLightingForSSS(diffuseLighting), 1.0);
}
else
{
outColor = float4(diffuseLighting + specularLighting, 1.0);
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
// Diffuse output is expected to be RGB10, so alpha must always be 1 to ensure it is written.
outDiffuseLighting = float4(0, 0, 0, 1);
}
ENCODE_INTO_SSSBUFFER(surfaceData, posInput.positionSS, outSSSBuffer);
#else
outColor = ApplyBlendMode(diffuseLighting, specularLighting, builtinData.opacity);
outColor = EvaluateAtmosphericScattering(posInput, V, outColor);
#endif

#ifdef _WRITE_TRANSPARENT_MOTION_VECTOR
VaryingsPassToPS inputPass = UnpackVaryingsPassToPS(packedInput.vpass);
bool forceNoMotion = any(unity_MotionVectorsParams.yw == 0.0);
// outMotionVec is already initialize at the value of forceNoMotion (see above)

//Motion vector is enabled in SG but not active in VFX
#if defined(HAVE_VFX_MODIFICATION) && !VFX_FEATURE_MOTION_VECTORS
forceNoMotion = true;
#endif

if (!forceNoMotion)
{
float2 motionVec = CalculateMotionVector(inputPass.positionCS, inputPass.previousPositionCS);
EncodeMotionVector(motionVec * 0.5, outMotionVec);

// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
// motion vector expected output format is RG16
outMotionVec.zw = 1.0;
}
#endif
}

#ifdef DEBUG_DISPLAY
}
#endif

#ifdef _DEPTHOFFSET_ON
outputDepth = posInput.deviceDepth;
#endif

#ifdef UNITY_VIRTUAL_TEXTURING
float vtAlphaValue = builtinData.opacity;
#if defined(HAS_REFRACTION) && HAS_REFRACTION
vtAlphaValue = 1.0f - bsdfData.transmittanceMask;
#endif
outVTFeedback = PackVTFeedbackWithAlpha(builtinData.vtPackedFeedback, input.positionSS.xy, vtAlphaValue);
#endif

createFragmentEntry(float4(outColor.rgb, builtinData.opacity), packedInput.vmesh.positionCS.xyz, uSampleIdx);
}
7 changes: 7 additions & 0 deletions HDRP/Shaders/OitShaderPassForwardHDRP.hlsl.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion PostProcessingStackV2/Runtime/OitPostProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class OitPostProcessRenderer : PostProcessEffectRenderer<OitPostProcess
public override void Init()
{
base.Init();
orderIndependentTransparency ??= new OitLinkedList("OitRenderPPv2");
orderIndependentTransparency ??= new OitLinkedList("OitRender");
Camera.onPreRender += PreRender;
}

Expand Down
8 changes: 8 additions & 0 deletions PostProcessingStackV2/Shaders.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Shader "OrderIndependentTransparency/Standard"

SubShader
{
PackageRequirements {
"org.happy-turtle.order-independent-transparency"
}

Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }


Expand All @@ -69,7 +73,7 @@ Shader "OrderIndependentTransparency/Standard"
Cull Off

CGPROGRAM
#pragma target 5.0
#pragma target 4.5

// -------------------------------------

Expand All @@ -91,7 +95,6 @@ Shader "OrderIndependentTransparency/Standard"

#pragma vertex vertBase
#pragma fragment fragBase
#pragma require randomwrite
// #pragma enable_d3d11_debug_symbols
#include "OitStandardCoreForward.cginc"

Expand All @@ -106,7 +109,7 @@ Shader "OrderIndependentTransparency/Standard"
ZWrite On ZTest LEqual

CGPROGRAM
#pragma target 5.0
#pragma target 4.5

// -------------------------------------

Expand Down Expand Up @@ -137,7 +140,7 @@ Shader "OrderIndependentTransparency/Standard"
Cull Off

CGPROGRAM
#pragma target 5.0
#pragma target 4.5
#pragma exclude_renderers nomrt


Expand Down Expand Up @@ -190,5 +193,5 @@ Shader "OrderIndependentTransparency/Standard"
}
}

FallBack "Standard"
FallBack "OrderIndependentTransparency/Unlit"
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#if UNITY_STANDARD_SIMPLE
#include "UnityStandardCoreForwardSimple.cginc"
#include "LinkedListCreation.hlsl"
#include "Packages/org.happy-turtle.order-independent-transparency/Shaders/LinkedListCreation.hlsl"
VertexOutputBaseSimple vertBase (VertexInput v) { return vertForwardBaseSimple(v); }
VertexOutputForwardAddSimple vertAdd (VertexInput v) { return vertForwardAddSimple(v); }
[earlydepthstencil]
Expand All @@ -30,7 +30,7 @@
}
#else
#include "UnityStandardCore.cginc"
#include "LinkedListCreation.hlsl"
#include "Packages/org.happy-turtle.order-independent-transparency/Shaders/LinkedListCreation.hlsl"
VertexOutputForwardBase vertBase (VertexInput v) { return vertForwardBase(v); }
VertexOutputForwardAdd vertAdd (VertexInput v) { return vertForwardAdd(v); }
[earlydepthstencil]
Expand Down
File renamed without changes.
Loading

0 comments on commit 975b5bb

Please sign in to comment.