Skip to content

Commit

Permalink
Refactor code to use preprocessor defines
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Apr 16, 2022
1 parent f7840c2 commit 3ecb7e6
Show file tree
Hide file tree
Showing 37 changed files with 1,985 additions and 2,847 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ kVignette | Natural vignetting effect
Practice | Variable
-------- | --------
**ALLCAPS** | System-Value semantics, state parameters
**ALL_CAPS** | Preprocessor macros
**ALL_CAPS** | Preprocessor macros and parameters
**SnakeCase** | Discrete local and global data
**Snake_Case** | Structs, functions, textures, sampler, and packed data (i.e. `float4 TexCoord_Base_Water` stores 2 UVs for 2 textures, base and water)
**Snake_Case** | Namespace, structs, functions, textures, sampler, and packed data (i.e. `float4 TexCoord_Base_Water` stores 2 UVs for 2 textures, base and water)
**_Snake_Case** | Uniform data
Suffix `VS` and `PS` | `PixelShader` and `VertexShader`

Expand Down
32 changes: 20 additions & 12 deletions shaders/ReShade.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#define RESHADE_DEPTH_INPUT_X_PIXEL_OFFSET 0
#endif

#define BUFFER_PixelSize float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT)
#define BUFFER_PIXEL_SIZE float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT)
#define BUFFER_SCREEN_SIZE float2(BUFFER_WIDTH, BUFFER_HEIGHT)
#define BUFFER_ASPECT_RATIO (BUFFER_WIDTH * BUFFER_RCP_HEIGHT)

Expand All @@ -63,12 +63,12 @@ namespace ReShade
float2 GetScreen_Size() { return float2(BUFFER_WIDTH, BUFFER_HEIGHT); }
#define Aspect_Ratio GetAspect_Ratio()
#define PixelSize GetPixelSize()
#define Screen_Size GetScreen_Size()
#define ScreenSize GetScreen_Size()
#else
// These are deprecated and will be removed eventually.
static const float Aspect_Ratio = BUFFER_WIDTH * BUFFER_RCP_HEIGHT;
static const float2 PixelSize = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
static const float2 Screen_Size = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
static const float2 ScreenSize = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
#endif

// Global textures and samplers
Expand All @@ -79,28 +79,28 @@ namespace ReShade
sampler DepthBuffer { Texture = DepthBufferTex; };

// Helper functions
float GetLinearizedDepth(float2 texcoord)
float GetLinearizedDepth(float2 TexCoord)
{
#if RESHADE_DEPTH_INPUT_IS_UPSIDE_DOWN
texcoord.y = 1.0 - texcoord.y;
TexCoord.y = 1.0 - TexCoord.y;
#endif

texcoord.x /= RESHADE_DEPTH_INPUT_X_SCALE;
texcoord.y /= RESHADE_DEPTH_INPUT_Y_SCALE;
TexCoord.x /= RESHADE_DEPTH_INPUT_X_SCALE;
TexCoord.y /= RESHADE_DEPTH_INPUT_Y_SCALE;

#if RESHADE_DEPTH_INPUT_X_PIXEL_OFFSET
texcoord.x -= RESHADE_DEPTH_INPUT_X_PIXEL_OFFSET * BUFFER_RCP_WIDTH;
TexCoord.x -= RESHADE_DEPTH_INPUT_X_PIXEL_OFFSET * BUFFER_RCP_WIDTH;
#else // Do not check RESHADE_DEPTH_INPUT_X_OFFSET, since it may be a decimal number, which the preprocessor cannot handle
texcoord.x -= RESHADE_DEPTH_INPUT_X_OFFSET / 2.000000001;
TexCoord.x -= RESHADE_DEPTH_INPUT_X_OFFSET / 2.000000001;
#endif

#if RESHADE_DEPTH_INPUT_Y_PIXEL_OFFSET
texcoord.y += RESHADE_DEPTH_INPUT_Y_PIXEL_OFFSET * BUFFER_RCP_HEIGHT;
TexCoord.y += RESHADE_DEPTH_INPUT_Y_PIXEL_OFFSET * BUFFER_RCP_HEIGHT;
#else
texcoord.y += RESHADE_DEPTH_INPUT_Y_OFFSET / 2.000000001;
TexCoord.y += RESHADE_DEPTH_INPUT_Y_OFFSET / 2.000000001;
#endif

float depth = tex2Dlod(DepthBuffer, float4(texcoord, 0, 0)).x * RESHADE_DEPTH_MULTIPLIER;
float depth = tex2Dlod(DepthBuffer, float4(TexCoord, 0, 0)).x * RESHADE_DEPTH_MULTIPLIER;

#if RESHADE_DEPTH_INPUT_IS_LOGARITHMIC
const float C = 0.01;
Expand All @@ -116,3 +116,11 @@ namespace ReShade
return depth;
}
}

