5

I am implementing some functionality which requires me to implement an API to wait for d3d10 to finish rendering.Basically i am trying to implement synchronize access to a shared texture such that i can update a texture after d3d10 is successfully able to present to backbuffer. By calling this black box api i think this can be achieved and i think it will be something similar like glfinish().I have read we can use ID3D10Query queries for implementing synchronize access.

  D3D10_QUERY_DESC queryDesc;

  ... // Fill out queryDesc structure

  ID3D10Query * pQuery;
  pDevice->CreateQuery( &queryDesc, &pQuery );

  pQuery->Begin();

  ... // Issue graphis commands, do whatever

  pQuery->End();

  UINT64 queryData; // This data type is different depending on the query type

  while( S_OK != pQuery->GetData( &queryData, sizeof( UINT64 ), 0 ) )
  {
  }

should i put some dummy command between begin and end this? since i want to expose this functionality as public API something named as waitforgraphiscompletion

what should be a dummy command here?

Bharat Ahuja
  • 394
  • 2
  • 15

1 Answers1

2

If you are trying to synchronize execution CPU and GPU in OpenGL, you would use glFenceSync to followed by a glClientWaitSync. The equivalents in Direct 10 are ID3D10Asynchronous::End, and ID3D10Asynchronous::GetData (note, in DX11, the interfaces are slightly different). These let you know when the GPU has finished processing the command buffer to a particular point. This allows you to know when previous read/write operations on a resource have completed, and the CPU can safely access the resource without additional synchronization.

You are not required to put any commands in the while loop. The command buffer will eventually process your query, and will return S_OK (or an error message, which you might want to handle). However, this is somewhat wasteful, as the CPU will just spin waiting for the GPU, so if possible, you should do some extra useful work within the loop.

Note, if you used D3D10_ASYNC_GETDATA_DONOTFLUSH as the final parameter to GetData (instead of '0'), the above would not be the case - there is no guarantee that the command buffer would 'automatically' kick off, and you could end up in an infinite loop (and, as such is not the recommended usage).

MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
  • thanks this make perfect sense to me. I have one or two questions about it. How is glfinish different than glFenceSync followed by a glClientWaitSync? Regarding D3D10 that just means i just need to call end on ID3D10Query( that means no begin ) and it will wait for all the command to execute on device from which ID3D10Query has been created? – Bharat Ahuja Aug 02 '16 at 18:42
  • glFinish will wait for all OpenGL operations to complete, including writing to the framebuffer. glClientWaitSync does not guarantee that, it only guarantees that any GL commands issued before the glFenceSync will have completed. This is generally not necessary if you're looking to synchronize access to a particular texture, as you should know when it is read/written from your own rendering code. – MuertoExcobito Aug 02 '16 at 19:28