| Flutter Impeller |

imgui_impl_impeller.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "imgui_impl_impeller.h"
6
7 #include <algorithm>
8 #include <climits>
9 #include <memory>
10 #include <vector>
11
12 #include "impeller/geometry/scalar.h"
13 #include "impeller/geometry/vector.h"
14 #include "impeller/playground/imgui/imgui_raster.frag.h"
15 #include "impeller/playground/imgui/imgui_raster.vert.h"
16 #include "third_party/imgui/imgui.h"
17
18 #include "impeller/core/allocator.h"
19 #include "impeller/core/formats.h"
20 #include "impeller/core/range.h"
21 #include "impeller/core/sampler.h"
22 #include "impeller/core/texture.h"
23 #include "impeller/core/texture_descriptor.h"
24 #include "impeller/core/vertex_buffer.h"
25 #include "impeller/geometry/matrix.h"
26 #include "impeller/geometry/point.h"
27 #include "impeller/geometry/rect.h"
28 #include "impeller/geometry/size.h"
29 #include "impeller/renderer/command.h"
30 #include "impeller/renderer/context.h"
31 #include "impeller/renderer/pipeline_builder.h"
32 #include "impeller/renderer/pipeline_descriptor.h"
33 #include "impeller/renderer/pipeline_library.h"
34 #include "impeller/renderer/render_pass.h"
35
36 struct ImGui_ImplImpeller_Data {
37 std::shared_ptr<impeller::Context> context;
38 std::shared_ptr<impeller::Texture> font_texture;
39 std::shared_ptr<impeller::Pipeline<impeller::PipelineDescriptor>> pipeline;
40 std::shared_ptr<const impeller::Sampler> sampler;
41 };
42
43 static ImGui_ImplImpeller_Data* ImGui_ImplImpeller_GetBackendData() {
44 return ImGui::GetCurrentContext()
45 ? static_cast<ImGui_ImplImpeller_Data*>(
46 ImGui::GetIO().BackendRendererUserData)
47 : nullptr;
48 }
49
50 bool ImGui_ImplImpeller_Init(
51 const std::shared_ptr<impeller::Context>& context) {
52 ImGuiIO& io = ImGui::GetIO();
53 IM_ASSERT(io.BackendRendererUserData == nullptr &&
54 "Already initialized a renderer backend!");
55
56 // Setup backend capabilities flags
57 auto* bd = new ImGui_ImplImpeller_Data();
58 io.BackendRendererUserData = reinterpret_cast<void*>(bd);
59 io.BackendRendererName = "imgui_impl_impeller";
60 io.BackendFlags |=
61 ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the
62 // ImDrawCmd::VtxOffset field,
63 // allowing for large meshes.
64
65 bd->context = context;
66
67 // Generate/upload the font atlas.
68 {
69 unsigned char* pixels;
70 int width, height;
71 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
72
73 auto texture_descriptor = impeller::TextureDescriptor{};
74 texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
75 texture_descriptor.format = impeller::PixelFormat::kR8G8B8A8UNormInt;
76 texture_descriptor.size = {width, height};
77 texture_descriptor.mip_count = 1u;
78
79 bd->font_texture =
80 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
81 IM_ASSERT(bd->font_texture != nullptr &&
82 "Could not allocate ImGui font texture.");
83 bd->font_texture->SetLabel("ImGui Font Texture");
84
85 [[maybe_unused]] bool uploaded = bd->font_texture->SetContents(
86 pixels, texture_descriptor.GetByteSizeOfBaseMipLevel());
87 IM_ASSERT(uploaded &&
88 "Could not upload ImGui font texture to device memory.");
89 }
90
91 // Build the raster pipeline.
92 {
93 auto desc = impeller::PipelineBuilder<impeller::ImguiRasterVertexShader,
94 impeller::ImguiRasterFragmentShader>::
95 MakeDefaultPipelineDescriptor(*context);
96 IM_ASSERT(desc.has_value() && "Could not create Impeller pipeline");
97 if (desc.has_value()) { // Needed to silence clang-tidy check
98 // bugprone-unchecked-optional-access.
99 desc->ClearStencilAttachments();
100 desc->ClearDepthAttachment();
101 }
102
103 bd->pipeline =
104 context->GetPipelineLibrary()->GetPipeline(std::move(desc)).Get();
105 IM_ASSERT(bd->pipeline != nullptr && "Could not create ImGui pipeline.");
106
107 bd->sampler = context->GetSamplerLibrary()->GetSampler({});
108 IM_ASSERT(bd->pipeline != nullptr && "Could not create ImGui sampler.");
109 }
110
111 return true;
112 }
113
114 void ImGui_ImplImpeller_Shutdown() {
115 auto* bd = ImGui_ImplImpeller_GetBackendData();
116 IM_ASSERT(bd != nullptr &&
117 "No renderer backend to shutdown, or already shutdown?");
118 delete bd;
119 }
120
121 void ImGui_ImplImpeller_RenderDrawData(ImDrawData* draw_data,
122 impeller::RenderPass& render_pass) {
123 if (draw_data->CmdListsCount == 0) {
124 return; // Nothing to render.
125 }
126
127 using VS = impeller::ImguiRasterVertexShader;
128 using FS = impeller::ImguiRasterFragmentShader;
129
130 auto* bd = ImGui_ImplImpeller_GetBackendData();
131 IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplImpeller_Init()?");
132
133 size_t total_vtx_bytes = draw_data->TotalVtxCount * sizeof(VS::PerVertexData);
134 size_t total_idx_bytes = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
135 if (!total_vtx_bytes || !total_idx_bytes) {
136 return; // Nothing to render.
137 }
138
139 // Allocate buffer for vertices + indices.
140 impeller::DeviceBufferDescriptor buffer_desc;
141 buffer_desc.size = total_vtx_bytes + total_idx_bytes;
142 buffer_desc.storage_mode = impeller::StorageMode::kHostVisible;
143
144 auto buffer = bd->context->GetResourceAllocator()->CreateBuffer(buffer_desc);
145 buffer->SetLabel(impeller::SPrintF("ImGui vertex+index buffer"));
146
147 auto display_rect = impeller::Rect::MakeXYWH(
148 draw_data->DisplayPos.x, draw_data->DisplayPos.y,
149 draw_data->DisplaySize.x, draw_data->DisplaySize.y);
150
151 auto viewport = impeller::Viewport{
152 .rect = display_rect.Scale(draw_data->FramebufferScale.x,
153 draw_data->FramebufferScale.y)};
154
155 // Allocate vertex shader uniform buffer.
156 VS::UniformBuffer uniforms;
157 uniforms.mvp = impeller::Matrix::MakeOrthographic(display_rect.GetSize())
158 .Translate(-display_rect.GetOrigin());
159 auto vtx_uniforms =
160 render_pass.GetTransientsBuffer().EmplaceUniform(uniforms);
161
162 size_t vertex_buffer_offset = 0;
163 size_t index_buffer_offset = total_vtx_bytes;
164
165 for (int draw_list_i = 0; draw_list_i < draw_data->CmdListsCount;
166 draw_list_i++) {
167 const ImDrawList* cmd_list = draw_data->CmdLists[draw_list_i];
168
169 // Convert ImGui's per-vertex data (`ImDrawVert`) into the per-vertex data
170 // required by the shader (`VS::PerVectexData`). The only difference is that
171 // `ImDrawVert` uses an `int` for the color and the impeller shader uses 4
172 // floats.
173
174 // TODO(102778): Remove the need for this by adding support for attribute
175 // mapping of uint32s host-side to vec4s shader-side in
176 // impellerc.
177 std::vector<VS::PerVertexData> vtx_data;
178 vtx_data.reserve(cmd_list->VtxBuffer.size());
179 for (const auto& v : cmd_list->VtxBuffer) {
180 ImVec4 color = ImGui::ColorConvertU32ToFloat4(v.col);
181 vtx_data.push_back({{v.pos.x, v.pos.y}, //
182 {v.uv.x, v.uv.y}, //
183 {color.x, color.y, color.z, color.w}});
184 }
185
186 auto draw_list_vtx_bytes =
187 static_cast<size_t>(vtx_data.size() * sizeof(VS::PerVertexData));
188 auto draw_list_idx_bytes =
189 static_cast<size_t>(cmd_list->IdxBuffer.size_in_bytes());
190 if (!buffer->CopyHostBuffer(reinterpret_cast<uint8_t*>(vtx_data.data()),
191 impeller::Range{0, draw_list_vtx_bytes},
192 vertex_buffer_offset)) {
193 IM_ASSERT(false && "Could not copy vertices to buffer.");
194 }
195 if (!buffer->CopyHostBuffer(
196 reinterpret_cast<uint8_t*>(cmd_list->IdxBuffer.Data),
197 impeller::Range{0, draw_list_idx_bytes}, index_buffer_offset)) {
198 IM_ASSERT(false && "Could not copy indices to buffer.");
199 }
200
201 for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
202 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
203
204 if (pcmd->UserCallback) {
205 pcmd->UserCallback(cmd_list, pcmd);
206 } else {
207 // Make the clip rect relative to the viewport.
208 auto clip_rect = impeller::Rect::MakeLTRB(
209 (pcmd->ClipRect.x - draw_data->DisplayPos.x) *
210 draw_data->FramebufferScale.x,
211 (pcmd->ClipRect.y - draw_data->DisplayPos.y) *
212 draw_data->FramebufferScale.y,
213 (pcmd->ClipRect.z - draw_data->DisplayPos.x) *
214 draw_data->FramebufferScale.x,
215 (pcmd->ClipRect.w - draw_data->DisplayPos.y) *
216 draw_data->FramebufferScale.y);
217 {
218 // Clamp the clip to the viewport bounds.
219 auto visible_clip = clip_rect.Intersection(viewport.rect);
220 if (!visible_clip.has_value()) {
221 continue; // Nothing to render.
222 }
223 clip_rect = visible_clip.value();
224 }
225 {
226 // Clamp the clip to ensure it never goes outside of the render
227 // target.
228 auto visible_clip = clip_rect.Intersection(
229 impeller::Rect::MakeSize(render_pass.GetRenderTargetSize()));
230 if (!visible_clip.has_value()) {
231 continue; // Nothing to render.
232 }
233 clip_rect = visible_clip.value();
234 }
235
236 impeller::Command cmd;
237 DEBUG_COMMAND_INFO(cmd,
238 impeller::SPrintF("ImGui draw list %d (command %d)",
239 draw_list_i, cmd_i));
240
241 cmd.viewport = viewport;
242 cmd.scissor = impeller::IRect(clip_rect);
243
244 cmd.pipeline = bd->pipeline;
245 VS::BindUniformBuffer(cmd, vtx_uniforms);
246 FS::BindTex(cmd, bd->font_texture, bd->sampler);
247
248 size_t vb_start =
249 vertex_buffer_offset + pcmd->VtxOffset * sizeof(ImDrawVert);
250
251 impeller::VertexBuffer vertex_buffer;
252 vertex_buffer.vertex_buffer = {
253 .buffer = buffer,
254 .range = impeller::Range(vb_start, draw_list_vtx_bytes - vb_start)};
255 vertex_buffer.index_buffer = {
256 .buffer = buffer,
257 .range = impeller::Range(
258 index_buffer_offset + pcmd->IdxOffset * sizeof(ImDrawIdx),
259 pcmd->ElemCount * sizeof(ImDrawIdx))};
260 vertex_buffer.vertex_count = pcmd->ElemCount;
261 vertex_buffer.index_type = impeller::IndexType::k16bit;
262 cmd.BindVertices(std::move(vertex_buffer));
263 cmd.base_vertex = pcmd->VtxOffset;
264
265 render_pass.AddCommand(std::move(cmd));
266 }
267 }
268
269 vertex_buffer_offset += draw_list_vtx_bytes;
270 index_buffer_offset += draw_list_idx_bytes;
271 }
272 }
ImGui_ImplImpeller_Data::context
std::shared_ptr< impeller::Context > context
Definition: imgui_impl_impeller.cc:37
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:92
impeller::VertexBuffer::index_type
IndexType index_type
Definition: vertex_buffer.h:29
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::Command::scissor
std::optional< IRect > scissor
Definition: command.h:139
point.h
impeller::IndexType::k16bit
@ k16bit
impeller::Viewport::rect
Rect rect
Definition: formats.h:396
vertex_buffer.h
impeller::TRect< Scalar >::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:34
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::VertexBuffer
Definition: vertex_buffer.h:13
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
texture_descriptor.h
formats.h
impeller::Command::viewport
std::optional< Viewport > viewport
Definition: command.h:133
impeller::DeviceBufferDescriptor::size
size_t size
Definition: device_buffer_descriptor.h:16
ImGui_ImplImpeller_GetBackendData
static ImGui_ImplImpeller_Data * ImGui_ImplImpeller_GetBackendData()
Definition: imgui_impl_impeller.cc:43
impeller::VertexBuffer::vertex_buffer
BufferView vertex_buffer
Definition: vertex_buffer.h:14
impeller::StorageMode::kHostVisible
@ kHostVisible
pipeline_builder.h
impeller::VertexBuffer::vertex_count
size_t vertex_count
Definition: vertex_buffer.h:23
sampler.h
impeller::Command::base_vertex
uint64_t base_vertex
Definition: command.h:126
matrix.h
command.h
ImGui_ImplImpeller_Data::sampler
std::shared_ptr< const impeller::Sampler > sampler
Definition: imgui_impl_impeller.cc:40
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:47
ImGui_ImplImpeller_Data::font_texture
std::shared_ptr< impeller::Texture > font_texture
Definition: imgui_impl_impeller.cc:38
render_pass.h
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::BufferView::buffer
std::shared_ptr< const Buffer > buffer
Definition: buffer_view.h:14
impeller::DeviceBufferDescriptor::storage_mode
StorageMode storage_mode
Definition: device_buffer_descriptor.h:15
impeller::Matrix::Translate
constexpr Matrix Translate(const Vector3 &t) const
Definition: matrix.h:235
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:108
range.h
impeller::IRect
TRect< int64_t > IRect
Definition: rect.h:489
pipeline_library.h
scalar.h
ImGui_ImplImpeller_Shutdown
void ImGui_ImplImpeller_Shutdown()
Definition: imgui_impl_impeller.cc:114
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Viewport
Definition: formats.h:395
impeller::VertexBuffer::index_buffer
BufferView index_buffer
The index buffer binding used by the vertex shader stage.
Definition: vertex_buffer.h:18
allocator.h
ImGui_ImplImpeller_RenderDrawData
void ImGui_ImplImpeller_RenderDrawData(ImDrawData *draw_data, impeller::RenderPass &render_pass)
Definition: imgui_impl_impeller.cc:121
impeller::Range
Definition: range.h:14
vector.h
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
ImGui_ImplImpeller_Data::pipeline
std::shared_ptr< impeller::Pipeline< impeller::PipelineDescriptor > > pipeline
Definition: imgui_impl_impeller.cc:39
rect.h
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:41
texture.h
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:459
context.h
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:38
ImGui_ImplImpeller_Init
bool ImGui_ImplImpeller_Init(const std::shared_ptr< impeller::Context > &context)
Definition: imgui_impl_impeller.cc:50
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:27
pipeline_descriptor.h
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:37
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
impeller::PipelineBuilder
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
Definition: pipeline_builder.h:32
ImGui_ImplImpeller_Data
Definition: imgui_impl_impeller.cc:36
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:67
imgui_impl_impeller.h
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55
size.h