Skip to content

Commit

Permalink
Merge pull request #10673 from iwubcode/spirv-backends
Browse files Browse the repository at this point in the history
D3D: Generate HLSL from SPIRV*
  • Loading branch information
JMC47 committed Jul 8, 2022
2 parents 7cc89f5 + a8b5266 commit 828afc6
Show file tree
Hide file tree
Showing 35 changed files with 1,180 additions and 1,290 deletions.
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
url = https://github.com/libusb/libusb.git
branch = master
shallow = true
[submodule "Externals/spirv_cross/SPIRV-Cross"]
path = Externals/spirv_cross/SPIRV-Cross
url = https://github.com/KhronosGroup/SPIRV-Cross.git
branch = master
shallow = true
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ else()
endif()
add_subdirectory(Externals/imgui)
add_subdirectory(Externals/glslang)
add_subdirectory(Externals/spirv_cross)

if(ENABLE_VULKAN)
add_definitions(-DHAS_VULKAN)
Expand Down
3 changes: 3 additions & 0 deletions Externals/ExternalsReferenceAll.props
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
<ProjectReference Include="$(ExternalsDir)soundtouch\SoundTouch.vcxproj">
<Project>{ec082900-b4d8-42e9-9663-77f02f6936ae}</Project>
</ProjectReference>
<ProjectReference Include="$(ExternalsDir)spirv_cross\spirv_cross.vcxproj">
<Project>{3d780617-ec8c-4721-b9fd-dfc9bb658c7c}</Project>
</ProjectReference>
<ProjectReference Include="$(ExternalsDir)xxhash\xxhash.vcxproj">
<Project>{677ea016-1182-440c-9345-dc88d1e98c0c}</Project>
</ProjectReference>
Expand Down
52 changes: 52 additions & 0 deletions Externals/spirv_cross/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
set(SRCS
SPIRV-Cross/GLSL.std.450.h
SPIRV-Cross/spirv.h
SPIRV-Cross/spirv.hpp
SPIRV-Cross/spirv_cfg.cpp
SPIRV-Cross/spirv_cfg.hpp
SPIRV-Cross/spirv_common.hpp
SPIRV-Cross/spirv_cpp.cpp
SPIRV-Cross/spirv_cpp.hpp
SPIRV-Cross/spirv_cross.cpp
SPIRV-Cross/spirv_cross.hpp
SPIRV-Cross/spirv_cross_c.cpp
SPIRV-Cross/spirv_cross_c.h
SPIRV-Cross/spirv_cross_containers.hpp
SPIRV-Cross/spirv_cross_error_handling.hpp
SPIRV-Cross/spirv_cross_parsed_ir.cpp
SPIRV-Cross/spirv_cross_parsed_ir.hpp
SPIRV-Cross/spirv_cross_util.cpp
SPIRV-Cross/spirv_cross_util.hpp
SPIRV-Cross/spirv_glsl.cpp
SPIRV-Cross/spirv_glsl.hpp
SPIRV-Cross/spirv_hlsl.cpp
SPIRV-Cross/spirv_hlsl.hpp
SPIRV-Cross/spirv_msl.cpp
SPIRV-Cross/spirv_msl.hpp
SPIRV-Cross/spirv_parser.cpp
SPIRV-Cross/spirv_parser.hpp
SPIRV-Cross/spirv_reflect.cpp
SPIRV-Cross/spirv_reflect.hpp
)

if(NOT MSVC)
# spirv_cross requires C++11 at a minimum to compile.
add_compile_options(-std=c++11)

# Silence some warnings that occur frequently to reduce noise in build logs.
add_compile_options(-Wno-shadow)
add_compile_options(-Wno-reorder)
add_compile_options(-Wno-sign-compare)
add_compile_options(-Wno-parentheses)
add_compile_options(-Wno-unused-variable)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-Wno-unused-but-set-variable)
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options(-Wno-missing-variable-declarations)
endif()
endif()

add_library(spirv_cross STATIC ${SRCS})

