light/modules/engine/src/platform/graphics/directx/render_command.cpp

106 lines
2.6 KiB
C++
Raw Normal View History

2025-07-05 13:28:41 +03:30
#include <engine/platform/graphics/directx/render_command.hpp>
#include <engine/platform/graphics/directx/shared_context.hpp>
2021-06-02 09:07:45 +04:30
namespace Light {
dxRenderCommand::dxRenderCommand(Ref<dxSharedContext> sharedContext): m_context(sharedContext)
2022-03-07 21:57:00 +03:30
{
}
2021-06-02 09:07:45 +04:30
2022-03-07 21:57:00 +03:30
void dxRenderCommand::SwapBuffers()
{
#ifdef LIGHT_DEBUG
2022-03-07 21:57:00 +03:30
HRESULT hr;
if (FAILED(hr = m_context->GetSwapChain()->Present(1u, 0u)))
2022-03-07 21:57:00 +03:30
{
if (hr == DXGI_ERROR_DEVICE_REMOVED)
{
2022-03-07 21:57:00 +03:30
LOG(critical, "dxRenderCommand::SwapBuffers: DeviceRemoved:");
LOG(critical, " {}", m_context->GetDevice()->GetDeviceRemovedReason());
2022-03-07 21:57:00 +03:30
throw dxException(hr, __FILE__, __LINE__);
}
2022-03-07 21:57:00 +03:30
}
#else
m_context->GetSwapChain()->Present(0u, 0u);
#endif
2022-03-07 21:57:00 +03:30
}
2021-06-02 09:07:45 +04:30
2025-07-05 13:28:41 +03:30
void dxRenderCommand::ClearBackBuffer(const glm::vec4 &clearColor)
2022-03-07 21:57:00 +03:30
{
m_context->GetDeviceContext()->ClearRenderTargetView(
m_context->GetRenderTargetView().Get(),
2025-07-05 13:28:41 +03:30
&clearColor[0]
);
2022-03-07 21:57:00 +03:30
}
2021-06-02 09:07:45 +04:30
2022-03-07 21:57:00 +03:30
void dxRenderCommand::Draw(unsigned int count)
{
m_context->GetDeviceContext()->Draw(count, 0u);
2022-03-07 21:57:00 +03:30
}
2021-06-02 09:07:45 +04:30
2022-03-07 21:57:00 +03:30
void dxRenderCommand::DrawIndexed(unsigned int count)
{
m_context->GetDeviceContext()->DrawIndexed(count, 0u, 0u);
2022-03-07 21:57:00 +03:30
}
2021-06-02 09:07:45 +04:30
2022-03-07 21:57:00 +03:30
void dxRenderCommand::DefaultTargetFramebuffer()
{
m_context->GetDeviceContext()
->OMSetRenderTargets(1, m_context->GetRenderTargetView().GetAddressOf(), nullptr);
2022-03-07 21:57:00 +03:30
}
2025-07-05 13:28:41 +03:30
void dxRenderCommand::SetViewport(
unsigned int x,
unsigned int y,
unsigned int width,
unsigned int height
)
2022-03-07 21:57:00 +03:30
{
// #todo: maybe call this somewhere else??
SetResolution(width, height);
2022-03-07 21:57:00 +03:30
// create viewport
D3D11_VIEWPORT viewport;
2022-03-07 21:57:00 +03:30
viewport.TopLeftX = x;
viewport.TopLeftY = y;
2025-07-05 13:28:41 +03:30
viewport.Width = width;
2022-03-07 21:57:00 +03:30
viewport.Height = height;
2022-03-07 21:57:00 +03:30
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
2022-03-07 21:57:00 +03:30
// set viewport
m_context->GetDeviceContext()->RSSetViewports(1u, &viewport);
2022-03-07 21:57:00 +03:30
}
2022-03-07 21:57:00 +03:30
void dxRenderCommand::SetResolution(unsigned int width, unsigned int height)
{
HRESULT hr;
2022-03-07 21:57:00 +03:30
// remove render target
2025-07-05 13:28:41 +03:30
ID3D11RenderTargetView *nullViews[] = { nullptr };
m_context->GetDeviceContext()->OMSetRenderTargets(1u, nullViews, nullptr);
m_context->GetRenderTargetViewRef().Reset();
2022-03-07 21:57:00 +03:30
// resize buffer
DXC(m_context->GetSwapChain()
2025-07-05 13:28:41 +03:30
->ResizeBuffers(0u, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, NULL));
2022-03-07 21:57:00 +03:30
// create render target
Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer = nullptr;
DXC(m_context->GetSwapChain()->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer));
DXC(m_context->GetDevice()->CreateRenderTargetView(
2025-07-05 13:28:41 +03:30
backBuffer.Get(),
nullptr,
&m_context->GetRenderTargetViewRef()
2025-07-05 13:28:41 +03:30
));
2021-07-29 17:12:13 +04:30
2022-03-07 21:57:00 +03:30
// set render target
m_context->GetDeviceContext()
->OMSetRenderTargets(1u, m_context->GetRenderTargetView().GetAddressOf(), nullptr);
2022-03-07 21:57:00 +03:30
}
2022-03-07 21:57:00 +03:30
} // namespace Light