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

106 lines
2.7 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
void dxRenderCommand::swap_buffers()
2022-03-07 21:57:00 +03:30
{
#ifdef LIGHT_DEBUG
2022-03-07 21:57:00 +03:30
HRESULT hr;
if (FAILED(hr = m_context->get_swap_chain()->Present(1u, 0u)))
2022-03-07 21:57:00 +03:30
{
if (hr == DXGI_ERROR_DEVICE_REMOVED)
{
2025-07-06 16:30:38 +03:30
log_crt("dxRenderCommand::swap_buffers: DeviceRemoved:");
log_crt(" {}", m_context->get_device()->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->get_swap_chain()->Present(0u, 0u);
#endif
2022-03-07 21:57:00 +03:30
}
2021-06-02 09:07:45 +04:30
void dxRenderCommand::clear_back_buffer(const glm::vec4 &clearColor)
2022-03-07 21:57:00 +03:30
{
m_context->get_device_context()->ClearRenderTargetView(
m_context->get_render_target_view().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
void dxRenderCommand::draw(unsigned int count)
2022-03-07 21:57:00 +03:30
{
m_context->get_device_context()->draw(count, 0u);
2022-03-07 21:57:00 +03:30
}
2021-06-02 09:07:45 +04:30
void dxRenderCommand::draw_indexed(unsigned int count)
2022-03-07 21:57:00 +03:30
{
m_context->get_device_context()->draw_indexed(count, 0u, 0u);
2022-03-07 21:57:00 +03:30
}
2021-06-02 09:07:45 +04:30
void dxRenderCommand::default_target_framebuffer()
2022-03-07 21:57:00 +03:30
{
m_context->get_device_context()
->OMSetRenderTargets(1, m_context->get_render_target_view().GetAddressOf(), nullptr);
2022-03-07 21:57:00 +03:30
}
void dxRenderCommand::set_viewport(
2025-07-05 13:28:41 +03:30
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??
set_resolution(width, height);
2022-03-07 21:57:00 +03:30
// create viewport
2025-07-06 14:02:50 +03:30
auto viewport = D3D11_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->get_device_context()->RSSetViewports(1u, &viewport);
2022-03-07 21:57:00 +03:30
}
void dxRenderCommand::set_resolution(unsigned int width, unsigned int height)
2022-03-07 21:57:00 +03:30
{
2025-07-06 14:02:50 +03:30
auto hr = HRESULT {};
2022-03-07 21:57:00 +03:30
// remove render target
2025-07-06 14:02:50 +03:30
auto *nullViews[] = (ID3D11RenderTargetView *) { nullptr };
m_context->get_device_context()->OMSetRenderTargets(1u, nullViews, nullptr);
m_context->GetRenderTargetViewRef().reset();
2022-03-07 21:57:00 +03:30
// resize buffer
dxc(m_context->get_swap_chain()
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
2025-07-06 14:02:50 +03:30
auto backBuffer = Microsoft::WRL::ComPtr<ID3D11Resource> { nullptr };
dxc(m_context->get_swap_chain()->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer));
dxc(m_context->get_device()->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->get_device_context()
->OMSetRenderTargets(1u, m_context->get_render_target_view().GetAddressOf(), nullptr);
2022-03-07 21:57:00 +03:30
}
2022-03-07 21:57:00 +03:30
} // namespace Light