1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00
xn65/framework/platform/buffer.cpp

161 lines
3.8 KiB
C++
Raw Normal View History

2024-05-20 09:09:08 -03:00
#include "graphics/buffer.hpp"
#include "common/numerics.hpp"
#include "platform-dx/dxheaders.hpp"
#include "platform-dx/implementations.hpp"
namespace xna {
2024-05-20 09:32:34 -03:00
ConstantBuffer::ConstantBuffer() : GraphicsResource(nullptr){
2024-05-21 14:20:37 -03:00
impl = uNew<PlatformImplementation>();
2024-05-20 09:32:34 -03:00
}
ConstantBuffer::ConstantBuffer(sptr<GraphicsDevice> const& device) : GraphicsResource(device){
impl = uNew<PlatformImplementation>();
}
2024-05-20 09:09:08 -03:00
ConstantBuffer::~ConstantBuffer() {
impl = nullptr;
}
bool ConstantBuffer::Initialize(xna_error_ptr_arg)
{
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_device) {
2024-05-20 09:09:08 -03:00
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
if (impl->_buffer) {
impl->_buffer->Release();
impl->_buffer = nullptr;
}
2024-05-24 22:26:10 -03:00
const auto hr = m_device->impl->_device->CreateBuffer(
2024-05-20 09:09:08 -03:00
&impl->_description,
&impl->_subResource,
&impl->_buffer);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
2024-05-20 09:32:34 -03:00
DataBuffer::DataBuffer() : GraphicsResource(nullptr) {
impl = uNew<PlatformImplementation>();
}
DataBuffer::DataBuffer(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = uNew<PlatformImplementation>();
}
DataBuffer::~DataBuffer() {
impl = nullptr;
}
bool DataBuffer::Initialize(xna_error_ptr_arg) {
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_device) {
2024-05-20 09:32:34 -03:00
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
if (impl->_blob) {
impl->_blob->Release();
impl->_blob = nullptr;
}
return true;
}
2024-05-21 14:20:37 -03:00
IndexBuffer::IndexBuffer() : GraphicsResource(nullptr) {
impl = uNew<PlatformImplementation>();
}
IndexBuffer::IndexBuffer(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = uNew<PlatformImplementation>();
}
IndexBuffer::~IndexBuffer() {
impl = nullptr;
}
bool IndexBuffer::Apply(xna_error_ptr_arg) {
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_context || !impl || !impl->dxBuffer) {
2024-05-21 14:20:37 -03:00
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
2024-05-24 22:26:10 -03:00
m_device->impl->_context->IASetIndexBuffer(impl->dxBuffer, DXGI_FORMAT_R16_UINT, 0);
2024-05-21 14:20:37 -03:00
return true;
}
2024-05-23 15:32:50 -03:00
VertexBuffer::VertexBuffer() : GraphicsResource(nullptr) {
impl = uNew<PlatformImplementation>();
}
VertexBuffer::VertexBuffer(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = uNew<PlatformImplementation>();
}
VertexBuffer::~VertexBuffer() {
impl = nullptr;
}
bool VertexBuffer::Apply(xna_error_ptr_arg) {
2024-05-24 22:26:10 -03:00
if (!impl || !m_device || !m_device->impl->_context) {
2024-05-23 15:32:50 -03:00
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
if (!impl->dxBuffer) {
xna_error_apply(err, XnaErrorCode::UNINTIALIZED_RESOURCE);
return false;
}
UINT stride = impl->size;
UINT offset = 0;
2024-05-24 22:26:10 -03:00
m_device->impl->_context->IASetVertexBuffers(0, 1,
2024-05-23 15:32:50 -03:00
&impl->dxBuffer, &stride, &offset);
return true;
}
2024-05-23 16:43:07 -03:00
VertexInputLayout::VertexInputLayout() : GraphicsResource(nullptr) {
impl = uNew<PlatformImplementation>();
}
VertexInputLayout::VertexInputLayout(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = uNew<PlatformImplementation>();
}
VertexInputLayout::~VertexInputLayout() {
impl = nullptr;
}
bool VertexInputLayout::Initialize(DataBuffer& blob, xna_error_ptr_arg) {
2024-05-24 22:26:10 -03:00
if (!impl || !m_device || !m_device->impl->_device || !blob.impl->_blob) {
2024-05-23 16:43:07 -03:00
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
return false;
}
if (impl->_inputLayout) {
impl->_inputLayout->Release();
impl->_inputLayout = nullptr;
}
2024-05-24 22:26:10 -03:00
const auto hr = m_device->impl->_device->CreateInputLayout(
2024-05-23 16:43:07 -03:00
impl->_description.data(),
static_cast<UINT>(impl->_description.size()),
blob.impl->_blob->GetBufferPointer(),
blob.impl->_blob->GetBufferSize(),
&impl->_inputLayout);
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
2024-05-20 09:09:08 -03:00
}