Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slang proof of concept #4

Merged
merged 32 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6a7a4f4
Slang proof of concept
Honeybunch May 5, 2024
8c7ae6a
Testing Custom slang port work
Honeybunch May 5, 2024
1943eb3
Trying to fix slang port again
Honeybunch May 5, 2024
44d6088
Fixing path to shaderc
Honeybunch May 5, 2024
c2b37e4
Updating shader-slang port again
Honeybunch May 5, 2024
91bfe2c
Updating shader slang port to fix macos
Honeybunch May 5, 2024
0dade75
Making sure more slangc deps are copied properly
Honeybunch May 5, 2024
faa7b95
Another slang update and an attempt to fix
Honeybunch May 5, 2024
10784c3
More attempted fixes
Honeybunch May 5, 2024
b9adc25
Updating port for macos again
Honeybunch May 6, 2024
8e6dec7
Another macos attempt
Honeybunch May 6, 2024
4293a91
Another port patch for slang
Honeybunch May 6, 2024
9c873be
Might work after slangc gets patched
Honeybunch May 8, 2024
f57d66c
Updating slang port
Honeybunch Jun 22, 2024
0158274
Merge remote-tracking branch 'origin' into slang
Honeybunch Aug 28, 2024
39363d2
Making progress on getting a working gltf shader from slang
Honeybunch Aug 28, 2024
0deecc0
Merge branch 'main' into slang
Honeybunch Aug 31, 2024
8ff3e3d
Trying newer version of slang
Honeybunch Aug 31, 2024
1d633d8
Slang actually works for gltf now
Honeybunch Aug 31, 2024
f0759e8
Moving sky shader to slang
Honeybunch Aug 31, 2024
920f838
Bistro works
Honeybunch Aug 31, 2024
d3f3d1f
Ocean works though something causes the ocean demo to not rendering t…
Honeybunch Aug 31, 2024
58a9061
Making sure resources are packed properly
Honeybunch Sep 1, 2024
aed57b7
Pedantic Warning fix
Honeybunch Sep 1, 2024
2ab822e
Trying to fix up linux workflow
Honeybunch Sep 3, 2024
0c44632
Trying to fix linux workflow and cmake preset env var nonsense
Honeybunch Sep 3, 2024
35bac79
Trying to fix cmake presets
Honeybunch Sep 3, 2024
7b60cad
Trying to fix cmake presets again this time by making sure to expliti…
Honeybunch Sep 3, 2024
1dd5e45
Making sure to penv android ndk
Honeybunch Sep 3, 2024
8491ee0
Another different way of trying to specify VCPKG_ROOT for linux
Honeybunch Sep 3, 2024
1dcdeaa
Bumping vcpkg versions
Honeybunch Sep 5, 2024
26a427f
Trying to fix ktx with a custom port
Honeybunch Sep 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v4

- name: Configure
run: cmake --preset ${{matrix.toolsets.preset}} -DCOOK_ASSETS=OFF -DCMAKE_C_COMPILER=clang-17 -DCMAKE_CXX_COMPILER=clang++-17
run: cmake --preset ${{matrix.toolsets.preset}} -DCOOK_ASSETS=OFF -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

- name: Build Debug
run: cmake --build --preset debug-${{matrix.toolsets.preset}}
Expand Down
104 changes: 93 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.20)
cmake_minimum_required(VERSION 3.21)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)

