Skip to content

Commit

Permalink
Added render stats
Browse files Browse the repository at this point in the history
  • Loading branch information
L4ZZA committed Apr 17, 2020
1 parent 2bafad0 commit 87358f3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
55 changes: 32 additions & 23 deletions pyro/src/pyro/renderer/renderer_2d.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "pyro_pch.h"
#include "pyro_pch.h"
#include "renderer_2d.h"
#include "vertex_array.h"
#include "shader.h"
Expand All @@ -16,15 +16,8 @@ namespace pyro
float tiling_factor;
};

static const uint32_t s_quad_vertices = 4;
static const uint32_t s_quad_indices = 6;
struct render_2d_data
{
const uint32_t max_quads = 10000;
const uint32_t max_vertices = max_quads * s_quad_vertices;
const uint32_t max_indices = max_quads * s_quad_indices;
static const uint32_t max_texture_slots = 32;

ref<vertex_array> quad_vertex_array;
ref<vertex_buffer> quad_vertex_buffer;
ref<shader> texture_shader;
Expand All @@ -34,10 +27,12 @@ namespace pyro
quad_vertex *quad_vertex_buffer_base = nullptr;
quad_vertex *quad_vertex_buffer_ptr = nullptr;

std::array<ref<texture_2d>, max_texture_slots> texture_slots;
std::array<ref<texture_2d>, renderer_2d::max_texture_slots> texture_slots;
uint32_t texture_slot_index = 1; // 0 white texture index

glm::vec4 quad_vertex_positions[4];

renderer_2d::statistics stats;
};

static render_2d_data s_data;
Expand All @@ -49,7 +44,7 @@ void pyro::renderer_2d::init()

s_data.quad_vertex_array = vertex_array::create();

s_data.quad_vertex_buffer = vertex_buffer::create(s_data.max_vertices * sizeof(quad_vertex));
s_data.quad_vertex_buffer = vertex_buffer::create(renderer_2d::max_vertices * sizeof(quad_vertex));
s_data.quad_vertex_buffer->layout({
{e_shader_data_type::float3, "a_position"},
{e_shader_data_type::float4, "a_color"},
Expand All @@ -59,11 +54,11 @@ void pyro::renderer_2d::init()
});
s_data.quad_vertex_array->add_buffer(s_data.quad_vertex_buffer);

s_data.quad_vertex_buffer_base = new quad_vertex[s_data.max_vertices];
s_data.quad_vertex_buffer_base = new quad_vertex[renderer_2d::max_vertices];

uint32_t *quad_indices = new uint32_t[s_data.max_indices];
uint32_t *quad_indices = new uint32_t[renderer_2d::max_indices];
uint32_t offset = 0;
for (uint32_t i = 0; i < s_data.max_indices; i += s_quad_indices)
for (uint32_t i = 0; i < renderer_2d::max_indices; i += s_quad_indices)
{
quad_indices[i + 0] = offset + 0;
quad_indices[i + 1] = offset + 1;
Expand All @@ -77,7 +72,7 @@ void pyro::renderer_2d::init()
}


const ref<index_buffer> quad_ib = index_buffer::create(quad_indices, s_data.max_indices);
const ref<index_buffer> quad_ib = index_buffer::create(quad_indices, renderer_2d::max_indices);
s_data.quad_vertex_array->add_buffer(quad_ib);
delete[] quad_indices;

Expand All @@ -86,13 +81,13 @@ void pyro::renderer_2d::init()
uint32_t white_tex_data = 0xffffffff; // 2 Fs per channel
s_data.wite_texture->data(&white_tex_data, sizeof(uint32_t));

int32_t samplers[s_data.max_texture_slots];
for (uint32_t i = 0; i < s_data.max_texture_slots; i++)
int32_t samplers[renderer_2d::max_texture_slots];
for (uint32_t i = 0; i < renderer_2d::max_texture_slots; i++)
samplers[i] = i;

s_data.texture_shader = shader::create("assets/shaders/texture_2d.glsl");
s_data.texture_shader->bind();
s_data.texture_shader->set_int_array("u_textures", samplers, s_data.max_texture_slots);
s_data.texture_shader->set_int_array("u_textures", samplers, renderer_2d::max_texture_slots);

// always set texture slot 0 to full white
s_data.texture_slots[0] = s_data.wite_texture;
Expand Down Expand Up @@ -134,6 +129,7 @@ void pyro::renderer_2d::flush()
s_data.texture_slots[i]->bind(i);

render_command::draw_indexed(s_data.quad_vertex_array, s_data.quad_index_count);
s_data.stats.draw_calls++;
}

