Quantcast
Channel: Questions in topic: "shader programming"
Viewing all articles
Browse latest Browse all 169

Get shadowmap in HDRP with commandBuffer

$
0
0
The code I've been working on for ages has this line of code
cmd.DrawRenderer(rc.renderer, srcMaterial, rc.submeshIndex, shadowPass);

which it takes the material's shadowpass and put the result into a RenderTexture.

However, the ShadowCaster pass in URP is simple that only have one half type output

    half4 ShadowPassFragment(Varyings input) : SV_TARGET
    {
        Alpha(SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a, _BaseColor, _Cutoff);
        return 0;
    }

And here's the HDRP Lit code for shadow:

    void Frag(  PackedVaryingsToPS packedInput
                #if defined(SCENESELECTIONPASS) || defined(SCENEPICKINGPASS)
                , out float4 outColor : SV_Target0
                #else
                    #ifdef WRITE_MSAA_DEPTH
                    // We need the depth color as SV_Target0 for alpha to coverage
                    , out float4 depthColor : SV_Target0
                        #ifdef WRITE_NORMAL_BUFFER
                        , out float4 outNormalBuffer : SV_Target1
                        #endif
                    #else
                        #ifdef WRITE_NORMAL_BUFFER
                        , out float4 outNormalBuffer : SV_Target0
                        #endif
                    #endif
    
                    // Decal buffer must be last as it is bind but we can optionally write into it (based on _DISABLE_DECALS)
                    #if defined(WRITE_DECAL_BUFFER) && !defined(_DISABLE_DECALS)
                    , out float4 outDecalBuffer : SV_TARGET_DECAL
                    #endif
                #endif
                #if defined(_DEPTHOFFSET_ON) && !defined(SCENEPICKINGPASS)
                , out float outputDepth : DEPTH_OFFSET_SEMANTIC
                #endif
            )
    {
        UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(packedInput);
        FragInputs input = UnpackVaryingsToFragInputs(packedInput);
        // input.positionSS is SV_Position
        PositionInputs posInput = GetPositionInput(input.positionSS.xy, _ScreenSize.zw, input.positionSS.z, input.positionSS.w, input.positionRWS);
    #ifdef VARYINGS_NEED_POSITION_WS
        float3 V = GetWorldSpaceNormalizeViewDir(input.positionRWS);
    #else
        // Unused
        float3 V = float3(1.0, 1.0, 1.0); // Avoid the division by 0
    #endif
        SurfaceData surfaceData;
        BuiltinData builtinData;
        GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
    #if defined(_DEPTHOFFSET_ON) && !defined(SCENEPICKINGPASS)
        outputDepth = posInput.deviceDepth;
    #if SHADERPASS == SHADERPASS_SHADOWS
        // If we are using the depth offset and manually outputting depth, the slope-scale depth bias is not properly applied
        // we need to manually apply.
        float bias = max(abs(ddx(posInput.deviceDepth)), abs(ddy(posInput.deviceDepth))) * _SlopeScaleDepthBias;
        outputDepth += bias;
    #endif
    #endif
    #ifdef SCENESELECTIONPASS
        // We use depth prepass for scene selection in the editor, this code allow to output the outline correctly
        outColor = float4(_ObjectId, _PassValue, 1.0, 1.0);
    #elif defined(SCENEPICKINGPASS)
        outColor = _SelectionID;
    #else
        // Depth and Alpha to coverage
        #ifdef WRITE_MSAA_DEPTH
            // 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
            depthColor = packedInput.vmesh.positionCS.z;
            #ifdef _ALPHATOMASK_ON
            // Alpha channel is used for alpha to coverage
            depthColor.a = SharpenAlpha(builtinData.opacity, builtinData.alphaClipTreshold);
            #endif
        #endif
        #if defined(WRITE_NORMAL_BUFFER)
        EncodeIntoNormalBuffer(ConvertSurfaceDataToNormalData(surfaceData), outNormalBuffer);
        #endif
        #if defined(WRITE_DECAL_BUFFER) && !defined(_DISABLE_DECALS)
        DecalPrepassData decalPrepassData;
        // We don't have the right to access SurfaceData in a shaderpass.
        // However it would be painful to have to add a function like ConvertSurfaceDataToDecalPrepassData() to every Material to return geomNormalWS anyway
        // Here we will put the constrain that any Material requiring to support Decal, will need to have geomNormalWS as member of surfaceData (and we already require normalWS anyway)
        decalPrepassData.geomNormalWS = surfaceData.geomNormalWS;
        decalPrepassData.decalLayerMask = GetMeshRenderingDecalLayer();
        EncodeIntoDecalPrepassBuffer(decalPrepassData, outDecalBuffer);
        #endif
    #endif // SCENESELECTIONPASS
    }

Can anybody please tell me which one in HDRP is equivalent to the URP version, and how am I going to get the same shadowmap for the command buffer as URP?

Viewing all articles
Browse latest Browse all 169

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>