File: SCREENSelectStereoDrawBuffer.c

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 (424 lines) | stat: -rw-r--r-- 19,286 bytes parent folder | download | duplicates (3)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 *        SCREENSelectStereoDrawBuffer.c
 *
 *        AUTHORS:
 *
 *                mario.kleiner.de@gmail.com    mk
 *
 *        PLATFORMS:
 *
 *                All.
 *
 *
 *        HISTORY:
 *
 *                04/03/05      mk      Created.
 *                10/30/05      mk      Synopsis string changed.
 *
 *        DESCRIPTION:
 *
 *                Selects the target buffer for drawing commands on a stereoscopic display:
 *                All drawing commands after this command will apply to the selected buffer.
 */

#include "Screen.h"

// If you change the useString then also change the corresponding synopsis string in ScreenSynopsis.c
static char useString[] = "currentbuffer = Screen('SelectStereoDrawBuffer', windowPtr [, bufferid] [, param1]);";
//                         1                                                1            2            3
static char synopsisString[] =
"Select the target buffer for drawing commands in stereo display mode and or return "
"the current/old buffer selection in 'currentbuffer'.\n"
"This function only applies to stereo mode, it does nothing in mono mode.\n"
"\"windowPtr\" is the pointer to the onscreen stereo window.\n"
"\"bufferid\" (optional) is either == 0 for selecting the left-eye buffer or == 1 for "
"selecting the right-eye buffer. You need to call this command after each "
"Screen('Flip') command or after drawing to an offscreen window again in order "
"to reestablish your selection of draw buffer, otherwise the results of drawing "
"operations will be undefined and most probably not what you want.\n"
"\"param1\" (optional) a parameter whose meaning depends on the active stereo mode:\n"
"In stereoModes 1 and 11 (frame sequential stereo) it allows to select if stimulus onset should "
"happen in an even video refresh interval (value 0) or in an odd interval (value 1). "
"Even and odd intervals correspond to either left- or right-eye view, so this allows "
"to choose if you want to have stimulus onset on left eye or right eye.\n"
"If you want to use a stereo display mode, we recommend enabling the imaging "
"pipeline as well. The imaging pipeline will not work with very old hardware "
"(roughly pre 2007) but it allows for more reliable stereo operation especially "
"when using low-level OpenGL for real 3D drawing. It also allows to parameterize "
"aspects of stereo presentation, e.g., gains and other settings of anaglyph stereo.\n";

static char seeAlsoString[] = "OpenWindow Flip";

PsychError SCREENSelectStereoDrawBuffer(void)
{
    PsychWindowRecordType *windowRecord;
    int bufferid, param1;

    //all subfunctions should have these two lines.
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if(PsychIsGiveHelp()){PsychGiveHelp();return(PsychError_none);};

    PsychErrorExit(PsychCapNumInputArgs(3));     //The maximum number of inputs
    PsychErrorExit(PsychRequireNumInputArgs(1)); //The required number of inputs
    PsychErrorExit(PsychCapNumOutputArgs(1));    //The maximum number of outputs

    //get the window record from the window record argument and get info from the window record
    PsychAllocInWindowRecordArg(kPsychUseDefaultArgPosition, TRUE, &windowRecord);

    if(!PsychIsOnscreenWindow(windowRecord)) PsychErrorExitMsg(PsychError_user, "Tried to select stereo draw buffer on something else than a onscreen window.");

    // Return (optionally) current buffer id:
    PsychCopyOutDoubleArg(1, FALSE, (double) windowRecord->stereodrawbuffer);

    // Get the optional buffer id (0==left, 1==right). If none provided then we're done and can return immediately:
    if (!PsychCopyInIntegerArg(2, FALSE, &bufferid)) return(PsychError_none);

    // Bufferid provided...
    if (bufferid < 0 || bufferid > 1) PsychErrorExitMsg(PsychError_user, "Invalid bufferid provided: Must be 0 for left-eye or 1 for right-eye buffer.");

    // Trying to select other than left buffer on mono-window?
    if (windowRecord->windowType != kPsychDoubleBufferOnscreen || windowRecord->stereomode == kPsychMonoscopic) {
        // Yes. Reset to left buffer, which is used for mono mode:
        bufferid = 0;
    }

    // Store assignment in windowRecord:
    windowRecord->stereodrawbuffer = bufferid;

    // Copy in optional param1 argument:
    param1 = -10000;
    PsychCopyInIntegerArg(3, FALSE, &param1);
    if (param1 != -10000) {
        if ((windowRecord->stereomode == kPsychOpenGLStereo) || (windowRecord->stereomode == kPsychFrameSequentialStereo)) {
            if (param1 < -1 || param1 > 1) PsychErrorExitMsg(PsychError_user, "Invalid fieldid provided: Must be -1 for don't care, 0 for even field or 1 for odd field.");
            // Valid: Assign it.
            windowRecord->targetFlipFieldType = param1;
        }
    }

    // If the imaging pipeline is active, then we're done.
    if (windowRecord->imagingMode & kPsychNeedFastBackingStore) {
        // Enable this windowRecords framebuffer as current drawingtarget. In imagingmode this will also
        // select the proper backing FBO:
        if (PsychGetDrawingTarget() == windowRecord) PsychSetDrawingTarget(NULL);
        PsychSetDrawingTarget(windowRecord);

        // Done for imaging mode:
        return(PsychError_none);
    }

    // The following code is only used for non-imaging mode operations:
    PsychSetDrawingTarget(windowRecord);
    PsychSwitchFixedFunctionStereoDrawbuffer(windowRecord);
    return(PsychError_none);
}

