example: Add config file to locate shaders

The example triangle now will have a absolute filepath to the shaders.
This allows running the exe from directories other than the location of the shaders in the filesystem.
Alternative solutions would be to copy/paste the shader binary into the source code.
This commit is contained in:
Charles Giessen 2021-04-16 10:30:04 -06:00
parent 82459b10c0
commit 23dcdd59bf
3 changed files with 15 additions and 3 deletions

View File

@ -5,6 +5,7 @@ target_link_libraries(vk-bootstrap-triangle
vk-bootstrap
vk-bootstrap-compiler-warnings
vk-boostrap-vulkan-headers)
target_include_directories(vk-bootstrap-triangle PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) #path to build directory for shaders
add_custom_command(
TARGET vk-bootstrap-triangle
@ -12,4 +13,9 @@ add_custom_command(
COMMAND
${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/example/shaders ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR})
DEPENDS ${CMAKE_CURRENT_BINARY_DIR})
configure_file (
"${PROJECT_SOURCE_DIR}/example/example_config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/example_config.h"
)

View File

@ -0,0 +1,4 @@
#pragma once
#define EXAMPLE_SOURCE_DIRECTORY "${CMAKE_SOURCE_DIR}"
#define EXAMPLE_BUILD_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"

View File

@ -7,6 +7,8 @@
#include "../tests/common.h"
#include "example_config.h"
const int MAX_FRAMES_IN_FLIGHT = 2;
struct Init {
@ -184,8 +186,8 @@ VkShaderModule createShaderModule (Init& init, const std::vector<char>& code) {
}
int create_graphics_pipeline (Init& init, RenderData& data) {
auto vert_code = readFile ("vert.spv");
auto frag_code = readFile ("frag.spv");
auto vert_code = readFile(std::string(EXAMPLE_BUILD_DIRECTORY) + "/vert.spv");
auto frag_code = readFile(std::string(EXAMPLE_BUILD_DIRECTORY) + "/frag.spv");
VkShaderModule vert_module = createShaderModule (init, vert_code);
VkShaderModule frag_module = createShaderModule (init, frag_code);