Add render resource handling for geometry
This commit is contained in:
@@ -19,21 +19,18 @@
|
||||
|
||||
namespace OpenVulkano::Scene
|
||||
{
|
||||
Geometry::Geometry(const Geometry& other)
|
||||
{
|
||||
this->vertexCount = other.vertexCount;
|
||||
this->indexCount = other.indexCount;
|
||||
this->indexType = other.indexType;
|
||||
this->aabb = other.aabb;
|
||||
this->ownsMemory = other.ownsMemory;
|
||||
this->freeAfterUpload = other.freeAfterUpload;
|
||||
this->renderGeo = nullptr;
|
||||
this->vertices = new Vertex[vertexCount];
|
||||
Geometry::Geometry(const Geometry& other)
|
||||
: vertexCount(other.vertexCount), indexCount(other.indexCount)
|
||||
, vertices(other.vertices ? new Vertex[other.vertexCount] : nullptr)
|
||||
, indices(other.indices ? malloc(static_cast<size_t>(Utils::EnumAsInt(other.indexType)) * other.indexCount) : nullptr)
|
||||
, indexType(other.indexType)
|
||||
, ownsMemory(other.ownsMemory), freeAfterUpload(other.freeAfterUpload)
|
||||
, aabb(other.aabb)
|
||||
{
|
||||
if (other.vertices)
|
||||
{
|
||||
std::copy(other.vertices, other.vertices + other.vertexCount, this->vertices);
|
||||
}
|
||||
this->indices = malloc(static_cast<size_t>(Utils::EnumAsInt(other.indexType)) * other.indexCount);
|
||||
if (other.indices)
|
||||
{
|
||||
if (other.indexType == VertexIndexType::UINT16)
|
||||
@@ -58,21 +55,14 @@ namespace OpenVulkano::Scene
|
||||
return *this;
|
||||
}
|
||||
|
||||
Geometry::Geometry(Geometry&& other) noexcept
|
||||
Geometry::Geometry(Geometry&& other) noexcept
|
||||
: RenderResourceHolder<Geometry>(std::move(other)), vertexCount(other.vertexCount), indexCount(other.indexCount)
|
||||
, vertices(other.vertices), indices(other.indices), indexType(other.indexType)
|
||||
, ownsMemory(other.ownsMemory), freeAfterUpload(other.freeAfterUpload), aabb(other.aabb)
|
||||
{
|
||||
this->vertexCount = other.vertexCount;
|
||||
this->indexCount = other.indexCount;
|
||||
this->indexType = other.indexType;
|
||||
this->ownsMemory = other.ownsMemory;
|
||||
this->freeAfterUpload = other.freeAfterUpload;
|
||||
this->aabb = std::move(other.aabb);
|
||||
this->vertices = other.vertices;
|
||||
this->indices = other.indices;
|
||||
this->renderGeo = other.renderGeo;
|
||||
other.vertexCount = other.indexCount = 0;
|
||||
other.vertices = nullptr;
|
||||
other.indices = nullptr;
|
||||
other.renderGeo = nullptr;
|
||||
}
|
||||
|
||||
Geometry& Geometry::operator=(Geometry&& other) noexcept
|
||||
@@ -88,11 +78,10 @@ namespace OpenVulkano::Scene
|
||||
this->aabb = std::move(other.aabb);
|
||||
this->vertices = other.vertices;
|
||||
this->indices = other.indices;
|
||||
this->renderGeo = other.renderGeo;
|
||||
RenderResourceHolder<Geometry>::operator=(std::move(other));
|
||||
other.vertexCount = other.indexCount = 0;
|
||||
other.vertices = nullptr;
|
||||
other.indices = nullptr;
|
||||
other.renderGeo = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -103,27 +92,26 @@ namespace OpenVulkano::Scene
|
||||
}
|
||||
|
||||
void Geometry::Swap(Geometry& other) noexcept
|
||||
{
|
||||
{
|
||||
RenderResourceHolder<Geometry>::Swap(other);
|
||||
std::swap(this->vertexCount, other.vertexCount);
|
||||
std::swap(this->indexCount, other.indexCount);
|
||||
std::swap(this->aabb, other.aabb);
|
||||
std::swap(this->indexType, other.indexType);
|
||||
std::swap(this->vertices, other.vertices);
|
||||
std::swap(this->indices, other.indices);
|
||||
std::swap(this->renderGeo, other.renderGeo);
|
||||
std::swap(this->ownsMemory, other.ownsMemory);
|
||||
std::swap(this->freeAfterUpload, other.freeAfterUpload);
|
||||
}
|
||||
|
||||
void Geometry::Init(uint32_t vertexCount, uint32_t indexCount)
|
||||
{
|
||||
if (this->vertexCount || this->indexCount) throw std::runtime_error("Geometry is already initialized.");
|
||||
if (HasRenderResource() || this->vertexCount || this->indexCount) throw std::runtime_error("Geometry is already initialized.");
|
||||
this->vertexCount = vertexCount;
|
||||
this->indexCount = indexCount;
|
||||
indexType = (vertexCount > UINT16_MAX) ? VertexIndexType::UINT32 : VertexIndexType::UINT16;
|
||||
vertices = new Vertex[vertexCount];
|
||||
indices = malloc(static_cast<size_t>(Utils::EnumAsInt(indexType)) * indexCount);
|
||||
renderGeo = nullptr;
|
||||
}
|
||||
|
||||
void Geometry::InitFromFile(const std::string& file)
|
||||
@@ -212,10 +200,9 @@ namespace OpenVulkano::Scene
|
||||
indexCount = 0;
|
||||
Free();
|
||||
}
|
||||
if (renderGeo)
|
||||
if (HasRenderResource())
|
||||
{
|
||||
renderGeo->Close();
|
||||
renderGeo = nullptr;
|
||||
GetRenderResource().Release();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Base/ICloseable.hpp"
|
||||
#include "Base/Render/RenderResource.hpp"
|
||||
#include "Math/AABB.hpp"
|
||||
#include "Base/Utils.hpp"
|
||||
#include <string>
|
||||
@@ -24,7 +25,7 @@ namespace OpenVulkano
|
||||
UINT16 = sizeof(uint16_t), UINT32 = sizeof(uint32_t)
|
||||
};
|
||||
|
||||
class Geometry : public ICloseable
|
||||
class Geometry : public RenderResourceHolder<Geometry>, public ICloseable
|
||||
{
|
||||
public:
|
||||
uint32_t vertexCount = 0, indexCount = 0;
|
||||
@@ -33,7 +34,6 @@ namespace OpenVulkano
|
||||
VertexIndexType indexType = VertexIndexType::UINT16;
|
||||
bool ownsMemory = true, freeAfterUpload = true;
|
||||
Math::AABB aabb;
|
||||
ICloseable* renderGeo = nullptr;
|
||||
public:
|
||||
Geometry() = default;
|
||||
Geometry(const Geometry& other);
|
||||
|
||||
Reference in New Issue
Block a user