Utility/Buffer: Refactor Buffer classes

This commit is contained in:
Lynix
2016-12-09 08:56:46 +01:00
parent e66e0dfdce
commit d62720d610
29 changed files with 461 additions and 677 deletions

View File

@@ -131,7 +131,6 @@ namespace Nz
static GLenum BufferLockRange[BufferAccess_Max+1];
static GLenum BufferTarget[BufferType_Max+1];
static GLenum BufferTargetBinding[BufferType_Max+1];
static GLenum BufferUsage[BufferUsage_Max+1];
static GLenum ComponentType[ComponentType_Max+1];
static GLenum CubemapFace[6]; // Un cube possède six faces et ça n'est pas près de changer
static GLenum FaceFilling[FaceFilling_Max+1];

View File

@@ -7,7 +7,8 @@
#ifndef NAZARA_ABSTRACTBUFFER_HPP
#define NAZARA_ABSTRACTBUFFER_HPP
#include <Nazara/Utility/Buffer.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Enums.hpp>
namespace Nz
{
@@ -17,14 +18,13 @@ namespace Nz
AbstractBuffer() = default;
virtual ~AbstractBuffer();
virtual bool Create(unsigned int size, BufferUsage usage = BufferUsage_Static) = 0;
virtual void Destroy() = 0;
virtual bool Fill(const void* data, UInt32 offset, UInt32 size) = 0;
virtual bool Fill(const void* data, unsigned int offset, unsigned int size, bool forceDiscard = false) = 0;
virtual bool Initialize(UInt32 size, BufferUsageFlags usage) = 0;
virtual bool IsHardware() const = 0;
virtual DataStorage GetStorage() const = 0;
virtual void* Map(BufferAccess access, unsigned int offset = 0, unsigned int size = 0) = 0;
virtual void* Map(BufferAccess access, UInt32 offset = 0, UInt32 size = 0) = 0;
virtual bool Unmap() = 0;
};
}

View File

@@ -11,8 +11,10 @@
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Utility/AbstractBuffer.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <array>
namespace Nz
{
@@ -21,8 +23,6 @@ namespace Nz
using BufferConstRef = ObjectRef<const Buffer>;
using BufferRef = ObjectRef<Buffer>;
class AbstractBuffer;
class NAZARA_UTILITY_API Buffer : public RefCounted
{
friend class Utility;
@@ -31,40 +31,41 @@ namespace Nz
using BufferFactory = AbstractBuffer* (*)(Buffer* parent, BufferType type);
Buffer(BufferType type);
Buffer(BufferType type, unsigned int size, UInt32 storage = DataStorage_Software, BufferUsage usage = BufferUsage_Static);
Buffer(BufferType type, UInt32 size, DataStorage storage = DataStorage_Software, BufferUsageFlags usage = 0);
Buffer(const Buffer&) = delete;
Buffer(Buffer&&) = delete;
Buffer(Buffer&&) = default;
~Buffer();
bool CopyContent(const Buffer& buffer);
bool CopyContent(const BufferRef& buffer);
bool Create(unsigned int size, UInt32 storage = DataStorage_Software, BufferUsage usage = BufferUsage_Static);
bool Create(UInt32 size, DataStorage storage = DataStorage_Software, BufferUsageFlags usage = 0);
void Destroy();
bool Fill(const void* data, unsigned int offset, unsigned int size, bool forceDiscard = false);
bool Fill(const void* data, UInt32 offset, UInt32 size);
AbstractBuffer* GetImpl() const;
unsigned int GetSize() const;
UInt32 GetStorage() const;
BufferType GetType() const;
BufferUsage GetUsage() const;
inline AbstractBuffer* GetImpl() const;
inline UInt32 GetSize() const;
inline DataStorage GetStorage() const;
inline BufferType GetType() const;
inline BufferUsageFlags GetUsage() const;
bool IsHardware() const;
bool IsValid() const;
inline bool HasStorage(DataStorage storage) const;
void* Map(BufferAccess access, unsigned int offset = 0, unsigned int size = 0);
void* Map(BufferAccess access, unsigned int offset = 0, unsigned int size = 0) const;
inline bool IsValid() const;
bool SetStorage(UInt32 storage);
void* Map(BufferAccess access, UInt32 offset = 0, UInt32 size = 0);
void* Map(BufferAccess access, UInt32 offset = 0, UInt32 size = 0) const;
bool SetStorage(DataStorage storage);
void Unmap() const;
Buffer& operator=(const Buffer&) = delete;
Buffer& operator=(Buffer&&) = delete;
Buffer& operator=(Buffer&&) = default;
static bool IsStorageSupported(UInt32 storage);
static bool IsStorageSupported(DataStorage storage);
template<typename... Args> static BufferRef New(Args&&... args);
static void SetBufferFactory(UInt32 storage, BufferFactory func);
static void SetBufferFactory(DataStorage storage, BufferFactory func);
// Signals:
NazaraSignal(OnBufferDestroy, const Buffer* /*buffer*/);
@@ -74,13 +75,12 @@ namespace Nz
static bool Initialize();
static void Uninitialize();
std::unique_ptr<AbstractBuffer> m_impl;
BufferType m_type;
BufferUsage m_usage;
UInt32 m_storage;
AbstractBuffer* m_impl;
unsigned int m_size;
BufferUsageFlags m_usage;
UInt32 m_size;
static BufferFactory s_bufferFactories[DataStorage_Max+1];
static std::array<BufferFactory, DataStorage_Max + 1> s_bufferFactories;
};
}

