Skip to content

Commit

Permalink
Slang proof of concept (#4)
Browse files Browse the repository at this point in the history
* Slang proof of concept

* Testing Custom slang port work

* Trying to fix slang port again

* Fixing path to shaderc

* Updating shader-slang port again

* Updating shader slang port to fix macos

* Making sure more slangc deps are copied properly

* Another slang update and an attempt to fix

* More attempted fixes

* Updating port for macos again

* Another macos attempt

* Another port patch for slang

* Might work after slangc gets patched

* Updating slang port

* Making progress on getting a working gltf shader from slang

* Trying newer version of slang

* Slang actually works for gltf now

* Moving sky shader to slang

* Bistro works

* Ocean works though something causes the ocean demo to not rendering the sky. Unsure why

* Making sure resources are packed properly

* Pedantic Warning fix

* Trying to fix up linux workflow

* Trying to fix linux workflow and cmake preset env var nonsense

* Trying to fix cmake presets

* Trying to fix cmake presets again this time by making sure to expliticly use penv rather than env

* Making sure to penv android ndk

* Another different way of trying to specify VCPKG_ROOT for linux

* Bumping vcpkg versions

* Trying to fix ktx with a custom port
  • Loading branch information
Honeybunch authored Sep 5, 2024
1 parent 4a8caf0 commit f97b72f
Show file tree
Hide file tree
Showing 96 changed files with 1,000 additions and 789 deletions.
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

0 comments on commit f97b72f

Please sign in to comment.