Merge pull request #8266 from lioncash/shadowing

D3DCommon/Shader: Use std::optional where applicable
This commit is contained in:
Connor McLaughlin 2019-07-28 14:24:32 +10:00 committed by GitHub
commit b0113b6c64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 19 deletions

View File

@ -16,8 +16,6 @@ public:
DXShader(ShaderStage stage, BinaryData bytecode, ID3D11DeviceChild* shader); DXShader(ShaderStage stage, BinaryData bytecode, ID3D11DeviceChild* shader);
~DXShader() override; ~DXShader() override;
const BinaryData& GetByteCode() const { return m_bytecode; }
ID3D11VertexShader* GetD3DVertexShader() const; ID3D11VertexShader* GetD3DVertexShader() const;
ID3D11GeometryShader* GetD3DGeometryShader() const; ID3D11GeometryShader* GetD3DGeometryShader() const;
ID3D11PixelShader* GetD3DPixelShader() const; ID3D11PixelShader* GetD3DPixelShader() const;

View File

@ -115,11 +115,11 @@ std::unique_ptr<AbstractFramebuffer> Renderer::CreateFramebuffer(AbstractTexture
std::unique_ptr<AbstractShader> Renderer::CreateShaderFromSource(ShaderStage stage, std::unique_ptr<AbstractShader> Renderer::CreateShaderFromSource(ShaderStage stage,
std::string_view source) std::string_view source)
{ {
DXShader::BinaryData bytecode; auto bytecode = DXShader::CompileShader(D3D::feature_level, stage, source);
if (!DXShader::CompileShader(D3D::feature_level, &bytecode, stage, source)) if (!bytecode)
return nullptr; return nullptr;
return DXShader::CreateFromBytecode(stage, std::move(bytecode)); return DXShader::CreateFromBytecode(stage, std::move(*bytecode));
} }
std::unique_ptr<AbstractShader> Renderer::CreateShaderFromBinary(ShaderStage stage, std::unique_ptr<AbstractShader> Renderer::CreateShaderFromBinary(ShaderStage stage,

View File

@ -26,11 +26,11 @@ std::unique_ptr<DXShader> DXShader::CreateFromBytecode(ShaderStage stage, Binary
std::unique_ptr<DXShader> DXShader::CreateFromSource(ShaderStage stage, std::string_view source) std::unique_ptr<DXShader> DXShader::CreateFromSource(ShaderStage stage, std::string_view source)
{ {
BinaryData bytecode; auto bytecode = CompileShader(g_dx_context->GetFeatureLevel(), stage, source);
if (!CompileShader(g_dx_context->GetFeatureLevel(), &bytecode, stage, source)) if (!bytecode)
return nullptr; return nullptr;
return CreateFromBytecode(stage, std::move(bytecode)); return CreateFromBytecode(stage, std::move(*bytecode));
} }
D3D12_SHADER_BYTECODE DXShader::GetD3DByteCode() const D3D12_SHADER_BYTECODE DXShader::GetD3DByteCode() const

View File

@ -89,8 +89,8 @@ static const char* GetCompileTarget(D3D_FEATURE_LEVEL feature_level, ShaderStage
} }
} }
bool Shader::CompileShader(D3D_FEATURE_LEVEL feature_level, BinaryData* out_bytecode, std::optional<Shader::BinaryData> Shader::CompileShader(D3D_FEATURE_LEVEL feature_level,
ShaderStage stage, std::string_view source) ShaderStage stage, std::string_view source)
{ {
static constexpr D3D_SHADER_MACRO macros[] = {{"API_D3D", "1"}, {nullptr, nullptr}}; static constexpr D3D_SHADER_MACRO macros[] = {{"API_D3D", "1"}, {nullptr, nullptr}};
const UINT flags = g_ActiveConfig.bEnableValidationLayer ? const UINT flags = g_ActiveConfig.bEnableValidationLayer ?
@ -116,7 +116,7 @@ bool Shader::CompileShader(D3D_FEATURE_LEVEL feature_level, BinaryData* out_byte
PanicAlert("Failed to compile %s:\nDebug info (%s):\n%s", filename.c_str(), target, PanicAlert("Failed to compile %s:\nDebug info (%s):\n%s", filename.c_str(), target,
static_cast<const char*>(errors->GetBufferPointer())); static_cast<const char*>(errors->GetBufferPointer()));
return false; return std::nullopt;
} }
if (errors && errors->GetBufferSize() > 0) if (errors && errors->GetBufferSize() > 0)
@ -125,16 +125,15 @@ bool Shader::CompileShader(D3D_FEATURE_LEVEL feature_level, BinaryData* out_byte
static_cast<const char*>(errors->GetBufferPointer())); static_cast<const char*>(errors->GetBufferPointer()));
} }
out_bytecode->resize(code->GetBufferSize()); return CreateByteCode(code->GetBufferPointer(), code->GetBufferSize());
std::memcpy(out_bytecode->data(), code->GetBufferPointer(), code->GetBufferSize());
return true;
} }
AbstractShader::BinaryData Shader::CreateByteCode(const void* data, size_t length) AbstractShader::BinaryData Shader::CreateByteCode(const void* data, size_t length)
{ {
BinaryData bytecode(length); const auto* const begin = static_cast<const u8*>(data);
std::memcpy(bytecode.data(), data, length); const auto* const end = begin + length;
return bytecode;
return {begin, end};
} }
} // namespace D3DCommon } // namespace D3DCommon

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <optional>
#include <string_view> #include <string_view>
#include "VideoBackends/D3DCommon/Common.h" #include "VideoBackends/D3DCommon/Common.h"
#include "VideoCommon/AbstractShader.h" #include "VideoCommon/AbstractShader.h"
@ -19,8 +20,8 @@ public:
BinaryData GetBinary() const override; BinaryData GetBinary() const override;
static bool CompileShader(D3D_FEATURE_LEVEL feature_level, BinaryData* out_bytecode, static std::optional<BinaryData> CompileShader(D3D_FEATURE_LEVEL feature_level, ShaderStage stage,
ShaderStage stage, std::string_view source); std::string_view source);
static BinaryData CreateByteCode(const void* data, size_t length); static BinaryData CreateByteCode(const void* data, size_t length);