Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Source/Core/DolphinLib.props
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@
<ClInclude Include="VideoCommon\Present.h" />
<ClInclude Include="VideoCommon\RenderState.h" />
<ClInclude Include="VideoCommon\ShaderCache.h" />
<ClInclude Include="VideoCommon\ShaderCompileUtils.h" />
<ClInclude Include="VideoCommon\ShaderGenCommon.h" />
<ClInclude Include="VideoCommon\Spirv.h" />
<ClInclude Include="VideoCommon\Statistics.h" />
Expand Down Expand Up @@ -1395,6 +1396,7 @@
<ClCompile Include="VideoCommon\Present.cpp" />
<ClCompile Include="VideoCommon\RenderState.cpp" />
<ClCompile Include="VideoCommon\ShaderCache.cpp" />
<ClCompile Include="VideoCommon\ShaderCompileUtils.cpp" />
<ClCompile Include="VideoCommon\ShaderGenCommon.cpp" />
<ClCompile Include="VideoCommon\Spirv.cpp" />
<ClCompile Include="VideoCommon\Statistics.cpp" />
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/VideoBackends/D3D/D3DGfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ Gfx::CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth
}

std::unique_ptr<AbstractShader>
Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source, std::string_view name)
Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer, std::string_view name)
{
auto bytecode = DXShader::CompileShader(D3D::feature_level, stage, source);
auto bytecode = DXShader::CompileShader(D3D::feature_level, stage, source, shader_includer);
if (!bytecode)
return nullptr;

Expand Down
6 changes: 4 additions & 2 deletions Source/Core/VideoBackends/D3D/D3DGfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ class Gfx final : public ::AbstractGfx
std::string_view name) override;
std::unique_ptr<AbstractStagingTexture>
CreateStagingTexture(StagingTextureType type, const TextureConfig& config) override;
std::unique_ptr<AbstractShader> CreateShaderFromSource(ShaderStage stage, std::string_view source,
std::string_view name) override;
std::unique_ptr<AbstractShader>
CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name) override;
std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage, const void* data,
size_t length,
std::string_view name) override;
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/VideoBackends/D3D12/D3D12Gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ Gfx::CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth
}

std::unique_ptr<AbstractShader>
Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source, std::string_view name)
Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer, std::string_view name)
{
return DXShader::CreateFromSource(stage, source, name);
return DXShader::CreateFromSource(stage, source, shader_includer, name);
}

std::unique_ptr<AbstractShader> Gfx::CreateShaderFromBinary(ShaderStage stage, const void* data,
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/VideoBackends/D3D12/D3D12Gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ class Gfx final : public ::AbstractGfx
CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
std::vector<AbstractTexture*> additional_color_attachments) override;

std::unique_ptr<AbstractShader> CreateShaderFromSource(ShaderStage stage, std::string_view source,
std::string_view name) override;
std::unique_ptr<AbstractShader>
CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name) override;
std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage, const void* data,
size_t length,
std::string_view name) override;
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/VideoBackends/D3D12/DX12Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ std::unique_ptr<DXShader> DXShader::CreateFromBytecode(ShaderStage stage, Binary
}

std::unique_ptr<DXShader> DXShader::CreateFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name)
{
auto bytecode = CompileShader(g_dx_context->GetFeatureLevel(), stage, source);
auto bytecode = CompileShader(g_dx_context->GetFeatureLevel(), stage, source, shader_includer);
if (!bytecode)
return nullptr;

Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoBackends/D3D12/DX12Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DXShader final : public D3DCommon::Shader
static std::unique_ptr<DXShader> CreateFromBytecode(ShaderStage stage, BinaryData bytecode,
std::string_view name);
static std::unique_ptr<DXShader> CreateFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name);

private:
Expand Down
30 changes: 20 additions & 10 deletions Source/Core/VideoBackends/D3DCommon/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Common/StringUtil.h"
#include "Common/Version.h"

#include "VideoCommon/ShaderCompileUtils.h"
#include "VideoCommon/Spirv.h"
#include "VideoCommon/VideoBackendBase.h"
#include "VideoCommon/VideoConfig.h"
Expand All @@ -35,6 +36,9 @@ namespace
constexpr std::string_view SHADER_HEADER = R"(
// Target GLSL 4.5.
#version 450 core

#extension GL_ARB_shading_language_include : enable

#define ATTRIBUTE_LOCATION(x) layout(location = x)
#define FRAGMENT_OUTPUT_LOCATION(x) layout(location = x)
#define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y) layout(location = x, index = y)
Expand Down Expand Up @@ -107,14 +111,16 @@ std::optional<std::string> GetHLSLFromSPIRV(SPIRV::CodeVector spv, D3D_FEATURE_L
return compiler.compile();
}

