diff --git a/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp b/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp index 7214e208b068ba7b9002363e594f76d66123bffd..9ba1595b22cda7ea848a834067ee66b770391f9a 100644 --- a/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp +++ b/modules/shader_compiler/src/vkcv/shader/GLSLCompiler.cpp @@ -3,6 +3,7 @@ #include <fstream> #include <glslang/SPIRV/GlslangToSpv.h> +#include <glslang/StandAlone/DirStackFileIncluder.h> #include <vkcv/Logger.hpp> @@ -216,12 +217,26 @@ namespace vkcv::shader { TBuiltInResource resources = {}; initResources(resources); + + const auto messages = (EShMessages)( + EShMsgSpvRules | + EShMsgVulkanRules + ); + + std::string preprocessedGLSL; + + DirStackFileIncluder includer; + includer.pushExternalLocalDirectory(shaderPath.parent_path().string()); + + if (!shader.preprocess(&resources, 100, ENoProfile, false, false, messages, &preprocessedGLSL, includer)) { + vkcv_log(LogLevel::ERROR, "Shader parsing failed {\n%s\n%s\n} (%s)", + shader.getInfoLog(), shader.getInfoDebugLog(), shaderPath.string().c_str()); + return; + } - const auto messages = (EShMessages) ( - EShMsgSpvRules | - EShMsgVulkanRules - ); - + const char* preprocessedCString = preprocessedGLSL.c_str(); + shader.setStrings(&preprocessedCString, 1); + if (!shader.parse(&resources, 100, false, messages)) { vkcv_log(LogLevel::ERROR, "Shader parsing failed {\n%s\n%s\n} (%s)", shader.getInfoLog(), shader.getInfoDebugLog(), shaderPath.string().c_str()); diff --git a/projects/cmd_sync_test/CMakeLists.txt b/projects/cmd_sync_test/CMakeLists.txt index da1d12949d9e8c918d78ab5cb0484106fae69b6a..63d8d65d70f28529bf4ebd3b38ceafb684ce091f 100644 --- a/projects/cmd_sync_test/CMakeLists.txt +++ b/projects/cmd_sync_test/CMakeLists.txt @@ -22,7 +22,7 @@ if(MSVC) endif() # including headers of dependencies and the VkCV framework -target_include_directories(cmd_sync_test SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_asset_loader_include} ${vkcv_camera_include}) +target_include_directories(cmd_sync_test SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_asset_loader_include} ${vkcv_camera_include} ${vkcv_shader_compiler_include}) # linking with libraries from all dependencies and the VkCV framework -target_link_libraries(cmd_sync_test vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_camera) +target_link_libraries(cmd_sync_test vkcv ${vkcv_libraries} vkcv_asset_loader ${vkcv_asset_loader_libraries} vkcv_camera vkcv_shader_compiler) diff --git a/projects/cmd_sync_test/src/main.cpp b/projects/cmd_sync_test/src/main.cpp index bead135d6aeb02cbe53a2798f5aea48cb0777289..7938610768ef853e8a022fb8241519e938c7f115 100644 --- a/projects/cmd_sync_test/src/main.cpp +++ b/projects/cmd_sync_test/src/main.cpp @@ -4,6 +4,7 @@ #include <vkcv/camera/CameraManager.hpp> #include <chrono> #include <vkcv/asset/asset_loader.hpp> +#include <vkcv/shader/GLSLCompiler.hpp> int main(int argc, const char** argv) { const char* applicationName = "First Mesh"; @@ -104,12 +105,20 @@ int main(int argc, const char** argv) { return EXIT_FAILURE; } - vkcv::ShaderProgram firstMeshProgram{}; - firstMeshProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("resources/shaders/vert.spv")); - firstMeshProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("resources/shaders/frag.spv")); + vkcv::shader::GLSLCompiler compiler; + + vkcv::ShaderProgram firstMeshProgram; + compiler.compile(vkcv::ShaderStage::VERTEX, std::filesystem::path("resources/shaders/shader.vert"), + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + firstMeshProgram.addShader(shaderStage, path); + }); + compiler.compile(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("resources/shaders/shader.frag"), + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + firstMeshProgram.addShader(shaderStage, path); + }); + + const std::vector<vkcv::VertexAttachment> vertexAttachments = firstMeshProgram.getVertexAttachments(); - const std::vector<vkcv::VertexAttachment> vertexAttachments = firstMeshProgram.getVertexAttachments(); - std::vector<vkcv::VertexBinding> bindings; for (size_t i = 0; i < vertexAttachments.size(); i++) { bindings.push_back(vkcv::VertexBinding(i, { vertexAttachments[i] })); @@ -162,8 +171,14 @@ int main(int argc, const char** argv) { const vkcv::ImageHandle swapchainInput = vkcv::ImageHandle::createSwapchainImageHandle(); vkcv::ShaderProgram shadowShader; - shadowShader.addShader(vkcv::ShaderStage::VERTEX, "resources/shaders/shadow_vert.spv"); - shadowShader.addShader(vkcv::ShaderStage::FRAGMENT, "resources/shaders/shadow_frag.spv"); + compiler.compile(vkcv::ShaderStage::VERTEX, "resources/shaders/shadow.vert", + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + shadowShader.addShader(shaderStage, path); + }); + compiler.compile(vkcv::ShaderStage::FRAGMENT, "resources/shaders/shadow.frag", + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + shadowShader.addShader(shaderStage, path); + }); const vk::Format shadowMapFormat = vk::Format::eD16Unorm; const std::vector<vkcv::AttachmentDescription> shadowAttachments = { @@ -211,9 +226,18 @@ int main(int argc, const char** argv) { vkcv::Image voxelizationDummyRenderTarget = core.createImage(voxelizationDummyFormat, voxelResolution, voxelResolution, 1, false, true); vkcv::ShaderProgram voxelizationShader; - voxelizationShader.addShader(vkcv::ShaderStage::VERTEX, "resources/shaders/voxelization_vert.spv"); - voxelizationShader.addShader(vkcv::ShaderStage::GEOMETRY, "resources/shaders/voxelization_geom.spv"); - voxelizationShader.addShader(vkcv::ShaderStage::FRAGMENT, "resources/shaders/voxelization_frag.spv"); + compiler.compile(vkcv::ShaderStage::VERTEX, "resources/shaders/voxelization.vert", + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + voxelizationShader.addShader(shaderStage, path); + }); + compiler.compile(vkcv::ShaderStage::GEOMETRY, "resources/shaders/voxelization.geom", + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + voxelizationShader.addShader(shaderStage, path); + }); + compiler.compile(vkcv::ShaderStage::FRAGMENT, "resources/shaders/voxelization.frag", + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + voxelizationShader.addShader(shaderStage, path); + }); vkcv::PassConfig voxelizationPassConfig({ vkcv::AttachmentDescription(vkcv::AttachmentOperation::DONT_CARE, vkcv::AttachmentOperation::DONT_CARE, voxelizationDummyFormat)}); @@ -312,7 +336,10 @@ int main(int argc, const char** argv) { }); vkcv::ShaderProgram resetVoxelShader; - resetVoxelShader.addShader(vkcv::ShaderStage::COMPUTE, "resources/shaders/voxelReset_comp.spv"); + compiler.compile(vkcv::ShaderStage::COMPUTE, "resources/shaders/voxelReset.comp", + [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) { + resetVoxelShader.addShader(shaderStage, path); + }); vkcv::DescriptorSetHandle resetVoxelDescriptorSet = core.createDescriptorSet(resetVoxelShader.getReflectedDescriptors()[0]); vkcv::PipelineHandle resetVoxelPipeline = core.createComputePipeline(