Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reflection to get DXIL library shader exports #1710

Merged
merged 2 commits into from
Oct 29, 2024
Merged
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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ if(MSVC)
add_definitions(-D_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR)

if(${D3D12_SUPPORT})
set(D3D12_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/external/AgilitySDK/inc")
add_definitions(-DD3D12_SUPPORT)

# Check Windows SDK version and print warning if there is a mismatch.
Expand All @@ -182,6 +183,12 @@ if(MSVC)
"set D3D12_SUPPORT=OFF or configure the build with the recommended Windows SDK version. See BUILD.md "
"for more information.")
endif()

find_package(DXC)
if (DXC_FOUND)
set(D3D12_INCLUDE_DIRECTORIES ${D3D12_INCLUDE_DIRECTORIES} ${DXC_INCLUDE_DIR})
add_definitions(-DGFXRECON_DXC_SUPPORT)
endif()

else()
set(BUILD_LAUNCHER_AND_INTERCEPTOR OFF)
Expand All @@ -202,6 +209,8 @@ if(MSVC)
if (AGS_FOUND)
add_definitions(-DGFXRECON_AGS_SUPPORT)

set(D3D12_INCLUDE_DIRECTORIES ${D3D12_INCLUDE_DIRECTORIES} ${AGS_INCLUDE_DIR})

# The value for option GFXRECON_AGS_SUPPORT gets cached so use a non-cached variable
# to determine the final result.
set(GFXRECON_AGS_SUPPORT_FINAL ON)
Expand Down
70 changes: 70 additions & 0 deletions LICENSE_ThirdParty.txt
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,73 @@ EFFET JURIDIQUE. Le présent contrat décrit certains droits juridiques.
Vous pourriez avoir d’autres droits prévus par les lois de votre pays.
Le présent contrat ne modifie pas les droits que vous confèrent les lois
de votre pays si celles-ci ne le permettent pas.

## DirectX Shader Compiler - d3d12shader.h

Copyright (c) Microsoft Corporation.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## DirectX Shader Compiler - all other files

==============================================================================
LLVM Release License
==============================================================================
University of Illinois/NCSA
Open Source License

Copyright (c) 2003-2015 University of Illinois at Urbana-Champaign.
All rights reserved.

Developed by:

LLVM Team

University of Illinois at Urbana-Champaign

http://llvm.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal with
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.

* Neither the names of the LLVM Team, University of Illinois at
Urbana-Champaign, nor the names of its contributors may be used to
endorse or promote products derived from this Software without specific
prior written permission.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
SOFTWARE.
2 changes: 1 addition & 1 deletion cmake/FindAGS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function(FindAgsLibrary AGS_SEARCH_PATH AGS_LIBRARY_NAME AGS_LIBRARY_VAR)
unset(${AGS_LIBRARY_VAR} CACHE)
endif()
endif()
find_library(${AGS_LIBRARY_VAR} NAMES ${AGS_LIBRARY_NAME} PATHS ${AGS_SEARCH_PATH} PATH_SUFFIXES lib)
find_library(${AGS_LIBRARY_VAR} NAMES ${AGS_LIBRARY_NAME} PATHS ${AGS_SEARCH_PATH} PATH_SUFFIXES lib NO_DEFAULT_PATH)
endfunction()

# Find the build architecture.
Expand Down
72 changes: 72 additions & 0 deletions cmake/FindDXC.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# FindDXC
# -----------
#
# Find the DXC includes and library.
#
# This module defines the following variables:
#
# DXC_FOUND : True if DXC was found.
# DXC_INCLUDE_DIR : The location of the DXC header file.
# DXC_LIBRARY_PATH : Location of the DXC library.
# DXC_DLL_PATH : Location of the DXC DLL.

if (${D3D12_SUPPORT})

