Jump to content

Flip takes 30 ms o_O


j.l.ocks
 Share

Recommended Posts

Hello everyone. I have a question about Flip function.

 

In my programm the render time is about 11-12 milliseconds (i cannot show you the code), and the flip time is about 29-30 milliseconds. And by saying flip time i mean

 

start = clock();
Flip(0) ;
printf("flip: %d\n", clock()-start);

 

And it takes 30 milliseconds.

 

So the question is: how can it be?

 

If it's important, i use a lot of buffers of different sizes.

 

P.S. VC++, LE 2.4

Link to comment
Share on other sites

..hello there...i really wanna help, but unfortunately , i found it rather difficult with provided pretty much none of necessary info about what your code does and how it look like..sample u post is..

 

start = clock();

Flip(0) ;

printf("flip: %d\n", clock()-start);

 

well..nothing to look for error....flip is NOT a reason for slowdown, but what you do, before it and that particular data missing..

 

Link to comment
Share on other sites

May be it's a little misunderstanding. I'm trying to find out why this particular bit of code takes 30 milliseconds of time.

 

I call clock() function right before Flip(0), and than, right after Flip(0) i call clock() again. Difference between this two moments in time is 30 milliseconds. That means that Flip(0) function is slowing down the programm.

 

Basically, programm looks like this

 

int main( int argn, char* argv[] ){
   // some initialization code
   while( !KeyHit() && !AppTerminate() ) // main loop
       start = clock();
       //=====================================
       // A lot of rendering code
       //=====================================
       printf("rendering time: %d\n", clock()-start); 
       // the result in output window is around 11-12

       start = clock();
       Flip(0);
       printf("flip time: %d\n", clock()-start); 
       // the result in output window is around 29-30
   }
}

 

So Flip(0) function takes more time then all the rendering code. How can it be? Why Flip(0) function takes so long to process?

 

I have a guess, that Flip(0) is so slow, because of using a lot of different buffers of different size, can it be a problem? Does Flip(0) clear all the buffers when called or something like that?

Link to comment
Share on other sites

Ok. I think you will not understend me without any code, so i've simplified the programm, but problem still here.

 

here is the code

 

#include <time.h>

#include "engine.h"

#include "ProcessScene.h"

 

int width = 800;

int height = 600;

 

int main( int argn, char* argv[] )

{

//=============================================================

// Initializing the engine and graphics

//=============================================================

 

Initialize() ;

RegisterAbstractPath("E:/GameDev(zona17)/Leadwerks Engine SDK 2.40");

SetAppTitle( "Basic Shader Pack" ) ;

Graphics( width, height,0,60,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER ) ;

 

int wb2 = width/2; // For center of the screen

int hb2 = height/2; // And some shaders

 

//=============================================================

// Initialize all the worlds and cameras

//=============================================================

 

// main world

TWorld world = CreateWorld() ;

if (!world) {

MessageBoxA(0,"Error","Failed to create world.",0);

return Terminate();

}

 

// main world camera

TCamera camera = CreateCamera();

CameraClearMode(camera,BUFFER_DEPTH|BUFFER_COLOR1|BUFFER_COLOR2);

 

// back world

TWorld backworld = CreateWorld();

 

// back world camera

TCamera backcamera = CreateCamera();

CameraClearMode(backcamera,BUFFER_COLOR|BUFFER_DEPTH);

 

//=============================================================

// Initialize all the buffers

//=============================================================

 

SetWorld(world);

 

// buffer for rendering worlds

TBuffer gbuffer = CreateBuffer(width,height,BUFFER_DEPTH|BUFFER_COLOR0|BUFFER_COLOR1|BUFFER_COLOR2);

TextureFilter(GetDepthBuffer(gbuffer),TEXFILTER_SMOOTH);

 

// buffer for rendering lights and post effects

TBuffer tePostbuffer = CreateBuffer(width,height,BUFFER_DEPTH|BUFFER_COLOR0|BUFFER_COLOR1);

 

// additional buffers for rendering post effects

TBuffer tempbuffer5 = CreateBuffer(width/16,height/16,BUFFER_COLOR);

TBuffer tempbuffer4 = CreateBuffer(width/8,height/8,BUFFER_COLOR);

TBuffer tempbuffer3 = CreateBuffer(width/4,height/4,BUFFER_COLOR);

TBuffer tempbuffer2 = CreateBuffer(width/2,height/2,BUFFER_COLOR);

TBuffer tempbuffer1 = CreateBuffer(width,height,BUFFER_COLOR);

 

TextureFilter(GetColorBuffer(tempbuffer2),TEXFILTER_SMOOTH);

TextureFilter(GetColorBuffer(tempbuffer3),TEXFILTER_SMOOTH);

TextureFilter(GetColorBuffer(tempbuffer4),TEXFILTER_SMOOTH);

TextureFilter(GetColorBuffer(tempbuffer5),TEXFILTER_SMOOTH);

 

TBuffer bluredbuffer5 = CreateBuffer(width/16,height/16,BUFFER_COLOR);

TBuffer bluredbuffer4 = CreateBuffer(width/8,height/8,BUFFER_COLOR);

TBuffer bluredbuffer3 = CreateBuffer(width/4,height/4,BUFFER_COLOR);

TBuffer bluredbuffer2 = CreateBuffer(width/2,height/2,BUFFER_COLOR);

TBuffer bluredbuffer1 = CreateBuffer(width,height,BUFFER_COLOR);

 

TBuffer downcolor4 = CreateBuffer(width/8,height/8,BUFFER_COLOR);

TBuffer downcolor3 = CreateBuffer(width/4,height/4,BUFFER_COLOR);

TBuffer downcolor2 = CreateBuffer(width/2,height/2,BUFFER_COLOR);

 

//=============================================================

// Initialize all the shaders

//=============================================================

 

// Bluring shader

TShader teBlurPass = LoadShader("abstract::tePost.vert","abstract::teBlurPass.frag");

 

// Just draw it

TShader teNoShader = LoadShader("abstract::tePost.vert","abstract::teNoShader.frag");

 

// Anti Aliasing shaders

TShader teAA = LoadShader("abstract::tePost.vert","abstract::teAA.frag");

TShader teEdgeDetection = LoadShader("abstract::tePost.vert","abstract::teEdgeDetection.frag");

TShader teEdgeDeletion = LoadShader("abstract::tePost.vert","abstract::teEdgeDeletion.frag");

 

// Up and Down scaling shders

TShader teUpPass = LoadShader("abstract::tePost.vert","abstract::teUpPass.frag");

TShader teDownPass = LoadShader("abstract::tePost.vert","abstract::teDownPass.frag");

 

// Bloom shaders

TShader teBloomPass = LoadShader("abstract::tePost.vert","abstract::teBloomPass.frag");

TShader teBloomRender = LoadShader("abstract::tePost.vert","abstract::teBloomRender.frag");

 

// Sky and Sun rendering shaders

TShader teSkyRender = LoadShader("abstract::teSkyRender.vert","abstract::teSkyRender.frag");

TShader teSkyBlurPass = LoadShader("abstract::tePost.vert","abstract::teSkyBlurPass.frag");

 

// Sun Shafts shaders

TShader teSunShaftsPass = LoadShader("abstract::tePost.vert","abstract::teSunShaftsPass.frag");

TShader teSunShaftsRender = LoadShader("abstract::tePost.vert","abstract::teSunShaftsRender.frag");

 

// Texture for noise

TTexture noisemap = LoadTexture("abstract::noise.dds");

 

//=============================================================

// Initialize sun and sky

//=============================================================

 

SetWorld(world);

 

TLight thesun = CreateDirectionalLight();

RotateEntity(thesun,Vec3(170.0f,35.0f,0.0f));

MoveEntity(thesun,Vec3(0.0,0.0,5000.0f));

EntityColor(thesun,Vec4(0.0f,0.0f,0.0f,1.0f));

AmbientLight(Vec3(0.2f,0.2f,0.4f));

SetShadowDistance(thesun, 8, 0);

SetShadowDistance(thesun, 32, 1);

SetShadowDistance(thesun, 128, 2);

SetShadowOffset(thesun, 0.1f, 0.99935f, 0);

SetShadowOffset(thesun, 0.2f, 0.99935f, 1);

SetShadowOffset(thesun, 0.3f, 0.99935f, 2);

SetShadowmapSize(thesun,1024);

 

SetWorld(backworld);

 

TMaterial skymat = LoadMaterial("abstract::skynight.mat");

TEntity skybox = CreateCube();

FlipMesh(skybox);

PaintEntity(skybox,skymat);

 

//=============================================================

// Initialize scene and Controller

//=============================================================

 

SetWorld(world);

 

OcclusionCulling(0);

 

Collisions(1,1,true);

Collisions(1,2,true);

 

TEntity mainscene = LoadScene("abstract::testscene2.sbx");

ProcessScene(mainscene);

EntityType(mainscene,2);

SetVegetationShadowMode(1);

 

TController character = CreateController(1.8f,0.4f,0.5f,45.01f);

SetBodyDamping(character,0.0f,0.0f);

SetWorldGravity(Vec3(0.0f,-10.0f,0.0f));

SetBodyMass(character,65.0f);

EntityType(character,1);

 

EntityParent(camera,character);

PositionEntity(camera,Vec3(0.0f,1.75f,0.0f));

PositionEntity(character,Vec3(5.0f,15.0f,5.0f));

 

int mx=wb2;

int my=hb2;

float move=0;

float strafe=0;

TVec3 camerarot = Vec3(0.0f);

clock_t start = 0;

clock_t delta = 0;

clock_t totalstart = 0;

int frames = 0;

TVec3 sunpos;

 

MoveMouse(wb2,hb2);

 

// Game loop

while( !KeyHit() && !AppTerminate() )

{

if( !AppSuspended() ) // We are not in focus!

{

//=============================================================

// Update Contorls

//=============================================================

 

totalstart = clock();

 

SetWorld(world);

mx = MouseX() - wb2;

my = MouseY() - hb2;

MoveMouse(wb2,hb2);

camerarot.X += my/5.0f;

camerarot.Y -= mx/5.0f;

RotateEntity(camera,Vec3(camerarot.X,0,0));

RotateEntity(backcamera,camerarot);

 

move = float(KeyDown(KEY_W) - KeyDown(KEY_S));

strafe = float(KeyDown(KEY_D) - KeyDown(KEY_A));

move*=4;

strafe*=4;

 

UpdateController(character,camerarot.Y,move,strafe,0,40);

 

//=============================================================

// Update Worlds and cameras

//=============================================================

 

UpdateAppTime();

 

SetWorld(world);

UpdateWorld(AppSpeed());

 

//=============================================================

// Render sky and sun

//=============================================================

 

sunpos = EntityPosition(thesun);

SetShaderVec4(teSkyRender,"skycolor",Vec4(0.2f,0.2f,0.4f,1.0f));

SetShaderVec4(teSkyRender,"skycolorh",Vec4(0.5f,0.3f,0.1f,1.0f));

SetShaderVec3(teSkyRender,"suncolor",Vec3(2.0f,1.2f,0.4f));

SetShaderVec3(teSkyRender,"sundir",Vec3(0.0)-sunpos);

 

SetWorld(backworld);

SetBuffer(gbuffer);

RenderWorld();

 

//=============================================================

// Render main world

//=============================================================

 

SetWorld(world);

SetBuffer(gbuffer);

RenderWorld();

 

//=============================================================

// Render Lights

//=============================================================

 

SetWorld(world);

SetBuffer(tePostbuffer);

RenderLights(gbuffer);

 

//=============================================================

// Render Bloom And Sun Shafts

//=============================================================

 

//=============================================================

// Down Scaling Frame Buffer

//=============================================================

SetBuffer(downcolor2);

SetShader(teDownPass);

DrawImage(GetColorBuffer(tePostbuffer),0,height,width,-height);

 

SetBuffer(downcolor3);

SetShader(teDownPass);

DrawImage(GetColorBuffer(downcolor2),0,height,width,-height);

 

SetBuffer(downcolor4);

SetShader(teDownPass);

DrawImage(GetColorBuffer(downcolor3),0,height,width,-height);

 

//=============================================================

// Blooming

//=============================================================

SetBuffer(bluredbuffer4);

SetShader(teBloomPass);

SetShaderFloat(teBloomPass,"direction",1.0f);

SetShaderFloat(teBloomPass,"darkness",0.0f);

SetShaderFloat(teBloomPass,"numsteps",16.0f);

DrawImage(GetColorBuffer(downcolor4),0,height,width,-height);

 

SetBuffer(tempbuffer4);

SetShader(teBloomPass);

SetShaderFloat(teBloomPass,"direction",0.0);

SetShaderFloat(teBloomPass,"darkness",5.0f);

SetShaderFloat(teBloomPass,"numsteps",16.0f);

DrawImage(GetColorBuffer(bluredbuffer4),0,height,width,-height);

 

SetBuffer(bluredbuffer3);

SetShader(teBloomPass);

SetShaderFloat(teBloomPass,"direction",1.0f);

SetShaderFloat(teBloomPass,"darkness",0.0f);

SetShaderFloat(teBloomPass,"numsteps",16.0f);

DrawImage(GetColorBuffer(downcolor3),0,height,width,-height);

 

SetBuffer(tempbuffer3);

SetShader(teBloomPass);

SetShaderFloat(teBloomPass,"direction",0.0);

SetShaderFloat(teBloomPass,"darkness",5.0f);

SetShaderFloat(teBloomPass,"numsteps",16.0f);

DrawImage(GetColorBuffer(bluredbuffer3),0,height,width,-height);

 

SetBuffer(bluredbuffer2);

SetShader(teBloomPass);

SetShaderFloat(teBloomPass,"direction",1.0f);

SetShaderFloat(teBloomPass,"darkness",0.0f);

SetShaderFloat(teBloomPass,"numsteps",16.0f);

DrawImage(GetColorBuffer(downcolor2),0,height,width,-height);

 

SetBuffer(tempbuffer2);

SetShader(teBloomPass);

SetShaderFloat(teBloomPass,"direction",0.0);

SetShaderFloat(teBloomPass,"darkness",5.0f);

SetShaderFloat(teBloomPass,"numsteps",16.0f);

DrawImage(GetColorBuffer(bluredbuffer2),0,height,width,-height);

 

//=============================================================

// Up Scaling Bloom Buffer

//=============================================================

SetBuffer(tempbuffer3);

SetShader(teUpPass);

SetShaderFloat(teUpPass,"blend",1.0f/2.0f);

BindTexture(GetColorBuffer(tempbuffer4),4);

DrawImage(GetColorBuffer(tempbuffer3),0,height,width,-height);

 

SetBuffer(tempbuffer2);

SetShader(teUpPass);

SetShaderFloat(teUpPass,"blend",1.0f/3.0f);

BindTexture(GetColorBuffer(tempbuffer3),4);

DrawImage(GetColorBuffer(tempbuffer2),0,height,width,-height);

 

//=============================================================

// Render Bloom to Screen

//=============================================================

SetBuffer(tePostbuffer);

SetShader(teBloomRender);

SetShaderFloat(teBloomRender,"bloomstr",0.5f);

SetShaderVec4(teBloomRender,"bloomcolor",Vec4(1.0,1.0,1.0,1.0));

BindTexture(GetColorBuffer(tempbuffer2),4);

DrawImage(GetColorBuffer(tePostbuffer),0,height,width,-height);

 

//=============================================================

// Sun Shafts

//=============================================================

 

SetBuffer(tempbuffer2);

SetShader(teSunShaftsPass);

 

sunpos = CameraUnproject(camera,Vec3(0.0)-EntityPosition(thesun));

 

SetShaderFloat(teSunShaftsPass,"skyonly",1.0f);

SetShaderFloat(teSunShaftsPass,"darkness",2.0f);

SetShaderFloat(teSunShaftsPass,"numsteps",16.0f);

SetShaderFloat(teSunShaftsPass,"step",0.75f);

SetShaderVec3(teSunShaftsPass,"sundir",sunpos);

DrawImage(GetColorBuffer(downcolor2),0,height,width,-height);

 

SetBuffer(bluredbuffer3);

SetShader(teUpPass);

SetShaderFloat(teUpPass,"blend",0.0f);

BindTexture(GetColorBuffer(tempbuffer2),4);

DrawImage(GetColorBuffer(tempbuffer3),0,height,width,-height);

 

SetBuffer(tempbuffer3);

SetShader(teSunShaftsPass);

 

sunpos = CameraUnproject(camera,Vec3(0.0)-EntityPosition(thesun));

 

SetShaderFloat(teSunShaftsPass,"skyonly",0.0f);

SetShaderFloat(teSunShaftsPass,"darkness",0.0f);

SetShaderFloat(teSunShaftsPass,"numsteps",16.0f);

SetShaderFloat(teSunShaftsPass,"step",0.5f);

SetShaderVec3(teSunShaftsPass,"sundir",sunpos);

DrawImage(GetColorBuffer(bluredbuffer3),0,height,width,-height);

 

SetBuffer(bluredbuffer2);

SetShader(teUpPass);

SetShaderFloat(teUpPass,"blend",0.0f);

BindTexture(GetColorBuffer(tempbuffer3),4);

DrawImage(GetColorBuffer(tempbuffer2),0,height,width,-height);

 

SetBuffer(tempbuffer2);

SetShader(teSunShaftsPass);

 

sunpos = CameraUnproject(camera,Vec3(0.0)-EntityPosition(thesun));

 

SetShaderFloat(teSunShaftsPass,"skyonly",0.0f);

SetShaderFloat(teSunShaftsPass,"darkness",0.0f);

SetShaderFloat(teSunShaftsPass,"numsteps",16.0f);

SetShaderFloat(teSunShaftsPass,"step",0.25f);

SetShaderVec3(teSunShaftsPass,"sundir",sunpos);

DrawImage(GetColorBuffer(bluredbuffer2),0,height,width,-height);

 

//=============================================================

// Render Sun Shafts to Screen

//=============================================================

 

SetBuffer(tePostbuffer);

SetShader(teSunShaftsRender);

SetShaderFloat(teSunShaftsRender,"sunshaftsstr",0.5f);

SetShaderVec4(teSunShaftsRender,"sunshaftscolor",Vec4(1.0,1.0,1.0,1.0));

BindTexture(GetColorBuffer(tempbuffer2),4);

DrawImage(GetColorBuffer(tePostbuffer),0,height,width,-height);

 

//=============================================================

// Render Multi-Sample Anti-Aliasing

//=============================================================

 

SetBuffer(tempbuffer1);

SetShader(teEdgeDetection);

BindTexture(GetDepthBuffer(gbuffer),1);

DrawImage(GetColorBuffer(tePostbuffer),0,height,width,-height);

 

SetBuffer(tempbuffer1);

SetShader(teEdgeDeletion);

BindTexture(GetDepthBuffer(gbuffer),1);

DrawImage(GetColorBuffer(tempbuffer1),0,height,width,-height);

 

SetBuffer(BackBuffer());

SetShader(teAA);

BindTexture(GetDepthBuffer(gbuffer),1);

BindTexture(GetColorBuffer(tempbuffer1),4);

DrawImage(GetColorBuffer(tePostbuffer),0,height,width,-height);

 

printf("total: %d\n", clock()-totalstart);

 

//=============================================================

// Send To Screen

//=============================================================

start = clock();

Flip(0) ;

printf("flip: %d\n", clock()-start);

 

}

}

 

// Done

return Terminate() ;

}

 

 

It's long, so under spoiler.

 

Now the rendering time is around 3-4 millisecond, and flip time is around 9-10 milliseconds, a lot higher then the rendering time.

What could be the problem?

 

Screenshot:

flip30ms.jpg

Link to comment
Share on other sites

You have vertical sync forced ON in your nVidia control panel. It should be always set to force OFF, so that applications can run above 60 FPS.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

It looks like you're using Fraps. Try to close Fraps and look if the result changes. Fraps inject the OpenGL dll and uses a custom Flip (glSwapBuffer) which may cause some slowdown.

  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

Ok, I think i have the reason. if you call renderworld the engine sends the opengl commands to the gfx card. This process is async and glSwapBuffers waits till the gfx card processed all commands. So the real rendering time is total + flip time.

  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

Sorry, I have seen some timing methods in C++ that were wildly inaccurate. I don't remember if clock() is one, but I know I saw some that gave results with errors of 100 millisecs or more. In C++, I use timeGetTime() on Windows.

 

Ok, I think i have the reason. if you call renderworld the engine sends the opengl commands to the gfx card. This process is async and glSwapBuffers waits till the gfx card processed all commands. So the real rendering time is total + flip time.

This only occurs if you call Flip(1).

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I wrote this example earlier, which allows to have 1ms accuracy on all platforms (the default C++ clock() has only 15ms accuracy):

http://www.leadwerks.com/werkspace/topic/3006-timegettime-identifier-not-found/page__view__findpost__p__27941

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...