gVirtualXRay  2.0.10
VirtualX-RayImagingLibraryonGPU
Shader.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2017, Dr Franck P. Vidal (franck.p.vidal@fpvidal.net),
4 http://www.fpvidal.net/
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation and/or
15 other materials provided with the distribution.
16 
17 3. Neither the name of the Bangor University nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 */
33 
34 
35 #ifndef __Shader_h
36 #define __Shader_h
37 
38 
65 //******************************************************************************
66 // Include
67 //******************************************************************************
68 #ifndef __gVirtualXRayConfig_h
70 #endif
71 
72 #include <string>
73 
74 
75 //******************************************************************************
76 // namespace
77 //******************************************************************************
78 namespace gVirtualXRay {
79 
80 
81 //==============================================================================
86 //==============================================================================
87 class Shader
88 //------------------------------------------------------------------------------
89 {
90 //******************************************************************************
91 public:
92  //--------------------------------------------------------------------------
94  //--------------------------------------------------------------------------
95  Shader();
96 
97 
98  //--------------------------------------------------------------------------
100  //--------------------------------------------------------------------------
101  ~Shader();
102 
103 
104  //--------------------------------------------------------------------------
106  //--------------------------------------------------------------------------
107  void enable() const;
108 
109 
110  //--------------------------------------------------------------------------
112  //--------------------------------------------------------------------------
113  static void disable();
114 
115 
116  //--------------------------------------------------------------------------
119  //--------------------------------------------------------------------------
120  void release();
121 
122 
123  //--------------------------------------------------------------------------
125 
129  //--------------------------------------------------------------------------
130  void loadFiles(const std::string& aVertexShaderFilename,
131  const std::string& aFragmentShaderFilename);
132 
133 
134  //--------------------------------------------------------------------------
136 
139  //--------------------------------------------------------------------------
140  void loadFile(const std::string& aComputeShaderFilename);
141 
142 
143  //--------------------------------------------------------------------------
145 
149  //--------------------------------------------------------------------------
150  void setLabels(const std::string& aVertexShaderLabel,
151  const std::string& aFragmentShaderLabel);
152 
153 
154  //--------------------------------------------------------------------------
156 
159  //--------------------------------------------------------------------------
160  void setLabel(const std::string& aComputeShaderLabel);
161 
162 
163  //--------------------------------------------------------------------------
165 
168  //--------------------------------------------------------------------------
169  void loadVertexShaderFile(const std::string& aVertexShaderFilename);
170 
171 
172  //--------------------------------------------------------------------------
174 
177  //--------------------------------------------------------------------------
178  void loadFragmentShaderFile(const std::string& aFragmentShaderFilename);
179 
180 
181  //--------------------------------------------------------------------------
183 
186  //--------------------------------------------------------------------------
187  void loadComputeShaderFile(const std::string& aComputeShaderFilename);
188 
189 
190  //--------------------------------------------------------------------------
192 
196  //--------------------------------------------------------------------------
197  void loadSource(const std::string& aVertexShaderSourceCode,
198  const std::string& aFragmentShaderSourceCode);
199 
200 
201  //--------------------------------------------------------------------------
203 
206  //--------------------------------------------------------------------------
207  void loadSource(const std::string& aComputeShaderSourceCode);
208 
209 
210  //--------------------------------------------------------------------------
212 
215  //--------------------------------------------------------------------------
216  void loadVertexShaderSource(const std::string& aVertexShaderSourceCode);
217 
218 
219  //--------------------------------------------------------------------------
221 
224  //--------------------------------------------------------------------------
225  void loadFragmentShaderSource(const std::string& aFragmentShaderSourceCode);
226 
227 
228  //--------------------------------------------------------------------------
230 
233  //--------------------------------------------------------------------------
234  void loadComputeShaderSource(const std::string& aComputeShaderSourceCode);
235 
236 
237  //--------------------------------------------------------------------------
239 
242  //--------------------------------------------------------------------------
243  unsigned int getProgramHandle() const;
244 
245 
246  //--------------------------------------------------------------------------
248 
251  //--------------------------------------------------------------------------
252  bool isValid() const;
253 
254 
255 //******************************************************************************
256 protected:
257  //--------------------------------------------------------------------------
259 
264  //--------------------------------------------------------------------------
265  int loadFile(const std::string& aShaderFilename, char**& aShaderSource);
266 
267 
268  //--------------------------------------------------------------------------
270 
277  //--------------------------------------------------------------------------
278  unsigned int compileShader(int aShaderType,
279  int aLineNumber,
280  char** aShaderSource);
281 
282 
283  //--------------------------------------------------------------------------
285  //--------------------------------------------------------------------------
286  void linkProgram();
287 
288 
289  //--------------------------------------------------------------------------
291  //--------------------------------------------------------------------------
293 
294 
295  //--------------------------------------------------------------------------
297  //--------------------------------------------------------------------------
299 
300 
301  //--------------------------------------------------------------------------
303  //--------------------------------------------------------------------------
305 
306 
307  //--------------------------------------------------------------------------
309 
313  //--------------------------------------------------------------------------
314  void releaseSourceMemory(int& aLineNumber, char**& aShaderSource);
315 
316 
318  static bool m_shader_capable;
319 
320 
323 
324 
327 
328 
331 
332 
335 
336 
339 
340 
343 
344 
347 
348 
351 
352 
355 
356 
359 
360 
363 
364 
367 
368 
372  bool m_valid;
373 
374 
376  unsigned int m_shader_program_id;
377 
378 
380  unsigned int m_vertex_shader_id;
381 
382 
384  unsigned int m_fragment_shader_id;
385 
386 
388  unsigned int m_compute_shader_id;
389 };
390 
391 
392 } // namespace gVirtualXRay
393 
394 
395 //******************************************************************************
396 #include "Shader.inl"
397 
398 
399 #endif // __Shader_h
void loadFiles(const std::string &aVertexShaderFilename, const std::string &aFragmentShaderFilename)
Load the vertex and fragment shaders from files.
void releaseComputeSourceMemory()
Release the memory held by the compute shader.
Definition: Shader.inl:119
void releaseFragmentSourceMemory()
Release the memory held by the fragment shader.
Definition: Shader.inl:111
unsigned int m_vertex_shader_id
ID of the vertex shader.
Definition: Shader.h:380
void loadVertexShaderFile(const std::string &aVertexShaderFilename)
Load the vertex shader from a file.
void loadFile(const std::string &aComputeShaderFilename)
Load the compute shader from a file.
unsigned int m_shader_program_id
ID of the shader program.
Definition: Shader.h:376
std::string m_vertex_shader_filename
Name of the file containing the vertex shader.
Definition: Shader.h:334
static void disable()
Disable the current shader (if any)
void loadFragmentShaderSource(const std::string &aFragmentShaderSourceCode)
Load the fragment shader from a string.
std::string m_compute_shader_filename
Name of the file containing the compute shader.
Definition: Shader.h:342
void loadFragmentShaderFile(const std::string &aFragmentShaderFilename)
Load the fragment shader from a file.
~Shader()
Destructor.
int m_vertex_source_number_of_lines
Number of line of the vertex shader.
Definition: Shader.h:350
void setLabel(const std::string &aComputeShaderLabel)
Set the compute shader label. This is useful for debugging.
Definition: Shader.inl:79
unsigned int m_compute_shader_id
ID of the compute shader.
Definition: Shader.h:388
bool isValid() const
Accessor on the validity status of the shader.
Definition: Shader.inl:95
char ** m_fragment_source
Source code of the fragment shader.
Definition: Shader.h:354
std::string m_fragment_shader_filename
Name of the file containing the fragment shader.
Definition: Shader.h:338
void linkProgram()
Link the vertex and fragment programs.
Class to handle GLSL shaders.
std::string m_vertex_shader_label
Label of the vertex shader.
Definition: Shader.h:322
void loadComputeShaderSource(const std::string &aComputeShaderSourceCode)
Load the compute shader from a string.
unsigned int compileShader(int aShaderType, int aLineNumber, char **aShaderSource)
Compile the vertex shader or the fragment shader.
Shader is a class to handle shaders written in GLSL.
Definition: Shader.h:87
Shader()
Default constructor.
char ** m_vertex_source
Source code of the vertex shader.
Definition: Shader.h:346
int m_fragment_source_number_of_lines
Number of line of the fragment shader.
Definition: Shader.h:358
int m_compute_source_number_of_lines
Number of line of the vertex shader.
Definition: Shader.h:366
unsigned int m_fragment_shader_id
ID of the fragment shader.
Definition: Shader.h:384
std::string m_compute_shader_label
Label of the compute shader.
Definition: Shader.h:330
void enable() const
Enable the current shader.
unsigned int getProgramHandle() const
Accessor on the shader program handle.
Definition: Shader.inl:87
void loadComputeShaderFile(const std::string &aComputeShaderFilename)
Load the compute shader from a file.
char ** m_compute_source
Source code of the vertex shader.
Definition: Shader.h:362
void releaseSourceMemory(int &aLineNumber, char **&aShaderSource)
Release the memory held by the vertex shader or the fragment shader.
std::string m_fragment_shader_label
Label of the fragment shader.
Definition: Shader.h:326
void loadSource(const std::string &aVertexShaderSourceCode, const std::string &aFragmentShaderSourceCode)
Load the shader source code.
static bool m_shader_capable
Flag set to true when a shader if fully valid.
Definition: Shader.h:318
void setLabels(const std::string &aVertexShaderLabel, const std::string &aFragmentShaderLabel)
Set the vertex and fragment shader labels. This is useful for debugging.
Definition: Shader.inl:69
void releaseVertexSourceMemory()
Release the memory held by the vertex shader.
Definition: Shader.inl:103
void loadVertexShaderSource(const std::string &aVertexShaderSourceCode)
Load the vertex shader from a string.