Flutter Impeller
imgui_impl_impeller.h File Reference
#include <memory>
#include "third_party/imgui/imgui.h"

Go to the source code of this file.

Namespaces

 impeller
 

Functions

IMGUI_IMPL_API bool ImGui_ImplImpeller_Init (const std::shared_ptr< impeller::Context > &context)
 
IMGUI_IMPL_API void ImGui_ImplImpeller_Shutdown ()
 
IMGUI_IMPL_API void ImGui_ImplImpeller_RenderDrawData (ImDrawData *draw_data, impeller::RenderPass &renderpass)
 

Function Documentation

◆ ImGui_ImplImpeller_Init()

IMGUI_IMPL_API bool ImGui_ImplImpeller_Init ( const std::shared_ptr< impeller::Context > &  context)

Definition at line 50 of file imgui_impl_impeller.cc.

51  {
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{};
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 }

References impeller::kHostVisible, impeller::kR8G8B8A8UNormInt, and impeller::TextureDescriptor::storage_mode.

Referenced by impeller::Playground::OpenPlaygroundHere().

◆ ImGui_ImplImpeller_RenderDrawData()

IMGUI_IMPL_API void ImGui_ImplImpeller_RenderDrawData ( ImDrawData *  draw_data,
impeller::RenderPass renderpass 
)

Definition at line 121 of file imgui_impl_impeller.cc.

122  {
123  if (draw_data->CmdListsCount == 0) {
124  return; // Nothing to render.
125  }
126 
127  using VS = impeller::ImguiRasterVertexShader;
128  using FS = impeller::ImguiRasterFragmentShader;
129 
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.
141  buffer_desc.size = total_vtx_bytes + total_idx_bytes;
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 }

References impeller::RenderPass::AddCommand(), impeller::Command::base_vertex, impeller::Command::BindVertices(), impeller::BufferView::buffer, DEBUG_COMMAND_INFO, impeller::HostBuffer::EmplaceUniform(), impeller::RenderPass::GetRenderTargetSize(), impeller::RenderPass::GetTransientsBuffer(), ImGui_ImplImpeller_GetBackendData(), impeller::VertexBuffer::index_buffer, impeller::VertexBuffer::index_type, impeller::k16bit, impeller::kHostVisible, impeller::TRect< Scalar >::MakeLTRB(), impeller::Matrix::MakeOrthographic(), impeller::TRect< Scalar >::MakeSize(), impeller::TRect< Scalar >::MakeXYWH(), impeller::Command::pipeline, impeller::Viewport::rect, impeller::TRect< T >::Scale(), impeller::Command::scissor, impeller::DeviceBufferDescriptor::size, impeller::SPrintF(), impeller::DeviceBufferDescriptor::storage_mode, impeller::Matrix::Translate(), impeller::VertexBuffer::vertex_buffer, impeller::VertexBuffer::vertex_count, and impeller::Command::viewport.

Referenced by impeller::Playground::OpenPlaygroundHere().

◆ ImGui_ImplImpeller_Shutdown()

IMGUI_IMPL_API void ImGui_ImplImpeller_Shutdown ( )

Definition at line 114 of file imgui_impl_impeller.cc.

114  {
116  IM_ASSERT(bd != nullptr &&
117  "No renderer backend to shutdown, or already shutdown?");
118  delete bd;
119 }

References ImGui_ImplImpeller_GetBackendData().

Referenced by impeller::Playground::OpenPlaygroundHere().

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
impeller::IndexType::k16bit
@ k16bit
impeller::Viewport::rect
Rect rect
Definition: formats.h:396
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
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
impeller::VertexBuffer::vertex_count
size_t vertex_count
Definition: vertex_buffer.h:23
impeller::Command::base_vertex
uint64_t base_vertex
Definition: command.h:126
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
impeller::IRect
TRect< int64_t > IRect
Definition: rect.h:489
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
impeller::Range
Definition: range.h:14
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:459
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:38
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:27
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