/*    This routine performs the setup work for switching between left- and right-eye
 *        drawbuffers in stereo mode when using the fixed function pipeline, i.e., imagingpipeline
 *        is disabled.
 *
 *        It is called by SCREENSelectStereoDrawBuffer() directly for setup of PTB's internal 2D
 *        drawing context. It's also called by SCREENBeginOpenGL() when performing a transition to
 *        the userspace 3D OpenGL drawing context, so both 2D and 3D contexts stay in sync for
 *        stereo drawing.
 *
 *        Setup of drawbuffers in imaging mode is much simpler, automatically performed in
 *        PsychSetDrawingTarget() whenever needed by a simple switch of the drawBuffer FBO's.
 *
 */
void PsychSwitchFixedFunctionStereoDrawbuffer(PsychWindowRecordType *windowRecord)
{
    int screenwidth, screenheight;
    int bufferid=windowRecord->stereodrawbuffer;

    if (windowRecord->windowType!=kPsychDoubleBufferOnscreen || windowRecord->stereomode == kPsychMonoscopic) {
        // Trying to select the draw target buffer on a non-stereo window: We just reset it to monoscopic default.
        glDrawBuffer(GL_BACK);
        return;
    }

    // OpenGL native stereo?
    if (windowRecord->stereomode==kPsychOpenGLStereo) {
        // OpenGL native stereo via separate back-buffers: Select target draw buffer:
        switch(bufferid) {
            case 0:
                glDrawBuffer(GL_BACK_LEFT);
                break;
            case 1:
                glDrawBuffer(GL_BACK_RIGHT);
                break;
        }
        return;
    }

    // Vertical compression stereo?
    if (windowRecord->stereomode==kPsychCompressedTLBRStereo || windowRecord->stereomode==kPsychCompressedTRBLStereo) {
        PsychSwitchCompressedStereoDrawBuffer(windowRecord, bufferid);
        return;
    }

    // "Free fusion" stereo? Simply place views side-by-side, downscaled by a factor of 2 in horizontal dimension...
    if (windowRecord->stereomode==kPsychFreeFusionStereo || windowRecord->stereomode==kPsychFreeCrossFusionStereo) {
        // Switch between drawing into left- and right-half of the single framebuffer:
        screenwidth=(int) PsychGetWidthFromRect(windowRecord->rect);
        screenheight=(int) PsychGetHeightFromRect(windowRecord->rect);

        // Cross fusion instead of fusion requested? Switch left-right if so:
        if (windowRecord->stereomode==kPsychFreeCrossFusionStereo) bufferid = 1 - bufferid;

        // Change of projection matrix is only applied if we are not executed in a userspace
        // GL context. Us changing the matrix here in conjunction with MOGL would defeat
        // 3D perpective projection code and thereby interfere with real 3D perspective rendering:
        if (!PsychIsUserspaceRendering()) {
            // Setup projection matrix for ortho-projection of full window height, but only
            // half window width:
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            if (!PsychIsGLES(windowRecord)) {
                gluOrtho2D(0, screenwidth/2, windowRecord->rect[kPsychBottom], windowRecord->rect[kPsychTop]);
            }
            else {
                glOrthof((GLfloat) 0, (GLfloat) screenwidth/2, (GLfloat) windowRecord->rect[kPsychBottom], (GLfloat) windowRecord->rect[kPsychTop], (GLfloat) -1, (GLfloat) 1);
            }

            // Switch back to modelview matrix, but leave it unaltered:
            glMatrixMode(GL_MODELVIEW);
        }

        // When using this stereo modes, we are most likely running on a
        // dual display setup with desktop set to "horizontal spanning mode". In this mode,
        // we get a virtual onscreen window that is at least twice as wide as its height and
        // the left half of the window is displayed on the left monitor, the right half of the
        // window is displayed on the right monitor. To make good use of the space, we only
        // scale the viewport horizontally to half the window width, but keep it at full height:
        // All Screen subfunctions that report the size of the onscreen window to Matlab/Octave/...
        // will report it to be only half of its real width, so experiment code can adapt to it.
        switch(bufferid) {
            case 0:
                // Place viewport in the left half of screen:
                glViewport(0, 0, screenwidth/2, screenheight);
                glScissor(0, 0, screenwidth/2, screenheight);
                break;

            case 1:
                // Place viewport in the right half of screen:
                glViewport(screenwidth/2, 0, screenwidth/2, screenheight);
                glScissor(screenwidth/2, 0, screenwidth/2, screenheight);
                break;
        }
        return;
    }

    // And now for the Anaglyph stereo modes, were the left-eye vs. right-eye images are encoded in
    // the separate color-channels of the same display. We do this via the OpenGL writemask, which
    // allows to selectively enable/disable write operations to the different color channels:
    // The alpha channel is always enabled, the red,gree,blue channels are depending on mode and
    // bufferid conditionally enabled/disabled:
    switch (windowRecord->stereomode) {
        case kPsychAnaglyphRGStereo:
            glColorMask(bufferid==0, bufferid==1, FALSE, TRUE);
            windowRecord->colorMask[0] = (bufferid==0) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[1] = (bufferid==1) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[2] = GL_FALSE;
            windowRecord->colorMask[3] = GL_TRUE;
            break;

        case kPsychAnaglyphGRStereo:
            glColorMask(bufferid==1, bufferid==0, FALSE, TRUE);
            windowRecord->colorMask[0] = (bufferid==1) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[1] = (bufferid==0) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[2] = GL_FALSE;
            windowRecord->colorMask[3] = GL_TRUE;
            break;

        case kPsychAnaglyphRBStereo:
            glColorMask(bufferid==0, FALSE, bufferid==1, TRUE);
            windowRecord->colorMask[0] = (bufferid==0) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[2] = (bufferid==1) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[1] = GL_FALSE;
            windowRecord->colorMask[3] = GL_TRUE;
            break;

        case kPsychAnaglyphBRStereo:
            glColorMask(bufferid==1, FALSE, bufferid==0, TRUE);
            windowRecord->colorMask[0] = (bufferid==1) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[2] = (bufferid==0) ? GL_TRUE : GL_FALSE;
            windowRecord->colorMask[1] = GL_FALSE;
            windowRecord->colorMask[3] = GL_TRUE;
            break;
    }
    return;
}

