Skip to content

Commit 6477c14

Browse files
committed
Divide files by pass
1 parent c115442 commit 6477c14

18 files changed

+335
-840
lines changed

Assets/Raymarching/Shaders/Common.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.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#if SHADERPASS != SHADERPASS_GBUFFER
2+
#error SHADERPASS_is_not_correctly_define
3+
#endif
4+
5+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl"
6+
7+
PackedVaryingsType Vert(AttributesMesh inputMesh)
8+
{
9+
VaryingsType varyingsType;
10+
varyingsType.vmesh = VertMesh(inputMesh);
11+
return PackVaryingsType(varyingsType);
12+
}
13+
14+
#ifdef TESSELLATION_ON
15+
16+
PackedVaryingsToPS VertTesselation(VaryingsToDS input)
17+
{
18+
VaryingsToPS output;
19+
output.vmesh = VertMeshTesselation(input.vmesh);
20+
return PackVaryingsToPS(output);
21+
}
22+
23+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/TessellationShare.hlsl"
24+
25+
#endif // TESSELLATION_ON
26+
27+
#define _DEPTHOFFSET_ON
28+
29+
void Frag( PackedVaryingsToPS packedInput,
30+
OUTPUT_GBUFFER(outGBuffer)
31+
#ifdef _DEPTHOFFSET_ON
32+
, out float outputDepth : SV_Depth
33+
#endif
34+
)
35+
{
36+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput);
37+
FragInputs input = UnpackVaryingsMeshToFragInputs(packedInput.vmesh);
38+
39+
// input.positionSS is SV_Position
40+
PositionInputs posInput = GetPositionInput(input.positionSS.xy, _ScreenSize.zw, input.positionSS.z, input.positionSS.w, input.positionRWS);
41+
42+
#ifdef VARYINGS_NEED_POSITION_WS
43+
float3 V = GetWorldSpaceNormalizeViewDir(input.positionRWS);
44+
#else
45+
// Unused
46+
float3 V = float3(1.0, 1.0, 1.0); // Avoid the division by 0
47+
#endif
48+
49+
float3 ray = normalize(input.positionRWS);
50+
float3 pos = float3(0.0, 0.0, 0.0);
51+
52+
DistanceFunctionSurfaceData surface = Trace(pos, ray, GBUFFER_MARCHING_ITERATION);
53+
54+
SurfaceData surfaceData;
55+
BuiltinData builtinData;
56+
ToHDRPSurfaceAndBuiltinData(input, V, posInput, surface, surfaceData, builtinData);
57+
58+
float depth = WorldPosToDeviceDepth(surface.Position);
59+
60+
ENCODE_INTO_GBUFFER(surfaceData, builtinData, posInput.positionSS, outGBuffer);
61+
62+
#ifdef _DEPTHOFFSET_ON
63+
outputDepth = depth;
64+
#endif
65+
}

