Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/OpenGL/OpenGLShader.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/OpenGL/OpenGLShader.cpp@30deba7bc8e2 |
children | 85e117739eca |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "OpenGLShader.h" | |
23 | |
24 #include <OrthancException.h> | |
25 | |
26 namespace OrthancStone | |
27 { | |
28 namespace OpenGL | |
29 { | |
30 static GLuint CompileShader(GLenum type, | |
31 const std::string& source) | |
32 { | |
33 // Create shader object | |
34 const GLchar* sourceString[1]; | |
35 GLint sourceStringLengths[1]; | |
36 | |
37 sourceString[0] = source.c_str(); | |
38 sourceStringLengths[0] = static_cast<GLint>(source.length()); | |
39 GLuint shader = glCreateShader(type); | |
40 ORTHANC_OPENGL_CHECK("glCreateShader"); | |
41 | |
42 if (shader == 0) | |
43 { | |
44 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
45 "Cannot create an OpenGL shader"); | |
46 } | |
47 else | |
48 { | |
49 // Assign and compile the source to the shader object | |
50 glShaderSource(shader, 1, sourceString, sourceStringLengths); | |
51 ORTHANC_OPENGL_CHECK("glShaderSource"); | |
52 glCompileShader(shader); | |
53 ORTHANC_OPENGL_CHECK("glCompileShader"); | |
54 | |
55 // Check if there were errors | |
56 int infoLen = 0; | |
57 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); | |
58 ORTHANC_OPENGL_CHECK("glGetShaderiv"); | |
59 | |
60 if (infoLen > 1) // Might be equal to 1, which amounts to no error | |
61 { | |
62 std::string infoLog; | |
63 infoLog.resize(infoLen + 1); | |
64 glGetShaderInfoLog(shader, infoLen, NULL, &infoLog[0]); | |
65 ORTHANC_OPENGL_CHECK("glGetShaderInfoLog"); | |
66 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteShader"); | |
67 glDeleteShader(shader); | |
68 ORTHANC_OPENGL_CHECK("glDeleteShader"); | |
69 | |
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
71 "Error while creating an OpenGL shader: " + infoLog); | |
72 } | |
73 else | |
74 { | |
75 return shader; | |
76 } | |
77 } | |
78 } | |
79 | |
80 | |
81 OpenGLShader::OpenGLShader(GLenum type, | |
82 const std::string& source) | |
83 { | |
84 shader_ = CompileShader(type, source); | |
85 isValid_ = true; | |
86 } | |
87 | |
88 | |
89 OpenGLShader::~OpenGLShader() | |
90 { | |
91 try | |
92 { | |
93 if (isValid_) | |
94 { | |
95 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteShader"); | |
96 glDeleteShader(shader_); | |
97 ORTHANC_OPENGL_CHECK("glDeleteShader"); | |
98 } | |
99 } | |
100 catch (const Orthanc::OrthancException& e) | |
101 { | |
102 if (e.HasDetails()) | |
103 { | |
104 LOG(ERROR) << "OrthancException in ~OpenGLShader: " << e.What() << " Details: " << e.GetDetails(); | |
105 } | |
106 else | |
107 { | |
108 LOG(ERROR) << "OrthancException in ~OpenGLShader: " << e.What(); | |
109 } | |
110 } | |
111 catch (const std::exception& e) | |
112 { | |
113 LOG(ERROR) << "std::exception in ~OpenGLShader: " << e.what(); | |
114 } | |
115 catch (...) | |
116 { | |
117 LOG(ERROR) << "Unknown exception in ~OpenGLShader"; | |
118 } | |
119 } | |
120 | |
121 GLuint OpenGLShader::Release() | |
122 { | |
123 if (isValid_) | |
124 { | |
125 isValid_ = false; | |
126 return shader_; | |
127 } | |
128 else | |
129 { | |
130 LOG(ERROR) << "OpenGLShader::Release(): (!isValid_)"; | |
131 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
132 } | |
133 } | |
134 } | |
135 } |