diff --git a/shaders/slang/multisampling/mesh.slang b/shaders/slang/multisampling/mesh.slang new file mode 100644 index 00000000..840c1c24 --- /dev/null +++ b/shaders/slang/multisampling/mesh.slang @@ -0,0 +1,63 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; + float3 Normal; + float2 UV; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Normal; + float3 Color; + float2 UV; + float3 ViewVec; + float3 LightVec; +}; + +struct UBO +{ + float4x4 projection; + float4x4 model; + float4 lightPos; +}; +ConstantBuffer ubo; + +[[vk::binding(0,1)]] Sampler2D samplerColorMap; + +[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.model, float4(input.Pos.xyz, 1.0))); + + float4 pos = mul(ubo.model, float4(input.Pos, 0.0)); + output.Normal = mul((float3x3)ubo.model, input.Normal); + float3 lPos = mul((float3x3)ubo.model, ubo.lightPos.xyz); + output.LightVec = lPos - input.Pos; + output.ViewVec = -input.Pos; + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + float4 color = samplerColorMap.Sample(input.UV) * float4(input.Color, 1.0); + 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.15) * input.Color; + float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75); + return float4(diffuse * color.rgb + specular, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/multiview/multiview.slang b/shaders/slang/multiview/multiview.slang new file mode 100644 index 00000000..a4edbfa5 --- /dev/null +++ b/shaders/slang/multiview/multiview.slang @@ -0,0 +1,60 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; + float3 Normal; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Normal; + float3 Color; + float3 ViewVec; + float3 LightVec; +}; + +struct UBO +{ + float4x4 projection[2]; + float4x4 modelview[2]; + float4 lightPos; +}; +ConstantBuffer ubo; + +[shader("vertex")] +VSOutput vertexMain(VSInput input, uint ViewIndex: SV_ViewID) +{ + VSOutput output; + output.Color = input.Color; + output.Normal = mul((float3x3)ubo.modelview[ViewIndex], input.Normal); + + float4 pos = float4(input.Pos.xyz, 1.0); + float4 worldPos = mul(ubo.modelview[ViewIndex], pos); + + float3 lPos = mul(ubo.modelview[ViewIndex], ubo.lightPos).xyz; + output.LightVec = lPos - worldPos.xyz; + output.ViewVec = -worldPos.xyz; + + output.Pos = mul(ubo.projection[ViewIndex], worldPos); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + float3 N = normalize(input.Normal); + float3 L = normalize(input.LightVec); + float3 V = normalize(input.ViewVec); + float3 R = reflect(-L, N); + float3 ambient = float3(0.1, 0.1, 0.1); + float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0); + float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75); + return float4((ambient + diffuse) * input.Color.rgb + specular, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/multiview/viewdisplay.slang b/shaders/slang/multiview/viewdisplay.slang new file mode 100644 index 00000000..4c982738 --- /dev/null +++ b/shaders/slang/multiview/viewdisplay.slang @@ -0,0 +1,46 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float2 UV; +}; + +struct UBO +{ + float4x4 projection[2]; + float4x4 modelview[2]; + float4 lightPos; + float distortionAlpha; +}; +ConstantBuffer ubo; + +Sampler2DArray samplerView; + +[[SpecializationConstant]] const float VIEW_LAYER = 0.0f; + +[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) +{ + const float alpha = ubo.distortionAlpha; + + float2 p1 = float2(2.0 * input.UV - 1.0); + float2 p2 = p1 / (1.0 - alpha * length(p1)); + p2 = (p2 + 1.0) * 0.5; + + bool inside = ((p2.x >= 0.0) && (p2.x <= 1.0) && (p2.y >= 0.0) && (p2.y <= 1.0)); + return inside ? samplerView.Sample(float3(p2, VIEW_LAYER)) : float4(0.0, 0.0, 0.0, 0.0); +} \ No newline at end of file diff --git a/shaders/slang/negativeviewportheight/quad.slang b/shaders/slang/negativeviewportheight/quad.slang new file mode 100644 index 00000000..1ea13bb3 --- /dev/null +++ b/shaders/slang/negativeviewportheight/quad.slang @@ -0,0 +1,30 @@ +// Copyright 2020 Google LLC + +struct VSInput +{ + float3 Pos; + float2 UV; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float2 UV; +}; + +Sampler2D samplerColor; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.UV = input.UV; + output.Pos = float4(input.Pos, 1.0f); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + return samplerColor.Sample(input.UV); +} \ No newline at end of file diff --git a/shaders/slang/pushdescriptors/cube.slang b/shaders/slang/pushdescriptors/cube.slang new file mode 100644 index 00000000..372c5eaa --- /dev/null +++ b/shaders/slang/pushdescriptors/cube.slang @@ -0,0 +1,51 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; + float3 Normal; + float2 UV; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Normal; + float3 Color; + float2 UV; +}; + +struct UBOCamera { + float4x4 projection; + float4x4 view; +}; +ConstantBuffer uboCamera; + +struct UBOModel { + float4x4 local; +}; +ConstantBuffer uboModel; + +Sampler2D samplerColorMap; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.Normal = input.Normal; + output.Color = input.Color; + output.UV = input.UV; + output.Pos = mul(uboCamera.projection, mul(uboCamera.view, mul(uboModel.local, float4(input.Pos.xyz, 1.0)))); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + return samplerColorMap.Sample(input.UV) * float4(input.Color, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/specializationconstants/uber.slang b/shaders/slang/specializationconstants/uber.slang new file mode 100644 index 00000000..1d7c1c3c --- /dev/null +++ b/shaders/slang/specializationconstants/uber.slang @@ -0,0 +1,110 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; + float3 Normal; + float2 UV; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Normal; + float3 Color; + float2 UV; + float3 ViewVec; + float3 LightVec; +}; + +struct UBO +{ + float4x4 projection; + float4x4 model; + float4 lightPos; +}; +ConstantBuffer ubo; + +Sampler2D samplerColormap; +Sampler2D samplerDiscard; + +// We use this constant to control the flow of the shader depending on the +// lighting model selected at pipeline creation time +[[SpecializationConstant]] const int LIGHTING_MODEL = 0; +// Parameter for the toon shading part of the shader +[[SpecializationConstant]] const int PARAM_TOON_DESATURATION = 0; + +[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.model, float4(input.Pos.xyz, 1.0))); + + float4 pos = mul(ubo.model, float4(input.Pos, 1.0)); + output.Normal = mul((float3x3)ubo.model, input.Normal); + float3 lPos = mul((float3x3)ubo.model, ubo.lightPos.xyz); + output.LightVec = lPos - pos.xyz; + output.ViewVec = -pos.xyz; + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) : SV_TARGET +{ + switch (LIGHTING_MODEL) { + case 0: // Phong + { + float3 ambient = input.Color * float3(0.25, 0.25, 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), 32.0) * float3(0.75, 0.75, 0.75); + return float4(ambient + diffuse * 1.75 + specular, 1.0); + } + case 1: // Toon + { + + float3 N = normalize(input.Normal); + float3 L = normalize(input.LightVec); + float intensity = dot(N,L); + float3 color; + if (intensity > 0.98) + color = input.Color * 1.5; + else if (intensity > 0.9) + color = input.Color * 1.0; + else if (intensity > 0.5) + color = input.Color * 0.6; + else if (intensity > 0.25) + color = input.Color * 0.4; + else + color = input.Color * 0.2; + // Desaturate a bit + color = float3(lerp(color, dot(float3(0.2126,0.7152,0.0722), color).xxx, asfloat(PARAM_TOON_DESATURATION))); + return float4(color, 1); + } + case 2: // Textured + { + float4 color = samplerColormap.Sample(input.UV).rrra; + float3 ambient = color.rgb * float3(0.25, 0.25, 0.25) * input.Color; + 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) * color.rgb; + float specular = pow(max(dot(R, V), 0.0), 32.0) * color.a; + return float4(ambient + diffuse + specular.xxx, 1.0); + } + } + + return float4(0, 0, 0, 0); +} \ No newline at end of file diff --git a/shaders/slang/sphericalenvmapping/sem.slang b/shaders/slang/sphericalenvmapping/sem.slang new file mode 100644 index 00000000..28f75f31 --- /dev/null +++ b/shaders/slang/sphericalenvmapping/sem.slang @@ -0,0 +1,58 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float4 Pos; + float3 Normal; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Color; + float3 EyePos; + float3 Normal; + int TexIndex; +}; + +struct UBO +{ + float4x4 projection; + float4x4 model; + float4x4 normal; + float4x4 view; + int texIndex; +}; +ConstantBuffer ubo; + +Sampler2DArray matCapSampler; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.Color = input.Color; + float4x4 modelView = mul(ubo.view, ubo.model); + output.EyePos = normalize(mul(modelView, input.Pos).xyz); + output.TexIndex = ubo.texIndex; + output.Normal = normalize(mul((float3x3)ubo.normal, input.Normal)); + float3 r = reflect(output.EyePos, output.Normal); + float m = 2.0 * sqrt(pow(r.x, 2.0) + pow(r.y, 2.0) + pow(r.z + 1.0, 2.0)); + output.Pos = mul(ubo.projection, mul(modelView, input.Pos)); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + float3 r = reflect( input.EyePos, input.Normal ); + float3 r2 = float3( r.x, r.y, r.z + 1.0 ); + float m = 2.0 * length( r2 ); + float2 vN = r.xy / m + .5; + return float4(matCapSampler.Sample(float3(vN, input.TexIndex)).rgb * (clamp(input.Color.r * 2, 0.0, 1.0)), 1.0); +}