Assets/Raymarching/Shaders/Common/GBufferPass.hlsl.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#if SHADERPASS != SHADERPASS_MOTION_VECTORS
2+
#error SHADERPASS_is_not_correctly_define
3+
#endif
4+
5+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/MotionVectorVertexShaderCommon.hlsl"
6+
7+
PackedVaryingsType Vert(AttributesMesh inputMesh,
8+
AttributesPass inputPass)
9+
{
10+
VaryingsType varyingsType;
11+
varyingsType.vmesh = VertMesh(inputMesh);
12+
13+
return MotionVectorVS(varyingsType, inputMesh, inputPass);
14+
}
15+
16+
#ifdef TESSELLATION_ON
17+
18+
PackedVaryingsToPS VertTesselation(VaryingsToDS input)
19+
{
20+
VaryingsToPS output;
21+
22+
output.vmesh = VertMeshTesselation(input.vmesh);
23+
24+
MotionVectorPositionZBias(output);
25+
26+
output.vpass.positionCS = input.vpass.positionCS;
27+
output.vpass.previousPositionCS = input.vpass.previousPositionCS;
28+
29+
return PackVaryingsToPS(output);
30+
}
31+
32+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/TessellationShare.hlsl"
33+
34+
#endif // TESSELLATION_ON
35+
36+
#define _DEPTHOFFSET_ON
37+
void Frag( PackedVaryingsToPS packedInput
38+
// The motion vector is always the first buffer
39+
, out float4 outMotionVector : SV_Target0
40+
41+
// Write the normal buffer
42+
#ifdef WRITE_NORMAL_BUFFER
43+
, out float4 outNormalBuffer : SV_Target1
44+
// Output the depth as a color if required
45+
#ifdef WRITE_MSAA_DEPTH
46+
, out float1 depthColor : SV_Target2
47+
#endif
48+
#elif defined(WRITE_MSAA_DEPTH) // When only WRITE_MSAA_DEPTH is define and not WRITE_NORMAL_BUFFER it mean we are Unlit and only need depth, but we still have normal buffer binded
49+
, out float4 outNormalBuffer : SV_Target1
50+
, out float1 depthColor : SV_Target2
51+
#endif
52+
53+
#ifdef _DEPTHOFFSET_ON
54+
, out float outputDepth : SV_Depth
55+
#endif
56+
)
57+
{
58+
FragInputs input = UnpackVaryingsMeshToFragInputs(packedInput.vmesh);
59+
60+
// input.positionSS is SV_Position
61+
PositionInputs posInput = GetPositionInput(input.positionSS.xy, _ScreenSize.zw, input.positionSS.z, input.positionSS.w, input.positionRWS);
62+
63+
#ifdef VARYINGS_NEED_POSITION_WS
64+
float3 V = GetWorldSpaceNormalizeViewDir(input.positionRWS);
65+
#else
66+
// Unused
67+
float3 V = float3(1.0, 1.0, 1.0); // Avoid the division by 0
68+
#endif
69+
70+
float3 ray = normalize(input.positionRWS);
71+
float3 pos = float3(0.0, 0.0, 0.0);
72+
73+
float t = TraceDepth(pos, ray, MOTIONVECTORS_MARCHING_ITERATION);
74+
75+
pos = pos + ray * t;
76+
77+
float depth = WorldPosToDeviceDepth(pos);
78+
79+
VaryingsPassToPS inputPass = UnpackVaryingsPassToPS(packedInput.vpass);
80+
81+
inputPass.positionCS = TransformWorldToHClip(pos);
82+
float3 prevPosRWS = TransformPreviousObjectToWorld(TransformWorldToObject(pos));
83+
inputPass.previousPositionCS = mul(UNITY_MATRIX_PREV_VP, float4(prevPosRWS, 1.0));
84+
85+
// TODO: How to allow overriden motion vector from GetSurfaceAndBuiltinData ?
86+
float2 motionVector = CalculateMotionVector(inputPass.positionCS, inputPass.previousPositionCS);
87+
88+
// Convert from Clip space (-1..1) to NDC 0..1 space.
89+
// Note it doesn't mean we don't have negative value, we store negative or positive offset in NDC space.
90+
// Note: ((positionCS * 0.5 + 0.5) - (previousPositionCS * 0.5 + 0.5)) = (motionVector * 0.5)
91+
EncodeMotionVector(motionVector * 0.5, outMotionVector);
92+
93+
// Note: unity_MotionVectorsParams.y is 0 is forceNoMotion is enabled
94+
bool forceNoMotion = unity_MotionVectorsParams.y == 0.0;
95+
96+
// Setting the motionVector to a value more than 2 set as a flag for "force no motion". This is valid because, given that the velocities are in NDC,
97+
// a value of >1 can never happen naturally, unless explicitely set.
98+
if (forceNoMotion)
99+
outMotionVector = float4(2.0, 0.0, 0.0, 0.0);
100+
101+
// Normal Buffer Processing
102+
#ifdef WRITE_NORMAL_BUFFER
103+
EncodeIntoNormalBuffer(ConvertSurfaceDataToNormalData(surfaceData), posInput.positionSS, outNormalBuffer);
104+
105+
#ifdef WRITE_MSAA_DEPTH
106+
// In case we are rendering in MSAA, reading the an MSAA depth buffer is way too expensive. To avoid that, we export the depth to a color buffer
107+
depthColor = depth;
108+
#endif
109+
#elif defined(WRITE_MSAA_DEPTH) // When we are MSAA depth only without normal buffer
110+
// Due to the binding order of these three render targets, we need to have them both declared
111+
outNormalBuffer = float4(0.0, 0.0, 0.0, 1.0);
112+
// In case we are rendering in MSAA, reading the an MSAA depth buffer is way too expensive. To avoid that, we export the depth to a color buffer
113+
depthColor = depth;
114+
#endif
115+
116+
#ifdef _DEPTHOFFSET_ON
117+
outputDepth = depth;
118+
#endif
119+
}

Assets/Raymarching/Shaders/Common/MotionVectorsPass.hlsl.meta

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

Assets/Raymarching/Shaders/RaymarchingUtility.hlsl renamed to Assets/Raymarching/Shaders/Common/RaymarchingUtility.hlsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define PI 3.141592
21
#define PI2 (PI*2.0)
32
#define mod(x, y) ((x) - (y) * floor((x) / (y)))
43

Assets/Raymarching/Shaders/RaymarchingUtility.hlsl.meta renamed to Assets/Raymarching/Shaders/Common/RaymarchingUtility.hlsl.meta

