File: LoadShaderFromFile.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (123 lines) | stat: -rw-r--r-- 4,096 bytes parent folder | download | duplicates (5)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function handle = LoadShaderFromFile(filename, shadertype, debug)
% handle = LoadShaderFromFile(filename [, shadertype] [, debug=0])
%
% 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.
%
% The optional 'debug' flag allows to enable debugging output:
% Zero = no output, 1 = a bit of output, 2 = detailed output,
% 3 = Don't compile and link anymore, but print out the shaders
% source code as OpenGL would see it.
%
% 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)
% 12-Sep-2015 Add dumping of shader compile log at debug level 2 (DHN)
% 12-Sep-2015 Only abort with full shader source dump at level 3. Cosmetic cleanups (MK)

global GL;

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

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

if nargin < 3
    debug = 0;
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 > 2
    glShaderSource(handle, shadersrc, debug);
else
    glShaderSource(handle, shadersrc);
end

if debug > 1
    % We need to temporarily raise moglcores debuglevel to 2 to get extended
    % error/validation information:
    oldDebug = InitializeMatlabOpenGL(-1);
    moglcore('DEBUGLEVEL', 2);

    % Compile the shader:
    glCompileShader(handle);

    % Restore old debuglevel for moglcore:
    moglcore('DEBUGLEVEL', oldDebug);
else
    % Compile the shader without raised debug level for moglcore:
    glCompileShader(handle);
end

% Done.
return;