JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
GPUshader.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
19
20// ####################################################################################################
22 itsId(0)
23{ }
24
25// ####################################################################################################
27{
28 if (itsId) glDeleteShader(itsId);
29}
30
31// ####################################################################################################
33{ return itsId; }
34
35// ####################################################################################################
36void jevois::GPUshader::load(char const * filename, GLuint type)
37{
38 if (itsId) { glDeleteShader(itsId); itsId = 0; }
39
40 // Read the whole file into memory (much faster than using streambuf):
41 FILE * f = fopen(filename, "rb"); if (f == nullptr) PLFATAL("Failed to read file " << filename);
42 fseek(f, 0, SEEK_END);
43 int sz = ftell(f);
44 fseek(f, 0, SEEK_SET);
45 char * src = new GLchar[sz+1];
46 if (fread(src, 1, sz, f) != 1) { fclose(f); LFATAL("Failed to read " << filename); }
47 src[sz] = 0; // null terminate it
48 fclose(f);
49
50 // Set and compile the shader:
51 try { this->set(filename, src, type); } catch (...) { jevois::warnAndIgnoreException(); }
52
53 delete [] src;
54}
55
56// ####################################################################################################
57void jevois::GPUshader::set(char const * name, char const * str, GLuint type)
58{
59 if (itsId) { glDeleteShader(itsId); itsId = 0; }
60
61 // Create and compile the shader:
62 GL_CHECK(itsId = glCreateShader(type));
63 GL_CHECK(glShaderSource(itsId, 1, (const GLchar**)&str, 0));
64 GL_CHECK(glCompileShader(itsId));
65
66 // Compilation check:
67 GLint compiled; glGetShaderiv(itsId, GL_COMPILE_STATUS, &compiled);
68 if (compiled == 0)
69 {
70 GLint loglen = 4096; char log[loglen];
71 glGetShaderInfoLog(itsId, loglen, &loglen, &log[0]); log[loglen] = '\0';
72 glDeleteShader(itsId);
73 LFATAL("Failed to compile shader [" << name << "], Log: " << &log[0]);
74 }
75
76 LDEBUG("Compiled shader [" << name << ']');
77}
#define GL_CHECK(stmt)
Simple macro to check for OpenGL errors.
Definition OpenGL.H:77
void load(char const *filename, GLuint type)
Load a shader from file.
Definition GPUshader.C:36
GPUshader()
Constructor.
Definition GPUshader.C:21
void set(char const *name, char const *str, GLuint type)
Load a shader from inlined code in a C-string.
Definition GPUshader.C:57
~GPUshader()
Destructor.
Definition GPUshader.C:26
GLuint id() const
Get the shader's ID.
Definition GPUshader.C:32
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LDEBUG(msg)
Convenience macro for users to print out console or syslog messages, DEBUG level.
Definition Log.H:173
std::string warnAndIgnoreException(std::string const &prefix="")
Convenience function to catch an exception, issue some LERROR (depending on type),...
Definition Log.C:236
#define PLFATAL(msg)
Like LDEBUG but appends errno and strerror(errno), to be used when some system call fails.
Definition Log.H:239