Skip to content

Commit

Permalink
renderer: Use vector of vectors for storing renderables.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed May 9, 2024
1 parent ac1be0f commit 52663d6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 74 deletions.
46 changes: 7 additions & 39 deletions libopenage/renderer/opengl/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,19 @@ void GlRenderer::render(const std::shared_ptr<RenderPass> &pass) {
// GlRenderer::optimize(gl_pass);

// render all objects in the pass
auto &layers = gl_pass->get_layers();
auto &renderables = gl_pass->get_renderables();
const auto &layers = gl_pass->get_layers();
const auto &renderables = gl_pass->get_renderables();

// Draw by layers
size_t offset = 0;
size_t next_offset = 0;
for (auto const &layer : layers) {
for (size_t i = 0; i < layers.size(); i++) {
const auto &layer = layers[i];
const auto &objects = renderables[i];

if (layer.clear_depth) {
glClear(GL_DEPTH_BUFFER_BIT);
}

next_offset = offset + layer.length;
for (size_t i = offset; i < next_offset; i++) {
const auto &obj = renderables[i];
for (auto const &obj : objects) {
if (obj.alpha_blending) {
glEnable(GL_BLEND);
}
Expand Down Expand Up @@ -221,38 +220,7 @@ void GlRenderer::render(const std::shared_ptr<RenderPass> &pass) {
geom->draw();
}
}

offset = next_offset;
}

// for (auto const &obj : gl_pass->get_renderables()) {
// if (obj.alpha_blending) {
// glEnable(GL_BLEND);
// }
// else {
// glDisable(GL_BLEND);
// }

// if (obj.depth_test) {
// glEnable(GL_DEPTH_TEST);
// }
// else {
// glDisable(GL_DEPTH_TEST);
// }

// auto in = std::dynamic_pointer_cast<GlUniformInput>(obj.uniform);
// auto program = std::static_pointer_cast<GlShaderProgram>(in->get_program());

// // this also calls program->use()
// program->update_uniforms(in);

// // draw the geometry
// if (obj.geometry != nullptr) {
// auto geom = std::dynamic_pointer_cast<GlGeometry>(obj.geometry);
// // TODO read obj.blend + family
// geom->draw();
// }
// }
}

} // namespace openage::renderer::opengl
48 changes: 18 additions & 30 deletions libopenage/renderer/render_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RenderPass::RenderPass(std::vector<Renderable> &&renderables,
log::log(MSG(dbg) << "Created render pass");
}

const std::vector<Renderable> &RenderPass::get_renderables() const {
const std::vector<std::vector<Renderable>> &RenderPass::get_renderables() const {
return this->renderables;
}

Expand All @@ -43,24 +43,20 @@ void RenderPass::set_renderables(std::vector<Renderable> &&renderables) {
}

void RenderPass::add_renderables(std::vector<Renderable> &&renderables, int64_t priority) {
// Insertion index for the renderables
size_t renderables_index = 0;
if (priority == LAYER_PRIORITY_MAX) {
// Add the renderables to the last (default) layer
this->renderables.back().insert(this->renderables.back().end(),
std::make_move_iterator(renderables.begin()),
std::make_move_iterator(renderables.end()));
return;
}

// Index of the layer where the renderables will be inserted
size_t layer_index = 0;

// Priority of the last observed layer
int64_t current_priority = LAYER_PRIORITY_MAX;

if (priority == LAYER_PRIORITY_MAX) {
// Add the renderables to the last (default) layer
this->renderables.insert(this->renderables.end(),
std::make_move_iterator(renderables.begin()),
std::make_move_iterator(renderables.end()));
this->layers.back().length += renderables.size();
return;
}

// Find the index in renderables to insert the renderables
for (size_t i = 0; i < this->layers.size(); i++) {
auto &layer = this->layers.at(i);
Expand All @@ -69,22 +65,18 @@ void RenderPass::add_renderables(std::vector<Renderable> &&renderables, int64_t
// Insert the renderables directly before this layer
break;
}
renderables_index += layer.length;
current_priority = layer.priority;
layer_index = i;
}

this->renderables.insert(this->renderables.begin() + renderables_index,
std::make_move_iterator(renderables.begin()),
std::make_move_iterator(renderables.end()));

if (current_priority != priority) {
// Lazily add a new layer with the desired priority
this->add_layer(layer_index, priority);
}

// Update the length of the layer with the number of added renderables
this->layers.at(layer_index).length += renderables.size();
this->renderables[layer_index].insert(this->renderables[layer_index].end(),
std::make_move_iterator(renderables.begin()),
std::make_move_iterator(renderables.end()));
}

void RenderPass::add_renderables(Renderable &&renderable, int64_t priority) {
Expand All @@ -104,26 +96,22 @@ void RenderPass::add_layer(int64_t priority, bool clear_depth) {
}

void RenderPass::add_layer(size_t index, int64_t priority, bool clear_depth) {
this->layers.insert(this->layers.begin() + index, Layer{priority, 0, clear_depth});
this->layers.insert(this->layers.begin() + index, Layer{priority, clear_depth});
this->renderables.insert(this->renderables.begin() + index, std::vector<Renderable>{});
}

void RenderPass::clear_renderables() {
// Erase all renderables
this->renderables.clear();

// Keep layer definitions, but reset the length of each layer to 0
for (auto &layer : this->layers) {
layer.length = 0;
for (size_t i = 0; i < this->layers.size(); i++) {
this->renderables[i].clear();
}
}

void RenderPass::sort(const compare_func &compare) {
size_t offset = 0;
for (auto &layer : this->layers) {
std::stable_sort(this->renderables.begin() + offset,
this->renderables.begin() + offset + layer.length,
for (size_t i = 0; i < this->layers.size(); i++) {
std::stable_sort(this->renderables[i].begin(),
this->renderables[i].end(),
compare);
offset += layer.length;
}
}

Expand Down
8 changes: 3 additions & 5 deletions libopenage/renderer/render_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ class RenderTarget;
* // not need layers at all.
*/
struct Layer {
/// Priority of the renderables in this slice.
/// Priority of the renderables.
int64_t priority;
/// Number of renderables in this slice.
size_t length;
/// Whether to clear the depth buffer before rendering this layer.
bool clear_depth = true;
};
Expand All @@ -44,7 +42,7 @@ class RenderPass {
*
* @return Renderables of the render pass.
*/
const std::vector<Renderable> &get_renderables() const;
const std::vector<std::vector<Renderable>> &get_renderables() const;

/**
* Get the layers of the render pass.
Expand Down Expand Up @@ -131,7 +129,7 @@ class RenderPass {
*
* Kept sorted by layer priorities (lowest to highest priority).
*/
std::vector<Renderable> renderables;
std::vector<std::vector<Renderable>> renderables;

private:
/**
Expand Down

0 comments on commit 52663d6

Please sign in to comment.