Skip to content

Commit

Permalink
Add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Nov 19, 2021
1 parent e72bf7d commit 1ddc774
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 54 deletions.
14 changes: 11 additions & 3 deletions shaders/cAbberation.fx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ sampler2D _SampleColor
#endif
};

/* [Vertex Shaders] */

void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float2 TexCoord : TEXCOORD)
{
TexCoord.x = (ID == 2) ? 2.0 : 0.0;
Expand All @@ -25,6 +27,8 @@ void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION
}

/*
[Pixel Shaders]
NOTE: PixelSize = 1.0 / screensize
TexCoord + _ShiftRed * pixelsize == TexCoord + _ShiftRed / screensize
Expand All @@ -36,9 +40,13 @@ void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION
void AbberationPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
const float2 PixelSize = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
OutputColor0.r = tex2D(_SampleColor, TexCoord + _ShiftRed * PixelSize).r; // shifted red channel
OutputColor0.g = tex2D(_SampleColor, TexCoord).g; // center green channel
OutputColor0.b = tex2D(_SampleColor, TexCoord + _ShiftBlue * PixelSize).b; // shifted blue channel
// Shift red channel
OutputColor0.r = tex2D(_SampleColor, TexCoord + _ShiftRed * PixelSize).r;
// Keep green channel to the center
OutputColor0.g = tex2D(_SampleColor, TexCoord).g;
// Shift blue channel
OutputColor0.b = tex2D(_SampleColor, TexCoord + _ShiftBlue * PixelSize).b;
// Write alpha value
OutputColor0.a = 1.0;
}

Expand Down
12 changes: 10 additions & 2 deletions shaders/cAutoExposure.fx
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,34 @@ sampler2D _SampleLumaLOD
Texture = _RenderLumaLOD;
};

/* [Vertex Shaders] */

void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float2 TexCoord : TEXCOORD)
{
TexCoord.x = (ID == 2) ? 2.0 : 0.0;
TexCoord.y = (ID == 1) ? 2.0 : 0.0;
Position = float4(TexCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}

/* [Pixel Shaders] */

void BlitPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_Target0)
{
float4 Color = tex2D(_SampleColor, TexCoord);

// OutputColor.rgb = Output the highest brightness out of red/green/blue component
// OutputColor.a = Output the weight for temporal blending
OutputColor0 = float4(max(Color.r, max(Color.g, Color.b)).rrr, _TimeRate);
}

void ExposurePS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
// Average Luma = Average value (1x1) for all of the pixels
float AverageLuma = tex2Dlod(_SampleLumaLOD, float4(TexCoord, 0.0, 8.0)).r;
float4 Color = tex2D(_SampleColor, TexCoord);

// KeyValue represents an exposure compensation curve
// From https://knarkowicz.wordpress.com/2016/01/09/automatic-exposure/
// KeyValue is an exposure compensation curve
// Source: https://knarkowicz.wordpress.com/2016/01/09/automatic-exposure/
float KeyValue = 1.03 - (2.0 / (log10(AverageLuma + 1.0) + 2.0));
float ExposureValue = log2(KeyValue / AverageLuma) + _ManualBias;
OutputColor0 = Color * exp2(ExposureValue);
Expand Down
47 changes: 29 additions & 18 deletions shaders/cBloom.fx
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

/*
Dual-filtering bloom
Kernels
http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
Thresholding
https://github.com/keijiro/Kino [MIT]
Tonemap
https://github.com/TheRealMJP/BakingLab [MIT]
*/

uniform float _Threshold <
ui_type = "drag";
ui_min = 0.0;
Expand Down Expand Up @@ -136,11 +146,7 @@ sampler2D _SampleBloom8
Texture = _RenderBloom8;
};

/*
[ Vertex Shaders ]
Dual Filtering Algorithm
[http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare] [MIT]
*/
/* [ Vertex Shaders ] */