void pyro::renderer_2d::draw_quad(quad_properties const& props)
Expand All @@ -148,13 +144,15 @@ void pyro::renderer_2d::draw_quad(quad_properties const& props)

//constexpr glm::vec4 color = { 1.0f, 1.0f, 1.0f, 1.0f };

// if texture is passed as parameter
// if texture is passed as parameter

// go through all current textures
// if texture is found
// store index
// quit loop
//

// go through all current textures
// if texture is found
// store index
// quit loop
//
// TODO - textures are added even if already present in s_data.texture_slots!
float tex_index = 0.0f;
auto tex_param = props.texture.get();
for (uint32_t i = 1; i < s_data.texture_slot_index; i++)
Expand Down Expand Up @@ -213,6 +211,17 @@ void pyro::renderer_2d::draw_quad(quad_properties const& props)
s_data.quad_vertex_buffer_ptr++;

s_data.quad_index_count += s_quad_indices; // 6 indices per quad
s_data.stats.quad_count++;
}

void pyro::renderer_2d::reset_stats()
{
memset(&s_data.stats, 0, sizeof(statistics));
}

pyro::renderer_2d::statistics pyro::renderer_2d::stats()
{
return s_data.stats;
}

void pyro::renderer_2d::reset_render_data()
Expand Down
21 changes: 21 additions & 0 deletions pyro/src/pyro/renderer/renderer_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,26 @@ namespace pyro
// primitives
static void draw_quad(quad_properties const& props);

static const uint32_t s_quad_vertices = 4;
static const uint32_t s_quad_indices = 6;
static const uint32_t max_quads = 20000;
static const uint32_t max_vertices = max_quads * s_quad_vertices;
static const uint32_t max_indices = max_quads * s_quad_indices;
static const uint32_t max_texture_slots = 32;

struct statistics {

uint32_t draw_calls = 0;
uint32_t quad_count = 0;

uint32_t total_vertex_count() const { return quad_count * s_quad_vertices; }
uint32_t total_index_count() const { return quad_count * s_quad_indices; }
};

static void reset_stats();
static statistics stats();

private:
static void reset_render_data();
};
}
30 changes: 27 additions & 3 deletions sandbox/src/layer_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void layer_2d::on_update(const pyro::timestep &ts)

void layer_2d::on_imgui_render()
{
pyro::renderer_2d::reset_stats();
{
// Pre Render
PYRO_PROFILE_SCOPE("layer_2d::pre_render");
Expand Down Expand Up @@ -66,8 +67,8 @@ void layer_2d::on_imgui_render()
pyro::renderer_2d::draw_quad(props);

props.position = { 0.f, 0.f, -0.1f };
props.size = {10.f, 10.f};
props.color = {.8f, 1.f, .8f, 1.f};
props.size = {20.f, 20.f};
props.color = glm::vec4(1.f);
props.tiling_factor = 10.f;
props.texture = m_checkerboard_texture;
pyro::renderer_2d::draw_quad(props);
Expand All @@ -77,20 +78,43 @@ void layer_2d::on_imgui_render()
props.size = {1.f, 1.f};
props.rotation = rotation;
props.tiling_factor = 20.f;
props.color = { .8f, 1.f, .8f, 1.f };
pyro::renderer_2d::draw_quad(props);

for (float y = -5.0f; y < 5.0f; y += 0.5f)
{
for (float x = -5.0f; x < 5.0f; x += 0.5f)
{
pyro::quad_properties props;
props.color = { (x + 5.0f) / 10.0f, 0.4f, (y + 5.0f) / 10.0f, 0.7f };
props.size = { 0.45f, 0.45f };
props.position = { x, y, 0.f };
pyro::renderer_2d::draw_quad(props);
}
}
pyro::renderer_2d::end_scene();
}

ImGui::Begin("Settings");

ImGui::ColorEdit3("Squares color", glm::value_ptr(m_rect_color));
for(auto& result : m_profile_results)
for (auto& result : m_profile_results)
{
char label[50];
strcpy_s(label, "%.3fms ");
strcat_s(label, result.name);
ImGui::Text(label, result.time);
}
m_profile_results.clear();

auto stats = pyro::renderer_2d::stats();
ImGui::Text("-- 2D Renderer stats:");
ImGui::Text("- Draw calls: %d", stats.draw_calls);
ImGui::Text("- Quads: %d", stats.quad_count);
ImGui::Text("- Vertices: %d", stats.total_vertex_count());
ImGui::Text("- Indices: %d", stats.total_index_count());
ImGui::Text("---------------------");

ImGui::End();
}

Expand Down

0 comments on commit 87358f3

Please sign in to comment.