Skip to content

Commit 975b5bb

Browse files
authored
feat(hdrp): add sample Lit shader (#37)
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
1 parent 45a0672 commit 975b5bb

32 files changed

+13293
-283
lines changed

HDRP/Runtime/OitRenderPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class OitRenderPass : CustomPass
1212

1313
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
1414
{
15-
orderIndependentTransparency ??= new OitLinkedList("OitRenderHDRP");
15+
orderIndependentTransparency ??= new OitLinkedList("OitRender");
1616
}
1717

1818
protected override void Execute(CustomPassContext ctx)

HDRP/Shaders.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HDRP/Shaders/OitLitHDRP.shader

Lines changed: 11974 additions & 0 deletions
Large diffs are not rendered by default.

Shaders/Resources/OitRenderURP.shader.meta renamed to HDRP/Shaders/OitLitHDRP.shader.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
#if SHADERPASS != SHADERPASS_FORWARD
2+
#error SHADERPASS_is_not_correctly_define
3+
#endif
4+
5+
#include "Packages/org.happy-turtle.order-independent-transparency/Shaders/LinkedListCreation.hlsl"
6+
7+
#ifdef _WRITE_TRANSPARENT_MOTION_VECTOR
8+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/MotionVectorVertexShaderCommon.hlsl"
9+
10+
PackedVaryingsType Vert(AttributesMesh inputMesh, AttributesPass inputPass)
11+
{
12+
VaryingsType varyingsType;
13+
#ifdef HAVE_VFX_MODIFICATION
14+
AttributesElement inputElement;
15+
varyingsType.vmesh = VertMesh(inputMesh, inputElement);
16+
return MotionVectorVS(varyingsType, inputMesh, inputPass, inputElement);
17+
#else
18+
varyingsType.vmesh = VertMesh(inputMesh);
19+
return MotionVectorVS(varyingsType, inputMesh, inputPass);
20+
#endif
21+
}
22+
23+
#ifdef TESSELLATION_ON
24+
25+
PackedVaryingsToPS VertTesselation(VaryingsToDS input)
26+
{
27+
VaryingsToPS output;
28+
output.vmesh = VertMeshTesselation(input.vmesh);
29+
return MotionVectorTessellation(output, input);
30+
}
31+
32+
#endif // TESSELLATION_ON
33+
34+
#else // _WRITE_TRANSPARENT_MOTION_VECTOR
35+
36+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl"
37+
38+
PackedVaryingsType Vert(AttributesMesh inputMesh)
39+
{
40+
VaryingsType varyingsType;
41+
42+
#if defined(HAVE_RECURSIVE_RENDERING)
43+
// If we have a recursive raytrace object, we will not render it.
44+
// As we don't want to rely on renderqueue to exclude the object from the list,
45+
// we cull it by settings position to NaN value.
46+
// TODO: provide a solution to filter dyanmically recursive raytrace object in the DrawRenderer
47+
if (_EnableRecursiveRayTracing && _RayTracing > 0.0)
48+
{
49+
ZERO_INITIALIZE(VaryingsType, varyingsType); // Divide by 0 should produce a NaN and thus cull the primitive.
50+
}
51+
else
52+
#endif
53+
{
54+
varyingsType.vmesh = VertMesh(inputMesh);
55+
}
56+
57+
return PackVaryingsType(varyingsType);
58+
}
59+
60+
#ifdef TESSELLATION_ON
61+
62+
PackedVaryingsToPS VertTesselation(VaryingsToDS input)
63+
{
64+
VaryingsToPS output;
65+
output.vmesh = VertMeshTesselation(input.vmesh);
66+
67+
return PackVaryingsToPS(output);
68+
}
69+
70+
#endif // TESSELLATION_ON
71+
72+
#endif // _WRITE_TRANSPARENT_MOTION_VECTOR
73+
74+
75+
#ifdef TESSELLATION_ON
76+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/TessellationShare.hlsl"
77+
#endif
78+
79+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplayMaterial.hlsl"
80+
81+
//NOTE: some shaders set target1 to be
82+
// Blend 1 SrcAlpha OneMinusSrcAlpha
83+
//The reason for this blend mode is to let virtual texturing alpha dither work.
84+
//Anything using Target1 should write 1.0 or 0.0 in alpha to write / not write into the target.
85+
86+
#ifdef UNITY_VIRTUAL_TEXTURING
87+
#ifdef OUTPUT_SPLIT_LIGHTING
88+
#define DIFFUSE_LIGHTING_TARGET SV_Target2
89+
#define SSS_BUFFER_TARGET SV_Target3
90+
#elif defined(_WRITE_TRANSPARENT_MOTION_VECTOR)
91+
#define MOTION_VECTOR_TARGET SV_Target2
92+
#endif
93+
#if defined(SHADER_API_PSSL)
94+
//For exact packing on pssl, we want to write exact 16 bit unorm (respect exact bit packing).
95+
//In some sony platforms, the default is FMT_16_ABGR, which would incur in loss of precision.
96+
//Thus, when VT is enabled, we force FMT_32_ABGR
97+
#pragma PSSL_target_output_format(target 1 FMT_32_ABGR)
98+
#endif
99+
#else
100+
#ifdef OUTPUT_SPLIT_LIGHTING
101+
#define DIFFUSE_LIGHTING_TARGET SV_Target1
102+
#define SSS_BUFFER_TARGET SV_Target2
103+
#elif defined(_WRITE_TRANSPARENT_MOTION_VECTOR)
104+
#define MOTION_VECTOR_TARGET SV_Target1
105+
#endif
106+
#endif
107+
108+
[earlydepthstencil]
109+
void Frag(PackedVaryingsToPS packedInput
110+
, out float4 outColor : SV_Target0 // outSpecularLighting when outputting split lighting
111+
#ifdef UNITY_VIRTUAL_TEXTURING
112+
, out float4 outVTFeedback : SV_Target1
113+
#endif
114+
#ifdef OUTPUT_SPLIT_LIGHTING
115+
, out float4 outDiffuseLighting : DIFFUSE_LIGHTING_TARGET
116+
, OUTPUT_SSSBUFFER(outSSSBuffer) : SSS_BUFFER_TARGET
117+
#elif defined(_WRITE_TRANSPARENT_MOTION_VECTOR)
118+
, out float4 outMotionVec : MOTION_VECTOR_TARGET
119+
#endif
120+
#ifdef _DEPTHOFFSET_ON
121+
, out float outputDepth : DEPTH_OFFSET_SEMANTIC
122+
#endif
123+
, uint uSampleIdx : SV_SampleIndex
124+
)
125+
{
126+
#ifdef _WRITE_TRANSPARENT_MOTION_VECTOR
127+
// Init outMotionVector here to solve compiler warning (potentially unitialized variable)
128+
// It is init to the value of forceNoMotion (with 2.0)
129+
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
130+
// motion vector expected output format is RG16
131+
outMotionVec = float4(2.0, 0.0, 0.0, 1.0);
132+
#endif
133+
134+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput);
135+
FragInputs input = UnpackVaryingsToFragInputs(packedInput);
136+
137+
AdjustFragInputsToOffScreenRendering(input, _OffScreenRendering > 0, _OffScreenDownsampleFactor);
138+
139+
uint2 tileIndex = uint2(input.positionSS.xy) / GetTileSize();
140+
141+
// input.positionSS is SV_Position
142+
PositionInputs posInput = GetPositionInput(input.positionSS.xy, _ScreenSize.zw, input.positionSS.z, input.positionSS.w, input.positionRWS.xyz, tileIndex);
143+
144+
#ifdef VARYINGS_NEED_POSITION_WS
145+
float3 V = GetWorldSpaceNormalizeViewDir(input.positionRWS);
146+
#else
147+
// Unused
148+
float3 V = float3(1.0, 1.0, 1.0); // Avoid the division by 0
149+
#endif
150+
151+
SurfaceData surfaceData;
152+
BuiltinData builtinData;
153+
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
154+
155+
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(input.positionSS.xy, surfaceData);
156+
157+
PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);
158+
159+
outColor = float4(0.0, 0.0, 0.0, 0.0);
160+
161+
// 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.
162+
163+
#ifdef DEBUG_DISPLAY
164+
// Init in debug display mode to quiet warning
165+
#ifdef OUTPUT_SPLIT_LIGHTING
166+
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
167+
// Diffuse output is expected to be RGB10, so alpha must always be 1 to ensure it is written.
168+
outDiffuseLighting = float4(0, 0, 0, 1);
169+
ENCODE_INTO_SSSBUFFER(surfaceData, posInput.positionSS, outSSSBuffer);
170+
#endif
171+
172+
bool viewMaterial = GetMaterialDebugColor(outColor, input, builtinData, posInput, surfaceData, bsdfData);
173+
174+
if (!viewMaterial)
175+
{
176+
if (_DebugFullScreenMode == FULLSCREENDEBUGMODE_VALIDATE_DIFFUSE_COLOR || _DebugFullScreenMode == FULLSCREENDEBUGMODE_VALIDATE_SPECULAR_COLOR)
177+
{
178+
float3 result = float3(0.0, 0.0, 0.0);
179+
180+
GetPBRValidatorDebug(surfaceData, result);
181+
182+
outColor = float4(result, 1.0f);
183+
}
184+
else if (_DebugFullScreenMode == FULLSCREENDEBUGMODE_TRANSPARENCY_OVERDRAW)
185+
{
186+
float4 result = _DebugTransparencyOverdrawWeight * float4(TRANSPARENCY_OVERDRAW_COST, TRANSPARENCY_OVERDRAW_COST, TRANSPARENCY_OVERDRAW_COST, TRANSPARENCY_OVERDRAW_A);
187+
outColor = result;
188+
}
189+
else
190+
#endif
191+
{
192+
#ifdef _SURFACE_TYPE_TRANSPARENT
193+
uint featureFlags = LIGHT_FEATURE_MASK_FLAGS_TRANSPARENT;
194+
#else
195+
uint featureFlags = LIGHT_FEATURE_MASK_FLAGS_OPAQUE;
196+
#endif
197+
LightLoopOutput lightLoopOutput;
198+
LightLoop(V, posInput, preLightData, bsdfData, builtinData, featureFlags, lightLoopOutput);
199+
200+
// Alias
201+
float3 diffuseLighting = lightLoopOutput.diffuseLighting;
202+
float3 specularLighting = lightLoopOutput.specularLighting;
203+
204+
diffuseLighting *= GetCurrentExposureMultiplier();
205+
specularLighting *= GetCurrentExposureMultiplier();
206+
207+
#ifdef OUTPUT_SPLIT_LIGHTING
208+
if (_EnableSubsurfaceScattering != 0 && ShouldOutputSplitLighting(bsdfData))
209+
{
210+
outColor = float4(specularLighting, 1.0);
211+
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
212+
// Diffuse output is expected to be RGB10, so alpha must always be 1 to ensure it is written.
213+
outDiffuseLighting = float4(TagLightingForSSS(diffuseLighting), 1.0);
214+
}
215+
else
216+
{
217+
outColor = float4(diffuseLighting + specularLighting, 1.0);
218+
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
219+
// Diffuse output is expected to be RGB10, so alpha must always be 1 to ensure it is written.
220+
outDiffuseLighting = float4(0, 0, 0, 1);
221+
}
222+
ENCODE_INTO_SSSBUFFER(surfaceData, posInput.positionSS, outSSSBuffer);
223+
#else
224+
outColor = ApplyBlendMode(diffuseLighting, specularLighting, builtinData.opacity);
225+
outColor = EvaluateAtmosphericScattering(posInput, V, outColor);
226+
#endif
227+
228+
#ifdef _WRITE_TRANSPARENT_MOTION_VECTOR
229+
VaryingsPassToPS inputPass = UnpackVaryingsPassToPS(packedInput.vpass);
230+
bool forceNoMotion = any(unity_MotionVectorsParams.yw == 0.0);
231+
// outMotionVec is already initialize at the value of forceNoMotion (see above)
232+
233+
//Motion vector is enabled in SG but not active in VFX
234+
#if defined(HAVE_VFX_MODIFICATION) && !VFX_FEATURE_MOTION_VECTORS
235+
forceNoMotion = true;
236+
#endif
237+
238+
if (!forceNoMotion)
239+
{
240+
float2 motionVec = CalculateMotionVector(inputPass.positionCS, inputPass.previousPositionCS);
241+
EncodeMotionVector(motionVec * 0.5, outMotionVec);
242+
243+
// Always write 1.0 in alpha since blend mode could be active on this target as a side effect of VT feedback buffer
244+
// motion vector expected output format is RG16
245+
outMotionVec.zw = 1.0;
246+
}
247+
#endif
248+
}
249+
250+
#ifdef DEBUG_DISPLAY
251+
}
252+
#endif
253+
254+
#ifdef _DEPTHOFFSET_ON
255+
outputDepth = posInput.deviceDepth;
256+
#endif
257+
258+
#ifdef UNITY_VIRTUAL_TEXTURING
259+
float vtAlphaValue = builtinData.opacity;
260+
#if defined(HAS_REFRACTION) && HAS_REFRACTION
261+
vtAlphaValue = 1.0f - bsdfData.transmittanceMask;
262+
#endif
263+
outVTFeedback = PackVTFeedbackWithAlpha(builtinData.vtPackedFeedback, input.positionSS.xy, vtAlphaValue);
264+
#endif
265+
266+
createFragmentEntry(float4(outColor.rgb, builtinData.opacity), packedInput.vmesh.positionCS.xyz, uSampleIdx);
267+
}

HDRP/Shaders/OitShaderPassForwardHDRP.hlsl.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PostProcessingStackV2/Runtime/OitPostProcess.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class OitPostProcessRenderer : PostProcessEffectRenderer<OitPostProcess
1919
public override void Init()
2020
{
2121
base.Init();
22-
orderIndependentTransparency ??= new OitLinkedList("OitRenderPPv2");
22+
orderIndependentTransparency ??= new OitLinkedList("OitRender");
2323
Camera.onPreRender += PreRender;
2424
}
2525

PostProcessingStackV2/Shaders.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shaders/OitStandard.shader renamed to PostProcessingStackV2/Shaders/OitStandard.shader

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Shader "OrderIndependentTransparency/Standard"
5353

5454
SubShader
5555
{
56+
PackageRequirements {
57+
"org.happy-turtle.order-independent-transparency"
58+
}
59+
5660
Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
5761

5862

@@ -69,7 +73,7 @@ Shader "OrderIndependentTransparency/Standard"
6973
Cull Off
7074

7175
CGPROGRAM
72-
#pragma target 5.0
76+
#pragma target 4.5
7377

7478
// -------------------------------------
7579

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

9296
#pragma vertex vertBase
9397
#pragma fragment fragBase
94-
#pragma require randomwrite
9598
// #pragma enable_d3d11_debug_symbols
9699
#include "OitStandardCoreForward.cginc"
97100

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

108111
CGPROGRAM
109-
#pragma target 5.0
112+
#pragma target 4.5
110113

111114
// -------------------------------------
112115

@@ -137,7 +140,7 @@ Shader "OrderIndependentTransparency/Standard"
137140
Cull Off
138141

139142
CGPROGRAM
140-
#pragma target 5.0
143+
#pragma target 4.5
141144
#pragma exclude_renderers nomrt
142145

143146

@@ -190,5 +193,5 @@ Shader "OrderIndependentTransparency/Standard"
190193
}
191194
}
192195

