SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
BufferBase.hpp
Go to the documentation of this file.
1#ifndef _BUFFERBASE_HPP_
2#define _BUFFERBASE_HPP_
3
4#include "ResourceBase.hpp"
5#include <Core/OSDef.hpp>
6#include <cstdint>
7#include <cstring>
8
9namespace Sleak {
10 namespace RenderEngine {
11
12 /// GPU buffer usage kind, drives backend binding flags and layout.
13 enum class BufferType { Vertex = 0, Index = 1, Constant = 2, ShaderResource = 3, DepthStencil = 4, RenderTarget = 5, UnorderedAccess = 6 };
14
15 /// Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
16 class ENGINE_API BufferBase : public ResourceBase {
17 public:
18 /// Maps the buffer for CPU writes; returns false if already mapped or unmappable.
19 virtual bool Map() = 0;
20 virtual void Unmap() = 0;
21 virtual void Update() = 0;
22 /// Overwrites the buffer contents with new data of a given size.
23 virtual void Update(void* data, size_t size) = 0;
24
25 virtual void* GetData() = 0;
26
27 inline size_t GetSize() {
28 return Size;
29 }
30
31 inline BufferType GetType() { return Type; }
32
33 inline int GetSlot() const { return Slot; }
34
35 inline void SetSlot(int slot)
36 {
37 Slot = slot;
38 }
39
40 /// Registered custom vertex layout of this buffer; 0 means the engine default Vertex.
41 uint32_t GetVertexFormat() const { return m_vertexFormat; }
42 /// Tags the buffer with a VertexFormatRegistry handle so backends pick the matching pipeline.
43 void SetVertexFormat(uint32_t handle) { m_vertexFormat = handle; }
44
45 // CPU-side shadow copy for shadow pass (avoids GPU readback)
46 const void* GetCPUShadowCopy() const { return m_cpuShadowCopy; }
47 size_t GetCPUShadowCopySize() const { return m_cpuShadowCopySize; }
48 /// Copies up to 128 bytes into the inline shadow-copy storage, clamping oversized input.
49 void StoreCPUShadowCopy(const void* data, size_t size) {
50 if (size > sizeof(m_cpuShadowStorage)) size = sizeof(m_cpuShadowStorage);
51 memcpy(m_cpuShadowStorage, data, size);
54 }
55
56 protected:
58 size_t Size = 0;
59 int Slot = 0;
60 void* Data = nullptr;
61 bool bIsMapped = false;
62 uint32_t m_vertexFormat = 0;
63 // Small inline storage for transform CB shadow copy (128 bytes = 2 matrices)
64 const void* m_cpuShadowCopy = nullptr;
66 alignas(16) char m_cpuShadowStorage[128] = {};
67 };
68 };
69};
70
71#endif
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
virtual bool Map()=0
Maps the buffer for CPU writes; returns false if already mapped or unmappable.
uint32_t GetVertexFormat() const
Registered custom vertex layout of this buffer; 0 means the engine default Vertex.
const void * GetCPUShadowCopy() const
virtual void Update(void *data, size_t size)=0
Overwrites the buffer contents with new data of a given size.
void SetVertexFormat(uint32_t handle)
Tags the buffer with a VertexFormatRegistry handle so backends pick the matching pipeline.
void StoreCPUShadowCopy(const void *data, size_t size)
Copies up to 128 bytes into the inline shadow-copy storage, clamping oversized input.
Common lifecycle contract for GPU-backed resources: init, per-frame update, teardown.
Backend-facing rendering layer shared by the four graphics backends.
BufferType
GPU buffer usage kind, drives backend binding flags and layout.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10