SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
MeshBatch.cpp
Go to the documentation of this file.
1
#include <
Runtime/MeshBatch.hpp
>
2
#include "
../../include/private/Graphics/Common/ResourceManager.hpp
"
3
#include "
../../include/private/Graphics/Common/RenderCommandQueue.hpp
"
4
#include "
../../include/private/Graphics/Common/ConstantBuffer.hpp
"
5
#include "
../../include/private/Graphics/Common/BufferBase.hpp
"
6
#include <
Camera/Camera.hpp
>
7
#include <
Runtime/Material.hpp
>
8
9
namespace
Sleak
{
10
11
// Shared identity transform buffer — allocated once, reused every frame
12
static
RefPtr<RenderEngine::BufferBase>
s_batchTransformBuffer
;
13
14
MeshHandle::MeshHandle
() =
default
;
15
MeshHandle::~MeshHandle
() =
default
;
16
MeshHandle::MeshHandle
(
const
MeshHandle
&) =
default
;
17
MeshHandle::MeshHandle
(
MeshHandle
&&) noexcept = default;
18
MeshHandle
&
MeshHandle
::operator=(const
MeshHandle
&) = default;
19
MeshHandle
&
MeshHandle
::operator=(
MeshHandle
&&) noexcept = default;
20
21
/// Uploads a vertex/index group as GPU buffers and returns a handle to them.
22
MeshHandle
MeshBatch
::
CreateMesh
(
VertexGroup
& vertices,
IndexGroup
& indices) {
23
MeshHandle
h;
24
if
(vertices.GetSize() == 0 || indices.GetSize() == 0)
return
h;
25
26
h.
vertexBuffer
=
RefPtr
(
RenderEngine::ResourceManager::CreateBuffer
(
27
RenderEngine::BufferType::Vertex
,
28
static_cast<
uint32_t
>
(vertices.GetSizeInBytes()),
29
vertices.GetRawData()));
30
31
h.
indexBuffer
=
RefPtr
(
RenderEngine::ResourceManager::CreateBuffer
(
32
RenderEngine::BufferType::Index
,
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.
42
MeshHandle
MeshBatch::CreateMesh
(
VertexFormatHandle
format,
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
50
auto
* vb =
RenderEngine::ResourceManager::CreateBuffer
(
51
RenderEngine::BufferType::Vertex
,
52
static_cast<
uint32_t
>
(vertexBytes),
53
const_cast<
void
*
>
(vertexData));
54
if
(vb) vb->SetVertexFormat(format);
55
h.
vertexBuffer
=
RefPtr
(vb);
56
57
h.
indexBuffer
=
RefPtr
(
RenderEngine::ResourceManager::CreateBuffer
(
58
RenderEngine::BufferType::Index
,
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.
68
void
MeshBatch::BeginBatch
(
Material
* material) {
69
auto
* queue =
RenderEngine::RenderCommandQueue::GetInstance
();
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
();
76
RenderEngine::TransformBuffer
tb(world,
77
Camera::GetMainViewMatrix
(),
78
Camera::GetMainProjectionMatrix
());
79
80
if
(!
s_batchTransformBuffer
) {
81
s_batchTransformBuffer
=
RefPtr<RenderEngine::BufferBase>
(
82
RenderEngine::ResourceManager::CreateBuffer
(
83
RenderEngine::BufferType::Constant
,
84
tb.
GetSize
(), tb.
GetData
()));
85
}
else
{
86
queue->SubmitUpdateConstantBuffer(
87
s_batchTransformBuffer
, tb.
GetData
(), tb.
GetSize
());
88
}
89
90
queue->SubmitBindConstantBuffer(
s_batchTransformBuffer
, 0);
91
}
92
93
/// Submits an indexed draw for a mesh already created via CreateMesh.
94
void
MeshBatch::Draw
(
const
MeshHandle
& mesh,
bool
castsShadow) {
95
if
(!mesh.
IsValid
())
return
;
96
RenderEngine::RenderCommandQueue::GetInstance
()->
SubmitDrawIndexed
(
97
mesh.
vertexBuffer
, mesh.
indexBuffer
, {}, mesh.
indexCount
, 0, 0,
98
castsShadow);
99
}
100
101
void
MeshBatch::EndBatch
() {
102
// Reserved for future use (e.g. restoring render state)
103
}
104
105
void
MeshBatch::Shutdown
() {
106
s_batchTransformBuffer
.reset();
107
}
108
109
}
BufferBase.hpp
Camera.hpp
ConstantBuffer.hpp
Material.hpp
MeshBatch.hpp
RenderCommandQueue.hpp
ResourceManager.hpp
Sleak::Camera::GetMainProjectionMatrix
static const Math::Matrix4 & GetMainProjectionMatrix()
Definition
Camera.hpp:107
Sleak::Camera::GetMainViewMatrix
static const Math::Matrix4 & GetMainViewMatrix()
Definition
Camera.hpp:103
Sleak::Material
Definition
Material.hpp:90
Sleak::Math::Matrix< float, 4, 4 >::Identity
static Matrix< float, Rows, Rows > Identity()
Definition
Matrix.hpp:203
Sleak::MeshBatch
Definition
MeshBatch.hpp:96
Sleak::MeshBatch::Shutdown
static void Shutdown()
Release static resources before renderer cleanup.
Definition
MeshBatch.cpp:105
Sleak::MeshBatch::CreateMesh
static MeshHandle CreateMesh(VertexGroup &vertices, IndexGroup &indices)
Create GPU vertex+index buffers from CPU mesh data.
Definition
MeshBatch.cpp:22
Sleak::MeshBatch::EndBatch
static void EndBatch()
End the batch (currently a no-op, reserved for future use).
Definition
MeshBatch.cpp:101
Sleak::MeshBatch::BeginBatch
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
Sleak::MeshBatch::Draw
static void Draw(const MeshHandle &mesh, bool castsShadow=true)
Submits an indexed draw for a mesh already created via CreateMesh.
Definition
MeshBatch.cpp:94
Sleak::RefPtr
Definition
RefPtr.hpp:62
Sleak::RefPtr::RefPtr
friend class RefPtr
Definition
RefPtr.hpp:65
Sleak::RenderEngine::RenderCommandQueue::GetInstance
static RenderCommandQueue * GetInstance()
Lazily creates and returns the process-wide singleton instance.
Definition
RenderCommandQueue.hpp:77
Sleak::RenderEngine::RenderCommandQueue::SubmitDrawIndexed
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.
Definition
RenderCommandQueue.cpp:22
Sleak::RenderEngine::ResourceManager::CreateBuffer
static BufferBase * CreateBuffer(BufferType Type, uint32_t Size, void *Data)
Creates a buffer via the currently registered backend factory.
Definition
ResourceManager.cpp:9
Sleak::VertexGroup
Definition
MeshData.hpp:76
Sleak::RenderEngine::BufferType::Index
@ Index
Definition
BufferBase.hpp:13
Sleak::RenderEngine::BufferType::Vertex
@ Vertex
Definition
BufferBase.hpp:13
Sleak::RenderEngine::BufferType::Constant
@ Constant
Definition
BufferBase.hpp:13
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::VertexFormatHandle
uint32_t VertexFormatHandle
Definition
VertexLayout.hpp:88
Sleak::s_batchTransformBuffer
static RefPtr< RenderEngine::BufferBase > s_batchTransformBuffer
Definition
MeshBatch.cpp:12
Sleak::IndexGroup
List< IndexType > IndexGroup
Definition
MeshData.hpp:12
Sleak::MeshHandle
Definition
MeshBatch.hpp:23
Sleak::MeshHandle::MeshHandle
MeshHandle()
Sleak::MeshHandle::vertexFormat
uint32_t vertexFormat
Registered custom vertex layout of this mesh; 0 means the engine default Vertex.
Definition
MeshBatch.hpp:28
Sleak::MeshHandle::indexCount
uint32_t indexCount
Definition
MeshBatch.hpp:26
Sleak::MeshHandle::vertexBuffer
RefPtr< RenderEngine::BufferBase > vertexBuffer
Definition
MeshBatch.hpp:24
Sleak::MeshHandle::indexBuffer
RefPtr< RenderEngine::BufferBase > indexBuffer
Definition
MeshBatch.hpp:25
Sleak::MeshHandle::~MeshHandle
~MeshHandle()
Sleak::MeshHandle::IsValid
bool IsValid() const
Definition
MeshBatch.hpp:40
Sleak::RenderEngine::TransformBuffer
Per-draw world-view-projection and world matrices.
Definition
ConstantBuffer.hpp:83
Sleak::RenderEngine::TransformBuffer::GetSize
virtual uint16_t GetSize() const override
Definition
ConstantBuffer.hpp:101
Sleak::RenderEngine::TransformBuffer::GetData
virtual void * GetData() const override
Definition
ConstantBuffer.hpp:97
src
Graphics
Common
MeshBatch.cpp
Generated on
for SleakEngine by
1.18.0