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 {
|
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
dxRenderCommand::dxRenderCommand(Ref<dxSharedContext> sharedContext): m_context(sharedContext)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
}
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxRenderCommand::swap_buffers()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2021-06-21 17:04:51 +04:30
|
|
|
#ifdef LIGHT_DEBUG
|
2022-03-07 21:57:00 +03:30
|
|
|
HRESULT hr;
|
2025-07-05 15:36:53 +03:30
|
|
|
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)
|
2021-06-21 17:04:51 +04:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_log(critical, "dxRenderCommand::swap_buffers: DeviceRemoved:");
|
|
|
|
lt_log(critical, " {}", m_context->get_device()->GetDeviceRemovedReason());
|
2022-03-07 21:57:00 +03:30
|
|
|
throw dxException(hr, __FILE__, __LINE__);
|
2021-06-21 17:04:51 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-06-21 17:04:51 +04:30
|
|
|
#else
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_swap_chain()->Present(0u, 0u);
|
2021-06-21 17:04:51 +04:30
|
|
|
#endif
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxRenderCommand::clear_back_buffer(const glm::vec4 &clearColor)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +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
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxRenderCommand::draw(unsigned int count)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +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
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxRenderCommand::draw_indexed(unsigned int count)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +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
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxRenderCommand::default_target_framebuffer()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +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
|
|
|
}
|
2021-07-15 15:46:28 +04:30
|
|
|
|
2025-07-05 15:36:53 +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??
|
2025-07-05 15:36:53 +03:30
|
|
|
set_resolution(width, height);
|
2021-07-22 13:00:41 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// create viewport
|
|
|
|
D3D11_VIEWPORT viewport;
|
2021-07-14 00:17:30 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
viewport.TopLeftX = x;
|
|
|
|
viewport.TopLeftY = y;
|
2021-07-14 00:17:30 +04:30
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
viewport.Width = width;
|
2022-03-07 21:57:00 +03:30
|
|
|
viewport.Height = height;
|
2021-07-14 00:17:30 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
viewport.MinDepth = 0.0f;
|
|
|
|
viewport.MaxDepth = 1.0f;
|
2021-07-14 00:17:30 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// set viewport
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->RSSetViewports(1u, &viewport);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-14 00:17:30 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxRenderCommand::set_resolution(unsigned int width, unsigned int height)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
HRESULT hr;
|
2021-07-22 13:00:41 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// remove render target
|
2025-07-05 13:28:41 +03:30
|
|
|
ID3D11RenderTargetView *nullViews[] = { nullptr };
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->OMSetRenderTargets(1u, nullViews, nullptr);
|
|
|
|
m_context->GetRenderTargetViewRef().reset();
|
2021-07-22 13:00:41 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// resize buffer
|
2025-07-05 15:36:53 +03:30
|
|
|
dxc(m_context->get_swap_chain()
|
2025-07-05 13:28:41 +03:30
|
|
|
->ResizeBuffers(0u, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, NULL));
|
2021-07-22 13:00:41 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// create render target
|
|
|
|
Microsoft::WRL::ComPtr<ID3D11Resource> backBuffer = nullptr;
|
2025-07-05 15:36:53 +03:30
|
|
|
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,
|
2025-07-05 14:23:01 +03:30
|
|
|
&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
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()
|
|
|
|
->OMSetRenderTargets(1u, m_context->get_render_target_view().GetAddressOf(), nullptr);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-22 13:00:41 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
} // namespace Light
|