Skip to content

Commit

Permalink
shader cleanup; minor bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Sep 27, 2024
1 parent b4ce4d6 commit 8ede829
Show file tree
Hide file tree
Showing 34 changed files with 267 additions and 269 deletions.
8 changes: 4 additions & 4 deletions data/pipelines/atmo.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ cbuffer Data : register (b4) {
float u_fog_top;
float u_fog_enabled;
float u_godrays_enabled;
uint u_output;
uint u_optical_depth;
uint u_depth_buffer;
uint u_inscatter;
RWTextureHandle u_output;
TextureHandle u_optical_depth;
TextureHandle u_depth_buffer;
TextureHandle u_inscatter;
};

// mie - Schlick appoximation phase function of Henyey-Greenstein
Expand Down
2 changes: 1 addition & 1 deletion data/pipelines/atmo_optical_depth.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cbuffer Data :register(b4) {
float u_fog_top;
float u_fog_enabled;
float u_godarys_enabled;
uint u_output;
RWTextureHandle u_output;
};

float3 getTopAtmo(float3 p, float3 dir) {
Expand Down
4 changes: 2 additions & 2 deletions data/pipelines/atmo_scattering.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ cbuffer Data : register(b4) {
float u_fog_top;
float u_fog_enabled;
float u_godarys_enabled;
uint u_inscatter;
uint u_optical_depth;
RWTextureHandle u_inscatter;
TextureHandle u_optical_depth;
};

float getOptDepthY(float3 position) {
Expand Down
21 changes: 10 additions & 11 deletions data/pipelines/avg_luminance.hlsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "pipelines/common.hlsli"

cbuffer Data : register (b4) {
float2 u_size;
float u_accomodation_speed;
uint u_image;
uint b_histogram;
TextureHandle u_image;
uint u_histogram;
};

groupshared uint histogram[256];
Expand All @@ -30,27 +29,27 @@ void main(uint3 local_thread_id : SV_GroupThreadID, uint3 thread_id : SV_Dispatc
const float range = 12.0;
const float min = -6;
#ifdef PASS0
bindless_rw_buffers[b_histogram].Store(local_thread_id.x * 4, 0);
bindless_rw_buffers[u_histogram].Store(local_thread_id.x * 4, 0);
#elif !defined PASS2
uint idx = local_thread_id.x + local_thread_id.y * 16;
histogram[idx] = 0;

AllMemoryBarrier();
GroupMemoryBarrierWithGroupSync();

if (all(thread_id.xy < uint2(u_size + 0.5))) {
float _luminance = luminance(sampleBindlessLod(LinearSamplerClamp, u_image, int2(thread_id.xy) / u_size, 0).rgb);
if (all(thread_id.xy < uint2(Global_framebuffer_size + 0.5))) {
float _luminance = luminance(sampleBindlessLod(LinearSamplerClamp, u_image, thread_id.xy * Global_rcp_framebuffer_size, 0).rgb);
uint bin = luminanceToBin(_luminance, min, 1 / range);
InterlockedAdd(histogram[bin], 1);
}
AllMemoryBarrier();
GroupMemoryBarrierWithGroupSync();

uint dummy;
bindless_rw_buffers[b_histogram].InterlockedAdd(idx * 4, histogram[idx], dummy);
bindless_rw_buffers[u_histogram].InterlockedAdd(idx * 4, histogram[idx], dummy);
#else
uint idx = local_thread_id.x;
histogram[idx] = bindless_rw_buffers[b_histogram].Load(idx * 4) * idx;
histogram[idx] = bindless_rw_buffers[u_histogram].Load(idx * 4) * idx;

AllMemoryBarrier();
GroupMemoryBarrierWithGroupSync();
Expand All @@ -65,12 +64,12 @@ void main(uint3 local_thread_id : SV_GroupThreadID, uint3 thread_id : SV_Dispatc

if (idx == 0) {
uint sum = histogram[0];
float avg_bin = sum / float(u_size.x * u_size.y);
float avg_bin = sum / float(Global_framebuffer_size.x * Global_framebuffer_size.y);
float avg_lum = binToLuminance(avg_bin, min, range);
float accumulator = asfloat(bindless_rw_buffers[b_histogram].Load(256 * 4));
float accumulator = asfloat(bindless_rw_buffers[u_histogram].Load(256 * 4));
accumulator += (avg_lum - accumulator) * (1 - exp(-Global_frame_time_delta * u_accomodation_speed));

bindless_rw_buffers[b_histogram].Store(256 * 4, asuint(accumulator));
bindless_rw_buffers[u_histogram].Store(256 * 4, asuint(accumulator));
}
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions data/pipelines/blit.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ cbuffer Drawcall : register(b4) {
float4 u_offsets;
uint2 u_position;
int2 u_scale;
uint u_input;
uint u_output;
TextureHandle u_input;
RWTextureHandle u_output;
};

[numthreads(16, 16, 1)]
Expand Down
61 changes: 12 additions & 49 deletions data/pipelines/bloom.hlsl
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
#include "pipelines/common.hlsli"

#ifdef EXTRACT
cbuffer Data : register(b4) {
float u_avg_lum_multiplier;
uint b_histogram;
uint u_input;
uint u_output;
};

[numthreads(16, 16, 1)]
void main(uint3 thread_id : SV_DispatchThreadID) {
float avg_lum = asfloat(bindless_buffers[b_histogram].Load(256 * 4));
float3 c = bindless_textures[u_input][thread_id.xy * 2].rgb
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 0)].rgb
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 1)].rgb
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(0, 1)].rgb;
float _luminance = luminance(c * 0.25);

float multiplier = saturate(1 + _luminance - avg_lum * u_avg_lum_multiplier);
bindless_rw_textures[u_output][thread_id.xy] = float4(c * multiplier, 1);
}
#elif defined DOWNSCALE
cbuffer Data : register(b4) {
uint u_input;
uint u_output;
};

