SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
VertexLayout.hpp
Go to the documentation of this file.
1
#ifndef _VERTEXLAYOUT_HPP_
2
#define _VERTEXLAYOUT_HPP_
3
4
#include <
Core/OSDef.hpp
>
5
#include <cstdint>
6
#include <string>
7
#include <vector>
8
9
namespace
Sleak
{
10
11
/// Data type of one vertex attribute as seen by the vertex shader.
12
/// @ingroup vertexformats
13
enum class
VertexAttribFormat
: uint8_t {
14
Float1
,
Float2
,
Float3
,
Float4
,
15
UInt1
,
Int1
16
};
17
18
/// One attribute: shader location, component format, byte offset in the vertex.
19
/// @ingroup vertexformats
20
struct
VertexAttribute
{
21
uint32_t
location
;
22
VertexAttribFormat
format
;
23
uint32_t
offset
;
24
};
25
26
/// A complete custom vertex layout plus the shader stems that consume it.
27
/// Register once at startup; the handle keys pipelines and mesh creation.
28
/// Shader stems are consumed by the Vulkan backend; OpenGL instead binds
29
/// the layout's attributes to its own fixed shader programs.
30
///
31
/// Fill one of these when your geometry does not fit the engine's built-in
32
/// vertex type: packed voxel vertices, extra UV sets, per-vertex instance
33
/// data, anything you want to define yourself. `stride` is the size of one
34
/// vertex in bytes and each VertexAttribute gives a shader location, a
35
/// component format, and a byte offset inside that vertex.
36
///
37
/// The four shader stems name the pipeline variants the format
38
/// participates in. An empty stem skips that pass entirely, so a format
39
/// with no `shadowShaderStem` casts no shadows and a format with no
40
/// `gbufferShaderStem` is forward only. There is no fallback shader: if a
41
/// named stem fails to load, the backend logs an error and skips that pass
42
/// for this format.
43
///
44
/// @code{.cpp}
45
/// struct VoxelVertex {
46
/// float position[3];
47
/// float normal[3];
48
/// float uv[2];
49
/// uint32_t packedLight;
50
/// };
51
///
52
/// Sleak::VertexLayoutDesc desc;
53
/// desc.stride = sizeof(VoxelVertex);
54
/// desc.attributes = {
55
/// {0, Sleak::VertexAttribFormat::Float3,
56
/// offsetof(VoxelVertex, position)},
57
/// {1, Sleak::VertexAttribFormat::Float3,
58
/// offsetof(VoxelVertex, normal)},
59
/// {2, Sleak::VertexAttribFormat::Float2, offsetof(VoxelVertex, uv)},
60
/// {3, Sleak::VertexAttribFormat::UInt1,
61
/// offsetof(VoxelVertex, packedLight)},
62
/// };
63
/// desc.shaderStem = "flat_shader";
64
/// desc.shadowShaderStem = "shadow_depth_voxel";
65
/// desc.gbufferShaderStem = "gbuffer_voxel";
66
/// desc.transparentShaderStem = "water_shader";
67
///
68
/// Sleak::VertexFormatHandle handle =
69
/// Sleak::VertexFormatRegistry::Register(desc);
70
/// @endcode
71
///
72
/// Set `transparentReadsScene` for transparent geometry that refracts or
73
/// reflects what lies behind it, such as water. The first transparent draw
74
/// of the format in a frame snapshots the scene drawn so far, and its
75
/// shaders can then read it:
76
/// - Vulkan, descriptor set 3: binding 2 scene color (linear HDR),
77
/// binding 3 scene depth, binding 4 sky cubemap, binding 5 the
78
/// SceneReadUBO block below.
79
/// - OpenGL: texture unit 4 scene color (tonemapped, as shown), unit 5
80
/// scene depth, unit 6 sky cubemap, uniform block binding 4 SceneReadUBO.
81
///
82
/// @code{.glsl}
83
/// uniform SceneReadUBO {
84
/// mat4 ViewProj; // unjittered, row-major engine matrix
85
/// mat4 InvViewProj;
86
/// vec4 ScreenSize; // width, height, 1/width, 1/height
87
/// vec4 Camera; // TAA jitter in NDC (xy), near, far
88
/// vec4 Flags; // x: scene textures valid, y: sky valid
89
/// };
90
/// @endcode
91
///
92
/// Add `Camera.xy * gl_Position.w` to the clip position after the Vulkan Y
93
/// flip so the draw lines up with the jittered opaque geometry.
94
///
95
/// On Vulkan, transparent draws set the HDR scene alpha to 0. TAA reads that
96
/// as a reactive mask and trusts history less there, since animated or
97
/// refracted content is not described by the depth buffer.
98
///
99
/// @see VertexFormatRegistry, VertexAttribute, VertexAttribFormat, MeshBatch
100
/// @ingroup vertexformats
101
struct
VertexLayoutDesc
{
102
uint32_t
stride
= 0;
103
std::vector<VertexAttribute>
attributes
;
104
std::string
shaderStem
;
// forward/main variant
105
// Depth-only shadow variant; empty string skips the shadow pass.
106
std::string
shadowShaderStem
;
107
// Deferred geometry (GBuffer) variant; empty string is forward only.
108
std::string
gbufferShaderStem
;
109
// Forward transparent variant; empty string skips the transparent pass.
110
std::string
transparentShaderStem
;
111
// Transparent draws sample the opaque scene; see above.
112
bool
transparentReadsScene
=
false
;
113
};
114
115
/// 0 is reserved for the engine's default vertex layout; registered
116
/// formats start at 1.
117
using
VertexFormatHandle
= uint32_t;
118
119
/// Global registry of custom vertex layouts. Register returns a stable handle.
120
///
121
/// This is how a game teaches the renderer about its own geometry. Call
122
/// Register() once per layout during startup, keep the returned
123
/// VertexFormatHandle, and pass it to MeshBatch::CreateMesh(). The handle
124
/// travels on the resulting MeshHandle, and the backend uses it to pick
125
/// the pipeline for every pass the format participates in.
126
///
127
/// Handles are stable for the lifetime of the process and start at 1;
128
/// handle 0 is reserved for the engine's built-in vertex layout. Register()
129
/// does not validate the descriptor. A zero stride or an empty attribute
130
/// list is caught later, at pipeline creation.
131
///
132
/// Register once and reuse the handle. Registering the same descriptor
133
/// repeatedly creates a new handle and a new set of pipelines each time.
134
/// Shutdown() is for application teardown only, and pointers returned by
135
/// Get() stay valid until it runs.
136
///
137
/// @code{.cpp}
138
/// // Startup: register the format your game uses
139
/// class VoxelVertexFormat {
140
/// public:
141
/// static Sleak::VertexFormatHandle Handle() {
142
/// static Sleak::VertexFormatHandle handle = Register();
143
/// return handle;
144
/// }
145
///
146
/// private:
147
/// static Sleak::VertexFormatHandle Register() {
148
/// Sleak::VertexLayoutDesc desc;
149
/// desc.stride = sizeof(VoxelVertex);
150
/// desc.attributes = MakeVoxelAttributes();
151
/// desc.shaderStem = "flat_shader";
152
/// return Sleak::VertexFormatRegistry::Register(desc);
153
/// }
154
/// };
155
///
156
/// // Mesh creation: key the buffers to that handle
157
/// Sleak::MeshHandle mesh = Sleak::MeshBatch::CreateMesh(
158
/// VoxelVertexFormat::Handle(),
159
/// verts.data(), verts.size() * sizeof(VoxelVertex),
160
/// indices.data(), indices.size());
161
///
162
/// // Teardown
163
/// Sleak::VertexFormatRegistry::Shutdown();
164
/// @endcode
165
///
166
/// @see VertexLayoutDesc, VertexFormatHandle, MeshBatch, MeshHandle
167
/// @ingroup vertexformats
168
class
ENGINE_API
VertexFormatRegistry
{
169
public
:
170
static
VertexFormatHandle
Register
(
const
VertexLayoutDesc
& desc);
171
172
/// Returns nullptr for handle 0 or an unknown handle. The returned
173
/// pointer stays valid until Shutdown(), which is intended for
174
/// application teardown only.
175
static
const
VertexLayoutDesc
*
Get
(
VertexFormatHandle
handle);
176
177
/// Clears the registry; call only during application teardown.
178
static
void
Shutdown
();
179
};
180
181
}
// namespace Sleak
182
183
#endif
OSDef.hpp
Sleak::VertexFormatRegistry
Definition
VertexLayout.hpp:168
Sleak::VertexFormatRegistry::Register
static VertexFormatHandle Register(const VertexLayoutDesc &desc)
Definition
VertexLayout.cpp:13
Sleak::VertexFormatRegistry::Get
static const VertexLayoutDesc * Get(VertexFormatHandle handle)
Definition
VertexLayout.cpp:19
Sleak::VertexFormatRegistry::Shutdown
static void Shutdown()
Clears the registry; call only during application teardown.
Definition
VertexLayout.cpp:27
Sleak::VertexAttribFormat
VertexAttribFormat
Definition
VertexLayout.hpp:13
Sleak::VertexAttribFormat::UInt1
@ UInt1
Definition
VertexLayout.hpp:15
Sleak::VertexAttribFormat::Float4
@ Float4
Definition
VertexLayout.hpp:14
Sleak::VertexAttribFormat::Int1
@ Int1
Definition
VertexLayout.hpp:15
Sleak::VertexAttribFormat::Float1
@ Float1
Definition
VertexLayout.hpp:14
Sleak::VertexAttribFormat::Float2
@ Float2
Definition
VertexLayout.hpp:14
Sleak::VertexAttribFormat::Float3
@ Float3
Definition
VertexLayout.hpp:14
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::VertexFormatHandle
uint32_t VertexFormatHandle
Definition
VertexLayout.hpp:117
Sleak::VertexAttribute
Definition
VertexLayout.hpp:20
Sleak::VertexAttribute::location
uint32_t location
Definition
VertexLayout.hpp:21
Sleak::VertexAttribute::format
VertexAttribFormat format
Definition
VertexLayout.hpp:22
Sleak::VertexAttribute::offset
uint32_t offset
Definition
VertexLayout.hpp:23
Sleak::VertexLayoutDesc
Definition
VertexLayout.hpp:101
Sleak::VertexLayoutDesc::stride
uint32_t stride
Definition
VertexLayout.hpp:102
Sleak::VertexLayoutDesc::transparentReadsScene
bool transparentReadsScene
Definition
VertexLayout.hpp:112
Sleak::VertexLayoutDesc::transparentShaderStem
std::string transparentShaderStem
Definition
VertexLayout.hpp:110
Sleak::VertexLayoutDesc::shaderStem
std::string shaderStem
Definition
VertexLayout.hpp:104
Sleak::VertexLayoutDesc::gbufferShaderStem
std::string gbufferShaderStem
Definition
VertexLayout.hpp:108
Sleak::VertexLayoutDesc::shadowShaderStem
std::string shadowShaderStem
Definition
VertexLayout.hpp:106
Sleak::VertexLayoutDesc::attributes
std::vector< VertexAttribute > attributes
Definition
VertexLayout.hpp:103
include
public
Runtime
VertexLayout.hpp
Generated on
for SleakEngine by
1.18.0