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 13:28:41 +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
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
void dxRenderCommand::SwapBuffers()
|
|
|
|
{
|
2021-06-21 17:04:51 +04:30
|
|
|
#ifdef LIGHT_DEBUG
|
2022-03-07 21:57:00 +03:30
|
|
|
HRESULT hr;
|
|
|
|
if (FAILED(hr = m_Context->GetSwapChain()->Present(1u, 0u)))
|
|
|
|
{
|
|
|
|
if (hr == DXGI_ERROR_DEVICE_REMOVED)
|
2021-06-21 17:04:51 +04:30
|
|
|
{
|
2022-03-07 21:57:00 +03:30
|
|
|
LOG(critical, "dxRenderCommand::SwapBuffers: DeviceRemoved:");
|
|
|
|
LOG(critical, " {}", m_Context->GetDevice()->GetDeviceRemovedReason());
|
|
|
|
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
|
2022-03-07 21:57:00 +03:30
|
|
|
m_Context->GetSwapChain()->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 13:28:41 +03:30
|
|
|
void dxRenderCommand::ClearBackBuffer(const glm::vec4 &clearColor)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
m_Context->GetDeviceContext()->ClearRenderTargetView(
|
|
|
|
m_Context->GetRenderTargetView().Get(),
|
|
|
|
&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);
|
|
|
|
}
|
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);
|
|
|
|
}
|
2021-06-02 09:07:45 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
void dxRenderCommand::DefaultTargetFramebuffer()
|
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
m_Context->GetDeviceContext()
|
|
|
|
->OMSetRenderTargets(1, m_Context->GetRenderTargetView().GetAddressOf(), nullptr);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-15 15:46:28 +04: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);
|
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
|
|
|
|
m_Context->GetDeviceContext()->RSSetViewports(1u, &viewport);
|
|
|
|
}
|
2021-07-14 00:17:30 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
void dxRenderCommand::SetResolution(unsigned int width, unsigned int height)
|
|
|
|
{
|
|
|
|
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 };
|
2022-03-07 21:57:00 +03:30
|
|
|
m_Context->GetDeviceContext()->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 13:28:41 +03:30
|
|
|
DXC(m_Context->GetSwapChain()
|
|
|
|
->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;
|
|
|
|
DXC(m_Context->GetSwapChain()->GetBuffer(0u, __uuidof(ID3D11Resource), &backBuffer));
|
2025-07-05 13:28:41 +03:30
|
|
|
DXC(m_Context->GetDevice()->CreateRenderTargetView(
|
|
|
|
backBuffer.Get(),
|
|
|
|
nullptr,
|
|
|
|
&m_Context->GetRenderTargetViewRef()
|
|
|
|
));
|
2021-07-29 17:12:13 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// set render target
|
2025-07-05 13:28:41 +03:30
|
|
|
m_Context->GetDeviceContext()
|
|
|
|
->OMSetRenderTargets(1u, m_Context->GetRenderTargetView().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
|