mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 09:59:44 +00:00
gpu instanced entities renderer
This commit is contained in:
parent
b985eed66a
commit
5d6446b06a
14 changed files with 492 additions and 228 deletions
20
src/graphics/core/InstanceBuffer.hpp
Normal file
20
src/graphics/core/InstanceBuffer.hpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "MeshData.hpp"
|
||||
|
||||
template <typename InstanceStructure>
|
||||
class InstanceBuffer {
|
||||
unsigned int vbo;
|
||||
size_t capacity;
|
||||
public:
|
||||
InstanceBuffer();
|
||||
~InstanceBuffer();
|
||||
|
||||
void reload(const InstanceStructure* instances, size_t count);
|
||||
|
||||
void bind(int location, size_t first) const;
|
||||
};
|
||||
|
||||
#include "graphics/core/InstanceBuffer.inl"
|
||||
55
src/graphics/core/InstanceBuffer.inl
Normal file
55
src/graphics/core/InstanceBuffer.inl
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "gl_util.hpp"
|
||||
|
||||
template <typename InstanceStructure>
|
||||
InstanceBuffer<InstanceStructure>::InstanceBuffer() : vbo(0), capacity(0) {
|
||||
static_assert(
|
||||
calc_size(InstanceStructure::ATTRIBUTES) == sizeof(InstanceStructure)
|
||||
);
|
||||
glGenBuffers(1, &vbo);
|
||||
}
|
||||
|
||||
template <typename InstanceStructure>
|
||||
InstanceBuffer<InstanceStructure>::~InstanceBuffer() {
|
||||
glDeleteBuffers(1, &vbo);
|
||||
}
|
||||
|
||||
template <typename InstanceStructure>
|
||||
void InstanceBuffer<InstanceStructure>::reload(
|
||||
const InstanceStructure* instances, size_t count
|
||||
) {
|
||||
size_t size = count * sizeof(InstanceStructure);
|
||||
if (size > capacity) {
|
||||
capacity = std::max(size, capacity * 2);
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, capacity, nullptr, GL_STREAM_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, size, instances);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
template <typename InstanceStructure>
|
||||
void InstanceBuffer<InstanceStructure>::bind(int location, size_t first) const {
|
||||
const auto& attrs = InstanceStructure::ATTRIBUTES;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
size_t offset = first * sizeof(InstanceStructure);
|
||||
for (int i = 0; attrs[i].count; i++) {
|
||||
const VertexAttribute& attr = attrs[i];
|
||||
glVertexAttribPointer(
|
||||
location + i,
|
||||
attr.count,
|
||||
gl::to_glenum(attr.type),
|
||||
attr.normalized,
|
||||
sizeof(InstanceStructure),
|
||||
(GLvoid*)offset
|
||||
);
|
||||
glVertexAttribDivisor(location + i, 1);
|
||||
glEnableVertexAttribArray(location + i);
|
||||
offset += attr.size();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
#include <vector>
|
||||
|
||||
#include "MeshData.hpp"
|
||||
#include "InstanceBuffer.hpp"
|
||||
|
||||
|
||||
struct MeshStats {
|
||||
|
|
@ -64,6 +65,13 @@ public:
|
|||
|
||||
/// @brief Draw mesh as triangles
|
||||
void draw() const;
|
||||
|
||||
template <typename InstanceStructure>
|
||||
void drawInstanced(
|
||||
const InstanceBuffer<InstanceStructure>& instances,
|
||||
size_t first,
|
||||
size_t count
|
||||
) const;
|
||||
private:
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,14 +3,6 @@
|
|||
#include "MeshData.hpp"
|
||||
#include "gl_util.hpp"
|
||||
|
||||
inline constexpr size_t calc_size(const VertexAttribute attrs[]) {
|
||||
size_t vertexSize = 0;
|
||||
for (int i = 0; attrs[i].count; i++) {
|
||||
vertexSize += attrs[i].size();
|
||||
}
|
||||
return vertexSize;
|
||||
}
|
||||
|
||||
template <typename VertexStructure>
|
||||
inline std::vector<IndexBufferData> convert_to_ibd(const MeshData<VertexStructure>& data) {
|
||||
std::vector<IndexBufferData> indices;
|
||||
|
|
@ -144,3 +136,21 @@ template <typename VertexStructure>
|
|||
void Mesh<VertexStructure>::draw() const {
|
||||
draw(GL_TRIANGLES);
|
||||
}
|
||||
|
||||
template <typename VertexStructure>
|
||||
template <typename InstanceStructure>
|
||||
void Mesh<VertexStructure>::drawInstanced(
|
||||
const InstanceBuffer<InstanceStructure>& instances,
|
||||
size_t first,
|
||||
size_t count
|
||||
) const {
|
||||
if (vertexCount == 0 || count == 0) {
|
||||
return;
|
||||
}
|
||||
MeshStats::drawCalls++;
|
||||
|
||||
glBindVertexArray(vao);
|
||||
instances.bind(count_attributes(VertexStructure::ATTRIBUTES), first);
|
||||
glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, count);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,22 @@ struct VertexAttribute {
|
|||
}
|
||||
};
|
||||
|
||||
inline constexpr size_t calc_size(const VertexAttribute attrs[]) {
|
||||
size_t vertexSize = 0;
|
||||
for (int i = 0; attrs[i].count; i++) {
|
||||
vertexSize += attrs[i].size();
|
||||
}
|
||||
return vertexSize;
|
||||
}
|
||||
|
||||
inline constexpr int count_attributes(const VertexAttribute attrs[]) {
|
||||
int count = 0;
|
||||
while (attrs[count].count) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/// @brief Raw mesh data structure
|
||||
template<typename VertexStructure>
|
||||
struct MeshData {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue