ENH: Dynamically enable and color tilings

Use multidraw and command buffer
This commit is contained in:
David Allemang
2023-02-10 13:20:34 -05:00
parent 92a55f8c74
commit 53fe3104db
7 changed files with 185 additions and 90 deletions

View File

@@ -8,6 +8,7 @@ namespace cgl {
template<class T>
class Buffer {
GLuint id{};
size_t _count = 0;
public:
using Element = T;
@@ -32,25 +33,26 @@ namespace cgl {
}
[[nodiscard]] size_t size() const {
GLint res;
glGetNamedBufferParameteriv(id, GL_BUFFER_SIZE, &res);
return (size_t) res;
return _count * sizeof(T);
}
[[nodiscard]] size_t count() const {
return size() / sizeof(T);
}
void put(const T &data, GLenum usage = GL_STATIC_DRAW) {
glNamedBufferData(id, sizeof(T), &data, usage);
return _count;
}
template<typename It>
void put(It begin, It end, GLenum usage = GL_STATIC_DRAW) {
glNamedBufferData(id, sizeof(T) * (end - begin), nullptr, usage);
_count = end - begin;
glNamedBufferData(id, sizeof(T) * _count, nullptr, usage);
void* ptr = glMapNamedBuffer(id, GL_WRITE_ONLY);
std::copy(begin, end, (T*) ptr);
glUnmapNamedBuffer(id);
}
void put(const T &data, GLenum usage = GL_STATIC_DRAW) {
T const* ptr = &data;
put(ptr, ptr + 1, usage);
}
};
}

View File

