Move shaders and assets into the src build tree
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -12,7 +12,6 @@
|
||||
# Build dirs
|
||||
/build/
|
||||
/cmake-build-*/
|
||||
/src/shaders/
|
||||
|
||||
# Build helpers
|
||||
build.bat
|
||||
|
||||
@@ -38,74 +38,11 @@ add_subdirectory(src)
|
||||
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/bin/shaders")
|
||||
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/bin/assets")
|
||||
|
||||
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_UPPER_BUILD_TYPE)
|
||||
|
||||
if (CMAKE_UPPER_BUILD_TYPE STREQUAL "DEBUG" OR CMAKE_UPPER_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
|
||||
set(SHADER_DEBUG_FLAGS "-g")
|
||||
else ()
|
||||
set(SHADER_DEBUG_FLAGS "")
|
||||
endif ()
|
||||
|
||||
file(GLOB_RECURSE ASSETS ${CMAKE_CURRENT_SOURCE_DIR}/assets/*)
|
||||
|
||||
foreach (ASSET IN LISTS ASSETS)
|
||||
get_filename_component(FILENAME ${ASSET} NAME)
|
||||
get_filename_component(FILE_DIRECTORY ${ASSET} DIRECTORY)
|
||||
file(RELATIVE_PATH REL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/assets ${FILE_DIRECTORY})
|
||||
|
||||
if (NOT REL_PATH STREQUAL "")
|
||||
set(REL_PATH ${REL_PATH}/)
|
||||
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin/assets/${REL_PATH})
|
||||
endif ()
|
||||
|
||||
set(OUT_PATH ${CMAKE_BINARY_DIR}/bin/assets/${REL_PATH}${FILENAME})
|
||||
|
||||
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/bin/assets/${FILENAME}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${ASSET} ${OUT_PATH}
|
||||
COMMENT " Copying ${ASSET} to ${OUT_PATH}")
|
||||
|
||||
list(APPEND OUT_ASSETS ${CMAKE_BINARY_DIR}/bin/assets/${FILENAME})
|
||||
list(APPEND ADDITIONAL_CLEAN_FILES ${OUT_PATH})
|
||||
endforeach ()
|
||||
|
||||
file(GLOB_RECURSE SHADERS
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/**.frag"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/**.vert"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/**.tesc"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/**.tese"
|
||||
)
|
||||
|
||||
foreach (SHADER IN LISTS SHADERS)
|
||||
get_filename_component(FILENAME ${SHADER} NAME)
|
||||
get_filename_component(FILE_DIRECTORY ${SHADER} DIRECTORY)
|
||||
file(RELATIVE_PATH REL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/shaders ${FILE_DIRECTORY})
|
||||
|
||||
if (NOT REL_PATH STREQUAL "")
|
||||
set(REL_PATH ${REL_PATH}/)
|
||||
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin/shaders/${REL_PATH})
|
||||
endif ()
|
||||
|
||||
set(OUT_PATH ${CMAKE_BINARY_DIR}/bin/shaders/${REL_PATH}${FILENAME}.spv)
|
||||
|
||||
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/bin/shaders/${FILENAME}.spv
|
||||
COMMAND ${Vulkan_GLSLC_EXECUTABLE} ${SHADER_DEBUG_FLAGS} ${SHADER} -o ${OUT_PATH} DEPENDS ${SHADER}
|
||||
COMMENT " Compiling ${SHADER} ")
|
||||
|
||||
list(APPEND SPV_SHADERS ${CMAKE_BINARY_DIR}/bin/shaders/${FILENAME}.spv)
|
||||
list(APPEND ADDITIONAL_CLEAN_FILES ${OUT_PATH})
|
||||
endforeach ()
|
||||
|
||||
add_custom_target(shaders ALL DEPENDS ${SPV_SHADERS})
|
||||
add_custom_target(assets ALL DEPENDS ${OUT_ASSETS})
|
||||
|
||||
add_dependencies(TitorEditor shaders)
|
||||
add_dependencies(TitorEditor assets)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${CMAKE_SOURCE_DIR}/compile_commands.json
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/compile_commands.json ${CMAKE_SOURCE_DIR}/compile_commands.json
|
||||
COMMENT "Copying build commands to src dir")
|
||||
|
||||
|
||||
SetVersionInfo()
|
||||
CompilerPostSetup()
|
||||
|
||||
@@ -42,3 +42,5 @@ include(versioninfo)
|
||||
include(profiling)
|
||||
include(docs)
|
||||
message(DEBUG "Leaving ${CMAKE_CURRENT_LIST_FILE}")
|
||||
|
||||
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_UPPER_BUILD_TYPE)
|
||||
|
||||
@@ -3,4 +3,9 @@ add_subdirectory(vma)
|
||||
add_subdirectory(engine)
|
||||
add_subdirectory(objectloaders)
|
||||
add_subdirectory(editor)
|
||||
add_subdirectory(tests)
|
||||
add_subdirectory(tests)
|
||||
add_subdirectory(shaders)
|
||||
add_subdirectory(assets)
|
||||
|
||||
add_dependencies(TitorEditor shaders)
|
||||
add_dependencies(TitorEditor assets)
|
||||
|
||||
39
src/assets/CMakeLists.txt
Normal file
39
src/assets/CMakeLists.txt
Normal file
@@ -0,0 +1,39 @@
|
||||
file(GLOB_RECURSE ASSETS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/**.obj
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/**.png
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/**.jpg
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/**.glb
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/**.bin
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/**.gltf
|
||||
)
|
||||
|
||||
foreach (ASSET IN LISTS ASSETS)
|
||||
get_filename_component(FILENAME ${ASSET} NAME)
|
||||
get_filename_component(FILE_DIRECTORY ${ASSET} DIRECTORY)
|
||||
file(RELATIVE_PATH REL_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${FILE_DIRECTORY})
|
||||
|
||||
if (NOT REL_PATH STREQUAL "")
|
||||
set(REL_PATH ${REL_PATH}/)
|
||||
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin/assets/${REL_PATH})
|
||||
endif ()
|
||||
|
||||
set(OUT_DIR ${CMAKE_BINARY_DIR}/bin/assets/${REL_PATH})
|
||||
set(OUT_PATH ${OUT_DIR}${FILENAME})
|
||||
|
||||
#add_custom_command(OUTPUT ${OUT_DIR}
|
||||
# COMMAND ${CMAKE_COMMAND} -E make_directory ${OUT_DIR} COMMENT "Creating directory ${OUT_DIR}")
|
||||
|
||||
if (EXISTS ${OUT_PATH})
|
||||
file(REMOVE ${OUT_PATH})
|
||||
endif ()
|
||||
|
||||
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/bin/assets/${FILENAME}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${ASSET} ${OUT_PATH} DEPENDS ${ASSET}
|
||||
COMMENT "Copying ${ASSET} to ${OUT_PATH}")
|
||||
|
||||
list(APPEND OUT_ASSETS ${CMAKE_BINARY_DIR}/bin/assets/${FILENAME})
|
||||
list(APPEND ADDITIONAL_CLEAN_FILES ${OUT_PATH})
|
||||
endforeach ()
|
||||
|
||||
add_custom_target(assets ALL DEPENDS ${OUT_ASSETS})
|
||||
|
||||
94
src/engine/shaders/Compiler.cpp
Normal file
94
src/engine/shaders/Compiler.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
//
|
||||
// Created by kj16609 on 8/5/24.
|
||||
//
|
||||
|
||||
#include "Compiler.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "engine/FGL_DEFINES.hpp"
|
||||
#include "engine/logging/logging.hpp"
|
||||
|
||||
namespace fgl::engine
|
||||
{
|
||||
|
||||
inline static std::unique_ptr< shaderc::Compiler > compiler;
|
||||
|
||||
shaderc::Compiler& getInstance()
|
||||
{
|
||||
if ( !compiler )
|
||||
{
|
||||
compiler = std::make_unique< shaderc::Compiler >();
|
||||
}
|
||||
|
||||
FGL_ASSERT( compiler );
|
||||
|
||||
return *compiler;
|
||||
}
|
||||
|
||||
shaderc_shader_kind getShaderKindFromName( const std::string_view str )
|
||||
{
|
||||
if ( str.ends_with( ".frag" ) ) return shaderc_shader_kind::shaderc_fragment_shader;
|
||||
if ( str.ends_with( ".vert" ) ) return shaderc_shader_kind::shaderc_vertex_shader;
|
||||
|
||||
throw std::runtime_error( "Unknown shader type!" );
|
||||
}
|
||||
|
||||
std::vector< std::byte > compilerShader( const std::string_view input_name, std::vector< std::byte >& input )
|
||||
{
|
||||
shaderc::CompileOptions options;
|
||||
#ifndef NDEBUG
|
||||
options.SetOptimizationLevel( shaderc_optimization_level_zero );
|
||||
#else
|
||||
options.SetOptimizationLevel( shaderc_optimization_level_performance );
|
||||
#endif
|
||||
|
||||
const shaderc_shader_kind kind { getShaderKindFromName( input_name ) };
|
||||
|
||||
const auto result { compiler->CompileGlslToSpvAssembly(
|
||||
reinterpret_cast< const char* >( input.data() ), input.size(), kind, input_name.data(), "main", options ) };
|
||||
|
||||
switch ( result.GetCompilationStatus() )
|
||||
{
|
||||
case shaderc_compilation_status_success:
|
||||
break;
|
||||
default:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_invalid_stage:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_compilation_error:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_internal_error:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_null_result_object:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_invalid_assembly:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_validation_error:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_transformation_error:
|
||||
[[fallthrough]];
|
||||
case shaderc_compilation_status_configuration_error:
|
||||
log::critical( "Failed to compile shader {} with error: {}", result.GetErrorMessage() );
|
||||
throw std::runtime_error( "Failed to compile shader" );
|
||||
break;
|
||||
}
|
||||
|
||||
std::vector< std::byte > output {};
|
||||
output.reserve( result.cend() - result.cbegin() );
|
||||
|
||||
for ( const auto& word : result )
|
||||
{
|
||||
output.insert(
|
||||
output.end(),
|
||||
reinterpret_cast< const std::byte* >( &word ),
|
||||
reinterpret_cast< const std::byte* >( &word ) + sizeof( std::remove_reference_t< decltype( word ) > ) );
|
||||
}
|
||||
|
||||
// Should be a multiple of 4
|
||||
assert( output.size() % 4 == 0 );
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace fgl::engine
|
||||
16
src/engine/shaders/Compiler.hpp
Normal file
16
src/engine/shaders/Compiler.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by kj16609 on 8/5/24.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <shaderc/shaderc.hpp>
|
||||
|
||||
namespace fgl::engine
|
||||
{
|
||||
|
||||
shaderc::Compiler& getInstance();
|
||||
|
||||
std::vector< std::byte > compileShader( const std::string_view str, std::vector< std::byte >& input );
|
||||
|
||||
} // namespace fgl::engine
|
||||
40
src/shaders/CMakeLists.txt
Normal file
40
src/shaders/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
|
||||
|
||||
file(GLOB_RECURSE SHADERS
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/**.frag"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/**.vert"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/**.tesc"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/**.tese"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/**.glsl"
|
||||
)
|
||||
|
||||
foreach (SHADER IN LISTS SHADERS)
|
||||
get_filename_component(FILENAME ${SHADER} NAME)
|
||||
get_filename_component(FILE_DIRECTORY ${SHADER} DIRECTORY)
|
||||
file(RELATIVE_PATH REL_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${FILE_DIRECTORY})
|
||||
|
||||
if (NOT REL_PATH STREQUAL "")
|
||||
set(REL_PATH ${REL_PATH}/)
|
||||
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin/shaders/${REL_PATH})
|
||||
endif ()
|
||||
|
||||
set(OUT_DIR ${CMAKE_BINARY_DIR}/bin/shaders/${REL_PATH})
|
||||
set(OUT_PATH ${OUT_DIR}${FILENAME})
|
||||
|
||||
#add_custom_command(OUTPUT ${OUT_DIR}
|
||||
# COMMAND ${CMAKE_COMMAND} -E make_directory ${OUT_DIR} COMMENT "Creating directory ${OUT_DIR}")
|
||||
|
||||
#add_custom_command(OUTPUT ${OUT_PATH}
|
||||
# COMMAND ${Vulkan_GLSLC_EXECUTABLE} ${SHADER_DEBUG_FLAGS} ${SHADER} -o ${OUT_PATH} DEPENDS ${SHADER} ${OUT_DIR}
|
||||
# COMMENT "Compiling ${SHADER} ")
|
||||
|
||||
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/bin/shaders/${FILENAME}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${SHADER} ${OUT_PATH} DEPENDS ${SHADER}
|
||||
COMMENT "Copying ${ASSET} to ${OUT_PATH}")
|
||||
|
||||
list(APPEND SPV_SHADERS ${CMAKE_BINARY_DIR}/bin/shaders/${FILENAME})
|
||||
list(APPEND ADDITIONAL_CLEAN_FILES ${OUT_PATH})
|
||||
endforeach ()
|
||||
|
||||
add_custom_target(shaders ALL DEPENDS ${SPV_SHADERS})
|
||||
@@ -5,7 +5,9 @@
|
||||
layout (location = 0) in vec3 in_normal;
|
||||
layout (location = 1) in vec2 in_tex_coord;
|
||||
layout (location = 2) in vec3 in_world_pos;
|
||||
layout (location = 3) in flat uint in_tex_idx;
|
||||
layout (location = 3) in flat uint in_albedo_idx;
|
||||
layout (location = 4) in flat uint in_normal_idx;
|
||||
layout (location = 5) in flat uint in_metallic_roughness_idx;
|
||||
|
||||
layout (location = 0) out vec4 out_position;
|
||||
layout (location = 1) out vec4 out_normal;
|
||||
@@ -28,15 +30,27 @@ float linearDepth(float depth)
|
||||
return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));
|
||||
}
|
||||
|
||||
uint INVALID_TEX_ID = 0;
|
||||
|
||||
void main()
|
||||
{
|
||||
out_position = vec4(in_world_pos, 1.0f);
|
||||
|
||||
vec3 N = normalize(in_normal);
|
||||
|
||||
vec3 N = vec3(0.0f);
|
||||
|
||||
if (in_normal_idx == INVALID_TEX_ID)
|
||||
{
|
||||
N = normalize(in_normal);
|
||||
}
|
||||
else
|
||||
{
|
||||
N = texture(tex[in_normal_idx], in_tex_coord).xyz;
|
||||
}
|
||||
|
||||
out_normal = vec4(N, 1.0);
|
||||
|
||||
vec4 tex_value = texture(tex[in_tex_idx], in_tex_coord);
|
||||
vec4 tex_value = texture(tex[in_albedo_idx], in_tex_coord);
|
||||
|
||||
if (tex_value.a < 1.0)
|
||||
{
|
||||
@@ -7,12 +7,16 @@ layout (location = 2) in vec3 normal;
|
||||
layout (location = 3) in vec2 uv;
|
||||
|
||||
layout (location = 4) in mat4 instance_model_matrix;// 4, 5, 6, 7
|
||||
layout (location = 8) in uint in_texture_id;
|
||||
layout (location = 8) in uint in_albedo_id;
|
||||
layout (location = 9) in uint in_normal_id;
|
||||
layout (location = 10) in uint in_metallic_roughness_id;
|
||||
|
||||
layout (location = 0) out vec3 out_normal;
|
||||
layout (location = 1) out vec2 out_tex_coord;
|
||||
layout (location = 2) out vec3 out_world_pos;
|
||||
layout (location = 3) out flat uint out_texture_idx;
|
||||
layout (location = 3) out flat uint out_albedo_id;
|
||||
layout (location = 4) out flat uint out_normal_id;
|
||||
layout (location = 5) out flat uint out_metallic_roughness_id;
|
||||
|
||||
layout (set = 1, binding = 0) uniform CameraInfo {
|
||||
mat4 projection;
|
||||
@@ -33,5 +37,6 @@ void main() {
|
||||
|
||||
out_tex_coord = uv;
|
||||
|
||||
out_texture_idx = in_texture_id;
|
||||
out_albedo_id = in_albedo_id;
|
||||
out_metallic_roughness_id = in_metallic_roughness_id;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user