2021-06-02 17:20:15 +04:30
|
|
|
#include "dxBuffers.h"
|
|
|
|
#include "dxSharedContext.h"
|
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
//======================================== CONSTANT_BUFFER ========================================//
|
2021-07-26 11:43:37 +04:30
|
|
|
dxConstantBuffer::dxConstantBuffer(ConstantBufferIndex index, unsigned int size, Ref<dxSharedContext> sharedContext)
|
2021-07-29 17:12:13 +04:30
|
|
|
: m_Context(sharedContext),
|
|
|
|
m_Buffer(nullptr),
|
|
|
|
m_Map{},
|
|
|
|
m_Index(static_cast<int>(index))
|
2021-07-05 14:36:36 +04:30
|
|
|
{
|
2021-07-29 17:12:13 +04:30
|
|
|
D3D11_BUFFER_DESC bDesc = {};
|
2021-07-05 14:36:36 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
bDesc.ByteWidth = size;
|
|
|
|
bDesc.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
|
|
|
bDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
2021-07-05 14:36:36 +04:30
|
|
|
|
|
|
|
HRESULT hr;
|
2021-07-29 17:12:13 +04:30
|
|
|
DXC(m_Context->GetDevice()->CreateBuffer(&bDesc, nullptr, &m_Buffer));
|
2021-07-05 14:36:36 +04:30
|
|
|
m_Context->GetDeviceContext()->VSSetConstantBuffers(m_Index, 1u, m_Buffer.GetAddressOf());
|
|
|
|
}
|
|
|
|
|
|
|
|
void dxConstantBuffer::Bind()
|
|
|
|
{
|
|
|
|
m_Context->GetDeviceContext()->VSSetConstantBuffers(m_Index, 1u, m_Buffer.GetAddressOf());
|
|
|
|
}
|
|
|
|
|
|
|
|
void* dxConstantBuffer::Map()
|
|
|
|
{
|
|
|
|
m_Context->GetDeviceContext()->VSSetConstantBuffers(m_Index, 1u, m_Buffer.GetAddressOf());
|
|
|
|
m_Context->GetDeviceContext()->Map(m_Buffer.Get(), NULL, D3D11_MAP_WRITE_DISCARD, NULL, &m_Map);
|
|
|
|
return m_Map.pData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dxConstantBuffer::UnMap()
|
|
|
|
{
|
|
|
|
m_Context->GetDeviceContext()->Unmap(m_Buffer.Get(), NULL);
|
|
|
|
}
|
2021-07-29 17:12:13 +04:30
|
|
|
//======================================== CONSTANT_BUFFER ========================================//
|
2021-07-05 14:36:36 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
//================================================== VERTEX_BUFFER ==================================================//
|
2021-07-26 11:43:37 +04:30
|
|
|
dxVertexBuffer::dxVertexBuffer(float* vertices, unsigned int stride, unsigned int count, Ref<dxSharedContext> sharedContext)
|
2021-07-29 17:12:13 +04:30
|
|
|
: m_Context(sharedContext),
|
|
|
|
m_Buffer(nullptr),
|
|
|
|
m_Map{},
|
|
|
|
m_Stride(stride)
|
2021-06-02 17:20:15 +04:30
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
// buffer desc
|
2021-07-29 17:12:13 +04:30
|
|
|
D3D11_BUFFER_DESC bDesc = {};
|
2021-06-02 17:20:15 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
bDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
|
|
bDesc.Usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
bDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
2021-06-02 17:20:15 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
bDesc.ByteWidth = count * stride;
|
|
|
|
bDesc.StructureByteStride = stride;
|
2021-06-02 17:20:15 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create buffer
|
|
|
|
HRESULT hr;
|
2021-07-29 17:12:13 +04:30
|
|
|
DXC(m_Context->GetDevice()->CreateBuffer(&bDesc, nullptr, &m_Buffer));
|
2021-06-02 17:20:15 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
dxVertexBuffer::~dxVertexBuffer()
|
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
UnBind();
|
2021-06-02 17:20:15 +04:30
|
|
|
}
|
|
|
|
|
2021-06-13 19:35:48 +04:30
|
|
|
void* dxVertexBuffer::Map()
|
|
|
|
{
|
2021-07-01 19:25:46 +04:30
|
|
|
m_Context->GetDeviceContext()->Map(m_Buffer.Get(), NULL, D3D11_MAP_WRITE_DISCARD, NULL, &m_Map);
|
2021-06-13 19:35:48 +04:30
|
|
|
return m_Map.pData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dxVertexBuffer::UnMap()
|
|
|
|
{
|
2021-07-01 19:25:46 +04:30
|
|
|
m_Context->GetDeviceContext()->Unmap(m_Buffer.Get(), NULL);
|
2021-06-13 19:35:48 +04:30
|
|
|
}
|
|
|
|
|
2021-06-02 17:20:15 +04:30
|
|
|
void dxVertexBuffer::Bind()
|
|
|
|
{
|
|
|
|
static const unsigned int offset = 0u;
|
2021-07-01 19:25:46 +04:30
|
|
|
m_Context->GetDeviceContext()->IASetVertexBuffers(0u, 1u, m_Buffer.GetAddressOf(), &m_Stride, &offset);
|
2021-06-02 17:20:15 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
void dxVertexBuffer::UnBind()
|
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
static const unsigned int offset = 0u;
|
|
|
|
static ID3D11Buffer* buffer = nullptr;
|
|
|
|
|
2021-07-01 19:25:46 +04:30
|
|
|
m_Context->GetDeviceContext()->IASetVertexBuffers(0u, 1u, &buffer, &m_Stride, &offset);
|
2021-06-02 17:20:15 +04:30
|
|
|
}
|
2021-07-29 17:12:13 +04:30
|
|
|
//================================================== VERTEX_BUFFER ==================================================//
|
2021-06-02 17:20:15 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
//======================================== INDEX_BUFFER ========================================//
|
2021-07-26 11:43:37 +04:30
|
|
|
dxIndexBuffer::dxIndexBuffer(unsigned int* indices, unsigned int count, Ref<dxSharedContext> sharedContext)
|
2021-07-29 17:12:13 +04:30
|
|
|
: m_Context(sharedContext),
|
|
|
|
m_Buffer(nullptr)
|
2021-06-13 19:35:48 +04:30
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
// generate indices if not provided
|
2021-06-14 17:02:56 +04:30
|
|
|
bool hasIndices = !!indices;
|
|
|
|
if (!hasIndices)
|
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
// check
|
2021-06-14 17:02:56 +04:30
|
|
|
if (count % 6 != 0)
|
|
|
|
{
|
2021-07-01 19:25:46 +04:30
|
|
|
LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: 'indices' can only be null if count is multiple of 6");
|
|
|
|
LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: adding {} to 'count' -> {}", (6 - (count % 6)), count + (6 - (count % 6)));
|
2021-06-14 17:02:56 +04:30
|
|
|
count = count + (6 - (count % 6));
|
|
|
|
}
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create indices
|
2021-06-14 17:02:56 +04:30
|
|
|
indices = new unsigned int[count];
|
|
|
|
unsigned int offset = 0;
|
|
|
|
for (unsigned int i = 0; i < count; i += 6)
|
|
|
|
{
|
2021-07-29 17:12:13 +04:30
|
|
|
indices[i + 0] = offset + 0u;
|
|
|
|
indices[i + 1] = offset + 3u;
|
|
|
|
indices[i + 2] = offset + 2u;
|
2021-06-14 17:02:56 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
indices[i + 3] = offset + 2u;
|
|
|
|
indices[i + 4] = offset + 1u;
|
|
|
|
indices[i + 5] = offset + 0u;
|
2021-06-14 17:02:56 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
offset += 4u;
|
2021-06-14 17:02:56 +04:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// buffer desc
|
2021-07-29 17:12:13 +04:30
|
|
|
D3D11_BUFFER_DESC bDesc = {};
|
|
|
|
bDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
|
|
bDesc.Usage = D3D11_USAGE_DEFAULT;
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2021-07-29 17:12:13 +04:30
|
|
|
bDesc.ByteWidth = count * sizeof(unsigned int);
|
|
|
|
bDesc.StructureByteStride = sizeof(unsigned int);
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// subresource data
|
2021-07-29 17:12:13 +04:30
|
|
|
D3D11_SUBRESOURCE_DATA sDesc = {};
|
|
|
|
sDesc.pSysMem = indices;
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create buffer
|
|
|
|
HRESULT hr;
|
2021-07-29 17:12:13 +04:30
|
|
|
DXC(m_Context->GetDevice()->CreateBuffer(&bDesc, &sDesc, &m_Buffer));
|
2021-06-14 17:02:56 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// delete indices
|
2021-06-14 17:02:56 +04:30
|
|
|
if (!hasIndices)
|
|
|
|
delete[] indices;
|
2021-06-13 19:35:48 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
dxIndexBuffer::~dxIndexBuffer()
|
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
UnBind();
|
2021-06-13 19:35:48 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
void dxIndexBuffer::Bind()
|
|
|
|
{
|
2021-07-01 19:25:46 +04:30
|
|
|
m_Context->GetDeviceContext()->IASetIndexBuffer(m_Buffer.Get(), DXGI_FORMAT_R32_UINT, 0u);
|
2021-06-13 19:35:48 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
void dxIndexBuffer::UnBind()
|
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
static const unsigned int offset = 0u;
|
|
|
|
static ID3D11Buffer* buffer = nullptr;
|
|
|
|
|
2021-07-01 19:25:46 +04:30
|
|
|
m_Context->GetDeviceContext()->IASetIndexBuffer(buffer, DXGI_FORMAT_R32_UINT, offset);
|
2021-06-13 19:35:48 +04:30
|
|
|
}
|
2021-07-29 17:12:13 +04:30
|
|
|
//======================================== INDEX_BUFFER ========================================//
|
2021-06-13 19:35:48 +04:30
|
|
|
|
2021-06-02 17:20:15 +04:30
|
|
|
}
|