void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float2 TexCoord : TEXCOORD0)
{
Expand All @@ -154,20 +160,27 @@ void DownsampleVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION,
float2 TexCoord0;
PostProcessVS(ID, Position, TexCoord0);
const float2 pSize = 1.0 / ldexp(float2(BUFFER_WIDTH, BUFFER_HEIGHT), -Factor);
TexCoord[0] = TexCoord0.xyxy + float4(-1.0, -1.0, 1.0, 1.0) * pSize.xyxy; // Quad
TexCoord[1] = TexCoord0.xyyy + float4(-2.0, 2.0, 0.0, -2.0) * pSize.xyyy; // Left column
TexCoord[2] = TexCoord0.xyyy + float4(0.0, 2.0, 0.0, -2.0) * pSize.xyyy; // Center column
TexCoord[3] = TexCoord0.xyyy + float4(2.0, 2.0, 0.0, -2.0) * pSize.xyyy; // Right column
// Quadrant
TexCoord[0] = TexCoord0.xyxy + float4(-1.0, -1.0, 1.0, 1.0) * pSize.xyxy;
// Left column
TexCoord[1] = TexCoord0.xyyy + float4(-2.0, 2.0, 0.0, -2.0) * pSize.xyyy;
// Center column
TexCoord[2] = TexCoord0.xyyy + float4(0.0, 2.0, 0.0, -2.0) * pSize.xyyy;
// Right column
TexCoord[3] = TexCoord0.xyyy + float4(2.0, 2.0, 0.0, -2.0) * pSize.xyyy;
}

void UpsampleVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoord[3] : TEXCOORD0, float Factor)
{
float2 TexCoord0;
PostProcessVS(ID, Position, TexCoord0);
const float2 pSize = 1.0 / ldexp(float2(BUFFER_WIDTH, BUFFER_HEIGHT), -Factor);
TexCoord[0] = TexCoord0.xyyy + float4(-1.0, 1.0, 0.0, -1.0) * pSize.xyyy; // Left column
TexCoord[1] = TexCoord0.xyyy + float4(0.0, 1.0, 0.0, -1.0) * pSize.xyyy; // Center column
TexCoord[2] = TexCoord0.xyyy + float4(1.0, 1.0, 0.0, -1.0) * pSize.xyyy; // Right column
// Left column
TexCoord[0] = TexCoord0.xyyy + float4(-1.0, 1.0, 0.0, -1.0) * pSize.xyyy;
// Center column
TexCoord[1] = TexCoord0.xyyy + float4(0.0, 1.0, 0.0, -1.0) * pSize.xyyy;
// Right column
TexCoord[2] = TexCoord0.xyyy + float4(1.0, 1.0, 0.0, -1.0) * pSize.xyyy;
}

void DownsampleVS1(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float4 TexCoord[4] : TEXCOORD0)
Expand Down Expand Up @@ -240,11 +253,7 @@ void UpsampleVS2(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION,
UpsampleVS(ID, Position, TexCoord, 2.0);
}

/*
[ Pixel Shaders ]
Thresholding - [https://github.com/keijiro/Kino] [MIT]
Tonemap - [https://github.com/TheRealMJP/BakingLab] [MIT]
*/
/* [Pixel Shaders] */

float4 DownsamplePS(sampler2D Source, float4 TexCoord[4])
{
Expand Down Expand Up @@ -280,7 +289,7 @@ float4 DownsamplePS(sampler2D Source, float4 TexCoord[4])
Output += (B0 + C0 + B1 + C1) * Weights.y;
Output += (A1 + B1 + A2 + B2) * Weights.y;
Output += (B1 + C1 + B2 + C2) * Weights.y;
return float4(Output.rgb, 1.0);
return Output;
}

float4 UpsamplePS(sampler2D Source, float4 TexCoord[3])
Expand Down Expand Up @@ -325,6 +334,8 @@ void PrefilterPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out
Color = Color * max(ResponseCurve, Brightness - _Threshold) / max(Brightness, 1e-10);
Brightness = max(max(Color.r, Color.g), Color.b);
OutputColor0 = saturate(lerp(Brightness, Color.rgb, _Saturation)) * _ColorShift;

// Set alpha to 1.0 so we can see the complete results in ReShade's statistics
OutputColor0.a = 1.0;
}

Expand Down
15 changes: 13 additions & 2 deletions shaders/cColorBlendOp.fx
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@

/*
Color tinting without texture fetches
*/

uniform float4 _Color <
ui_min = 0.0;
ui_label = "Color";
ui_type = "color";
> = 1.0;

/* [Vertex Shaders] */

