Advertisement
Guest User

multi-core submit

a guest
Feb 2nd, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1.  
  2.         static bool multicoreSubmit = false;
  3.         static uint batchSize = 500;
  4.         if( multicoreSubmit && opaqueCount > batchSize )
  5.         {
  6.             batchSize = min(batchSize, opaqueCount / 2);//todo - some formula that tries to balance the size, instead of letting the last job be smaller
  7.  
  8.             const DrawItemKey* draw = opaque;
  9.             uint numGroups = (opaqueCount+batchSize-1)/batchSize;
  10.             struct Arg { Atomic* done; const DrawItemKey* begin, *end; GpuContext* ctx; const RenderPass* pass; ClearCommand* clear; };
  11.             GpuContext** contexts = eiAllocArray(a, GpuContext*, numGroups);
  12.             Arg* args = eiAllocArray(a, Arg, numGroups);
  13.             Atomic done;
  14.             {
  15.                 eiProfile("AcquireDeferredContexts");//todo - turns out that preparing a deferred context is expensive - worthwhile adding this to the job chain instead of having the main thread do it here
  16.                 for( uint group=0; group!=numGroups; ++group )
  17.                 {
  18.                     contexts[group] = &m_gpuDevice->AcquireDeferredContext();
  19.                 }
  20.             }
  21.             for( uint group=0; group!=numGroups; ++group )
  22.             {
  23.                 Arg arg = { &done, draw, draw+batchSize, contexts[group], renderPass, group==0?clearOpaque:0 };
  24.                 if( arg.end > opaque + opaqueCount )
  25.                 {
  26.                     eiASSERT( group == numGroups-1 );
  27.                     arg.end = opaque + opaqueCount;
  28.                 }
  29.                 draw = arg.end;
  30.                 args[group] = arg;
  31.                 JobPool::Job j = { []( void* data, ThreadId& )
  32.                 {
  33.                     Arg& arg = *(Arg*)data;
  34.                     arg.ctx->TransferThreadOwnership();
  35.                     arg.ctx->Submit( *arg.pass, DrawList(arg.begin, arg.end), arg.clear );
  36.                     arg.ctx->Finish();
  37.                     ++(*arg.done);
  38.                 }, &args[group] };
  39.                 GetThreadId()->Jobs().PushJob(j);//todo - push all jobs at once instead of one at a time
  40.             }
  41.             YieldThreadUntil(WaitForValue(done, numGroups));
  42.             m_gpuDevice->Submit(numGroups, contexts); // calls ExecuteCommandLists once with all the results
  43.         }
  44.         else
  45.         {
  46.             ctx.Submit( *renderPass, DrawList(opaqueCount, opaque), clearOpaque );
  47.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement