Skip to content

Commit 6d0ce8e

Browse files
committed
Merged PR 39167: [BR] DXGI error code mapping; tighter packing for non-planar resources
* Map DXGI HRESULTS to D3DERR/D3DDDIERR HRESULTS so that d3d9 apps can react appropriately ([#102](#102) ) * Contain dxgi error code mapping fix * Remove debug assert in ConvertBoxToRect ([#103](#103)) * Contain debug assert fix * Use tighter packing for non-planar resources ([#105](#105)) * Contain tighter packing for non-planar resources change Backport bug: https://microsoft.visualstudio.com/OS/_workitems/edit/61561225/ ---- #### AI description (iteration 1) #### PR Classification This pull request implements an API enhancement by optimizing non-planar resource packing and improving DXGI error code translation for D3D9. #### PR Summary The changes introduce a feature-flag-controlled tighter packing strategy in resource memory layout and add a new inline function to map DXGI error codes to their D3D9 counterparts, ensuring more efficient error handling and resource management. - **`src/9on12Resource.cpp`**: Implements tighter packing for non-planar resources by recalculating row pitches and total size when the feature is enabled. - **`include/9on12Util.h`**: Adds the `TranslateDxgiHrToD3D9` function and updates error handling macros to use a cached feature flag for efficient DXGI error translation. - **`src/9on12Blit.cpp`**: Adjusts the box-to-rectangle conversion check to conditionally bypass strict validations based on the feature flag. - **`src/CMakeLists.txt` & `include/pch.h`**: Update build dependencies and include directories to support the new feature integration. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
2 parents 869661c + e980cd4 commit 6d0ce8e

5 files changed

Lines changed: 60 additions & 2 deletions

File tree

include/9on12Util.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ namespace D3D9on12
8787
Value value;
8888
};
8989

90+
//
91+
// Translates DXGI error codes to their D3D9 equivalents so that the
92+
// correct HRESULTs propagate to the D3D9 runtime. Non-DXGI codes pass
93+
// through unchanged.
94+
//
95+
// Not an exhuastive list, but the ones most likely to be hit
96+
//
97+
inline HRESULT TranslateDxgiHrToD3D9(HRESULT hr)
98+
{
99+
switch (hr)
100+
{
101+
case DXGI_ERROR_DEVICE_REMOVED: return D3DDDIERR_DEVICEREMOVED;
102+
case DXGI_ERROR_DEVICE_HUNG: return D3DERR_DEVICEHUNG;
103+
case DXGI_ERROR_DEVICE_RESET: return D3DERR_DEVICELOST;
104+
case DXGI_ERROR_DRIVER_INTERNAL_ERROR: return D3DERR_DRIVERINTERNALERROR;
105+
case DXGI_ERROR_INVALID_CALL: return D3DERR_DRIVERINTERNALERROR; // This is a driver error from the app's perspective as it means that 9on12 made an invalid call
106+
default: return hr;
107+
}
108+
}
109+
90110
#define D3D9on12_DDI_ENTRYPOINT_START(implemented) \
91111
__pragma(warning(suppress:4127)) /* conditional is constant due to constant macro parameter(s) */ \
92112
if((implemented) == false && RegistryConstants::g_cBreakOnMissingDDI) \
@@ -103,6 +123,7 @@ __pragma(warning(suppress:4127)) /* conditional is constant due to constant macr
103123
try \
104124
{ \
105125

126+
static bool g_Feature_K2FixesRound1_IsEnabled = Feature_K2FixesRound1::IsEnabled(); // cache this value since it's used in a lot of entry points and is weirdly expensive to call
106127
#define CLOSE_TRYCATCH_AND_STORE_HRESULT(hr) \
107128
EntryPointHr = hr; \
108129
} \
@@ -114,6 +135,9 @@ __pragma(warning(suppress:4127)) /* conditional is constant due to constant macr
114135
{ \
115136
EntryPointHr = E_OUTOFMEMORY; \
116137
} \
138+
if(g_Feature_K2FixesRound1_IsEnabled) {\
139+
EntryPointHr = TranslateDxgiHrToD3D9(EntryPointHr); \
140+
} \
117141

118142
#define D3D9on12_DDI_ENTRYPOINT_END_AND_RETURN_HR(hr) \
119143
CLOSE_TRYCATCH_AND_STORE_HRESULT(hr) \

include/pch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
#include <stack>
4747
#include <deque>
4848

49+
#define WIL_SUPPRESS_PRIVATE_API_USE
50+
#include "FeatureStaging-D3DCD.h"
51+
4952

5053
#define BIT( x ) ( 1 << (x) )
5154
#if DBG

src/9on12Blit.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,10 @@ namespace D3D9on12
441441

442442
static inline D3D12_RECT ConvertBoxToRect(const D3D12_BOX& box)
443443
{
444-
Check9on12(box.front == 0 && box.back == 1);
445-
444+
if (!Feature_K2FixesRound1::IsEnabled())
445+
{
446+
Check9on12(box.front == 0 && box.back == 1);
447+
}
446448
D3D12_RECT rect =
447449
{
448450
static_cast<LONG>(box.left),

src/9on12Resource.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,30 @@ namespace D3D9on12
900900
&m_physicalLinearRepresentation.m_rowPitces[0],
901901
&m_totalSize);
902902

903+
if (Feature_K2FixesRound1::IsEnabled())
904+
{
905+
// If unrestricted pitch is supported, we can use tighter packing for non-planar resources
906+
// instead of the default 256-byte alignment that GetCopyableFootprints applies
907+
if (m_pParentDevice->GetContext().GetOptions13().UnrestrictedBufferTextureCopyPitchSupported && m_NonOpaquePlaneCount == 1)
908+
{
909+
m_totalSize = 0;
910+
for (UINT subresourceIndex = 0; subresourceIndex < m_numSubresources; subresourceIndex++)
911+
{
912+
auto& footprint = m_physicalLinearRepresentation.m_footprints[subresourceIndex];
913+
914+
UINT minPitch = 0;
915+
CD3D11FormatHelper::CalculateMinimumRowMajorRowPitch(footprint.Footprint.Format, footprint.Footprint.Width, minPitch);
916+
footprint.Footprint.RowPitch = minPitch;
917+
m_physicalLinearRepresentation.m_rowPitces[subresourceIndex] = minPitch; // Even though we don't use the m_rowPitches field, keep it consistent with the newly computed RowPitch
918+
footprint.Offset = m_totalSize;
919+
920+
UINT slicePitch = 0;
921+
CD3D11FormatHelper::CalculateMinimumRowMajorSlicePitch(footprint.Footprint.Format, footprint.Footprint.RowPitch, footprint.Footprint.Height, slicePitch);
922+
m_totalSize += UINT64(slicePitch) * footprint.Footprint.Depth;
923+
}
924+
}
925+
}
926+
903927
// We can safely skip initializing app memory information if only the first surf is
904928
// initialized because this only happens as when we internally create this resource
905929
// and won't be working with any app initial memory

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ FetchContent_MakeAvailable(d3d12translationlayer)
2525
target_link_libraries(d3d9on12 d3d12translationlayer_wdk)
2626
target_link_libraries(d3d9on12 d3d9on12_shaderconv)
2727

28+
# KIR dependencies
29+
target_link_libraries(d3d12translationlayer Microsoft.Windows.Wil.Internal shcore.lib)
30+
add_dependencies(d3d12translationlayer FeatureStaging-D3DCD)
31+
target_include_directories(d3d9on12 PRIVATE "${Direct3DMappingLayers_BINARY_DIR}/include/")
32+
2833
if(TARGET dxbcsigner_static)
2934
target_link_libraries(d3d9on12 dxbcsigner_static)
3035
else()

0 commit comments

Comments
 (0)