CommandBufferExecutor.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "..\..\..\common_h\render.h"
  2. #ifndef COMPILE_PROPS_INCLUDED
  3. #error CompileProps.h must be included
  4. #endif
  5. #if defined (_XBOX) && defined(_PRECOMPILED_COMMAND_BUFFER_BAKER)
  6. #include "CommandBufferExecutor.h"
  7. #include "PrecompiledBatch.h"
  8. #include "..\Render.h"
  9. batchExecutor::batchExecutor()
  10. {
  11. dwCurrentBufferIndex = 0;
  12. maxSizedBatch = NULL;
  13. dwMaxSize = 0;
  14. srcBuffer = NULL;
  15. }
  16. batchExecutor::~batchExecutor()
  17. {
  18. for (DWORD i = 0; i < MAX_CB_BUFFERS_POOL; i++)
  19. {
  20. cbExecutor & cb = cbBuffers[i];
  21. if (cb.pHeaderBytes)
  22. {
  23. free(cb.pHeaderBytes);
  24. cb.pHeaderBytes = NULL;
  25. }
  26. if (cb.pInitializationBytes)
  27. {
  28. free(cb.pInitializationBytes);
  29. cb.pInitializationBytes = NULL;
  30. }
  31. if (cb.pPhysicalBytes)
  32. {
  33. XPhysicalFree(cb.pPhysicalBytes);
  34. cb.pPhysicalBytes = NULL;
  35. }
  36. }
  37. }
  38. void batchExecutor::calculateMaxSize (precompiledBatch * batch)
  39. {
  40. DWORD dwBatchMaxSize = batch->getPhysicalSize();
  41. if (dwBatchMaxSize > dwMaxSize)
  42. {
  43. dwMaxSize = dwBatchMaxSize;
  44. maxSizedBatch = batch;
  45. if (srcBuffer)
  46. {
  47. srcBuffer->Release();
  48. srcBuffer = NULL;
  49. }
  50. srcBuffer = maxSizedBatch->getSourceCommandBuffer();
  51. srcBuffer->AddRef();
  52. }
  53. }
  54. void batchExecutor::create()
  55. {
  56. //Создаем командных буферов для исполнения...
  57. for (DWORD i = 0; i < MAX_CB_BUFFERS_POOL; i++)
  58. {
  59. createCommandBuffer(cbBuffers[i]);
  60. }
  61. //Отпускаем матер буфер он под 3 мега...
  62. srcBuffer->Release();
  63. srcBuffer = NULL;
  64. }
  65. void batchExecutor::createCommandBuffer (cbExecutor & cbExec)
  66. {
  67. Assert(srcBuffer);
  68. cbExec.dwHeaderSize = 0;
  69. cbExec.dwPhysicalSize = 0;
  70. cbExec.dwInitializationSize = 0;
  71. srcBuffer->Deconstruct( 0, NULL, &cbExec.dwHeaderSize, NULL, &cbExec.dwPhysicalSize, NULL, &cbExec.dwInitializationSize );
  72. cbExec.pHeaderBytes = ( BYTE* )malloc( cbExec.dwHeaderSize );
  73. cbExec.pPhysicalBytes = ( BYTE* )XPhysicalAlloc( cbExec.dwPhysicalSize, MAXULONG_PTR, 4096, PAGE_READWRITE | PAGE_WRITECOMBINE);
  74. cbExec.pInitializationBytes = ( BYTE* )malloc( cbExec.dwInitializationSize );
  75. Assert( cbExec.pHeaderBytes != NULL && cbExec.pPhysicalBytes != NULL && cbExec.pInitializationBytes != NULL );
  76. Assert(!((DWORD)cbExec.pPhysicalBytes & 3) && ((DWORD)cbExec.pPhysicalBytes >= GPU_CPU_VIRTUAL_ADDRESS_64KB_START));
  77. srcBuffer->Deconstruct( 0, ( D3DCommandBuffer* )cbExec.pHeaderBytes, &cbExec.dwHeaderSize, cbExec.pPhysicalBytes , &cbExec.dwPhysicalSize, cbExec.pInitializationBytes, &cbExec.dwInitializationSize);
  78. cbExec.cbToExecute = (D3DCommandBuffer*)cbExec.pHeaderBytes;
  79. cbExec.cbToExecute->BeginReconstruction( 0, cbExec.pPhysicalBytes, cbExec.pInitializationBytes);
  80. cbExec.cbToExecute->EndReconstruction();
  81. }
  82. /*
  83. void fastMemoryCopyDword(DWORD * volatile dest, DWORD * volatile src)
  84. {
  85. *dest = *src;
  86. }
  87. */
  88. void batchExecutor::Run (precompiledBatch * batch, IDirect3DDevice9* device, bool bDepthDisabled, bool bHyperZDisabled)
  89. {
  90. batch->fixupHyperZState(bHyperZDisabled);
  91. batch->fixupDepthState(bDepthDisabled);
  92. cbExecutor & cbDesc = cbBuffers[dwCurrentBufferIndex % MAX_CB_BUFFERS_POOL];
  93. dwCurrentBufferIndex++;
  94. D3DCommandBuffer * cb = cbDesc.cbToExecute;
  95. BYTE * srcCommandBufferData = batch->getPhysicalBytes();
  96. DWORD dwSizeInDwords = batch->getSizeInDwords();
  97. DWORD dwOffsetToEndToken = dwSizeInDwords * sizeof(DWORD);
  98. DWORD dwSizeInBytes = dwOffsetToEndToken + (sizeof(DWORD) * 2);
  99. D3DCommandBuffer * cmdBuffer = batch->getCommandBuffer();
  100. cb->m_Inherited = cmdBuffer->m_Inherited;
  101. cb->m_Persisted = cmdBuffer->m_Persisted;
  102. cb->BeginDynamicFixups();
  103. //Правим новый размер
  104. GPUSetCommandBufferSizeInDwords(cb, dwSizeInDwords);
  105. NGRender::RenderStat & stats = NGRender::pRS->renderStats();
  106. stats.dwWCBytesTraffic += dwOffsetToEndToken;
  107. //Копируем содержимое (не надо endToken мы его сгенерируем и так...)
  108. XMemCpyStreaming_WriteCombined(cbDesc.pPhysicalBytes, srcCommandBufferData, dwOffsetToEndToken);
  109. batch->restoreDepthState();
  110. batch->restoreHyperZState();
  111. /*
  112. DWORD * __restrict dest = (DWORD *)cbDesc.pPhysicalBytes;
  113. DWORD * __restrict src = (DWORD *)srcCommandBufferData;
  114. for (DWORD s = 0; s < dwSizeInDwords; s++)
  115. {
  116. fastMemoryCopyDword(dest, src);
  117. src++;
  118. dest++;
  119. }
  120. */
  121. //Маркируем окончание command buffer
  122. GPUCOMMAND_BUFFER_END * cmdEnd = (GPUCOMMAND_BUFFER_END*)(cbDesc.pPhysicalBytes + dwOffsetToEndToken);
  123. cmdEnd->Place();
  124. cb->EndDynamicFixups();
  125. /*
  126. //------------------------------------------------------------------------------------------
  127. BYTE * memory = GPUGetCommandBufferMemoryAddr(cb);
  128. batch->debugOutCommandBuffer(memory, true);
  129. GPUCOMMAND_BUFFER_END * cmdEndStatic = (GPUCOMMAND_BUFFER_END*)(srcCommandBufferData + dwOffsetToEndToken);
  130. cmdEndStatic->Place();
  131. //batch->tempFixupVertexShaderAndPixelShader();
  132. batch->debugOutCommandBuffer(srcCommandBufferData, true);
  133. int a = 0;
  134. */
  135. device->RunCommandBuffer(cb, 0);
  136. /*
  137. if (batch->isNeedFlushHiZ())
  138. {
  139. device->FlushHiZStencil(D3DFHZS_ASYNCHRONOUS);
  140. }
  141. */
  142. }
  143. #endif