How to write to SV_Depth?
- Fu-Bama
- Topic Author
Less
More
I'm trying to UV-distort the depth buffer along with the image distortion, for effects that follow and use depth.
Problem is that I don't know how to write to the depth buffer SV_Depth.
I tried to write the depth to Pass1 alpha channel, then in second pass, that targets SV_Depth, return the alpha channel from the first one.
But it gives an error undefined variable "_return"
Shader Code:
Problem is that I don't know how to write to the depth buffer SV_Depth.
I tried to write the depth to Pass1 alpha channel, then in second pass, that targets SV_Depth, return the alpha channel from the first one.
But it gives an error undefined variable "_return"
Shader Code:
Warning: Spoiler!
/*
Copyright (c) 2018 Jacob Maximilian Fober
This work is licensed under the Creative Commons
Attribution-ShareAlike 4.0 International License.
To view a copy of this license, visit
http://creativecommons.org/licenses/by-sa/4.0/.
*/
// Perfect Perspective PS ver. 2.0
////////////////////
/////// MENU ///////
////////////////////
uniform float3 Color <
ui_label = "Borders Color";
ui_type = "Color";
> = float3(0.027, 0.027, 0.027);
uniform int FOV <
ui_label = "Field of View";
ui_tooltip = "Match in-game Field of View";
ui_type = "drag";
ui_min = 45; ui_max = 120;
> = 90;
uniform int Type <
ui_label = "Type of FOV";
ui_tooltip = "If image bulges in movement, change it to Diagonal. When proportions are distorted, choose Vertical";
ui_type = "combo";
ui_items = "Horizontal FOV\0Diagonal FOV\0Vertical FOV\0";
> = 0;
uniform float Zooming <
ui_label = "Adjust Borders Size";
ui_type = "drag";
ui_min = 0.0; ui_max = 3.0; ui_step = 0.001;
> = 1.0;
//////////////////////
/////// SHADER ///////
//////////////////////
#include "ReShade.fxh"
// Stereographic-Gnomonic lookup function
// Input data:
// SqrTanFOVq >> squared tangent of quater FOV angle
// Coordinates >> UV coordinates (from -1, to 1), where (0,0) is at the center of the screen
float Formula(float SqrTanFOVq, float2 Coordinates)
{
float Result = 1.0 - SqrTanFOVq;
Result /= 1.0 - SqrTanFOVq * (Coordinates.x * Coordinates.x + (Coordinates.y * Coordinates.y));
return Result;
}
// Shader pass
float4 PerfectPerspectivePS(float4 vois : SV_Position, float2 texcoord : TexCoord) : SV_Target
{
// Get Pixel Position
float2 SphCoord = texcoord;
// Get Aspect Ratio
float AspectR = 1.0 / ReShade::AspectRatio;
// Convert FOV type..
float FovType = 1.0;
if (Type == 1) // ..to diagonal
{
FovType = sqrt(AspectR * AspectR + 1.0);
}
else if (Type == 2) // ..to vertical
{
FovType = AspectR;
}
// Convert 1/4 FOV to radians and calc tangent squared
float SqrTanFOVq = float(FOV) * 0.25;
SqrTanFOVq = radians(SqrTanFOVq);
SqrTanFOVq = tan(SqrTanFOVq);
SqrTanFOVq *= SqrTanFOVq;
// Convert UV to Radial Coordinates
SphCoord = SphCoord * 2.0 - 1.0;
// Aspect Ratio correction
SphCoord.y *= AspectR;
// Zoom in image and adjust FOV type (pass 1 of 2)
SphCoord *= Zooming / FovType;
// Stereographic-Gnomonic lookup
SphCoord *= Formula(SqrTanFOVq, SphCoord);
// Adjust FOV type (pass 2 of 2)
SphCoord *= FovType;
// Aspect Ratio back to square
SphCoord.y /= AspectR;
// Back to UV Coordinates
SphCoord = SphCoord * 0.5 + 0.5;
// Sample display image
float4 Display;
Display.rgb = tex2D(ReShade::BackBuffer, SphCoord).rgb;
Display.a = tex2D(ReShade::DepthBuffer, SphCoord).r;
// Mask out outside-border pixels
if (SphCoord.x < 1.0 && SphCoord.x > 0.0 && SphCoord.y < 1.0 && SphCoord.y > 0.0)
{
return Display;
}
else
{
return float4(Color, 0);
}
}
float AdjustDepthPS(float4 vois : SV_Position, float2 texcoord : TexCoord) : SV_Depth
{
return tex2D(ReShade::BackBuffer, texcoord).a;
}
technique PerfectPerspectiveBeta
{
pass ColorPass
{
VertexShader = PostProcessVS;
PixelShader = PerfectPerspectivePS;
}
pass DepthPass
{
VertexShader = PostProcessVS;
PixelShader = AdjustDepthPS;
}
}
Please Log in or Create an account to join the conversation.
- crosire
Less
More
This is not going to work, unfortunately. The depth-stencil buffer bound for writing is not the one you get as input.
In fact ReShade does not guarantee a depth buffer being bound during post-processing at all, it's only defined to at least bind a stencil buffer when the render target size matches that of the game viewport (SMAA needs this). A depth buffer doesn't make much sense during post-processing usually, because we only draw fullscreen primitives that should not be depth sorted.
Depth data you get as input comes straight from the depth texture the game had bound as depth buffer during rendering, but it's considered immutable at this point like all resources ReShade gives access to. The backbuffer is the one and only exception. Giving write access to any other resource would come with a heavy performance penalty and is therefore not allowed nor implemented.
In fact ReShade does not guarantee a depth buffer being bound during post-processing at all, it's only defined to at least bind a stencil buffer when the render target size matches that of the game viewport (SMAA needs this). A depth buffer doesn't make much sense during post-processing usually, because we only draw fullscreen primitives that should not be depth sorted.
Depth data you get as input comes straight from the depth texture the game had bound as depth buffer during rendering, but it's considered immutable at this point like all resources ReShade gives access to. The backbuffer is the one and only exception. Giving write access to any other resource would come with a heavy performance penalty and is therefore not allowed nor implemented.
Please Log in or Create an account to join the conversation.