View File

@@ -7,6 +7,41 @@
namespace Nz
{
inline AbstractBuffer* Buffer::GetImpl() const
{
return m_impl.get();
}
inline UInt32 Buffer::GetSize() const
{
return m_size;
}
inline DataStorage Buffer::GetStorage() const
{
return m_impl->GetStorage();
}
inline BufferType Buffer::GetType() const
{
return m_type;
}
inline BufferUsageFlags Buffer::GetUsage() const
{
return m_usage;
}
inline bool Buffer::HasStorage(DataStorage storage) const
{
return GetStorage() == storage;
}
inline bool Buffer::IsValid() const
{
return m_impl != nullptr;
}
template<typename... Args>
BufferRef Buffer::New(Args&&... args)
{

View File

@@ -56,11 +56,19 @@ namespace Nz
enum BufferUsage
{
BufferUsage_Dynamic,
BufferUsage_Static,
BufferUsage_FastRead,
BufferUsage_Max = BufferUsage_Static
BufferUsage_Max = BufferUsage_FastRead
};
template<>
struct EnableFlagsOperators<BufferUsage>
{
static constexpr bool value = true;
};
using BufferUsageFlags = Flags<BufferUsage>;
enum ComponentType
{
ComponentType_Color,
@@ -95,14 +103,12 @@ namespace Nz
CubemapFace_Max = CubemapFace_NegativeZ
};
enum DataStorageFlags
enum DataStorage
{
DataStorage_Hardware = 0x1,
DataStorage_Software = 0x2,
DataStorage_Hardware,
DataStorage_Software,
DataStorage_Both = DataStorage_Hardware | DataStorage_Software,
DataStorage_Max = DataStorage_Software*2-1
DataStorage_Max = DataStorage_Software
};
enum FaceFilling

View File

@@ -23,46 +23,46 @@ namespace Nz
{
public:
IndexBuffer() = default;
IndexBuffer(bool largeIndices, Buffer* buffer);
IndexBuffer(bool largeIndices, Buffer* buffer, unsigned int startOffset, unsigned int endOffset);
IndexBuffer(bool largeIndices, unsigned int length, UInt32 storage = DataStorage_Software, BufferUsage usage = BufferUsage_Static);
IndexBuffer(bool largeIndices, BufferRef buffer);
IndexBuffer(bool largeIndices, BufferRef buffer, UInt32 offset, UInt32 size);
IndexBuffer(bool largeIndices, UInt32 length, DataStorage storage, BufferUsageFlags usage);
IndexBuffer(const IndexBuffer& indexBuffer);
IndexBuffer(IndexBuffer&&) = delete;
~IndexBuffer();
unsigned int ComputeCacheMissCount() const;
bool Fill(const void* data, unsigned int startIndex, unsigned int length, bool forceDiscard = false);
bool FillRaw(const void* data, unsigned int offset, unsigned int size, bool forceDiscard = false);
bool Fill(const void* data, UInt32 startIndex, UInt32 length);
bool FillRaw(const void* data, UInt32 offset, UInt32 size);
Buffer* GetBuffer() const;
unsigned int GetEndOffset() const;
unsigned int GetIndexCount() const;
unsigned int GetStride() const;
unsigned int GetStartOffset() const;
inline const BufferRef& GetBuffer() const;
inline UInt32 GetEndOffset() const;
inline UInt32 GetIndexCount() const;
inline DataStorage GetStorage() const;
inline UInt32 GetStride() const;
inline UInt32 GetStartOffset() const;
bool HasLargeIndices() const;
inline bool HasLargeIndices() const;
bool IsHardware() const;
bool IsValid() const;
inline bool IsValid() const;
void* Map(BufferAccess access, unsigned int startVertex = 0, unsigned int length = 0);
void* Map(BufferAccess access, unsigned int startVertex = 0, unsigned int length = 0) const;
void* MapRaw(BufferAccess access, unsigned int offset = 0, unsigned int size = 0);
void* MapRaw(BufferAccess access, unsigned int offset = 0, unsigned int size = 0) const;
inline void* Map(BufferAccess access, UInt32 startVertex = 0, UInt32 length = 0);
inline void* Map(BufferAccess access, UInt32 startVertex = 0, UInt32 length = 0) const;
void* MapRaw(BufferAccess access, UInt32 offset = 0, UInt32 size = 0);
void* MapRaw(BufferAccess access, UInt32 offset = 0, UInt32 size = 0) const;
void Optimize();
void Reset();
void Reset(bool largeIndices, Buffer* buffer);
void Reset(bool largeIndices, Buffer* buffer, unsigned int startOffset, unsigned int endOffset);
void Reset(bool largeIndices, unsigned int length, UInt32 storage = DataStorage_Software, BufferUsage usage = BufferUsage_Static);
void Reset(bool largeIndices, BufferRef buffer);
void Reset(bool largeIndices, BufferRef buffer, UInt32 offset, UInt32 size);
void Reset(bool largeIndices, UInt32 length, DataStorage storage, BufferUsageFlags usage);
void Reset(const IndexBuffer& indexBuffer);
bool SetStorage(UInt32 storage);
void Unmap() const;
IndexBuffer& operator=(const IndexBuffer& indexBuffer);
IndexBuffer& operator=(IndexBuffer&&) = delete;
template<typename... Args> static IndexBufferRef New(Args&&... args);
@@ -71,10 +71,10 @@ namespace Nz
private:
BufferRef m_buffer;
UInt32 m_endOffset;
UInt32 m_indexCount;
UInt32 m_startOffset;
bool m_largeIndices;
unsigned int m_endOffset;
unsigned int m_indexCount;
unsigned int m_startOffset;
};
}

View File

@@ -2,11 +2,64 @@
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/IndexBuffer.hpp>
#include <memory>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
inline const BufferRef& IndexBuffer::GetBuffer() const
{
return m_buffer;
}
inline UInt32 IndexBuffer::GetEndOffset() const
{
return m_endOffset;
}
inline UInt32 IndexBuffer::GetIndexCount() const
{
return m_indexCount;
}
inline DataStorage IndexBuffer::GetStorage() const
{
return DataStorage();
}
inline UInt32 IndexBuffer::GetStride() const
{
return static_cast<UInt32>((m_largeIndices) ? sizeof(UInt32) : sizeof(UInt16));
}
inline UInt32 IndexBuffer::GetStartOffset() const
{
return m_startOffset;
}
inline bool IndexBuffer::HasLargeIndices() const
{
return m_largeIndices;
}
inline bool IndexBuffer::IsValid() const
{
return m_buffer;
}
inline void* IndexBuffer::Map(BufferAccess access, UInt32 startIndex, UInt32 length)
{
UInt32 stride = GetStride();
return MapRaw(access, startIndex*stride, length*stride);
}
inline void* IndexBuffer::Map(BufferAccess access, UInt32 startIndex, UInt32 length) const
{
UInt32 stride = GetStride();
return MapRaw(access, startIndex*stride, length*stride);
}
template<typename... Args>
IndexBufferRef IndexBuffer::New(Args&&... args)
{

View File

@@ -30,13 +30,13 @@ namespace Nz
{
MeshParams();
Matrix4f matrix = Matrix4f::Identity(); ///< A matrix which will transform every vertex position
UInt32 storage = DataStorage_Hardware; ///< The place where the buffers will be allocated
Vector2f texCoordOffset = {0.f, 0.f}; ///< Offset to apply on the texture coordinates (not scaled)
Vector2f texCoordScale = {1.f, 1.f}; ///< Scale to apply on the texture coordinates
bool animated = true; ///< If true, will load an animated version of the model if possible
bool center = false; ///< If true, will center the mesh vertices around the origin
bool optimizeIndexBuffers = true; ///< Optimize the index buffers after loading, improve cache locality (and thus rendering speed) but increase loading time.
Matrix4f matrix = Matrix4f::Identity(); ///< A matrix which will transform every vertex position
DataStorage storage = DataStorage_Hardware; ///< The place where the buffers will be allocated
Vector2f texCoordOffset = {0.f, 0.f}; ///< Offset to apply on the texture coordinates (not scaled)
Vector2f texCoordScale = {1.f, 1.f}; ///< Scale to apply on the texture coordinates
bool animated = true; ///< If true, will load an animated version of the model if possible
bool center = false; ///< If true, will center the mesh vertices around the origin
bool optimizeIndexBuffers = true; ///< Optimize the index buffers after loading, improve cache locality (and thus rendering speed) but increase loading time.
bool IsValid() const;
};

View File

@@ -25,42 +25,42 @@ namespace Nz
{
public:
VertexBuffer() = default;
VertexBuffer(const VertexDeclaration* vertexDeclaration, Buffer* buffer);
VertexBuffer(const VertexDeclaration* vertexDeclaration, Buffer* buffer, unsigned int startOffset, unsigned int endOffset);
VertexBuffer(const VertexDeclaration* vertexDeclaration, unsigned int length, UInt32 storage = DataStorage_Software, BufferUsage usage = BufferUsage_Static);
VertexBuffer(VertexDeclarationConstRef vertexDeclaration, BufferRef buffer);
VertexBuffer(VertexDeclarationConstRef vertexDeclaration, BufferRef buffer, UInt32 offset, UInt32 size);
VertexBuffer(VertexDeclarationConstRef vertexDeclaration, UInt32 length, DataStorage storage, BufferUsageFlags usage);
VertexBuffer(const VertexBuffer& vertexBuffer);
VertexBuffer(VertexBuffer&&) = delete;
~VertexBuffer();
bool Fill(const void* data, unsigned int startVertex, unsigned int length, bool forceDiscard = false);
bool FillRaw(const void* data, unsigned int offset, unsigned int size, bool forceDiscard = false);
bool Fill(const void* data, UInt32 startVertex, UInt32 length);
bool FillRaw(const void* data, UInt32 offset, UInt32 size);
Buffer* GetBuffer() const;
unsigned int GetEndOffset() const;
unsigned int GetStartOffset() const;
unsigned int GetStride() const;
unsigned int GetVertexCount() const;
const VertexDeclaration* GetVertexDeclaration() const;
inline const BufferRef& GetBuffer() const;
inline UInt32 GetEndOffset() const;
inline UInt32 GetStartOffset() const;
inline UInt32 GetStride() const;
inline UInt32 GetVertexCount() const;
inline const VertexDeclarationConstRef& GetVertexDeclaration() const;
bool IsHardware() const;
bool IsValid() const;
inline bool IsValid() const;
void* Map(BufferAccess access, unsigned int startVertex = 0, unsigned int length = 0);
void* Map(BufferAccess access, unsigned int startVertex = 0, unsigned int length = 0) const;
void* MapRaw(BufferAccess access, unsigned int offset = 0, unsigned int size = 0);
void* MapRaw(BufferAccess access, unsigned int offset = 0, unsigned int size = 0) const;
void* Map(BufferAccess access, UInt32 startVertex = 0, UInt32 length = 0);
void* Map(BufferAccess access, UInt32 startVertex = 0, UInt32 length = 0) const;
void* MapRaw(BufferAccess access, UInt32 offset = 0, UInt32 size = 0);
void* MapRaw(BufferAccess access, UInt32 offset = 0, UInt32 size = 0) const;
void Reset();
void Reset(const VertexDeclaration* vertexDeclaration, Buffer* buffer);
void Reset(const VertexDeclaration* vertexDeclaration, Buffer* buffer, unsigned int startOffset, unsigned int endOffset);
void Reset(const VertexDeclaration* vertexDeclaration, unsigned int length, UInt32 storage = DataStorage_Software, BufferUsage usage = BufferUsage_Static);
void Reset(VertexDeclarationConstRef vertexDeclaration, BufferRef buffer);
void Reset(VertexDeclarationConstRef vertexDeclaration, BufferRef buffer, UInt32 offset, UInt32 size);
void Reset(VertexDeclarationConstRef vertexDeclaration, UInt32 length, DataStorage storage, BufferUsageFlags usage);
void Reset(const VertexBuffer& vertexBuffer);
bool SetStorage(UInt32 storage);
void SetVertexDeclaration(const VertexDeclaration* vertexDeclaration);
void SetVertexDeclaration(VertexDeclarationConstRef vertexDeclaration);
void Unmap() const;
VertexBuffer& operator=(const VertexBuffer& vertexBuffer);
VertexBuffer& operator=(VertexBuffer&&) = delete;
template<typename... Args> static VertexBufferRef New(Args&&... args);
@@ -69,10 +69,10 @@ namespace Nz
private:
BufferRef m_buffer;
UInt32 m_endOffset;
UInt32 m_startOffset;
UInt32 m_vertexCount;
VertexDeclarationConstRef m_vertexDeclaration;
unsigned int m_endOffset;
unsigned int m_startOffset;
unsigned int m_vertexCount;
};
}

View File

@@ -7,6 +7,41 @@
namespace Nz
{
inline const BufferRef& VertexBuffer::GetBuffer() const
{
return m_buffer;
}
inline UInt32 VertexBuffer::GetEndOffset() const
{
return m_endOffset;
}
inline UInt32 VertexBuffer::GetStride() const
{
return static_cast<UInt32>(m_vertexDeclaration->GetStride());
}
inline UInt32 VertexBuffer::GetStartOffset() const
{
return m_startOffset;
}
inline UInt32 VertexBuffer::GetVertexCount() const
{
return m_vertexCount;
}
inline const VertexDeclarationConstRef& VertexBuffer::GetVertexDeclaration() const
{
return m_vertexDeclaration;
}
inline bool VertexBuffer::IsValid() const
{
return m_buffer && m_vertexDeclaration;
}
template<typename... Args>
VertexBufferRef VertexBuffer::New(Args&&... args)
{