/*
 * PsychSwitchCompressedStereoDrawBuffer  -- Handle buffer transitions
 *
 * If in stereomode for vertically compressed stereo display, switch between the
 * two drawing buffers via some AUX buffer copy magic...
 *
 * Vertical compressed stereo works as follows:
 *
 * The left-eye view is drawn at full resolution into the backbuffer as if it would
 * be a regular monoscopic stimulus on a full-res mono-display. When user wants to
 * switch to right-eye drawing, this routine makes a full scale backup copy of the
 * framebuffer into AUX buffer 0 --> AUX0 contains the full res left-eye image.
 * Now the right eye image is drawn into the backbuffer. When switch back to left-eye
 * view occurs, the backbuffer is copied into AUX1 buffer.
 *
 * All possible transitions between the two eye buffers are handled by copying data
 * from backbuffer to AUX0/1 and back. The net result is that at Screen('DrawingFinished')
 * time or Screen('Flip') time, AUX0 contains the left-eye image, AUX1 contains the right-eye
 * image. Additional logic in PsychPreFlipOperations() makes sure this is always the case,
 * regardless what the user does. The content of both AUX buffers is now copied back into
 * the backbuffer, AUX0 into top half, AUX1 into bottom half of screen (or vice versa, depending
 * on mode). Both buffers are vertically scaled to half resolution, so both fit onto screen
 * simultaneously. This is done by PsychComposeCompressedStereoBuffer().
 *
 */
