forked from maplibre/maplibre-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniform_buffer.hpp
93 lines (72 loc) · 2.91 KB
/
uniform_buffer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include <mbgl/shaders/shader_defines.hpp>
#include <memory>
#include <vector>
#include <array>
namespace mbgl {
namespace gfx {
class Context;
class UniformBuffer;
class UniformBufferArray;
class RenderPass;
using UniformBufferPtr = std::shared_ptr<UniformBuffer>;
using UniqueUniformBuffer = std::unique_ptr<UniformBuffer>;
using UniqueUniformBufferArray = std::unique_ptr<UniformBufferArray>;
class UniformBuffer {
// Can only be created by platform specific implementations
protected:
UniformBuffer(std::size_t size_)
: size(size_) {}
UniformBuffer(const UniformBuffer& other)
: size(other.size) {}
UniformBuffer(UniformBuffer&& other)
: size(other.size) {}
public:
virtual ~UniformBuffer() = default;
virtual void update(const void* data, std::size_t dataSize) = 0;
std::size_t getSize() const { return size; }
UniformBuffer& operator=(const UniformBuffer&) = delete;
protected:
UniformBuffer& operator=(UniformBuffer&& other) {
size = other.size;
return *this;
}
protected:
std::size_t size;
};
/// Stores a collection of uniform buffers by id
class UniformBufferArray {
public:
UniformBufferArray() = default;
UniformBufferArray(UniformBufferArray&&);
// Would need to use the virtual assignment operator
UniformBufferArray(const UniformBufferArray&) = delete;
virtual ~UniformBufferArray() = default;
/// Number of maximum allocated elements
std::size_t allocatedSize() const { return uniformBufferVector.size(); }
/// Get an uniform buffer element.
/// Returns a pointer to the element on success, or null if the uniform buffer doesn't exists.
const std::shared_ptr<UniformBuffer>& get(const size_t id) const;
/// Set a new uniform buffer element or replace the existing one.
virtual const std::shared_ptr<UniformBuffer>& set(const size_t id, std::shared_ptr<UniformBuffer> uniformBuffer);
/// Create and add a new buffer or update an existing one
void createOrUpdate(const size_t id, const std::vector<uint8_t>& data, gfx::Context&, bool persistent = false);
virtual void createOrUpdate(
const size_t id, const void* data, std::size_t size, gfx::Context&, bool persistent = false);
template <typename T>
void createOrUpdate(const size_t id, const T* data, gfx::Context& context, bool persistent = false)
requires(!std::is_pointer_v<T>)
{
createOrUpdate(id, data, sizeof(T), context, persistent);
}
virtual void bind(gfx::RenderPass& renderPass) = 0;
UniformBufferArray& operator=(UniformBufferArray&&);
UniformBufferArray& operator=(const UniformBufferArray&);
protected:
virtual std::unique_ptr<UniformBuffer> copy(const UniformBuffer&) = 0;
protected:
std::array<UniformBufferPtr, shaders::maxUBOCountPerShader> uniformBufferVector;
static std::shared_ptr<UniformBuffer> nullref;
};
} // namespace gfx
} // namespace mbgl