Skip to content

Commit

Permalink
D3D12 (GameObjectsDisplay, ChainViewer): Fix some correctness issues
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Nov 15, 2024
1 parent ac59669 commit 5619822
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 30 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -2525,6 +2526,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -4399,6 +4401,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -4608,6 +4611,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -4817,6 +4821,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -7525,6 +7530,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -7734,6 +7740,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -8777,6 +8784,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -9818,6 +9826,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -10861,6 +10870,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -11904,6 +11914,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -12947,6 +12958,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down Expand Up @@ -13990,6 +14002,7 @@ if(REF_BUILD_FRAMEWORK AND CMAKE_SIZEOF_VOID_P EQUAL 8) # build-framework
"src/re2-imgui/re2_imconfig.hpp"
"src/utility/ImGui.hpp"
"src/cimgui/cimgui.h"
"src/mods/spritefonts/Roboto.spritefont.h"
"src/re2-imgui/af_fadef.h"
"src/re2-imgui/imgui_impl_dx11.h"
"src/re2-imgui/imgui_impl_dx12.h"
Expand Down
5 changes: 2 additions & 3 deletions src/REFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2175,9 +2175,8 @@ bool REFramework::init_d3d12() {
spdlog::info("[D3D12] Creating DXTK graphics memory...");

// Realistically we only have one device. If not, then... IDK
if (m_d3d12.graphics_memory == nullptr) {
m_d3d12.graphics_memory = std::make_unique<DirectX::DX12::GraphicsMemory>(device);
}
m_d3d12.graphics_memory.reset();
m_d3d12.graphics_memory = std::make_unique<DirectX::DX12::GraphicsMemory>(device);

spdlog::info("[D3D12] Creating command allocator...");

Expand Down
6 changes: 3 additions & 3 deletions src/mods/BackBufferRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ void BackBufferRenderer::render_d3d12() {
}

if (m_d3d12.backbuffers[i] == nullptr || m_d3d12.backbuffers[i]->texture.Get() != backbuffer.Get()) {
spdlog::info("[ChainViewer] Setting up backbuffer {}", i);
spdlog::info("[BackBufferRenderer] Setting up backbuffer {}", i);

m_d3d12.backbuffers[i].reset();

m_d3d12.backbuffers[i] = std::make_unique<d3d12::TextureContext>();
if (!m_d3d12.backbuffers[i]->setup(device, backbuffer.Get(), std::nullopt, std::nullopt, L"ChainViewer Backbuffer")) {
spdlog::error("[ChainViewer] Failed to setup backbuffer {}", i);
if (!m_d3d12.backbuffers[i]->setup(device, backbuffer.Get(), std::nullopt, std::nullopt, L"BackBufferRenderer Backbuffer")) {
spdlog::error("[BackBufferRenderer] Failed to setup backbuffer {}", i);
m_d3d12.backbuffers[i].reset();
continue;
}
Expand Down
19 changes: 14 additions & 5 deletions src/mods/tools/ChainViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@
#include "ChainViewer.hpp"

std::optional<std::string> ChainViewer::on_initialize() {

// OK
return Mod::on_initialize();
}

std::optional<std::string> ChainViewer::on_initialize_d3d_thread() {
return initialize_d3d_resources();
}

std::optional<std::string> ChainViewer::initialize_d3d_resources() {
if (g_framework->is_dx12()) {
m_d3d12 = {};

DirectX::EffectPipelineStateDescription psd(
&DirectX::DX12::GeometricPrimitive::VertexType::InputLayout,
DirectX::DX12::CommonStates::AlphaBlend,
Expand All @@ -90,10 +95,10 @@ std::optional<std::string> ChainViewer::on_initialize_d3d_thread() {
spdlog::info("ChainViewer D3D11 initialized");
}

// OK
return Mod::on_initialize();
}
m_needs_d3d_init = false;

return std::nullopt;
}

void ChainViewer::on_config_load(const utility::Config& cfg) {
for (IModValue& option : m_options) {
Expand Down Expand Up @@ -125,6 +130,10 @@ void ChainViewer::on_draw_dev_ui() {
}

void ChainViewer::on_present() {
if (m_needs_d3d_init) {
initialize_d3d_resources();
}

if (g_framework->is_dx12()) {
if (m_effect_dirty) {
m_d3d12.effect->SetAlpha(m_effect_alpha);
Expand All @@ -136,7 +145,7 @@ void ChainViewer::on_present() {
}

void ChainViewer::on_frame() {
if (!m_enabled->value()) {
if (!m_enabled->value() || m_needs_d3d_init) {
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/mods/tools/ChainViewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ class ChainViewer : public Tool {
void on_config_load(const utility::Config& cfg) override;
void on_config_save(utility::Config& cfg) override;

void on_device_reset() override {
m_needs_d3d_init = true;
}

void on_draw_dev_ui() override;
void on_frame() override;
void on_present() override;

private:
std::optional<std::string> initialize_d3d_resources();

struct {
std::unique_ptr<DirectX::DX12::BasicEffect> effect{};
std::unique_ptr<DirectX::DX12::GeometricPrimitive> cylinder{};
Expand All @@ -37,6 +43,7 @@ class ChainViewer : public Tool {

float m_effect_alpha{0.5f};
bool m_effect_dirty{false};
bool m_needs_d3d_init{true};

private:
const ModToggle::Ptr m_enabled{ ModToggle::create(generate_name("Enabled")) };
Expand Down
62 changes: 47 additions & 15 deletions src/mods/tools/GameObjectsDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ struct Predicate : public REManagedObject {
}

std::optional<std::string> GameObjectsDisplay::on_initialize_d3d_thread() {
return initialize_d3d_resources();
}

std::optional<std::string> GameObjectsDisplay::on_initialize() {
// OK
return Mod::on_initialize();
}

std::optional<std::string> GameObjectsDisplay::initialize_d3d_resources() {
if (g_framework->is_dx12()) {
DirectX::EffectPipelineStateDescription psd(
m_d3d12 = {};

DirectX::EffectPipelineStateDescription bbuffer_psd(
&DirectX::DX12::GeometricPrimitive::VertexType::InputLayout,
DirectX::DX12::CommonStates::AlphaBlend,
DirectX::DX12::CommonStates::DepthNone,
Expand All @@ -42,24 +53,34 @@ std::optional<std::string> GameObjectsDisplay::on_initialize_d3d_thread() {

m_d3d12.states = std::make_unique<DirectX::DX12::CommonStates>(device);

m_d3d12.effect = std::make_unique<DirectX::DX12::BasicEffect>(device, DirectX::EffectFlags::Texture, psd);
m_d3d12.effect = std::make_unique<DirectX::DX12::BasicEffect>(device, DirectX::EffectFlags::Texture, bbuffer_psd);
m_d3d12.effect->SetWorld(DirectX::SimpleMath::Matrix::Identity);
m_d3d12.effect->SetView(DirectX::SimpleMath::Matrix::Identity);
m_d3d12.effect->SetProjection(DirectX::SimpleMath::Matrix::Identity);
m_d3d12.effect->SetAlpha(1.0f);
//m_d3d12.effect->SetDiffuseColor(DirectX::Colors::Black);

m_d3d12.effect_no_tex = std::make_unique<DirectX::DX12::BasicEffect>(device, DirectX::EffectFlags::None, bbuffer_psd);
m_d3d12.effect_no_tex->SetWorld(DirectX::SimpleMath::Matrix::Identity);
m_d3d12.effect_no_tex->SetView(DirectX::SimpleMath::Matrix::Identity);
m_d3d12.effect_no_tex->SetProjection(DirectX::SimpleMath::Matrix::Identity);
m_d3d12.effect_no_tex->SetDiffuseColor(DirectX::Colors::Blue);
m_d3d12.effect_no_tex->SetAlpha(0.5f);


m_d3d12.font_heap = std::make_unique<DirectX::DescriptorHeap>(device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, FontHeapIndices::FontHeap_Count);

// Upload sprite font
DirectX::ResourceUploadBatch upload(device);
upload.Begin();

m_d3d12.font_heap = std::make_unique<DirectX::DescriptorHeap>(device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 10);
m_d3d12.font = std::make_unique<DirectX::SpriteFont>(
device, upload, Roboto_spritefont, sizeof(Roboto_spritefont),
m_d3d12.font_heap->GetCpuHandle(FontHeapIndices::SpriteFont_Data),
m_d3d12.font_heap->GetGpuHandle(FontHeapIndices::SpriteFont_Data)
);

DirectX::RenderTargetState output_state{BackBufferRenderer::get()->get_default_rt_state()};
DirectX::RenderTargetState output_state{DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_UNKNOWN};
DirectX::SpriteBatchPipelineStateDescription pd{output_state};

m_d3d12.sprite_batch = std::make_unique<DirectX::DX12::SpriteBatch>(device, upload, pd);
Expand All @@ -71,7 +92,7 @@ std::optional<std::string> GameObjectsDisplay::on_initialize_d3d_thread() {

// Create text texture
d3d12::ComPtr<ID3D12Resource> text_texture{};
const auto tex_desc = CD3DX12_RESOURCE_DESC::Tex2D(output_state.rtvFormats[0], 512, 512, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
const auto tex_desc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, 512, 512, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
const auto heap_props = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
if (FAILED(device->CreateCommittedResource(&heap_props, D3D12_HEAP_FLAG_NONE, &tex_desc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, nullptr, IID_PPV_ARGS(&text_texture)))) {
return "Failed to create text texture";
Expand All @@ -88,16 +109,10 @@ std::optional<std::string> GameObjectsDisplay::on_initialize_d3d_thread() {
} else {
// TODO
}

m_needs_d3d_init = false;

// OK
return Mod::on_initialize();
}

std::optional<std::string> GameObjectsDisplay::on_initialize() {


// OK
return Mod::on_initialize();
return std::nullopt;
}

void GameObjectsDisplay::on_config_load(const utility::Config& cfg) {
Expand Down Expand Up @@ -125,10 +140,27 @@ void GameObjectsDisplay::on_draw_dev_ui() {
}

m_max_distance->draw("Max Distance for GameObjects");

if (ImGui::SliderFloat("Object Effect Alpha", &m_effect_alpha, 0.0f, 1.0f)) {
m_effect_dirty = true;
}
}

void GameObjectsDisplay::on_present() {
if (m_needs_d3d_init) {
if (auto result = initialize_d3d_resources(); result.has_value()) {
spdlog::error(result.value());
}
}

if (m_effect_dirty) {
m_d3d12.effect_no_tex->SetAlpha(m_effect_alpha);
m_effect_dirty = false;
}
}

void GameObjectsDisplay::on_frame() {
if (!m_enabled->value()) {
if (!m_enabled->value() || m_needs_d3d_init) {
return;
}

Expand Down
21 changes: 17 additions & 4 deletions src/mods/tools/GameObjectsDisplay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,42 @@ class GameObjectsDisplay : public Tool {
void on_config_load(const utility::Config& cfg) override;
void on_config_save(utility::Config& cfg) override;

void on_device_reset() override {
m_needs_d3d_init = true;
}
void on_draw_dev_ui() override;
void on_frame() override;
void on_present() override;

private:
std::optional<std::string> initialize_d3d_resources();

enum FontHeapIndices {
SpriteFont_Data,
SpriteFont_Data = 0,
FontHeap_Count
};

struct {
std::unique_ptr<d3d12::TextureContext> text_texture{};

std::unique_ptr<DirectX::DX12::BasicEffect> effect{};
std::unique_ptr<DirectX::DX12::BasicEffect> effect_no_tex{};
std::unique_ptr<DirectX::DX12::SpriteFont> font{};
std::unique_ptr<DirectX::DX12::GeometricPrimitive> quad{}; // For rendering the text in 3D space

std::unique_ptr<DirectX::DescriptorHeap> font_heap{};
std::unique_ptr<DirectX::DX12::SpriteBatch> sprite_batch{};
std::unique_ptr<DirectX::DX12::GeometricPrimitive> quad{}; // For rendering the text in 3D space
std::unique_ptr<DirectX::DX12::CommonStates> states{};

std::unique_ptr<d3d12::TextureContext> text_texture{};
} m_d3d12;

private:
const ModToggle::Ptr m_enabled{ ModToggle::create(generate_name("Enabled")) };
const ModSlider::Ptr m_max_distance{ ModSlider::create(generate_name("MaxDistance"), 0.0f, 1000.0f, 0.0f) };

float m_effect_alpha{ 0.5f };
bool m_effect_dirty{ false };
bool m_needs_d3d_init{ false };

ValueList m_options{
*m_enabled,
*m_max_distance
Expand Down

0 comments on commit 5619822

Please sign in to comment.