Delayed flip times during a gif-like animation loop

Hi all,

I've been using Psychtoolboox for creating a gif-like image sequence within a window. Code seems to work fine until around 15th loop. After that Screen('Flip') command starts to get delayed for 150 ms and animation slows down. If i add a little pause time (couple of seconds) before the animation loop starts, then Screen('Flip') starts to function with the same amount of delay without the un-delayed Flips. 


This is a representation of image flow:
sam1 (ISI: 85ms) -> sam2 (Target: 185ms) ->sam1 (ISI: 85ms)-> sam3 (Target: 185ms)

I'm using 'printtime' matrice for checking Flip delays. 
I've used tic & toc for measuring Flip delays, i had the same problem. I've tried to synchronize Flips by using when arguments however it did not solve the problem. I've Flipped screens without two stimulus durations (without adding samduration&samlatency into when argument of Screen(Flip)) then there was no delay in Flipping -at least it was not 150ms-, but the images sometimes lose their order of Flipping. 


Does this problem stem from using DrawTexture command and .jpg images? Would i experience the same problem If I used FillRect command and manually create all the images within Matlab?

Here is my code;

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

close all;

sca;

PsychDefaultSetup(2);

KbName('UnifyKeyNames');     

%%Window    

screens=Screen('Screens');

screenNumber=max(screens);

white=WhiteIndex(screenNumber);

black=BlackIndex(screenNumber);    

grey=white/2;

%OpenWindow%

PsychImaging('PrepareConfiguration');

PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');

[window,windowrect]=PsychImaging('OpenWindow',screenNumber,white);

[Xcenter,Ycenter]=RectCenter(windowrect);

%RefreshRate

ifi=Screen('GetFlipInterval',window);

toppriorityLevel=MaxPriority(window,'Screen');

numFrames=round(numSecs/ifi);

waitframes=1;

screenwait=(waitframes-0.5)*ifi;

HideCursor();


%%Import Jpg Images%%

SAM1="sam1.jpg";SAM1copy="sam1 - Copy.jpg";

SAM2="sam2.jpg";SAM3="sam3.jpg";

%%Read JPGs & Convert into Textures%%

SAMfirst=imread(SAM1,'jpg');SAMfirstcopy=imread(SAM1copy,'jpg');

SAMsecond=imread(SAM2,'jpg');SAMthird=imread(SAM3,'jpg');

%%Make Texture%%

samfirstTEXT=Screen('MakeTexture',window,SAMfirst);

samfirstTEXTcopy=Screen('MakeTexture',window,SAMfirstcopy);

samsecondTEXT=Screen('MakeTexture',window,SAMsecond);

samthirdTEXT=Screen('MakeTexture',window,SAMthird);

%%Durations%%

trainingduration=120; 

samlatency=0.085;

samduration=0.185;


%%KeyCodes%%

switchkey=KbName('space');

escapekey=KbName('ESCAPE');


%%%Response & Timer Sheets%%%

response_sheet=zeros(1,100);

time_sheet=zeros(1,100);


%%Flip Interval Sheet%%

printtime=nan(240,6);


%%%KbQueue Initiate%%

experiment_start=GetSecs;

KbQueueCreate;

KbQueueStart;


%%Loop & Response Iteration Marker%%

i=0;

sheetmarker=0;

%%Loop Start Timer%%

samtimer=tic;

vb=Screen('Flip',window);

Priority(toppriorityLevel);

