Skip to content

Commit

Permalink
Better way of fixing Slate UI on res changes
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Oct 31, 2023
1 parent c9371e7 commit 3a87869
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/mods/vr/D3D11Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,44 @@ vr::EVRCompositorError D3D11Component::on_frame(VR* vr) {
if (ui_target != nullptr) {
// We use SRGB for the RTV but not for the SRV because it screws up the colors when drawing the spectator view
m_engine_ui_ref.set((ID3D11Texture2D*)ui_target->get_native_resource(), DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, DXGI_FORMAT_B8G8R8A8_UNORM);

// Recreate UI texture if needed
if (!vr->is_extreme_compatibility_mode_enabled()) {
const auto native = (ID3D11Texture2D*)ui_target->get_native_resource();
const auto is_same_native = native == m_last_checked_native;
m_last_checked_native = native;

if (native != nullptr && !is_same_native) {
// get desc
D3D11_TEXTURE2D_DESC desc{};
native->GetDesc(&desc);

if (runtime->is_openxr()) {
if (auto it = vr->m_openxr->swapchains.find((uint32_t)runtimes::OpenXR::SwapchainIndex::UI);
it != vr->m_openxr->swapchains.end())
{
const auto& uisc = it->second;
if (desc.Width != uisc.width ||
desc.Height != uisc.height)
{
SPDLOG_INFO_EVERY_N_SEC(1, "[OpenXR] UI size changed, recreating [{}x{}]->[{}x{}]", desc.Width, desc.Height, uisc.width, uisc.height);
ffsr->set_should_recreate_textures(true);
}
}
} else if (m_ui_tex != nullptr) {
D3D11_TEXTURE2D_DESC ui_desc{};
m_ui_tex->GetDesc(&ui_desc);

if (desc.Width != ui_desc.Width || desc.Height != ui_desc.Height) {
SPDLOG_INFO_EVERY_N_SEC(1, "[OpenVR] UI size changed, recreating texture [{}x{}]->[{}x{}]", desc.Width, desc.Height, ui_desc.Width, ui_desc.Height);
ffsr->set_should_recreate_textures(true);
}
}
} else if (native == nullptr) {
spdlog::error("[VR] Recreating UI texture because native resource is null");
ffsr->set_should_recreate_textures(true);
}
}
} else {
m_engine_ui_ref.reset();
}
Expand Down
2 changes: 2 additions & 0 deletions src/mods/vr/D3D11Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class D3D11Component {
std::array<uint32_t, 2> m_backbuffer_size{};
std::array<uint32_t, 2> m_real_backbuffer_size{};

ID3D11Texture2D* m_last_checked_native{nullptr};

uint32_t m_last_rendered_frame{0};
bool m_force_reset{true};
bool m_submitted_left_eye{false};
Expand Down
35 changes: 35 additions & 0 deletions src/mods/vr/D3D12Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,41 @@ vr::EVRCompositorError D3D12Component::on_frame(VR* vr) {
m_game_ui_tex.reset();
}
}

// Recreate UI texture if needed
if (!vr->is_extreme_compatibility_mode_enabled()) {
const auto native = (ID3D12Resource*)ui_target->get_native_resource();
const auto is_same_native = native == m_last_checked_native;
m_last_checked_native = native;

if (native != nullptr && !is_same_native) {
const auto desc = native->GetDesc();

if (runtime->is_openxr()) {
if (auto it = vr->m_openxr->swapchains.find((uint32_t)runtimes::OpenXR::SwapchainIndex::UI);
it != vr->m_openxr->swapchains.end())
{
const auto& uisc = it->second;
if (desc.Width != uisc.width ||
desc.Height != uisc.height)
{
SPDLOG_INFO_EVERY_N_SEC(1, "[OpenXR] UI size changed, recreating [{}x{}]->[{}x{}]", desc.Width, desc.Height, uisc.width, uisc.height);
ffsr->set_should_recreate_textures(true);
}
}
} else if (m_game_ui_tex.texture != nullptr) {
const auto ui_desc = m_game_ui_tex.texture->GetDesc();

if (desc.Width != ui_desc.Width || desc.Height != ui_desc.Height) {
SPDLOG_INFO_EVERY_N_SEC(1, "[OpenVR] UI size changed, recreating texture [{}x{}]->[{}x{}]", desc.Width, desc.Height, ui_desc.Width, ui_desc.Height);
ffsr->set_should_recreate_textures(true);
}
}
} else if (native == nullptr) {
spdlog::error("[VR] Recreating UI texture because native resource is null");
ffsr->set_should_recreate_textures(true);
}
}
}

const float clear_color[] = { 0.0f, 0.0f, 0.0f, 0.0f };
Expand Down
2 changes: 2 additions & 0 deletions src/mods/vr/D3D12Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class D3D12Component {
std::unique_ptr<DirectX::DX12::SpriteBatch> m_backbuffer_batch{};
std::unique_ptr<DirectX::DX12::SpriteBatch> m_game_batch{};

ID3D12Resource* m_last_checked_native{nullptr};

// Mimicking what OpenXR does.
struct OpenVR {
OpenVR(D3D12Component* p) : parent{p} {}
Expand Down

0 comments on commit 3a87869

Please sign in to comment.