Advertisement
Zgragselus

ImGUI Render

May 30th, 2025
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1.  
  2. /// <summary>
  3. /// Submit user interface buffers for rendering
  4. /// </summary>
  5. /// <param name="draw_data">ImGui render data to submit</param>
  6. void RenderPassImgui::RenderGui(ImDrawData* draw_data)
  7. {
  8.     ImGuiIO& io = ImGui::GetIO();
  9.  
  10.     mContext->SetPipelineState(mGuiPS);
  11.     mContext->SetRootSignature(mGuiRS);
  12.     mContext->SetPrimitiveTopology(Graphics::TRIANGLELIST);
  13.     mContext->SetConstants(0, DWParam(io.DisplaySize.x), DWParam(io.DisplaySize.y));
  14.     mContext->GetCommandList()->Get()->SetGraphicsRootDescriptorTable(1, mGuiFont->GetSRV().mGpuHandle);
  15.  
  16.     // TODO: Grow index and vertex buffer if and when necessary
  17.  
  18.     for (int n = 0; n < draw_data->CmdListsCount; n++)
  19.     {
  20.         int idx_offset = 0;
  21.         int vtx_offset = 0;
  22.  
  23.         const ImDrawList* cmd_list = draw_data->CmdLists[n];
  24.         size_t verticesCount = cmd_list->VtxBuffer.size();
  25.         size_t indicesCount = cmd_list->IdxBuffer.size();
  26.         size_t verticesSize = verticesCount * sizeof(ImDrawVert);
  27.         size_t indicesSize = indicesCount * sizeof(ImDrawIdx);
  28.  
  29.         mContext->TransitionResource(mGuiVBO, D3D12_RESOURCE_STATE_COPY_DEST, false);
  30.         mContext->TransitionResource(mGuiIBO, D3D12_RESOURCE_STATE_COPY_DEST, true);
  31.         mContext->WriteBuffer(mGuiVBO, 0, &cmd_list->VtxBuffer[0], verticesSize);
  32.         mContext->WriteBuffer(mGuiIBO, 0, &cmd_list->IdxBuffer[0], indicesSize);
  33.         mContext->TransitionResource(mGuiVBO, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, false);
  34.         mContext->TransitionResource(mGuiIBO, D3D12_RESOURCE_STATE_INDEX_BUFFER, true);
  35.  
  36.         mContext->SetIndexBuffer(mGuiIBO->IndexBufferView(0, (unsigned int)indicesSize));
  37.         mContext->SetVertexBuffer(0, mGuiVBO->VertexBufferView(0, (unsigned int)verticesSize, sizeof(ImDrawVert)));
  38.  
  39.         for (int cmd_i = 0; cmd_i < draw_data->CmdLists[n]->CmdBuffer.Size; cmd_i++)
  40.         {
  41.             const ImDrawCmd* pcmd = &draw_data->CmdLists[n]->CmdBuffer[cmd_i];
  42.             if (pcmd->TextureId != io.Fonts->TexID)
  43.             {
  44.                 D3D12_GPU_DESCRIPTOR_HANDLE handle;
  45.                 handle.ptr = (uint64_t)pcmd->TextureId;
  46.                 if (handle.ptr != 0)
  47.                 {
  48.                     mContext->GetCommandList()->Get()->SetGraphicsRootDescriptorTable(1, handle);
  49.                     if (pcmd->UserCallback)
  50.                     {
  51.                         pcmd->UserCallback(draw_data->CmdLists[n], pcmd);
  52.                     }
  53.                     else
  54.                     {
  55.                         mContext->SetScissorRect(pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w);
  56.                         mContext->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
  57.                     }
  58.                     mContext->GetCommandList()->Get()->SetGraphicsRootDescriptorTable(1, mGuiFont->GetSRV().mGpuHandle);
  59.                 }
  60.             }
  61.             else
  62.             {
  63.                 if (pcmd->UserCallback)
  64.                 {
  65.                     pcmd->UserCallback(draw_data->CmdLists[n], pcmd);
  66.                 }
  67.                 else
  68.                 {
  69.                     mContext->SetScissorRect(pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w);
  70.                     mContext->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
  71.                 }
  72.             }
  73.             idx_offset += pcmd->ElemCount;
  74.         }
  75.         vtx_offset += draw_data->CmdLists[n]->VtxBuffer.size();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement