From 4df49dba717f0a1b718092c50323f11351e3cfeb Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Sat, 3 May 2025 09:42:32 +0200 Subject: [PATCH] Add slang shaders for additional samples --- shaders/slang/bloom/colorpass.slang | 45 ++++++++++++ shaders/slang/bloom/gaussblur.slang | 63 +++++++++++++++++ shaders/slang/bloom/phongpass.slang | 70 +++++++++++++++++++ shaders/slang/bloom/skybox.slang | 41 +++++++++++ shaders/slang/dynamicuniformbuffer/base.slang | 47 +++++++++++++ 5 files changed, 266 insertions(+) create mode 100644 shaders/slang/bloom/colorpass.slang create mode 100644 shaders/slang/bloom/gaussblur.slang create mode 100644 shaders/slang/bloom/phongpass.slang create mode 100644 shaders/slang/bloom/skybox.slang create mode 100644 shaders/slang/dynamicuniformbuffer/base.slang diff --git a/shaders/slang/bloom/colorpass.slang b/shaders/slang/bloom/colorpass.slang new file mode 100644 index 00000000..40ada51e --- /dev/null +++ b/shaders/slang/bloom/colorpass.slang @@ -0,0 +1,45 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float4 Pos; + float2 UV; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Color; + float2 UV; +}; + +struct UBO +{ + float4x4 projection; + float4x4 view; + float4x4 model; +}; +ConstantBuffer ubo; + +Sampler2D colorMapSampler; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.UV = input.UV; + output.Color = input.Color; + output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, input.Pos))); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + return float4(input.Color, 1); +} \ No newline at end of file diff --git a/shaders/slang/bloom/gaussblur.slang b/shaders/slang/bloom/gaussblur.slang new file mode 100644 index 00000000..e381a27a --- /dev/null +++ b/shaders/slang/bloom/gaussblur.slang @@ -0,0 +1,63 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float2 UV; +}; + +struct UBO +{ + float blurScale; + float blurStrength; +}; +ConstantBuffer ubo; + +Sampler2D samplerColor; + +[[SpecializationConstant]] const int blurdirection = 0; + +[shader("vertex")] +VSOutput vertexMain(uint VertexIndex: SV_VertexID) +{ + VSOutput output; + output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2); + output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + float weight[5]; + weight[0] = 0.227027; + weight[1] = 0.1945946; + weight[2] = 0.1216216; + weight[3] = 0.054054; + weight[4] = 0.016216; + + float2 textureSize; + samplerColor.GetDimensions(textureSize.x, textureSize.y); + float2 tex_offset = 1.0 / textureSize * ubo.blurScale; // gets size of single texel + float3 result = samplerColor.Sample(input.UV).rgb * weight[0]; // current fragment's contribution + for(int i = 1; i < 5; ++i) + { + if (blurdirection == 1) + { + // H + result += samplerColor.Sample(input.UV + float2(tex_offset.x * i, 0.0)).rgb * weight[i] * ubo.blurScale; + result += samplerColor.Sample(input.UV - float2(tex_offset.x * i, 0.0)).rgb * weight[i] * ubo.blurScale; + } + else + { + // V + result += samplerColor.Sample(input.UV + float2(0.0, tex_offset.y * i)).rgb * weight[i] * ubo.blurScale; + result += samplerColor.Sample(input.UV - float2(0.0, tex_offset.y * i)).rgb * weight[i] * ubo.blurScale; + } + } + return float4(result, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/bloom/phongpass.slang b/shaders/slang/bloom/phongpass.slang new file mode 100644 index 00000000..8d581753 --- /dev/null +++ b/shaders/slang/bloom/phongpass.slang @@ -0,0 +1,70 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float4 Pos; + float2 UV; + float3 Color; + float3 Normal; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Normal; + float2 UV; + float3 Color; + float3 ViewVec; + float3 LightVec; +}; + +struct UBO +{ + float4x4 projection; + float4x4 view; + float4x4 model; +}; +ConstantBuffer ubo; + +Sampler2D colorMapSampler; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.Normal = input.Normal; + output.Color = input.Color; + output.UV = input.UV; + output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, input.Pos))); + + float3 lightPos = float3(-5.0, -5.0, 0.0); + float4 pos = mul(ubo.view, mul(ubo.model, input.Pos)); + output.Normal = mul((float4x3)mul(ubo.view, ubo.model), input.Normal).xyz; + output.LightVec = lightPos - pos.xyz; + output.ViewVec = -pos.xyz; + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + float3 ambient = float3(0.0f, 0.0f, 0.0f); + + // Adjust light calculations for glow color + if ((input.Color.r >= 0.9) || (input.Color.g >= 0.9) || (input.Color.b >= 0.9)) + { + ambient = input.Color * 0.25; + } + + float3 N = normalize(input.Normal); + float3 L = normalize(input.LightVec); + float3 V = normalize(input.ViewVec); + float3 R = reflect(-L, N); + float3 diffuse = max(dot(N, L), 0.0) * input.Color; + float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.75f, 0.75f, 0.75f); + return float4(ambient + diffuse + specular, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/bloom/skybox.slang b/shaders/slang/bloom/skybox.slang new file mode 100644 index 00000000..3b21695b --- /dev/null +++ b/shaders/slang/bloom/skybox.slang @@ -0,0 +1,41 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; +} + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 UVW; +}; + +struct UBO +{ + float4x4 projection; + float4x4 view; + float4x4 model; +}; +ConstantBuffer ubo; + +SamplerCube samplerCubeMap; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.UVW = input.Pos; + output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0)))); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + return samplerCubeMap.Sample(input.UVW); +} \ No newline at end of file diff --git a/shaders/slang/dynamicuniformbuffer/base.slang b/shaders/slang/dynamicuniformbuffer/base.slang new file mode 100644 index 00000000..ec8e444d --- /dev/null +++ b/shaders/slang/dynamicuniformbuffer/base.slang @@ -0,0 +1,47 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Color; +}; + +struct UboView +{ + float4x4 projection; + float4x4 view; +}; +ConstantBuffer uboView; + +struct UboInstance +{ + float4x4 model; +}; +ConstantBuffer uboInstance; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.Color = input.Color; + float4x4 modelView = mul(uboView.view, uboInstance.model); + float3 worldPos = mul(modelView, float4(input.Pos, 1.0)).xyz; + output.Pos = mul(uboView.projection, mul(modelView, float4(input.Pos.xyz, 1.0))); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + return float4(input.Color, 1.0); +} \ No newline at end of file