Render function
| 117 | |
| 118 | // Render function |
| 119 | void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data) |
| 120 | { |
| 121 | // Avoid rendering when minimized |
| 122 | if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| 123 | return; |
| 124 | |
| 125 | ImGui_ImplDX11_Data* bd = ImGui_ImplDX11_GetBackendData(); |
| 126 | ID3D11DeviceContext* ctx = bd->pd3dDeviceContext; |
| 127 | |
| 128 | // Create and grow vertex/index buffers if needed |
| 129 | if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount) |
| 130 | { |
| 131 | if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; } |
| 132 | bd->VertexBufferSize = draw_data->TotalVtxCount + 5000; |
| 133 | D3D11_BUFFER_DESC desc; |
| 134 | memset(&desc, 0, sizeof(D3D11_BUFFER_DESC)); |
| 135 | desc.Usage = D3D11_USAGE_DYNAMIC; |
| 136 | desc.ByteWidth = bd->VertexBufferSize * sizeof(ImDrawVert); |
| 137 | desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| 138 | desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 139 | desc.MiscFlags = 0; |
| 140 | if (bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pVB) < 0) |
| 141 | return; |
| 142 | } |
| 143 | if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount) |
| 144 | { |
| 145 | if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; } |
| 146 | bd->IndexBufferSize = draw_data->TotalIdxCount + 10000; |
| 147 | D3D11_BUFFER_DESC desc; |
| 148 | memset(&desc, 0, sizeof(D3D11_BUFFER_DESC)); |
| 149 | desc.Usage = D3D11_USAGE_DYNAMIC; |
| 150 | desc.ByteWidth = bd->IndexBufferSize * sizeof(ImDrawIdx); |
| 151 | desc.BindFlags = D3D11_BIND_INDEX_BUFFER; |
| 152 | desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 153 | if (bd->pd3dDevice->CreateBuffer(&desc, nullptr, &bd->pIB) < 0) |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | // Upload vertex/index data into a single contiguous GPU buffer |
| 158 | D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource; |
| 159 | if (ctx->Map(bd->pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK) |
| 160 | return; |
| 161 | if (ctx->Map(bd->pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK) |
| 162 | return; |
| 163 | ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData; |
| 164 | ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData; |
| 165 | for (int n = 0; n < draw_data->CmdListsCount; n++) |
| 166 | { |
| 167 | const ImDrawList* cmd_list = draw_data->CmdLists[n]; |
| 168 | memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert)); |
| 169 | memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| 170 | vtx_dst += cmd_list->VtxBuffer.Size; |
| 171 | idx_dst += cmd_list->IdxBuffer.Size; |
| 172 | } |
| 173 | ctx->Unmap(bd->pVB, 0); |
| 174 | ctx->Unmap(bd->pIB, 0); |
| 175 | |
| 176 | // Setup orthographic projection matrix into our constant buffer |
no test coverage detected