No more pointer arithmetic in GL/use geom shaders
authorDavid Gow <[email protected]>
Sun, 1 Jun 2014 06:41:43 +0000 (14:41 +0800)
committerDavid Gow <[email protected]>
Sun, 1 Jun 2014 06:41:43 +0000 (14:41 +0800)
bin/ipdf
src/screen.cpp
src/shaderprogram.cpp
src/shaderprogram.h
src/view.cpp
src/view.h

index 4143438..1fb8dd3 100755 (executable)
Binary files a/bin/ipdf and b/bin/ipdf differ
index 2d63bc9..a49fa08 100644 (file)
@@ -309,7 +309,7 @@ void Screen::RenderBMP(const char * filename) const
        int w = bmp->w;
        int h = bmp->h;
 
-       GLenum texture_format
+       GLenum texture_format = GL_RGBA;
        switch (bmp->format->BytesPerPixel)
        {
                case 4: //contains alpha
@@ -451,7 +451,7 @@ void Screen::DebugFontPrint(const char* str)
                        DebugFontPrint(str);
                        return;
                }
-               if (*str >= 32 && *str < 128) {
+               if (*str >= 32 && (unsigned char)(*str) < 128) {
                        stbtt_aligned_quad q;
                        stbtt_GetBakedQuad(m_debug_font_rects, 1024,1024, *str-32, &m_debug_font_x,&m_debug_font_y,&q,1);
                        size_t index = vertexData.Add({q.x0, q.y0, q.s0, q.t0});
index 7ccb970..eb7a82d 100644 (file)
@@ -1,4 +1,5 @@
 #include "shaderprogram.h"
+#include "log.h"
 
 using namespace IPDF;
 
@@ -27,6 +28,16 @@ bool ShaderProgram::AttachShader(const char *src, GLenum type)
        GLuint shader_obj = glCreateShader(type);
        glShaderSource(shader_obj, 1, &src, 0);
        glCompileShader(shader_obj);
+       int did_compile = 0;
+       glGetShaderiv(shader_obj, GL_COMPILE_STATUS, &did_compile);
+       if (!did_compile)
+       {
+               char info_log[2048];
+
+               glGetShaderInfoLog(shader_obj, 2048, nullptr, info_log);
+               Error("Shader compile error: %s\n", info_log);
+               return false;
+       }
 
        m_shaders.push_back(Shader{type, shader_obj});
        LazyCreateProgram();
@@ -44,6 +55,11 @@ bool ShaderProgram::AttachFragmentProgram(const char *src)
        return AttachShader(src, GL_FRAGMENT_SHADER);
 }
 
+bool ShaderProgram::AttachGeometryProgram(const char *src)
+{
+       return AttachShader(src, GL_GEOMETRY_SHADER);
+}
+
 bool ShaderProgram::Link()
 {
        glLinkProgram(m_program);
index 25318d7..c052566 100644 (file)
@@ -17,6 +17,7 @@ namespace IPDF
                ~ShaderProgram();
                bool AttachVertexProgram(const char *src);
                bool AttachFragmentProgram(const char *src);
+               bool AttachGeometryProgram(const char *src);
                bool Link();
                const void Use() const;
                // Unfortunately, we don't require GL 4.3/ARB_explicit_uniform_location
index db7a100..587f1ba 100644 (file)
@@ -1,4 +1,5 @@
 #include "view.h"
+#include "bufferbuilder.h"
 
 #include "gl_core44.h"
 
@@ -43,6 +44,46 @@ using namespace std;
        "\toutput_colour = colour;\n"\
        "}\n"
 
+#define RECT_OUTLINE_GEOM \
+       "#version 150\n"\
+       "\n"\
+       "layout(lines) in;\n"\
+       "layout(line_strip, max_vertices = 5) out;\n"\
+       "\n"\
+       "void main()\n"\
+       "{\n"\
+       "\tgl_Position = gl_in[0].gl_Position;\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = vec4(gl_in[0].gl_Position.x, gl_in[1].gl_Position.y, 0.0, 1.0);\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = gl_in[1].gl_Position;\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = vec4(gl_in[1].gl_Position.x, gl_in[0].gl_Position.y, 0.0, 1.0);\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = gl_in[0].gl_Position;\n"\
+       "\tEmitVertex();\n"\
+       "\tEndPrimitive();\n"\
+       "}\n"
+
+#define RECT_FILLED_GEOM \
+       "#version 150\n"\
+       "\n"\
+       "layout(lines) in;\n"\
+       "layout(triangle_strip, max_vertices = 4) out;\n"\
+       "\n"\
+       "void main()\n"\
+       "{\n"\
+       "\tgl_Position = gl_in[0].gl_Position;\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = vec4(gl_in[0].gl_Position.x, gl_in[1].gl_Position.y, 0.0, 1.0);\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = vec4(gl_in[1].gl_Position.x, gl_in[0].gl_Position.y, 0.0, 1.0);\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = gl_in[1].gl_Position;\n"\
+       "\tEmitVertex();\n"\
+       "\tEndPrimitive();\n"\
+       "}\n"
+
 void View::Translate(Real x, Real y)
 {
        x *= m_bounds.w;
@@ -140,8 +181,11 @@ void View::Render(int width, int height)
        m_cached_display.Bind();
        m_cached_display.Clear();
 
+       if (!m_render_inited)
+               PrepareRender();
+
        if (m_buffer_dirty)
-               ReRender();
+               UpdateObjBoundsVBO();
 
        if (m_bounds_dirty)
        {
@@ -163,18 +207,21 @@ void View::Render(int width, int height)
                glEnable(GL_BLEND);
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        }
-       m_vertex_buffer.Bind();
-       m_index_buffer.Bind();
+       m_objbounds_vbo.Bind();
        m_bounds_ubo.Bind();
-       m_rect_shader.Use();
-       glEnable(GL_PRIMITIVE_RESTART);
-       glPrimitiveRestartIndex(0xFFFFFFFF);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
-       glDrawElements(GL_TRIANGLE_STRIP, m_rendered_filled * 5, GL_UNSIGNED_INT, 0);
-       glDrawElements(GL_LINE_LOOP, m_rendered_outline*5, GL_UNSIGNED_INT,(void*)(sizeof(uint32_t)*m_rendered_filled*5));
+
+       // Filled Rectangles
+       m_rect_filled_shader.Use();
+       m_filled_ibo.Bind();
+       glDrawElements(GL_LINES, m_rendered_filled*2, GL_UNSIGNED_INT, 0);
+
+       // Rectangle Outlines
+       m_rect_outline_shader.Use();
+       m_outline_ibo.Bind();
+       glDrawElements(GL_LINES, m_rendered_outline*2, GL_UNSIGNED_INT, 0);
        glDisableVertexAttribArray(0);
-       glDisable(GL_PRIMITIVE_RESTART);
        if (m_colour.a < 1.0f)
        {
                glDisable(GL_BLEND);
@@ -184,55 +231,30 @@ void View::Render(int width, int height)
 
 }
 
-void View::ReRender()
+struct GPUObjBounds
 {
-       static bool debug_output_done = false;
-       if (!debug_output_done)
-       {
-               //m_document.DebugDumpObjects();
-               debug_output_done = true;
-
-               // TODO: Error check here.
-               m_rect_shader.AttachVertexProgram(RECT_VERT);
-               m_rect_shader.AttachFragmentProgram(RECT_FRAG);
-               m_rect_shader.Link();
-               m_rect_shader.Use();
-               glUniform4f(m_rect_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
-
-               m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
-               m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
-
-               m_vertex_buffer.SetType(GraphicsBuffer::BufferTypeVertex);
-               m_index_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
-               m_index_buffer.SetType(GraphicsBuffer::BufferTypeIndex);
+       float x0, y0;
+       float x1, y1;
+};
 
-               m_vertex_buffer.Upload(m_document.ObjectCount() * 8 * sizeof(float), NULL);
-               m_index_buffer.Upload(m_document.ObjectCount() * 5 * sizeof(uint32_t), NULL);
-       }
-       m_rendered_filled = m_rendered_outline = 0;
-       
+void View::UpdateObjBoundsVBO()
+{
+       m_objbounds_vbo.Invalidate();
+       m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
        if (m_use_gpu_transform)
        {
-               m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
+               m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
        }
        else
        {
-               m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
+               m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
        }
+       m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
 
+       BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.Map(false, true, true), m_objbounds_vbo.GetSize());
 
-       //DrawGrid(); // Draw the gridlines
-
-
-
-       float *vertexData = (float*)m_vertex_buffer.Map(false, true, true);
-       uint32_t *indexData = (uint32_t*)m_index_buffer.Map(false, true, true);
-
-       uint32_t currentIndex = 0;
        for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
        {
-               if (m_document.m_objects.types[id] != RECT_FILLED)
-                       continue;
                Rect obj_bounds;
                if (m_use_gpu_transform)
                {
@@ -242,60 +264,70 @@ void View::ReRender()
                {
                        obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
                }
-               *vertexData = Float(obj_bounds.x); vertexData++;
-               *vertexData = Float(obj_bounds.y); vertexData++;
-               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
-               *vertexData = Float(obj_bounds.y); vertexData++;
-               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
-               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
-               *vertexData = Float(obj_bounds.x); vertexData++;
-               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
-
-               *indexData = currentIndex; indexData++;
-               *indexData = currentIndex+1; indexData++;
-               *indexData = currentIndex+3; indexData++;
-               *indexData = currentIndex+2; indexData++;
-               *indexData = 0xFFFFFFFF; // Primitive restart.
-               indexData++;
-               currentIndex += 4;
-               m_rendered_filled++;
+               GPUObjBounds gpu_bounds = {
+                       (float)Float(obj_bounds.x),
+                       (float)Float(obj_bounds.y),
+                       (float)Float(obj_bounds.x + obj_bounds.w),
+                       (float)Float(obj_bounds.y + obj_bounds.h)
+               };
+               obj_bounds_builder.Add(gpu_bounds);
 
        }
-       
+       m_objbounds_vbo.UnMap();
+       m_buffer_dirty = false;
+}
+
+void View::PrepareRender()
+{
+       // TODO: Error check here.
+       m_rect_outline_shader.AttachGeometryProgram(RECT_OUTLINE_GEOM);
+       m_rect_outline_shader.AttachVertexProgram(RECT_VERT);
+       m_rect_outline_shader.AttachFragmentProgram(RECT_FRAG);
+       m_rect_outline_shader.Link();
+       m_rect_outline_shader.Use();
+       glUniform4f(m_rect_outline_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
+
+       m_rect_filled_shader.AttachGeometryProgram(RECT_FILLED_GEOM);
+       m_rect_filled_shader.AttachVertexProgram(RECT_VERT);
+       m_rect_filled_shader.AttachFragmentProgram(RECT_FRAG);
+       m_rect_filled_shader.Link();
+       m_rect_filled_shader.Use();
+       glUniform4f(m_rect_filled_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
+
+       m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
+       m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
+
+       m_outline_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
+       m_outline_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
+       m_outline_ibo.Resize(m_document.ObjectCount() * 2 * sizeof(uint32_t));
+       BufferBuilder<uint32_t> outline_builder(m_outline_ibo.Map(false, true, true), m_outline_ibo.GetSize()); 
+
+       m_filled_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
+       m_filled_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
+       m_filled_ibo.Resize(m_document.ObjectCount() * 2 * sizeof(uint32_t));
+       BufferBuilder<uint32_t> filled_builder(m_filled_ibo.Map(false, true, true), m_filled_ibo.GetSize());
+
+
+       m_rendered_filled = m_rendered_outline = 0;
+       uint32_t currentIndex = 0;
        for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
        {
-               if (m_document.m_objects.types[id] != RECT_OUTLINE)
-                       continue;
-               Rect obj_bounds;
-               if (m_use_gpu_transform)
+               if (m_document.m_objects.types[id] != RECT_FILLED)
                {
-                       obj_bounds = m_document.m_objects.bounds[id];
+                       outline_builder.Add(currentIndex++);
+                       outline_builder.Add(currentIndex++);
+                       m_rendered_outline++;
                }
                else
                {
-                       obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
+                       filled_builder.Add(currentIndex++);
+                       filled_builder.Add(currentIndex++);
+                       m_rendered_filled++;
                }
-               *vertexData = Float(obj_bounds.x); vertexData++;
-               *vertexData = Float(obj_bounds.y); vertexData++;
-               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
-               *vertexData = Float(obj_bounds.y); vertexData++;
-               *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
-               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
-               *vertexData = Float(obj_bounds.x); vertexData++;
-               *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
-
-               *indexData = currentIndex; indexData++;
-               *indexData = currentIndex+1; indexData++;
-               *indexData = currentIndex+2; indexData++;
-               *indexData = currentIndex+3; indexData++;
-               *indexData = 0xFFFFFFFF; // Primitive restart.
-               indexData++;
-               currentIndex += 4;
-               m_rendered_outline++;
-       }
-       m_vertex_buffer.UnMap();
-       m_index_buffer.UnMap();
 
-       m_buffer_dirty = false;
+       }
+       m_outline_ibo.UnMap();
+       m_filled_ibo.UnMap();
 
+       m_render_inited = true;
 }
index 4457e1d..344c560 100644 (file)
@@ -13,7 +13,7 @@ namespace IPDF
        {
                public:
                        View(Document & document, const Rect & bounds = Rect(0,0,1,1), const Colour & colour = Colour(0.f,0.f,0.f,1.f)) 
-                               : m_use_gpu_transform(false), m_bounds_dirty(true), m_buffer_dirty(true), m_document(document), m_bounds(bounds), m_colour(colour) 
+                               : m_use_gpu_transform(false), m_bounds_dirty(true), m_buffer_dirty(true), m_render_inited(false), m_document(document), m_bounds(bounds), m_colour(colour) 
                        {
                                Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
                        }
@@ -32,15 +32,22 @@ namespace IPDF
                        void ToggleGPUTransform() { m_use_gpu_transform = (!m_use_gpu_transform); m_bounds_dirty = true; m_buffer_dirty = true; }
                
                private:
-                       void ReRender();
+                       void PrepareRender();
+                       void UpdateObjBoundsVBO();
                        void DrawGrid();
                        bool m_use_gpu_transform;
                        bool m_bounds_dirty;
                        bool m_buffer_dirty;
-                       ShaderProgram m_rect_shader;
+                       bool m_render_inited;
+                       ShaderProgram m_rect_outline_shader;
+                       ShaderProgram m_rect_filled_shader;
+                       // Stores the view bounds.
                        GraphicsBuffer m_bounds_ubo;
-                       GraphicsBuffer m_vertex_buffer;
-                       GraphicsBuffer m_index_buffer;
+                       // Stores the bounds for _all_ objects.
+                       GraphicsBuffer m_objbounds_vbo;
+                       // Stores indices into the objbounds vbo for each type of object.
+                       GraphicsBuffer m_outline_ibo;   // Rectangle outline
+                       GraphicsBuffer m_filled_ibo;    // Filled rectangle
                        FrameBuffer m_cached_display;
                        Document & m_document;
                        Rect m_bounds;

UCC git Repository :: git.ucc.asn.au