void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float2 TexCoord : TEXCOORD0)
{
// Clip a triangle twice the screen's size to make a quad
TexCoord.x = (ID == 2) ? 2.0 : 0.0;
TexCoord.y = (ID == 1) ? 2.0 : 0.0;
Position = float4(TexCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}

float4 ColorPS(float4 Position : SV_Position) : SV_Target
/* [Pixel Shaders] */

void ColorPS(float4 Position : SV_Position, out float4 OutputColor0 : SV_Target0)
{
return _Color;
// Fill this quad with a color
OutputColor0 = _Color;
}

// Use BlendOp to multiple the backbuffer with this quad's color
technique cColorBlendOp
{
pass
Expand Down
6 changes: 5 additions & 1 deletion shaders/cColorNormalization.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

// The various types of color normalization via http://jamie-wong.com/post/color/
/*
The various types of color normalization
Learn more
http://jamie-wong.com/post/color/
*/

uniform int _Select <
ui_type = "combo";
Expand Down
2 changes: 2 additions & 0 deletions shaders/cFilmGrain.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

/*
FilmGrain without texture fetches
"Well ill believe it when i see it."
Yoinked code by Luluco250 (RIP) [https://www.shadertoy.com/view/4t2fRz] [MIT]
*/
Expand Down
2 changes: 2 additions & 0 deletions shaders/cFrameBlending.fx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION

void BlendPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
// Copy backbuffer to a that continuously blends with its previous result
OutputColor0 = float4(tex2D(_SampleColor, TexCoord).rgb, _BlendFactor);
}

void DisplayPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
// Display the buffer
OutputColor0 = tex2D(_SampleCopy, TexCoord);
}

Expand Down
6 changes: 6 additions & 0 deletions shaders/cInterpolation.fx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

/*
Quasi frame-rate interpolation shader
Note: Make better masking
*/

