A game about forced loneliness, made by TACStudios
1// This is necessary to prevent Unity from deciding that our default config logic is actually an include guard declaration
2#ifndef STP_COMMON_UNITY_INCLUDE_GUARD
3#define STP_COMMON_UNITY_INCLUDE_GUARD
4
5///
6/// Spatial-Temporal Post-Processing (STP) Common Shader Code
7///
8/// This file provides configuration defines and other common utilities associated with STP
9/// See STP.cs for more details on how this shader code interacts with C#
10///
11/// Usage:
12/// - Add control defines
13/// - Include this file in a shader pass associated with STP
14/// - Call relevant STP function
15///
16/// By default, no shader functions are available until they are specifically requested via define.
17///
18/// The following defines can be used to enable shader functionality:
19/// - STP_PAT
20/// - Enables the "Pattern" pass
21/// - STP_DIL
22/// - Enables the "Dilation" pass
23/// - STP_SAA
24/// - Enables the "Spatial Anti-Aliasing" pass
25/// - STP_TAA
26/// - Enables the "TAA" pass
27
28// Indicate that we'll be using the HLSL implementation of STP
29#define STP_HLSL 1
30#define STP_GPU 1
31
32// Disable grain since we don't currently have a way to integrate this with the rest of Unity's post processing
33#define STP_GRAIN 0
34
35// Enable the minimum precision path when supported by the shader environment.
36#if REAL_IS_HALF || defined(UNITY_DEVICE_SUPPORTS_NATIVE_16BIT)
37 #define STP_MEDIUM 1
38#endif
39
40// Mobile platforms use a simplified version of STP to reduce runtime overhead.
41#if defined(SHADER_API_MOBILE) || defined(SHADER_API_SWITCH)
42 #define STP_TAA_Q 0
43#endif
44
45#if defined(SHADER_API_SWITCH)
46 #define STP_BUG_SAT_INF 1
47#endif
48
49// Enable workarounds that help us avoid issues on Metal
50#if defined(SHADER_API_METAL)
51 // Relying on infinity behavior causes issues in the on-screen inline pass calculations
52 // We expect this option to be required on Metal because the shading language spec states that the fast-math
53 // option is on by default which disables support for proper INF handling.
54 #define STP_BUG_SAT_INF 1
55#endif
56
57#if defined(SHADER_API_PSSL)
58 #define STP_BUG_SAT_INF 1
59#endif
60
61#if defined(UNITY_DEVICE_SUPPORTS_NATIVE_16BIT)
62 #define STP_16BIT 1
63
64 #if defined(SHADER_API_PSSL)
65 #define STP_BUG_PRX 1
66 #endif
67#else
68 #define STP_32BIT 1
69#endif
70
71#if defined(ENABLE_DEBUG_MODE)
72 #define STP_BUG 1
73#endif
74
75// Include the STP HLSL files
76#include "Packages/com.unity.render-pipelines.core/Runtime/STP/Stp.hlsl"
77#include "Packages/com.unity.render-pipelines.core/Runtime/STP/STP.cs.hlsl"
78
79// Include TextureXR.hlsl for XR macros
80#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureXR.hlsl"
81
82//
83// Common
84//
85
86#if defined(ENABLE_LARGE_KERNEL)
87#define STP_GROUP_SIZE 128
88#define STP_GROUP_SIZE_SHIFT_X 3
89#define STP_GROUP_SIZE_SHIFT_Y 4
90#else
91#define STP_GROUP_SIZE 64
92#define STP_GROUP_SIZE_SHIFT_X 3
93#define STP_GROUP_SIZE_SHIFT_Y 3
94#endif
95
96#if defined(STP_16BIT)
97StpW2 ComputeGroupPos(StpW2 groupId)
98{
99 return StpW2(
100 groupId.x << StpW1_(STP_GROUP_SIZE_SHIFT_X),
101 groupId.y << StpW1_(STP_GROUP_SIZE_SHIFT_Y)
102 );
103}
104#else
105StpMU2 ComputeGroupPos(StpMU2 groupId)
106{
107 return StpMU2(
108 groupId.x << StpMU1_(STP_GROUP_SIZE_SHIFT_X),
109 groupId.y << StpMU1_(STP_GROUP_SIZE_SHIFT_Y)
110 );
111}
112#endif
113
114#define STP_COMMON_CONSTANT asuint(_StpCommonConstant.x)
115#define STP_ZBUFFER_PARAMS_Z _StpCommonConstant.y
116#define STP_ZBUFFER_PARAMS_W _StpCommonConstant.z
117
118TEXTURE2D(_StpBlueNoiseIn);
119
120RW_TEXTURE2D_X(float4, _StpDebugOut);
121
122SAMPLER(s_point_clamp_sampler);
123SAMPLER(s_linear_clamp_sampler);
124SAMPLER(s_linear_repeat_sampler);
125
126#if defined(STP_32BIT)
127StpMU1 StpBfeF(StpMU1 data, StpMU1 offset, StpMU1 numBits)
128{
129 StpMU1 mask = (StpMU1(1) << numBits) - StpMU1(1);
130 return (data >> offset) & mask;
131}
132
133StpMU1 StpBfiF(StpMU1 mask, StpMU1 src, StpMU1 dst)
134{
135 return (src & mask) | (dst & ~mask);
136}
137
138StpMU2 StpRemapLaneTo8x16F(StpMU1 i)
139{
140 return StpMU2(StpBfiF(StpMU1(1), i, StpBfeF(i, StpMU1(2), StpMU1(3))),
141 StpBfiF(StpMU1(3), StpBfeF(i, StpMU1(1), StpMU1(2)), StpBfeF(i, StpMU1(3), StpMU1(4))));
142}
143
144StpMU1 DecodeNoiseWidthMinusOneF(StpMU1 param)
145{
146 return param & StpMU1(0xFF);
147}
148#endif
149
150#if defined(STP_16BIT)
151StpW1 StpBfeH(StpW1 data, StpW1 offset, StpW1 numBits)
152{
153 StpW1 mask = (StpW1(1) << numBits) - StpW1(1);
154 return (data >> offset) & mask;
155}
156
157StpW1 StpBfiH(StpW1 mask, StpW1 src, StpW1 dst)
158{
159 return (src & mask) | (dst & ~mask);
160}
161
162StpW2 StpRemapLaneTo8x16H(StpW1 i)
163{
164 return StpW2(StpBfiH(StpW1(1), i, StpBfeH(i, StpW1(2), StpW1(3))),
165 StpBfiH(StpW1(3), StpBfeH(i, StpW1(1), StpW1(2)), StpBfeH(i, StpW1(3), StpW1(4))));
166}
167
168StpW1 DecodeNoiseWidthMinusOneH(StpW1 param)
169{
170 return param & StpW1(0xFF);
171}
172#endif
173
174bool DecodeHasValidHistory(uint param)
175{
176 return (param >> 8) & 1;
177}
178
179uint DecodeStencilMask(uint param)
180{
181 return (param >> 16) & 0xFF;
182}
183
184uint DecodeDebugViewIndex(uint param)
185{
186 return (param >> 24) & 0xFF;
187}
188
189#if defined(STP_32BIT)
190StpMF1 StpDitF1(StpMU2 o)
191{
192 StpMU1 noiseWidthMinusOne = DecodeNoiseWidthMinusOneF(StpMU1(STP_COMMON_CONSTANT));
193 return (StpMF1)LOAD_TEXTURE2D_LOD(_StpBlueNoiseIn, o & noiseWidthMinusOne, 0).a;
194}
195// TODO: Broadcast one value as all three outputs a bug that will effect 'STP_GRAIN=3' output.
196StpMF3 StpDitF3(StpMU2 o) { return (StpMF3)StpDitF1(o); }
197#endif
198
199// NOTE: This function is used by both the 32-bit path, and the 16-bit path (when various workarounds are active)
200void StpBugF(StpU3 p, StpF4 c)
201{
202 if (p.z == DecodeDebugViewIndex(STP_COMMON_CONSTANT))
203 _StpDebugOut[COORD_TEXTURE2D_X(p.xy)] = c;
204}
205
206#if defined(STP_16BIT)
207StpH1 StpDitH1(StpW2 o)
208{
209 StpW1 noiseWidthMinusOne = DecodeNoiseWidthMinusOneH(StpW1(STP_COMMON_CONSTANT));
210 return (StpH1)LOAD_TEXTURE2D_LOD(_StpBlueNoiseIn, o & noiseWidthMinusOne, 0).a;
211}
212// TODO: Broadcast one value as all three outputs a bug that will effect 'STP_GRAIN=3' output.
213StpH3 StpDitH3(StpW2 o) { return (StpH3)StpDitH1(o); }
214#endif
215
216#endif // STP_COMMON_UNITY_INCLUDE_GUARD