egui_ash_renderer/lib.rs
1//! # egui-ash-renderer
2//!
3//! A Vulkan renderer for [egui][egui] using [Ash][ash].
4//!
5//! This is meant to add support for egui in your existing Vulkan/ash applications. Not a full eframe integration for Vulkan/ash.
6//!
7//! ## Compatibility
8//!
9//! | crate | egui | ash | gpu-allocator (feature) | vk-mem (feature) |
10//! |--------|--------------|--------------|-------------------------|------------------|
11//! | 0.10.0 | 0.33 | 0.38 | 0.28 | 0.5 |
12//! | 0.9.0 | 0.32 | 0.38 | 0.27 | 0.4 |
13//! | 0.8.0 | [0.26, 0.31] | 0.38 | 0.27 | 0.4 |
14//! | 0.7.0 | [0.26, 0.30] | 0.38 | 0.27 | 0.4 |
15//! | 0.6.0 | [0.26, 0.29] | 0.38 | 0.27 | 0.4 |
16//! | 0.5.0 | [0.26, 0.28] | 0.38 | 0.27 | 0.4 |
17//! | 0.4.0 | [0.26, 0.28] | [0.34, 0.37] | [0.25, 0.26] | 0.3 |
18//!
19//! ## How it works
20//!
21//! The renderer records drawing command to a command buffer supplied by the application. Here is a little breakdown of the features of this crate and how they work.
22//!
23//! ### Vertex/Index buffers
24//!
25//! The renderer creates a vertex buffer and a index buffer that will be updated every time
26//! `Renderer::cmd_draw` is called. If the vertex/index count is more than what the buffers can
27//! actually hold then the buffers are resized (actually destroyed then re-created).
28//!
29//! ### Frames in flight
30//!
31//! The renderer support having multiple frames in flight. You need to specify the number of frames
32//! during initialization of the renderer. The renderer manages one vertex and index buffer per frame.
33//!
34//! ### No draw call execution
35//!
36//! The `Renderer::cmd_draw` only record commands to a command buffer supplied by the application. It does not submit anything to the gpu.
37//!
38//! ### sRGB/Linear framebuffer
39//!
40//! You can indicate wether you will target an sRGB framebuffer or not by passing the option `srgb_framebuffer` when initializing the renderer.
41//! When you target an sRGB framebuffer, the fragment shader will output linear color values, otherwise it will convert the colors to sRGB.
42//!
43//! ### Managed textures
44//!
45//! Textures managed by egui must be kept in sync with the renderer. To do so, the user should call `Renderer::set_textures` and
46//! `Renderer::free_textures`. The former must be call before submitting the command buffer for rendering and the latter must be
47//! called after rendering is complete. Example:
48//!
49//! ```rust
50//! let output = egui_ctx.run(raw_input, |ui| {
51//! // ui code ..
52//! });
53//!
54//! // before rendering the ui
55//! renderer.set_textures(queue, command_pool, output.textures_delta.set.as_slice()).unwrap();
56//!
57//! // rendering code goes here .. (calling cmd_draw, submitting the command buffer, waiting for rendering to be finished...)
58//!
59//! // after the rendering is done
60//! renderer.free_textures(output.textures_delta.free.as_slice()).unwrap();
61//! ```
62//!
63//! > If you have multiple frames in flight you might want to hold a set of textures to free for each frame and call
64//! `Renderer::free_textures` after waiting for the fence of the previous frame.
65//!
66//! ### Custom textures
67//!
68//! You can also render used managed textures in egui. You just need to call `Renderer::add_user_texture` and pass a
69//! `vk::DescriptorSet` compatible with the layout used in the renderer's graphics pipeline
70//! (see [create_vulkan_descriptor_set_layout](./src/renderer/vulkan.rs)). This will return a `egui::TextureId` that you
71//! can use in your ui code. Example:
72//!
73//! ```rust
74//! let user_texture_set: vk::DescriptorSet = ...;
75//! let texture_id = renderer.add_user_texture(user_texture_set);
76//!
77//! let output = egui_ctx.run(raw_input, |ui| {
78//! let egui_texture = SizedTexture {
79//! id: texture_id,
80//! size: Vec2 {
81//! x: 128.0,
82//! y: 128.0,
83//! },
84//! };
85//!
86//! egui::Image::new(egui_texture).ui(ui);
87//! });
88//!
89//! // When the texture won't be used anymore you can remove it from the renderer
90//! renderer.remove_user_texture(texture_id);
91//! ```
92//!
93//! You can find a example using egui managed and user managed textures [here](./examples/textures.rs).
94//!
95//! ## Features
96//!
97//! ### gpu-allocator
98//!
99//! This feature adds support for [gpu-allocator][gpu-allocator]. It adds `Renderer::with_gpu_allocator` which takes
100//! a `Arc<Mutex<gpu_allocator::vulkan::Allocator>>`. All internal allocator are then done using the allocator.
101//!
102//! ### vk-mem
103//!
104//! This feature adds support for [vk-mem-rs][vk-mem-rs]. It adds `Renderer::with_vk_mem_allocator` which takes
105//! a `Arc<vk_mem::Allocator>`. All internal allocator are then done using the allocator.
106//!
107//! ### dynamic-rendering
108//!
109//! This feature is useful if you want to integrate the library in an app making use of Vulkan's dynamic rendering.
110//! When enabled, functions that usually takes a `vk::RenderPass` as argument will now take a `DynamicRendering` which
111//! contains the format of the color attachment the UI will be drawn to and an optional depth attachment format.
112//!
113//! ## Integration
114//!
115//! You can find an example of integration with [winit][winit] in the [common module](examples/common/mod.rs) of the examples.
116//!
117//! ```rust
118//! // Example with default allocator
119//! let renderer = Renderer::with_default_allocator(
120//! &vk_instance,
121//! vk_physical_device,
122//! vk_device.clone(),
123//! vk_render_pass,
124//! Options::default(),
125//! ).unwrap();
126//! ```
127//!
128//! ## Examples
129//!
130//! You can run a set of examples by running the following command:
131//!
132//! ```sh
133//! # If you want to enable validation layers
134//! export VK_LAYER_PATH=$VULKAN_SDK/Bin
135//! export VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation
136//!
137//! # Or with Powershell
138//! $env:VK_LAYER_PATH = "$env:VULKAN_SDK\Bin"
139//! $env:VK_INSTANCE_LAYERS = "VK_LAYER_KHRONOS_validation"
140//!
141//! # If you changed the shader code (you'll need glslangValidator on you PATH)
142//! # There is also a PowerShell version (compile_shaders.ps1)
143//! ./scripts/compile_shaders.sh
144//!
145//! # Run an example
146//! cargo run --example <example>
147//!
148//! # Example can be one of the following value:
149//! # - demo_windows
150//! # - textures
151//! ```
152//!
153//! [egui]: https://github.com/emilk/egui
154//! [ash]: https://github.com/MaikKlein/ash
155//! [gpu-allocator]: https://github.com/Traverse-Research/gpu-allocator
156//! [vk-mem-rs]: https://github.com/gwihlidal/vk-mem-rs
157//! [winit]: https://github.com/rust-windowing/winit
158
159mod error;
160mod renderer;
161
162pub use error::*;
163pub use renderer::*;