glReadPixels() burns up all CPU cycles of a single core










1















I have an SDL2 app with an OpenGL window, and it is well behaved: When it runs, the app gets synchronized with my 60Hz display, and I see 12% CPU Usage for the app.



So far so good.
But when I add 3D picking by reading a single (!) depth value from the depth buffer (after drawing), the following happens:



  • FPS still at 60

  • CPU usage for the main thread goes to 100%

If I don't do the glReadPixels, the CPU use drops back to 12% again. Why does reading a single value from the depth buffer cause the CPU to burn all cycles?



My window is created with:



SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, use_aa ? 1 : 0 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, use_aa ? 4 : 0 );
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

window = SDL_CreateWindow
(
"Fragger",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
fbw, fbh,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
);


My drawing is concluded with:



SDL_GL_SwapWindow( window );


My depth read is performed with:



float depth;
glReadPixels( scrx, scry, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );


My display sync is configured using:



int rv = SDL_GL_SetSwapInterval( -1 );
if ( rv < 0 )

LOGI( "Late swap tearing not available. Using hard v-sync with display." );
rv = SDL_GL_SetSwapInterval( 1 );
if ( rv < 0 ) LOGE( "SDL_GL_SetSwapInterval() failed." );

else

LOGI( "Can use late vsync swap." );



Investigations with 'perf' shows that the cycles are burnt up by nVidia's driver, doing relentless system calls, one of which is sys_clock_gettime() as can be seen below:



flame graph



I've tried some variations by reading GL_BACK or GL_FRONT, with same result.
I also tried reading just before and just after the window swap.
But the CPU usage is always at a 100% level.




  • Platform: Ubuntu 18.04.1


  • SDL: version 2.0.8


  • CPU: Intel Haswell


  • GPU: nVidia GTX750Ti


  • GL_VERSION: 3.2.0 NVIDIA 390.87









share|improve this question
























  • How/when do you read your depth? From which buffer? How no-vsync'd FPS drops when you do that? Looks like you force driver to synchronise since requested depth is not yet rendered. Logically you probably want depth from previous frame, not the one you're currently rendering.

    – keltar
    Nov 15 '18 at 12:06















1















I have an SDL2 app with an OpenGL window, and it is well behaved: When it runs, the app gets synchronized with my 60Hz display, and I see 12% CPU Usage for the app.



So far so good.
But when I add 3D picking by reading a single (!) depth value from the depth buffer (after drawing), the following happens:



  • FPS still at 60

  • CPU usage for the main thread goes to 100%

If I don't do the glReadPixels, the CPU use drops back to 12% again. Why does reading a single value from the depth buffer cause the CPU to burn all cycles?



My window is created with:



SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, use_aa ? 1 : 0 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, use_aa ? 4 : 0 );
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

window = SDL_CreateWindow
(
"Fragger",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
fbw, fbh,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
);


My drawing is concluded with:



SDL_GL_SwapWindow( window );


My depth read is performed with:



float depth;
glReadPixels( scrx, scry, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );


My display sync is configured using:



int rv = SDL_GL_SetSwapInterval( -1 );
if ( rv < 0 )

LOGI( "Late swap tearing not available. Using hard v-sync with display." );
rv = SDL_GL_SetSwapInterval( 1 );
if ( rv < 0 ) LOGE( "SDL_GL_SetSwapInterval() failed." );

else

LOGI( "Can use late vsync swap." );



Investigations with 'perf' shows that the cycles are burnt up by nVidia's driver, doing relentless system calls, one of which is sys_clock_gettime() as can be seen below:



flame graph



I've tried some variations by reading GL_BACK or GL_FRONT, with same result.
I also tried reading just before and just after the window swap.
But the CPU usage is always at a 100% level.




  • Platform: Ubuntu 18.04.1


  • SDL: version 2.0.8


  • CPU: Intel Haswell


  • GPU: nVidia GTX750Ti


  • GL_VERSION: 3.2.0 NVIDIA 390.87









share|improve this question
























  • How/when do you read your depth? From which buffer? How no-vsync'd FPS drops when you do that? Looks like you force driver to synchronise since requested depth is not yet rendered. Logically you probably want depth from previous frame, not the one you're currently rendering.

    – keltar
    Nov 15 '18 at 12:06













1












1








1








I have an SDL2 app with an OpenGL window, and it is well behaved: When it runs, the app gets synchronized with my 60Hz display, and I see 12% CPU Usage for the app.



So far so good.
But when I add 3D picking by reading a single (!) depth value from the depth buffer (after drawing), the following happens:



  • FPS still at 60

  • CPU usage for the main thread goes to 100%

If I don't do the glReadPixels, the CPU use drops back to 12% again. Why does reading a single value from the depth buffer cause the CPU to burn all cycles?



My window is created with:



SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, use_aa ? 1 : 0 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, use_aa ? 4 : 0 );
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

window = SDL_CreateWindow
(
"Fragger",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
fbw, fbh,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
);


My drawing is concluded with:



SDL_GL_SwapWindow( window );


My depth read is performed with:



float depth;
glReadPixels( scrx, scry, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );


My display sync is configured using:



int rv = SDL_GL_SetSwapInterval( -1 );
if ( rv < 0 )

LOGI( "Late swap tearing not available. Using hard v-sync with display." );
rv = SDL_GL_SetSwapInterval( 1 );
if ( rv < 0 ) LOGE( "SDL_GL_SetSwapInterval() failed." );

else

LOGI( "Can use late vsync swap." );



Investigations with 'perf' shows that the cycles are burnt up by nVidia's driver, doing relentless system calls, one of which is sys_clock_gettime() as can be seen below:



flame graph



I've tried some variations by reading GL_BACK or GL_FRONT, with same result.
I also tried reading just before and just after the window swap.
But the CPU usage is always at a 100% level.




  • Platform: Ubuntu 18.04.1


  • SDL: version 2.0.8


  • CPU: Intel Haswell


  • GPU: nVidia GTX750Ti


  • GL_VERSION: 3.2.0 NVIDIA 390.87









share|improve this question
















I have an SDL2 app with an OpenGL window, and it is well behaved: When it runs, the app gets synchronized with my 60Hz display, and I see 12% CPU Usage for the app.



So far so good.
But when I add 3D picking by reading a single (!) depth value from the depth buffer (after drawing), the following happens:



  • FPS still at 60

  • CPU usage for the main thread goes to 100%

If I don't do the glReadPixels, the CPU use drops back to 12% again. Why does reading a single value from the depth buffer cause the CPU to burn all cycles?



My window is created with:



SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, use_aa ? 1 : 0 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, use_aa ? 4 : 0 );
SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

window = SDL_CreateWindow
(
"Fragger",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
fbw, fbh,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
);


My drawing is concluded with:



SDL_GL_SwapWindow( window );


My depth read is performed with:



float depth;
glReadPixels( scrx, scry, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );


My display sync is configured using:



int rv = SDL_GL_SetSwapInterval( -1 );
if ( rv < 0 )

LOGI( "Late swap tearing not available. Using hard v-sync with display." );
rv = SDL_GL_SetSwapInterval( 1 );
if ( rv < 0 ) LOGE( "SDL_GL_SetSwapInterval() failed." );

else

LOGI( "Can use late vsync swap." );



Investigations with 'perf' shows that the cycles are burnt up by nVidia's driver, doing relentless system calls, one of which is sys_clock_gettime() as can be seen below:



flame graph



I've tried some variations by reading GL_BACK or GL_FRONT, with same result.
I also tried reading just before and just after the window swap.
But the CPU usage is always at a 100% level.




  • Platform: Ubuntu 18.04.1


  • SDL: version 2.0.8


  • CPU: Intel Haswell


  • GPU: nVidia GTX750Ti


  • GL_VERSION: 3.2.0 NVIDIA 390.87






nvidia cpu-usage sdl-2 glreadpixels






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:37







Bram

















asked Nov 15 '18 at 2:43









BramBram

2,8502452




2,8502452












  • How/when do you read your depth? From which buffer? How no-vsync'd FPS drops when you do that? Looks like you force driver to synchronise since requested depth is not yet rendered. Logically you probably want depth from previous frame, not the one you're currently rendering.

    – keltar
    Nov 15 '18 at 12:06

















  • How/when do you read your depth? From which buffer? How no-vsync'd FPS drops when you do that? Looks like you force driver to synchronise since requested depth is not yet rendered. Logically you probably want depth from previous frame, not the one you're currently rendering.

    – keltar
    Nov 15 '18 at 12:06
















How/when do you read your depth? From which buffer? How no-vsync'd FPS drops when you do that? Looks like you force driver to synchronise since requested depth is not yet rendered. Logically you probably want depth from previous frame, not the one you're currently rendering.

– keltar
Nov 15 '18 at 12:06





How/when do you read your depth? From which buffer? How no-vsync'd FPS drops when you do that? Looks like you force driver to synchronise since requested depth is not yet rendered. Logically you probably want depth from previous frame, not the one you're currently rendering.

– keltar
Nov 15 '18 at 12:06












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311673%2fglreadpixels-burns-up-all-cpu-cycles-of-a-single-core%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311673%2fglreadpixels-burns-up-all-cpu-cycles-of-a-single-core%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo