From 4f60a4a972fa76800580f8731aba54a8fe94e1fb Mon Sep 17 00:00:00 2001
From: David Gow <david@ingeniumdigital.com>
Date: Wed, 13 Aug 2014 12:45:26 +0800
Subject: [PATCH] A bunch of OpenGL debug annotations.

(Which will probably break compiling on cabellera)
---
 src/graphicsbuffer.cpp | 9 +++++++++
 src/graphicsbuffer.h   | 2 ++
 src/objectrenderer.cpp | 3 +++
 src/screen.cpp         | 9 ++++++++-
 src/shaderprogram.cpp  | 1 +
 src/view.cpp           | 7 +++++++
 6 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/src/graphicsbuffer.cpp b/src/graphicsbuffer.cpp
index da522dc..91c3478 100644
--- a/src/graphicsbuffer.cpp
+++ b/src/graphicsbuffer.cpp
@@ -74,6 +74,7 @@ GraphicsBuffer::GraphicsBuffer()
 	m_buffer_handle = 0;
 	m_buffer_usage = BufferUsageDynamicDraw;
 	m_faking_map = false;
+	m_name = "Unnamed Buffer";
 	SetUsage(BufferUsageStaticDraw);
 }
 
@@ -91,6 +92,11 @@ void GraphicsBuffer::SetType(GraphicsBuffer::BufferType bufType)
 	m_buffer_type = bufType;
 }
 
+void GraphicsBuffer::SetName(const char *name)
+{
+	m_name = name;
+}
+
 void GraphicsBuffer::SetUsage(GraphicsBuffer::BufferUsage bufUsage)
 {
 	if (bufUsage != m_buffer_usage)
@@ -125,6 +131,7 @@ bool GraphicsBuffer::RecreateBuffer(const void *data)
 		glDeleteBuffers(1, &m_buffer_handle);
 	}
 	glGenBuffers(1, &m_buffer_handle);
+	glObjectLabel(GL_BUFFER, m_buffer_handle, -1, m_name);
 	m_buffer_shape_dirty = false;
 	if (m_buffer_size)
 		Upload(m_buffer_size, data);
@@ -236,6 +243,7 @@ void GraphicsBuffer::UploadRange(size_t length, intptr_t offset, const void* dat
 
 void GraphicsBuffer::Resize(size_t length)
 {
+	glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Resizing buffer.");
 	if (m_invalidated)
 	{
 		m_buffer_size = length;
@@ -253,6 +261,7 @@ void GraphicsBuffer::Resize(size_t length)
 		glDeleteBuffers(1, &old_buffer);
 		m_buffer_size = length;
 	}
+	glPopDebugGroup();
 }
 
 void GraphicsBuffer::Bind() const
diff --git a/src/graphicsbuffer.h b/src/graphicsbuffer.h
index b102c51..4053f90 100644
--- a/src/graphicsbuffer.h
+++ b/src/graphicsbuffer.h
@@ -40,6 +40,7 @@ namespace IPDF
 		GraphicsBuffer();
 		~GraphicsBuffer();
 		
+		void SetName(const char *name);
 		void SetType(BufferType bufType);
 		void SetUsage(BufferUsage bufUsage);
 		
@@ -74,6 +75,7 @@ namespace IPDF
 		bool m_invalidated;
 		bool m_buffer_shape_dirty;
 		bool m_faking_map;
+		const char *m_name;
 	};
 
 }
diff --git a/src/objectrenderer.cpp b/src/objectrenderer.cpp
index cb96c30..998b62f 100644
--- a/src/objectrenderer.cpp
+++ b/src/objectrenderer.cpp
@@ -72,6 +72,7 @@ void ObjectRenderer::PrepareBuffers(unsigned max_objects)
 	m_ibo.Invalidate();
 	m_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
 	m_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
+	m_ibo.SetName("m_ibo: ObjectRenderer GPU indices");
 	m_ibo.Resize(max_objects * 2 * sizeof(uint32_t));
 	// BufferBuilder is used to construct the ibo
 	m_buffer_builder = new BufferBuilder<uint32_t>(m_ibo.Map(false, true, true), m_ibo.GetSize()); // new matches delete in ObjectRenderer::FinaliseBuffers
@@ -277,6 +278,7 @@ void BezierRenderer::PrepareBezierGPUBuffer(const Objects& objects)
 {
 	m_bezier_coeffs.SetType(GraphicsBuffer::BufferTypeTexture);
 	m_bezier_coeffs.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
+	m_bezier_coeffs.SetName("m_bezier_coeffs: Bezier coefficients");
 	m_bezier_coeffs.Resize(objects.beziers.size()*sizeof(GPUBezierCoeffs));
 	BufferBuilder<GPUBezierCoeffs> builder(m_bezier_coeffs.Map(false, true, true), m_bezier_coeffs.GetSize());
 
@@ -297,6 +299,7 @@ void BezierRenderer::PrepareBezierGPUBuffer(const Objects& objects)
 
 	m_bezier_ids.SetType(GraphicsBuffer::BufferTypeTexture);
 	m_bezier_ids.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
+	m_bezier_ids.SetName("m_bezier_ids: object data_indices");
 	m_bezier_ids.Upload(objects.data_indices.size() * sizeof(uint32_t), &objects.data_indices[0]);
 	
 	glGenTextures(1, &m_bezier_id_buffer_texture);
diff --git a/src/screen.cpp b/src/screen.cpp
index 3bbf008..c183bf6 100644
--- a/src/screen.cpp
+++ b/src/screen.cpp
@@ -391,11 +391,13 @@ void Screen::DebugFontInit(const char *name, float font_size)
 
 	m_debug_font_vertices.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
 	m_debug_font_vertices.SetType(GraphicsBuffer::BufferTypeVertex);
+	m_debug_font_vertices.SetName("m_debug_font_vertices");
 	m_debug_font_vertices.Upload(8192,NULL);
 	m_debug_font_vertex_head = 0;
 
 	m_debug_font_indices.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
 	m_debug_font_indices.SetType(GraphicsBuffer::BufferTypeIndex);
+	m_debug_font_indices.SetName("m_debug_font_indices");
 	m_debug_font_indices.Resize(500);
 	m_debug_font_index_head = 0;
 }