File renamed without changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#if (SHADERPASS != SHADERPASS_DEPTH_ONLY && SHADERPASS != SHADERPASS_SHADOWS)
2+
#error SHADERPASS_is_not_correctly_define
3+
#endif
4+
5+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl"
6+
7+
PackedVaryingsType Vert(AttributesMesh inputMesh)
8+
{
9+
VaryingsType varyingsType;
10+
varyingsType.vmesh = VertMesh(inputMesh);
11+
varyingsType.vmesh.positionCS.z = 0.0;
12+
return PackVaryingsType(varyingsType);
13+
}
14+
15+
#ifdef TESSELLATION_ON
16+
17+
PackedVaryingsToPS VertTesselation(VaryingsToDS input)
18+
{
19+
VaryingsToPS output;
20+
output.vmesh = VertMeshTesselation(input.vmesh);
21+
return PackVaryingsToPS(output);
22+
}
23+
24+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/TessellationShare.hlsl"
25+
26+
#endif // TESSELLATION_ON
27+
28+
#define _DEPTHOFFSET_ON
29+
void Frag( PackedVaryingsToPS packedInput
30+
#ifdef WRITE_NORMAL_BUFFER
31+
, out float4 outNormalBuffer : SV_Target0
32+
#ifdef WRITE_MSAA_DEPTH
33+
, out float1 depthColor : SV_Target1
34+
#endif
35+
#elif defined(WRITE_MSAA_DEPTH) // When only WRITE_MSAA_DEPTH is define and not WRITE_NORMAL_BUFFER it mean we are Unlit and only need depth, but we still have normal buffer binded
36+
, out float4 outNormalBuffer : SV_Target0
37+
, out float1 depthColor : SV_Target1
38+
#elif defined(SCENESELECTIONPASS)
39+
, out float4 outColor : SV_Target0
40+
#endif
41+
42+
#ifdef _DEPTHOFFSET_ON
43+
, out float outputDepth : SV_Depth
44+
#endif
45+
)
46+
{
47+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput);
48+
FragInputs input = UnpackVaryingsMeshToFragInputs(packedInput.vmesh);
49+
50+
// input.positionSS is SV_Position
51+
PositionInputs posInput = GetPositionInput(input.positionSS.xy, _ScreenSize.zw, input.positionSS.z, input.positionSS.w, input.positionRWS);
52+
53+
#ifdef VARYINGS_NEED_POSITION_WS
54+
float3 V = GetWorldSpaceNormalizeViewDir(input.positionRWS);
55+
#else
56+
// Unused
57+
float3 V = float3(1.0, 1.0, 1.0); // Avoid the division by 0
58+
#endif
59+
60+
float3 pos = GetShadowRayOrigin(input.positionRWS);
61+
float3 ray = -V;
62+
63+
float t = TraceDepth(pos, ray, SHADOWCASTER_MARCHING_ITERATION);
64+
65+
float depth = WorldPosToDeviceDepth(pos + ray * t);
66+
67+
#ifdef _DEPTHOFFSET_ON
68+
outputDepth = depth;
69+
#endif
70+
71+
#ifdef WRITE_NORMAL_BUFFER
72+
EncodeIntoNormalBuffer(ConvertSurfaceDataToNormalData(surfaceData), posInput.positionSS, outNormalBuffer);
73+
#ifdef WRITE_MSAA_DEPTH
74+
// In case we are rendering in MSAA, reading the an MSAA depth buffer is way too expensive. To avoid that, we export the depth to a color buffer
75+
depthColor = depth;
76+
#endif
77+
#elif defined(WRITE_MSAA_DEPTH) // When we are MSAA depth only without normal buffer
78+
// Due to the binding order of these two render targets, we need to have them both declared
79+
outNormalBuffer = float4(0.0, 0.0, 0.0, 1.0);
80+
// In case we are rendering in MSAA, reading the an MSAA depth buffer is way too expensive. To avoid that, we export the depth to a color buffer
81+
depthColor = depth;
82+
#elif defined(SCENESELECTIONPASS)
83+
// We use depth prepass for scene selection in the editor, this code allow to output the outline correctly
84+
outColor = float4(_ObjectId, _PassValue, 1.0, 1.0);
85+
#endif
86+
}

Assets/Raymarching/Shaders/Common/ShadowCasterPass.hlsl.meta

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

Assets/Raymarching/Shaders/Field/DF.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#define GBUFFER_MARCHING_ITERATION 300
2+
#define SHADOWCASTER_MARCHING_ITERATION 99
3+
#define MOTIONVECTORS_MARCHING_ITERATION 99
4+
15
// https://gam0022.net/blog/2019/06/25/unity-raymarching/
26
float dMenger(float3 z0, float3 offset, float scale) {
37
float4 z = float4(z0, 1.0);

0 commit comments

Comments
 (0)