From 97ed75969ff330304a5fd8b5d6af3f20bf8d4e9c Mon Sep 17 00:00:00 2001 From: Bret Curtis Date: Tue, 28 Nov 2023 12:40:37 +0100 Subject: [PATCH] [#1035] glslang support --- CMakeLists.txt | 50 ++++++++++++++++++-------------- src/vsg/CMakeLists.txt | 1 + src/vsg/utils/ShaderCompiler.cpp | 26 ++--------------- src/vsg/vk/Context.cpp | 3 -- 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64fe97cfc..6e91d4fe9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,29 +37,35 @@ find_package(Vulkan ${Vulkan_MIN_VERSION} REQUIRED) find_package(Threads REQUIRED) -# Enable/disable shader compilation support that pulls in glslang -set(VSG_SUPPORTS_ShaderCompiler 1 CACHE STRING "Optional shader compiler support, 0 for off, 1 for enabled." ) -if (VSG_SUPPORTS_ShaderCompiler) - if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/glslang/build_vars.cmake) - - if (Git_FOUND) - - set(glslang_URL "https://github.com/vsg-dev/glslang.git" CACHE STRING "URL of the glslang git repository") - set(glslang_branch "VSG-1.0.x" CACHE STRING "branch/tag of the glslang git repository") - - execute_process(COMMAND ${GIT_EXECUTABLE} clone --depth 1 --branch ${glslang_branch} ${glslang_URL} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src - RESULT_VARIABLE GIT_SUBMOD_RESULT) - - if(NOT GIT_SUBMOD_RESULT EQUAL "0") - message(WARNING "git clone of glslang failed. ShaderCompile support disabled.") - set(VSG_SUPPORTS_ShaderCompiler 0) +set(VSG_USE_SYSTEM_GLSLANG 1 CACHE STRING "Use system installed glslang, 0 for off, 1 for enabled." ) +set(GLSLANG_VERSION 13.1.1) +if (VSG_USE_SYSTEM_GLSLANG) + find_package(glslang REQUIRED ${GLSLANG_VERSION}) +else () + cmake_policy(SET CMP0135 NEW) # use DOWNLOAD_EXTRACT_TIMESTAMP + set(ENABLE_OPT OFF CACHE BOOL "") + include(FetchContent) + FetchContent_Declare(glslang + URL https://github.com/KhronosGroup/glslang/archive/refs/tags/${GLSLANG_VERSION}.zip + URL_HASH SHA3_256=4b01f8b92754eb6ea2aee3747fba25a9439a37d69c24058222ec920b132a172a + SOURCE_DIR fetched/glslang + ) + # Like `FetchContent_MakeAvailable` but passes EXCLUDE_FROM_ALL to `add_subdirectory`. + macro(FetchContent_MakeAvailableExcludeFromAll) + foreach(contentName IN ITEMS ${ARGV}) + string(TOLOWER ${contentName} contentNameLower) + FetchContent_GetProperties(${contentName}) + if(NOT ${contentNameLower}_POPULATED) + FetchContent_Populate(${contentName}) + if(EXISTS ${${contentNameLower}_SOURCE_DIR}/CMakeLists.txt) + add_subdirectory(${${contentNameLower}_SOURCE_DIR} + ${${contentNameLower}_BINARY_DIR} EXCLUDE_FROM_ALL) + endif() endif() - else() - message(WARNING "git clone of glslang failed. ShaderCompile support disabled.") - set(VSG_SUPPORTS_ShaderCompiler 0) - endif() - endif() + endforeach() + endmacro() + FetchContent_MakeAvailableExcludeFromAll(glslang) + endif() set(VSG_SUPPORTS_Windowing 1 CACHE STRING "Optional native windowing support providing a default implementation of vsg::Window::create(), 0 for off, 1 for enabled." ) diff --git a/src/vsg/CMakeLists.txt b/src/vsg/CMakeLists.txt index 188cfbe24..7f8814704 100644 --- a/src/vsg/CMakeLists.txt +++ b/src/vsg/CMakeLists.txt @@ -236,6 +236,7 @@ endif() set(LIBRARIES PUBLIC Vulkan::Vulkan Threads::Threads + glslang ) # Check for std::atomic diff --git a/src/vsg/utils/ShaderCompiler.cpp b/src/vsg/utils/ShaderCompiler.cpp index 71a7f09fb..30c2cd36a 100644 --- a/src/vsg/utils/ShaderCompiler.cpp +++ b/src/vsg/utils/ShaderCompiler.cpp @@ -19,11 +19,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include -#if VSG_SUPPORTS_ShaderCompiler -# include -# include -# include -#endif +#include +#include +#include #include #include @@ -35,7 +33,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI using namespace vsg; -#if VSG_SUPPORTS_ShaderCompiler static std::atomic_uint s_initialized = 0; static void s_initializeProcess() @@ -54,8 +51,6 @@ static void s_finalizeProcess() } } -#endif - std::string debugFormatShaderSource(const std::string& source) { std::istringstream iss(source); @@ -79,17 +74,9 @@ ShaderCompiler::ShaderCompiler() : ShaderCompiler::~ShaderCompiler() { -#if VSG_SUPPORTS_ShaderCompiler s_finalizeProcess(); -#endif -} - -bool ShaderCompiler::supported() const -{ - return VSG_SUPPORTS_ShaderCompiler == 1; } -#if VSG_SUPPORTS_ShaderCompiler bool ShaderCompiler::compile(ShaderStages& shaders, const std::vector& defines, ref_ptr options) { // need to balance the inits. @@ -290,13 +277,6 @@ bool ShaderCompiler::compile(ShaderStages& shaders, const std::vector&, ref_ptr /*options*/) -{ - warn("ShaderCompile::compile(..) not supported,"); - return false; -} -#endif bool ShaderCompiler::compile(ref_ptr shaderStage, const std::vector& defines, ref_ptr options) { diff --git a/src/vsg/vk/Context.cpp b/src/vsg/vk/Context.cpp index 47646470d..7c58393ad 100644 --- a/src/vsg/vk/Context.cpp +++ b/src/vsg/vk/Context.cpp @@ -143,7 +143,6 @@ ShaderCompiler* Context::getOrCreateShaderCompiler() { if (shaderCompiler) return shaderCompiler; -#if VSG_SUPPORTS_ShaderCompiler shaderCompiler = ShaderCompiler::create(); if (device && device->getInstance()) @@ -151,8 +150,6 @@ ShaderCompiler* Context::getOrCreateShaderCompiler() shaderCompiler->defaults->vulkanVersion = device->getInstance()->apiVersion; } -#endif - return shaderCompiler; }