Work around a bug in the Intel driver
[ipdf/code.git] / src / view.cpp
index 0354ee9..a92da9b 100644 (file)
@@ -1,9 +1,129 @@
 #include "view.h"
+#include "bufferbuilder.h"
 
-#include "SDL_opengl.h"
+#include "gl_core44.h"
 
 using namespace IPDF;
 using namespace std;
+#define RECT_VERT \
+       "#version 140\n"\
+       "#extension GL_ARB_shading_language_420pack : require\n"\
+       "#extension GL_ARB_explicit_attrib_location : require\n"\
+       "\n"\
+       "layout(std140, binding=0) uniform ViewBounds\n"\
+       "{\n"\
+       "\tfloat bounds_x;\n"\
+       "\tfloat bounds_y;\n"\
+       "\tfloat bounds_w;\n"\
+       "\tfloat bounds_h;\n"\
+       "};\n"\
+       "\n"\
+       "layout(location = 0) in vec2 position;\n"\
+       "\n"\
+       "void main()\n"\
+       "{\n"\
+       "\tvec2 transformed_position;\n"\
+       "\ttransformed_position.x = (position.x - bounds_x) / bounds_w;\n"\
+       "\ttransformed_position.y = (position.y - bounds_y) / bounds_h;\n"\
+       "\t// Transform to clip coordinates (-1,1, -1,1).\n"\
+       "\tgl_Position.x = (transformed_position.x*2) - 1;\n"\
+       "\tgl_Position.y = 1 - (transformed_position.y*2);\n"\
+       "\tgl_Position.z = 0.0;\n"\
+       "\tgl_Position.w = 1.0;\n"\
+       "}\n"
+
+#define RECT_FRAG \
+       "#version 140\n"\
+       "\n"\
+       "out vec4 output_colour;\n"\
+       "\n"\
+       "uniform vec4 colour;\n"\
+       "\n"\
+       "void main()\n"\
+       "{\n"\
+       "\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"
+
+#define CIRCLE_FILLED_GEOM \
+       "#version 150\n"\
+       "\n"\
+       "layout(lines) in;\n"\
+       "layout(triangle_strip, max_vertices = 4) out;\n"\
+       "out vec2 objcoords;\n"\
+       "\n"\
+       "void main()\n"\
+       "{\n"\
+       "\tgl_Position = gl_in[0].gl_Position;\n"\
+       "\tobjcoords = vec2(-1.0, -1.0);\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = vec4(gl_in[0].gl_Position.x, gl_in[1].gl_Position.y, 0.0, 1.0);\n"\
+       "\tobjcoords = vec2(-1.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"\
+       "\tobjcoords = vec2(1.0, -1.0);\n"\
+       "\tEmitVertex();\n"\
+       "\tgl_Position = gl_in[1].gl_Position;\n"\
+       "\tobjcoords = vec2(1.0, 1.0);\n"\
+       "\tEmitVertex();\n"\
+       "\tEndPrimitive();\n"\
+       "}\n"
+
+#define CIRCLE_FRAG \
+       "#version 140\n"\
+       "\n"\
+       "in vec2 objcoords;\n"\
+       "out vec4 output_colour;\n"\
+       "\n"\
+       "uniform vec4 colour;\n"\
+       "\n"\
+       "void main()\n"\
+       "{\n"\
+       "\tif ((objcoords.x)*(objcoords.x) + (objcoords.y)*(objcoords.y) > 1.0)\n"\
+       "\t{\n"\
+       "\t\tdiscard;\n"\
+       "\t}\n"\
+       "\toutput_colour = colour;\n"\
+       "}\n"
 
 void View::Translate(Real x, Real y)
 {
@@ -11,17 +131,24 @@ void View::Translate(Real x, Real y)
        y *= m_bounds.h;
        m_bounds.x += x;
        m_bounds.y += y;
+       Debug("View Bounds => %s", m_bounds.Str().c_str());
+       if (!m_use_gpu_transform)
+       {
+               m_buffer_dirty = true;
+       }
+       m_bounds_dirty = true;
 }
 
 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
 {
+       // x and y are coordinates in the window
        // Convert to local coords.
        x *= m_bounds.w;
        y *= m_bounds.h;
        x += m_bounds.x;
        y += m_bounds.y;
        
-       Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
+       //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
        
        Real top = y - m_bounds.y;
        Real left = x - m_bounds.x;
@@ -33,6 +160,10 @@ void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
        m_bounds.y = y - top;
        m_bounds.w *= scaleAmt;
        m_bounds.h *= scaleAmt;
+       Debug("View Bounds => %s", m_bounds.Str().c_str());
+       if (!m_use_gpu_transform)
+               m_buffer_dirty = true;
+       m_bounds_dirty = true;
 }
 
 Rect View::TransformToViewCoords(const Rect& inp) const
