comparison Framework/Scene2D/Internals/OpenGLTextureProgram.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 e3f21a265be5
children 2d8ab34c8c91
comparison
equal deleted inserted replaced
946:dbe3e1e47019 947:1091b2adeb5a
20 20
21 21
22 #include "OpenGLTextureProgram.h" 22 #include "OpenGLTextureProgram.h"
23 #include "OpenGLShaderVersionDirective.h" 23 #include "OpenGLShaderVersionDirective.h"
24 24
25 #include <Core/OrthancException.h>
26
25 static const unsigned int COMPONENTS = 2; 27 static const unsigned int COMPONENTS = 2;
26 static const unsigned int COUNT = 6; // 2 triangles in 2D 28 static const unsigned int COUNT = 6; // 2 triangles in 2D
27 29
28 static const char* VERTEX_SHADER = 30 static const char* VERTEX_SHADER =
29 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE 31 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE
43 namespace Internals 45 namespace Internals
44 { 46 {
45 void OpenGLTextureProgram::InitializeExecution(OpenGL::OpenGLTexture& texture, 47 void OpenGLTextureProgram::InitializeExecution(OpenGL::OpenGLTexture& texture,
46 const AffineTransform2D& transform) 48 const AffineTransform2D& transform)
47 { 49 {
48 context_.MakeCurrent(); 50 if (!context_.IsContextLost())
49 program_->Use(); 51 {
52 context_.MakeCurrent();
53 program_->Use();
50 54
51 AffineTransform2D scale = AffineTransform2D::CreateScaling 55 AffineTransform2D scale = AffineTransform2D::CreateScaling
52 (texture.GetWidth(), texture.GetHeight()); 56 (texture.GetWidth(), texture.GetHeight());
53 57
54 AffineTransform2D t = AffineTransform2D::Combine(transform, scale); 58 AffineTransform2D t = AffineTransform2D::Combine(transform, scale);
55 59
56 float m[16]; 60 float m[16];
57 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight()); 61 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight());
58 62
59 texture.Bind(program_->GetUniformLocation("u_texture")); 63 texture.Bind(program_->GetUniformLocation("u_texture"));
60 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m); 64 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m);
61 65
62 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); 66 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
63 glEnableVertexAttribArray(positionLocation_); 67 glEnableVertexAttribArray(positionLocation_);
64 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); 68 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
65 69
66 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); 70 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
67 glEnableVertexAttribArray(textureLocation_); 71 glEnableVertexAttribArray(textureLocation_);
68 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); 72 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
73 }
69 } 74 }
70 75
71
72 void OpenGLTextureProgram::FinalizeExecution() 76 void OpenGLTextureProgram::FinalizeExecution()
73 { 77 {
74 glDisableVertexAttribArray(positionLocation_); 78 if (!context_.IsContextLost())
75 glDisableVertexAttribArray(textureLocation_); 79 {
80 glDisableVertexAttribArray(positionLocation_);
81 glDisableVertexAttribArray(textureLocation_);
82 }
76 } 83 }
77 84
78
79 OpenGLTextureProgram::OpenGLTextureProgram(OpenGL::IOpenGLContext& context, 85 OpenGLTextureProgram::OpenGLTextureProgram(OpenGL::IOpenGLContext& context,
80 const char* fragmentShader) : 86 const char* fragmentShader) :
81 context_(context) 87 context_(context)
82 { 88 {
83 static const float POSITIONS[COMPONENTS * COUNT] = { 89 static const float POSITIONS[COMPONENTS * COUNT] = {
86 1, 0, 92 1, 0,
87 1, 0, 93 1, 0,
88 0, 1, 94 0, 1,
89 1, 1 95 1, 1
90 }; 96 };
91
92 context_.MakeCurrent();
93 97
94 program_.reset(new OpenGL::OpenGLProgram); 98 if (!context_.IsContextLost())
95 program_->CompileShaders(VERTEX_SHADER, fragmentShader); 99 {
100 context_.MakeCurrent();
96 101
97 positionLocation_ = program_->GetAttributeLocation("a_position"); 102 program_.reset(new OpenGL::OpenGLProgram(context_));
98 textureLocation_ = program_->GetAttributeLocation("a_texcoord"); 103 program_->CompileShaders(VERTEX_SHADER, fragmentShader);
99 104
100 glGenBuffers(2, buffers_); 105 positionLocation_ = program_->GetAttributeLocation("a_position");
106 textureLocation_ = program_->GetAttributeLocation("a_texcoord");
101 107
102 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); 108 glGenBuffers(2, buffers_);
103 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
104 109
105 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); 110 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
106 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW); 111 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
112
113 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
114 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
115 }
107 } 116 }
108
109 117
110 OpenGLTextureProgram::~OpenGLTextureProgram() 118 OpenGLTextureProgram::~OpenGLTextureProgram()
111 { 119 {
112 context_.MakeCurrent(); 120 if (!context_.IsContextLost())
113 glDeleteBuffers(2, buffers_); 121 {
122 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("OpenGLTextureProgram::~OpenGLTextureProgram() | About to call glDeleteBuffers");
123 context_.MakeCurrent();
124 glDeleteBuffers(2, buffers_);
125 }
114 } 126 }
115 127
116 128
117 void OpenGLTextureProgram::Execution::DrawTriangles() 129 void OpenGLTextureProgram::Execution::DrawTriangles()
118 { 130 {
119 glDrawArrays(GL_TRIANGLES, 0, COUNT); 131 if (!that_.context_.IsContextLost())
132 {
133 glDrawArrays(GL_TRIANGLES, 0, COUNT);
134 }
120 } 135 }
121 } 136 }
122 } 137 }