CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
RendererMesh.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
13
16
21
22namespace CeresEngine {
23
26 enum class RendererMeshFlag {
30 };
31
33 using RendererMeshFlags = Flags<RendererMeshFlag>;
35
36
58
79
82
95 class RendererMesh : public RefCounted<RendererMesh> {
96 private:
99
102
105
109
115
121
125
129
132
133 public:
136
141
142 public:
148
154
155 public:
157 void bind(GPUCommandBuffer& commandBuffer) const;
158
170
183
196
207 void draw(GPUCommandBuffer& commandBuffer, const SubMesh& subMesh, const Optional<const ConvexVolume&>& viewFrustum = {},
209
210 public:
213
216
219
222
225
228
231
233 [[nodiscard]] explicit operator bool() const noexcept { return isReady(); }
234
237
240
243
246
249
252
255
258
259 private:
261
264
267
270
271 private: // Uploading
276 };
277
280
284 private:
285 struct MeshInfo {
287
290 bool isLoading = false;
291
292 bool isLoaded = false;
293 };
294
295 private:
298
301
305
306 public:
308 explicit RendererMeshManager(Renderer& renderer, GPUDevice& device);
309
313
314 public:
325
336
349 [[nodiscard]] RendererMeshPtr get(const MeshPtr& mesh) { return getOrCreate(mesh); }
350
351 private:
352 friend class RendererMesh;
353
357
360
362 void notifyDestroy(const RendererMesh* mesh) noexcept;
363 };
364
365} // namespace CeresEngine
#define CE_FLAGS_OPERATORS(Enum)
Defines global operators for a Flags<Enum, Storage> implementation.
Definition Flags.hpp:216
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
Definition GPUCommandBuffer.hpp:77
Definition GPUDevice.hpp:357
Definition GPUBufferObject.hpp:169
Definition GPUBufferObject.hpp:126
Definition Optional.hpp:17
A simple reference counter base class.
Definition SmartPtr.hpp:438
The CeresEngine renderer.
Definition Renderer.hpp:35
A mesh that is usable by the renderer and the GPU.
Definition RendererMesh.hpp:95
void setBuffers(GPUVertexBuffer &&vertexBuffer, GPUIndexBuffer &&indexBuffer, const GPUVertexFormat &vertexFormat)
Updates the vertex buffer, index buffer and the vertex formats.
void setFlags(RendererMeshFlags flags)
A set of flags for the renderer mesh.
Async uploadMesh()
Uploads the raw mesh data to the GPU.
Sphere getBoundingSphere() const noexcept
Gets the mesh bounding sphere.
const SPtr< Mesh > & getResource() const noexcept
A pointer to the mesh resource object that will be uploaded to the GPU.
Definition RendererMesh.hpp:236
const GPUVertexFormat & getVertexFormat() const noexcept
A structure that describes the format of the vertex data as it was uploaded to the GPU.
Definition RendererMesh.hpp:245
bool isPending() const noexcept
The mesh is not uploaded to the GPU and the upload process hasn't started yet.
Definition RendererMesh.hpp:218
bool isRelocating() const noexcept
The mesh is uploaded to the GPU but is in the process of being relocated to another vertex and index ...
Definition RendererMesh.hpp:224
const GPUIndexBuffer & getIndexBuffer() const noexcept
The current index buffer as allocated by the mesh manager.
Definition RendererMesh.hpp:242
SPtr< Mesh > mResource
A pointer to the mesh resource object that will be uploaded to the GPU.
Definition RendererMesh.hpp:108
RendererMeshState mState
The state the renderer mesh is currently in.
Definition RendererMesh.hpp:104
bool isUploading() const noexcept
The mesh is in the process of being uploaded to the GPU, but the asynchronous upload operation hasn't...
Definition RendererMesh.hpp:221
GPUVertexFormat mVertexFormat
A structure that describes the format of the vertex data as it was uploaded to the GPU.
Definition RendererMesh.hpp:124
VertexDeclaration mVertexDeclaration
Determines how vertices are laid-out on a vertex buffer or a mesh data.
Definition RendererMesh.hpp:128
RendererMeshState getState() const noexcept
The state the renderer mesh is currently in.
Definition RendererMesh.hpp:215
bool isReady() const noexcept
Checks if the mesh is ready to be used by the renderer.
Definition RendererMesh.hpp:230
Event< void()> didChangeIndexBuffer
An event triggered internally in the renderer whenever the index buffer for the mesh has changed.
Definition RendererMesh.hpp:153
GPUIndexBuffer mIndexBuffer
The current index buffer as allocated by the mesh manager.
Definition RendererMesh.hpp:120
Vector< SubMesh > mSubMeshes
An array of sub-meshes.
Definition RendererMesh.hpp:131
void setState(RendererMeshState state)
The state the renderer mesh is currently in.
AABox getBoundingBox() const noexcept
Gets the mesh bounding box.
const VertexDeclaration & getVertexDeclaration() const noexcept
Determines how vertices are laid-out on a vertex buffer or a mesh data.
Definition RendererMesh.hpp:248
GPUVertexBuffer mVertexBuffer
The current vertex buffer as allocated by the mesh manager.
Definition RendererMesh.hpp:114
void draw(GPUCommandBuffer &commandBuffer, const SubMesh &subMesh, const Optional< const ConvexVolume & > &viewFrustum={}, const RendererMeshDrawFlags &flags=RendererMeshDrawFlag::Default) const
Draws a specific sub-mesh.
const GPUVertexBuffer & getVertexBuffer() const noexcept
The current vertex buffer as allocated by the mesh manager.
Definition RendererMesh.hpp:239
RendererMeshFlags mFlags
A set of flags for the renderer mesh.
Definition RendererMesh.hpp:101
const Vector< SubMesh > & getSubMeshes() const noexcept
An array of sub-meshes.
Definition RendererMesh.hpp:257
RendererMesh(RendererMeshManager &manager, const MeshPtr &engineMesh)
Creates a new renderer mesh instance.
bool isUploaded() const noexcept
The mesh is uploaded to the GPU and is ready to be used by the renderer.
Definition RendererMesh.hpp:227
Event< void()> didChangeVertexBuffer
An event triggered internally in the renderer whenever the vertex buffer for the mesh has changed.
Definition RendererMesh.hpp:147
RendererMeshManager & mManager
The mesh manager that owns and manages this mesh instance.
Definition RendererMesh.hpp:98
void draw(GPUCommandBuffer &commandBuffer, const Span< UInt32 > &subMeshIndices, const Optional< const ConvexVolume & > &viewFrustum={}, const RendererMeshDrawFlags &flags=RendererMeshDrawFlag::Default) const
Draws an specific set of sub-meshes by index.
void draw(GPUCommandBuffer &commandBuffer, UInt32 subMeshIdx, const Optional< const ConvexVolume & > &viewFrustum={}, const RendererMeshDrawFlags &flags=RendererMeshDrawFlag::Default) const
Draws an specific sub-mesh index.
void bind(GPUCommandBuffer &commandBuffer) const
Binds the mesh vertex and index buffers.
RendererMeshFlags getFlags() const noexcept
A set of flags for the renderer mesh.
Definition RendererMesh.hpp:212
void draw(GPUCommandBuffer &commandBuffer, const Optional< const ConvexVolume & > &viewFrustum={}, const RendererMeshDrawFlags &flags=RendererMeshDrawFlag::Default) const
Draws the entire mesh.
~RendererMesh()
Destroys the renderer mesh instance.
A manager class that creates, manages and handles GPU vertex and index buffers for meshes used by the...
Definition RendererMesh.hpp:283
RendererMeshPtr get(const MeshPtr &mesh)
Gets a renderer mesh object.
Definition RendererMesh.hpp:349
RendererMeshPtr getOrCreate(const MeshPtr &mesh)
Gets an existing or creates a new renderer mesh instance for the given engine mesh.
RendererMeshManager(Renderer &renderer, GPUDevice &device)
Creates a new instance of the RendererMeshManager.
Renderer & mRenderer
The renderer that owns this manager instance.
Definition RendererMesh.hpp:297
GPUVertexBuffer getVertexBuffer(const HMesh &mesh)
Tries to locate a VertexBuffer for the given mesh.
HashMap< Mesh *, MeshInfo > mMeshes
A map that holds mesh information for each engine mesh used by the renderer.
Definition RendererMesh.hpp:304
GPUDevice & mDevice
The device to which the meshes will be uploaded to.
Definition RendererMesh.hpp:300
~RendererMeshManager()
Destroys the RendererMeshManager and releases all GPU resources managed and allocated by it.
void notifyDestroy(const RendererMesh *mesh) noexcept
Notifies the manager that the renderer mesh is being destroyed.
RendererMeshPtr create(const MeshPtr &mesh)
Creates a new renderer mesh instance for the given engine mesh.
GPUIndexBuffer getIndexBuffer(const HMesh &mesh)
Tries to locate a IndexBuffer for the given mesh.
Base template for the event class.
Definition Event.hpp:27
Definition Application.hpp:19
SPtr< Mesh > MeshPtr
Definition Forward.hpp:17
std::shared_ptr< T > SPtr
SPtr is a smart pointer that retains shared ownership of an object through a pointer.
Definition SmartPtr.hpp:37
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
struct CeresEngine::GLState state
RendererMeshFlag
Flags that can be either set by the renderer or by the user to customize the behavior of the mesh.
Definition RendererMesh.hpp:26
@ Optimized
If defined, the mesh was optimized before being uploaded to the GPU.
RendererMeshDrawFlag
Definition RendererMesh.hpp:59
@ AssumeIndexBufferIsAlreadyBound
If set, the index buffer will be considered as already bound and will not be rebound.
@ NoCoalescing
If set, the renderer is allowed to coalesce and merge sequential sub-meshes into a single larger draw...
@ AssumeVertexBufferIsAlreadyBound
If set, the vertex buffer will be considered as already bound and will not be rebound.
@ Default
The default value for draw calls.
@ AssumeBuffersAreAlreadyBound
Same as AssumeVertexBufferIsAlreadyBound and AssumeIndexBufferIsAlreadyBound.
Flags< RendererMeshFlag > RendererMeshFlags
Flags that can be either set by the renderer or by the user to customize the behavior of the mesh.
Definition Forward.hpp:18
RendererMeshState
An enumeration that contains possible states for a renderer mesh.
Definition RendererMesh.hpp:38
@ Pending
The mesh is not uploaded to the GPU and the upload process hasn't started yet.
@ Uploading
The mesh is in the process of being uploaded to the GPU, but the asynchronous upload operation hasn't...
@ Relocating
The mesh is uploaded to the GPU but is in the process of being relocated to another vertex and index ...
@ Uploaded
The mesh is uploaded to the GPU and is ready to be used by the renderer.
std::uint32_t UInt32
Definition DataTypes.hpp:23
tcb ::span< T, Extent > Span
Span describes an object that can refer to a contiguous sequence of objects with the first element of...
Definition Span.hpp:708
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Wrapper around an enum that allows simple use of bitwise logic operations.
Definition Flags.hpp:19
Definition GPUVertexFormat.hpp:77
Definition RendererMesh.hpp:285
RendererMesh * rendererMesh
Definition RendererMesh.hpp:286
bool isLoading
A boolean flag that indicates if the mesh is being loaded asynchronously.
Definition RendererMesh.hpp:290
bool isLoaded
Definition RendererMesh.hpp:292
Definition Mesh.hpp:68
Determines how vertices are laid-out on a vertex buffer or a mesh data.
Definition VertexDeclaration.hpp:205