int PsychSwitchCompressedStereoDrawBuffer(PsychWindowRecordType *windowRecord, int newbuffer)
{
    // Query screen dimension:
    int screenwidth=(int) PsychGetWidthFromRect(windowRecord->rect);
    int screenheight=(int) PsychGetHeightFromRect(windowRecord->rect);

    // Query currently active buffer:
    int oldbuffer = windowRecord->stereodrawbuffer;

    // Transition necessary?
    if (oldbuffer != newbuffer) {
        // Real transition requested...

        // Disable shader:
        PsychSetShader(windowRecord, 0);

        glDisable(GL_BLEND);

        // Set transform matrix to well-defined state:
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glPixelZoom(1,1);

        // Do we have a drawbuffer active at the moment?
        if (oldbuffer!=2) {
            // Stereo drawbuffer is active: Content of our backbuffer represents
            // the content of this drawbuffer. We need to save a backup copy in
            // corresponding AUX-Buffer.
            glReadBuffer(GL_BACK);
            glDrawBuffer((oldbuffer==0) ? GL_AUX0 : GL_AUX1);
            glRasterPos2i(0, screenheight);
            glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);
            // Mark this AUX buffer as "dirty" it contains real image content
            // instead of just background color.
            windowRecord->auxbuffer_dirty[oldbuffer] = TRUE;
        }

        glDrawBuffer(GL_BACK);

        // Ok, old content backed-up if necessary. Now we switch to new buffer...
        if (newbuffer!=2) {
            // Switch to real buffer requested. Check if corresponding AUX buffer
            // is dirty, aka contains real content instead of just background color.
            if (windowRecord->auxbuffer_dirty[newbuffer]) {
                // AUX buffer contains real content. Copy its content back into
                // backbuffer:
                glReadBuffer((newbuffer==0) ? GL_AUX0 : GL_AUX1);
                glRasterPos2i(0, screenheight);
                glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);
                glReadBuffer(GL_BACK);
            }
            else {
                // AUX buffer is clean, aka just contains background color. We do
                // a clear screen as its faster than an AUX->BACK copy.
                PsychGLClear(windowRecord);
            }
        }

        glEnable(GL_BLEND);

        // Restore transform matrix:
        glPopMatrix();

        // Store new state:
        windowRecord->stereodrawbuffer = newbuffer;

        // Transition finished. Backbuffers content represents new draw buffers content.
    }

    // Done. Return id of previous buffer (0=left, 1=right, 2=none).
    return(oldbuffer);
}

/*
 * PsychComposeCompressedStereoBuffer - Final compositing for compressed stereo.
 *
 * This routine copies both AUX buffers (0 and 1) back into the backbuffer, each of them
 * vertically scaled/compressed by a factor of 2. Its called by PsychPreFlipOperations().
 */
void PsychComposeCompressedStereoBuffer(PsychWindowRecordType *windowRecord)
{
    /*
     *    if (FALSE) {
     *        // Upload, setup and enable Anaglyph stereo fragment shader:
     *        const float redgain=1.0, greengain=0.7, bluegain=0.0;
     *
     *        // This is the shader source code:
     *        const char anaglyphshader[] =
     *        "!!ARBfp1.0 "
     *        "PARAM ColorToGrayWeights = { 0.3, 0.59, 0.11, 1.0 }; "
     *        "TEMP luminance; "
     *        "TEMP incolor;"
     *        //"MOV incolor, fragment.color;"
     *        "TEX incolor, fragment.texcoord[0], texture[0], RECT;"
     *        "DP3 luminance, incolor, ColorToGrayWeights; "
     *        "MUL result.color.rgb, luminance, program.env[0]; "
     *        "MOV result.color.a, ColorToGrayWeights.a; "
     *        //"MOV result.color.a, fragment.color.a; "
     *        "END";
     *
     *        // Upload and compile shader:
     *        PsychTestForGLErrors();
     *        glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(anaglyphshader), anaglyphshader);
     *        PsychTestForGLErrors();
     *        // Setup the rgb-gains as global parameters for the shader:
     *        glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, redgain, greengain, bluegain, 0.0);
     *        // Enable the shader:
     *        glEnable(GL_FRAGMENT_PROGRAM_ARB);
     *    }
     */

    // Query screen dimension:
    int screenwidth=(int) PsychGetWidthFromRect(windowRecord->rect);
    int screenheight=(int) PsychGetHeightFromRect(windowRecord->rect);

    // When entering this routine, the modelview matrix is already set to identity and
    // the proper OpenGL context is active.

    // Disable shader:
    PsychSetShader(windowRecord, 0);

    // Set up zoom for vertical compression:
    glPixelZoom(1, 0.5f);
    glDrawBuffer(GL_BACK);
    glDisable(GL_BLEND);

    // Draw left view aka AUX0:
    glReadBuffer(GL_AUX0);
    glRasterPos2i(0, (windowRecord->stereomode==kPsychCompressedTLBRStereo) ? screenheight/2 : screenheight);
    glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);

    // Draw right view aka AUX1:
    glReadBuffer(GL_AUX1);
    glRasterPos2i(0, (windowRecord->stereomode==kPsychCompressedTLBRStereo) ? screenheight : screenheight/2);
    glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);

    // Restore settings:
    glReadBuffer(GL_BACK);
    glPixelZoom(1,1);
    glEnable(GL_BLEND);

    // Unconditionally disable fragment shaders:
    // glDisable(GL_FRAGMENT_PROGRAM_ARB);

    // Done.
    return;
}