2025-07-10 13:29:03 +03:30
|
|
|
#include <renderer/dx/buffers.hpp>
|
|
|
|
#include <renderer/dx/shared_context.hpp>
|
2021-06-02 17:20:15 +04:30
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
//======================================== CONSTANT_BUFFER
|
|
|
|
//========================================//
|
|
|
|
dxConstantBuffer::dxConstantBuffer(
|
|
|
|
ConstantBufferIndex index,
|
|
|
|
unsigned int size,
|
|
|
|
Ref<dxSharedContext> sharedContext
|
|
|
|
)
|
2025-07-05 14:23:01 +03:30
|
|
|
: m_context(sharedContext)
|
|
|
|
, m_buffer(nullptr)
|
|
|
|
, m_map {}
|
|
|
|
, m_index(static_cast<int>(index))
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-06 14:02:50 +03:30
|
|
|
auto bDesc = D3D11_BUFFER_DESC {};
|
2022-03-07 21:57:00 +03:30
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
bDesc.ByteWidth = size;
|
|
|
|
bDesc.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
2022-03-07 21:57:00 +03:30
|
|
|
bDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
|
|
|
2025-07-06 14:02:50 +03:30
|
|
|
auto hr = HRESULT {};
|
2025-07-05 15:36:53 +03:30
|
|
|
dxc(m_context->get_device()->CreateBuffer(&bDesc, nullptr, &m_buffer));
|
|
|
|
m_context->get_device_context()->VSSetConstantBuffers(m_index, 1u, m_buffer.GetAddressOf());
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxConstantBuffer::bind()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->VSSetConstantBuffers(m_index, 1u, m_buffer.GetAddressOf());
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-06 14:02:50 +03:30
|
|
|
auto dxConstantBuffer::map() -> void *
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->VSSetConstantBuffers(m_index, 1u, m_buffer.GetAddressOf());
|
2025-07-06 14:02:50 +03:30
|
|
|
m_context->get_device_context()
|
|
|
|
->map(m_buffer.Get(), NULL, D3D11_MAP_WRITE_DISCARD, NULL, &m_map);
|
2025-07-05 14:23:01 +03:30
|
|
|
return m_map.pData;
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxConstantBuffer::un_map()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->Unmap(m_buffer.Get(), NULL);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
dxVertexBuffer::dxVertexBuffer(
|
|
|
|
float *vertices,
|
|
|
|
unsigned int stride,
|
|
|
|
unsigned int count,
|
|
|
|
Ref<dxSharedContext> sharedContext
|
|
|
|
)
|
2025-07-05 14:23:01 +03:30
|
|
|
: m_context(sharedContext)
|
|
|
|
, m_buffer(nullptr)
|
|
|
|
, m_map {}
|
|
|
|
, m_stride(stride)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
// buffer desc
|
2025-07-06 14:02:50 +03:30
|
|
|
auto bDesc = D3D11_BUFFER_DESC {};
|
2022-03-07 21:57:00 +03:30
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
bDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
|
|
bDesc.Usage = D3D11_USAGE_DYNAMIC;
|
2022-03-07 21:57:00 +03:30
|
|
|
bDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
bDesc.ByteWidth = count * stride;
|
2022-03-07 21:57:00 +03:30
|
|
|
bDesc.StructureByteStride = stride;
|
|
|
|
|
|
|
|
// create buffer
|
2025-07-06 14:02:50 +03:30
|
|
|
auto hr = HRESULT {};
|
2025-07-05 15:36:53 +03:30
|
|
|
dxc(m_context->get_device()->CreateBuffer(&bDesc, nullptr, &m_buffer));
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
dxVertexBuffer::~dxVertexBuffer()
|
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
un_bind();
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-06 14:02:50 +03:30
|
|
|
auto dxVertexBuffer::map() -> void *
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-06 14:02:50 +03:30
|
|
|
m_context->get_device_context()
|
|
|
|
->map(m_buffer.Get(), NULL, D3D11_MAP_WRITE_DISCARD, NULL, &m_map);
|
2025-07-05 14:23:01 +03:30
|
|
|
return m_map.pData;
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxVertexBuffer::un_map()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->Unmap(m_buffer.Get(), NULL);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxVertexBuffer::bind()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-06 14:02:50 +03:30
|
|
|
static const auto offset = unsigned int { 0u };
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()
|
2025-07-05 14:23:01 +03:30
|
|
|
->IASetVertexBuffers(0u, 1u, m_buffer.GetAddressOf(), &m_stride, &offset);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxVertexBuffer::un_bind()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-06 14:02:50 +03:30
|
|
|
static const auto offset = unsigned int { 0u };
|
|
|
|
static auto *buffer = (ID3D11Buffer *)(nullptr);
|
2022-03-07 21:57:00 +03:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->IASetVertexBuffers(0u, 1u, &buffer, &m_stride, &offset);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
dxIndexBuffer::dxIndexBuffer(
|
|
|
|
unsigned int *indices,
|
|
|
|
unsigned int count,
|
|
|
|
Ref<dxSharedContext> sharedContext
|
|
|
|
)
|
2025-07-05 14:23:01 +03:30
|
|
|
: m_context(sharedContext)
|
|
|
|
, m_buffer(nullptr)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
// generate indices if not provided
|
2025-07-06 14:02:50 +03:30
|
|
|
auto hasIndices = !!indices;
|
2022-03-07 21:57:00 +03:30
|
|
|
if (!hasIndices)
|
2021-06-02 17:20:15 +04:30
|
|
|
{
|
2022-03-07 21:57:00 +03:30
|
|
|
// check
|
|
|
|
if (count % 6 != 0)
|
|
|
|
{
|
2025-07-06 16:30:38 +03:30
|
|
|
log_wrn("'indices' can only be null if count is multiple of 6");
|
2025-07-06 14:02:50 +03:30
|
|
|
lt_log(
|
|
|
|
warn,
|
|
|
|
"Adding {} to 'count' -> {}",
|
|
|
|
(6 - (count % 6)),
|
|
|
|
count + (6 - (count % 6))
|
|
|
|
);
|
2022-03-07 21:57:00 +03:30
|
|
|
count = count + (6 - (count % 6));
|
|
|
|
}
|
2021-06-02 17:20:15 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// create indices
|
2025-07-05 13:28:41 +03:30
|
|
|
indices = new unsigned int[count];
|
2025-07-06 14:02:50 +03:30
|
|
|
auto offset = 0;
|
2022-03-07 21:57:00 +03:30
|
|
|
for (unsigned int i = 0; i < count; i += 6)
|
|
|
|
{
|
|
|
|
indices[i + 0] = offset + 0u;
|
|
|
|
indices[i + 1] = offset + 3u;
|
|
|
|
indices[i + 2] = offset + 2u;
|
2021-06-19 15:12:42 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
indices[i + 3] = offset + 2u;
|
|
|
|
indices[i + 4] = offset + 1u;
|
|
|
|
indices[i + 5] = offset + 0u;
|
2021-06-02 17:20:15 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
offset += 4u;
|
2021-06-14 17:02:56 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-06-14 17:02:56 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// buffer desc
|
2025-07-06 14:02:50 +03:30
|
|
|
auto bDesc = D3D11_BUFFER_DESC {};
|
2025-07-05 13:28:41 +03:30
|
|
|
bDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
|
|
bDesc.Usage = D3D11_USAGE_DEFAULT;
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
bDesc.ByteWidth = count * sizeof(unsigned int);
|
2022-03-07 21:57:00 +03:30
|
|
|
bDesc.StructureByteStride = sizeof(unsigned int);
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// subresource data
|
2025-07-06 14:02:50 +03:30
|
|
|
auto sDesc = D3D11_SUBRESOURCE_DATA {};
|
2025-07-05 13:28:41 +03:30
|
|
|
sDesc.pSysMem = indices;
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// create buffer
|
2025-07-06 14:02:50 +03:30
|
|
|
auto hr = HRESULT {};
|
2025-07-05 15:36:53 +03:30
|
|
|
dxc(m_context->get_device()->CreateBuffer(&bDesc, &sDesc, &m_buffer));
|
2021-06-14 17:02:56 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// delete indices
|
|
|
|
if (!hasIndices)
|
|
|
|
delete[] indices;
|
|
|
|
}
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
dxIndexBuffer::~dxIndexBuffer()
|
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
un_bind();
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxIndexBuffer::bind()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->IASetIndexBuffer(m_buffer.Get(), DXGI_FORMAT_R32_UINT, 0u);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxIndexBuffer::un_bind()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-06 14:02:50 +03:30
|
|
|
static const auto offset = (unsigned int) { 0u };
|
|
|
|
static auto *buffer = (ID3D11Buffer *)(nullptr);
|
2021-06-19 15:12:42 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->IASetIndexBuffer(buffer, DXGI_FORMAT_R32_UINT, offset);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
//======================================== INDEX_BUFFER ========================================//
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
} // namespace Light
|