Skip to content

Commit

Permalink
Making progress on getting a working gltf shader from slang
Browse files Browse the repository at this point in the history
  • Loading branch information
Honeybunch committed Aug 28, 2024
1 parent 0158274 commit 39363d2
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 56 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<$<CONFIG:Debug>:-O0> $<$<CONFIG:Debug>:-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 $<$<CONFIG:Debug>:-O0> $<$<CONFIG:Debug>:-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 $<$<CONFIG:Debug>:-O0> $<$<NOT:$<CONFIG:Release>>:-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 $<$<CONFIG:Debug>:-O0> $<$<NOT:$<CONFIG:Release>>:-g> -I ${shader_include_dir} -I ${engine_shader_include_dir} -o ${frag_out_path} ${shader}
MAIN_DEPENDENCY ${shader}
DEPENDS ${shader_includes}
)
Expand Down
14 changes: 7 additions & 7 deletions addons/water/include/ocean.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ _Static_assert(sizeof(OceanPushConstants) <= PUSH_CONSTANT_BYTES,

#ifdef __HLSL_VERSION

#define OCEAN_SET(space) \
ConstantBuffer<OceanData> 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<OceanData> 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<OceanPushConstants> consts : register(b5, space);
ConstantBuffer<OceanPushConstants> consts

void gerstner_wave(TbOceanWave wave, float time, inout float3 pos,
inout float3 tangent, inout float3 binormal) {
Expand Down
4 changes: 2 additions & 2 deletions addons/water/source/ocean.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions include/common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -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<TbCommonObjectData> object_data;
#define TB_IDX_SET(b) [[vk::binding(b, 0)]] RWBuffer<int32_t> idx_buffers[];
#define TB_POS_SET(b) [[vk::binding(b, 0)]] RWBuffer<int4> pos_buffers[];
#define TB_NORM_SET(b) [[vk::binding(b, 0)]] RWBuffer<float4> norm_buffers[];
#define TB_TAN_SET(b) [[vk::binding(b, 0)]] RWBuffer<float4> tan_buffers[];
#define TB_UV0_SET(b) [[vk::binding(b, 0)]] RWBuffer<int2> uv0_buffers[];
#define TB_JOINT_SET(b) [[vk::binding(b, 0)]] RWBuffer<int4> joint_buffers[];
#define TB_WEIGHT_SET(b) [[vk::binding(b, 0)]] RWBuffer<int4> weight_buffers[];
[[vk::binding(0, b)]] StructuredBuffer<TbCommonObjectData> object_data;
#define TB_IDX_SET(b) [[vk::binding(0, b)]] RWBuffer<int32_t> idx_buffers[];
#define TB_POS_SET(b) [[vk::binding(0, b)]] RWBuffer<int4> pos_buffers[];
#define TB_NORM_SET(b) [[vk::binding(0, b)]] RWBuffer<float4> norm_buffers[];
#define TB_TAN_SET(b) [[vk::binding(0, b)]] RWBuffer<float4> tan_buffers[];
#define TB_UV0_SET(b) [[vk::binding(0, b)]] RWBuffer<int2> uv0_buffers[];
#define TB_JOINT_SET(b) [[vk::binding(0, b)]] RWBuffer<int4> joint_buffers[];
#define TB_WEIGHT_SET(b) [[vk::binding(0, b)]] RWBuffer<int4> weight_buffers[];

// Common input layout info and permutation settings
#define TB_INPUT_PERM_NONE 0x00000000
Expand Down
24 changes: 12 additions & 12 deletions include/gltf.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -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<TbGLTFMaterialData> gltf_data[];
[[vk::binding(0, b)]] SamplerState material_sampler; \
[[vk::binding(1, b)]] SamplerComparisonState shadow_sampler; \
[[vk::binding(2, b)]] StructuredBuffer<TbGLTFMaterialData> gltf_data[];

#define GLTF_DRAW_SET(b) \
[[vk::binding(b, 0)]] StructuredBuffer<TbGLTFDrawData> draw_data;
[[vk::binding(0, b)]] StructuredBuffer<TbGLTFDrawData> draw_data;

#define GLTF_VIEW_SET(b) \
[[vk::binding(b, 0)]] ConstantBuffer<TbCommonViewData> 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<TbCommonLightData> 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<TbCommonViewData> 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<TbCommonLightData> 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) \
{ \
Expand Down
8 changes: 6 additions & 2 deletions source/gltf.slang
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions source/tb_mesh_rnd_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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){
Expand Down
24 changes: 6 additions & 18 deletions source/tb_view_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 39363d2

Please sign in to comment.