From 3918efb4a851bb5ce6929e3499855650e9219dbd Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Wed, 21 Sep 2022 19:26:16 +0800 Subject: [PATCH] supported SPIRV-Cross of VulkanSDK --- CMakeLists.txt | 36 ++++++---- DirStackFileIncluder.h | 149 +++++++++++++++++++++++++++++++++++++++++ FindVulkan.cmake | 87 ++++++++++++++++++++++++ glsl2spv.cpp | 2 +- 4 files changed, 260 insertions(+), 14 deletions(-) create mode 100644 DirStackFileIncluder.h create mode 100644 FindVulkan.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 188fb1e..f629ffb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,23 +3,33 @@ SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE) project(GLSLCompiler) -if(MSVC) - SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd") - SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") - - SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT") - SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") - add_definitions(-D_CRT_SECURE_NO_WARNINGS) -endif() - include("system_bit.cmake") include("output_path.cmake") -add_subdirectory(SPIRV-Cross) -add_subdirectory(SPIRV-Cross/external/glslang) -add_subdirectory(SPIRV-Cross/external/spirv-tools) +option(USE_PRIVATE_SPIRV_CROSS "Use private SPIRV-Cross" OFF) -include_directories(SPIRV-Cross/external) +IF(USE_PRIVATE_SPIRV_CROSS) + + include("FindVulkan.cmake") + include_directories(${Vulkan_INCLUDE_DIRS}) + link_directories(${Vulkan_LIBRARIES_DIR}) + +ELSE() + if(MSVC) + SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd") + SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") + + SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT") + SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") + add_definitions(-D_CRT_SECURE_NO_WARNINGS) + endif() + + add_subdirectory(SPIRV-Cross) + add_subdirectory(SPIRV-Cross/external/glslang) + add_subdirectory(SPIRV-Cross/external/spirv-tools) + + include_directories(SPIRV-Cross/external) +ENDIF() set(VULKAN_SPIRV_LIBS GenericCodeGen glslang diff --git a/DirStackFileIncluder.h b/DirStackFileIncluder.h new file mode 100644 index 0000000..08528c9 --- /dev/null +++ b/DirStackFileIncluder.h @@ -0,0 +1,149 @@ +// +// Copyright (C) 2002-2005 3Dlabs Inc. Ltd. +// Copyright (C) 2017 Google, Inc. +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// +// Neither the name of 3Dlabs Inc. Ltd. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// + +#pragma once + +#include +#include +#include +#include +#include + +#include + +// Default include class for normal include convention of search backward +// through the stack of active include paths (for nested includes). +// Can be overridden to customize. +class DirStackFileIncluder : public glslang::TShader::Includer { +public: + DirStackFileIncluder() : externalLocalDirectoryCount(0) { } + + virtual IncludeResult* includeLocal(const char* headerName, + const char* includerName, + size_t inclusionDepth) override + { + return readLocalPath(headerName, includerName, (int)inclusionDepth); + } + + virtual IncludeResult* includeSystem(const char* headerName, + const char* /*includerName*/, + size_t /*inclusionDepth*/) override + { + return readSystemPath(headerName); + } + + // Externally set directories. E.g., from a command-line -I. + // - Most-recently pushed are checked first. + // - All these are checked after the parse-time stack of local directories + // is checked. + // - This only applies to the "local" form of #include. + // - Makes its own copy of the path. + virtual void pushExternalLocalDirectory(const std::string& dir) + { + directoryStack.push_back(dir); + externalLocalDirectoryCount = (int)directoryStack.size(); + } + + virtual void releaseInclude(IncludeResult* result) override + { + if (result != nullptr) { + delete [] static_cast(result->userData); + delete result; + } + } + + virtual std::set getIncludedFiles() + { + return includedFiles; + } + + virtual ~DirStackFileIncluder() override { } + +protected: + typedef char tUserDataElement; + std::vector directoryStack; + int externalLocalDirectoryCount; + std::set includedFiles; + + // Search for a valid "local" path based on combining the stack of include + // directories and the nominal name of the header. + virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth) + { + // Discard popped include directories, and + // initialize when at parse-time first level. + directoryStack.resize(depth + externalLocalDirectoryCount); + if (depth == 1) + directoryStack.back() = getDirectory(includerName); + + // Find a directory that works, using a reverse search of the include stack. + for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) { + std::string path = *it + '/' + headerName; + std::replace(path.begin(), path.end(), '\\', '/'); + std::ifstream file(path, std::ios_base::binary | std::ios_base::ate); + if (file) { + directoryStack.push_back(getDirectory(path)); + includedFiles.insert(path); + return newIncludeResult(path, file, (int)file.tellg()); + } + } + + return nullptr; + } + + // Search for a valid path. + // Not implemented yet; returning nullptr signals failure to find. + virtual IncludeResult* readSystemPath(const char* /*headerName*/) const + { + return nullptr; + } + + // Do actual reading of the file, filling in a new include result. + virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const + { + char* content = new tUserDataElement [length]; + file.seekg(0, file.beg); + file.read(content, length); + return new IncludeResult(path, content, length, content); + } + + // If no path markers, return current working directory. + // Otherwise, strip file name and return path leading up to it. + virtual std::string getDirectory(const std::string path) const + { + size_t last = path.find_last_of("/\\"); + return last == std::string::npos ? "." : path.substr(0, last); + } +}; diff --git a/FindVulkan.cmake b/FindVulkan.cmake new file mode 100644 index 0000000..607d985 --- /dev/null +++ b/FindVulkan.cmake @@ -0,0 +1,87 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#.rst: +# FindVulkan +# ---------- +# +# Try to find Vulkan +# +# IMPORTED Targets +# ^^^^^^^^^^^^^^^^ +# +# This module defines :prop_tgt:`IMPORTED` target ``Vulkan::Vulkan``, if +# Vulkan has been found. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module defines the following variables:: +# +# Vulkan_FOUND - True if Vulkan was found +# Vulkan_INCLUDE_DIRS - include directories for Vulkan +# Vulkan_LIBRARIES - link against this library to use Vulkan +# +# The module will also define two cache variables:: +# +# Vulkan_INCLUDE_DIR - the Vulkan include directory +# Vulkan_LIBRARY - the path to the Vulkan library +# + +if(WIN32) + find_path(Vulkan_INCLUDE_DIR + NAMES vulkan/vulkan.h + PATHS + "$ENV{VULKAN_SDK}/Include" + ) + + if(CMAKE_SIZEOF_VOID_P EQUAL 8) + find_library(Vulkan_LIBRARY + NAMES vulkan-1 + PATHS + "$ENV{VULKAN_SDK}/Lib" + "$ENV{VULKAN_SDK}/Bin" + ) + + set(Vulkan_LIBRARIES_DIR $ENV{VULKAN_SDK}/Lib) + elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) + find_library(Vulkan_LIBRARY + NAMES vulkan-1 + PATHS + "$ENV{VULKAN_SDK}/Lib32" + "$ENV{VULKAN_SDK}/Bin32" + NO_SYSTEM_ENVIRONMENT_PATH + ) + + set(Vulkan_LIBRARIES_DIR $ENV{VULKAN_SDK}/Lib32) + endif() +else() + find_path(Vulkan_INCLUDE_DIR + NAMES vulkan/vulkan.h + PATHS + "$ENV{VULKAN_SDK}/include") + find_library(Vulkan_LIBRARY + NAMES vulkan + PATHS + "$ENV{VULKAN_SDK}/lib") + + set(Vulkan_LIBRARIES_DIR $ENV{VULKAN_SDK}/Lib) +endif() + +set(Vulkan_LIBRARIES ${Vulkan_LIBRARY}) +set(Vulkan_INCLUDE_DIRS ${Vulkan_INCLUDE_DIR}) + +#include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Vulkan + DEFAULT_MSG + Vulkan_LIBRARY Vulkan_INCLUDE_DIR) + +mark_as_advanced(Vulkan_INCLUDE_DIR Vulkan_LIBRARY) + +if(Vulkan_FOUND AND NOT TARGET Vulkan::Vulkan) + add_library(Vulkan::Vulkan UNKNOWN IMPORTED) + set_target_properties(Vulkan::Vulkan PROPERTIES + IMPORTED_LOCATION "${Vulkan_LIBRARIES}" + INTERFACE_INCLUDE_DIRECTORIES "${Vulkan_INCLUDE_DIRS}") +endif() diff --git a/glsl2spv.cpp b/glsl2spv.cpp index 692eea5..30af513 100644 --- a/glsl2spv.cpp +++ b/glsl2spv.cpp @@ -1,7 +1,7 @@ #include #include #include"SPIRV-Cross/spirv_common.hpp" -#include"StandAlone/DirStackFileIncluder.h" +#include"DirStackFileIncluder.h" #include"VKShaderParse.h" #include #include