std::optional<SPIRV::CodeVector> GetSpirv(ShaderStage stage, std::string_view source)
std::optional<SPIRV::CodeVector> GetSpirv(ShaderStage stage, std::string_view source,
glslang::TShader::Includer* shader_includer)
{
switch (stage)
{
case ShaderStage::Vertex:
{
const auto full_source = fmt::format("{}{}", SHADER_HEADER, source);
return SPIRV::CompileVertexShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0);
return SPIRV::CompileVertexShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0,
shader_includer);
}

case ShaderStage::Geometry:
Expand All @@ -126,27 +132,30 @@ std::optional<SPIRV::CodeVector> GetSpirv(ShaderStage stage, std::string_view so
case ShaderStage::Pixel:
{
const auto full_source = fmt::format("{}{}", SHADER_HEADER, source);
return SPIRV::CompileFragmentShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0);
return SPIRV::CompileFragmentShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0,
shader_includer);
}

case ShaderStage::Compute:
{
const auto full_source = fmt::format("{}{}", COMPUTE_SHADER_HEADER, source);
return SPIRV::CompileComputeShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0);
return SPIRV::CompileComputeShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0,
shader_includer);
}
};

return std::nullopt;
}

std::optional<std::string> GetHLSL(D3D_FEATURE_LEVEL feature_level, ShaderStage stage,
std::string_view source)
std::string_view source,
VideoCommon::ShaderIncluder* shader_includer)
{
if (stage == ShaderStage::Geometry)
{
return std::string{source};
}
else if (const auto spirv = GetSpirv(stage, source))
else if (const auto spirv = GetSpirv(stage, source, shader_includer))
{
return GetHLSLFromSPIRV(std::move(*spirv), feature_level);
}
Expand Down Expand Up @@ -230,10 +239,11 @@ static const char* GetCompileTarget(D3D_FEATURE_LEVEL feature_level, ShaderStage
}
}

std::optional<Shader::BinaryData> Shader::CompileShader(D3D_FEATURE_LEVEL feature_level,
ShaderStage stage, std::string_view source)
std::optional<Shader::BinaryData>
Shader::CompileShader(D3D_FEATURE_LEVEL feature_level, ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer)
{
const auto hlsl = GetHLSL(feature_level, stage, source);
const auto hlsl = GetHLSL(feature_level, stage, source, shader_includer);
if (!hlsl)
return std::nullopt;

Expand All @@ -260,7 +270,7 @@ std::optional<Shader::BinaryData> Shader::CompileShader(D3D_FEATURE_LEVEL featur
file << "Dolphin Version: " + Common::GetScmRevStr() + "\n";
file << "Video Backend: " + g_video_backend->GetDisplayName();

if (const auto spirv = GetSpirv(stage, source))
if (const auto spirv = GetSpirv(stage, source, shader_includer))
{
file << "\nOriginal Source: \n";
file << source << std::endl;
Expand Down
8 changes: 7 additions & 1 deletion Source/Core/VideoBackends/D3DCommon/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "VideoBackends/D3DCommon/D3DCommon.h"
#include "VideoCommon/AbstractShader.h"

namespace VideoCommon
{
class ShaderIncluder;
}

namespace D3DCommon
{
class Shader : public AbstractShader
Expand All @@ -20,7 +25,8 @@ class Shader : public AbstractShader
BinaryData GetBinary() const override;

static std::optional<BinaryData> CompileShader(D3D_FEATURE_LEVEL feature_level, ShaderStage stage,
std::string_view source);
std::string_view source,
VideoCommon::ShaderIncluder* shader_includer);

static BinaryData CreateByteCode(const void* data, size_t length);

Expand Down
6 changes: 4 additions & 2 deletions Source/Core/VideoBackends/Metal/MTLGfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ class Gfx final : public ::AbstractGfx
CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
std::vector<AbstractTexture*> additional_color_attachments) override;

std::unique_ptr<AbstractShader> CreateShaderFromSource(ShaderStage stage, std::string_view source,
std::string_view name) override;
std::unique_ptr<AbstractShader>
CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name) override;
std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage, const void* data,
size_t length,
std::string_view name) override;
Expand Down
9 changes: 5 additions & 4 deletions Source/Core/VideoBackends/Metal/MTLGfx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ static MTLTextureType FromAbstract(AbstractTextureType type, bool multisample)

// MARK: Pipeline Creation

