Skip to content

Commit

Permalink
Added runtime debug setting for render batch info.
Browse files Browse the repository at this point in the history
Shows how inefficient the default renderer batcher is.
  • Loading branch information
fruxo committed Jun 15, 2013
1 parent aa3001c commit 8bbfdc4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Demo/renderers/tb_renderer_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdio.h>
#include "tb_renderer_gl.h"
#include "tb_bitmap_fragment.h"
#include "tb_system.h"

namespace tinkerbell {

Expand Down Expand Up @@ -40,6 +41,11 @@ struct Vertex {
GLuint g_current_texture = (GLuint)-1;
class Batch *g_current_batch = 0;

#ifdef TB_RUNTIME_DEBUG_INFO
uint32 dbg_begin_paint_batch_id = 0;
uint32 dbg_bitmap_validations = 0;
#endif // TB_RUNTIME_DEBUG_INFO

void BindBitmap(TBBitmap *bitmap)
{
GLuint texture = bitmap ? static_cast<TBBitmapGL*>(bitmap)->m_texture : 0;
Expand Down Expand Up @@ -95,6 +101,21 @@ void Batch::Flush()

// Flush
glDrawArrays(GL_TRIANGLES, 0, vertex_count);
#ifdef TB_RUNTIME_DEBUG_INFO
if (TB_DEBUG_SETTING(RENDER_BATCHES))
{
// Draw the triangles again using a random color based on the batch
// id. This indicates which triangles belong to the same batch.
glBindTexture(GL_TEXTURE_2D, 0);
uint32 id = batch_id - dbg_begin_paint_batch_id;
uint32 hash = id * (2166136261U ^ id);
uint32 color = 0xAA000000 + (hash & 0x00FFFFFF);
for (int i = 0; i < vertex_count; i++)
vertex[i].col = color;
glDrawArrays(GL_TRIANGLES, 0, vertex_count);
glBindTexture(GL_TEXTURE_2D, g_current_texture);
}
#endif // TB_RUNTIME_DEBUG_INFO
vertex_count = 0;

batch_id++; // Will overflow eventually, but that doesn't really matter.
Expand Down Expand Up @@ -190,6 +211,20 @@ class BatchRenderer
if (batch.vertex_count && bitmap_fragment->m_batch_id == batch.batch_id)
batch.Flush();
}
void BeginPaint()
{
#ifdef TB_RUNTIME_DEBUG_INFO
if (TB_DEBUG_SETTING(RENDER_BATCHES))
{
TBStr str;
str.SetFormatted("Last frame had %d batches and %d bitmap validations.\n",
batch.batch_id - dbg_begin_paint_batch_id, dbg_bitmap_validations);
TBDebugOut(str);
}
dbg_bitmap_validations = 0;
dbg_begin_paint_batch_id = batch.batch_id;
#endif // TB_RUNTIME_DEBUG_INFO
}
private:
Batch batch; ///< The one and only batch. (this should be improved)
};
Expand Down Expand Up @@ -236,6 +271,7 @@ void TBBitmapGL::SetData(uint32 *data)
batch.FlushBitmap(this);
BindBitmap(this);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_w, m_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
TB_IF_DEBUG_SETTING(RENDER_BATCHES, dbg_bitmap_validations++);
}

// == TBRendererGL ================================================================================
Expand All @@ -247,6 +283,8 @@ TBRendererGL::TBRendererGL()

void TBRendererGL::BeginPaint(int render_target_w, int render_target_h)
{
batch.BeginPaint();

m_screen_rect.Set(0, 0, render_target_w, render_target_h);
m_clip_rect = m_screen_rect;
g_current_texture = (GLuint)-1;
Expand Down
1 change: 1 addition & 0 deletions tinkerbell/src/tb_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DebugSettingsWindow : public TBWindow
AddCheckbox(TBDebugInfo::LAYOUT_BOUNDS, "Layout bounds");
AddCheckbox(TBDebugInfo::LAYOUT_CLIPPING, "Layout clipping");
AddCheckbox(TBDebugInfo::LAYOUT_PS_DEBUGGING, "Layout size calculation");
AddCheckbox(TBDebugInfo::RENDER_BATCHES, "Render batches");

ResizeToFitContent();
SetPosition(TBPoint((root->GetRect().w - GetRect().w) / 2,
Expand Down
3 changes: 3 additions & 0 deletions tinkerbell/src/tb_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class TBDebugInfo
/** Show highlights on widgets that recalculate their preferred
size, and those who recalculate their layout. */
LAYOUT_PS_DEBUGGING,
/** Show render batch info and log batch info in the debug
output. It depends on the renderer backend if this is available. */
RENDER_BATCHES,

NUM_SETTINGS
};
Expand Down

0 comments on commit 8bbfdc4

Please sign in to comment.