@@ -140,17 +140,53 @@ Indices<1> cell<1>(
}
template<int N>
auto hull(
const tc::Group &G
) {
struct Hull {
struct Tiling {
Eigen::Index first;
Eigen::Index count;
};
std::vector<Tiling> tilings{};
std::vector<std::vector<size_t>> subgroups{};
Indices<N> inds{};
explicit Hull(tc::Group const &G) {
std::vector<Indices<N>> parts;
for (auto H: G.subs(N - 1)) {
Eigen::Index first = 0;
for (const auto &H: G.subs(N - 1)) {
auto sub_base = cell<N>(H);
auto base = recontext(sub_base, G, H);
auto tiles = tile(base, G, H);
parts.push_back(merge(tiles));
auto tiling = merge(tiles);
subgroups.push_back(H.gens());
tilings.push_back({first, tiling.cols()});
parts.push_back(tiling);
first += tiling.cols();
}
return parts;
inds = merge(parts);
}
};
struct Points {
Eigen::Array<float, 4, Eigen::Dynamic> verts;
explicit Points(tc::Group const &G, Eigen::Vector<float, 5> root) {
auto mirrors = mirror<5>(G);
auto corners = plane_intersections(mirrors);
auto start = corners * root;
tc::Cosets table = G.solve();
tc::Path<vec5> path(table, mirrors.colwise());
Eigen::Array<float, 5, Eigen::Dynamic> higher(5, path.order());
path.walk(start, Reflect(), higher.matrix().colwise().begin());
verts = Stereo()(higher);
}
};

View File

@@ -15,10 +15,10 @@ layout(std140, binding=2) uniform ModelMatrices {
};
layout(location=0) in ivec4 inds;
layout(location=1) in vec4 col;
layout(location=1) in vec3 col;
out ivec4 vInds;
out vec4 vCol;
out vec3 vCol;
out gl_PerVertex {
vec4 gl_Position;

View File

@@ -18,10 +18,10 @@ layout(std140, binding=2) uniform ModelMatrices {
};
in ivec4 vInds[];
in vec4 vCol[];
in vec3 vCol[];
layout(location=0) out vec4 pos;
layout(location=1) out vec4 col;
layout(location=1) out vec3 col;
layout(location=2) out vec3 normal;
out gl_PerVertex {

View File

@@ -1,8 +1,7 @@
#version 430
layout(location=2) uniform vec3 col;
layout(location=0) in vec3 pos;
layout(location=1) in vec3 col;
layout(location=2) in vec3 normal;
out vec4 color;

View File

@@ -18,65 +18,93 @@
#include <shaders.hpp>
namespace vis {
struct Group {
tc::Group group;
vec5 root;
vec3 color;
template<int N>
struct Structure {
using Affine4f = Eigen::Transform<float, 4, Eigen::Affine>;
using Vertex = Eigen::Vector<float, 4>;
using Color = Eigen::Vector<float, 3>;
using Cell = Eigen::Array<unsigned int, 4, 1>;
Points points;
Hull<N> hull;
std::vector<char> enabled;
std::vector<Eigen::Vector3f> colors;
Affine4f transform = Affine4f::Identity();
template<typename P, typename H>
explicit Structure(P &&points_, H &&hull_, Color color_ = Color::Ones()):
points(std::forward<P>(points_)),
hull(std::forward<H>(hull_)),
enabled(hull.tilings.size(), true),
colors(hull.tilings.size(), color_),
transform(Affine4f::Identity()) {
}
};
struct VBOs {
struct ModelMatrix {
struct Uniform {
Eigen::Matrix4f linear;
Eigen::Vector4f translation;
};
cgl::Buffer<vec4> verts;
cgl::Buffer<Eigen::Array<unsigned int, 4, 1>> ibo;
cgl::Buffer<ModelMatrix> ubo;
using Affine4f = Eigen::Transform<float, 4, Eigen::Affine>;
Affine4f tform = Affine4f::Identity();
struct Command {
unsigned int count, instanceCount, first, baseInstance;
};
void upload_groups(entt::registry &reg) {
auto view = reg.view<const Group, VBOs>();
cgl::Buffer<Structure<4>::Vertex> vertices;
cgl::Buffer<Structure<4>::Color> colors;
cgl::Buffer<Structure<4>::Cell> indices;
cgl::Buffer<Uniform> uniform;
cgl::Buffer<Command> commands;
};
for (auto [entity, group, vbos]: view.each()) {
auto cosets = group.group.solve();
auto mirrors = mirror<5>(group.group);
auto corners = plane_intersections(mirrors);
void upload_structure(entt::registry &registry) {
auto view = registry.view<Structure<4>, VBOs>();
vec5 start = corners * group.root;
for (auto [entity, structure, vbos]: view.each()) {
auto vertices = structure.points.verts.colwise();
auto indices = structure.hull.inds.colwise();
tc::Path<vec5> path(cosets, mirrors.colwise());
Eigen::Array<float, 5, Eigen::Dynamic> higher(5, path.order());
path.walk(start, Reflect(), higher.matrix().colwise().begin());
Eigen::Array<float, 4, Eigen::Dynamic> lower = Stereo()(higher);
vbos.verts.put(lower.colwise().begin(), lower.colwise().end());
// todo generate all, then mask using glMultiDraw.
const Eigen::Index N = 4;
auto tiles = hull<N>(group.group);
tiles.erase(tiles.begin()); // remove {0, 1, 2} cells
auto inds = merge<N>(tiles);
vbos.ibo.put(inds.colwise().begin(), inds.colwise().end());
vbos.vertices.put(vertices.begin(), vertices.end());
vbos.indices.put(indices.begin(), indices.end());
}
}
void upload_ubo(entt::registry &reg) {
auto view = reg.view<VBOs>();
void upload_uniforms(entt::registry &registry) {
auto view = registry.view<Structure<4>, VBOs>();
for (auto [entity, vbos]: view.each()) {
vbos.ubo.put({vbos.tform.linear(),
vbos.tform.translation()});
for (auto [entity, structure, vbos]: view.each()) {
auto colors = structure.colors;
VBOs::Uniform uniform{
structure.transform.linear(),
structure.transform.translation(),
};
vbos.colors.put(colors.begin(), colors.end());
vbos.uniform.put(uniform, GL_STREAM_DRAW);
}
}
void upload_commands(entt::registry &registry) {
auto view = registry.view<Structure<4>, VBOs>();
for (auto [entity, structure, vbos]: view.each()) {
const auto &tilings = structure.hull.tilings;
std::vector<VBOs::Command> commands;
for (unsigned int i = 0; i < tilings.size(); ++i) {
if (structure.enabled[i]) {
auto [first, count] = tilings[i];
commands.push_back({(unsigned int) count, 1, (unsigned int) first, i});
}
}
vbos.commands.put(commands.begin(), commands.end(), GL_STREAM_DRAW);
}
}
@@ -95,25 +123,29 @@ namespace vis {
pipe.stage(solid);
vao.iformat(0, 4, GL_UNSIGNED_INT);
vao.format(1, 3, GL_FLOAT);
glVertexArrayBindingDivisor(vao, 1, 1);
}
void operator()(entt::registry &reg) {
auto view = reg.view<const Group, VBOs>();
auto view = reg.view<VBOs>();
for (auto [entity, group, vbos]: view.each()) {
for (auto [entity, vbos]: view.each()) {
const size_t N = 4;
glBindProgramPipeline(pipe);
glProgramUniform3fv(solid, 2, 1, group.color.data());
glBindBufferBase(GL_UNIFORM_BUFFER, 2, vbos.ubo);
glBindBufferBase(GL_UNIFORM_BUFFER, 2, vbos.uniform);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbos.vertices);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbos.verts);
vao.vertexBuffer(0, vbos.indices);
vao.vertexBuffer(1, vbos.colors);
vao.vertexBuffer(0, vbos.ibo);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, vbos.commands);
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, vbos.ibo.count());
glMultiDrawArraysIndirect(GL_POINTS, nullptr, vbos.commands.count(), 0);
glBindVertexArray(0);
glBindProgramPipeline(0);

View File

@@ -15,11 +15,13 @@
#include "comps.hpp"
#include "fmt/core.h"
#include "fmt/ranges.h"
#include <shaders.hpp>
#ifndef NDEBUG
#include <cgl/debug.hpp>
#include <utility>
#endif
#ifdef _WIN32
@@ -36,8 +38,8 @@ struct Matrices {
Eigen::Matrix4f proj;
Eigen::Matrix4f view;
Matrices(const Eigen::Matrix4f &proj, const Eigen::Matrix4f &view)
: proj(proj), view(view) {
Matrices(Eigen::Matrix4f proj, Eigen::Matrix4f view)
: proj(std::move(proj)), view(std::move(view)) {
}
};
@@ -106,6 +108,23 @@ void show_overlay(State &state) {
ImGui::End();
}
void show_options(entt::registry &registry) {
auto view = registry.view<vis::Structure<4>>();
for (auto [entity, structure]: view.each()) {
ImGui::Begin("Structure View Options");
for (int i = 0; i < structure.hull.tilings.size(); ++i) {
std::string label = fmt::format("{}", structure.hull.subgroups[i]);
ImGui::Checkbox(label.c_str(), (bool*) (&(structure.enabled[i])));
ImGui::ColorEdit3(label.c_str(), structure.colors[i].data(), ImGuiColorEditFlags_NoLabel);
}
ImGui::End();
}
}
void set_style() {
ImGui::StyleColorsDark();
@@ -135,16 +154,21 @@ int run(GLFWwindow* window, ImGuiContext* ctx) {
state.dimension = 4;
auto entity = registry.create();
{
// todo move symbol and root to structure
// cache and recompute cells/points on frame (only if changed) in a system.
registry.emplace<vis::Group>(
entity,
tc::schlafli({5, 3, 3, 2}),
vec5{0.80, 0.09, 0.09, 0.09, 0.09},
vec3{0.90, 0.90, 0.90}
);
tc::Group group = tc::schlafli({5, 3, 3, 2});
Points points(group, vec5{0.80, 0.3, 0.15, 0.15, 0.03});
Hull<4> hull(group);
auto& structure = registry.emplace<vis::Structure<4>>(entity, std::move(points), std::move(hull));
registry.emplace<vis::VBOs>(entity);
vis::upload_groups(registry);
structure.enabled[0] = false; // disable {0,1,2} cells
}
vis::upload_structure(registry);
auto ubo = cgl::Buffer<Matrices>();
glBindBufferBase(GL_UNIFORM_BUFFER, 1, ubo);
@@ -163,6 +187,7 @@ int run(GLFWwindow* window, ImGuiContext* ctx) {
ImGui::NewFrame();
show_overlay(state);
show_options(registry);
ImGui::Render();
int width, height;
@@ -171,10 +196,10 @@ int run(GLFWwindow* window, ImGuiContext* ctx) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ubo.put(build(window, state, ctx));
ubo.put(build(window, state, ctx), GL_STREAM_DRAW);
{
auto &tform = registry.get<vis::VBOs>(entity).tform;
auto &tform = registry.get<vis::Structure<4>>(entity).transform;
if (!io.KeysDown[GLFW_KEY_SPACE]) {
float speed = 1.0 / 8.0;
@@ -202,7 +227,8 @@ int run(GLFWwindow* window, ImGuiContext* ctx) {
tform.translation().w() = std::sin(state.time * 1.4) * 1.0;
}
vis::upload_ubo(registry);
vis::upload_commands(registry);
vis::upload_uniforms(registry);
renderer(registry);