File: LoadShaderFromFile.m

package info (click to toggle)
psychtoolbox-3 3.0.11.20140816.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 77,468 kB
  • ctags: 17,316
  • sloc: ansic: 95,595; cpp: 11,170; objc: 4,026; sh: 1,844; python: 383; php: 312; makefile: 191; java: 113
file content (103 lines) | stat: -rwxr-xr-x 3,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function handle = LoadShaderFromFile(filename, shadertype, debug)
% handle = LoadShaderFromFile(filename [, shadertype] [, debug])
%
% Loads a GLSL OpenGL shader definition from textfile 'filename' and
% creates an OpenGL GLSL shader of type 'shadertype'. Returns a handle to
% the new shader. If shadertype is omitted, the type of the shader is
% derived from the filename extension: .vert == GL_VERTEX_SHADER, .frag ==
% GL_FRAGMENT_SHADER, .geom == GL_GEOMETRY_SHADER, .tesscontrol =
% GL_TESS_CONTROL_SHADER, .tesseval = GL_TESS_EVALUATION_SHADER. The
% optional 'debug' flag allows to enable debugging output.
%
% On successfull load & creation, a 'handle' to the new shader is returned.
%

% 29-Mar-2006 written by MK
% 29-Jul-2009 Bugfixes by MK: debug must default to 1, not 2!
% 06-Sep-2013 Add geometry shader and tesselation shader support. (MK)

global GL;

if isempty(GL)
    InitializeMatlabOpenGL([],[],1);
end;

% Make sure we run on a GLSL capable system.
AssertGLSL;

if nargin < 3
    debug = 1;
end;

if nargin < 1 
    filename = [];
end

if isempty(filename)
    % Set default shader file name:
    error('LoadShaderFromFile() called without any shader filename!');
end;


% Add path to our own shader directory if no path given:
if isempty(fileparts(filename))
    % No path to the shader defined as part of the filename. Add our
    % default path to our standard shader directory:
    filename = [ PsychtoolboxRoot 'PsychOpenGL/PsychGLSLShaders/' filename ];
end;

if nargin < 2
    shadertype = [];
end

if isempty(shadertype)
    % Try to autodetect shadertype from file extension:
    if ~isempty(strfind(filename, '.frag'))
        shadertype = GL.FRAGMENT_SHADER;
        if debug>0, fprintf('Building a fragment shader:'); end;
    elseif ~isempty(strfind(filename, '.vert'))
        shadertype = GL.VERTEX_SHADER;
        if debug>0, fprintf('Building a vertex shader:'); end;
    elseif ~isempty(strfind(filename, '.geom'))
        shadertype = GL.GEOMETRY_SHADER;
        if debug>0, fprintf('Building a geometry shader:'); end;
    elseif ~isempty(strfind(filename, '.tesscontrol'))
        shadertype = GL.TESS_CONTROL_SHADER;
        if debug>0, fprintf('Building a tesselation control shader:'); end;        
    elseif ~isempty(strfind(filename, '.tesseval'))
        shadertype = GL.TESS_EVALUATION_SHADER;
        if debug>0, fprintf('Building a tesselation evaluation shader:'); end;        
    else
        error('No shadertype provided and could not derive type from filename extension!');
    end;
end;

if debug>0
    fprintf('Reading shader from file %s ...\n', filename);
end;

% Read shader source code from file:
[fid errmsg]=fopen(filename, 'rt');
if fid<0
    error('Could not open shader definition file %s [%s]!', filename, errmsg);
end;
shadersrc = fread(fid);
fclose(fid);

% Create shader, assign sourcecode and compile it:
handle = glCreateShader(shadertype);
if handle <= 0
    fprintf('The handle created by glCreateShader is %i -- An invalid handle!\n', handle);
    error('LoadShaderFromFile: glCreateShader failed to create a valid shader! Something is wrong with your graphics drivers!');
end

if debug > 1
    glShaderSource(handle, shadersrc, debug);
else
    glShaderSource(handle, shadersrc);
end;

glCompileShader(handle);

% Done.
return;