def __init__(self, width, height, title): super().__init__(width, height, title) self.elapsed_time = 0 self.write_r = ImGui.Bool(True) self.write_g = ImGui.Bool(True) self.write_b = ImGui.Bool(True) self.write_a = ImGui.Bool(True) self.primitive_geometry = ImGui.Int(0) self.init_conf = bgfx.Init() self.init_conf.debug = True self.init_conf.resolution.width = self.width self.init_conf.resolution.height = self.height if "GITHUB_ACTIONS" in os.environ: self.init_conf.type = bgfx.RendererType.NOOP self.init_conf.resolution.reset = BGFX_RESET_VSYNC
def resource_bar(name, tooltip, num, _max, max_width, height): item_hovered = False ImGui.Text(f"{name}: {num:4d} / {_max:4d}") item_hovered |= ImGui.IsItemHovered() ImGui.SameLine() percentage = float(num) / float(_max) item_hovered |= bar(max(1.0, percentage * max_width), max_width, height * 2, s_resourceColor) ImGui.SameLine() ImGui.Text(f"{(percentage * 100.0):5.2f}%") if item_hovered: ImGui.SetTooltip(f"{tooltip} {(percentage * 100.0):5.2f}%")
def show_example_dialog(): ImGui.set_next_window_pos(ImGui.Vec2(10.0, 70.0), ImGui.Condition.FirstUseEver) ImGui.set_next_window_size(ImGui.Vec2(300.0, 500.0), ImGui.Condition.FirstUseEver) ImGui.begin("\uf080 Statistics") renderer_name = bgfx.get_renderer_name(bgfx.get_renderer_type()) ImGui.text_wrapped(f"Current renderer: {renderer_name}") ImGui.separator() stats = bgfx.get_stats() to_ms_cpu = 1000.0 / stats.cpu_timer_freq to_ms_gpu = 1000.0 / stats.gpu_timer_freq frame_ms = max(float(stats.cpu_time_end - stats.cpu_time_begin), 1.0e-9) s_frame_time.push_sample(frame_ms * to_ms_cpu) frame_text_overlay = f"\uf063{s_frame_time.m_min:7.3f}ms, \uf062{s_frame_time.m_max:7.3f}ms\nAvg: {s_frame_time.m_avg:7.3f}ms, {(stats.cpu_timer_freq / frame_ms):6.2f} FPS" ImGui.push_style_color(ImGui.Colors.PlotHistogram, ImGui.Vec4(0.0, 0.5, 0.15, 1.0)) ImGui.push_item_width(-1) ImGui.plot_histogram( "", array("f", s_frame_time.m_values)[0], 100, s_frame_time.m_offset, frame_text_overlay, 0.0, 60.0, ImGui.Vec2(0.0, 45.0), ) ImGui.pop_item_width() ImGui.pop_style_color() ImGui.text( f"Submit CPU {(stats.cpu_time_end - stats.cpu_time_begin) * to_ms_cpu:.3f}, GPU {(stats.gpu_time_end - stats.gpu_time_begin) * to_ms_gpu:.3f} (L: {stats.max_gpu_latency})" ) if stats.gpu_memory_max > 0: ImGui.text( f"GPU mem: {stats.gpu_memory_used} / {stats.gpu_memory_max}") if ImGui.collapsing_header("\uf12e Resources", 1 << 5): caps = bgfx.get_caps() item_height = ImGui.get_text_line_height_with_spacing() max_width = 90.0 ImGui.push_font(ImGui.Font.Mono) ImGui.text("Res: Num / Max") resource_bar( "DIB", "Dynamic index buffers", stats.num_dynamic_index_buffers, caps.limits.max_dynamic_index_buffers, max_width, item_height, ) resource_bar( "DVB", "Dynamic vertex buffers", stats.num_dynamic_vertex_buffers, caps.limits.max_dynamic_vertex_buffers, max_width, item_height, ) resource_bar( " FB", "Frame buffers", stats.num_frame_buffers, caps.limits.max_frame_buffers, max_width, item_height, ) resource_bar( " IB", "Index buffers", stats.num_index_buffers, caps.limits.max_index_buffers, max_width, item_height, ) resource_bar( " OQ", "Occlusion queries", stats.num_occlusion_queries, caps.limits.max_occlusion_queries, max_width, item_height, ) resource_bar( " P", "Programs", stats.num_programs, caps.limits.max_programs, max_width, item_height, ) resource_bar( " S", "Shaders", stats.num_shaders, caps.limits.max_shaders, max_width, item_height, ) resource_bar( " T", "Textures", stats.num_textures, caps.limits.max_textures, max_width, item_height, ) resource_bar( " U", "Uniforms", stats.num_uniforms, caps.limits.max_uniforms, max_width, item_height, ) resource_bar( " VB", "Vertex buffers", stats.num_vertex_buffers, caps.limits.max_vertex_buffers, max_width, item_height, ) resource_bar( " VL", "Vertex layouts", stats.num_vertex_layouts, caps.limits.max_vertex_layouts, max_width, item_height, ) ImGui.pop_font() ImGui.end()
def resource_bar(name, tooltip, num, _max, max_width, height): item_hovered = False ImGui.text(f"{name}: {num:4d} / {_max:4d}") item_hovered |= ImGui.is_item_hovered(0) ImGui.same_line() percentage = float(num) / float(_max) item_hovered |= bar(max(1.0, percentage * max_width), max_width, height, s_resourceColor) ImGui.same_line() ImGui.text(f"{(percentage * 100.0):5.2f}%") if item_hovered: ImGui.set_tooltip(f"{tooltip} {(percentage * 100.0):5.2f}%")
def bar(width, max_width, height, color): style = ImGui.get_style() hovered_color = ImGui.Vec4( color.x + color.x * 0.1, color.y + color.y * 0.1, color.z + color.z * 0.1, color.w + color.w * 0.1, ) ImGui.push_style_color(ImGui.Colors.Button, color) ImGui.push_style_color(ImGui.Colors.ButtonHovered, hovered_color) ImGui.push_style_color(ImGui.Colors.ButtonActive, color) ImGui.push_style_var(ImGui.Style.FrameRounding, 0.0) ImGui.push_style_var(ImGui.Style.ItemSpacing, ImGui.Vec2(0.0, style.item_spacing.y)) item_hovered = False ImGui.button("", ImGui.Vec2(width, height)) item_hovered |= ImGui.is_item_hovered(0) ImGui.same_line() ImGui.invisible_button("", ImGui.Vec2(max(1.0, max_width - width), height)) item_hovered |= ImGui.is_item_hovered(0) ImGui.pop_style_var(2) ImGui.pop_style_color(3) return item_hovered
item_hovered = False ImGui.button("", ImGui.Vec2(width, height)) item_hovered |= ImGui.is_item_hovered(0) ImGui.same_line() ImGui.invisible_button("", ImGui.Vec2(max(1.0, max_width - width), height)) item_hovered |= ImGui.is_item_hovered(0) ImGui.pop_style_var(2) ImGui.pop_style_color(3) return item_hovered s_resourceColor = ImGui.Vec4(0.5, 0.5, 0.5, 1.0) s_frame_time = SampleData() def resource_bar(name, tooltip, num, _max, max_width, height): item_hovered = False ImGui.text(f"{name}: {num:4d} / {_max:4d}") item_hovered |= ImGui.is_item_hovered(0) ImGui.same_line() percentage = float(num) / float(_max) item_hovered |= bar(max(1.0, percentage * max_width), max_width, height, s_resourceColor) ImGui.same_line()
def show_properties_dialog(fluid_simulator: FluidSimulator, particles_area: SmoothParticlesArea, hidpi: bool): res_multiplier = 2 if hidpi else 1 ImGui.SetNextWindowPos( ImVec2(20.0 * res_multiplier, 300.0 * res_multiplier), 1 << 2) ImGui.SetNextWindowSize( ImVec2(300.0 * res_multiplier, 400.0 * res_multiplier), 1 << 2) ImGui.Begin("\uf013 Properties") ImGui.TextWrapped("Simulation performances") ImGui.Separator() stats = bgfx.getStats() to_ms_cpu = 1000.0 / stats.cpuTimerFreq to_ms_gpu = 1000.0 / stats.gpuTimerFreq frame_ms = float(stats.cpuTimeEnd - stats.cpuTimeBegin) s_frame_time.push_sample(frame_ms * to_ms_cpu) frame_text_overlay = f"\uf063{s_frame_time.m_min:7.3f}ms, \uf062{s_frame_time.m_max:7.3f}ms\nAvg: {s_frame_time.m_avg:7.3f}ms, {(stats.cpuTimerFreq / frame_ms):6.2f} FPS" ImGui.PushStyleColor(40, ImVec4(0.0, 0.5, 0.15, 1.0)) ImGui.PushItemWidth(-1) ImGui.PlotHistogram( "", array("f", s_frame_time.m_values)[0], 100, s_frame_time.m_offset, frame_text_overlay, 0.0, 60.0, ImVec2(0.0, 45.0 * res_multiplier), ) ImGui.PopItemWidth() ImGui.PopStyleColor() ImGui.Text( f"Submit CPU {(stats.cpuTimeEnd - stats.cpuTimeBegin) * to_ms_cpu:3.3f}, GPU {(stats.gpuTimeEnd - stats.gpuTimeBegin) * to_ms_gpu:3.3f} (L: {stats.maxGpuLatency})" ) if stats.gpuMemoryMax > 0: ImGui.Text(f"GPU mem: {stats.gpuMemoryUsed} / {stats.gpuMemoryMax}") ImGui.Separator() vorticity = ImGui.Float(fluid_simulator.vorticity) viscosity = ImGui.Float(fluid_simulator.viscosity) speed = ImGui.Float(fluid_simulator.speed) iterations = ImGui.Int(fluid_simulator.iterations) borders = ImGui.Bool(fluid_simulator.has_borders) ImGui.Text("Fluid simulation parameters") if ImGui.SliderFloat("Vorticity", vorticity, 0.0, 10.0): fluid_simulator.vorticity = vorticity.value if ImGui.SliderFloat("Viscosity", viscosity, 0.000, 1.0): fluid_simulator.viscosity = viscosity.value if ImGui.SliderFloat("Speed", speed, 1.0, 1000.0): fluid_simulator.speed = speed.value particles_area.speed = speed.value if ImGui.SliderInt("Iteration", iterations, 10, 100): fluid_simulator.iterations = iterations.value if ImGui.Checkbox("Borders", borders): fluid_simulator.has_borders = borders.value ImGui.Separator() dissipation = ImGui.Float(particles_area.dissipation) ImGui.Text("Particles area parameters") if ImGui.SliderFloat("Dissipation", dissipation, 0.900, 1.0): particles_area.dissipation = dissipation.value ImGui.Separator() stop = ImGui.Bool(not fluid_simulator.simulate) if ImGui.Checkbox("Stop", stop): fluid_simulator.simulate = not stop.value particles_area.simulate = not stop.value ImGui.End()
def bar(width, max_width, height, color): style = ImGui.GetStyle() hovered_color = ImVec4( color.x + color.x * 0.1, color.y + color.y * 0.1, color.z + color.z * 0.1, color.w + color.w * 0.1, ) ImGui.PushStyleColor(21, color) ImGui.PushStyleColor(22, hovered_color) ImGui.PushStyleColor(23, color) ImGui.PushStyleVar(12, 0.0) ImGui.PushStyleVar(14, ImVec2(0.0, style.ItemSpacing.y)) item_hovered = False ImGui.Button("", ImVec2(width, height)) item_hovered |= ImGui.IsItemHovered() ImGui.SameLine() ImGui.InvisibleButton("", ImVec2(max(1.0, max_width - width), height)) item_hovered |= ImGui.IsItemHovered() ImGui.PopStyleVar(2) ImGui.PopStyleColor(3) return item_hovered
def _create_imgui_cubes_selection_dialog(self): ImGui.set_next_window_pos( ImGui.Vec2(self.width - self.width / 5.0 - 10.0, 10.0), 1 << 2) ImGui.set_next_window_size( ImGui.Vec2(self.width / 5.0, self.height / 3.5), 1 << 2) ImGui.begin("Settings") ImGui.checkbox("Write R", self.write_r) ImGui.checkbox("Write G", self.write_g) ImGui.checkbox("Write B", self.write_b) ImGui.checkbox("Write A", self.write_a) ImGui.text("Primitive topology:") ImGui.combo( "", self.primitive_geometry, [ "Triangle List", "Triangle Strip", "Lines", "Line Strip", "Points" ], ) ImGui.end()