WindowPtr and opengl context ??

Hi,

I am a new user of this wonderful toolbox and came acros a problem while trying to use shaders.

My scenario:


I have 2 onscreen windows. I want to use a fixed rendering pipeline on the first and a shader on the second. Now the problem is that my shader only seems  to be working on the 1st window and not on the second. So I was wondering if the 2 windows share the same opengl context or not.  I create the shader and pass texture location as follow:


glsl = LoadGLSLProgramFromFiles(filename,1);

glUseProgram(glsl);

glUniform1i(glGetUniformLocation(glsl, 'Image'), 0);

glUseProgram(0);


Help much appreciated.

Many thanks

Psychtoolbox uses up to 3 different OpenGL contexts per onscreen window, but all contexts of all windows are set up to share resources like shaders and textures whenever the underlying OS allows this. There must be something else wrong with your code, so we'd need to see enough of your code to judge this.

-mario

---In PSYCHTOOLBOX@yahoogroups.com, <seckalou46@...> wrote :

Hi,

I am a new user of this wonderful toolbox and came acros a problem while trying to use shaders.

My scenario:


I have 2 onscreen windows. I want to use a fixed rendering pipeline on the first and a shader on the second. Now the problem is that my shader only seems  to be working on the 1st window and not on the second. So I was wondering if the 2 windows share the same opengl context or not.  I create the shader and pass texture location as follow:


glsl = LoadGLSLProgramFromFiles(filename,1);

glUseProgram(glsl);

glUniform1i(glGetUniformLocation(glsl, 'Image'), 0);

glUseProgram(0);


Help much appreciated.

Many thanks

Hi Mario,

I attached/copied my sources. Basically I am trying to do realtime processing on video while displaying the result in another window. In a nutshell here are the steps I am trying to accomplish:

1) copy downsampled version of current frame of playback to CPU
2) apply my custom matlab function
3) upload the result along with original frame to second window where there are blended using a shader.

I am sure I am doing something wrong, I just can't figure out what.

Many thanks


--------------------------------------------------------------------------------------------------------------------------------------------------------


function MyDemo()

Screen('Preference', 'SkipSyncTests', 1);

% Check if Psychtoolbox is properly installed:
AssertOpenGL;

moviename = [ 'risen_trailer_c.mp4' ];

windowrect = [];

% Wait until user releases keys on keyboard:
KbReleaseWait;

% Select screen for display of movie:
screenid = max(Screen('Screens'));

scale = 0.25;

try
    % Open 1st window (playback input. Use opengl fixed pipeline to render):
    [win, w_size]= Screen('OpenWindow', screenid, [], windowrect);
    [c_x, c_y] = RectCenter(w_size);
    
% Open an offscreen window keeping downsample version of current frame (to move to CPU)
[win1, w1_size] = Screen('OpenOffscreenWindow', screenid, [], [0 0 w_size(3)*scale w_size(4)*scale]);
% Open 2nd window (playback output. Use shader with multitexture)
    win2 = Screen('OpenWindow', screenid - 1, [], windowrect);
    
% Setup Uniform location for texture mapping in shader
    glsl = LoadGLSLProgramFromFiles('C:\toolbox\Psychtoolbox\PsychDemos\MovieDemos\shader',1);
    glUseProgram(glsl);
    glUniform1i(glGetUniformLocation(glsl, 'Image'), 0);
    glUniform1i(glGetUniformLocation(glsl, 'Image1'), 1);
    glUseProgram(0);
    
    % Open movie file:
    movie = Screen('OpenMovie', win, moviename);
    
    % Start playback engine:
    Screen('PlayMovie', movie, 1);
    
    % Playback loop: Runs until end of movie or keypress:
    n=0;
    
    while ~KbCheck
        n=n+1;
        
        pause(1/90);
        
        tex = Screen('GetMovieImage', win, movie,0, [], 1);
        
        if tex<0
            break;
        end
        
        % Get form GPU
        cap = Screen('GetImage', win1) ; %, crop_rect);
        
% Process in CPU
gain = myfunction(cap);
        
        % Copy back to GPU on win2
        tex1 = Screen('MakeTexture', win2, cap, [], 1 );
        tex2 = Screen('MakeTexture', win2, gain, [], 1 );
        
        if tex == 0
            continue;
        end
        
        % Draw the new texture immediately to screen:
        Screen('DrawTexture', win, tex, [], [], [],[],[],[]);
        Screen('DrawTexture', win1, tex, [],[0 0 w_size(3)*scale w_size(4)*scale],[],1);
        Screen('DrawTextures',win2, [tex1 tex2], [], [], [],[],[],[], glsl);
        
        Screen('Flip', win);
        Screen('Flip', win2);
        
        % Release texture:
        Screen('Close', tex);
        Screen('Close', tex1);
        
    end
    
    toc
    
    
    % Stop playback:
    Screen('PlayMovie', movie, 0)
    
    % Close movie:
    Screen('CloseMovie', movie);
    
    % Close Screen, we're done:
    Screen('CloseAll');
    
   
    
