From 39363d261e6af6d6c231588fb792a6ded36af787 Mon Sep 17 00:00:00 2001 From: Honeybunch Date: Tue, 27 Aug 2024 18:04:32 -0700 Subject: [PATCH] Making progress on getting a working gltf shader from slang --- CMakeLists.txt | 4 ++-- addons/water/include/ocean.hlsli | 14 +++++++------- addons/water/source/ocean.hlsl | 4 ++-- include/common.hlsli | 18 +++++++++--------- include/gltf.hlsli | 24 ++++++++++++------------ source/gltf.slang | 8 ++++++-- source/tb_mesh_rnd_sys.c | 8 ++++---- source/tb_view_system.c | 24 ++++++------------------ vcpkg.json | 4 ++++ 9 files changed, 52 insertions(+), 56 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e4ff6e4..3e57956c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,8 +193,8 @@ function(cook_shaders out_shader_sources out_shader_headers) add_custom_command( OUTPUT ${out_paths} COMMAND ${CMAKE_COMMAND} -E make_directory ${shader_out_path} - COMMAND ${SLANG} -DTB_SHADER=1 -profile sm_6_5 -stage vertex -entry vert -target spirv $<$:-O0> $<$:-g> -I ${shader_include_dir} -I ${engine_shader_include_dir} -o ${vert_out_path} ${shader} - COMMAND ${SLANG} -DTB_SHADER=1 -profile sm_6_5 -stage fragment -entry frag -target spirv $<$:-O0> $<$:-g> -I ${shader_include_dir} -I ${engine_shader_include_dir} -o ${frag_out_path} ${shader} + COMMAND ${SLANG} -DTB_SHADER=1 -profile sm_6_5 -stage vertex -entry vert -target spirv $<$:-O0> $<$>:-g> -I ${shader_include_dir} -I ${engine_shader_include_dir} -o ${vert_out_path} ${shader} + COMMAND ${SLANG} -DTB_SHADER=1 -profile sm_6_5 -stage fragment -entry frag -target spirv $<$:-O0> $<$>:-g> -I ${shader_include_dir} -I ${engine_shader_include_dir} -o ${frag_out_path} ${shader} MAIN_DEPENDENCY ${shader} DEPENDS ${shader_includes} ) diff --git a/addons/water/include/ocean.hlsli b/addons/water/include/ocean.hlsli index e8faba14..1ef9bd50 100644 --- a/addons/water/include/ocean.hlsli +++ b/addons/water/include/ocean.hlsli @@ -25,14 +25,14 @@ _Static_assert(sizeof(OceanPushConstants) <= PUSH_CONSTANT_BYTES, #ifdef __HLSL_VERSION -#define OCEAN_SET(space) \ - ConstantBuffer ocean_data : register(b0, space); \ - Texture2D depth_map : register(t1, space); \ - Texture2D color_map : register(t2, space); \ - SamplerState material_sampler : register(s3, space); \ - SamplerComparisonState shadow_sampler : register(s4, space); \ +#define OCEAN_SET(b) \ + [[vk::binding(b, 0)]] ConstantBuffer ocean_data; \ + [[vk::binding(b, 1)]] Texture2D depth_map; \ + [[vk::binding(b, 2)]] Texture2D color_map; \ + [[vk::binding(b, 3)]] SamplerState material_sampler; \ + [[vk::binding(b, 4)]] SamplerComparisonState shadow_sampler; \ [[vk::push_constant]] \ - ConstantBuffer consts : register(b5, space); + ConstantBuffer consts void gerstner_wave(TbOceanWave wave, float time, inout float3 pos, inout float3 tangent, inout float3 binormal) { diff --git a/addons/water/source/ocean.hlsl b/addons/water/source/ocean.hlsl index 4fd9a155..80112258 100644 --- a/addons/water/source/ocean.hlsl +++ b/addons/water/source/ocean.hlsl @@ -6,8 +6,8 @@ #include "ocean.hlsli" #include "pbr.hlsli" -OCEAN_SET(space0); -GLTF_VIEW_SET(space1); +OCEAN_SET(0); +GLTF_VIEW_SET(1); struct VertexIn { int3 local_pos : SV_POSITION; diff --git a/include/common.hlsli b/include/common.hlsli index 1841cc96..f094f0e6 100644 --- a/include/common.hlsli +++ b/include/common.hlsli @@ -101,16 +101,16 @@ typedef struct TB_GPU_STRUCT TbCommonObjectData { // Macros for declaring access to common toybox descriptor sets // that represent global loaded resource tables -#define TB_TEXTURE_SET(b) [[vk::binding(b, 0)]] Texture2D gltf_textures[]; +#define TB_TEXTURE_SET(b) [[vk::binding(0, b)]] Texture2D gltf_textures[]; #define TB_OBJECT_SET(b) \ - [[vk::binding(b, 0)]] StructuredBuffer object_data; -#define TB_IDX_SET(b) [[vk::binding(b, 0)]] RWBuffer idx_buffers[]; -#define TB_POS_SET(b) [[vk::binding(b, 0)]] RWBuffer pos_buffers[]; -#define TB_NORM_SET(b) [[vk::binding(b, 0)]] RWBuffer norm_buffers[]; -#define TB_TAN_SET(b) [[vk::binding(b, 0)]] RWBuffer tan_buffers[]; -#define TB_UV0_SET(b) [[vk::binding(b, 0)]] RWBuffer uv0_buffers[]; -#define TB_JOINT_SET(b) [[vk::binding(b, 0)]] RWBuffer joint_buffers[]; -#define TB_WEIGHT_SET(b) [[vk::binding(b, 0)]] RWBuffer weight_buffers[]; + [[vk::binding(0, b)]] StructuredBuffer object_data; +#define TB_IDX_SET(b) [[vk::binding(0, b)]] RWBuffer idx_buffers[]; +#define TB_POS_SET(b) [[vk::binding(0, b)]] RWBuffer pos_buffers[]; +#define TB_NORM_SET(b) [[vk::binding(0, b)]] RWBuffer norm_buffers[]; +#define TB_TAN_SET(b) [[vk::binding(0, b)]] RWBuffer tan_buffers[]; +#define TB_UV0_SET(b) [[vk::binding(0, b)]] RWBuffer uv0_buffers[]; +#define TB_JOINT_SET(b) [[vk::binding(0, b)]] RWBuffer joint_buffers[]; +#define TB_WEIGHT_SET(b) [[vk::binding(0, b)]] RWBuffer weight_buffers[]; // Common input layout info and permutation settings #define TB_INPUT_PERM_NONE 0x00000000 diff --git a/include/gltf.hlsli b/include/gltf.hlsli index 66972a34..6f3a8166 100644 --- a/include/gltf.hlsli +++ b/include/gltf.hlsli @@ -130,22 +130,22 @@ float2 uv_transform(int2 quant_uv, TextureTransform trans) { // by specific systems #define GLTF_MATERIAL_SET(b) \ - [[vk::binding(b, 0)]] SamplerState material_sampler; \ - [[vk::binding(b, 1)]] SamplerComparisonState shadow_sampler; \ - [[vk::binding(b, 2)]] StructuredBuffer gltf_data[]; + [[vk::binding(0, b)]] SamplerState material_sampler; \ + [[vk::binding(1, b)]] SamplerComparisonState shadow_sampler; \ + [[vk::binding(2, b)]] StructuredBuffer gltf_data[]; #define GLTF_DRAW_SET(b) \ - [[vk::binding(b, 0)]] StructuredBuffer draw_data; + [[vk::binding(0, b)]] StructuredBuffer draw_data; #define GLTF_VIEW_SET(b) \ - [[vk::binding(b, 0)]] ConstantBuffer camera_data; \ - [[vk::binding(b, 1)]] TextureCube irradiance_map; \ - [[vk::binding(b, 2)]] TextureCube prefiltered_map; \ - [[vk::binding(b, 3)]] Texture2D brdf_lut; \ - [[vk::binding(b, 4)]] ConstantBuffer light_data; \ - [[vk::binding(b, 5)]] Texture2DArray shadow_map; \ - [[vk::binding(b, 6)]] SamplerState filtered_env_sampler; \ - [[vk::binding(b, 7)]] SamplerState brdf_sampler; + [[vk::binding(0, b)]] ConstantBuffer camera_data; \ + [[vk::binding(1, b)]] TextureCube irradiance_map; \ + [[vk::binding(2, b)]] TextureCube prefiltered_map; \ + [[vk::binding(3, b)]] Texture2D brdf_lut; \ + [[vk::binding(4, b)]] ConstantBuffer light_data; \ + [[vk::binding(5, b)]] Texture2DArray shadow_map; \ + [[vk::binding(6, b)]] SamplerState filtered_env_sampler; \ + [[vk::binding(7, b)]] SamplerState brdf_sampler; #define GLTF_OPAQUE_LIGHTING(out, color, normal, view, refl, s_uv, met, rough) \ { \ diff --git a/source/gltf.slang b/source/gltf.slang index d42a1fb8..e58c59b8 100644 --- a/source/gltf.slang +++ b/source/gltf.slang @@ -15,10 +15,12 @@ TB_UV0_SET(9) TB_JOINT_SET(10) TB_WEIGHT_SET(11) +// PROBLEM: VkGraphicsPipelineCreateInfo thinks there is a vertex input here +// when there is not struct VertexIn { int32_t vert_idx : SV_VertexID; - [[KnownBuiltin("DrawIndex")]] - uint32_t draw_idx : POSITION0; + [KnownBuiltin("DrawIndex")] + uint32_t draw_idx; }; struct Interpolators { @@ -33,6 +35,7 @@ struct Interpolators { float3x3 tbn : TEXCOORD2; }; +[shader("vertex")] Interpolators vert(VertexIn i) { TbGLTFDrawData draw = tb_get_gltf_draw_data(i.draw_idx, draw_data); int32_t vert_perm = draw.perm; @@ -81,6 +84,7 @@ Interpolators vert(VertexIn i) { return o; } +[shader("fragment")] float4 frag(Interpolators i, bool front_face: SV_IsFrontFace) : SV_TARGET { TbGLTFMaterialData gltf = tb_get_gltf_mat_data(i.mat_idx, gltf_data); diff --git a/source/tb_mesh_rnd_sys.c b/source/tb_mesh_rnd_sys.c index d62857a7..99e09998 100644 --- a/source/tb_mesh_rnd_sys.c +++ b/source/tb_mesh_rnd_sys.c @@ -237,13 +237,13 @@ VkPipeline create_opaque_mesh_pipeline(void *args) { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vert_mod, - .pName = "vert", + .pName = "main", }, { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = frag_mod, - .pName = "frag", + .pName = "main", }}, .pVertexInputState = &(VkPipelineVertexInputStateCreateInfo){ @@ -373,13 +373,13 @@ VkPipeline create_transparent_mesh_pipeline(void *args) { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vert_mod, - .pName = "vert", + .pName = "main", }, { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = frag_mod, - .pName = "frag", + .pName = "main", }}, .pVertexInputState = &(VkPipelineVertexInputStateCreateInfo){ diff --git a/source/tb_view_system.c b/source/tb_view_system.c index 79df17ae..72caa9b7 100644 --- a/source/tb_view_system.c +++ b/source/tb_view_system.c @@ -69,9 +69,9 @@ TbViewSystem create_view_system(TbAllocator gp_alloc, TbAllocator tmp_alloc, { VkDescriptorSetLayoutCreateInfo create_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = 9, + .bindingCount = 8, .pBindings = - (VkDescriptorSetLayoutBinding[9]){ + (VkDescriptorSetLayoutBinding[8]){ { .binding = 0, .descriptorCount = 1, @@ -113,18 +113,12 @@ TbViewSystem create_view_system(TbAllocator gp_alloc, TbAllocator tmp_alloc, { .binding = 6, .descriptorCount = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, - .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, - }, - { - .binding = 7, - .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, .pImmutableSamplers = &sys.filtered_env_sampler, }, { - .binding = 8, + .binding = 7, .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, @@ -141,9 +135,9 @@ TbViewSystem create_view_system(TbAllocator gp_alloc, TbAllocator tmp_alloc, VkDescriptorSetLayoutCreateInfo create_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_DESCRIPTOR_BUFFER_BIT_EXT, - .bindingCount = 9, + .bindingCount = 8, .pBindings = - (VkDescriptorSetLayoutBinding[9]){ + (VkDescriptorSetLayoutBinding[8]){ { .binding = 0, .descriptorCount = 1, @@ -185,18 +179,12 @@ TbViewSystem create_view_system(TbAllocator gp_alloc, TbAllocator tmp_alloc, { .binding = 6, .descriptorCount = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, - .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, - }, - { - .binding = 7, - .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, .pImmutableSamplers = &sys.filtered_env_sampler, }, { - .binding = 8, + .binding = 7, .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, diff --git a/vcpkg.json b/vcpkg.json index a5d56173..1f07e506 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -88,6 +88,10 @@ "name": "sdl3-mixer", "version": "3.0.0-087004f" }, + { + "name": "shader-slang", + "version": "2024.1.22" + }, { "name": "tracy", "version": "0.10.0"