| 19 | } |
| 20 | |
| 21 | void ImguiRenderer::Render(ref_ptr<dp::GraphicsContext> context, ref_ptr<dp::TextureManager> textureManager, |
| 22 | ref_ptr<gpu::ProgramManager> programManager) |
| 23 | { |
| 24 | std::lock_guard<std::mutex> lock(m_bufferMutex); |
| 25 | size_t renderDataIndex = (m_updateIndex + 1) % m_uiDataBuffer.size(); |
| 26 | UiDataBuffer & dataBuffer = m_uiDataBuffer[renderDataIndex]; |
| 27 | |
| 28 | auto gpuProgram = programManager->GetProgram(m_state.GetProgram<gpu::Program>()); |
| 29 | |
| 30 | bool needUpdate = true; |
| 31 | if (!m_mesh || dataBuffer.m_vertices.size() > m_vertexCount || dataBuffer.m_indices.size() > m_indexCount) |
| 32 | { |
| 33 | while (dataBuffer.m_vertices.size() > m_vertexCount) |
| 34 | m_vertexCount *= 2; |
| 35 | while (dataBuffer.m_indices.size() > m_indexCount) |
| 36 | m_indexCount *= 2; |
| 37 | m_indexCount = std::min(m_indexCount, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max())); |
| 38 | |
| 39 | dataBuffer.m_vertices.resize(m_vertexCount); |
| 40 | dataBuffer.m_indices.resize(m_indexCount); |
| 41 | |
| 42 | m_mesh = make_unique_dp<dp::MeshObject>(context, dp::MeshObject::DrawPrimitive::Triangles, "imGui"); |
| 43 | |
| 44 | m_mesh->SetBuffer(0, std::move(dataBuffer.m_vertices)); |
| 45 | m_mesh->SetAttribute("a_position", 0, 0 /* offset */, 2); |
| 46 | m_mesh->SetAttribute("a_texCoords", 0, 2 * sizeof(float) /* offset */, 2); |
| 47 | m_mesh->SetAttribute("a_color", 0, 4 * sizeof(float) /* offset */, 4); |
| 48 | m_mesh->SetIndexBuffer(std::move(dataBuffer.m_indices)); |
| 49 | m_mesh->Build(context, gpuProgram); |
| 50 | |
| 51 | dataBuffer.m_vertices.clear(); |
| 52 | dataBuffer.m_indices.clear(); |
| 53 | needUpdate = false; |
| 54 | } |
| 55 | |
| 56 | if (!m_texture) |
| 57 | { |
| 58 | std::lock_guard<std::mutex> lock(m_textureMutex); |
| 59 | if (!m_textureData.empty()) |
| 60 | { |
| 61 | m_texture = make_unique_dp<dp::StaticTexture>(); |
| 62 | m_texture->Create(context, |
| 63 | dp::Texture::Params{ |
| 64 | .m_width = m_textureWidth, |
| 65 | .m_height = m_textureHeight, |
| 66 | .m_format = dp::TextureFormat::RGBA8, |
| 67 | .m_allocator = textureManager->GetTextureAllocator(), |
| 68 | }, |
| 69 | m_textureData.data()); |
| 70 | m_textureData.clear(); |
| 71 | m_state.SetColorTexture(make_ref(m_texture)); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | // Can't render without texture. |
| 76 | return; |
| 77 | } |
| 78 | } |
no test coverage detected