From d6a60050ffd667ec1bd9a1759fb103ab0af9cfce Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 30 May 2019 03:52:05 -0400 Subject: [PATCH] VideoVulkan/ShaderCompiler: Use a std::optional instead of bool+out variable Now that we utilize C++17, we can simply return an optional containing the code instead of using an out variable and a boolean result, essentially combining them into one. This provides a much more straightforward interface. --- .../VideoBackends/Vulkan/ShaderCompiler.cpp | 35 ++++++++++--------- .../VideoBackends/Vulkan/ShaderCompiler.h | 9 ++--- Source/Core/VideoBackends/Vulkan/VKShader.cpp | 16 ++++----- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Source/Core/VideoBackends/Vulkan/ShaderCompiler.cpp b/Source/Core/VideoBackends/Vulkan/ShaderCompiler.cpp index e526ed2924..107c9f4962 100644 --- a/Source/Core/VideoBackends/Vulkan/ShaderCompiler.cpp +++ b/Source/Core/VideoBackends/Vulkan/ShaderCompiler.cpp @@ -108,11 +108,11 @@ static const char SUBGROUP_HELPER_HEADER[] = R"( #define SUBGROUP_MAX(value) value = subgroupMax(value) )"; -bool CompileShaderToSPV(SPIRVCodeVector* out_code, EShLanguage stage, const char* stage_filename, - std::string_view source, std::string_view header) +std::optional CompileShaderToSPV(EShLanguage stage, const char* stage_filename, + std::string_view source, std::string_view header) { if (!InitializeGlslang()) - return false; + return std::nullopt; std::unique_ptr shader = std::make_unique(stage); std::unique_ptr program; @@ -172,7 +172,7 @@ bool CompileShaderToSPV(SPIRVCodeVector* out_code, EShLanguage stage, const char includer)) { DumpBadShader("Failed to parse shader"); - return false; + return std::nullopt; } // Even though there's only a single shader, we still need to link it to generate SPV @@ -181,18 +181,19 @@ bool CompileShaderToSPV(SPIRVCodeVector* out_code, EShLanguage stage, const char if (!program->link(messages)) { DumpBadShader("Failed to link program"); - return false; + return std::nullopt; } glslang::TIntermediate* intermediate = program->getIntermediate(stage); if (!intermediate) { DumpBadShader("Failed to generate SPIR-V"); - return false; + return std::nullopt; } + SPIRVCodeVector out_code; spv::SpvBuildLogger logger; - glslang::GlslangToSpv(*intermediate, *out_code, &logger); + glslang::GlslangToSpv(*intermediate, out_code, &logger); // Write out messages // Temporary: skip if it contains "Warning, version 450 is not yet complete; most version-specific @@ -230,11 +231,11 @@ bool CompileShaderToSPV(SPIRVCodeVector* out_code, EShLanguage stage, const char stream << "SPIR-V conversion messages: " << std::endl; stream << spv_messages; stream << "SPIR-V:" << std::endl; - spv::Disassemble(stream, *out_code); + spv::Disassemble(stream, out_code); } } - return true; + return out_code; } bool InitializeGlslang() @@ -356,24 +357,24 @@ const TBuiltInResource* GetCompilerResourceLimits() return &limits; } -bool CompileVertexShader(SPIRVCodeVector* out_code, std::string_view source_code) +std::optional CompileVertexShader(std::string_view source_code) { - return CompileShaderToSPV(out_code, EShLangVertex, "vs", source_code, SHADER_HEADER); + return CompileShaderToSPV(EShLangVertex, "vs", source_code, SHADER_HEADER); } -bool CompileGeometryShader(SPIRVCodeVector* out_code, std::string_view source_code) +std::optional CompileGeometryShader(std::string_view source_code) { - return CompileShaderToSPV(out_code, EShLangGeometry, "gs", source_code, SHADER_HEADER); + return CompileShaderToSPV(EShLangGeometry, "gs", source_code, SHADER_HEADER); } -bool CompileFragmentShader(SPIRVCodeVector* out_code, std::string_view source_code) +std::optional CompileFragmentShader(std::string_view source_code) { - return CompileShaderToSPV(out_code, EShLangFragment, "ps", source_code, SHADER_HEADER); + return CompileShaderToSPV(EShLangFragment, "ps", source_code, SHADER_HEADER); } -bool CompileComputeShader(SPIRVCodeVector* out_code, std::string_view source_code) +std::optional CompileComputeShader(std::string_view source_code) { - return CompileShaderToSPV(out_code, EShLangCompute, "cs", source_code, COMPUTE_SHADER_HEADER); + return CompileShaderToSPV(EShLangCompute, "cs", source_code, COMPUTE_SHADER_HEADER); } } // namespace ShaderCompiler diff --git a/Source/Core/VideoBackends/Vulkan/ShaderCompiler.h b/Source/Core/VideoBackends/Vulkan/ShaderCompiler.h index 2c6e20c394..7f96968555 100644 --- a/Source/Core/VideoBackends/Vulkan/ShaderCompiler.h +++ b/Source/Core/VideoBackends/Vulkan/ShaderCompiler.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include @@ -19,16 +20,16 @@ using SPIRVCodeType = u32; using SPIRVCodeVector = std::vector; // Compile a vertex shader to SPIR-V. -bool CompileVertexShader(SPIRVCodeVector* out_code, std::string_view source_code); +std::optional CompileVertexShader(std::string_view source_code); // Compile a geometry shader to SPIR-V. -bool CompileGeometryShader(SPIRVCodeVector* out_code, std::string_view source_code); +std::optional CompileGeometryShader(std::string_view source_code); // Compile a fragment shader to SPIR-V. -bool CompileFragmentShader(SPIRVCodeVector* out_code, std::string_view source_code); +std::optional CompileFragmentShader(std::string_view source_code); // Compile a compute shader to SPIR-V. -bool CompileComputeShader(SPIRVCodeVector* out_code, std::string_view source_code); +std::optional CompileComputeShader(std::string_view source_code); } // namespace ShaderCompiler } // namespace Vulkan diff --git a/Source/Core/VideoBackends/Vulkan/VKShader.cpp b/Source/Core/VideoBackends/Vulkan/VKShader.cpp index 15fc90f61f..d0fdb285f6 100644 --- a/Source/Core/VideoBackends/Vulkan/VKShader.cpp +++ b/Source/Core/VideoBackends/Vulkan/VKShader.cpp @@ -88,31 +88,29 @@ static std::unique_ptr CreateShaderObject(ShaderStage stage, std::unique_ptr VKShader::CreateFromSource(ShaderStage stage, std::string_view source) { - ShaderCompiler::SPIRVCodeVector spv; - bool result; + std::optional spv; switch (stage) { case ShaderStage::Vertex: - result = ShaderCompiler::CompileVertexShader(&spv, source); + spv = ShaderCompiler::CompileVertexShader(source); break; case ShaderStage::Geometry: - result = ShaderCompiler::CompileGeometryShader(&spv, source); + spv = ShaderCompiler::CompileGeometryShader(source); break; case ShaderStage::Pixel: - result = ShaderCompiler::CompileFragmentShader(&spv, source); + spv = ShaderCompiler::CompileFragmentShader(source); break; case ShaderStage::Compute: - result = ShaderCompiler::CompileComputeShader(&spv, source); + spv = ShaderCompiler::CompileComputeShader(source); break; default: - result = false; break; } - if (!result) + if (!spv) return nullptr; - return CreateShaderObject(stage, std::move(spv)); + return CreateShaderObject(stage, std::move(*spv)); } std::unique_ptr VKShader::CreateFromBinary(ShaderStage stage, const void* data,