std::unique_ptr<AbstractShader> Metal::Gfx::CreateShaderFromSource(ShaderStage stage,
std::string_view source,
std::string_view name)
std::unique_ptr<AbstractShader>
Metal::Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name)
{
std::optional<std::string> msl = Util::TranslateShaderToMSL(stage, source);
std::optional<std::string> msl = Util::TranslateShaderToMSL(stage, source, shader_includer);
if (!msl.has_value())
{
PanicAlertFmt("Failed to convert shader {} to MSL", name);
Expand Down
8 changes: 7 additions & 1 deletion Source/Core/VideoBackends/Metal/MTLUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

#include "VideoBackends/Metal/MRCHelpers.h"

namespace VideoCommon
{
class ShaderIncluder;
}

namespace Metal
{
struct DeviceFeatures
Expand Down Expand Up @@ -54,7 +59,8 @@ static inline bool HasStencil(AbstractTextureFormat format)
return format == AbstractTextureFormat::D24_S8 || format == AbstractTextureFormat::D32F_S8;
}

std::optional<std::string> TranslateShaderToMSL(ShaderStage stage, std::string_view source);
std::optional<std::string> TranslateShaderToMSL(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer);

} // namespace Util
} // namespace Metal
15 changes: 10 additions & 5 deletions Source/Core/VideoBackends/Metal/MTLUtil.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "VideoCommon/Constants.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/ShaderCompileUtils.h"
#include "VideoCommon/Spirv.h"

Metal::DeviceFeatures Metal::g_features;
Expand Down Expand Up @@ -490,8 +491,9 @@ fragment float4 is_helper_test() {
return resource;
}

std::optional<std::string> Metal::Util::TranslateShaderToMSL(ShaderStage stage,
std::string_view source)
std::optional<std::string>
Metal::Util::TranslateShaderToMSL(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer)
{
std::string full_source;

Expand All @@ -514,16 +516,19 @@ fragment float4 is_helper_test() {
switch (stage)
{
case ShaderStage::Vertex:
code = SPIRV::CompileVertexShader(full_source, APIType::Metal, glslang::EShTargetSpv_1_5);
code = SPIRV::CompileVertexShader(full_source, APIType::Metal, glslang::EShTargetSpv_1_5,
shader_includer);
break;
case ShaderStage::Geometry:
PanicAlertFmt("Tried to compile geometry shader for Metal, but Metal doesn't support them!");
break;
case ShaderStage::Pixel:
code = SPIRV::CompileFragmentShader(full_source, APIType::Metal, glslang::EShTargetSpv_1_5);
code = SPIRV::CompileFragmentShader(full_source, APIType::Metal, glslang::EShTargetSpv_1_5,
shader_includer);
break;
case ShaderStage::Compute:
code = SPIRV::CompileComputeShader(full_source, APIType::Metal, glslang::EShTargetSpv_1_5);
code = SPIRV::CompileComputeShader(full_source, APIType::Metal, glslang::EShTargetSpv_1_5,
shader_includer);
break;
}
if (!code.has_value())
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoBackends/Null/NullGfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class NullShader final : public AbstractShader

std::unique_ptr<AbstractShader>
NullGfx::CreateShaderFromSource(ShaderStage stage, [[maybe_unused]] std::string_view source,
[[maybe_unused]] VideoCommon::ShaderIncluder* shader_includer,
[[maybe_unused]] std::string_view name)
{
return std::make_unique<NullShader>(stage);
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/VideoBackends/Null/NullGfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class NullGfx final : public AbstractGfx
CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
std::vector<AbstractTexture*> additional_color_attachments) override;

std::unique_ptr<AbstractShader> CreateShaderFromSource(ShaderStage stage, std::string_view source,
std::string_view name) override;
std::unique_ptr<AbstractShader>
CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name) override;
std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage, const void* data,
size_t length,
std::string_view name) override;
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/VideoBackends/OGL/OGLGfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,10 @@ OGLGfx::CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* de
}

std::unique_ptr<AbstractShader>
OGLGfx::CreateShaderFromSource(ShaderStage stage, std::string_view source, std::string_view name)
OGLGfx::CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer, std::string_view name)
{
return OGLShader::CreateFromSource(stage, source, name);
return OGLShader::CreateFromSource(stage, source, shader_includer, name);
}

std::unique_ptr<AbstractShader>
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/VideoBackends/OGL/OGLGfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class OGLGfx final : public AbstractGfx
std::string_view name) override;
std::unique_ptr<AbstractStagingTexture>
CreateStagingTexture(StagingTextureType type, const TextureConfig& config) override;
std::unique_ptr<AbstractShader> CreateShaderFromSource(ShaderStage stage, std::string_view source,
std::string_view name) override;
std::unique_ptr<AbstractShader>
CreateShaderFromSource(ShaderStage stage, std::string_view source,
VideoCommon::ShaderIncluder* shader_includer,
std::string_view name) override;
std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage, const void* data,
size_t length,
std::string_view name) override;
Expand Down
Loading