while toc(samtimer)<trainingduration

        i=i+1;

        Screen('DrawTexture',window,samfirstTEXT);

        Screen('DrawingFinished',window,2);

        vb2=Screen('Flip',window,vb+samduration+screenwait);         

        a=vb2-vb;

        printtime(i,1)=a;

            Screen('DrawTexture',window,samsecondTEXT);

            Screen('DrawingFinished',window,2); 

            vb3=Screen('Flip', window,vb2+samlatency+screenwait);

            b=vb3-vb2;

            printtime(i,2)=b;

                Screen('DrawTexture',window,samfirstTEXTcopy);

                Screen('DrawingFinished',window,2);

                vb4=Screen('Flip',window,vb3+samduration+screenwait);

                wth=vb4-experiment_start;

                c=vb4-vb3;

                printtime(i,3)=c;

                    Screen('DrawTexture',window,samthirdTEXT);

                    Screen('DrawingFinished',window,2);

                    vb=Screen('Flip',window,vb4+samlatency+screenwait);

                    d=vb-vb4;

                    printtime(i,4)=d; 

                    

            %%%%%Keyboard Input Check, Response & Reaction Time Save%%%%%%

            [pressed, firstPress]=KbQueueCheck(); 

            if pressed 

            firstPress(find(firstPress==0))=NaN;  %#ok<FNDSB>

            reactiontimevector=firstPress-experiment_start;

            reactiontimes=reactiontimevector(reactiontimevector>0.05);        

            [endtime, Index]=min(firstPress); 

            thekeys=KbName(Index); 

                if KbName(thekeys)==switchkey

                    sheetmarker=sheetmarker+1;

                    response_sheet(1,sheetmarker)=1;

                    time_sheet(1,sheetmarker)=min(reactiontimes);

                elseif KbName(thekeys)==escapekey

                    break;

                KbQueueFlush;

                end

            end    

end

Priority(0);

toc(samtimer);

ShowCursor;

KbQueueFlush;

sca;

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







Hi all,

I've been using Psychtoolboox for creating a gif-like image sequence within a window. Code seems to work fine until around 15th loop. After that Screen('Flip') command starts to get delayed for 150 ms and animation slows down. If i add a little pause time (couple of seconds) before the animation loop starts, then Screen('Flip') starts to function with the same amount of delay without the un-delayed Flips.


I see a few mildly weird things in your code, but nothing that should cause extra 150 msecs delays after some loop iterations:

1. The Screen('Drawingfinished') is redundant if it is followed immediately by a 'Flip', and the dontclear flag 2 contradicts what you specify in Flip.
2. The + screenwait in 'Flip' makes no sense if you specify a wait time in seconds.
3. The Priority() should go before the first 'flip', as it can take a longer time.

You should just drop 2 and 3 in your case, but none of this should cause the delay you describe.
What is the hardware + software setup, ie., which operating system and version, which graphics card, number and type and configuration of monitors connected etc.? Any warnings from PTB during startup etc.?

-mario


This is a representation of image flow:
sam1 (ISI: 85ms) -> sam2 (Target: 185ms) ->sam1 (ISI: 85ms)-> sam3 (Target: 185ms)

I'm using 'printtime' matrice for checking Flip delays.
I've used tic & toc for measuring Flip delays, i had the same problem. I've tried to synchronize Flips by using when arguments however it did not solve the problem. I've Flipped screens without two stimulus durations (without adding samduration&samlatency into when argument of Screen(Flip)) then there was no delay in Flipping -at least it was not 150ms-, but the images sometimes lose their order of Flipping.


Does this problem stem from using DrawTexture command and .jpg images? Would i experience the same problem If I used FillRect command and manually create all the images within Matlab?

Here is my code;

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

close all;

sca;

PsychDefaultSetup(2);

KbName('UnifyKeyNames');

%%Window

screens=Screen('Screens');

screenNumber=max(screens);

white=WhiteIndex(screenNumber);

black=BlackIndex(screenNumber);

grey=white/2;

%OpenWindow%

PsychImaging('PrepareConfiguration');

PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');

[window,windowrect]=PsychImaging('OpenWindow',screenNumber,white);

[Xcenter,Ycenter]=RectCenter(windowrect);

%RefreshRate

ifi=Screen('GetFlipInterval',window);

toppriorityLevel=MaxPriority(window,'Screen');

numFrames=round(numSecs/ifi);

waitframes=1;

screenwait=(waitframes-0.5)*ifi;

HideCursor();


%%Import Jpg Images%%