target_compile_definitions(spirv_cross PUBLIC SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS)
target_include_directories(spirv_cross PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/SPIRV-Cross/include ${CMAKE_CURRENT_SOURCE_DIR}/SPIRV-Cross)
1 change: 1 addition & 0 deletions Externals/spirv_cross/SPIRV-Cross
Submodule SPIRV-Cross added at 50b4d5
53 changes: 53 additions & 0 deletions Externals/spirv_cross/spirv_cross.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\Source\VSProps\Base.Macros.props" />
<Import Project="$(VSPropsDir)Base.Targets.props" />
<PropertyGroup Label="Globals">
<ProjectGuid>{3d780617-ec8c-4721-b9fd-dfc9bb658c7c}</ProjectGuid>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<Import Project="$(VSPropsDir)Configuration.StaticLibrary.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings" />
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(VSPropsDir)Base.props" />
<Import Project="$(VSPropsDir)ClDisableAllWarnings.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemGroup>
<ClCompile Include="SPIRV-Cross\spirv_cfg.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_cpp.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_cross.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_cross_c.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_cross_parsed_ir.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_cross_util.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_glsl.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_hlsl.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_msl.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_parser.cpp" />
<ClCompile Include="SPIRV-Cross\spirv_reflect.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="SPIRV-Cross\GLSL.std.450.h" />
<ClInclude Include="SPIRV-Cross\spirv.h" />
<ClInclude Include="SPIRV-Cross\spirv.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_cfg.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_common.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_cpp.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_cross.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_cross_c.h" />
<ClInclude Include="SPIRV-Cross\spirv_cross_containers.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_cross_error_handling.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_cross_parsed_ir.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_cross_util.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_glsl.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_hlsl.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_msl.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_parser.hpp" />
<ClInclude Include="SPIRV-Cross\spirv_reflect.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
2 changes: 2 additions & 0 deletions Source/Core/DolphinLib.props
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@
<ClInclude Include="VideoCommon\RenderState.h" />
<ClInclude Include="VideoCommon\ShaderCache.h" />
<ClInclude Include="VideoCommon\ShaderGenCommon.h" />
<ClInclude Include="VideoCommon\Spirv.h" />
<ClInclude Include="VideoCommon\Statistics.h" />
<ClInclude Include="VideoCommon\TextureCacheBase.h" />
<ClInclude Include="VideoCommon\TextureConfig.h" />
Expand Down Expand Up @@ -1253,6 +1254,7 @@
<ClCompile Include="VideoCommon\RenderState.cpp" />
<ClCompile Include="VideoCommon\ShaderCache.cpp" />
<ClCompile Include="VideoCommon\ShaderGenCommon.cpp" />
<ClCompile Include="VideoCommon\Spirv.cpp" />
<ClCompile Include="VideoCommon\Statistics.cpp" />
<ClCompile Include="VideoCommon\TextureCacheBase.cpp" />
<ClCompile Include="VideoCommon\TextureConfig.cpp" />
Expand Down
8 changes: 5 additions & 3 deletions Source/Core/VideoBackends/D3D/D3DBoundingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ bool D3DBoundingBox::Initialize()
// Create 2 buffers here.
// First for unordered access on default pool.
auto desc = CD3D11_BUFFER_DESC(NUM_BBOX_VALUES * sizeof(BBoxType), D3D11_BIND_UNORDERED_ACCESS,
D3D11_USAGE_DEFAULT, 0, 0, sizeof(BBoxType));
D3D11_USAGE_DEFAULT, 0, D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS,
sizeof(BBoxType));
const BBoxType initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = initial_values;
Expand All @@ -44,6 +45,7 @@ bool D3DBoundingBox::Initialize()
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.BindFlags = 0;
desc.MiscFlags = 0;
hr = D3D::device->CreateBuffer(&desc, nullptr, &m_staging_buffer);
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox Staging Buffer: {}",
DX11HRWrap(hr));
Expand All @@ -53,10 +55,10 @@ bool D3DBoundingBox::Initialize()

// UAV is required to allow concurrent access.
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {};
UAVdesc.Format = DXGI_FORMAT_R32_SINT;
UAVdesc.Format = DXGI_FORMAT_R32_TYPELESS;
UAVdesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
UAVdesc.Buffer.FirstElement = 0;
UAVdesc.Buffer.Flags = 0;
UAVdesc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_RAW;
UAVdesc.Buffer.NumElements = NUM_BBOX_VALUES;
hr = D3D::device->CreateUnorderedAccessView(m_buffer.Get(), &UAVdesc, &m_uav);
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox UAV: {}", DX11HRWrap(hr));
Expand Down
17 changes: 9 additions & 8 deletions Source/Core/VideoBackends/D3D/D3DNativeVertexFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ D3DVertexFormat::D3DVertexFormat(const PortableVertexDeclaration& vtx_decl)
const AttributeFormat* format = &vtx_decl.position;
if (format->enable)
{
m_elems[m_num_elems].SemanticName = "POSITION";
m_elems[m_num_elems].SemanticName = "TEXCOORD";
m_elems[m_num_elems].SemanticIndex = SHADER_POSITION_ATTRIB;
m_elems[m_num_elems].AlignedByteOffset = format->offset;
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
Expand All @@ -115,12 +116,11 @@ D3DVertexFormat::D3DVertexFormat(const PortableVertexDeclaration& vtx_decl)

for (int i = 0; i < 3; i++)
{
static constexpr std::array<const char*, 3> NAMES = {"NORMAL", "TANGENT", "BINORMAL"};
format = &vtx_decl.normals[i];
if (format->enable)
{
m_elems[m_num_elems].SemanticName = NAMES[i];
m_elems[m_num_elems].SemanticIndex = 0;
m_elems[m_num_elems].SemanticName = "TEXCOORD";
m_elems[m_num_elems].SemanticIndex = SHADER_NORMAL_ATTRIB + i;
m_elems[m_num_elems].AlignedByteOffset = format->offset;
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
Expand All @@ -133,8 +133,8 @@ D3DVertexFormat::D3DVertexFormat(const PortableVertexDeclaration& vtx_decl)
format = &vtx_decl.colors[i];
if (format->enable)
{
m_elems[m_num_elems].SemanticName = "COLOR";
m_elems[m_num_elems].SemanticIndex = i;
m_elems[m_num_elems].SemanticName = "TEXCOORD";
m_elems[m_num_elems].SemanticIndex = SHADER_COLOR0_ATTRIB + i;
m_elems[m_num_elems].AlignedByteOffset = format->offset;
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
Expand All @@ -148,7 +148,7 @@ D3DVertexFormat::D3DVertexFormat(const PortableVertexDeclaration& vtx_decl)
if (format->enable)
{
m_elems[m_num_elems].SemanticName = "TEXCOORD";
m_elems[m_num_elems].SemanticIndex = i;
m_elems[m_num_elems].SemanticIndex = SHADER_TEXTURE0_ATTRIB + i;
m_elems[m_num_elems].AlignedByteOffset = format->offset;
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
Expand All @@ -159,7 +159,8 @@ D3DVertexFormat::D3DVertexFormat(const PortableVertexDeclaration& vtx_decl)
format = &vtx_decl.posmtx;
if (format->enable)
{
m_elems[m_num_elems].SemanticName = "BLENDINDICES";
m_elems[m_num_elems].SemanticName = "TEXCOORD";
m_elems[m_num_elems].SemanticIndex = SHADER_POSMTX_ATTRIB;
m_elems[m_num_elems].AlignedByteOffset = format->offset;
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoBackends/D3D/D3DState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void StateManager::Apply()
if (m_current.vertexConstants != m_pending.vertexConstants)
{
D3D::context->VSSetConstantBuffers(0, 1, &m_pending.vertexConstants);
D3D::context->VSSetConstantBuffers(1, 1, &m_pending.vertexConstants);
m_current.vertexConstants = m_pending.vertexConstants;
}

Expand Down
2 changes: 2 additions & 0 deletions Source/Core/VideoBackends/D3D12/D3D12Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ bool Renderer::ApplyState()
{
cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_VS_CBV,
m_state.constant_buffers[1]);
cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_VS_CBV2,
m_state.constant_buffers[1]);

if (g_ActiveConfig.bEnablePixelLighting)
{
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/VideoBackends/D3D12/DX12Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ bool DXContext::CreateRootSignatures()
bool DXContext::CreateGXRootSignature()
{
// GX:
// - 3 constant buffers (bindings 0-2), 0/1 visible in PS, 1 visible in VS, 2 visible in GS.
// - 3 constant buffers (bindings 0-2), 0/1 visible in PS, 2 visible in VS, 1 visible in GS.
// - 8 textures (visible in PS).
// - 8 samplers (visible in PS).
// - 1 UAV (visible in PS).
Expand All @@ -341,6 +341,8 @@ bool DXContext::CreateGXRootSignature()
param_count++;
SetRootParamCBV(&params[param_count], 0, D3D12_SHADER_VISIBILITY_VERTEX);
param_count++;
SetRootParamCBV(&params[param_count], 1, D3D12_SHADER_VISIBILITY_VERTEX);
param_count++;
SetRootParamCBV(&params[param_count], 0, D3D12_SHADER_VISIBILITY_GEOMETRY);
param_count++;

Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoBackends/D3D12/DX12Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum ROOT_PARAMETER
ROOT_PARAMETER_PS_SRV,
ROOT_PARAMETER_PS_SAMPLERS,
ROOT_PARAMETER_VS_CBV,
ROOT_PARAMETER_VS_CBV2,
ROOT_PARAMETER_GS_CBV,
ROOT_PARAMETER_PS_UAV_OR_CBV2,
ROOT_PARAMETER_PS_CBV2, // ROOT_PARAMETER_PS_UAV_OR_CBV2 if bbox is not enabled
Expand Down
11 changes: 5 additions & 6 deletions Source/Core/VideoBackends/D3D12/DX12VertexFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void DXVertexFormat::MapAttributes()
if (m_decl.position.enable)
{
AddAttribute(
"POSITION", 0, 0,
"TEXCOORD", SHADER_POSITION_ATTRIB, 0,
VarToDXGIFormat(m_decl.position.type, m_decl.position.components, m_decl.position.integer),
m_decl.position.offset);
}
Expand All @@ -92,8 +92,7 @@ void DXVertexFormat::MapAttributes()
{
if (m_decl.normals[i].enable)
{
static constexpr std::array<const char*, 3> NAMES = {"NORMAL", "TANGENT", "BINORMAL"};
AddAttribute(NAMES[i], 0, 0,
AddAttribute("TEXCOORD", SHADER_NORMAL_ATTRIB + i, 0,
VarToDXGIFormat(m_decl.normals[i].type, m_decl.normals[i].components,
m_decl.normals[i].integer),
m_decl.normals[i].offset);
Expand All @@ -104,7 +103,7 @@ void DXVertexFormat::MapAttributes()
{
if (m_decl.colors[i].enable)
{
AddAttribute("COLOR", i, 0,
AddAttribute("TEXCOORD", SHADER_COLOR0_ATTRIB + i, 0,
VarToDXGIFormat(m_decl.colors[i].type, m_decl.colors[i].components,
m_decl.colors[i].integer),
m_decl.colors[i].offset);
Expand All @@ -115,7 +114,7 @@ void DXVertexFormat::MapAttributes()
{
if (m_decl.texcoords[i].enable)
{
AddAttribute("TEXCOORD", i, 0,
AddAttribute("TEXCOORD", SHADER_TEXTURE0_ATTRIB + i, 0,
VarToDXGIFormat(m_decl.texcoords[i].type, m_decl.texcoords[i].components,
m_decl.texcoords[i].integer),
m_decl.texcoords[i].offset);
Expand All @@ -125,7 +124,7 @@ void DXVertexFormat::MapAttributes()
if (m_decl.posmtx.enable)
{
AddAttribute(
"BLENDINDICES", 0, 0,
"TEXCOORD", SHADER_POSMTX_ATTRIB, 0,
VarToDXGIFormat(m_decl.posmtx.type, m_decl.posmtx.components, m_decl.posmtx.integer),
m_decl.posmtx.offset);
}
Expand Down
1 change: 1 addition & 0 deletions Source/Core/VideoBackends/D3DCommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ target_link_libraries(videod3dcommon
PUBLIC
common
videocommon
spirv_cross
)

if(MSVC)
Expand Down
Loading

0 comments on commit 828afc6

Please sign in to comment.