Skip to content

Commit

Permalink
D3D11: Groundwork for 2d screen view
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jun 17, 2023
1 parent fa5bd68 commit 700967b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 22 additions & 6 deletions src/mods/vr/D3D11Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ bool D3D11Component::TextureContext::clear_rtv(float* color) {
}

vr::EVRCompositorError D3D11Component::on_frame(VR* vr) {
if (m_left_eye_tex == nullptr || m_force_reset || m_last_afr_state != vr->is_using_afr()) {
if (m_force_reset || m_last_afr_state != vr->is_using_afr()) {
if (!setup()) {
spdlog::error("Failed to setup D3D11Component, trying again next frame");
m_force_reset = true;
Expand Down Expand Up @@ -710,6 +710,8 @@ void D3D11Component::on_post_present(VR* vr) {
}

void D3D11Component::on_reset(VR* vr) {
m_force_reset = true;

m_backbuffer_rtv.Reset();
m_backbuffer.Reset();
m_copied_backbuffer.Reset();
Expand All @@ -720,18 +722,20 @@ void D3D11Component::on_reset(VR* vr) {
m_left_eye_srv.Reset();
m_right_eye_srv.Reset();
m_ui_tex.Reset();
m_blank_tex.Reset();
m_left_eye_depthstencil.Reset();
m_right_eye_depthstencil.Reset();
m_vs_shader_blob.Reset();
m_ps_shader_blob.Reset();
m_vs_shader.Reset();
m_ps_shader.Reset();
m_input_layout.Reset();
m_constant_buffer.Reset();
m_backbuffer_batch.reset();
m_game_batch.reset();
m_is_shader_setup = false;

for (auto& tex : m_2d_screen_tex) {
tex.reset();
}

if (vr->get_runtime()->is_openxr() && vr->get_runtime()->loaded) {
auto& rt_pool = vr->get_render_target_pool_hook();
ComPtr<ID3D11Texture2D> scene_depth_tex{rt_pool->get_texture<ID3D11Texture2D>(L"SceneDepthZ")};
Expand Down Expand Up @@ -810,6 +814,7 @@ bool D3D11Component::setup() {
}

m_backbuffer_batch = std::make_unique<DirectX::DX11::SpriteBatch>(context.Get());
m_game_batch = std::make_unique<DirectX::DX11::SpriteBatch>(context.Get());

// Get backbuffer description.
D3D11_TEXTURE2D_DESC backbuffer_desc{};
Expand Down Expand Up @@ -851,11 +856,22 @@ bool D3D11Component::setup() {
backbuffer_desc.Width = (uint32_t)g_framework->get_d3d11_rt_size().x;
backbuffer_desc.Height = (uint32_t)g_framework->get_d3d11_rt_size().y;
device->CreateTexture2D(&backbuffer_desc, nullptr, &m_ui_tex);
device->CreateTexture2D(&backbuffer_desc, nullptr, &m_blank_tex);

for (auto& ctx : m_2d_screen_tex) {
ComPtr<ID3D11Texture2D> tex{};
if (FAILED(device->CreateTexture2D(&backbuffer_desc, nullptr, &tex))) {
spdlog::error("[VR] Failed to create 2D screen texture (D3D11).");
continue;
}

if (!ctx.set(tex.Get(), DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, DXGI_FORMAT_B8G8R8A8_UNORM_SRGB)) {
spdlog::error("[VR] Failed to setup 2D screen texture context (D3D11).");
continue;
}
}

// No need to pass the format as the backbuffer is not a typeless format.
clear_tex(m_ui_tex.Get());
clear_tex(m_blank_tex.Get());

// copy backbuffer into right eye
context->CopyResource(m_right_eye_tex.Get(), backbuffer.Get());
Expand Down
8 changes: 3 additions & 5 deletions src/mods/vr/D3D11Component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class D3D11Component {
auto& openxr() { return m_openxr; }

auto& get_ui_tex() { return m_ui_tex; }
auto& get_blank_tex() { return m_blank_tex; }

bool clear_tex(ID3D11Resource* rsrc, std::optional<DXGI_FORMAT> format = std::nullopt);
void copy_tex(ID3D11Resource* src, ID3D11Resource* dst);
Expand Down Expand Up @@ -117,11 +116,9 @@ class D3D11Component {
ComPtr<ID3D11Texture2D> m_ui_tex{};
TextureContext m_engine_ui_ref{};
TextureContext m_engine_tex_ref{};
ComPtr<ID3D11Texture2D> m_blank_tex{};
std::array<TextureContext, 2> m_2d_screen_tex{};
ComPtr<ID3D11Texture2D> m_left_eye_tex{};
ComPtr<ID3D11Texture2D> m_right_eye_tex{};
ComPtr<ID3D11Texture2D> m_left_eye_depthstencil{};
ComPtr<ID3D11Texture2D> m_right_eye_depthstencil{};
ComPtr<ID3DBlob> m_vs_shader_blob{};
ComPtr<ID3DBlob> m_ps_shader_blob{};

Expand All @@ -137,6 +134,7 @@ class D3D11Component {
ComPtr<ID3D11ShaderResourceView> m_right_eye_srv{};

std::unique_ptr<DirectX::DX11::SpriteBatch> m_backbuffer_batch{};
std::unique_ptr<DirectX::DX11::SpriteBatch> m_game_batch{};

vr::HmdMatrix44_t m_left_eye_proj{};
vr::HmdMatrix44_t m_right_eye_proj{};
Expand All @@ -148,7 +146,7 @@ class D3D11Component {
std::array<uint32_t, 2> m_real_backbuffer_size{};

uint32_t m_last_rendered_frame{0};
bool m_force_reset{false};
bool m_force_reset{true};
bool m_submitted_left_eye{false};
bool m_is_shader_setup{false};
bool m_last_afr_state{false};
Expand Down

0 comments on commit 700967b

Please sign in to comment.