diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2020-09-20 21:13:47 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2020-09-20 21:13:47 +0000 |
| commit | 58c21c74df84655423e2ae8ebd14357e7af81bea (patch) | |
| tree | 144eed23f6feff70f8147698d3984357b87580bf /Graphics/GraphicsEngineVulkan | |
| parent | Updated submodules and release history (diff) | |
| download | DiligentCore-58c21c74df84655423e2ae8ebd14357e7af81bea.tar.gz DiligentCore-58c21c74df84655423e2ae8ebd14357e7af81bea.zip | |
Added support for shader macros when compiling GLSL_VERBATIM with glslang
Diffstat (limited to 'Graphics/GraphicsEngineVulkan')
| -rw-r--r-- | Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp index 855bdb0a..65505ad3 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp @@ -34,6 +34,7 @@ #include "DataBlobImpl.hpp" #include "GLSLUtils.hpp" #include "DXCompiler.hpp" +#include "ShaderToolsCommon.hpp" #if !DILIGENT_NO_GLSLANG # include "GLSLangUtils.hpp" @@ -44,27 +45,27 @@ namespace Diligent ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters, RenderDeviceVkImpl* pRenderDeviceVk, - const ShaderCreateInfo& CreationAttribs) : + const ShaderCreateInfo& ShaderCI) : // clang-format off TShaderBase { pRefCounters, pRenderDeviceVk, - CreationAttribs.Desc + ShaderCI.Desc } // clang-format on { - if (CreationAttribs.Source != nullptr || CreationAttribs.FilePath != nullptr) + if (ShaderCI.Source != nullptr || ShaderCI.FilePath != nullptr) { - DEV_CHECK_ERR(CreationAttribs.ByteCode == nullptr, "'ByteCode' must be null when shader is created from source code or a file"); - DEV_CHECK_ERR(CreationAttribs.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from source code or a file"); + DEV_CHECK_ERR(ShaderCI.ByteCode == nullptr, "'ByteCode' must be null when shader is created from source code or a file"); + DEV_CHECK_ERR(ShaderCI.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from source code or a file"); static constexpr char VulkanDefine[] = "#ifndef VULKAN\n" "# define VULKAN 1\n" "#endif\n"; - auto ShaderCompiler = CreationAttribs.ShaderCompiler; + auto ShaderCompiler = ShaderCI.ShaderCompiler; if (ShaderCompiler == SHADER_COMPILER_DXC) { auto* pDXComiler = pRenderDeviceVk->GetDxCompiler(); @@ -81,7 +82,7 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters, { auto* pDXComiler = pRenderDeviceVk->GetDxCompiler(); VERIFY_EXPR(pDXComiler != nullptr && pDXComiler->IsLoaded()); - pDXComiler->Compile(CreationAttribs, ShaderVersion{}, VulkanDefine, nullptr, &m_SPIRV, CreationAttribs.ppCompilerOutput); + pDXComiler->Compile(ShaderCI, ShaderVersion{}, VulkanDefine, nullptr, &m_SPIRV, ShaderCI.ppCompilerOutput); } break; @@ -91,20 +92,40 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters, #if DILIGENT_NO_GLSLANG LOG_ERROR_AND_THROW("Diligent engine was not linked with glslang, use DXC or precompiled SPIRV bytecode."); #else - if (CreationAttribs.SourceLanguage == SHADER_SOURCE_LANGUAGE_HLSL) + if (ShaderCI.SourceLanguage == SHADER_SOURCE_LANGUAGE_HLSL) { - m_SPIRV = GLSLangUtils::HLSLtoSPIRV(CreationAttribs, VulkanDefine, CreationAttribs.ppCompilerOutput); + m_SPIRV = GLSLangUtils::HLSLtoSPIRV(ShaderCI, VulkanDefine, ShaderCI.ppCompilerOutput); } else { - auto GLSLSource = BuildGLSLSourceString(CreationAttribs, pRenderDeviceVk->GetDeviceCaps(), - TargetGLSLCompiler::glslang, - VulkanDefine); - - m_SPIRV = GLSLangUtils::GLSLtoSPIRV(m_Desc.ShaderType, GLSLSource.c_str(), - static_cast<int>(GLSLSource.length()), - CreationAttribs.pShaderSourceStreamFactory, - CreationAttribs.ppCompilerOutput); + std::string GLSLSourceString; + RefCntAutoPtr<IDataBlob> pSourceFileData; + + const char* ShaderSource = nullptr; + size_t SourceLength = 0; + const ShaderMacro* Macros = nullptr; + if (ShaderCI.SourceLanguage == SHADER_SOURCE_LANGUAGE_GLSL_VERBATIM) + { + // Read the source file directly and use it as is + ShaderSource = ReadShaderSourceFile(ShaderCI.Source, ShaderCI.pShaderSourceStreamFactory, ShaderCI.FilePath, pSourceFileData, SourceLength); + + // Add user macros. + // BuildGLSLSourceString adds the macros to the source string, so we don't need to do this for SHADER_SOURCE_LANGUAGE_GLSL + Macros = ShaderCI.Macros; + } + else + { + // Build the full source code string that will contain GLSL version declaration, + // platform definitions, user-provided shader macros, etc. + GLSLSourceString = BuildGLSLSourceString(ShaderCI, pRenderDeviceVk->GetDeviceCaps(), TargetGLSLCompiler::glslang, VulkanDefine); + ShaderSource = GLSLSourceString.c_str(); + SourceLength = GLSLSourceString.length(); + } + + m_SPIRV = GLSLangUtils::GLSLtoSPIRV(m_Desc.ShaderType, ShaderSource, + static_cast<int>(SourceLength), Macros, + ShaderCI.pShaderSourceStreamFactory, + ShaderCI.ppCompilerOutput); } #endif break; @@ -116,15 +137,15 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters, if (m_SPIRV.empty()) { - LOG_ERROR_AND_THROW("Failed to compile shader '", CreationAttribs.Desc.Name, '\''); + LOG_ERROR_AND_THROW("Failed to compile shader '", ShaderCI.Desc.Name, '\''); } } - else if (CreationAttribs.ByteCode != nullptr) + else if (ShaderCI.ByteCode != nullptr) { - DEV_CHECK_ERR(CreationAttribs.ByteCodeSize != 0, "ByteCodeSize must not be 0"); - DEV_CHECK_ERR(CreationAttribs.ByteCodeSize % 4 == 0, "Byte code size (", CreationAttribs.ByteCodeSize, ") is not multiple of 4"); - m_SPIRV.resize(CreationAttribs.ByteCodeSize / 4); - memcpy(m_SPIRV.data(), CreationAttribs.ByteCode, CreationAttribs.ByteCodeSize); + DEV_CHECK_ERR(ShaderCI.ByteCodeSize != 0, "ByteCodeSize must not be 0"); + DEV_CHECK_ERR(ShaderCI.ByteCodeSize % 4 == 0, "Byte code size (", ShaderCI.ByteCodeSize, ") is not multiple of 4"); + m_SPIRV.resize(ShaderCI.ByteCodeSize / 4); + memcpy(m_SPIRV.data(), ShaderCI.ByteCode, ShaderCI.ByteCodeSize); } else { @@ -144,7 +165,7 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters, pRenderDeviceVk, m_SPIRV, m_Desc, - CreationAttribs.UseCombinedTextureSamplers ? CreationAttribs.CombinedSamplerSuffix : nullptr, + ShaderCI.UseCombinedTextureSamplers ? ShaderCI.CombinedSamplerSuffix : nullptr, LoadShaderInputs, m_EntryPoint // }; |