Expand Down Expand Up @@ -149,11 +149,14 @@ endif()
# but on linux a dxc on the default path will probably have spirv codegen
# so *shrug*
set(CMAKE_FIND_ROOT_PATH "$ENV{VULKAN_SDK}/${VULKAN_BIN_PATH};${CMAKE_FIND_ROOT_PATH}")
find_program(DXC dxc REQUIRED)
find_program(GLTFPACK gltfpack
PATHS ${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/${host_triple}/tools/meshoptimizer
REQUIRED)

find_program(SLANG slangc
PATHS ${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/${host_triple}/tools/shader-slang
REQUIRED)

# The list of all external targets to link
list(APPEND library_list "$<IF:$<TARGET_EXISTS:flecs::flecs>,flecs::flecs,flecs::flecs_static>")
list(APPEND library_list "volk::volk")
Expand All @@ -179,6 +182,80 @@ function(cook_shaders out_shader_sources out_shader_headers)
set(shader_include_dir "${CMAKE_CURRENT_LIST_DIR}/include")
file(GLOB shader_includes CONFIGURE_DEPENDS "${shader_include_dir}/*.hlsli" "${shader_include_dir}/*.h")

file(GLOB slang_shaders CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/source/*.slang")
foreach(shader ${slang_shaders})
get_filename_component(filename ${shader} NAME_WLE)
set(shader_out_path ${CMAKE_CFG_INTDIR}/shaders)

# Outputting short names for shader spv modules because due to xxd nonsense
# the name of the variable in the C header will be based on this file name
set(vert_out_path "${filename}_vert")
set(frag_out_path "${filename}_frag")
set(out_paths "${vert_out_path};${frag_out_path}")

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> $<$<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}
)

set(vert_header "${shader_out_path}/${filename}_vert.h")
set(frag_header "${shader_out_path}/${filename}_frag.h")

# Use xxd to convert spv binary files to C headers that can be included
# Renaming shenanigans to work around old xxd versions not supporting the
# '-n' flag to rename the C variable
add_custom_command(
OUTPUT ${vert_header}
COMMAND xxd -i ${vert_out_path} ${vert_header}
MAIN_DEPENDENCY ${vert_out_path}
)
add_custom_command(
OUTPUT ${frag_header}
COMMAND xxd -i ${frag_out_path} ${frag_header}
MAIN_DEPENDENCY ${frag_out_path}
)

list(APPEND shader_headers ${vert_header} ${vert_out_path})
list(APPEND shader_headers ${frag_header} ${frag_out_path})
endforeach()

file(GLOB slang_compute_shaders CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/source/*.slangc")
foreach(shader ${slang_compute_shaders})
get_filename_component(filename ${shader} NAME_WLE)
set(shader_out_path ${CMAKE_CFG_INTDIR}/shaders)

# Outputting short names for shader spv modules because due to xxd nonsense
# the name of the variable in the C header will be based on this file name
set(comp_out_path "${filename}_comp")
set(out_paths "${comp_out_path};")

add_custom_command(
OUTPUT ${out_paths}
COMMAND ${CMAKE_COMMAND} -E make_directory ${shader_out_path}
COMMAND ${SLANG} -DTB_SHADER=1 -lang slang -profile sm_6_5 -stage compute -entry comp -target spirv $<$<CONFIG:Debug>:-O0> $<$<NOT:$<CONFIG:Release>>:-g> -I ${shader_include_dir} -I ${engine_shader_include_dir} -o ${comp_out_path} ${shader}
MAIN_DEPENDENCY ${shader}
DEPENDS ${shader_includes}
)

set(comp_header "${shader_out_path}/${filename}_comp.h")

# Use xxd to convert spv binary files to C headers that can be included
# Renaming shenanigans to work around old xxd versions not supporting the
# '-n' flag to rename the C variable
add_custom_command(
OUTPUT ${comp_header}
COMMAND xxd -i ${comp_out_path} ${comp_header}
MAIN_DEPENDENCY ${comp_out_path}
)

list(APPEND shader_headers ${comp_header} ${comp_out_path})
endforeach()

#[[
file(GLOB shader_files CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/source/*.hlsl")
foreach(shader ${shader_files})
get_filename_component(filename ${shader} NAME_WLE)
Expand All @@ -191,15 +268,17 @@ 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 ${DXC} -T vs_6_5 -E vert -Vn ${filename}_vert $<$<CONFIG:Debug>:-O0> $<$<NOT:$<CONFIG:Release>>:-Zi> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -enable-16bit-types -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${vert_out_path}
COMMAND ${DXC} -T ps_6_5 -E frag -Vn ${filename}_frag $<$<CONFIG:Debug>:-O0> $<$<NOT:$<CONFIG:Release>>:-Zi> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -enable-16bit-types -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${frag_out_path}
COMMAND ${DXC} -DTB_SHADER=1 -T vs_6_5 -E vert -Vn ${filename}_vert $<$<CONFIG:Debug>:-O0> $<$<NOT:$<CONFIG:Release>>:-Zi> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -enable-16bit-types -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${vert_out_path}
COMMAND ${DXC} -DTB_SHADER=1 -T ps_6_5 -E frag -Vn ${filename}_frag $<$<CONFIG:Debug>:-O0> $<$<NOT:$<CONFIG:Release>>:-Zi> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -enable-16bit-types -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${frag_out_path}
MAIN_DEPENDENCY ${shader}
DEPENDS ${shader_includes}
)
list(APPEND shader_headers ${out_paths})
endforeach()
list(APPEND shader_sources ${shader_files})
]]#

#[[
file(GLOB mesh_shader_files CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/source/*.hlsl.m")
foreach(shader ${mesh_shader_files})
get_filename_component(filename ${shader} NAME_WLE)
Expand All @@ -213,15 +292,17 @@ 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 ${DXC} -T ms_6_5 -E mesh -Vn ${filename}_mesh $<$<CONFIG:Debug>:-O0> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Zi> $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${mesh_out_path}
COMMAND ${DXC} -T ps_6_5 -E frag -Vn ${filename}_frag $<$<CONFIG:Debug>:-O0> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Zi> $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${frag_out_path}
COMMAND ${DXC} -DTB_SHADER=1 -T ms_6_5 -E mesh -Vn ${filename}_mesh $<$<CONFIG:Debug>:-O0> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Zi> $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${mesh_out_path}
COMMAND ${DXC} -DTB_SHADER=1 -T ps_6_5 -E frag -Vn ${filename}_frag $<$<CONFIG:Debug>:-O0> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Zi> $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${frag_out_path}
MAIN_DEPENDENCY ${shader}
DEPENDS ${shader_includes}
)
list(APPEND shader_headers ${out_paths})
endforeach()
list(APPEND shader_sources ${mesh_shader_files})
]]#

#[[
file(GLOB compute_shader_files CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/source/*.hlsl.cs")
foreach(shader ${compute_shader_files})
get_filename_component(filename ${shader} NAME_WLE)
Expand All @@ -234,13 +315,14 @@ 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 ${DXC} -T cs_6_5 -E comp -Vn ${filename}_comp $<$<CONFIG:Debug>:-O0> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Zi> $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -enable-16bit-types -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${comp_out_path}
COMMAND ${DXC} -DTB_SHADER=1 -T cs_6_5 -E comp -Vn ${filename}_comp $<$<CONFIG:Debug>:-O0> -I ${shader_include_dir} -I ${engine_shader_include_dir} $<$<NOT:$<CONFIG:Release>>:-Zi> $<$<NOT:$<CONFIG:Release>>:-Qembed_debug> -enable-16bit-types -fspv-target-env=vulkan1.3 -spirv ${shader} -Fh ${comp_out_path}
MAIN_DEPENDENCY ${shader}
DEPENDS ${shader_includes}
)
list(APPEND shader_headers ${out_paths})
endforeach()
list(APPEND shader_sources ${compute_shader_files})
]]#

set("${out_shader_sources}" "${shader_sources}" PARENT_SCOPE)
set("${out_shader_headers}" "${shader_headers}" PARENT_SCOPE)
Expand Down Expand Up @@ -354,7 +436,7 @@ function(cook_assets target_name out_assets_path)
string(APPEND SCENE_LIST "${idx},")
endforeach()

configure_file(${toybox_source_dir}/source/assetmanifest.h.in ${generated_manifest})
configure_file(${toybox_source_dir}/source/tb_assetmanifest.h.in ${generated_manifest})
add_custom_target(${target_name}_asset_manifest DEPENDS ${generated_manifest})

add_dependencies(${target_name} ${target_name}_scenes ${target_name}_textures ${target_name}_audio ${target_name}_asset_manifest)
Expand Down Expand Up @@ -422,7 +504,7 @@ execute_process(COMMAND git log -1 --format=%h

# Generate config header
set(engine_config_include_dir ${CMAKE_CURRENT_BINARY_DIR}/config)
configure_file(source/config.h.in ${engine_config_include_dir}/tb_engine_config.h @ONLY)
configure_file(source/tb_config.h.in ${engine_config_include_dir}/tb_engine_config.h @ONLY)

target_include_directories(toybox PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include")
target_include_directories(toybox PUBLIC "source/" "${CGLTF_INCLUDE_DIRS}" "${engine_config_include_dir}")
Expand Down Expand Up @@ -450,7 +532,7 @@ target_compile_options(toybox PUBLIC
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic>
)

target_compile_features(toybox PUBLIC c_std_11)
target_compile_features(toybox PUBLIC c_std_23)
target_compile_features(toybox PUBLIC cxx_std_20)

# Cook engine shaders
Expand Down Expand Up @@ -553,7 +635,7 @@ function(tb_add_app target_name source)

# Find config header
set(config_include_dir ${CMAKE_CURRENT_BINARY_DIR}/config)
configure_file(${CMAKE_CURRENT_LIST_DIR}/source/config.h.in ${config_include_dir}/config.h @ONLY)
configure_file(${CMAKE_CURRENT_LIST_DIR}/source/tb_config.h.in ${config_include_dir}/tb_config.h @ONLY)

cook_shaders(${target_name}_shader_sources ${target_name}_shader_headers)
if(out_shader_headers)
Expand Down
6 changes: 4 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
"name": "ninja",
"hidden": true,
"generator": "Ninja Multi-Config",
"toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"environment": {
"VCPKG_OVERLAY_TRIPLETS": "${sourceDir}/triplets/"
},
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$penv{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
},
{
Expand All @@ -27,7 +29,7 @@
"hidden": true,
"cacheVariables": {
"VCPKG_TARGET_TRIPLET": "arm64-android",
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "$env{ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake",
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "$penv{ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake",
"ANDROID_PLATFORM": "android-31",
"ANDROID_ABI": "arm64-v8a"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
#pragma once

#include "common.hlsli"
#include "tb_pi.h"
#include "tb_common.slangh"

#define TB_WAVE_MAX 8

// To avoid struct packing issues
typedef float4 TbOceanWave; // xy = dir, z = steep, w = wavelength

typedef struct OceanData {
float4 time_waves; // x = time, y = wave count
TB_GPU_STRUCT_DECL_NOPACK(TbOceanData, {
float4 time_waves; // x = time, y = wave count
TbOceanWave wave[TB_WAVE_MAX];
} OceanData;

typedef struct OceanPushConstants {
});
TB_GPU_STRUCT_DECL(TbOceanPushConstants, {
float4x4 m;
} OceanPushConstants;
});

// If not in a shader, make a quick static assert check
#ifndef __HLSL_VERSION
_Static_assert(sizeof(OceanPushConstants) <= PUSH_CONSTANT_BYTES,
#ifndef TB_SHADER
_Static_assert(sizeof(TbOceanPushConstants) <= TB_PUSH_CONSTANT_BYTES,
"Too Many Push Constants");
#endif

#ifdef __HLSL_VERSION
#ifdef TB_SHADER

#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(0, b)]] \
ConstantBuffer<TbOceanData> ocean_data; \
[[vk::binding(1, b)]] \
Texture2D depth_map; \
[[vk::binding(2, b)]] \
Texture2D color_map; \
[[vk::binding(3, b)]] \
SamplerState material_sampler; \
[[vk::binding(4, b)]] \
SamplerComparisonState shadow_sampler; \
[[vk::push_constant]] \
ConstantBuffer<OceanPushConstants> consts : register(b5, space);
ConstantBuffer<TbOceanPushConstants> consts

void gerstner_wave(TbOceanWave wave, float time, inout float3 pos,
inout float3 tangent, inout float3 binormal) {
Expand All @@ -53,7 +56,7 @@ void gerstner_wave(TbOceanWave wave, float time, inout float3 pos,
pos += float3(d.x * (a * cosf), a * sinf, d.y * (a * cosf));
}

float3 calc_wave_pos(float3 pos, OceanData data, inout float3 tangent,
float3 calc_wave_pos(float3 pos, TbOceanData data, inout float3 tangent,
inout float3 binormal) {
float time = data.time_waves.x;
uint count = (uint)data.time_waves.y;
Expand Down
2 changes: 1 addition & 1 deletion addons/water/include/tb_ocean_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "tb_simd.h"

#include "ocean.hlsli" // Must include tb_simd.h before shader includes
#include "tb_ocean.slangh"

#include <flecs.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// Adapted heavily from https://catlikecoding.com/unity/tutorials/flow/waves/

#include "common.hlsli"
#include "gltf.hlsli"
#include "lighting.hlsli"
#include "ocean.hlsli"
#include "pbr.hlsli"
#include "tb_gltf.slangh"
#include "tb_lighting.slangh"
#include "tb_ocean.slangh"
#include "tb_pbr.slangh"

OCEAN_SET(space0);
GLTF_VIEW_SET(space1);
OCEAN_SET(0);
GLTF_VIEW_SET(1);

struct VertexIn {
int3 local_pos : SV_POSITION;
float4 inst_offset : POSITION_0;
int3 local_pos : POSITION;
float4 inst_offset : POSITION0;
};

struct Interpolators {
Expand Down
Loading