comparison Framework/Scene2D/Internals/OpenGLTextProgram.cpp @ 947:1091b2adeb5a toa2019081001

Fixed animation frame stopping when returning false + big work on the OpenGL objects to make them lost context-safe + debug code to forcefully tag a context as lost + debug macros
author Benjamin Golinvaux <bgo@osimis.io>
date Sat, 10 Aug 2019 13:07:31 +0200
parents 61ba4b504e9a
children a7351ad54960
comparison
equal deleted inserted replaced
946:dbe3e1e47019 947:1091b2adeb5a
58 namespace Internals 58 namespace Internals
59 { 59 {
60 OpenGLTextProgram::OpenGLTextProgram(OpenGL::IOpenGLContext& context) : 60 OpenGLTextProgram::OpenGLTextProgram(OpenGL::IOpenGLContext& context) :
61 context_(context) 61 context_(context)
62 { 62 {
63 63 if (!context_.IsContextLost())
64 context_.MakeCurrent(); 64 {
65 65 context_.MakeCurrent();
66 program_.reset(new OpenGL::OpenGLProgram); 66
67 program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER); 67 program_.reset(new OpenGL::OpenGLProgram(context_));
68 68 program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER);
69 positionLocation_ = program_->GetAttributeLocation("a_position"); 69
70 textureLocation_ = program_->GetAttributeLocation("a_texcoord"); 70 positionLocation_ = program_->GetAttributeLocation("a_position");
71 textureLocation_ = program_->GetAttributeLocation("a_texcoord");
72 }
71 } 73 }
72 74
73 75
74 OpenGLTextProgram::Data::Data(OpenGL::IOpenGLContext& context, 76 OpenGLTextProgram::Data::Data(OpenGL::IOpenGLContext& context,
75 const GlyphTextureAlphabet& alphabet, 77 const GlyphTextureAlphabet& alphabet,
93 } 95 }
94 else 96 else
95 { 97 {
96 coordinatesCount_ = coordinates.GetRenderingCoords().size(); 98 coordinatesCount_ = coordinates.GetRenderingCoords().size();
97 99
98 context_.MakeCurrent(); 100 if (!context_.IsContextLost())
99 glGenBuffers(2, buffers_); 101 {
100 102 context_.MakeCurrent();
101 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); 103 glGenBuffers(2, buffers_);
102 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_, 104 ORTHANC_OPENGL_CHECK("glGenBuffers");
103 &coordinates.GetRenderingCoords() [0], GL_STATIC_DRAW); 105
104 106 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
105 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); 107 ORTHANC_OPENGL_CHECK("glBindBuffer");
106 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_, 108 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_,
107 &coordinates.GetTextureCoords() [0], GL_STATIC_DRAW); 109 &coordinates.GetRenderingCoords()[0], GL_STATIC_DRAW);
110 ORTHANC_OPENGL_CHECK("glBufferData");
111
112 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
113 ORTHANC_OPENGL_CHECK("glBindBuffer");
114 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_,
115 &coordinates.GetTextureCoords()[0], GL_STATIC_DRAW);
116 ORTHANC_OPENGL_CHECK("glBufferData");
117 }
108 } 118 }
109 } 119 }
110 120
111 121
112 OpenGLTextProgram::Data::~Data() 122 OpenGLTextProgram::Data::~Data()
113 { 123 {
114 if (!IsEmpty()) 124 if (!context_.IsContextLost() && !IsEmpty())
115 { 125 {
116 context_.MakeCurrent(); 126 try
117 glDeleteBuffers(2, buffers_); 127 {
128 context_.MakeCurrent();
129 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteBuffers");
130 glDeleteBuffers(2, buffers_);
131 ORTHANC_OPENGL_CHECK("glDeleteBuffers");
132 }
133 catch (const Orthanc::OrthancException& e)
134 {
135 if (e.HasDetails())
136 {
137 LOG(ERROR) << "OrthancException in ~Data: " << e.What() << " Details: " << e.GetDetails();
138 }
139 else
140 {
141 LOG(ERROR) << "OrthancException in ~Data: " << e.What();
142 }
143 }
144 catch (const std::exception& e)
145 {
146 LOG(ERROR) << "std::exception in ~Data: " << e.what();
147 }
148 catch (...)
149 {
150 LOG(ERROR) << "Unknown exception in ~Data";
151 }
118 } 152 }
119 } 153 }
120 154
121 155
122 GLuint OpenGLTextProgram::Data::GetSceneLocationsBuffer() const 156 GLuint OpenGLTextProgram::Data::GetSceneLocationsBuffer() const
129 { 163 {
130 return buffers_[0]; 164 return buffers_[0];
131 } 165 }
132 } 166 }
133 167
134
135 GLuint OpenGLTextProgram::Data::GetTextureLocationsBuffer() const 168 GLuint OpenGLTextProgram::Data::GetTextureLocationsBuffer() const
136 { 169 {
137 if (IsEmpty()) 170 if (IsEmpty())
138 { 171 {
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 172 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
141 else 174 else
142 { 175 {
143 return buffers_[1]; 176 return buffers_[1];
144 } 177 }
145 } 178 }
146
147 179
148 void OpenGLTextProgram::Apply(OpenGL::OpenGLTexture& fontTexture, 180 void OpenGLTextProgram::Apply(OpenGL::OpenGLTexture& fontTexture,
149 const Data& data, 181 const Data& data,
150 const AffineTransform2D& transform) 182 const AffineTransform2D& transform)
151 { 183 {
152 if (!data.IsEmpty()) 184 if (!context_.IsContextLost() && !data.IsEmpty())
153 { 185 {
154 context_.MakeCurrent(); 186 context_.MakeCurrent();
155 program_->Use(); 187 program_->Use();
156 188
157 double dx, dy; // In pixels 189 double dx, dy; // In pixels