annotate Framework/OpenGL/OpenGLShader.cpp @ 1255:c1c83c1fb837 broker

GetOrthancImageCommand: handling of unsupported formats
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 08 Jan 2020 15:22:13 +0100
parents a7351ad54960
children 2d8ab34c8c91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "OpenGLShader.h"
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include <Core/OrthancException.h>
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 namespace OrthancStone
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 namespace OpenGL
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 static GLuint CompileShader(GLenum type,
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 const std::string& source)
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 // Create shader object
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 const GLchar* sourceString[1];
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 GLint sourceStringLengths[1];
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 sourceString[0] = source.c_str();
701
58e1faeafb1b Fixed truncating conversion warning
Benjamin Golinvaux <bgo@osimis.io>
parents: 599
diff changeset
38 sourceStringLengths[0] = static_cast<GLint>(source.length());
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 GLuint shader = glCreateShader(type);
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
40 ORTHANC_OPENGL_CHECK("glCreateShader");
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 if (shader == 0)
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 "Cannot create an OpenGL shader");
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 else
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 // Assign and compile the source to the shader object
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 glShaderSource(shader, 1, sourceString, sourceStringLengths);
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
51 ORTHANC_OPENGL_CHECK("glShaderSource");
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 glCompileShader(shader);
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
53 ORTHANC_OPENGL_CHECK("glCompileShader");
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 // Check if there were errors
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 int infoLen = 0;
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
58 ORTHANC_OPENGL_CHECK("glGetShaderiv");
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
599
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 584
diff changeset
60 if (infoLen > 1) // Might be equal to 1, which amounts to no error
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 579
diff changeset
62 std::string infoLog;
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 579
diff changeset
63 infoLog.resize(infoLen + 1);
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 579
diff changeset
64 glGetShaderInfoLog(shader, infoLen, NULL, &infoLog[0]);
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
65 ORTHANC_OPENGL_CHECK("glGetShaderInfoLog");
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
66 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteShader");
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 glDeleteShader(shader);
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
68 ORTHANC_OPENGL_CHECK("glDeleteShader");
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 579
diff changeset
71 "Error while creating an OpenGL shader: " + infoLog);
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 else
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 return shader;
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 OpenGLShader::OpenGLShader(GLenum type,
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 const std::string& source)
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 shader_ = CompileShader(type, source);
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 isValid_ = true;
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 OpenGLShader::~OpenGLShader()
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
91 try
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
92 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
93 if (isValid_)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
94 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
95 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteShader");
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
96 glDeleteShader(shader_);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
97 ORTHANC_OPENGL_CHECK("glDeleteShader");
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
98 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
99 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
100 catch (const Orthanc::OrthancException& e)
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
102 if (e.HasDetails())
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
103 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
104 LOG(ERROR) << "OrthancException in ~OpenGLShader: " << e.What() << " Details: " << e.GetDetails();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
105 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
106 else
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
107 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
108 LOG(ERROR) << "OrthancException in ~OpenGLShader: " << e.What();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
109 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
110 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
111 catch (const std::exception& e)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
112 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
113 LOG(ERROR) << "std::exception in ~OpenGLShader: " << e.what();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
114 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
115 catch (...)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
116 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 701
diff changeset
117 LOG(ERROR) << "Unknown exception in ~OpenGLShader";
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 GLuint OpenGLShader::Release()
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 if (isValid_)
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 isValid_ = false;
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 return shader_;
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 else
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
130 LOG(ERROR) << "OpenGLShader::Release(): (!isValid_)";
578
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 }
21fd70df3fc9 starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }