-
Notifications
You must be signed in to change notification settings - Fork 5
/
textures.rs
166 lines (143 loc) · 4.69 KB
/
textures.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
mod common;
use ash::vk;
use common::{run_app, vulkan::texture::Texture, App, System, VulkanContext};
use egui::{load::SizedTexture, Vec2, Widget};
use egui_ash_renderer::vulkan::*;
use simple_logger::SimpleLogger;
use std::error::Error;
struct TexturesDemo {
srgb_texture: UserTexture,
linear_texture: UserTexture,
descriptor_set_layout: vk::DescriptorSetLayout,
descriptor_pool: vk::DescriptorPool,
show_srgb_texture: bool,
}
impl App for TexturesDemo {
fn title() -> &'static str {
"textures"
}
fn new(app: &mut System) -> Self {
let VulkanContext {
physical_device,
device,
..
} = &app.vulkan_context;
let memory_properties = unsafe {
app.vulkan_context
.instance
.get_physical_device_memory_properties(*physical_device)
};
let descriptor_set_layout = create_vulkan_descriptor_set_layout(device).unwrap();
let descriptor_pool = create_vulkan_descriptor_pool(device, 2).unwrap();
let srgb_texture = UserTexture::from_memory(
app,
memory_properties,
descriptor_set_layout,
descriptor_pool,
vk::Format::R8G8B8A8_SRGB,
include_bytes!("../assets/images/img2.jpg"),
);
let linear_texture = UserTexture::from_memory(
app,
memory_properties,
descriptor_set_layout,
descriptor_pool,
vk::Format::R8G8B8A8_UNORM,
include_bytes!("../assets/images/normals.jpg"),
);
let show_srgb_texture = true;
Self {
srgb_texture,
linear_texture,
descriptor_set_layout,
descriptor_pool,
show_srgb_texture,
}
}
fn build_ui(&mut self, ctx: &egui::Context) {
egui::Window::new("Managed texture")
.show(ctx, |ui| {
ui.label("This texture is loaded and managed by egui. Loaders must be installed for it to work.");
egui::Image::new(egui::include_image!("../assets/images/img1.jpg")).fit_to_original_size(0.8).ui(ui);
});
egui::Window::new("Used defined texture").show(ctx, |ui| {
ui.label("This texture is loaded and managed by the user.");
ui.horizontal(|ui| {
ui.radio_value(&mut self.show_srgb_texture, true, "sRGB");
ui.radio_value(&mut self.show_srgb_texture, false, "Linear");
});
let texture = if self.show_srgb_texture {
self.srgb_texture.egui_texture
} else {
self.linear_texture.egui_texture
};
egui::Image::new(texture).fit_to_original_size(0.8).ui(ui);
});
}
fn clean(&mut self, vulkan_ctx: &VulkanContext) {
let device = &vulkan_ctx.device;
unsafe {
device.destroy_descriptor_pool(self.descriptor_pool, None);
device.destroy_descriptor_set_layout(self.descriptor_set_layout, None);
}
self.srgb_texture.texture.destroy(device);
self.linear_texture.texture.destroy(device);
}
}
fn main() -> Result<(), Box<dyn Error>> {
SimpleLogger::new().init()?;
run_app::<TexturesDemo>()?;
Ok(())
}
struct UserTexture {
texture: Texture,
_set: vk::DescriptorSet,
egui_texture: SizedTexture,
}
impl UserTexture {
fn from_memory(
app: &mut System,
memory_properties: vk::PhysicalDeviceMemoryProperties,
set_layout: vk::DescriptorSetLayout,
set_pool: vk::DescriptorPool,
format: vk::Format,
data: &[u8],
) -> Self {
let image = image::load_from_memory(data).unwrap();
let width = image.width();
let height = image.height();
let data = image.to_rgba8().into_vec();
let texture = Texture::from_rgba8(
&app.vulkan_context.device,
app.vulkan_context.graphics_queue,
app.vulkan_context.command_pool,
memory_properties,
width,
height,
format,
data.as_slice(),
)
.unwrap();
let set = create_vulkan_descriptor_set(
&app.vulkan_context.device,
set_layout,
set_pool,
texture.image_view,
texture.sampler,
)
.unwrap();
let texture_id = app.renderer.add_user_texture(set);
let egui_texture = SizedTexture {
id: texture_id,
size: Vec2 {
x: width as f32,
y: height as f32,
},
};
Self {
texture,
_set: set,
egui_texture,
}
}
}