[numthreads(16, 16, 1)]
void main(uint3 thread_id : SV_DispatchThreadID) {
float4 o_color = bindless_textures[u_input][thread_id.xy * 2]
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 0)]
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 1)]
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(0, 1)];
o_color *= 0.25;
bindless_rw_textures[u_output][thread_id.xy] = o_color;
}
#else
cbuffer Data : register(b4) {
uint u_input;
uint u_output;
};

[numthreads(16, 16, 1)]
void main(uint3 thread_id : SV_DispatchThreadID) {
float2 uv = thread_id.xy / float2(Global_framebuffer_size.xy);
float4 a = sampleBindlessLod(LinearSamplerClamp, u_input, uv, 0);
float4 b = bindless_rw_textures[u_output][thread_id.xy];
bindless_rw_textures[u_output][thread_id.xy] = a + b;
}
#endif
cbuffer Data : register(b4) {
TextureHandle u_input;
RWTextureHandle u_output;
};

[numthreads(16, 16, 1)]
void main(uint3 thread_id : SV_DispatchThreadID) {
float2 uv = thread_id.xy * Global_rcp_framebuffer_size;
float4 a = sampleBindlessLod(LinearSamplerClamp, u_input, uv, 0);
float4 b = bindless_rw_textures[u_output][thread_id.xy];
bindless_rw_textures[u_output][thread_id.xy] = a + b;
}
22 changes: 11 additions & 11 deletions data/pipelines/bloom_blur.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "pipelines/common.hlsli"

cbuffer Drawcall : register(b4) {
float4 u_inv_sm_size;
uint u_big;
uint u_small;
float4 u_rcp_size;
TextureHandle u_big;
TextureHandle u_small;
};

struct VSOutput {
Expand All @@ -21,21 +21,21 @@ VSOutput mainVS(uint vertex_id : SV_VertexID) {
output.tc0 = uv;
#ifdef BLUR_H
output.tc1 = float4(
uv.x + 1.3846153846 * u_inv_sm_size.x, uv.y,
uv.x + 3.2307692308 * u_inv_sm_size.x, uv.y
uv.x + 1.3846153846 * u_rcp_size.x, uv.y,
uv.x + 3.2307692308 * u_rcp_size.x, uv.y
);
output.tc2 = float4(
uv.x - 1.3846153846 * u_inv_sm_size.x, uv.y,
uv.x - 3.2307692308 * u_inv_sm_size.x, uv.y
uv.x - 1.3846153846 * u_rcp_size.x, uv.y,
uv.x - 3.2307692308 * u_rcp_size.x, uv.y
);
#else
output.tc1 = float4(
uv.x, uv.y + 1.3846153846 * u_inv_sm_size.y,
uv.x, uv.y + 3.2307692308 * u_inv_sm_size.y
uv.x, uv.y + 1.3846153846 * u_rcp_size.y,
uv.x, uv.y + 3.2307692308 * u_rcp_size.y
);
output.tc2 = float4(
uv.x, uv.y - 1.3846153846 * u_inv_sm_size.y,
uv.x, uv.y - 3.2307692308 * u_inv_sm_size.y
uv.x, uv.y - 1.3846153846 * u_rcp_size.y,
uv.x, uv.y - 3.2307692308 * u_rcp_size.y
);
#endif
return output;
Expand Down
16 changes: 16 additions & 0 deletions data/pipelines/bloom_downscale.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "pipelines/common.hlsli"

cbuffer Data : register(b4) {
TextureHandle u_input;
RWTextureHandle u_output;
};

[numthreads(16, 16, 1)]
void main(uint3 thread_id : SV_DispatchThreadID) {
float4 o_color = bindless_textures[u_input][thread_id.xy * 2]
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 0)]
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 1)]
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(0, 1)];
o_color *= 0.25;
bindless_rw_textures[u_output][thread_id.xy] = o_color;
}
21 changes: 21 additions & 0 deletions data/pipelines/bloom_extract.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "pipelines/common.hlsli"

