File: ImagingPipelineExternalConsumerTest.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 (303 lines) | stat: -rw-r--r-- 9,830 bytes parent folder | download | duplicates (4)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
function ImagingPipelineExternalConsumerTest(cmd, varargin)
% ImagingPipelineExternalConsumerTest - Test code for imaging pipe output redirection.
%
% To proof-of-concept test the imaging pipelines mechanisms to redirect
% final rendered output to an external consumer or video sink, instead of
% displaying in the onscreen window.
%
% This test not really useful for end users, just for internal
% development and debugging. Does not work on all platforms or
% runtimes.
%
% History:
% Mar-2017  mk Written.

  global GL;
  global w;
  global stereomode;
  global oldglleft;
  global oldglright;
  global curtex;
  global leftTextures;
  global rightTextures;

  if nargin > 0
    if cmd == 0
      ProducerConsumer(varargin{:});
      return;
    end

    if cmd == 1
      ReleaseProducer;
      return;
    end

    if cmd == 2
      PassiveConsumer;
      return;
    end
  end

  PsychDefaultSetup(2);
  close all;

  %InitializeMatlabOpenGL;
  Screen('Preference', 'ConserveVRAM', 524288);
  Screen('Preference', 'SkipSyncTests', 2);
  Screen('Preference', 'Verbosity', 5);
  injectexternal = 1;
  multisample = 0;
  stereomode = 12;
  doGamma = 0;
  doPost = 0;
  imagingmode = kPsychNeedFinalizedFBOSinks + kPsychSinkIsMSAACapable;
  if injectexternal
    imagingmode = imagingmode + kPsychUseExternalSinkTextures;
  end

  PsychImaging('PrepareConfiguration');
  PsychImaging('AddTask', 'General', 'UseVirtualFramebuffer');
  %PsychImaging('AddTask', 'General', 'UseDisplayRotation', 90);
  %PsychImaging('AddTask', 'General', 'UsePanelFitter', [200 200], 'Centered');

  % Sample image processing step:
  if doGamma
    if stereomode == 0
      PsychImaging('AddTask', 'FinalFormatting', 'DisplayColorCorrection', 'SimpleGamma');
    else
      PsychImaging('AddTask', 'LeftView', 'DisplayColorCorrection', 'SimpleGamma');
      PsychImaging('AddTask', 'RightView', 'DisplayColorCorrection', 'SimpleGamma');
    end
  end

  % Sample post processing step:
  if doPost
    PsychImaging('AddTask', 'General', 'EnablePseudoGrayOutput');
  end

  [w, wrect] = PsychImaging('Openwindow', 0, 0.5, [0 0 300 300], [], [], stereomode, multisample, imagingmode);

  % Sample setup code for external consumer creating/providing the
  % backing textures as a triple-buffered texture swapchain:
  if injectexternal
    % Attach 1st buffer as initial render buffer:
    curtex = 0;

    [oldglleft, oldglright, target, format, multisample, width, height] = Screen('Hookfunction', w, 'GetDisplayBufferTextures');

    % Create triple-buffer textures as swapchain:
    leftTextures = glGenTextures(3);
    for i=1:3
      glBindTexture(target, leftTextures(i));
      if target ~= GL.TEXTURE_2D_MULTISAMPLE
        glTexParameteri(target, GL.TEXTURE_MIN_FILTER, GL.NEAREST);
        glTexParameteri(target, GL.TEXTURE_MAG_FILTER, GL.NEAREST);
        glTexImage2D(target, 0, format, width, height, 0, GL.RGBA, GL.UNSIGNED_BYTE, 0);
      else
        glTexImage2DMultisample(target, multisample, format, width, height, 1);
      end
    end
    glBindTexture(target, 0);

    if stereomode > 0
      rightTextures = glGenTextures(3);
      for i=1:3
        glBindTexture(target, rightTextures(i));
        if target ~= GL.TEXTURE_2D_MULTISAMPLE
          glTexParameteri(target, GL.TEXTURE_MIN_FILTER, GL.NEAREST);
          glTexParameteri(target, GL.TEXTURE_MAG_FILTER, GL.NEAREST);
          glTexImage2D(target, 0, format, width, height, 0, GL.RGBA, GL.UNSIGNED_BYTE, 0);
        else
          glTexImage2DMultisample(target, multisample, format, width, height, 1);
        end
      end
      glBindTexture(target, 0);

      Screen('Hookfunction', w, 'SetDisplayBufferTextures', '', double(leftTextures(curtex + 1)), double(rightTextures(curtex + 1)), target, format, multisample, width, height);
    else
      Screen('Hookfunction', w, 'SetDisplayBufferTextures', '', double(leftTextures(curtex + 1)), [], target, format, multisample, width, height);
    end

    if 1
      Screen('Hookfunction', w, 'AppendMFunction', 'PreSwapbuffersOperations', 'Snapshotter', 'ImagingPipelineExternalConsumerTest(0, IMAGINGPIPE_FLIPTWHEN, IMAGINGPIPE_FLIPDONTCLEAR, IMAGINGPIPE_FLIPVBLSYNCLEVEL);');
    else
      Screen('Hookfunction', w, 'AppendMFunction', 'LeftFinalizerBlitChain', 'Snapshotter', 'ImagingPipelineExternalConsumerTest(0, IMAGINGPIPE_FLIPTWHEN, IMAGINGPIPE_FLIPDONTCLEAR, IMAGINGPIPE_FLIPVBLSYNCLEVEL);');
      Screen('Hookfunction', w, 'Enable', 'LeftFinalizerBlitChain');
    end
    Screen('Hookfunction', w, 'PrependMFunction', 'CloseOnscreenWindowPreGLShutdown', 'ProducerConsumerTearDown', 'ImagingPipelineExternalConsumerTest(1)');
    Screen('Hookfunction', w, 'Enable', 'CloseOnscreenWindowPreGLShutdown');
  else
    Screen('Hookfunction', w, 'AppendMFunction', 'PreSwapbuffersOperations', 'Snapshotter', 'ImagingPipelineExternalConsumerTest(2)');
  end
  Screen('Hookfunction', w, 'Enable', 'PreSwapbuffersOperations');

  if doGamma && stereomode > 0
    PsychColorCorrection('SetEncodingGamma', w, 1/2.8, 'LeftView');
    PsychColorCorrection('SetEncodingGamma', w, 1/0.5, 'RightView');
  end
  winfo = Screen('GetWindowInfo', w)

  Screen('SelectStereoDrawBuffer', w, 0);
  Screen('FillOval', w, [1 0 0]);
  DrawFormattedText(w, 'Left', 'center', 'center', [1 1 0]);
  dl = Screen('GetImage', w, [], 'drawBuffer');

  if stereomode > 0
    Screen('SelectStereoDrawBuffer', w, 1);
    Screen('FillOval', w, [0 1 0]);
    DrawFormattedText(w, 'Right', 'center', 'center', [1 1 0]);
    dr = Screen('GetImage', w, [], 'drawBuffer');
  end

  Screen('Flip', w, [], 0);
  KbStrokeWait;
  
  while ~KbCheck
    Screen('SelectStereoDrawBuffer', w, 0);
    Screen('FillOval', w, [1 0 0]);
    DrawFormattedText(w, sprintf('%0.3f', GetSecs), 'center', 'center', [1 1 0]);
    if stereomode > 0
      Screen('SelectStereoDrawBuffer', w, 1);
      Screen('FillOval', w, [0 1 0]);
      DrawFormattedText(w, sprintf('%0.3f', GetSecs), 'center', 'center', [1 1 0]);
    end

    Screen('Flip', w, GetSecs + 0.001, [], round(mod(GetSecs, 2)));
  end
  
  if stereomode > 0
    bl = Screen('GetImage', w, [0 0 300 300], 'backLeftBuffer');
    br = Screen('GetImage', w, [0 0 300 300], 'backRightBuffer');
  else
    bl = Screen('GetImage', w, [0 0 300 300], 'backBuffer');
  end

  sca;

  figure;
  imshow(dl);
  title('Left drawBuffer drawBufferFBO[0]');
  
  if stereomode > 0
    figure;
    imshow(dr);
    title('Right drawBuffer drawBufferFBO[1]');
  end

  figure;
  imshow(bl);
  title('Back left buffer - finalizedFBO[0]');
  if stereomode > 0
    figure;
    imshow(br);
    title('Back right buffer - finalizedFBO[1]');
  end
