Skip to content

Commit 365b1d1

Browse files
committed
Fix DFR with Unity.
1 parent 080794c commit 365b1d1

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

XR_APILAYER_MBUCCHIA_toolkit/factories.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ namespace toolkit {
130130
uint32_t renderHeight,
131131
uint32_t displayWidth,
132132
uint32_t displayHeight,
133-
bool hasVisibilityMask);
134-
133+
bool hasVisibilityMask,
134+
bool needMirroredPattern);
135135

136136
bool IsDeviceSupportingFP16(std::shared_ptr<IDevice> device);
137137

XR_APILAYER_MBUCCHIA_toolkit/layer.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ namespace {
270270
if (m_isOpenComposite) {
271271
Log("Detected OpenComposite\n");
272272
}
273+
m_isUnity = std::string_view(createInfo->applicationInfo.engineName) == "Unity";
273274

274275
// Dump the OpenXR runtime information to help debugging customer issues.
275276
XrInstanceProperties instanceProperties = {XR_TYPE_INSTANCE_PROPERTIES, nullptr};
@@ -759,15 +760,17 @@ namespace {
759760
m_configManager, m_graphicsDevice, m_displayWidth, m_displayHeight, heuristic);
760761
}
761762

762-
m_variableRateShader = graphics::CreateVariableRateShader(*this,
763-
m_configManager,
764-
m_graphicsDevice,
765-
m_eyeTracker,
766-
renderWidth,
767-
renderHeight,
768-
m_displayWidth,
769-
m_displayHeight,
770-
!m_isOpenComposite && m_hasVisibilityMaskKHR);
763+
m_variableRateShader =
764+
graphics::CreateVariableRateShader(*this,
765+
m_configManager,
766+
m_graphicsDevice,
767+
m_eyeTracker,
768+
renderWidth,
769+
renderHeight,
770+
m_displayWidth,
771+
m_displayHeight,
772+
!m_isOpenComposite && m_hasVisibilityMaskKHR,
773+
m_isUnity);
771774

772775
// Register intercepted events.
773776
m_graphicsDevice->registerSetRenderTargetEvent(
@@ -3386,6 +3389,7 @@ namespace {
33863389

33873390
std::string m_applicationName;
33883391
bool m_isOpenComposite{false};
3392+
bool m_isUnity{false};
33893393
std::string m_runtimeName;
33903394
std::string m_systemName;
33913395
XrSystemId m_vrSystemId{XR_NULL_SYSTEM_ID};

XR_APILAYER_MBUCCHIA_toolkit/vrs.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace {
105105
// The number of frames since the mask was last used during a rendering pass.
106106
uint16_t age;
107107

108-
std::shared_ptr<IShaderBuffer> cbShading[ViewCount + 2];
108+
std::shared_ptr<IShaderBuffer> cbShading[ViewCount * 2 + 2];
109109
std::shared_ptr<ITexture> mask[ViewCount + 1];
110110
std::shared_ptr<ITexture> maskDoubleWide;
111111
std::shared_ptr<ITexture> maskTextureArray;
@@ -129,11 +129,13 @@ namespace {
129129
uint32_t displayHeight,
130130
uint32_t tileSize,
131131
uint32_t tileRateMax,
132-
bool hasVisibilityMask)
132+
bool hasVisibilityMask,
133+
bool needMirroredPattern)
133134
: m_openXR(openXR), m_configManager(configManager), m_device(graphicsDevice), m_eyeTracker(eyeTracker),
134135
m_renderWidth(renderWidth), m_renderHeight(renderHeight),
135136
m_renderRatio(float(renderWidth) / renderHeight), m_tileSize(tileSize), m_tileRateMax(tileRateMax),
136-
m_actualRenderWidth(renderWidth), m_hasVisibilityMask(hasVisibilityMask) {
137+
m_actualRenderWidth(renderWidth), m_hasVisibilityMask(hasVisibilityMask),
138+
m_needMirroredPattern(needMirroredPattern) {
137139
createRenderResources(m_renderWidth, m_renderHeight);
138140

139141
// Set initial projection center
@@ -890,6 +892,20 @@ namespace {
890892
m_device->dispatchShader();
891893
mask.mask[target]->setState(D3D12_RESOURCE_STATE_COPY_SOURCE);
892894
}
895+
if (m_usingEyeTracking && m_needMirroredPattern) {
896+
for (size_t i = 0; i < 2; i++) {
897+
const auto constants = makeShadingConstants(i, mask.widthInTiles, mask.heightInTiles, true);
898+
mask.cbShading[i]->uploadData(&constants, sizeof(constants));
899+
900+
m_csShading->updateThreadGroups({dispatchX, dispatchY, 1});
901+
m_device->setShader(m_csShading, SamplerType::NearestClamp);
902+
m_device->setShaderInput(0, mask.cbShading[i]);
903+
mask.mask[i]->setState(D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
904+
m_device->setShaderOutput(0, mask.mask[i]);
905+
m_device->dispatchShader();
906+
mask.mask[i]->setState(D3D12_RESOURCE_STATE_COPY_SOURCE);
907+
}
908+
}
893909

894910
// Copy to the double wide/texture arrays mask.
895911
mask.mask[0]->copyTo(mask.maskDoubleWide, 0, 0, 0);
@@ -904,9 +920,13 @@ namespace {
904920
TraceLoggingWriteStop(local, "VariableRateShading_UpdateMask");
905921
}
906922

907-
ShadingConstants makeShadingConstants(size_t eye, uint32_t texW, uint32_t texH) {
923+
ShadingConstants makeShadingConstants(size_t eye, uint32_t texW, uint32_t texH, bool upsideDown = false) {
908924
ShadingConstants constants;
909-
constants.GazeXY = m_gazeLocation[eye];
925+
if (!upsideDown) {
926+
constants.GazeXY = m_gazeLocation[eye];
927+
} else {
928+
constants.GazeXY = {m_gazeLocation[eye].x, -m_gazeLocation[eye].y};
929+
}
910930
constants.InvDim = {1.f / texW, 1.f / texH};
911931
for (size_t i = 0; i < std::size(m_Rings); i++) {
912932
constants.Rings[i] = m_Rings[i];
@@ -1052,6 +1072,7 @@ namespace {
10521072

10531073
XrSession m_session{XR_NULL_HANDLE};
10541074
bool m_usingEyeTracking{false};
1075+
bool m_needMirroredPattern{false};
10551076

10561077
std::map<uint32_t, uint32_t> m_renderScales;
10571078
uint32_t m_actualRenderWidth;
@@ -1141,7 +1162,8 @@ namespace toolkit::graphics {
11411162
uint32_t renderHeight,
11421163
uint32_t displayWidth,
11431164
uint32_t displayHeight,
1144-
bool hasVisibilityMask) {
1165+
bool hasVisibilityMask,
1166+
bool needMirroredPattern) {
11451167
try {
11461168
uint32_t tileSize = 0;
11471169
uint32_t tileRateMax = 0;
@@ -1207,7 +1229,8 @@ namespace toolkit::graphics {
12071229
displayHeight,
12081230
tileSize,
12091231
tileRateMax,
1210-
hasVisibilityMask);
1232+
hasVisibilityMask,
1233+
needMirroredPattern);
12111234

12121235
} catch (FeatureNotSupported&) {
12131236
return nullptr;

0 commit comments

Comments
 (0)