cbuffer Data : register(b4) {
float u_avg_lum_multiplier;
uint u_histogram;
TextureHandle u_input;
RWTextureHandle u_output;
};

[numthreads(16, 16, 1)]
void main(uint3 thread_id : SV_DispatchThreadID) {
float avg_lum = asfloat(bindless_buffers[u_histogram].Load(256 * 4));
float3 c = bindless_textures[u_input][thread_id.xy * 2].rgb
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 0)].rgb
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(1, 1)].rgb
+ bindless_textures[u_input][thread_id.xy * 2 + uint2(0, 1)].rgb;
float _luminance = luminance(c * 0.25);

float multiplier = saturate(1 + _luminance - avg_lum * u_avg_lum_multiplier);
bindless_rw_textures[u_output][thread_id.xy] = float4(c * multiplier, 1);
}
4 changes: 2 additions & 2 deletions data/pipelines/bloom_tonemap.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

cbuffer Drawcall : register (b4) {
float u_exposure;
uint u_input;
TextureHandle u_input;
uint u_accum;
uint u_output;
RWTextureHandle u_output;
};

[numthreads(16, 16, 1)]
Expand Down
12 changes: 6 additions & 6 deletions data/pipelines/blur.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

cbuffer Drawcall : register(b4) {
float4 u_inv_sm_size;
uint u_input;
TextureHandle u_input;
};

struct VSOutput {
Expand Down Expand Up @@ -41,11 +41,11 @@ VSOutput mainVS(uint vertex_id : SV_VertexID) {
}

float4 mainPS(VSOutput input) : SV_Target {
float2 uv0 = saturate(input.tc0.xy);
float2 uv1 = saturate(input.tc1.xy);
float2 uv2 = saturate(input.tc1.zw);
float2 uv3 = saturate(input.tc2.xy);
float2 uv4 = saturate(input.tc2.zw);
float2 uv0 = input.tc0.xy;
float2 uv1 = input.tc1.xy;
float2 uv2 = input.tc1.zw;
float2 uv3 = input.tc2.xy;
float2 uv4 = input.tc2.zw;
float4 c0 = sampleBindless(LinearSamplerClamp, u_input, uv0);
float4 c1 = sampleBindless(LinearSamplerClamp, u_input, uv1);
float4 c2 = sampleBindless(LinearSamplerClamp, u_input, uv2);
Expand Down
9 changes: 5 additions & 4 deletions data/pipelines/ibl_filter.hlsl
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
//@surface

#include "pipelines/common.hlsli"

cbuffer Drawcall : register(b4) {
float u_filter_roughness;
int u_face;
int u_mip;
uint u_texture;
TextureHandle u_texture;
};

struct Output {
struct VSOutput {
float2 uv : TEXCOORD0;
float4 position : SV_POSITION;
};

Output mainVS(uint vertex_id : SV_VertexID) {
Output output;
VSOutput mainVS(uint vertex_id : SV_VertexID) {
VSOutput output;
float4 pos = fullscreenQuad(vertex_id, output.uv);
pos.xy = pos.xy;
pos.y = -pos.y;
Expand Down
9 changes: 5 additions & 4 deletions data/pipelines/impostor.hlsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@surface
#include "pipelines/common.hlsli"

//@texture_slot "Albedo", "textures/common/white.tga"
//@texture_slot "Normal", "", "HAS_NORMAL"
Expand All @@ -16,6 +15,8 @@
//@uniform "Center", "float3", {0, 0, 0}
//@uniform "Radius", "float", 1

#include "pipelines/common.hlsli"

struct VSOutput {
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
Expand All @@ -27,7 +28,7 @@ struct VSOutput {
float lod : TEXCOORD5;
float4 position : SV_POSITION;
};
struct Input {
struct VSInput {
float3 position : TEXCOORD0;
float2 uv : TEXCOORD1;
#define ATTR(X) TEXCOORD##X
Expand Down Expand Up @@ -59,7 +60,7 @@ float2 dirToGrid(float3 vec) {


#ifdef USE_MATRIX
VSOutput mainVS(Input input) {
VSOutput mainVS(VSInput input) {
VSOutput output;
float3x3 to_model_space = (float3x3)model_mtx;
#ifdef DEPTH
Expand Down Expand Up @@ -101,7 +102,7 @@ float2 dirToGrid(float3 vec) {
return output;
}
#else
VSOutput mainVS(Input input) {
VSOutput mainVS(VSInput input) {
VSOutput output;
float3x3 tangent_space;

Expand Down
6 changes: 3 additions & 3 deletions data/pipelines/impostor_shadow.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ cbuffer Data : register(b4){
int2 u_tile_size;
int u_size;
float u_radius;
uint u_depth;
uint u_normalmap;
uint u_output;
TextureHandle u_depth;
TextureHandle u_normalmap;
RWTextureHandle u_output;
};

float3 impostorToWorld(float2 uv) {
Expand Down
Loading

0 comments on commit 8ede829

Please sign in to comment.