# Find the build architecture.
set(DXC_ARCH "")
if(CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64")
set(DXC_ARCH "arm64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(DXC_ARCH "x64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(DXC_ARCH "x86")
endif()

if(DXC_ARCH)

set(DXC_SDK_DIR "${CMAKE_BINARY_DIR}/external/DXC")
set(DXC_SDK_URL "https://github.com/microsoft/DirectXShaderCompiler/releases/download/v1.8.2407/dxc_2024_07_31.zip")

# Suppress warning on newer versions of CMake related to FetchContent file timestamp behavior.
if (${CMAKE_VERSION} VERSION_GREATER "3.24")
cmake_policy(SET CMP0135 NEW)
endif()

message(STATUS "Fetching DXC files from ${DXC_SDK_URL}")
include(FetchContent)
FetchContent_Declare(
DXC_SDK
URL ${DXC_SDK_URL}
SOURCE_DIR ${DXC_SDK_DIR}
)
FetchContent_MakeAvailable(DXC_SDK)

if(DEFINED CACHE{DXC_INCLUDE_DIR} AND NOT EXISTS "${DXC_INCLUDE_DIR}/dxcapi.h")
message("Current DXC_INCLUDE_DIR '${DXC_INCLUDE_DIR}' does not contain 'dxcapi.h'. Resetting DXC_INCLUDE_DIR.")
unset(DXC_INCLUDE_DIR CACHE)
endif()
find_path(DXC_INCLUDE_DIR NAME "dxcapi.h" PATHS "${DXC_SDK_DIR}/inc")
mark_as_advanced(DXC_INCLUDE_DIR)

# If the cached library path doesn't exist or arch doesn't match, unset variable.
if(DEFINED CACHE{DXC_LIBRARY_PATH} AND NOT EXISTS "${DXC_LIBRARY_PATH}")
message("Current DXC_LIBRARY_PATH '${DXC_LIBRARY_PATH}' does not exist. Resetting DXC_LIBRARY_PATH.")
unset(DXC_LIBRARY_PATH CACHE)
endif()
find_library(DXC_LIBRARY_PATH NAME "dxcompiler.lib" PATHS "${DXC_SDK_DIR}/lib/${DXC_ARCH}" NO_DEFAULT_PATH)
mark_as_advanced(DXC_LIBRARY_PATH)

# If the cached DLL path doesn't exist or arch doesn't match, unset variable.
if(DEFINED CACHE{DXC_DLL_PATH} AND NOT EXISTS "${DXC_DLL_PATH}")
message("Current DXC_DLL_PATH '${DXC_DLL_PATH}' does not exist. Resetting DXC_DLL_PATH.")
unset(DXC_DLL_PATH CACHE)
endif()
find_file(DXC_DLL_PATH NAME "dxcompiler.dll" PATHS "${DXC_SDK_DIR}/bin/${DXC_ARCH}" NO_DEFAULT_PATH)
mark_as_advanced(DXC_DLL_PATH)

endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(DXC REQUIRED_VARS DXC_LIBRARY_PATH DXC_DLL_PATH DXC_INCLUDE_DIR)

endif() # D3D12_SUPPORT
19 changes: 19 additions & 0 deletions cmake/InstallDXC.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if (${D3D12_SUPPORT} AND "${DXC_FOUND}")

# Setup dst folders for each build config
set(DXC_DST_DBG ${CMAKE_CURRENT_BINARY_DIR}/Debug)
set(DXC_DST_REL ${CMAKE_CURRENT_BINARY_DIR}/Release)
set(DXC_DST_REL_WITH_DBG_INFO ${CMAKE_CURRENT_BINARY_DIR}/RelWithDebInfo)
set(DXC_DST_MIN_SIZE_REL ${CMAKE_CURRENT_BINARY_DIR}/MinSizeRel)
set(DXC_DST_INSTALL ${CMAKE_INSTALL_BINDIR})

# Copy dxcompiler.dll to dst build folder
file(COPY ${DXC_DLL_PATH} DESTINATION ${DXC_DST_DBG})
file(COPY ${DXC_DLL_PATH} DESTINATION ${DXC_DST_REL})
file(COPY ${DXC_DLL_PATH} DESTINATION ${DXC_DST_REL_WITH_DBG_INFO})
file(COPY ${DXC_DLL_PATH} DESTINATION ${DXC_DST_MIN_SIZE_REL})

# Copy dxcompiler.dll to install folder
install(FILES ${DXC_DLL_PATH} DESTINATION ${DXC_DST_INSTALL})

endif()
8 changes: 4 additions & 4 deletions framework/decode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,15 @@ if (WIN32)
${CMAKE_SOURCE_DIR}/framework
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/precompiled/win64/include/
$<$<BOOL:${D3D12_SUPPORT}>:${CMAKE_SOURCE_DIR}/external/AgilitySDK/inc>)
$<$<BOOL:${D3D12_SUPPORT}>:${D3D12_INCLUDE_DIRECTORIES}>)
else()
target_include_directories(gfxrecon_decode
PUBLIC
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/framework
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/precompiled/win32/include/
$<$<BOOL:${D3D12_SUPPORT}>:${CMAKE_SOURCE_DIR}/external/AgilitySDK/inc>)
$<$<BOOL:${D3D12_SUPPORT}>:${D3D12_INCLUDE_DIRECTORIES}>)
endif()
elseif(APPLE)
target_include_directories(gfxrecon_decode
Expand All @@ -301,7 +301,7 @@ elseif(APPLE)
${CMAKE_SOURCE_DIR}/framework
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/precompiled/macos/include/
$<$<BOOL:${D3D12_SUPPORT}>:${CMAKE_SOURCE_DIR}/external/AgilitySDK/inc>)
$<$<BOOL:${D3D12_SUPPORT}>:${D3D12_INCLUDE_DIRECTORIES}>)
else()
target_include_directories(gfxrecon_decode
PRIVATE
Expand All @@ -311,7 +311,7 @@ else()
${CMAKE_SOURCE_DIR}/framework
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/precompiled/linux/include/
$<$<BOOL:${D3D12_SUPPORT}>:${CMAKE_SOURCE_DIR}/external/AgilitySDK/inc>)
$<$<BOOL:${D3D12_SUPPORT}>:${D3D12_INCLUDE_DIRECTORIES}>)
endif()

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
Expand Down
118 changes: 115 additions & 3 deletions framework/decode/dx12_resource_value_mapper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** Copyright (c) 2022 LunarG, Inc.
** Copyright (c) 2022-2024 LunarG, Inc.
** Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining a
Expand Down Expand Up @@ -27,6 +27,13 @@
#include "decode/dx12_experimental_resource_value_tracker.h"
#include "decode/dx12_object_mapping_util.h"

#if defined(GFXRECON_DXC_SUPPORT)
#include <d3d12shader.h>
#include <dxcapi.h>
#endif

#include <codecvt>

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(decode)

Expand Down Expand Up @@ -141,6 +148,30 @@ void CopyMappedResourceValuesFromSrcToDst(std::map<uint64_t, T>&
}
}

std::string DemangleDxilExportName(const std::string& mangled_name)
{
size_t demangled_name_start = mangled_name.find_first_of("?");
size_t demangled_name_end = mangled_name.find_first_of("@");

std::string demangled_name = "";
if (demangled_name_start != std::string::npos && demangled_name_end != std::string::npos)
{
// The char after '?' is the first char of the unmangled name so increment start pos.
++demangled_name_start;
demangled_name = mangled_name.substr(demangled_name_start, demangled_name_end - demangled_name_start);
}

if (!demangled_name.empty())
{
GFXRECON_LOG_DEBUG("Found demangled DXIL export name '%s'.", demangled_name.c_str());
}
else
{
GFXRECON_LOG_WARNING("Failed to demangle DXIL export name '%s'.", mangled_name.c_str());
}
return demangled_name;
}

} // namespace

Dx12ResourceValueMapper::Dx12ResourceValueMapper(std::function<DxObjectInfo*(format::HandleId id)> get_object_info_func,
Expand Down Expand Up @@ -1690,7 +1721,8 @@ void Dx12ResourceValueMapper::GetStateObjectLrsAssociationInfo(
}
else if (subobject_type == D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY)
{
// TODO: Parse local root signatures and their shader associations from the DXIL library.
// TODO: DXIL libraries can also contain local root signature definitions as well as subobject associations.
// That information should be parsed here as well to ensure correct LRS handling.
GFXRECON_LOG_DEBUG_ONCE("A state object is being created with a DXIL library subobject. Some usages of "
"DXIL library subobjects may not be fully supported by GFXR replay.");

Expand All @@ -1699,10 +1731,90 @@ void Dx12ResourceValueMapper::GetStateObjectLrsAssociationInfo(
auto num_exports = dxil_lib_desc_decoder->GetPointer()->NumExports;
if (num_exports == 0)
{
// TODO: Parse the names of all shaders exported from the DXIL library.
#if defined(GFXRECON_DXC_SUPPORT)
// If D3D12_DXIL_LIBRARY_DESC::NumExports == 0, everything in the DXIL library is exported. Use
// reflection to get the list of exported shader names.
HRESULT hr;
Copy link
Collaborator

@rurra-amd rurra-amd Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of machinery here - do we know how it might affect perf to do this work?

Wondering if any of these things can be a 1-time init and reusable thing? For instance all DxcCreateInstance calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not do any specific performance measuring. This code should only run during unoptimized DXR or EI replay where performance is already heavily affected.

The dxc_utils object could be reused but I don't know whether that is a heavy-weight operation. The other classes are based on the incoming DXIL library so the code would need to compare the DXIL library's bytes to detect if previous objects or results could be reused.

graphics::dx12::IDxcUtilsComPtr dxc_utils = nullptr;
hr = DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(&dxc_utils));
if (SUCCEEDED(hr))
{
graphics::dx12::IDxcContainerReflectionComPtr dxc_container_reflection = nullptr;
DxcCreateInstance(CLSID_DxcContainerReflection, IID_PPV_ARGS(&dxc_container_reflection));
if (SUCCEEDED(hr))
{
// Create a DXC blob from the DXIL library's bytes.
graphics::dx12::IDxcBlobEncodingComPtr dxc_blob_encoding;
hr = dxc_utils->CreateBlobFromPinned(
dxil_lib_desc_decoder->GetPointer()->DXILLibrary.pShaderBytecode,
dxil_lib_desc_decoder->GetPointer()->DXILLibrary.BytecodeLength,
DXC_CP_ACP,
&dxc_blob_encoding);
if (SUCCEEDED(hr))
{
// Load the DXIL library blob into the container reflection object.
hr = dxc_container_reflection->Load(dxc_blob_encoding);
if (SUCCEEDED(hr))
{
// Get the DXIL library reflection object.
UINT32 dxil_part;
hr = dxc_container_reflection->FindFirstPartKind(DXC_PART_DXIL, &dxil_part);
if (SUCCEEDED(hr))
{
graphics::dx12::ID3D12LibraryReflectionComPtr library_reflection;
hr = dxc_container_reflection->GetPartReflection(dxil_part,
IID_PPV_ARGS(&library_reflection));
if (SUCCEEDED(hr))
{
// Parse all exported function/shader names from the DXIL library.
D3D12_LIBRARY_DESC library_desc;
hr = library_reflection->GetDesc(&library_desc);
if (SUCCEEDED(hr))
{
for (UINT i = 0; i < library_desc.FunctionCount; i++)
{
// The pointer returned by GetFunctionByIndex is owned by the
// ID3D12LibraryReflection object and does not need to be memory managed
// in this scope.
ID3D12FunctionReflection* function_reflection =
library_reflection->GetFunctionByIndex(i);

D3D12_FUNCTION_DESC function_desc;
hr = function_reflection->GetDesc(&function_desc);
if (SUCCEEDED(hr))
{
// Export names are mangled so parse the unmangled name.
auto function_name = DemangleDxilExportName(function_desc.Name);
if (!function_name.empty())
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>
converter;
export_names.insert(converter.from_bytes(function_name));
}
}
}
}
}
}
}
}
}
}
if (FAILED(hr))
{
GFXRECON_LOG_WARNING("Failed to parse shader exports from the DXIL subobject. Shader ID to LRS "
"associations may be incorrect.");
}
#else // GFXRECON_DXC_SUPPORT
GFXRECON_LOG_WARNING_ONCE(
"GFXReconstruct was built without DirectX Shader Compiler support and cannot parse the exports "
"from DXIL_LIBRARY subobjects. This may lead to incorrect DXR behavior. To fix this, be sure that "
"the DXC depedency is successfully found during CMake project configuration.");
#endif // GFXRECON_DXC_SUPPORT
}
else
{
// Get the shader names specified explicitly in the D3D12_DXIL_LIBRARY_DESC.
for (UINT j = 0; j < num_exports; ++j)
{
export_names.insert(dxil_lib_desc_decoder->GetMetaStructPointer()
Expand Down
Loading
Loading