comparison Framework/OpenGL/OpenGLProgram.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 fadacfbf5538
children 6a9300ecfa13
comparison
equal deleted inserted replaced
946:dbe3e1e47019 947:1091b2adeb5a
20 20
21 21
22 #include "OpenGLProgram.h" 22 #include "OpenGLProgram.h"
23 23
24 #include "OpenGLShader.h" 24 #include "OpenGLShader.h"
25 #include "IOpenGLContext.h"
25 26
26 #include <Core/OrthancException.h> 27 #include <Core/OrthancException.h>
27
28 28
29 namespace OrthancStone 29 namespace OrthancStone
30 { 30 {
31 namespace OpenGL 31 namespace OpenGL
32 { 32 {
33 OpenGLProgram::OpenGLProgram() 33 OpenGLProgram::OpenGLProgram(OpenGL::IOpenGLContext& context)
34 : context_(context)
34 { 35 {
35 program_ = glCreateProgram(); 36 program_ = glCreateProgram();
37 ORTHANC_OPENGL_CHECK("glCreateProgram");
36 if (program_ == 0) 38 if (program_ == 0)
37 { 39 {
38 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, 40 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
39 "Cannot create an OpenGL program"); 41 "Cannot create an OpenGL program");
40 } 42 }
41 } 43 }
42 44
43 45
44 OpenGLProgram::~OpenGLProgram() 46 OpenGLProgram::~OpenGLProgram()
45 { 47 {
46 assert(program_ != 0); 48 try
47 glDeleteProgram(program_); 49 {
50 if (!context_.IsContextLost())
51 {
52 ORTHANC_CHECK_CURRENT_CONTEXT(context_);
53 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteProgram");
54 assert(program_ != 0);
55 glDeleteProgram(program_);
56 ORTHANC_OPENGL_CHECK("glDeleteProgram");
57 }
58 }
59 catch (const Orthanc::OrthancException& e)
60 {
61 if (e.HasDetails())
62 {
63 LOG(ERROR) << "OrthancException in ~OpenGLProgram: " << e.What() << " Details: " << e.GetDetails();
64 }
65 else
66 {
67 LOG(ERROR) << "OrthancException in ~OpenGLProgram: " << e.What();
68 }
69 }
70 catch (const std::exception& e)
71 {
72 LOG(ERROR) << "std::exception in ~OpenGLProgram: " << e.what();
73 }
74 catch (...)
75 {
76 LOG(ERROR) << "Unknown exception in ~OpenGLProgram";
77 }
48 } 78 }
49
50 79
51 void OpenGLProgram::Use() 80 void OpenGLProgram::Use()
52 { 81 {
82 //ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glUseProgram");
53 glUseProgram(program_); 83 glUseProgram(program_);
84 ORTHANC_OPENGL_CHECK("glUseProgram");
54 } 85 }
55 86
56
57 void OpenGLProgram::CompileShaders(const std::string& vertexCode, 87 void OpenGLProgram::CompileShaders(const std::string& vertexCode,
58 const std::string& fragmentCode) 88 const std::string& fragmentCode)
59 { 89 {
60 assert(program_ != 0); 90 assert(program_ != 0);
61 91
62 OpenGLShader vertexShader(GL_VERTEX_SHADER, vertexCode); 92 OpenGLShader vertexShader(GL_VERTEX_SHADER, vertexCode);
63 OpenGLShader fragmentShader(GL_FRAGMENT_SHADER, fragmentCode); 93 OpenGLShader fragmentShader(GL_FRAGMENT_SHADER, fragmentCode);
64 94
65 glAttachShader(program_, vertexShader.Release()); 95 glAttachShader(program_, vertexShader.Release());
96 ORTHANC_OPENGL_CHECK("glAttachShader");
66 glAttachShader(program_, fragmentShader.Release()); 97 glAttachShader(program_, fragmentShader.Release());
98 ORTHANC_OPENGL_CHECK("glAttachShader");
67 glLinkProgram(program_); 99 glLinkProgram(program_);
100 ORTHANC_OPENGL_CHECK("glLinkProgram");
68 glValidateProgram(program_); 101 glValidateProgram(program_);
102 ORTHANC_OPENGL_CHECK("glValidateProgram");
69 } 103 }
70
71 104
72 GLint OpenGLProgram::GetUniformLocation(const std::string& name) 105 GLint OpenGLProgram::GetUniformLocation(const std::string& name)
73 { 106 {
74 GLint location = glGetUniformLocation(program_, name.c_str()); 107 GLint location = glGetUniformLocation(program_, name.c_str());
108 ORTHANC_OPENGL_CHECK("glGetUniformLocation");
75 109
76 if (location == -1) 110 if (location == -1)
77 { 111 {
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, 112 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
79 "Inexistent uniform variable in shader: " + name); 113 "Inexistent uniform variable in shader: " + name);
86 120
87 121
88 GLint OpenGLProgram::GetAttributeLocation(const std::string& name) 122 GLint OpenGLProgram::GetAttributeLocation(const std::string& name)
89 { 123 {
90 GLint location = glGetAttribLocation(program_, name.c_str()); 124 GLint location = glGetAttribLocation(program_, name.c_str());
125 ORTHANC_OPENGL_CHECK("glGetAttribLocation");
91 126
92 if (location == -1) 127 if (location == -1)
93 { 128 {
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, 129 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
95 "Inexistent attribute in shader: " + name); 130 "Inexistent attribute in shader: " + name);