// Vertex shader generating a triangle covering the entire screen
void PostProcessVS(in uint id : SV_VERTEXID, out float4 position : SV_POSITION, out float2 TexCoord : TEXCOORD0)
{
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);
}
10 changes: 5 additions & 5 deletions shaders/buggyassshaderlmao.fx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

void Basic_VS(in uint ID : SV_VERTEXID, out float4 Position : SV_POSITION, out float2 Coord : TEXCOORD0)
void Basic_VS(in uint ID : SV_VERTEXID, out float4 Position : SV_POSITION, out float2 TexCoord : TEXCOORD0)
{
Coord.x = (ID == 2) ? 2.0 : 0.0;
Coord.y = (ID == 1) ? 2.0 : 0.0;
Position = float4(Coord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
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);
}

void Shader_PS(in float4 Position : SV_POSITION, in float2 Coord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
void Shader_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
float2 PositionMod = Position.xy % 2.0;
OutputColor0.r = (PositionMod.x == 1.0 && PositionMod.y == 1.0) ? 1.0 : 0.0;
Expand Down
16 changes: 8 additions & 8 deletions shaders/cAbberation.fx
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ sampler2D Sample_Color

// Vertex shaders

void Basic_VS(in uint ID : SV_VERTEXID, out float4 Position : SV_POSITION, out float2 Coord : TEXCOORD0)
void Basic_VS(in uint ID : SV_VERTEXID, out float4 Position : SV_POSITION, out float2 TexCoord : TEXCOORD0)
{
Coord.x = (ID == 2) ? 2.0 : 0.0;
Coord.y = (ID == 1) ? 2.0 : 0.0;
Position = float4(Coord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
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 Abberation_PS(in float4 Position : SV_POSITION, in float2 Coord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
void Abberation_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
const float2 PixelSize = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
// Shift red channel
OutputColor0.r = tex2D(Sample_Color, Coord + _ShiftRed * PixelSize).r;
OutputColor0.r = tex2D(Sample_Color, TexCoord + _ShiftRed * PixelSize).r;
// Keep green channel to the center
OutputColor0.g = tex2D(Sample_Color, Coord + _ShiftGreen * PixelSize).g;
OutputColor0.g = tex2D(Sample_Color, TexCoord + _ShiftGreen * PixelSize).g;
// Shift blue channel
OutputColor0.b = tex2D(Sample_Color, Coord + _ShiftBlue * PixelSize).b;
OutputColor0.b = tex2D(Sample_Color, TexCoord + _ShiftBlue * PixelSize).b;
// Write alpha value
OutputColor0.a = 1.0;
}
Expand Down
18 changes: 9 additions & 9 deletions shaders/cAutoExposure.fx
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,29 @@ sampler2D Sample_Luma_LOD

// Vertex shaders

void Basic_VS(in uint ID : SV_VERTEXID, out float4 Position : SV_POSITION, out float2 Coord : TEXCOORD0)
void Basic_VS(in uint ID : SV_VERTEXID, out float4 Position : SV_POSITION, out float2 TexCoord : TEXCOORD0)
{
Coord.x = (ID == 2) ? 2.0 : 0.0;
Coord.y = (ID == 1) ? 2.0 : 0.0;
Position = float4(Coord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
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 Blit_PS(in float4 Position : SV_POSITION, in float2 Coord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
void Blit_PS(in float4 Position : SV_POSITION, in float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
float4 Color = tex2D(Sample_Color, Coord);
float4 Color = tex2D(Sample_Color, TexCoord);

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

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

// KeyValue is an exposure compensation curve
// Source: https://knarkowicz.wordpress.com/2016/01/09/automatic-exposure/
Expand Down
Loading

0 comments on commit 3ecb7e6

Please sign in to comment.