./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 143
0.0%
Branches:
0 / 160
0.0%
Functions:
0 / 14
0.0%

libs/render/src/eu/render/world.cc
Line Branch Exec Source
1 #include "eu/render/world.h"
2
3 #include "eu/assert/assert.h"
4
5
6 #include "eu/core/geom.extract.h"
7 #include "eu/render/opengl_utils.h"
8 #include "eu/render/shader.h"
9 #include "eu/core/vertex_layout.h"
10
11 #include <utility>
12
13 namespace eu::render
14 {
15
16 ✗ CompiledGeom::CompiledGeom(u32 b, u32 a, u32 e, const core::CompiledGeomVertexAttributes& att, i32 tc)
17 ✗ : vbo(b)
18 ✗ , vao(a)
19 ✗ , ebo(e)
20 ✗ , number_of_triangles(tc)
21
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 9 not taken.
✗ , debug_types(att.debug_types.begin(), att.debug_types.end())
22
23 {
24 ✗ }
25
26 ✗ CompiledGeom_TransformInstance::CompiledGeom_TransformInstance(
27 u32 iv, std::size_t mi, u32 b, u32 a, u32 e, const core::CompiledGeomVertexAttributes& att, i32 tc
28 ✗ )
29 ✗ : instance_vbo(iv)
30 ✗ , max_instances(mi)
31 ✗ , vbo(b)
32 ✗ , vao(a)
33 ✗ , ebo(e)
34 ✗ , number_of_triangles(tc)
35
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 9 not taken.
✗ , debug_types(att.debug_types.begin(), att.debug_types.end())
36
37 {
38 ✗ }
39
40 ✗ std::shared_ptr<MeshInstance> make_mesh_instance(std::shared_ptr<CompiledGeom> geom, std::shared_ptr<Material> mat)
41 {
42 ✗ auto instance = std::make_shared<MeshInstance>();
43 ✗ instance->geom = std::move(geom);
44 ✗ instance->material = std::move(mat);
45 ✗ return instance;
46 }
47
48 ✗ std::shared_ptr<MeshInstance_TransformInstanced> make_mesh_instance(
49 std::shared_ptr<CompiledGeom_TransformInstance> geom, std::shared_ptr<Material> mat
50 )
51 {
52 ✗ auto instance = std::make_shared<MeshInstance_TransformInstanced>();
53 ✗ instance->geom = std::move(geom);
54 ✗ instance->material = std::move(mat);
55 ✗ return instance;
56 }
57
58 ✗ std::shared_ptr<CompiledGeom> compile_geom(DEBUG_LABEL_ARG_MANY const core::Geom& geom, const core::CompiledGeomVertexAttributes& geom_layout)
59 {
60
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 66 not taken.
✗ const auto ex = extract_geom(geom, geom_layout);
61
62
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 64 not taken.
✗ const auto vao = create_vertex_array();
63
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 64 not taken.
✗ glBindVertexArray(vao);
64
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 53 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 51 not taken.
✗ SET_DEBUG_LABEL_NAMED(vao, DebugLabelFor::vertex_array, fmt::format("VERT {}", debug_label));
65
66
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 64 not taken.
✗ const auto vbo = create_buffer();
67
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 64 not taken.
✗ glBindBuffer(GL_ARRAY_BUFFER, vbo);
68
0/4
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 57 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 55 not taken.
✗ SET_DEBUG_LABEL_NAMED(vbo, DebugLabelFor::buffer, fmt::format("ARR BUF {}", debug_label));
69
0/4
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 64 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 64 not taken.
✗ glBufferData(GL_ARRAY_BUFFER, glsizeiptr_from_sizet(ex.data.size()), ex.data.data(), GL_STATIC_DRAW);
70
71 ✗ const auto get_type = [](const core::ExtractedAttribute& extracted) -> GLenum
72 {
73
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ switch (extracted.type)
74 {
75 ✗ case core::ExtractedAttributeType::Float: return GL_FLOAT;
76
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 9 not taken.
✗ default: DIE("invalid extracted attribute"); return GL_FLOAT;
77 }
78 };
79
80 ✗ const auto stride = ex.stride;
81 ✗ int attrib_location = 0;
82 ✗ std::size_t offset = 0;
83
0/2
✗ Branch 36 → 19 not taken.
✗ Branch 36 → 37 not taken.
✗ for (const auto& att: ex.attributes)
84 {
85 ✗ constexpr auto normalize = false;
86
0/6
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 59 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 59 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 59 not taken.
✗ glVertexAttribPointer(
87 gluint_from_int(attrib_location),
88
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 59 not taken.
✗ att.count,
89 get_type(att),
90 normalize ? GL_TRUE : GL_FALSE,
91 glsizei_from_sizet(stride),
92 reinterpret_cast<void*>(offset)
93 );
94
0/4
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 59 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 59 not taken.
✗ glEnableVertexAttribArray(gluint_from_int(attrib_location));
95
96 ✗ attrib_location += 1;
97 ✗ offset += att.size;
98 }
99
100
101
0/2
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 64 not taken.
✗ const auto ebo = create_buffer();
102
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 64 not taken.
✗ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
103
0/4
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 62 not taken.
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 60 not taken.
✗ SET_DEBUG_LABEL_NAMED(ebo, DebugLabelFor::buffer, fmt::format("IND BUF {}", debug_label));
104
105
0/4
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 64 not taken.
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 64 not taken.
✗ glBufferData(
106 GL_ELEMENT_ARRAY_BUFFER,
107 ✗ glsizeiptr_from_sizet(sizeof(u32) * ex.indices.size()),
108 ✗ ex.indices.data(),
109 GL_STATIC_DRAW
110 );
111
112
0/2
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 64 not taken.
✗ return std::make_shared<CompiledGeom>(vbo, vao, ebo, geom_layout, ex.face_size);
113 ✗ }
114
115 ✗ CompiledGeom::~CompiledGeom()
116 {
117 ✗ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
118 ✗ destroy_buffer(ebo);
119
120 ✗ glBindBuffer(GL_ARRAY_BUFFER, 0);
121 ✗ destroy_buffer(vbo);
122
123 ✗ glBindVertexArray(0);
124 ✗ destroy_vertex_array(vao);
125 ✗ }
126
127 ✗ std::shared_ptr<CompiledGeom_TransformInstance> compile_geom_with_transform_instance(
128 DEBUG_LABEL_ARG_MANY
129 const core::Geom& geom, const core::CompiledGeomVertexAttributes& geom_layout, std::size_t max_instances
130 )
131 {
132
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 84 not taken.
✗ const auto ex = extract_geom(geom, geom_layout);
133
134
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 82 not taken.
✗ const auto vao = create_vertex_array();
135
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 82 not taken.
✗ glBindVertexArray(vao);
136
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 67 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 65 not taken.
✗ SET_DEBUG_LABEL_NAMED(vao, DebugLabelFor::vertex_array, fmt::format("VERT (in) {}", debug_label));
137
138
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 82 not taken.
✗ const auto vbo = create_buffer();
139
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 82 not taken.
✗ glBindBuffer(GL_ARRAY_BUFFER, vbo);
140
0/4
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 71 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 69 not taken.
✗ SET_DEBUG_LABEL_NAMED(vbo, DebugLabelFor::buffer, fmt::format("ARR BUF (in) {}", debug_label));
141
0/4
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 82 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 82 not taken.
✗ glBufferData(GL_ARRAY_BUFFER, glsizeiptr_from_sizet(ex.data.size()), ex.data.data(), GL_STATIC_DRAW);
142
143 ✗ const auto get_type = [](const core::ExtractedAttribute& extracted) -> GLenum
144 {
145
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ switch (extracted.type)
146 {
147 ✗ case core::ExtractedAttributeType::Float: return GL_FLOAT;
148
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 9 not taken.
✗ default: DIE("invalid extracted attribute"); return GL_FLOAT;
149 }
150 };
151
152 ✗ const auto stride = ex.stride;
153 ✗ int attrib_location = 0;
154 ✗ std::size_t offset = 0;
155
0/2
✗ Branch 36 → 19 not taken.
✗ Branch 36 → 37 not taken.
✗ for (const auto& att: ex.attributes)
156 {
157 ✗ constexpr auto normalize = false;
158
0/6
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 73 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 73 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 73 not taken.
✗ glVertexAttribPointer(
159 gluint_from_int(attrib_location),
160
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 73 not taken.
✗ att.count,
161 get_type(att),
162 normalize ? GL_TRUE : GL_FALSE,
163 glsizei_from_sizet(stride),
164 reinterpret_cast<void*>(offset)
165 );
166
0/4
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 73 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 73 not taken.
✗ glEnableVertexAttribArray(gluint_from_int(attrib_location));
167
168 ✗ attrib_location += 1;
169 ✗ offset += att.size;
170 }
171
172 // finally bind instance_vbo data, use a dummy data since the data will be uploaded before rendering
173 // todo(Gustav): is dynamic draw correct?
174
0/2
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 82 not taken.
✗ const auto instance_vbo = create_buffer();
175
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 82 not taken.
✗ glBindBuffer(GL_ARRAY_BUFFER, instance_vbo);
176
0/4
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 76 not taken.
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 74 not taken.
✗ SET_DEBUG_LABEL_NAMED(instance_vbo, DebugLabelFor::buffer, fmt::format("ARRAY BUF (trans in) {}", debug_label));
177 ✗ constexpr auto instance_size = sizeof(float) * 16;
178
0/4
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 82 not taken.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 82 not taken.
✗ glBufferData(GL_ARRAY_BUFFER, glsizeiptr_from_sizet(instance_size * max_instances), nullptr, GL_DYNAMIC_DRAW);
179
180
0/2
✗ Branch 50 → 45 not taken.
✗ Branch 50 → 51 not taken.
✗ for (int matrix = 0; matrix < 4; matrix += 1)
181 {
182
0/2
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 82 not taken.
✗ const auto attribute = gluint_from_int(attrib_location + matrix);
183 ✗ glVertexAttribPointer(
184 attribute,
185 4,
186 GL_FLOAT,
187 GL_FALSE,
188 instance_size,
189
0/2
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 82 not taken.
✗ reinterpret_cast<void*>(sizeof(v4) * static_cast<std::size_t>(matrix))
190 );
191
0/2
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 82 not taken.
✗ glEnableVertexAttribArray(attribute);
192
0/2
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 82 not taken.
✗ glVertexAttribDivisor(attribute, 1);
193 }
194
195
0/2
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 82 not taken.
✗ const auto ebo = create_buffer();
196
0/2
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 82 not taken.
✗ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
197
0/4
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 80 not taken.
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 78 not taken.
✗ SET_DEBUG_LABEL_NAMED(ebo, DebugLabelFor::buffer, fmt::format("IND BUF (in) {}", debug_label));
198
0/4
✗ Branch 58 → 59 not taken.
✗ Branch 58 → 82 not taken.
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 82 not taken.
✗ glBufferData(
199 GL_ELEMENT_ARRAY_BUFFER,
200 ✗ glsizeiptr_from_sizet(sizeof(u32) * ex.indices.size()),
201 ✗ ex.indices.data(),
202 GL_STATIC_DRAW
203 );
204
205 return std::make_shared<CompiledGeom_TransformInstance>(
206 instance_vbo, max_instances, vbo, vao, ebo, geom_layout, ex.face_size
207
0/2
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 82 not taken.
✗ );
208 ✗ }
209
210 ✗ LocalAxis MeshInstance::get_local_axis() const
211 {
212 // todo(Gustav): this this work with the matrix now including both rotation ADN translation? is it used anymore?
213 return {
214
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 13 not taken.
✗ .x = transform.get_transformed_vec(v3{1, 0, 0}),
215
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 12 not taken.
✗ .y = transform.get_transformed_vec(v3{0, 1, 0}),
216
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 11 not taken.
✗ .z = transform.get_transformed_vec(v3{0, 0, 1})
217 ✗ };
218 }
219
220 ✗ void render_geom(const CompiledGeom& geom)
221 {
222
0/6
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 10 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 10 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 13 not taken.
✗ ASSERT(is_bound_for_shader(geom.debug_types));
223 ✗ glBindVertexArray(geom.vao);
224 ✗ glDrawElements(GL_TRIANGLES, geom.number_of_triangles * 3, GL_UNSIGNED_INT, nullptr);
225 ✗ }
226
227 ✗ void render_geom_instanced(const MeshInstance_TransformInstanced& instanced)
228 {
229 ✗ auto* geom = instanced.geom.get();
230
0/6
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 11 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 11 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 37 not taken.
✗ ASSERT(is_bound_for_shader(geom->debug_types));
231
0/6
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 19 not taken.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 19 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 40 not taken.
✗ ASSERT(! instanced.world_from_locals.empty());
232
233
0/2
✗ Branch 35 → 20 not taken.
✗ Branch 35 → 36 not taken.
✗ for (std::size_t start_index = 0; start_index < instanced.world_from_locals.size();
234 ✗ start_index += instanced.geom->max_instances)
235 {
236 const std::size_t step_size
237 ✗ = std::min(instanced.world_from_locals.size() - start_index, instanced.geom->max_instances);
238 ✗ glBindBuffer(GL_ARRAY_BUFFER, instanced.geom->instance_vbo);
239 ✗ glBufferSubData(
240 ✗ GL_ARRAY_BUFFER, 0, glsizeiptr_from_sizet(sizeof(m4) * step_size), &instanced.world_from_locals[start_index]
241 );
242
243 ✗ glBindVertexArray(geom->vao);
244 ✗ glDrawElementsInstanced(
245 GL_TRIANGLES,
246 ✗ geom->number_of_triangles * 3,
247 GL_UNSIGNED_INT,
248 nullptr,
249 glsizei_from_sizet(instanced.world_from_locals.size())
250 );
251 }
252 ✗ }
253
254 ✗ CompiledGeom_TransformInstance::~CompiledGeom_TransformInstance()
255 {
256 ✗ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
257 ✗ destroy_buffer(ebo);
258
259 ✗ glBindBuffer(GL_ARRAY_BUFFER, 0);
260 ✗ destroy_buffer(vbo);
261 ✗ destroy_buffer(instance_vbo);
262
263 ✗ glBindVertexArray(0);
264 ✗ destroy_vertex_array(vao);
265 ✗ }
266
267 ✗ CameraVectors create_vectors(const DirectionalLight& p)
268 {
269 ✗ return create_vectors(p.rotation);
270 }
271
272 } // namespace eu::render
273