Plasma Engine  2.0
Loading...
Searching...
No Matches
MeshBufferResource.h
1#pragma once
2
3#include <Core/ResourceManager/Resource.h>
4#include <Foundation/Math/BoundingBoxSphere.h>
5#include <Foundation/Memory/AllocatorWrapper.h>
6#include <RendererCore/RendererCoreDLL.h>
7#include <RendererFoundation/Descriptors/Descriptors.h>
8
10class plGeometry;
11
12struct PL_RENDERERCORE_DLL plVertexStreamInfo : public plHashableStruct<plVertexStreamInfo>
13{
14 PL_DECLARE_POD_TYPE();
15
16 plGALVertexAttributeSemantic::Enum m_Semantic;
17 plUInt8 m_uiVertexBufferSlot = 0;
18 plGALResourceFormat::Enum m_Format;
19 plUInt16 m_uiOffset;
20 plUInt16 m_uiElementSize;
21};
22
23struct PL_RENDERERCORE_DLL plVertexDeclarationInfo
24{
25 void ComputeHash();
26
28 plUInt32 m_uiHash;
29};
30
31
32struct PL_RENDERERCORE_DLL plMeshBufferResourceDescriptor
33{
34public:
37
38 void Clear();
39
41 plUInt32 AddStream(plGALVertexAttributeSemantic::Enum semantic, plGALResourceFormat::Enum format);
42
50 void AddCommonStreams();
51
54 void AllocateStreams(plUInt32 uiNumVertices, plGALPrimitiveTopology::Enum topology = plGALPrimitiveTopology::Triangles, plUInt32 uiNumPrimitives = 0, bool bZeroFill = false);
55
58 void AllocateStreamsFromGeometry(const plGeometry& geom, plGALPrimitiveTopology::Enum topology = plGALPrimitiveTopology::Triangles);
59
61 plArrayPtr<const plUInt8> GetVertexBufferData() const;
62
64 plArrayPtr<const plUInt8> GetIndexBufferData() const;
65
68
71
77 template <typename TYPE>
78 void SetVertexData(plUInt32 uiStream, plUInt32 uiVertexIndex, const TYPE& data)
79 {
80 reinterpret_cast<TYPE&>(m_VertexStreamData[m_uiVertexSize * uiVertexIndex + m_VertexDeclaration.m_VertexStreams[uiStream].m_uiOffset]) = data;
81 }
82
87 plArrayPtr<plUInt8> GetVertexData(plUInt32 uiStream, plUInt32 uiVertexIndex) { return m_VertexStreamData.GetArrayPtr().GetSubArray(m_uiVertexSize * uiVertexIndex + m_VertexDeclaration.m_VertexStreams[uiStream].m_uiOffset); }
88
90 void SetPointIndices(plUInt32 uiPoint, plUInt32 uiVertex0);
91
93 void SetLineIndices(plUInt32 uiLine, plUInt32 uiVertex0, plUInt32 uiVertex1);
94
96 void SetTriangleIndices(plUInt32 uiTriangle, plUInt32 uiVertex0, plUInt32 uiVertex1, plUInt32 uiVertex2);
97
99 const plVertexDeclarationInfo& GetVertexDeclaration() const { return m_VertexDeclaration; }
100
102 plUInt32 GetVertexDataSize() const { return m_uiVertexSize; }
103
105 plUInt32 GetVertexCount() const { return m_uiVertexCount; }
106
108 plUInt32 GetPrimitiveCount() const;
109
111 bool Uses32BitIndices() const { return m_uiVertexCount > 0xFFFF; }
112
114 bool HasIndexBuffer() const { return !m_IndexBufferData.IsEmpty(); }
115
117 plBoundingBoxSphere ComputeBounds() const;
118
120 plGALPrimitiveTopology::Enum GetTopology() const { return m_Topology; }
121
122 plResult RecomputeNormals();
123
124private:
125 plGALPrimitiveTopology::Enum m_Topology;
126 plUInt32 m_uiVertexSize;
127 plUInt32 m_uiVertexCount;
128 plVertexDeclarationInfo m_VertexDeclaration;
131};
132
133class PL_RENDERERCORE_DLL plMeshBufferResource : public plResource
134{
135 PL_ADD_DYNAMIC_REFLECTION(plMeshBufferResource, plResource);
136 PL_RESOURCE_DECLARE_COMMON_CODE(plMeshBufferResource);
137 PL_RESOURCE_DECLARE_CREATEABLE(plMeshBufferResource, plMeshBufferResourceDescriptor);
138
139public:
141 : plResource(DoUpdate::OnAnyThread, 1)
142
143 {
144 }
146
147 PL_ALWAYS_INLINE plUInt32 GetPrimitiveCount() const { return m_uiPrimitiveCount; }
148
149 PL_ALWAYS_INLINE plGALBufferHandle GetVertexBuffer() const { return m_hVertexBuffer; }
150
151 PL_ALWAYS_INLINE plGALBufferHandle GetIndexBuffer() const { return m_hIndexBuffer; }
152
153 PL_ALWAYS_INLINE plGALPrimitiveTopology::Enum GetTopology() const { return m_Topology; }
154
156 const plVertexDeclarationInfo& GetVertexDeclaration() const { return m_VertexDeclaration; }
157
159 const plBoundingBoxSphere& GetBounds() const { return m_Bounds; }
160
161private:
162 virtual plResourceLoadDesc UnloadData(Unload WhatToUnload) override;
163 virtual plResourceLoadDesc UpdateContent(plStreamReader* Stream) override;
164 virtual void UpdateMemoryUsage(MemoryUsage& out_NewMemoryUsage) override;
165
166 plBoundingBoxSphere m_Bounds;
167 plVertexDeclarationInfo m_VertexDeclaration;
168 plUInt32 m_uiPrimitiveCount = 0;
169 plGALBufferHandle m_hVertexBuffer;
170 plGALBufferHandle m_hIndexBuffer;
171 plGALPrimitiveTopology::Enum m_Topology = plGALPrimitiveTopology::Enum::Default;
172};
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
PL_FORCE_INLINE plArrayPtr< T > GetSubArray(plUInt32 uiStart, plUInt32 uiCount) const
Creates a sub-array from this array.
Definition ArrayPtr.h:148
Definition DynamicArray.h:81
Definition RendererFoundationDLL.h:418
Provides functions to generate standard geometric shapes, such as boxes, spheres, cylinders,...
Definition Geometry.h:17
This class provides a base class for hashable structs (e.g. descriptor objects).
Definition HashableStruct.h:17
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition MeshBufferResource.h:134
const plVertexDeclarationInfo & GetVertexDeclaration() const
Returns the vertex declaration used by this mesh buffer.
Definition MeshBufferResource.h:156
const plBoundingBoxSphere & GetBounds() const
Returns the bounds of the mesh.
Definition MeshBufferResource.h:159
The base class for all resources.
Definition Resource.h:10
Interface for binary in (read) streams.
Definition Stream.h:22
Definition MeshBufferResource.h:33
void SetVertexData(plUInt32 uiStream, plUInt32 uiVertexIndex, const TYPE &data)
Slow, but convenient method to write one piece of vertex data at a time into the stream buffer.
Definition MeshBufferResource.h:78
bool HasIndexBuffer() const
Returns whether an index buffer is available.
Definition MeshBufferResource.h:114
bool Uses32BitIndices() const
Returns whether 16 or 32 Bit indices are to be used.
Definition MeshBufferResource.h:111
plUInt32 GetVertexCount() const
Return the number of vertices, with which AllocateStreams() was called.
Definition MeshBufferResource.h:105
plArrayPtr< plUInt8 > GetVertexData(plUInt32 uiStream, plUInt32 uiVertexIndex)
Slow, but convenient method to access one piece of vertex data at a time into the stream buffer.
Definition MeshBufferResource.h:87
const plVertexDeclarationInfo & GetVertexDeclaration() const
Allows to read the stream info of the descriptor, which is filled out by AddStream()
Definition MeshBufferResource.h:99
plUInt32 GetVertexDataSize() const
Returns the byte size of all the data for one vertex.
Definition MeshBufferResource.h:102
plGALPrimitiveTopology::Enum GetTopology() const
Returns the primitive topology.
Definition MeshBufferResource.h:120
Describes in which loading state a resource currently is, and how many different quality levels there...
Definition Declarations.h:102
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Definition MeshBufferResource.h:24
Definition MeshBufferResource.h:13
plUInt16 m_uiOffset
at which byte offset the first element starts
Definition MeshBufferResource.h:19
plUInt16 m_uiElementSize
the number of bytes for this element type (depends on the format); this is not the stride between ele...
Definition MeshBufferResource.h:20