From 880b48485f797439a3c098582bb207b8d0fe3afc Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Fri, 28 Mar 2025 15:16:45 +0100 Subject: [PATCH] Slang shaders for buffer device address example --- .../slang/bufferdeviceaddress/cube.frag.slang | 20 ++++++++ .../slang/bufferdeviceaddress/cube.vert.slang | 47 +++++++++++++++++++ shaders/slang/compileshaders.py | 2 +- 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 shaders/slang/bufferdeviceaddress/cube.frag.slang create mode 100644 shaders/slang/bufferdeviceaddress/cube.vert.slang diff --git a/shaders/slang/bufferdeviceaddress/cube.frag.slang b/shaders/slang/bufferdeviceaddress/cube.frag.slang new file mode 100644 index 00000000..05b8e1d0 --- /dev/null +++ b/shaders/slang/bufferdeviceaddress/cube.frag.slang @@ -0,0 +1,20 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + + [[vk::binding(0, 0)]] +Sampler2D samplerColorMap; + +struct VSInput +{ + [[vk::location(1)]] float3 Color; + [[vk::location(2)]] float2 UV; +}; + +[shader("fragment")] +float4 main(VSInput input) +{ + return samplerColorMap.Sample(input.UV) * float4(input.Color, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/bufferdeviceaddress/cube.vert.slang b/shaders/slang/bufferdeviceaddress/cube.vert.slang new file mode 100644 index 00000000..2d419f5a --- /dev/null +++ b/shaders/slang/bufferdeviceaddress/cube.vert.slang @@ -0,0 +1,47 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + [[vk::location(0)]] float3 Pos; + [[vk::location(1)]] float3 Normal; + [[vk::location(2)]] float2 UV; + [[vk::location(3)]] float3 Color; +}; + +struct MatrixReference { + float4x4 matrix; +}; + +struct PushConsts { + // Pointer to the buffer with the scene's MVP matrix + ConstBufferPointer sceneDataReference; + // Pointer to the buffer for the data for each model + ConstBufferPointer modelDataReference; +}; +[[vk::push_constant]] PushConsts pushConstants; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + [[vk::location(0)]] float3 Normal; + [[vk::location(1)]] float3 Color; + [[vk::location(2)]] float2 UV; +}; + +[shader("vertex")] +VSOutput main(VSInput input) +{ + MatrixReference sceneData = pushConstants.sceneDataReference.get(); + MatrixReference modelData = pushConstants.modelDataReference.get(); + + VSOutput output; + output.Normal = input.Normal; + output.Color = input.Color; + output.UV = input.UV; + output.Pos = mul(sceneData.matrix, mul(modelData.matrix, float4(input.Pos.xyz, 1.0))); + return output; +} \ No newline at end of file diff --git a/shaders/slang/compileshaders.py b/shaders/slang/compileshaders.py index 2f5e2e83..cd3f54c1 100644 --- a/shaders/slang/compileshaders.py +++ b/shaders/slang/compileshaders.py @@ -41,6 +41,6 @@ for root, dirs, files in os.walk(dir_path): input_file = os.path.join(root, file) output_file = input_file + ".spv" output_file = output_file.replace(".slang", "") - res = subprocess.call("%s %s -profile spirv_1_4 -target spirv -o %s -entry %s" % (compiler_path, input_file, output_file, "main"), shell=True) + res = subprocess.call("%s %s -profile spirv_1_4 -matrix-layout-column-major -target spirv -o %s -entry %s" % (compiler_path, input_file, output_file, "main"), shell=True) if res != 0: sys.exit(res) \ No newline at end of file