-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinstance.h
158 lines (137 loc) · 5.4 KB
/
instance.h
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2020-2024 Sami Väisänen
// Copyright (C) 2020-2024 Ensisoft http://www.ensisoft.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "config.h"
#include <cstring> // for memcpy
#include <cstdint>
#include <vector>
#include <string>
#include <memory>
#include "base/assert.h"
#include "graphics/vertex.h"
#include "graphics/types.h"
namespace gfx
{
// A CPU buffer for (geometry) instance data, containing InstanceDataLayout
// and per geometry instance
class InstancedDrawBuffer
{
public:
// Define how the contents of the instance buffer are used
using Usage = BufferUsage;
void AddInstanceData(const void* data, size_t size)
{
ASSERT(size == mLayout.vertex_struct_size);
const auto offset = mVertexData.size();
mVertexData.resize(mVertexData.size() + size);
std::memcpy(&mVertexData[offset], data, size);
}
template<typename InstanceAttr>
void AddInstanceData(const InstanceAttr& attr)
{
AddInstanceData(&attr, sizeof(attr));
}
template<typename InstanceAttr>
void SetInstanceData(const InstanceAttr& attr, size_t index)
{
const auto byte_offset = index * mLayout.vertex_struct_size;
ASSERT(mLayout.vertex_struct_size);
ASSERT(mLayout.vertex_struct_size == sizeof(attr));
ASSERT(byte_offset + mLayout.vertex_struct_size <= mVertexData.size());
std::memcpy(&mVertexData[byte_offset], &attr, sizeof(attr));
}
void Resize(size_t count)
{
ASSERT(mLayout.vertex_struct_size);
const auto bytes = count * mLayout.vertex_struct_size;
mVertexData.resize(bytes);
}
void SetInstanceBuffer(const void* ptr, size_t bytes)
{
ASSERT(ptr && bytes);
mVertexData.resize(bytes);
std::memcpy(&mVertexData[0], ptr, bytes);
}
template<typename InstanceAttr>
void SetInstanceBuffer(const InstanceAttr* data, std::size_t count)
{ SetInstanceBuffer((const void*)data, count * sizeof(InstanceAttr)); }
template<typename InstanceAttr>
inline void SetInstanceBuffer(const std::vector<InstanceAttr>& data)
{ SetInstanceBuffer(data.data(), data.size()); }
inline void SetInstanceBuffer(std::vector<uint8_t>&& buffer) noexcept
{ mVertexData = std::move(buffer); }
inline void SetInstanceBuffer(const std::vector<uint8_t>& buffer)
{ mVertexData = buffer; }
inline void Clear()
{
mVertexData.clear();
mLayout = InstanceDataLayout{};
}
inline bool IsValid() const noexcept
{
if (mLayout.vertex_struct_size == 0)
return false;
if (mVertexData.empty())
return false;
if (mVertexData.size() % mLayout.vertex_struct_size)
return false;
return true;
}
inline bool IsEmpty() const noexcept
{ return mVertexData.empty(); }
inline size_t GetInstanceCount() const noexcept
{
ASSERT(mLayout.vertex_struct_size);
return mVertexData.size() / mLayout.vertex_struct_size;
}
inline void SetInstanceDataLayout(const InstanceDataLayout& layout)
{ mLayout = layout; }
inline size_t GetInstanceDataSize() const noexcept
{ return mVertexData.size(); }
inline const void* GetVertexDataPtr() const noexcept
{ return mVertexData.data(); }
inline const auto& GetInstanceDataLayout() const & noexcept
{ return mLayout; }
inline auto&& GetInstanceDataLayout() const && noexcept
{ return std::move(mLayout); }
private:
InstanceDataLayout mLayout;
std::vector<uint8_t> mVertexData;
};
// Per geometry instance vertex data.
class InstancedDraw
{
public:
using Usage = InstancedDrawBuffer::Usage;
struct CreateArgs {
InstancedDrawBuffer buffer;
// The expected usage of the geometry instance data.
Usage usage = Usage::Stream;
// Set the (human-readable) name of the instance geometry.
// This has debug significance only.
std::string content_name;
// Set the hash value based on the contents of the buffer.
std::size_t content_hash = 0;
};
virtual ~InstancedDraw() = default;
virtual std::size_t GetContentHash() const = 0;
virtual std::string GetContentName() const = 0;
virtual void SetContentHash(std::size_t hash) = 0;
virtual void SetContentName(std::string name) = 0;
private:
};
using InstancedDrawPtr = std::shared_ptr<const InstancedDraw>;
} // namespace