SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
MeshBatch.cpp
Go to the documentation of this file.
6#include <Camera/Camera.hpp>
8
9namespace Sleak {
10
11// Shared identity transform buffer — allocated once, reused every frame
13
14MeshHandle::MeshHandle() = default;
15MeshHandle::~MeshHandle() = default;
16MeshHandle::MeshHandle(const MeshHandle&) = default;
17MeshHandle::MeshHandle(MeshHandle&&) noexcept = default;
18MeshHandle& MeshHandle::operator=(const MeshHandle&) = default;
19MeshHandle& MeshHandle::operator=(MeshHandle&&) noexcept = default;
20
21/// Uploads a vertex/index group as GPU buffers and returns a handle to them.
23 MeshHandle h;
24 if (vertices.GetSize() == 0 || indices.GetSize() == 0) return h;
25
28 static_cast<uint32_t>(vertices.GetSizeInBytes()),
29 vertices.GetRawData()));
30
33 static_cast<uint32_t>(indices.GetByteSize()),
34 indices.GetRawData()));
35
36 h.indexCount = static_cast<uint32_t>(indices.GetSize());
37 return h;
38}
39
40/// Uploads raw custom-format vertices/indices as GPU buffers, tagging the vertex buffer with the format handle.
41/// format == 0 creates a mesh using the engine's default vertex layout.
43 const void* vertexData,
44 size_t vertexBytes,
45 const uint32_t* indices,
46 size_t indexCount) {
47 MeshHandle h;
48 if (!vertexData || vertexBytes == 0 || !indices || indexCount == 0) return h;
49
52 static_cast<uint32_t>(vertexBytes),
53 const_cast<void*>(vertexData));
54 if (vb) vb->SetVertexFormat(format);
55 h.vertexBuffer = RefPtr(vb);
56
59 static_cast<uint32_t>(indexCount * sizeof(uint32_t)),
60 const_cast<uint32_t*>(indices)));
61
62 h.indexCount = static_cast<uint32_t>(indexCount);
63 h.vertexFormat = format;
64 return h;
65}
66
67/// Binds the batch material and (re)uploads the shared identity-world transform buffer once per batch.
70
71 // Bind material once for all draws in this batch
72 queue->SubmitBindMaterial(material);
73
74 // Build identity world matrix — batch vertices are already in world space
75 auto world = Math::Matrix4::Identity();
79
84 tb.GetSize(), tb.GetData()));
85 } else {
86 queue->SubmitUpdateConstantBuffer(
88 }
89
90 queue->SubmitBindConstantBuffer(s_batchTransformBuffer, 0);
91}
92
93/// Submits an indexed draw for a mesh already created via CreateMesh.
94void MeshBatch::Draw(const MeshHandle& mesh, bool castsShadow) {
95 if (!mesh.IsValid()) return;
97 mesh.vertexBuffer, mesh.indexBuffer, {}, mesh.indexCount, 0, 0,
98 castsShadow);
99}
100
102 // Reserved for future use (e.g. restoring render state)
103}
104
108
109}
static const Math::Matrix4 & GetMainProjectionMatrix()
Definition Camera.hpp:107
static const Math::Matrix4 & GetMainViewMatrix()
Definition Camera.hpp:103
static Matrix< float, Rows, Rows > Identity()
Definition Matrix.hpp:203
static void Shutdown()
Release static resources before renderer cleanup.
static MeshHandle CreateMesh(VertexGroup &vertices, IndexGroup &indices)
Create GPU vertex+index buffers from CPU mesh data.
Definition MeshBatch.cpp:22
static void EndBatch()
End the batch (currently a no-op, reserved for future use).
static void BeginBatch(Material *material)
Binds the batch material and (re)uploads the shared identity-world transform buffer once per batch.
Definition MeshBatch.cpp:68
static void Draw(const MeshHandle &mesh, bool castsShadow=true)
Submits an indexed draw for a mesh already created via CreateMesh.
Definition MeshBatch.cpp:94
static RenderCommandQueue * GetInstance()
Lazily creates and returns the process-wide singleton instance.
void SubmitDrawIndexed(RefPtr< BufferBase > vertexBuffer, RefPtr< BufferBase > indexBuffer, List< RefPtr< BufferBase > > constantBuffers, uint32_t indexCount, uint32_t startIndexLocation=0, int32_t baseVertexLocation=0, bool castsShadow=true)
Queues an indexed draw with its vertex/index buffers and constant buffers.
static BufferBase * CreateBuffer(BufferType Type, uint32_t Size, void *Data)
Creates a buffer via the currently registered backend factory.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
uint32_t VertexFormatHandle
static RefPtr< RenderEngine::BufferBase > s_batchTransformBuffer
Definition MeshBatch.cpp:12
List< IndexType > IndexGroup
Definition MeshData.hpp:12
uint32_t vertexFormat
Registered custom vertex layout of this mesh; 0 means the engine default Vertex.
Definition MeshBatch.hpp:28
uint32_t indexCount
Definition MeshBatch.hpp:26
RefPtr< RenderEngine::BufferBase > vertexBuffer
Definition MeshBatch.hpp:24
RefPtr< RenderEngine::BufferBase > indexBuffer
Definition MeshBatch.hpp:25
bool IsValid() const
Definition MeshBatch.hpp:40
Per-draw world-view-projection and world matrices.
virtual uint16_t GetSize() const override
virtual void * GetData() const override