Skip to content

Commit

Permalink
[vsg-dev#1035] glslang support
Browse files Browse the repository at this point in the history
  • Loading branch information
psi29a committed Nov 28, 2023
1 parent 8d8acf9 commit 97ed759
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 48 deletions.
50 changes: 28 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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." )
Expand Down
1 change: 1 addition & 0 deletions src/vsg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ endif()
set(LIBRARIES PUBLIC
Vulkan::Vulkan
Threads::Threads
glslang
)

# Check for std::atomic
Expand Down
26 changes: 3 additions & 23 deletions src/vsg/utils/ShaderCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/state/GraphicsPipeline.h>
#include <vsg/utils/ShaderCompiler.h>

#if VSG_SUPPORTS_ShaderCompiler
# include <SPIRV/GlslangToSpv.h>
# include <glslang/Public/ResourceLimits.h>
# include <glslang/Public/ShaderLang.h>
#endif
#include <SPIRV/GlslangToSpv.h>
#include <glslang/Public/ResourceLimits.h>
#include <glslang/Public/ShaderLang.h>

#include <algorithm>
#include <iomanip>
Expand All @@ -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()
Expand All @@ -54,8 +51,6 @@ static void s_finalizeProcess()
}
}

#endif

std::string debugFormatShaderSource(const std::string& source)
{
std::istringstream iss(source);
Expand All @@ -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<std::string>& defines, ref_ptr<const Options> options)
{
// need to balance the inits.
Expand Down Expand Up @@ -290,13 +277,6 @@ bool ShaderCompiler::compile(ShaderStages& shaders, const std::vector<std::strin

return true;
}
#else
bool ShaderCompiler::compile(ShaderStages&, const std::vector<std::string>&, ref_ptr<const Options> /*options*/)
{
warn("ShaderCompile::compile(..) not supported,");
return false;
}
#endif

bool ShaderCompiler::compile(ref_ptr<ShaderStage> shaderStage, const std::vector<std::string>& defines, ref_ptr<const Options> options)
{
Expand Down
3 changes: 0 additions & 3 deletions src/vsg/vk/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,13 @@ ShaderCompiler* Context::getOrCreateShaderCompiler()
{
if (shaderCompiler) return shaderCompiler;

#if VSG_SUPPORTS_ShaderCompiler
shaderCompiler = ShaderCompiler::create();

if (device && device->getInstance())
{
shaderCompiler->defaults->vulkanVersion = device->getInstance()->apiVersion;
}

#endif

return shaderCompiler;
}

Expand Down

0 comments on commit 97ed759

Please sign in to comment.