193-
FallBack "Standard"
196+
FallBack "OrderIndependentTransparency/Unlit"
194197
}

Shaders/OitStandardCoreForward.cginc renamed to PostProcessingStackV2/Shaders/OitStandardCoreForward.cginc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#if UNITY_STANDARD_SIMPLE
1313
#include "UnityStandardCoreForwardSimple.cginc"
14-
#include "LinkedListCreation.hlsl"
14+
#include "Packages/org.happy-turtle.order-independent-transparency/Shaders/LinkedListCreation.hlsl"
1515
VertexOutputBaseSimple vertBase (VertexInput v) { return vertForwardBaseSimple(v); }
1616
VertexOutputForwardAddSimple vertAdd (VertexInput v) { return vertForwardAddSimple(v); }
1717
[earlydepthstencil]
@@ -30,7 +30,7 @@
3030
}
3131
#else
3232
#include "UnityStandardCore.cginc"
33-
#include "LinkedListCreation.hlsl"
33+
#include "Packages/org.happy-turtle.order-independent-transparency/Shaders/LinkedListCreation.hlsl"
3434
VertexOutputForwardBase vertBase (VertexInput v) { return vertForwardBase(v); }
3535
VertexOutputForwardAdd vertAdd (VertexInput v) { return vertForwardAdd(v); }
3636
[earlydepthstencil]

0 commit comments

Comments
 (0)