File: moglCreateFBO.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 (128 lines) | stat: -rw-r--r-- 4,015 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
124
125
126
127
128
function [fbo , texids] = moglCreateFBO(width, height, nrbuffers, layers, format, withdepth, withstencil)
% [fbo , texids] = moglCreateFBO(width, height [, nrbuffers, layers, format, withdepth, withstencil])
%
% moglCreateFBO creates a standard OpenGL Framebuffer Object, suitable for
% computer vision and other GPGPU tasks and returns a handle to it.
%
% The FBO will have a size of width x height pixels and its 'nrbuffers'
% color buffers will have textures with 'layers' layers (default = 4 for RGBA)
% attached.
% 'format' specifies the data format. It defaults to single precision
% floating point resolution (32 bit float). If 'withdepth' > 0 then a
% depth buffer gets attached as well. If 'withstencil' > 0 then a stencil
% drawable gets attached as well.
%
% The function will create appropriate textures and renderbuffers, create
% an appropriate FBO and attach the textures and renderbuffers. After
% validation, the handle to the FBO is returned.

% History:
% 30.05.2006 Wrote it. (MK)
% 06.07.2016 Remove weird Linux + Nvidia format hack. Caused failure.

global GL;

% Child protection:
AssertGLSL;

if nargin < 2
    error('Must specify a widht x height of FBO in CreateGLFBO!');
end;

if nargin < 3
    nrbuffers = 1;
end;

if isempty(nrbuffers)
    nrbuffers = 1;
end;

if nargin < 4
    layers = 4;
end;
if isempty(layers)
    layers = 4;
end;

if nargin < 5
    format = GL.RGBA_FLOAT32_APPLE;
end;
if isempty(format)
    format = GL.RGBA_FLOAT32_APPLE;
end;

if nargin < 6
    withdepth = 0;
end;
if isempty(withdepth)
    withdepth = 0;
end;

if nargin < 7
    withstencil = 0;
end;
if isempty(withstencil)
    withstencil = 0;
end;

if nrbuffers > glGetIntegerv(GL.MAX_COLOR_ATTACHMENTS_EXT)
    error('moglCreateFBO: Sorry this hardware does not support the requested number of color buffers.');
end;

% Enable 2D rectangle textures. Power-of-two textures are known to make
% trouble on many older gfx-cards...
glEnable(GL.TEXTURE_RECTANGLE_EXT);

% Generate texture objects:
texids=glGenTextures(nrbuffers);

% Create a framebuffer object:
fbo = glGenFramebuffersEXT(1);

% Bind fbo:
glBindFramebufferEXT(GL.FRAMEBUFFER_EXT, fbo);

% Create and attach textures as color buffer attachments:
for i=1:nrbuffers
    % Enable texture by binding it:
    glBindTexture(GL.TEXTURE_RECTANGLE_EXT,texids(i));

    % Create representation: A rectangle texture with only mipmap level zero
    % and without a border, single precision float, RGBA:
    glTexImage2D(GL.TEXTURE_RECTANGLE_EXT, 0, format, width, height, 0, GL.RGBA, GL.FLOAT, 0);

    % Setup texture wrapping behaviour to clamp, as other behaviours are
    % unsupported on many gfx-cards for rectangle textures:
    glTexParameterfv(GL.TEXTURE_RECTANGLE_EXT,GL.TEXTURE_WRAP_S,GL.CLAMP);
    glTexParameterfv(GL.TEXTURE_RECTANGLE_EXT,GL.TEXTURE_WRAP_T,GL.CLAMP);

    % Setup filtering for the textures - Use nearest neighbour as floating
    % point filtering usually unsupported.
    glTexParameterfv(GL.TEXTURE_RECTANGLE_EXT,GL.TEXTURE_MAG_FILTER,GL.NEAREST);
    glTexParameterfv(GL.TEXTURE_RECTANGLE_EXT,GL.TEXTURE_MIN_FILTER,GL.NEAREST);

    % Choose texture application function to be a neutral REPLACE:
    glTexEnvfv(GL.TEXTURE_ENV,GL.TEXTURE_ENV_MODE,GL.REPLACE);

    % Unbind it after setup:
    glBindTexture(GL.TEXTURE_RECTANGLE_EXT, 0);

    % Attach textures mipmap level zero as color buffer:
    glFramebufferTexture2DEXT(GL.FRAMEBUFFER_EXT, GL.COLOR_ATTACHMENT0_EXT + i - 1, GL.TEXTURE_RECTANGLE_EXT, texids(i), 0);    
end;
    
% Check if FBO is framebuffer complete:
fbostatus = glCheckFramebufferStatusEXT(GL.FRAMEBUFFER_EXT);
if not(fbostatus == GL.FRAMEBUFFER_COMPLETE_EXT)
    glDeleteFramebuffersEXT(1, fbo);
    glDeleteTextures(length(texids), texids);
    fprintf('Error code from framebuffer status call: %i\n', fbostatus);
    error('Failed to setup framebuffer object!');
    return;
end;
    
% Unbind it:
glBindFramebufferEXT(GL.FRAMEBUFFER_EXT, 0);

% Done. Return handle to FBO and texture:
return;