SAM1="sam1.jpg";SAM1copy="sam1 - Copy.jpg";

SAM2="sam2.jpg";SAM3="sam3.jpg";

%%Read JPGs & Convert into Textures%%

SAMfirst=imread(SAM1,'jpg');SAMfirstcopy=imread(SAM1copy,'jpg');

SAMsecond=imread(SAM2,'jpg');SAMthird=imread(SAM3,'jpg');

%%Make Texture%%

samfirstTEXT=Screen('MakeTexture',window,SAMfirst);

samfirstTEXTcopy=Screen('MakeTexture',window,SAMfirstcopy);

samsecondTEXT=Screen('MakeTexture',window,SAMsecond);

samthirdTEXT=Screen('MakeTexture',window,SAMthird);

%%Durations%%

trainingduration=120;

samlatency=0.085;

samduration=0.185;


%%KeyCodes%%

switchkey=KbName('space');

escapekey=KbName('ESCAPE');


%%%Response & Timer Sheets%%%

response_sheet=zeros(1,100);

time_sheet=zeros(1,100);


%%Flip Interval Sheet%%

printtime=nan(240,6);


%%%KbQueue Initiate%%

experiment_start=GetSecs;

KbQueueCreate;

KbQueueStart;


%%Loop & Response Iteration Marker%%

i=0;

sheetmarker=0;

%%Loop Start Timer%%

samtimer=tic;

vb=Screen('Flip',window);

Priority(toppriorityLevel);

while toc(samtimer)<trainingduration

i=i+1;

Screen('DrawTexture',window,samfirstTEXT);

Screen('DrawingFinished',window,2);

vb2=Screen('Flip',window,vb+samduration+screenwait);

a=vb2-vb;

printtime(i,1)=a;

Screen('DrawTexture',window,samsecondTEXT);

Screen('DrawingFinished',window,2);

vb3=Screen('Flip', window,vb2+samlatency+screenwait);

b=vb3-vb2;

printtime(i,2)=b;

Screen('DrawTexture',window,samfirstTEXTcopy);

Screen('DrawingFinished',window,2);

vb4=Screen('Flip',window,vb3+samduration+screenwait);

wth=vb4-experiment_start;

c=vb4-vb3;

printtime(i,3)=c;

Screen('DrawTexture',window,samthirdTEXT);

Screen('DrawingFinished',window,2);

vb=Screen('Flip',window,vb4+samlatency+screenwait);

d=vb-vb4;

printtime(i,4)=d;

%%%%%Keyboard Input Check, Response & Reaction Time Save%%%%%%

[pressed, firstPress]=KbQueueCheck();

if pressed

firstPress(find(firstPress==0))=NaN; %#ok<FNDSB>

reactiontimevector=firstPress-experiment_start;

reactiontimes=reactiontimevector(reactiontimevector>0.05);

[endtime, Index]=min(firstPress);

thekeys=KbName(Index);

if KbName(thekeys)==switchkey

sheetmarker=sheetmarker+1;

response_sheet(1,sheetmarker)=1;

time_sheet(1,sheetmarker)=min(reactiontimes);

elseif KbName(thekeys)==escapekey

break;

KbQueueFlush;

end

end

end

Priority(0);

toc(samtimer);

ShowCursor;

KbQueueFlush;

sca;

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





Hi thanks for the reply!


**
What is the hardware + software setup, ie., which operating system and version, which graphics card, number and type and configuration of monitors connected etc.? Any warnings from PTB during startup etc.?
**

Its Windows 10 & Matlab R2017a. Im using a Lenovo z510 idepad. It has GeForce GT 740M graphics card and Intel HD Graphics 4600 driver and so far i've only used laptop's monitor.

Sorry i've missed it at the first but PTB gives these warnings:


---
PTB-WARNING: Call to PsychAvSetMmMaxThreadCharacteristics() for Vista-MMCSS scheduling failed for threadhandle 00007FF944898790. Setting thread priority to HIGHEST as a work-around...
PTB-INFO: The detected endline of the vertical blank interval is equal or lower than the startline. This indicates
PTB-INFO: that i couldn't detect the duration of the vertical blank interval and won't be able to correct timestamps
PTB-INFO: for it. This will introduce a very small and constant offset (typically << 1 msec). Read 'help BeampositionQueries'
PTB-INFO: for how to correct this, should you really require that last few microseconds of precision.
PTB-INFO: Btw. this can also mean that your systems beamposition queries are slightly broken. It may help timing precision to
PTB-INFO: enable the beamposition workaround, as explained in 'help ConserveVRAMSettings', section 'kPsychUseBeampositionQueryWorkaround'.
PTB-WARNING: Call to PsychAvSetMmMaxThreadCharacteristics() for Vista-MMCSS scheduling failed for threadhandle 00007FF944898790. Setting thread priority to HIGHEST as a work-around...
---
Ok, this is an Optimus laptop, which is pretty hopeless wrt. visual timing on MS-Windows. All kinds of timing problems are to be expected, with no way to fix them.

You could try disabling Optimus/The NVidia gpu for Psychtoolbox -- there's some way to do this for Matlab either by right-clicking on the Matlab icon and then somewhere in the context menu, or in its "Properties" submenu, or in the NVidia display control panel -- defining per-application settings. Or you could go into the system BIOS setup and disable the NVidia gpu completely. Using only the Intel onboard graphics might work ok for simple tasks like yours, assuming the Intel graphics driver is not too buggy - which it is in my experience in a large fraction of all Windows laptops.

For MS-Windows you should generally avoid PC's/Laptops with Intel graphics or Intel + NVidia/AMD graphics, they are usually troublemakers. Pure AMD or NVidia graphics may work better.

Other than that, upgrading to Linux, e.g., Ubuntu 18.04.1-LTS would very likely solve the problem.
-mario


On Wed, Oct 3, 2018 at 8:49 AM kurtulusmertkucuk@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com> wrote:


Hi thanks for the reply!

**
What is the hardware + software setup, ie., which operating system and version, which graphics card, number and type and configuration of monitors connected etc.? Any warnings from PTB during startup etc.?
**

Its Windows 10 & Matlab R2017a. Im using a Lenovo z510 idepad. It has GeForce GT 740M graphics card and Intel HD Graphics 4600 driver and so far i've only used laptop's monitor.

Sorry i've missed it at the first but PTB gives these warnings:

---
PTB-WARNING: Call to PsychAvSetMmMaxThreadCharacteristics() for Vista-MMCSS scheduling failed for threadhandle 00007FF944898790. Setting thread priority to HIGHEST as a work-around...
PTB-INFO: The detected endline of the vertical blank interval is equal or lower than the startline. This indicates
PTB-INFO: that i couldn't detect the duration of the vertical blank interval and won't be able to correct timestamps
PTB-INFO: for it. This will introduce a very small and constant offset (typically << 1 msec). Read 'help BeampositionQueries'
PTB-INFO: for how to correct this, should you really require that last few microseconds of precision.
PTB-INFO: Btw. this can also mean that your systems beamposition queries are slightly broken. It may help timing precision to
PTB-INFO: enable the beamposition workaround, as explained in 'help ConserveVRAMSettings', section 'kPsychUseBeampositionQueryWorkaround'.
PTB-WARNING: Call to PsychAvSetMmMaxThreadCharacteristics() for Vista-MMCSS scheduling failed for threadhandle 00007FF944898790. Setting thread priority to HIGHEST as a work-around...
---

kurtulusmertkucuk@... [PSYCHTOOLBOX] <PSYCHTOOLBOX@yahoogroups.com> wrote:
> Aha i see. Thanks a lot for the help, really appreciated. Ill try each one of these solutions.
> One more question, is it possible to fix this by dual booting with ubuntu or should i get rid of w10 entirely?

Dual-Booting is fine.
-mario
Good to hear, thanks a lot!