catch %#ok<CTCH>
    sca;
    psychrethrow(psychlasterror);
end

return


The script is a bit convoluted.

The shaders make wrong assumptions about how Screen's shader handling works, but the shaders themselves also seem to be somewhat non-sensical. What they may try to do could be achieves as/more easily without them.

So what exactly do you want to achieve? Also which operating system is this? And what does your custom Matlab function do?

-mario


---In PSYCHTOOLBOX@yahoogroups.com, <seckalou46@...> wrote :

Hi Mario,

I attached/copied my sources. Basically I am trying to do realtime processing on video while displaying the result in another window. In a nutshell here are the steps I am trying to accomplish:

1) copy downsampled version of current frame of playback to CPU
2) apply my custom matlab function
3) upload the result along with original frame to second window where there are blended using a shader.

I am sure I am doing something wrong, I just can't figure out what.

Many thanks


--------------------------------------------------------------------------------------------------------------------------------------------------------


function MyDemo()

Screen('Preference', 'SkipSyncTests', 1);

% Check if Psychtoolbox is properly installed:
AssertOpenGL;

moviename = [ 'risen_trailer_c.mp4' ];

windowrect = [];

% Wait until user releases keys on keyboard:
KbReleaseWait;

% Select screen for display of movie:
screenid = max(Screen('Screens'));

scale = 0.25;

try
    % Open 1st window (playback input. Use opengl fixed pipeline to render):
    [win, w_size]= Screen('OpenWindow', screenid, [], windowrect);
    [c_x, c_y] = RectCenter(w_size);
    
% Open an offscreen window keeping downsample version of current frame (to move to CPU)
[win1, w1_size] = Screen('OpenOffscreenWindow', screenid, [], [0 0 w_size(3)*scale w_size(4)*scale]);
% Open 2nd window (playback output. Use shader with multitexture)
    win2 = Screen('OpenWindow', screenid - 1, [], windowrect);
    
% Setup Uniform location for texture mapping in shader
    glsl = LoadGLSLProgramFromFiles('C:\toolbox\Psychtoolbox\PsychDemos\MovieDemos\shader',1);
    glUseProgram(glsl);
    glUniform1i(glGetUniformLocation(glsl, 'Image'), 0);
    glUniform1i(glGetUniformLocation(glsl, 'Image1'), 1);
    glUseProgram(0);
    
    % Open movie file:
    movie = Screen('OpenMovie', win, moviename);
    
    % Start playback engine:
    Screen('PlayMovie', movie, 1);
    
    % Playback loop: Runs until end of movie or keypress:
    n=0;
    
    while ~KbCheck
        n=n+1;
        
        pause(1/90);
        
        tex = Screen('GetMovieImage', win, movie,0, [], 1);
        
        if tex<0
            break;
        end
        
        % Get form GPU
        cap = Screen('GetImage', win1) ; %, crop_rect);
        
% Process in CPU
gain = myfunction(cap);
        
        % Copy back to GPU on win2
        tex1 = Screen('MakeTexture', win2, cap, [], 1 );
        tex2 = Screen('MakeTexture', win2, gain, [], 1 );
        
        if tex == 0
            continue;
        end
        
        % Draw the new texture immediately to screen:
        Screen('DrawTexture', win, tex, [], [], [],[],[],[]);
        Screen('DrawTexture', win1, tex, [],[0 0 w_size(3)*scale w_size(4)*scale],[],1);
        Screen('DrawTextures',win2, [tex1 tex2], [], [], [],[],[],[], glsl);
        
        Screen('Flip', win);
        Screen('Flip', win2);
        
        % Release texture:
        Screen('Close', tex);
        Screen('Close', tex1);
        
    end
    
    toc
    
    
    % Stop playback:
    Screen('PlayMovie', movie, 0)
    
    % Close movie:
    Screen('CloseMovie', movie);
    
    % Close Screen, we're done:
    Screen('CloseAll');
    
   
    
catch %#ok<CTCH>
    sca;
    psychrethrow(psychlasterror);
end

return


Many thanks Mario,

I finally solved my problem using only opengl commands. I will try the solutions you advised to see if they could simplify the code.

Thanks