end

function PassiveConsumer
  global GL;
  global w;
  [glleft, glright, target, format, multisample, width, height] = Screen('Hookfunction', w, 'GetDisplayBufferTextures')
  glEnable(GL.TEXTURE_2D);
  glBindTexture(GL.TEXTURE_2D, glleft);
%  pixels = glGetTexImage(GL.TEXTURE_2D, 0, GL.RGBA, GL.UNSIGNED_BYTE);
%  fbo = glGetIntegerv(GL.DRAW_FRAMEBUFFER_BINDING)
  glBegin(GL.QUADS);
  glColor4f(1,1,1,1);
  glTexCoord2f(0,1);
  glVertex2i(0,0);
  glTexCoord2f(1,1);
  glVertex2i(width,0);
  glTexCoord2f(1,0);
  glVertex2i(width,height);
  glTexCoord2f(0,0);
  glVertex2i(0,height);
  glEnd();
  glBindTexture(GL.TEXTURE_2D, 0);
  glDisable(GL.TEXTURE_2D);
%  imshow(squeeze(pixels(2, :, :))');
end

function ProducerConsumer(when, dontclear, synclevel)
  global GL;
  global w;
  global stereomode;
  global curtex;
  global leftTextures;
  global rightTextures;

  if nargin >= 3
    fprintf('synclevel %i : dontclear=%i : tWhen = %f secs.\n', synclevel, dontclear, when);
  end

  % Advance to next buffer in swapchain:
  curtex = mod(curtex + 1, length(leftTextures));

  % Dequeue old texture with brand-new rendered content, enqueue next idle buffer in chain:
  fbo1 = glGetIntegerv(GL.DRAW_FRAMEBUFFER_BINDING)
  if stereomode > 0
    [glleft, glright, target, format, multisample, width, height] = Screen('Hookfunction', w, 'SetDisplayBufferTextures', [], double(leftTextures(curtex + 1)), double(rightTextures(curtex + 1)))
  else
    [glleft, glright, target, format, multisample, width, height] = Screen('Hookfunction', w, 'SetDisplayBufferTextures', [], double(leftTextures(curtex + 1)))
  end
  if target == GL.TEXTURE_2D_MULTISAMPLE
    % Can not handle MSAA textures with fixed function pipeline.
    return;
  end

  glEnable(GL.TEXTURE_2D);
  glBindTexture(GL.TEXTURE_2D, glleft);
%  pixels = glGetTexImage(GL.TEXTURE_2D, 0, GL.RGBA, GL.UNSIGNED_BYTE);
  fbo2 = glGetIntegerv(GL.DRAW_FRAMEBUFFER_BINDING)
  glBindFramebuffer(GL.DRAW_FRAMEBUFFER, 0);
  glBegin(GL.QUADS);
  glColor4f(1,1,1,1);
  glTexCoord2f(0,1);
  glVertex2i(0,0);
  glTexCoord2f(1,1);
  glVertex2i(width,0);
  glTexCoord2f(1,0);
  glVertex2i(width,height);
  glTexCoord2f(0,0);
  glVertex2i(0,height);
  glEnd();

  if stereomode > 0
    glBindTexture(GL.TEXTURE_2D, glright);
    glBegin(GL.QUADS);
    glColor4f(1,1,1,1);
    glTexCoord2f(0,1);
    glVertex2i(width/2,height/2);
    glTexCoord2f(1,1);
    glVertex2i(width,height/2);
    glTexCoord2f(1,0);
    glVertex2i(width,height);
    glTexCoord2f(0,0);
    glVertex2i(width/2,height);
    glEnd();
  end

  
  glBindTexture(GL.TEXTURE_2D, 0);
  glDisable(GL.TEXTURE_2D);
%  imshow(squeeze(pixels(2, :, :))');
end

function ReleaseProducer
  global GL;
  global w;
  global stereomode;
  global oldglleft;
  global oldglright;
  global leftTextures;
  global rightTextures;

  % Reattach old backing textures, so it can get properly destroyed during pipeline shutdown:
  Screen('Hookfunction', w, 'SetDisplayBufferTextures', '', oldglleft, oldglright);

  % Delete our swapchain textures:
  glDeleteTextures(length(leftTextures), leftTextures);
  if stereomode > 0
    glDeleteTextures(length(leftTextures), leftTextures);
  end
end