@@ -409,7 +411,7 @@ void Screen::DebugFontClear()
 
 void Screen::DebugFontFlush()
 {
-	
+	glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 40, -1, "Screen::DebugFontFlush()");	
 		
 	glEnable(GL_BLEND);
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -437,6 +439,8 @@ void Screen::DebugFontFlush()
 	m_debug_font_indices.Invalidate();
 	m_debug_font_vertex_head = 0;
 	m_debug_font_index_head = 0;
+
+	glPopDebugGroup();
 }
 
 struct fontvertex
@@ -448,6 +452,7 @@ void Screen::DebugFontPrint(const char* str)
 {
 	if (!m_debug_font_atlas) return;
 
+	glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 41, -1, "Screen::DebugFontPrint()");
 
 	BufferBuilder<fontvertex> vertexData(m_debug_font_vertices.MapRange(m_debug_font_vertex_head*sizeof(float), m_debug_font_vertices.GetSize() - m_debug_font_vertex_head*sizeof(float), false, true, true), m_debug_font_vertices.GetSize() - m_debug_font_vertex_head*sizeof(float));
 	BufferBuilder<uint16_t> indexData(m_debug_font_indices.MapRange(m_debug_font_index_head*sizeof(uint16_t), m_debug_font_indices.GetSize() - m_debug_font_index_head*sizeof(uint16_t), false, true, true), m_debug_font_indices.GetSize() - m_debug_font_index_head*sizeof(uint16_t));
@@ -460,6 +465,7 @@ void Screen::DebugFontPrint(const char* str)
 			m_debug_font_vertices.UnMap();
 			DebugFontFlush();
 			DebugFontPrint(str);
+			glPopDebugGroup();
 			return;
 		}
 		if (*str >= 32 && (unsigned char)(*str) < 128) {
@@ -492,6 +498,7 @@ void Screen::DebugFontPrint(const char* str)
 	}
 	m_debug_font_indices.UnMap();
 	m_debug_font_vertices.UnMap();
+	glPopDebugGroup();
 	//DebugFontFlush();
 }
 
diff --git a/src/shaderprogram.cpp b/src/shaderprogram.cpp
index 45f4585..68a4767 100644
--- a/src/shaderprogram.cpp
+++ b/src/shaderprogram.cpp
@@ -104,6 +104,7 @@ char * ShaderProgram::GetShaderSource(const char * src_file) const
 bool ShaderProgram::AttachShader(const char * src_file, GLenum type)
 {
 	GLuint shader_obj = glCreateShader(type);
+	glObjectLabel(GL_SHADER, shader_obj, -1, src_file);
 	char * src = GetShaderSource(src_file);
 	if (src == NULL)
 	{
diff --git a/src/view.cpp b/src/view.cpp
index 004664b..749df66 100644
--- a/src/view.cpp
+++ b/src/view.cpp
@@ -127,6 +127,7 @@ Rect View::TransformToViewCoords(const Rect& inp) const
  */
 void View::Render(int width, int height)
 {
+	glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION,42,-1, "Beginning View::Render()");
 	// View dimensions have changed (ie: Window was resized)
 	int prev_width = m_cached_display.GetWidth();
 	int prev_height = m_cached_display.GetHeight();
@@ -141,6 +142,7 @@ void View::Render(int width, int height)
 	{
 		m_cached_display.UnBind();
 		m_cached_display.Blit();
+		glPopDebugGroup();
 		return;
 	}
 	m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
@@ -191,6 +193,7 @@ void View::Render(int width, int height)
 	m_cached_display.UnBind(); // resets render target to the screen
 	m_cached_display.Blit(); // blit FrameBuffer to screen
 	m_buffer_dirty = false;
+	glPopDebugGroup();
 }
 
 #ifndef QUADTREE_DISABLED
@@ -222,6 +225,7 @@ void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int rem
 
 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
 {
+	glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
 	if (m_render_dirty) // document has changed
 		PrepareRender();
 
@@ -275,12 +279,14 @@ void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_
 			m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
 		}
 	}
+	glPopDebugGroup();
 }
 
 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
 {
 	//m_objbounds_vbo.Invalidate();
 	m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
+	m_objbounds_vbo.SetName("Object Bounds VBO");
 	if (m_use_gpu_transform)
 	{
 		m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
@@ -327,6 +333,7 @@ void View::PrepareRender()
 	m_bounds_ubo.Invalidate();
 	m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
 	m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
+	m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
 	
 	// Instead of having each ObjectRenderer go through the whole document
 	//  we initialise them, go through the document once adding to the appropriate Renderers
-- 
2.20.1