|
| 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 | +} |
0 commit comments