Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_uniform_buffer_object.hpp
1#pragma once
2
3#include "structure/container/spk_buffer_layout.hpp"
4#include "structure/opengl/spk_buffer_object.hpp"
5#include "structure/opengl/spk_opengl_includes.hpp"
6#include "utils/spk_opengl_utils.hpp"
7#include <cstring>
8#include <stdexcept>
9#include <type_traits>
10
11namespace spk
12{
13 namespace OpenGL
14 {
21 {
22 private:
23 GLuint _bindingPoint = 0;
24 BufferLayout _layout;
25
26 BufferLayout _initializeLayout(size_t p_size);
27 void _registerCallbacks();
28
29 public:
37
44 UniformBufferObject(GLuint p_bindingPoint, BufferObject::Usage p_usage, const size_t &p_size);
56
66 const BufferLayout &layout() const;
67
74 template <typename TType>
75 TType pull() const
76 {
77 static_assert(std::is_trivially_copyable_v<TType>, "UniformBufferObject::pull requires a trivially copyable type");
78
79 const auto rawData = spk::OpenGLUtils::readBuffer(static_cast<GLenum>(_type), static_cast<GLuint>(_id));
80 if (rawData.size() < sizeof(TType))
81 {
82 throw std::runtime_error("UniformBufferObject::pull: GPU buffer is smaller than requested type");
83 }
84
85 TType value{};
86 std::memcpy(&value, rawData.data(), sizeof(TType));
87 return value;
88 }
89
95 BufferLayout::Element &operator[](const std::wstring &p_name);
101 const BufferLayout::Element &operator[](const std::wstring &p_name) const;
102 };
103
104 using UBO = UniformBufferObject;
105 }
106}
void deactivate()
Sets the state to Deactivated.
Definition spk_activable_object.hpp:61
void activate()
Sets the state to Activated.
Definition spk_activable_object.hpp:54
bool isActive() const
Checks whether the object is currently activated.
Definition spk_activable_object.hpp:77
Node within a BufferLayout representing an object, array or leaf segment.
Definition spk_buffer_layout.hpp:65
Describes and writes typed data into a raw buffer with object/array hierarchy.
Definition spk_buffer_layout.hpp:26
spk::CachedData< GLuint > _id
Lazily created GPU buffer identifier.
Definition spk_buffer_object.hpp:91
Buffer::value_type * data()
Mutable pointer to underlying bytes.
Definition spk_buffer_object.cpp:183
Usage
Usage hints forwarded to the OpenGL driver.
Definition spk_buffer_object.hpp:61
BufferObject(const Type &p_type, const Usage &p_usage)
Constructs a buffer with target type and usage hint.
Definition spk_buffer_object.cpp:45
size_t size() const
Returns the current CPU buffer size.
Definition spk_buffer_object.cpp:168
Type _type
Buffer target selected at construction.
Definition spk_buffer_object.hpp:83
void _registerCallbacks()
Registers GPU lifecycle callbacks with the cached buffer id.
Definition spk_buffer_object.cpp:33
BufferLayout::Element & operator[](const std::wstring &p_name)
Accesses a named field within the layout.
Definition spk_uniform_buffer_object.cpp:85
UniformBufferObject & operator=(const UniformBufferObject &p_other)
Copies layout and buffer binding information from another UBO.
Definition spk_uniform_buffer_object.cpp:44
TType pull() const
Reads the entire UBO content as a trivially copyable type.
Definition spk_uniform_buffer_object.hpp:75
UniformBufferObject(GLuint p_bindingPoint, BufferObject::Usage p_usage, const size_t &p_size)
Creates a UBO bound to a binding point.
Definition spk_uniform_buffer_object.cpp:22
BufferLayout & layout()
Accesses the layout describing the UBO contents.
Definition spk_uniform_buffer_object.cpp:75
bool needsSynchronization() const noexcept
Checks if synchronization is pending.
Definition spk_synchronizable_object.hpp:32
void requestSynchronization() noexcept
Marks the object as needing synchronization.
Definition spk_synchronizable_object.hpp:23