uniform float _Blend <
ui_type = "drag";
ui_label = "Temporal Blending";
Expand Down Expand Up @@ -256,6 +261,7 @@ void HorizontalBlurPS1(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD
{
OutputColor0 = GaussianBlur(_SampleOpticalFlow, TexCoord, Offsets).xy;
}

void VerticalBlurPS1(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, float4 Offsets[7] : TEXCOORD1, out float2 OutputColor0 : SV_TARGET0)
{
OutputColor0 = GaussianBlur(_SampleData0, TexCoord, Offsets).xy;
Expand Down
6 changes: 5 additions & 1 deletion shaders/cLetterBox.fx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION
TexCoord.y = (ID == 1) ? 2.0 : 0.0;
Position = float4(TexCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}

/* [Pixel Shaders] */

void LetterboxPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float3 OutputColor : SV_Target0)
{
const float2 Scale = mad(-_Scale, 0.5, 0.5);
// Output a rectangle
const float2 Scale = -_Scale * 0.5 + 0.5;
float2 Shaper = step(Scale, TexCoord);
Shaper *= step(Scale, 1.0 - TexCoord);
OutputColor = Shaper.x * Shaper.y;
Expand All @@ -34,6 +36,8 @@ technique cLetterBox
{
VertexShader = PostProcessVS;
PixelShader = LetterboxPS;
// Blend the rectangle with the backbuffer
ClearRenderTargets = FALSE;
BlendEnable = TRUE;
BlendOp = ADD;
SrcBlend = DESTCOLOR;
Expand Down
11 changes: 8 additions & 3 deletions shaders/cMosaic.fx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

/*
Various mosaic shaders
Circle mosaic
https://www.shadertoy.com/view/3sVcRh
Triangle mosaic
https://www.shadertoy.com/view/4d2SWy
*/

uniform int2 _Radius <
ui_type = "drag";
ui_label = "Mosaic Radius";
Expand Down Expand Up @@ -66,7 +74,6 @@ void MosaicPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out fl

switch(_Shape)
{
// Circle https://www.shadertoy.com/view/4d2SWy
case 0:
BlockCoord = floor(PixelPosition / MaxRadius) * MaxRadius;
MosaicCoord = BlockCoord * PixelSize;
Expand All @@ -77,9 +84,7 @@ void MosaicPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out fl
float Length = distance(Center, Offset);
float Circle = 1.0 - smoothstep(-2.0 , 0.0, Length - Center.x);
OutputColor0 = Color * Circle;
//OutputColor0 = MosaicCoord;
break;
// Triangle https://www.shadertoy.com/view/4d2SWy
case 1:
const float MaxLODLevel = log2(sqrt((BUFFER_WIDTH * BUFFER_HEIGHT) / (_Radius.x * _Radius.y)));
const float2 Divisor = 1.0 / (2.0 * _Radius);
Expand Down
5 changes: 3 additions & 2 deletions shaders/cMotionBlur.fx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Special Thanks to
- MartinBFFan and Pao on Discord for reporting bugs
- BSD for bug propaganda and helping to solve my issue
- Lord of Lunacy, KingEric1992, and Marty McFly for power of 2 function
*/

uniform float _Constraint <
Expand Down Expand Up @@ -243,7 +242,9 @@ void HorizontalBlurPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0
void VerticalBlurPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, float4 Offsets[7] : TEXCOORD1, out float OutputColor0 : SV_TARGET0, out float OutputColor1 : SV_TARGET1)
{
OutputColor0 = GaussianBlur(_SampleData1, TexCoord, Offsets).x;
OutputColor1 = OutputColor0.x; // Store previous blurred image before it gets overwritten!

// Store blurred image for the next before we overwrite the rendertarget!
OutputColor1 = OutputColor0.x;
}

void DerivativesPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, float4 Offsets : TEXCOORD1, out float2 OutputColor0 : SV_TARGET0)
Expand Down
34 changes: 17 additions & 17 deletions shaders/cOpticalFlow.fx
Original file line number Diff line number Diff line change
Expand Up @@ -178,52 +178,52 @@ void DerivativesVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION
Offsets = TexCoord0.xyxy + PixelOffset;
}

void VelocityStreamsVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION, inout float2 Velocity : TEXCOORD0)
void VelocityStreamsVS(in uint ID : SV_VERTEXID, out float4 Position : SV_POSITION, out float2 Velocity : TEXCOORD0)
{
// get line index / vertex index
int LineID = ID / 2;
int VertexID = ID % 2; // either 0 (line-start) or 1 (line-end)
int LineID = ID / 2; // Line Index
int VertexID = ID % 2; // Vertex Index within the line (0 = start, 1 = end)

// get position (xy)
// Get Row (x) and Column (y) position
int Row = LineID / LINES_X;
int Column = LineID - LINES_X * Row;

// compute origin (line-start)
// Compute origin (line-start)
const float2 Spacing = float2(SPACE_X, SPACE_Y);
float2 Offset = Spacing * 0.5;
float2 Origin = Offset + float2(Column, Row) * Spacing;

// get velocity from texture at origin location
const float2 wh_rcp = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
Velocity = tex2Dlod(_SampleData1, float4(Origin.x * wh_rcp.x, 1.0 - Origin.y * wh_rcp.y, 0.0, _Detail)).xy;
// Get velocity from texture at origin location
const float2 PixelSize = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
float2 VelocityCoord = float2(0.0, -1.0) + Origin * PixelSize;
Velocity = tex2Dlod(_SampleData1, float4(VelocityCoord, 0.0, _Detail)).xy;

// SCALE velocity
// Scale velocity
float2 Direction = Velocity * VELOCITY_SCALE;

float Length = length(Direction + 1e-5);
Direction = Direction / sqrt(Length * 0.1);

// for fragmentshader ... coloring
// Color for fragmentshader
Velocity = Direction * 0.2;

// compute current vertex position (based on vtx_id)
float2 VertexPosition = (0.0);
// Compute current vertex position (based on VertexID)
float2 VertexPosition = 0.0;

if(_Normal)
{
// lines, normal to velocity direction
// Lines: Normal to velocity direction
Direction *= 0.5;
float2 DirectionNormal = float2(Direction.y, -Direction.x);
VertexPosition = Origin + Direction - DirectionNormal + DirectionNormal * VertexID * 2;
}
else
{
// lines,in velocity direction
// Lines: Velocity direction
VertexPosition = Origin + Direction * VertexID;
}

// finish vertex coordinate
float2 VertexPositionNormal = (VertexPosition + 0.5) * wh_rcp; // [0, 1]
// Finish vertex position
float2 VertexPositionNormal = (VertexPosition + 0.5) * PixelSize; // [0, 1]
Position = float4(VertexPositionNormal * 2.0 - 1.0, 0.0, 1.0); // ndc: [-1, +1]
}

Expand Down
Loading

0 comments on commit 1ddc774

Please sign in to comment.