Skip to content

Instantly share code, notes, and snippets.

@FransBouma
Created November 15, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FransBouma/11cefab9e50a910c6835 to your computer and use it in GitHub Desktop.
Save FransBouma/11cefab9e50a910c6835 to your computer and use it in GitHub Desktop.
Shader which crashes reshade compiler with compilation failed but no errors. Work in progress, so it will bug at runtime, if it gets compiled that is...
////-------------------//
/// **DEPTH HAZE** ///
//-------------------////
// This simple effect slightly blurs the far away scenery based on the depth buffer. It's a small, detuned depth of field with only
// a far plane. The DoF is very simple as it's not a lens effect but an effect caused by haze and the lack of the human eye to see
// far away detail. Modern render engines however render far away scenery with a crisp pixel detail causing it to look 'off'.
#define USE_DEPTHHAZE 1
#define DEH_ToggleKey VK_F4
#define DEH_NEARPLANE 0.6 //[0.0:1.0] //- start of scenery to become affected by depth haze. 1.0 is infinity.
#define DEH_FARBLURCURVE 0.88 //[0.0:20.0] //- strenght of the blur curve starting with DEH_NEARPLANE
#define DEH_BLURRADIUS 14.0 //[5.0:50.0] //-Blur radius approximately in pixels. Radius, not diameter.
#define DEH_RingDOFSamples 6 //[5:30] //-Samples on the first ring. The other rings around have more samples
#define DEH_RingDOFRings 7 //[1:8] //-Ring count
#define DEH_RingDOFThreshold 1.74 //[0.8:3.0] //-Threshold for bokeh brightening. Above this value, everything gets much much brighter. 1.0 is maximum value for LDR games like GTASA, higher values work only on HDR games like Skyrim etc.
#define DEH_RingDOFGain 0.1 //[0.1:2.0] //-Amount of brightening for pixels brighter than threshold.
#define DEH_RingDOFBias 0.11 //[0.0:2.0] //-bokeh bias.
#define DEH_RingDOFFring 0.13 //[0.0:1.0] //-Amount of chromatic abberation
NAMESPACE_ENTER(OFX)
#include OFX_SETTINGS_DEF
#if USE_DEPTHHAZE
///////////////////////////////////////////////////////////////////
// Based on default old DoF. The old DoF was original programmed by:
// Matso, gp65cj042, MartyMcFly
//
// This effect works like a one-side DoF for distance haze, which slightly
// blurs far away elements. A normal DoF has a focus point and blurs using
// two planes.
//
// This effect blurs from the near threshold to infinity (1.0f), using 0
// as blur strength at the near threshold to the max blur strength at infinity.
///////////////////////////////////////////////////////////////////
float CalculateCoC(float2 texcoord)
{
float scenedepth = tex2D(RFX_depthTexColor, texcoord).r;
float toReturn = 0.0f;
if(scenedepth > DEH_NEARPLANE)
{
float depthdiff = abs(scenedepth-DEH_NEARPLANE);
float power = pow(depthdiff, DEH_FARBLURCURVE);
toReturn = saturate(power);
}
return toReturn;
}
float4 GetDoF(sampler tex, float2 coords, float blur)
{
const float3 lumCoef = float3(0.212656, 0.715158, 0.072186);
float4 colDF = tex2Dlod(tex, float4(coords,0,0));
float lum = dot(colDF.xyz, lumCoef);
float thresh = max((lum - DEH_RingDOFThreshold) * DEH_RingDOFGain, 0.0);
float3 nullcol = float3(0,0,0);
colDF.xyz +=max(0, lerp(nullcol.xyz, colDF.xyz, thresh * blur));
return colDF;
}
void PS_OFX_DEH_RingDOF(float4 vpos : SV_Position, float2 texcoord : TEXCOORD, out float4 hdr2R : SV_Target0)
{
float origCoC = CalculateCoC(texcoord.xy);
float2 discRadius = DEH_BLURRADIUS * float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT) * origCoC / DEH_RingDOFRings;
float4 col = tex2D(RFX_backbufferColor, texcoord.xy);
if(discRadius.x/BUFFER_RCP_WIDTH > 0.25)
{
float s = 1.0;
int ringsamples;
[loop]
for (int g = 1; g <= DEH_RingDOFRings; g += 1)
{
ringsamples = g * DEH_RingDOFSamples;
[loop]
for (int j = 0 ; j < ringsamples ; j += 1)
{
float step = 3.1415972*2.0 / ringsamples;
float2 sampleoffset = discRadius.xy * float2(cos(j*step)*g, sin(j*step)*g);
float sampleCoC = CalculateCoC(float2(texcoord.xy + sampleoffset));
if(sampleCoC<origCoC)
{
sampleoffset = sampleoffset / origCoC * sampleCoC;
}
col.xyz += GetDoF(RFX_backbufferColor, texcoord.xy + sampleoffset, origCoC).xyz * lerp(1.0, g /DEH_RingDOFRings, DEH_RingDOFBias);
s += 1.0*lerp(1.0,g/ DEH_RingDOFRings, DEH_RingDOFBias);
}
}
col = col/s;
}
hdr2R = col;
}
technique OFX_DEH_Tech <bool enabled = false; int toggle = DEH_ToggleKey; >
{
// single pass, simply calculate CoC (which is straight forward) on the fly, write directly back into backbuffer.
pass OFX_DEH_RingDOF
{
VertexShader = RFX_VS_PostProcess;
PixelShader = PS_OFX_DEH_RingDOF;
}
}
#endif
#include OFX_SETTINGS_UNDEF
NAMESPACE_LEAVE()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment