Advertisement
Zgragselus

Generate mipmaps for HiZ

May 25th, 2024 (edited)
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. compute->SetPipelineState(mMipmapPS);
  2. compute->SetRootSignature(mMipmapRS);
  3. compute->SetDescriptorHeap(Engine::DescriptorHeap::CBV_SRV_UAV, heap);
  4.  
  5. compute->TransitionResource(mColorBuffer, RESOURCE_STATE_UNORDERED_ACCESS, true);
  6.  
  7. int todo = mNumMipmaps - 1;
  8. int base = 0;
  9. int dimension[2] = { (int)mWidth, (int)mHeight };
  10.  
  11. while (todo != 0)
  12. {
  13.     int mipLevels = 3;
  14.  
  15.     int npotflag = 0;
  16.     if (((dimension[0] % 2) == 1) && ((dimension[1] % 2) == 1))
  17.     {
  18.         npotflag = 3;
  19.     }
  20.     else if ((dimension[1] % 2) == 1)
  21.     {
  22.         npotflag = 2;
  23.     }
  24.     else if ((dimension[0] % 2) == 1)
  25.     {
  26.         npotflag = 1;
  27.     }
  28.  
  29.     if (todo == 1)
  30.     {
  31.         mipLevels = 1;
  32.     }
  33.     else if (todo == 2)
  34.     {
  35.         mipLevels = 2;
  36.     }
  37.  
  38.     int dispatch[2] = { 0, 0 };
  39.     dispatch[0] = dimension[0] / 8;
  40.     if ((dimension[0] % 8) > 0)
  41.     {
  42.         dispatch[0]++;
  43.     }
  44.  
  45.     dispatch[1] = dimension[1] / 8;
  46.     if ((dimension[1] % 8) > 0)
  47.     {
  48.         dispatch[1]++;
  49.     }
  50.  
  51.     if (dispatch[0] == 0 && dispatch[1] > 0)
  52.     {
  53.         dispatch[0] = 1;
  54.     }
  55.     else if (dispatch[0] > 0 && dispatch[1] == 0)
  56.     {
  57.         dispatch[1] = 1;
  58.     }
  59.     else if (dispatch[0] == 0 && dispatch[1] == 0)
  60.     {
  61.         break;
  62.     }
  63.  
  64.     compute->SetConstants(0, Engine::DWParam(base), Engine::DWParam(npotflag), Engine::DWParam(1.0f / (float)dimension[0]), Engine::DWParam(1.0f / (float)dimension[1]));
  65.     compute->SetConstants(1, Engine::DWParam(mipLevels), Engine::DWParam(mipLevels), Engine::DWParam(mipLevels), Engine::DWParam(mipLevels));
  66.     compute->SetDescriptorTable(2, mColorBuffer->GetSRV());
  67.     compute->SetDescriptorTable(3, mColorBuffer->GetUAV(base + 1));
  68.     if (mipLevels >= 2) { compute->SetDescriptorTable(4, mColorBuffer->GetUAV(base + 2)); }
  69.     if (mipLevels >= 3) { compute->SetDescriptorTable(5, mColorBuffer->GetUAV(base + 3)); }
  70.     compute->Dispatch(dispatch[0], dispatch[1], 1);
  71.     compute->TransitionResource(mColorBuffer, RESOURCE_STATE_UNORDERED_ACCESS, true);
  72.  
  73.     todo -= mipLevels;
  74.     base += mipLevels;
  75.     dimension[0] /= 2 << (mipLevels - 1);
  76.     dimension[1] /= 2 << (mipLevels - 1);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement