Skip to content

Commit 0d54c57

Browse files
committed
- Add overlay example as a official example and add to cargo
- Moved `vec` allocation for per-frame pass to a bump allocator; huge speed improvement. - Change shaders to be precompiled by default, removing `shader-compiler` feature requirement, reduces build times/removes shaderc requirement - bump version for publish
1 parent 66c4498 commit 0d54c57

9 files changed

Lines changed: 105 additions & 23 deletions

File tree

Cargo.toml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "amethyst-imgui"
3-
version = "0.5.1"
3+
version = "0.5.2"
44
description = "imgui library for amethyst"
55
license = "CC0-1.0"
66
authors = ["Awpteamoose <cargo@awpteamoose.my.to>", "Walter Pearce <jaynus@gmail.com>"]
@@ -10,22 +10,32 @@ edition = "2018"
1010

1111
[features]
1212
default = ["amethyst/vulkan"]
13+
shader-compiler = ["amethyst/shader-compiler"]
1314
docking = []
1415

1516
[dependencies]
1617
imgui = { version = "0.2" }
1718
imgui-winit-support = { version = "0.2" }
18-
amethyst = { version = "0.13", features = ["shader-compiler"] }
19-
#amethyst = { path = "../amethyst", features = ["nightly", "saveload", "vulkan", "gltf", "experimental-spirv-reflection", "shader-compiler", "tiles"] }
19+
amethyst = { version = "0.13" }
2020

21-
# For docking support
22-
#imgui = { git = "https://github.com/jaynus/imgui-rs", branch"="docking" }
23-
#imgui-winit-support = { git = "https://github.com/jaynus/imgui-rs", branch"="docking" }
21+
# Development dependencies
22+
#amethyst = { path = "../amethyst", features = ["saveload", "vulkan", "gltf", "experimental-spirv-reflection", "shader-compiler", "tiles"] }
23+
#imgui = { path = "../imgui-rs" }
24+
#imgui-winit-support = { path = "../imgui-rs/imgui-winit-support" }
25+
26+
# Docking dependencies
27+
#imgui = { git = "https://github.com/jaynus/imgui-rs.git" }
28+
#imgui-winit-support = { git = "https://github.com/jaynus/imgui-rs.git" }
2429

2530
lazy_static = "1.3"
2631
derivative = "1.0"
2732
failure = "0.1"
33+
bumpalo = "2.6"
2834

2935
[[example]]
3036
name = "demo_window"
3137
path = "examples/demo_window.rs"
38+
39+
[[example]]
40+
name = "demo_overlay"
41+
path = "examples/demo_overlay.rs"

Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This Makefile is meant for shader compilation only.
2+
# Use cargo to compile the rust part of the project.
3+
4+
GLSLC = $(shell ./find_glslc.sh)
5+
ifeq "$(GLSLC)" ""
6+
break;
7+
endif
8+
9+
FLAGS = -c -g
10+
11+
SHADERS=$(wildcard src/shaders/*)
12+
COMP_SHADERS = $(patsubst src/shaders/%,compiled/%.spv,$(SHADERS))
13+
COMP_DISASMS = $(patsubst src/shaders/%,compiled/%.spvasm,$(SHADERS))
14+
15+
16+
all: $(COMP_SHADERS) $(COMP_DISASMS)
17+
18+
compiled/%.spv: src/shaders/%
19+
mkdir -p $(dir $@)
20+
$(GLSLC) -MD -c -O -o $@ $<
21+
22+
compiled/%.spvasm: src/shaders/%
23+
mkdir -p $(dir $@)
24+
$(GLSLC) -MD -S -g -O -o $@ $<
25+
26+
clean:
27+
rm compiled/*.spv compiled/*.spvasm compiled/*.d
28+
29+
.PHONY: all clean

compiled/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.spvasm
2+
*.d

compiled/imgui.frag.spv

636 Bytes
Binary file not shown.

compiled/imgui.vert.spv

988 Bytes
Binary file not shown.

examples/demo_overlay.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use amethyst::{
77
renderer::{bundle::RenderingBundle, types::DefaultBackend, RenderToWindow},
88
utils::application_root_dir,
99
};
10-
use amethyst_imgui::{imgui, imgui::im_str, RenderImgui};
10+
use amethyst_imgui::{
11+
imgui,
12+
imgui::{im_str, ImString},
13+
RenderImgui,
14+
};
1115
use std::sync::{Arc, Mutex};
1216

1317
const DISTANCE: f32 = 10.0;

find_glslc.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cd "${0%/*}"
2+
set -e
3+
4+
BIN=""
5+
6+
if [ -e "$(which glslc)" ]; then
7+
BIN="$(which glslc)"
8+
elif [ -e "$(which glslc.exe)" ]; then
9+
# support WSL
10+
BIN="$(which glslc.exe)"
11+
else
12+
>&2 echo "Cannot find glslc binary. Make sure you have VulkanSDK installed and added to PATH."
13+
exit 1
14+
fi
15+
16+
echo "$BIN"

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ impl<'a, 'b, T: BindingTypes> SystemDesc<'a, 'b, ImguiInputSystem<T>> for ImguiI
112112
}
113113
}
114114

115-
/// Ui is actually Ui<'a>
116-
/// This implies 'static
117115
static mut CURRENT_UI: Option<imgui::Ui<'static>> = None;
118116

119117
pub fn with(f: impl FnOnce(&imgui::Ui)) {
@@ -124,7 +122,6 @@ pub fn with(f: impl FnOnce(&imgui::Ui)) {
124122
}
125123
}
126124

127-
// what lifeimtes go here? how do I use transmute here?
128125
pub unsafe fn current_ui<'a>() -> Option<&'a imgui::Ui<'a>> { CURRENT_UI.as_ref() }
129126

130127
/// A [RenderPlugin] for rendering Imgui elements.

src/pass.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use amethyst::{
2424
pso,
2525
},
2626
mesh::{AsAttribute, AsVertex, Color, TexCoord, VertexFormat},
27-
shader::{PathBufShaderInfo, Shader, ShaderKind, SourceLanguage, SpirvShader},
27+
shader::{Shader, SpirvShader},
2828
texture::TextureBuilder,
2929
},
3030
submodules::{DynamicIndexBuffer, DynamicVertexBuffer, TextureId, TextureSub},
@@ -36,17 +36,25 @@ use amethyst::{
3636
window::Window,
3737
winit::Event,
3838
};
39+
use bumpalo::{collections::Vec as BumpVec, Bump};
40+
3941
use derivative::Derivative;
4042
use imgui::{internal::RawWrapper, DrawCmd, DrawCmdParams};
4143
use std::{
4244
borrow::Cow,
43-
path::PathBuf,
4445
sync::{Arc, Mutex},
4546
};
4647

4748
use crate::ImguiContextWrapper;
48-
use imgui_winit_support::{WinitPlatform};
49+
use imgui_winit_support::WinitPlatform;
50+
51+
#[cfg(feature = "shader-compiler")]
52+
use amethyst::renderer::rendy::shader::{PathBufShaderInfo, ShaderKind, SourceLanguage};
53+
54+
#[cfg(feature = "shader-compiler")]
55+
use std::path::PathBuf;
4956

57+
#[cfg(feature = "shader-compiler")]
5058
lazy_static::lazy_static! {
5159
static ref VERTEX_SRC: SpirvShader = PathBufShaderInfo::new(
5260
PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/src/shaders/imgui.vert")),
@@ -73,12 +81,25 @@ lazy_static::lazy_static! {
7381
(*FRAGMENT_SRC).stage(),
7482
"main",
7583
);
84+
}
7685

77-
static ref TIME: std::sync::Mutex<std::time::Instant> = std::sync::Mutex::new(std::time::Instant::now());
86+
#[cfg(not(feature = "shader-compiler"))]
87+
lazy_static::lazy_static! {
88+
static ref VERTEX: SpirvShader = SpirvShader::new(
89+
include_bytes!("../compiled/imgui.vert.spv").to_vec(),
90+
pso::ShaderStageFlags::VERTEX,
91+
"main",
92+
);
7893

79-
//static ref SHADERS: ShaderSetBuilder = ShaderSetBuilder::default()
80-
// .with_vertex(&*VERTEX).unwrap()
81-
// .with_fragment(&*FRAGMENT).unwrap();
94+
static ref FRAGMENT: SpirvShader = SpirvShader::new(
95+
include_bytes!("../compiled/imgui.frag.spv").to_vec(),
96+
pso::ShaderStageFlags::FRAGMENT,
97+
"main",
98+
);
99+
}
100+
101+
lazy_static::lazy_static! {
102+
static ref TIME: std::sync::Mutex<std::time::Instant> = std::sync::Mutex::new(std::time::Instant::now());
82103
}
83104

84105
#[repr(transparent)]
@@ -220,10 +241,8 @@ impl<B: Backend> RenderGroupDesc<B, World> for DrawImguiDesc {
220241
_buffers: Vec<NodeBuffer>,
221242
_images: Vec<NodeImage>,
222243
) -> Result<Box<dyn RenderGroup<B, World>>, failure::Error> {
223-
let (context_mutex, mut winit_events) = <(
224-
ReadExpect<'_, Arc<Mutex<ImguiContextWrapper>>>,
225-
Write<'_, EventChannel<Event>>,
226-
)>::fetch(world);
244+
let (context_mutex, mut winit_events) =
245+
<(ReadExpect<'_, Arc<Mutex<ImguiContextWrapper>>>, Write<'_, EventChannel<Event>>)>::fetch(world);
227246

228247
let context = &mut context_mutex.lock().unwrap().0;
229248

@@ -251,6 +270,7 @@ impl<B: Backend> RenderGroupDesc<B, World> for DrawImguiDesc {
251270
batches: Default::default(),
252271
imgui_textures,
253272
reader_id: winit_events.register_reader(),
273+
bump: Mutex::new(Bump::default()),
254274
}))
255275
}
256276
}
@@ -276,6 +296,7 @@ pub struct DrawImgui<B: Backend> {
276296
constant: ImguiPushConstant,
277297
imgui_textures: Vec<Handle<Texture>>,
278298
reader_id: ReaderId<Event>,
299+
bump: Mutex<Bump>,
279300
}
280301

281302
impl<B: Backend> DrawImgui<B> {}
@@ -297,6 +318,9 @@ impl<B: Backend> RenderGroup<B, World> for DrawImgui<B> {
297318
Read<'_, EventChannel<Event>>,
298319
)>::fetch(world);
299320

321+
let mut bump = self.bump.lock().unwrap();
322+
bump.reset();
323+
300324
let state = &mut context.lock().unwrap().0;
301325

302326
for texture in &self.imgui_textures {
@@ -324,8 +348,8 @@ impl<B: Backend> RenderGroup<B, World> for DrawImgui<B> {
324348
&*(imgui::sys::igGetDrawData() as *mut imgui::DrawData)
325349
};
326350

327-
let mut vertices: Vec<ImguiArgs> = Vec::with_capacity(draw_data.total_vtx_count as usize);
328-
let mut indices: Vec<u16> = Vec::with_capacity(draw_data.total_idx_count as usize);
351+
let mut vertices: BumpVec<ImguiArgs> = BumpVec::with_capacity_in(draw_data.total_vtx_count as usize, &bump);
352+
let mut indices: BumpVec<u16> = BumpVec::with_capacity_in(draw_data.total_idx_count as usize, &bump);
329353

330354
self.commands.reserve(draw_data.draw_lists().count());
331355

0 commit comments

Comments
 (0)