@@ -48,6 +179,8 @@ Rect View::TransformToViewCoords(const Rect& inp) const
 
 void View::DrawGrid()
 {
+       //TODO: Implement this with OpenGL 3.1+
+#if 0
        // Draw some grid lines at fixed pixel positions
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
@@ -69,44 +202,106 @@ void View::DrawGrid()
                glEnd();
        
        }
+#endif
 }
 
-void View::Render()
+void View::Render(int width, int height)
 {
-       static bool debug_output_done = false;
-       if (!debug_output_done)
+       if (width != m_cached_display.GetWidth() || height != m_cached_display.GetHeight())
        {
-               m_document.DebugDumpObjects();
-               debug_output_done = true;
+               m_cached_display.Create(width, height);
+               m_bounds_dirty = true;
        }
 
-
-       //DrawGrid(); // Draw the gridlines
-
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       if (m_use_gpu_transform)
+       if (!m_bounds_dirty)
        {
-               glOrtho(Float(m_bounds.x), Float(m_bounds.x)+Float(m_bounds.w), Float(m_bounds.y) + Float(m_bounds.h), Float(m_bounds.y), -1.f, 1.f);
+               m_cached_display.UnBind();
+               m_cached_display.Blit();
+               return;
        }
-       else
+       m_cached_display.Bind();
+       m_cached_display.Clear();
+
+       if (!m_render_inited)
+               PrepareRender();
+
+       if (m_buffer_dirty)
+               UpdateObjBoundsVBO();
+
+       if (m_bounds_dirty)
        {
-               glOrtho(0,1,1,0,-1,1);
+               if (m_use_gpu_transform)
+               {
+                       GLfloat glbounds[] = {static_cast<GLfloat>(Float(m_bounds.x)), static_cast<GLfloat>(Float(m_bounds.y)), static_cast<GLfloat>(Float(m_bounds.w)), static_cast<GLfloat>(Float(m_bounds.h))};
+                       m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
+               }
+               else
+               {
+                       GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
+                       m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
+               }
        }
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
+       m_bounds_dirty = false;
 
        if (m_colour.a < 1.0f)
        {
                glEnable(GL_BLEND);
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        }
-       glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a);
-       glBegin(GL_QUADS);
+       m_objbounds_vbo.Bind();
+       m_bounds_ubo.Bind();
+       glEnableVertexAttribArray(0);
+       glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
+
+       // Filled Circles
+       m_circle_filled_shader.Use();
+       m_circle_ibo.Bind();
+       glDrawElements(GL_LINES, m_rendered_circle*2, GL_UNSIGNED_INT, 0);
+
+       // 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);
+       if (m_colour.a < 1.0f)
+       {
+               glDisable(GL_BLEND);
+       }
+       m_cached_display.UnBind();
+       m_cached_display.Blit();
+
+}
+
+struct GPUObjBounds
+{
+       float x0, y0;
+       float x1, y1;
+};
+
+void View::UpdateObjBoundsVBO()
+{
+       m_objbounds_vbo.Invalidate();
+       m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
+       if (m_use_gpu_transform)
+       {
+               m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
+       }
+       else
+       {
+               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());
+
        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)
                {
@@ -116,38 +311,88 @@ void View::Render()
                {
                        obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
                }
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
+               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);
+
        }
-       glEnd();
+       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_circle_filled_shader.AttachGeometryProgram(CIRCLE_FILLED_GEOM);
+       m_circle_filled_shader.AttachVertexProgram(RECT_VERT);
+       m_circle_filled_shader.AttachFragmentProgram(CIRCLE_FRAG);
+       m_circle_filled_shader.Link();
+       m_circle_filled_shader.Use();
+       glUniform4f(m_circle_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_circle_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
+       m_circle_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
+       m_circle_ibo.Resize(m_document.ObjectCount() * 2 * sizeof(uint32_t));
+       BufferBuilder<uint32_t> circle_builder(m_circle_ibo.Map(false, true, true), m_circle_ibo.GetSize());
+
+       m_rendered_filled = m_rendered_outline = m_rendered_circle = 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_OUTLINE)
                {
-                       obj_bounds = m_document.m_objects.bounds[id];
+                       outline_builder.Add(currentIndex++);
+                       outline_builder.Add(currentIndex++);
+                       m_rendered_outline++;
+               }
+               else if (m_document.m_objects.types[id] == RECT_FILLED)
+               {
+                       filled_builder.Add(currentIndex++);
+                       filled_builder.Add(currentIndex++);
+                       m_rendered_filled++;
                }
                else
                {
-                       obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
+                       circle_builder.Add(currentIndex++);
+                       circle_builder.Add(currentIndex++);
+                       m_rendered_circle++;
                }
-               glBegin(GL_LINE_LOOP);
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
-               glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
-               glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
-               glEnd();
-       }
 
-       if (m_colour.a < 1.0f)
-       {
-               glDisable(GL_BLEND);
        }
+       m_outline_ibo.UnMap();
+       m_filled_ibo.UnMap();
+       m_circle_ibo.UnMap();
 
+       m_render_inited = true;
 }

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