Skip to content

Commit e9bf069

Browse files
adrien-de-tocquevilleEvergreen
authored andcommitted
[HDRP] Move water specific global shader variables to separate file
Move water specific global shader variables to separate file per user request. https://forum.unity.com/threads/water-system-for-the-high-definition-render-pipeline.1203751/page-15#post-9716743
1 parent da9a1e3 commit e9bf069

14 files changed

+308
-271
lines changed

Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesMatrixDefsHDCamera.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// Matrix overrides for water system
3737
#if defined(WATER_SURFACE_GBUFFER) || defined(WATER_ONE_BAND) || defined(WATER_TWO_BANDS) || defined(WATER_THREE_BANDS)
3838

39-
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/WaterSystemDef.cs.hlsl"
39+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/ShaderVariablesWater.cs.hlsl"
4040

4141
#define _WaterCustomMeshTransform GetRawUnityObjectToWorld()
4242
#define _WaterCustomMeshTransform_Inverse GetRawUnityWorldToObject()
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using Unity.Mathematics;
2+
3+
namespace UnityEngine.Rendering.HighDefinition
4+
{
5+
// This buffer contains surface data that mostly don't change
6+
[GenerateHLSL(needAccessors = false, generateCBuffer = true)]
7+
unsafe struct ShaderVariablesWaterPerSurface
8+
{
9+
// Transform of the water surface
10+
public Matrix4x4 _WaterSurfaceTransform;
11+
public Matrix4x4 _WaterSurfaceTransform_Inverse;
12+
13+
// Per band data
14+
public Vector4 _PatchOrientation;
15+
public Vector4 _PatchWindSpeed;
16+
public Vector4 _PatchDirectionDampener;
17+
public int4 _PatchGroup;
18+
19+
// Scale of the water mask
20+
public Vector2 _WaterMaskScale;
21+
// Offset of the water mask
22+
public Vector2 _WaterMaskOffset;
23+
// Remap range of the water mask
24+
public Vector2 _WaterMaskRemap;
25+
26+
// Per group data
27+
public float2 _GroupOrientation;
28+
29+
public Vector2 _DeformationRegionOffset;
30+
public Vector2 _DeformationRegionScale;
31+
32+
// Per band data
33+
public Vector4 _Band0_ScaleOffset_AmplitudeMultiplier;
34+
public Vector4 _Band1_ScaleOffset_AmplitudeMultiplier;
35+
public Vector4 _Band2_ScaleOffset_AmplitudeMultiplier;
36+
public float2 _Band0_Fade;
37+
public float2 _Band1_Fade;
38+
public float2 _Band2_Fade;
39+
40+
// Resolution at which the simulation is evaluated
41+
public uint _BandResolution;
42+
// Surface Index
43+
public int _SurfaceIndex;
44+
45+
// Scale of the foam mask
46+
public Vector2 _SimulationFoamMaskScale;
47+
// Offset of the foam mask
48+
public Vector2 _SimulationFoamMaskOffset;
49+
50+
// Foam Intensity
51+
public float _SimulationFoamIntensity;
52+
// Amount of surface foam
53+
public float _SimulationFoamAmount;
54+
// Foam region resolution
55+
public float _WaterFoamRegionResolution;
56+
// Foam Tiling
57+
public float _FoamTiling;
58+
59+
// Size of the foam region
60+
public Vector2 _FoamRegionScale;
61+
// Center of the foam region
62+
public Vector2 _FoamRegionOffset;
63+
64+
// Up direction of the water surface
65+
public float4 _WaterUpDirection;
66+
67+
// Extinction coefficient
68+
public float4 _WaterExtinction;
69+
70+
// Maximum refraction distance
71+
public float _MaxRefractionDistance;
72+
// Caustics data
73+
public float _CausticsRegionSize;
74+
// Caustic band index
75+
public int _CausticsBandIndex;
76+
public float _PaddingW2;
77+
78+
// Base color data
79+
public Vector4 _WaterAlbedo;
80+
81+
public float _AmbientScattering;
82+
public float _HeightBasedScattering;
83+
public float _DisplacementScattering;
84+
public float _ScatteringWaveHeight;
85+
86+
// Influence of current on foam scrolling
87+
public float _FoamCurrentInfluence;
88+
// Smoothness of the foam
89+
public float _FoamSmoothness;
90+
// Water smoothness
91+
public float _WaterSmoothness;
92+
// Controls the fade multiplier of the foam
93+
public float _FoamPersistenceMultiplier;
94+
95+
// Forward vector of water surface
96+
public float2 _WaterForwardXZ;
97+
// Deformation region resolution
98+
public int _DeformationRegionResolution;
99+
// Offset applied to the caustics LOD
100+
public float _CausticsMaxLOD;
101+
102+
// Tiling of the caustics texture
103+
public float _CausticsTilingFactor;
104+
// Intensity of the water caustics
105+
public float _CausticsIntensity;
106+
// Intensity of the water caustics in sun shadow
107+
public float _CausticsShadowIntensity;
108+
// Blend distance
109+
public float _CausticsPlaneBlendDistance;
110+
111+
// Maximal horizontal displacement
112+
public float _MaxWaveDisplacement;
113+
// Maximal wave height of the current setup
114+
public float _MaxWaveHeight;
115+
116+
// Current Map Influence
117+
public Vector2 _CurrentMapInfluence;
118+
119+
// Scale & offset of the large
120+
public Vector4 _Group0CurrentRegionScaleOffset;
121+
// Scale & offset of the ripples
122+
public Vector4 _Group1CurrentRegionScaleOffset;
123+
124+
// Which rendering layers should affect this surface - for decals
125+
public uint _WaterRenderingLayer;
126+
// Max tessellation factor
127+
public float _WaterMaxTessellationFactor;
128+
// Distance at which the fade of the tessellation starts
129+
public float _WaterTessellationFadeStart;
130+
// Size of the range of the tessellation
131+
public float _WaterTessellationFadeRange;
132+
133+
// This matrix is used for caustics in case of a custom mesh
134+
public Matrix4x4 _WaterCustomTransform_Inverse;
135+
136+
// Below are the only data that needs to be changed every frame
137+
// Currently the whole buffer is reupload anyway, but this should be changed
138+
139+
// Maximum horizontal deformation
140+
public float _MaxWaterDeformation;
141+
// Current simulation time
142+
public float _SimulationTime;
143+
// Delta-time since the last simulation step
144+
public float _DeltaTime;
145+
// Padding
146+
public float _PaddingW1;
147+
}
148+
149+
// This buffer contains surface data that vary per camera
150+
[GenerateHLSL(needAccessors = false, generateCBuffer = true)]
151+
unsafe struct ShaderVariablesWaterPerCamera
152+
{
153+
// Offset of the patch w/r to the origin. w is used to scale the low res water mesh
154+
public float2 _PatchOffset;
155+
// Horizontal size of the grid in the horizontal plane
156+
public float2 _GridSize;
157+
158+
// Size of the quad in world space (to cull non-infinite instanced quads)
159+
public float2 _RegionExtent;
160+
161+
// Low res grid multiplier
162+
public float _GridSizeMultiplier;
163+
// Maximum LOD
164+
public uint _MaxLOD;
165+
}
166+
167+
[GenerateHLSL(needAccessors = false, generateCBuffer = true)]
168+
unsafe struct ShaderVariablesWaterDebug
169+
{
170+
public int _WaterDebugMode;
171+
public int _WaterMaskDebugMode;
172+
public int _WaterCurrentDebugMode;
173+
public float _CurrentDebugMultiplier;
174+
175+
public int _WaterFoamDebugMode;
176+
public int _PaddingWDbg0;
177+
public int _PaddingWDbg1;
178+
public int _PaddingWDbg2;
179+
}
180+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//
2+
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
3+
//
4+
5+
#ifndef SHADERVARIABLESWATER_CS_HLSL
6+
#define SHADERVARIABLESWATER_CS_HLSL
7+
// Generated from UnityEngine.Rendering.HighDefinition.ShaderVariablesWaterDebug
8+
// PackingRules = Exact
9+
CBUFFER_START(ShaderVariablesWaterDebug)
10+
int _WaterDebugMode;
11+
int _WaterMaskDebugMode;
12+
int _WaterCurrentDebugMode;
13+
float _CurrentDebugMultiplier;
14+
int _WaterFoamDebugMode;
15+
int _PaddingWDbg0;
16+
int _PaddingWDbg1;
17+
int _PaddingWDbg2;
18+
CBUFFER_END
19+
20+
// Generated from UnityEngine.Rendering.HighDefinition.ShaderVariablesWaterPerCamera
21+
// PackingRules = Exact
22+
CBUFFER_START(ShaderVariablesWaterPerCamera)
23+
float2 _PatchOffset;
24+
float2 _GridSize;
25+
float2 _RegionExtent;
26+
float _GridSizeMultiplier;
27+
uint _MaxLOD;
28+
CBUFFER_END
29+
30+
// Generated from UnityEngine.Rendering.HighDefinition.ShaderVariablesWaterPerSurface
31+
// PackingRules = Exact
32+
CBUFFER_START(ShaderVariablesWaterPerSurface)
33+
float4x4 _WaterSurfaceTransform;
34+
float4x4 _WaterSurfaceTransform_Inverse;
35+
float4 _PatchOrientation;
36+
float4 _PatchWindSpeed;
37+
float4 _PatchDirectionDampener;
38+
int4 _PatchGroup;
39+
float2 _WaterMaskScale;
40+
float2 _WaterMaskOffset;
41+
float2 _WaterMaskRemap;
42+
float2 _GroupOrientation;
43+
float2 _DeformationRegionOffset;
44+
float2 _DeformationRegionScale;
45+
float4 _Band0_ScaleOffset_AmplitudeMultiplier;
46+
float4 _Band1_ScaleOffset_AmplitudeMultiplier;
47+
float4 _Band2_ScaleOffset_AmplitudeMultiplier;
48+
float2 _Band0_Fade;
49+
float2 _Band1_Fade;
50+
float2 _Band2_Fade;
51+
uint _BandResolution;
52+
int _SurfaceIndex;
53+
float2 _SimulationFoamMaskScale;
54+
float2 _SimulationFoamMaskOffset;
55+
float _SimulationFoamIntensity;
56+
float _SimulationFoamAmount;
57+
float _WaterFoamRegionResolution;
58+
float _FoamTiling;
59+
float2 _FoamRegionScale;
60+
float2 _FoamRegionOffset;
61+
float4 _WaterUpDirection;
62+
float4 _WaterExtinction;
63+
float _MaxRefractionDistance;
64+
float _CausticsRegionSize;
65+
int _CausticsBandIndex;
66+
float _PaddingW2;
67+
float4 _WaterAlbedo;
68+
float _AmbientScattering;
69+
float _HeightBasedScattering;
70+
float _DisplacementScattering;
71+
float _ScatteringWaveHeight;
72+
float _FoamCurrentInfluence;
73+
float _FoamSmoothness;
74+
float _WaterSmoothness;
75+
float _FoamPersistenceMultiplier;
76+
float2 _WaterForwardXZ;
77+
int _DeformationRegionResolution;
78+
float _CausticsMaxLOD;
79+
float _CausticsTilingFactor;
80+
float _CausticsIntensity;
81+
float _CausticsShadowIntensity;
82+
float _CausticsPlaneBlendDistance;
83+
float _MaxWaveDisplacement;
84+
float _MaxWaveHeight;
85+
float2 _CurrentMapInfluence;
86+
float4 _Group0CurrentRegionScaleOffset;
87+
float4 _Group1CurrentRegionScaleOffset;
88+
uint _WaterRenderingLayer;
89+
float _WaterMaxTessellationFactor;
90+
float _WaterTessellationFadeStart;
91+
float _WaterTessellationFadeRange;
92+
float4x4 _WaterCustomTransform_Inverse;
93+
float _MaxWaterDeformation;
94+
float _SimulationTime;
95+
float _DeltaTime;
96+
float _PaddingW1;
97+
CBUFFER_END
98+
99+
100+
#endif

Packages/com.unity.render-pipelines.high-definition/Runtime/Water/ShaderVariablesWater.cs.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.

Packages/com.unity.render-pipelines.high-definition/Runtime/Water/ShaderVariablesWater.cs.meta

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

Packages/com.unity.render-pipelines.high-definition/Runtime/Water/Shaders/ShoreWaveUtilities.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef SHORE_WAVE_UTILITIES_H
22
#define SHORE_WAVE_UTILITIES_H
33

4+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/ShaderVariablesWater.cs.hlsl"
5+
46
struct WaveData
57
{
68
float position;

Packages/com.unity.render-pipelines.high-definition/Runtime/Water/Shaders/UnderWaterUtilities.hlsl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/WaterSystemDef.cs.hlsl"
66
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
77

8+
// This file is included in various part of HDRP
9+
// We don't want water specific global shader variables to leak into the global scope
10+
#if defined(WATER_FOG_PASS) || defined(WATER_SURFACE_GBUFFER) || defined(WATER_ONE_BAND) || defined(WATER_TWO_BANDS) || defined(WATER_THREE_BANDS)
11+
#define IS_HDRP_WATER_SYSTEM_PASS
12+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/ShaderVariablesWater.cs.hlsl"
13+
#endif
14+
815
#if defined(_SURFACE_TYPE_TRANSPARENT) && defined(_ENABLE_FOG_ON_TRANSPARENT)
916
#define SUPPORT_WATER_ABSORPTION
1017
#endif
@@ -71,10 +78,11 @@ uint GetWaterSurfaceIndex(uint2 positionSS)
7178

7279
Texture2D<float4> _WaterCausticsDataBuffer;
7380

81+
#if defined(IS_HDRP_WATER_SYSTEM_PASS) || defined(SUPPORT_WATER_ABSORPTION)
7482
float EvaluateSimulationCaustics(float3 refractedWaterPosRWS, float refractedWaterDepth, float2 distortedWaterNDC)
7583
{
7684
// We cannot have same variable names in different constant buffers, use some defines to select the correct ones
77-
#if defined(_TRANSPARENT_REFRACTIVE_SORT) || defined(SUPPORT_WATER_ABSORPTION)
85+
#ifdef SUPPORT_WATER_ABSORPTION
7886
#define _CausticsIntensity _UnderWaterCausticsIntensity
7987
#define _CausticsPlaneBlendDistance _UnderWaterCausticsPlaneBlendDistance
8088
#define _CausticsTilingFactor _UnderWaterCausticsTilingFactor
@@ -130,6 +138,7 @@ float EvaluateSimulationCaustics(float3 refractedWaterPosRWS, float refractedWat
130138
// Evaluate the triplanar weights and blend the samples togheter
131139
return 1.0 + caustics;
132140
}
141+
#endif
133142

134143
#ifdef SUPPORT_WATER_ABSORPTION
135144
bool EvaluateUnderwaterAbsorption(PositionInputs posInput, out bool underWater, inout float3 opacity)

Packages/com.unity.render-pipelines.high-definition/Runtime/Water/Shaders/WaterCurrentUtilities.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef WATER_CURRENT_UTILITIES_H
22
#define WATER_CURRENT_UTILITIES_H
33

4+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/ShaderVariablesWater.cs.hlsl"
5+
46
// Water Current
57
// NOTE: This should be a structured buffer, however, due to a metal shader translation bug,
68
// we have to make it a texture. Which sucks, but we don't have a choice.

Packages/com.unity.render-pipelines.high-definition/Runtime/Water/Shaders/WaterDeformation.shader

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Shader "Hidden/HDRP/WaterDeformation"
1515
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
1616
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
1717
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
18-
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/WaterSystemDef.cs.hlsl"
1918
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/WaterDeformer/WaterDeformer.cs.hlsl"
2019
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/Shaders/WaterDeformationUtilities.hlsl"
2120

Packages/com.unity.render-pipelines.high-definition/Runtime/Water/Shaders/WaterDeformationUtilities.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef WATER_DEFORMATION_UTILITIES_H
22
#define WATER_DEFORMATION_UTILITIES_H
33

4+
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Water/WaterSystemDef.cs.hlsl"
5+
46
// The set of deformers that should be applied this frame
57
StructuredBuffer<WaterDeformerData> _WaterDeformerData;
68
Texture2D<float> _WaterDeformerTextureAtlas;

0 commit comments

Comments
 (0)