Skip to content

Commit

Permalink
BackBufferRenderer: Pass additional info to predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Nov 11, 2024
1 parent c92548f commit cad903b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
17 changes: 11 additions & 6 deletions src/mods/BackBufferRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ void BackBufferRenderer::render_d3d12() {
scissor_rect.right = (LONG)desc.Width;
scissor_rect.bottom = (LONG)desc.Height;

D3D12_VIEWPORT viewport{};
viewport.Width = (float)desc.Width;
viewport.Height = (float)desc.Height;
viewport.MaxDepth = 1.0f;
m_d3d12.viewport.Width = (float)desc.Width;
m_d3d12.viewport.Height = (float)desc.Height;
m_d3d12.viewport.MaxDepth = 1.0f;

D3D12_RESOURCE_BARRIER barrier{};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
Expand All @@ -116,7 +115,7 @@ void BackBufferRenderer::render_d3d12() {
D3D12_CPU_DESCRIPTOR_HANDLE rtv_heaps[] = { bb_context->get_rtv() };
cmd_list->OMSetRenderTargets(1, rtv_heaps, FALSE, nullptr);

cmd_list->RSSetViewports(1, &viewport);
cmd_list->RSSetViewports(1, &m_d3d12.viewport);
cmd_list->RSSetScissorRects(1, &scissor_rect);

decltype(m_d3d12.render_work) works{};
Expand All @@ -126,8 +125,14 @@ void BackBufferRenderer::render_d3d12() {
m_d3d12.render_work.clear();
}

const RenderWorkData data{
cmd_list.Get(),
m_d3d12.viewport,
bb_context.get()
};

for (auto& work : works) {
work(cmd_list.Get());
work(data);
}

barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
Expand Down
15 changes: 13 additions & 2 deletions src/mods/BackBufferRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ class BackBufferRenderer : public Mod {
void on_device_reset() override;

public:
using D3D12RenderWorkFn = std::function<void(ID3D12GraphicsCommandList*)>;

struct RenderWorkData {
ID3D12GraphicsCommandList* command_list;
D3D12_VIEWPORT viewport;
d3d12::TextureContext* backbuffer_ctx;
};
using D3D12RenderWorkFn = std::function<void(const RenderWorkData&)>;

void submit_work_d3d12(D3D12RenderWorkFn&& work) {
std::scoped_lock _{ m_d3d12.render_work_mtx };
m_d3d12.render_work.push_back(std::move(work));
Expand All @@ -52,6 +57,10 @@ class BackBufferRenderer : public Mod {
return m_d3d12.default_rt_state;
}

D3D12_VIEWPORT get_viewport_d3d12() {
return m_d3d12.viewport;
}

private:
void render_d3d12();
void render_d3d11();
Expand All @@ -63,5 +72,7 @@ class BackBufferRenderer : public Mod {
std::mutex render_work_mtx{};

DirectX::RenderTargetState default_rt_state{};

D3D12_VIEWPORT viewport{};
} m_d3d12;
};
38 changes: 21 additions & 17 deletions src/mods/tools/ChainViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void ChainViewer::on_frame() {
std::vector<BackBufferRenderer::D3D12RenderWorkFn> d3d12_work{};

// Set the view and projection matrices for the effect once per frame
d3d12_work.emplace_back([this, proj_directx, view_directx](ID3D12GraphicsCommandList* cmd_list) {
d3d12_work.emplace_back([this, proj_directx, view_directx](const BackBufferRenderer::RenderWorkData& data) {
m_d3d12.effect->SetProjection(proj_directx);
m_d3d12.effect->SetView(view_directx);
});
Expand Down Expand Up @@ -323,12 +323,14 @@ void ChainViewer::on_frame() {
mat[3] = Vector4f{adjusted_pos1, 1.0f};

if (g_framework->is_dx12()) {
d3d12_work.emplace_back([this, adjusted_pos1, radius = collider.sphere.r](ID3D12GraphicsCommandList* cmd_list){
DirectX::SimpleMath::Matrix world = DirectX::SimpleMath::Matrix::CreateScale(radius) * DirectX::SimpleMath::Matrix::CreateTranslation(adjusted_pos1.x, adjusted_pos1.y, adjusted_pos1.z);
const auto radius = collider.sphere.r;
DirectX::SimpleMath::Matrix world = DirectX::SimpleMath::Matrix::CreateScale(radius) * DirectX::SimpleMath::Matrix::CreateTranslation(adjusted_pos1.x, adjusted_pos1.y, adjusted_pos1.z);

d3d12_work.emplace_back([this, world](const BackBufferRenderer::RenderWorkData& data){
m_d3d12.effect->SetWorld(world);

m_d3d12.effect->Apply(cmd_list);
m_d3d12.sphere->Draw(cmd_list);
m_d3d12.effect->Apply(data.command_list);
m_d3d12.sphere->Draw(data.command_list);
});
} else {
// TODO
Expand All @@ -353,20 +355,22 @@ void ChainViewer::on_frame() {
} else {
// Capsule
if (g_framework->is_dx12()) {
d3d12_work.emplace_back([this, adjusted_pos1, adjusted_pos2, radius = collider.capsule.r](ID3D12GraphicsCommandList* cmd_list){
const auto delta = adjusted_pos2 - adjusted_pos1;
const auto dir = glm::normalize(delta);
const auto length = glm::length(delta) + (radius * 2.0f);
const auto center = (adjusted_pos1 + adjusted_pos2) * 0.5f;
DirectX::SimpleMath::Matrix world =
DirectX::SimpleMath::Matrix::CreateScale(radius * 2.0f, radius * 2.0f, length) *
DirectX::SimpleMath::Matrix::CreateLookAt(DirectX::SimpleMath::Vector3::Zero, DirectX::SimpleMath::Vector3(dir.x, dir.y, dir.z), DirectX::SimpleMath::Vector3::Up).Invert() *
DirectX::SimpleMath::Matrix::CreateTranslation(center.x, center.y, center.z);

const auto radius = collider.capsule.r;
const auto delta = adjusted_pos2 - adjusted_pos1;
const auto dir = glm::normalize(delta);
const auto length = glm::length(delta) + (radius * 2.0f);
const auto center = (adjusted_pos1 + adjusted_pos2) * 0.5f;
DirectX::SimpleMath::Matrix world =
DirectX::SimpleMath::Matrix::CreateScale(radius * 2.0f, radius * 2.0f, length) *
DirectX::SimpleMath::Matrix::CreateLookAt(DirectX::SimpleMath::Vector3::Zero, DirectX::SimpleMath::Vector3(dir.x, dir.y, dir.z), DirectX::SimpleMath::Vector3::Up).Invert() *
DirectX::SimpleMath::Matrix::CreateTranslation(center.x, center.y, center.z);


d3d12_work.emplace_back([this, world](const BackBufferRenderer::RenderWorkData& data){
m_d3d12.effect->SetWorld(world);

m_d3d12.effect->Apply(cmd_list);
m_d3d12.sphere->Draw(cmd_list);
m_d3d12.effect->Apply(data.command_list);
m_d3d12.sphere->Draw(data.command_list);
});
} else {
// TODO
Expand Down

0 comments on commit cad903b

Please sign in to comment.