From 834494ae5a74baa1ea6cf7ab11f669c273256e6f Mon Sep 17 00:00:00 2001 From: cxl Date: Wed, 24 Oct 2018 11:35:02 +0000 Subject: [PATCH] GLDraw: DrawGL git-svn-id: svn://ultimatepp.org/upp/trunk@12413 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/GLCtrl/GLCtrl.h | 2 + uppsrc/GLCtrl/Win32GLCtrl.cpp | 46 ++-------------------- uppsrc/GLDraw/Arc.cpp | 1 + uppsrc/GLDraw/DrawGL.cpp | 6 ++- uppsrc/GLDraw/DrawTexture.cpp | 3 ++ uppsrc/GLDraw/Ellipse.cpp | 4 +- uppsrc/GLDraw/GLPainter.h | 2 +- uppsrc/GLDraw/GLPainter.hpp | 72 +++++++++++++++++++---------------- uppsrc/GLDraw/GLTexture.cpp | 2 + uppsrc/GLDraw/Line.cpp | 2 + uppsrc/GLDraw/VertexData.cpp | 2 + 11 files changed, 64 insertions(+), 78 deletions(-) diff --git a/uppsrc/GLCtrl/GLCtrl.h b/uppsrc/GLCtrl/GLCtrl.h index eb93f92fc..c46f08312 100644 --- a/uppsrc/GLCtrl/GLCtrl.h +++ b/uppsrc/GLCtrl/GLCtrl.h @@ -155,6 +155,8 @@ public: static void SetDoubleBuffering(bool b = true) { doubleBuffering = b; } static void SetMSAA(int n = 4) { numberOfSamples = n; } + static void CreateContext(); + static Size CurrentViewport() { return current_viewport; } static void SetCurrentViewport(); // intended to restore viewport after changing it in e.g. TextureDraw diff --git a/uppsrc/GLCtrl/Win32GLCtrl.cpp b/uppsrc/GLCtrl/Win32GLCtrl.cpp index e1d10193c..1b7eac14c 100644 --- a/uppsrc/GLCtrl/Win32GLCtrl.cpp +++ b/uppsrc/GLCtrl/Win32GLCtrl.cpp @@ -11,7 +11,7 @@ static HGLRC s_openGLContext; // we only have single OpenGL cont static PIXELFORMATDESCRIPTOR s_pfd; static int s_pixelFormatID; -void MakeGLContext(HWND hwnd, int depthBits, int stencilBits, int samples) +void MakeGLContext(int depthBits, int stencilBits, int samples) { ONCELOCK { for(int pass = 0; pass < 2; pass++) { @@ -81,47 +81,9 @@ void MakeGLContext(HWND hwnd, int depthBits, int stencilBits, int samples) } } - -void InitializeGlew() +void GLCtrl::CreateContext() { - ONCELOCK { // create temporary window to initialize glew - HWND hWND = CreateWindow("UPP-CLASS-A", "Fake Window", - WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, - 0, 0, 1, 1, NULL, NULL, - NULL, NULL); - if(!hWND) - return; - HDC hDC = ::GetDC(hWND); - if(!hDC) - return; - PIXELFORMATDESCRIPTOR s_pfd; - memset(&s_pfd, 0, sizeof(s_pfd)); - s_pfd.nSize = sizeof(s_pfd); - s_pfd.nVersion = 1; - s_pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED | PFD_GENERIC_FORMAT; -// if (ctrl->doubleBuffering) s_pfd.dwFlags |= PFD_DOUBLEBUFFER; - s_pfd.iPixelType = PFD_TYPE_RGBA; - s_pfd.cColorBits = 32; - s_pfd.cAlphaBits = 8; - s_pfd.cDepthBits = 24; - s_pfd.cStencilBits = 8; - s_pfd.iLayerType = PFD_MAIN_PLANE; - int pixelFormatID = ChoosePixelFormat(hDC, &s_pfd); - - DescribePixelFormat(hDC, pixelFormatID, sizeof(PIXELFORMATDESCRIPTOR), &s_pfd); - SetPixelFormat(hDC, pixelFormatID, &s_pfd); - - HGLRC hRC = wglCreateContext(hDC); - - wglMakeCurrent(hDC, hRC); - - glewInit(); - - wglMakeCurrent(NULL, NULL); - wglDeleteContext(hRC); - ReleaseDC(hWND, hDC); - DestroyWindow(hWND); - } + MakeGLContext(depthSize, stencilSize, numberOfSamples); } void GLCtrl::GLPane::State(int reason) @@ -130,7 +92,7 @@ void GLCtrl::GLPane::State(int reason) if(reason == OPEN) { HWND hwnd = GetHWND(); - MakeGLContext(hwnd, ctrl->depthSize, ctrl->stencilSize, ctrl->numberOfSamples); + GLCtrl::CreateContext(); HDC hDC = GetDC(hwnd); if(!SetPixelFormat(hDC, s_pixelFormatID, &s_pfd)) return; diff --git a/uppsrc/GLDraw/Arc.cpp b/uppsrc/GLDraw/Arc.cpp index 162d46d41..fe62f9f78 100644 --- a/uppsrc/GLDraw/Arc.cpp +++ b/uppsrc/GLDraw/Arc.cpp @@ -4,6 +4,7 @@ namespace Upp { void GLArc(Vector>& line, const Rectf& rc, Pointf start, Pointf end) { + GL_TIMING("GLArc"); if(rc.Width() <= 0 || rc.Height() <= 0) return; Sizef radius = Sizef(rc.Size()) / 2.0; diff --git a/uppsrc/GLDraw/DrawGL.cpp b/uppsrc/GLDraw/DrawGL.cpp index 0560d8320..4067112a7 100644 --- a/uppsrc/GLDraw/DrawGL.cpp +++ b/uppsrc/GLDraw/DrawGL.cpp @@ -86,6 +86,7 @@ void DrawGL::EndOp() void DrawGL::SyncScissor() { + GL_TIMING("SyncScissor"); Rect clip = cloff.Top().clip; Size sz = clip.GetSize(); glScissor(clip.left, view_size.cy - sz.cy - clip.top, sz.cx, sz.cy); @@ -151,7 +152,7 @@ const Vector& DrawGL::GetDash(int& width) width = 1; if(width > 0) return nodash; - if(width == PEN_NULL) { + if(width == PEN_NULL || IsNull(width)) { width = 0; return nodash; } @@ -193,6 +194,7 @@ void DrawGL::DrawLineOp(int x1, int y1, int x2, int y2, int width, Color color) void DrawGL::DrawArcOp(const Rect& rc, Point start, Point end, int width, Color color) { + GL_TIMING("DrawGL::DrawArcOp"); Vector> poly; GLArc(poly, rc, start, end); ApplyDash(poly, width); @@ -217,6 +219,7 @@ void DrawGL::DoPath(Vector>& poly, const Point *pp, const Point * void DrawGL::DrawPolyPolylineOp(const Point *vertices, int vertex_count, const int *counts, int count_count, int width, Color color, Color doxor) { + GL_TIMING("DrawGL::DrawPolyPolylineOp"); if(vertex_count < 2 || IsNull(color)) return; Vector> poly; @@ -237,6 +240,7 @@ void DrawGL::DrawPolyPolyPolygonOp(const Point *vertices, int vertex_count, cons const int *disjunct_polygon_counts, int dpcc, Color color, int width, Color outline, uint64 pattern, Color doxor) { + GL_TIMING("DrawGL::DrawPolyPolyPolygonOp"); Vector> poly; while(--dpcc >= 0) { const Point *sp = vertices; diff --git a/uppsrc/GLDraw/DrawTexture.cpp b/uppsrc/GLDraw/DrawTexture.cpp index 9d6a46a95..464a3993c 100644 --- a/uppsrc/GLDraw/DrawTexture.cpp +++ b/uppsrc/GLDraw/DrawTexture.cpp @@ -21,6 +21,7 @@ void GLTextureDraw::Clear() GLTexture GLTextureDraw::GetResult() { + GL_TIMING("GLTextureDraw::GetResult"); ASSERT(texture); GLTexture t; if(msaa > 1) { @@ -70,6 +71,8 @@ GLTexture GLTextureDraw::GetResult() void GLTextureDraw::Create(Size sz_, int msaa_) { + GL_TIMING("GLTextureDraw::Create"); + sz = sz_; msaa = msaa_; diff --git a/uppsrc/GLDraw/Ellipse.cpp b/uppsrc/GLDraw/Ellipse.cpp index 15e079451..e0aba1dbb 100644 --- a/uppsrc/GLDraw/Ellipse.cpp +++ b/uppsrc/GLDraw/Ellipse.cpp @@ -5,6 +5,7 @@ namespace Upp { void GLDrawEllipse(const GLContext2D& dd, Pointf center, Sizef radius, Color fill_color, double width, Color line_color, const Vector& dash, double distance) { + GL_TIMING("Ellipse"); static GLVertexData fill, line; const int N = 200; @@ -23,8 +24,9 @@ void GLDrawEllipse(const GLContext2D& dd, Pointf center, Sizef radius, Color fil if(r.cx > 0 && r.cy > 0 && !IsNull(fill_color)) GLDrawPolygons(dd, center, fill, r, fill_color); r = radius - Sizef(width / 2, width / 2); - if(width > 0 && !IsNull(line_color)) + if(width && !IsNull(line_color)) if(dash.GetCount()) { + GL_TIMING("Ellipse Dash"); Vector line; for(int i = 0; i < N; i++) line.Add(r * Polar(i * M_2PI / N) + center); diff --git a/uppsrc/GLDraw/GLPainter.h b/uppsrc/GLDraw/GLPainter.h index fce1b57c1..90a74d72d 100644 --- a/uppsrc/GLDraw/GLPainter.h +++ b/uppsrc/GLDraw/GLPainter.h @@ -5,7 +5,7 @@ namespace Upp { struct GL_TIMING_FINISH__ { ~GL_TIMING_FINISH__() { glFinish(); } }; -#define GL_TIMING(x) GL_TIMING_FINISH__ COMBINE(sGlTiming, __LINE__); RTIMING(x) +#define GL_TIMING(x) // RTIMING(x); GL_TIMING_FINISH__ COMBINE(sGlTiming, __LINE__) #ifdef _DEBUG #define GLCHK(x) do { \ diff --git a/uppsrc/GLDraw/GLPainter.hpp b/uppsrc/GLDraw/GLPainter.hpp index ebb54371a..55d3c9e14 100644 --- a/uppsrc/GLDraw/GLPainter.hpp +++ b/uppsrc/GLDraw/GLPainter.hpp @@ -6,12 +6,15 @@ void GLPolygons(GLVertexData& mesh, const Src& polygon) mesh.Clear(); - for(const auto& p: polygon) { - int i0 = vertex.GetCount(); - for(int i = 0; i < p.GetCount(); i++) { - if(i > 1) - ndx << i0 << i0 + i - 1 << i0 + i; - vertex.Add(p[i]); + { + GL_TIMING("GLPolygons"); + for(const auto& p: polygon) { + int i0 = vertex.GetCount(); + for(int i = 0; i < p.GetCount(); i++) { + if(i > 1) + ndx << i0 << i0 + i - 1 << i0 + i; + vertex.Add(p[i]); + } } } @@ -24,34 +27,37 @@ void GLPolylines(GLVertexData& data, const Src& polygon) Vector vertex; Vector ndx; - int ii = 0; - for(const auto& p: polygon) { - int i0 = vertex.GetCount(); - int ii0 = ii; - for(int i = 0; i < p.GetCount() - 1; i++) { - Pointf p1 = p[i]; - Pointf p2 = p[i + 1 < p.GetCount() ? i + 1 : 0]; - Pointf un = p1 - p2; - - vertex << (float)p1.x << (float)p1.y << (float)un.x << (float)un.y; - vertex << (float)p1.x << (float)p1.y << -(float)un.x << -(float)un.y; - - vertex << (float)p2.x << (float)p2.y << (float)un.x << (float)un.y; - vertex << (float)p2.x << (float)p2.y << -(float)un.x << -(float)un.y; - - ndx << ii << ii + 1 << ii + 2 - << ii + 3 << ii + 2 << ii + 1; - - if(i) // if line is not first, draw bevel join between current and previous line - ndx << ii << ii + 1 << ii - 4 + 3 - << ii - 4 + 2 << ii - 4 + 3 << ii; - - ii += 4; + { + GL_TIMING("GLPolylines"); + int ii = 0; + for(const auto& p: polygon) { + int i0 = vertex.GetCount(); + int ii0 = ii; + for(int i = 0; i < p.GetCount() - 1; i++) { + Pointf p1 = p[i]; + Pointf p2 = p[i + 1 < p.GetCount() ? i + 1 : 0]; + Pointf un = p1 - p2; + + vertex << (float)p1.x << (float)p1.y << (float)un.x << (float)un.y; + vertex << (float)p1.x << (float)p1.y << -(float)un.x << -(float)un.y; + + vertex << (float)p2.x << (float)p2.y << (float)un.x << (float)un.y; + vertex << (float)p2.x << (float)p2.y << -(float)un.x << -(float)un.y; + + ndx << ii << ii + 1 << ii + 2 + << ii + 3 << ii + 2 << ii + 1; + + if(i) // if line is not first, draw bevel join between current and previous line + ndx << ii << ii + 1 << ii - 4 + 3 + << ii - 4 + 2 << ii - 4 + 3 << ii; + + ii += 4; + } + + if(p.GetCount() > 2 && p.Top() == p[0]) // Line loop is closed, draw bevel join + ndx << ii0 << ii0 + 1 << ii - 4 + 3 + << ii - 4 + 2 << ii - 4 + 3 << ii0; } - - if(p.GetCount() > 2 && p.Top() == p[0]) // Line loop is closed, draw bevel join - ndx << ii0 << ii0 + 1 << ii - 4 + 3 - << ii - 4 + 2 << ii - 4 + 3 << ii0; } data.Add(vertex, 4).Index(ndx); diff --git a/uppsrc/GLDraw/GLTexture.cpp b/uppsrc/GLDraw/GLTexture.cpp index dbfa62c07..64ee80f98 100644 --- a/uppsrc/GLDraw/GLTexture.cpp +++ b/uppsrc/GLDraw/GLTexture.cpp @@ -66,6 +66,7 @@ const GLVertexData& GLRectMesh() void GLDrawTexture(const GLContext2D& dd, const Rectf& rect, int textureid) { + GL_TIMING("GLDrawTexture"); static GLCode program(R"( #version 330 core uniform vec2 offset; @@ -115,6 +116,7 @@ void GLDrawImage(const GLContext2D& dd, const Rectf& rect, const Image& img) void GLDrawTexture(const GLContext2D& dd, const Rectf& rect, int textureid, Size tsz, const Rect& src) { + GL_TIMING("GLDrawTexture src"); static GLCode program(R"( #version 330 core uniform vec2 offset; diff --git a/uppsrc/GLDraw/Line.cpp b/uppsrc/GLDraw/Line.cpp index 4ac0ddb68..ff92bf8b1 100644 --- a/uppsrc/GLDraw/Line.cpp +++ b/uppsrc/GLDraw/Line.cpp @@ -64,6 +64,8 @@ void GLDrawPolylines(const GLContext2D& dd, Pointf at, const GLVertexData& mesh, void DashPolyline(Vector>& polyline, const Vector& line, const Vector& pattern, double distance) { + GL_TIMING("DashPolyline"); + struct LineStore : LinearPathConsumer { Vector>& polyline; diff --git a/uppsrc/GLDraw/VertexData.cpp b/uppsrc/GLDraw/VertexData.cpp index d61738d36..17ff76029 100644 --- a/uppsrc/GLDraw/VertexData.cpp +++ b/uppsrc/GLDraw/VertexData.cpp @@ -21,6 +21,7 @@ GLVertexData& GLVertexData::operator=(const GLVertexData& src) void GLVertexData::Clear() { if(data && --data->refcount == 0) { + GL_TIMING("Delete vertex data"); glDeleteVertexArrays(1, &data->VAO); glDeleteBuffers(1, &data->EBO); for(auto h : data->VBO) @@ -81,6 +82,7 @@ GLVertexData& GLVertexData::Index(const int *indices, int count) void GLVertexData::Draw(int mode) const { if(data) { + GL_TIMING("GLVertexData::Draw"); glBindVertexArray(data->VAO); glDrawElements(mode, data->elements, GL_UNSIGNED_INT, 0); glBindVertexArray(0);