-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathimage_sampler.rs
272 lines (242 loc) · 9.21 KB
/
image_sampler.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
mod profile_with_puffin;
use {
clap::Parser,
hassle_rs::compile_hlsl,
inline_spirv::inline_spirv,
screen_13::prelude::*,
screen_13_window::WindowBuilder,
std::{
path::{Path, PathBuf},
sync::Arc,
},
};
/// Displays a sequence of image samplers.
///
/// Note that manually specifying image samplers is completely optional, valid defaults will be used
/// if they are not specified when creating the shader which uses them. Additionally, you could
/// instead use use name suffixes such as _llr or _nne for linear/linear repeat or nearest/nearest
/// clamp-to-edge.
///
/// You may run this example program with either --hlsl or --separate arguments as follows:
///
/// cargo run --example image_sampler -- --hlsl --separate
///
/// Run with --help for more information.
///
/// See min_max.rs for more advanced image sampler usage.
fn main() -> anyhow::Result<()> {
pretty_env_logger::init();
profile_with_puffin::init();
let args = Args::parse();
let window = WindowBuilder::default().debug(args.debug).build()?;
let gulf_image = read_image(&window.device, "examples/res/image/gulf.jpg")?;
// Sampler info contains the full definition of Vulkan sampler settings using a builder struct
let edge_edge = SamplerInfoBuilder::default()
.address_mode_u(vk::SamplerAddressMode::CLAMP_TO_EDGE)
.address_mode_v(vk::SamplerAddressMode::CLAMP_TO_EDGE);
let border_edge_black = SamplerInfoBuilder::default()
.address_mode_u(vk::SamplerAddressMode::CLAMP_TO_BORDER)
.address_mode_v(vk::SamplerAddressMode::CLAMP_TO_EDGE)
.border_color(vk::BorderColor::FLOAT_OPAQUE_BLACK);
let edge_border_white = SamplerInfoBuilder::default()
.address_mode_u(vk::SamplerAddressMode::CLAMP_TO_EDGE)
.address_mode_v(vk::SamplerAddressMode::CLAMP_TO_BORDER)
.border_color(vk::BorderColor::FLOAT_OPAQUE_WHITE);
// Image samplers are part of the shader pipeline and so we will create three pipelines total
let pipelines = [edge_edge, border_edge_black, edge_border_white]
.into_iter()
.map(|sampler_info| create_pipeline(&window.device, sampler_info))
.collect::<Result<Box<_>, _>>()?;
let mut pipeline_index = 0;
let mut pipeline_time = 0.0;
window.run(|frame| {
// Periodically change the active pipeline index
pipeline_time += 0.016;
if pipeline_time > 2.0 {
pipeline_time = 0.0;
pipeline_index += 1;
pipeline_index %= pipelines.len();
}
// Draw gulf.jpg using the active pipeline
let gulf_image = frame.render_graph.bind_node(&gulf_image);
frame
.render_graph
.begin_pass("Draw gulf image to swapchain")
.bind_pipeline(&pipelines[pipeline_index])
.read_descriptor(0, gulf_image)
.store_color(0, frame.swapchain_image)
.record_subpass(|subpass, _| {
subpass.draw(3, 1, 0, 0);
});
})?;
Ok(())
}
fn create_pipeline(
device: &Arc<Device>,
sampler_info: impl Into<SamplerInfo>,
) -> anyhow::Result<Arc<GraphicPipeline>> {
let args = Args::parse();
let mut frag_shader = match (args.hlsl, args.separate) {
(true, true) => {
// HLSL separate image sampler
Shader::new_fragment(
inline_spirv!(
r#"
struct FullscreenVertexOutput
{
float4 position : SV_Position;
[[vk::location(0)]] float2 uv : TEXCOORD0;
};
[[vk::binding(0, 0)]] Texture2D screenTexture : register(t0);
[[vk::binding(1, 0)]] SamplerState textureSampler : register(s0);
float4 main(FullscreenVertexOutput input)
: SV_Target
{
return screenTexture.Sample(textureSampler, input.uv);
}
"#,
frag,
hlsl
)
.as_slice(),
)
}
(true, false) => {
// HLSL combined image sampler: inline_spirv uses shaderc which does not support this, so
// we are using hassle_rs which uses dxc. You must follow the instructions listed here to
// use hassle_rs:
// See: https://github.com/Traverse-Research/hassle-rs
// See: https://github.com/microsoft/DirectXShaderCompiler/wiki/Vulkan-combined-image-sampler-type
// See: https://github.com/google/shaderc/issues/1310
Shader::new_fragment(
compile_hlsl(
"fragment.hlsl",
r#"
struct FullscreenVertexOutput
{
float4 position : SV_Position;
[[vk::location(0)]] float2 uv : TEXCOORD0;
};
[[vk::combinedImageSampler]][[vk::binding(0, 0)]] Texture2D<float4> screenTexture : register(t0);
[[vk::combinedImageSampler]][[vk::binding(0, 0)]] SamplerState textureSampler : register(s0);
float4 main(FullscreenVertexOutput input)
: SV_Target
{
return screenTexture.Sample(textureSampler, input.uv);
}
"#,
"main", "ps_5_0", &["-spirv"], &[],
)?
.as_slice(),
)
}
(false, true) => {
// GLSL separate image sampler
Shader::new_fragment(
inline_spirv!(
r#"
#version 460 core
layout(binding = 0) uniform texture2D image;
layout(binding = 1) uniform sampler image_sampler;
layout(location = 0) in vec2 vk_TexCoord;
layout(location = 0) out vec4 vk_Color;
void main() {
vk_Color = texture(sampler2D(image, image_sampler), vk_TexCoord);
}
"#,
frag
)
.as_slice(),
)
}
(false, false) => {
// GLSL combined image sampler
Shader::new_fragment(
inline_spirv!(
r#"
#version 460 core
layout(binding = 0) uniform sampler2D image;
layout(location = 0) in vec2 vk_TexCoord;
layout(location = 0) out vec4 vk_Color;
void main() {
vk_Color = texture(image, vk_TexCoord);
}
"#,
frag
)
.as_slice(),
)
}
};
// Use the builder pattern to specify an image sampler at the combined binding index (0) or
// separate binding index (1).
let sampler_binding = args.separate as u32;
frag_shader = frag_shader.image_sampler(sampler_binding, sampler_info);
Ok(Arc::new(GraphicPipeline::create(
device,
GraphicPipelineInfo::default(),
[
Shader::new_vertex(
inline_spirv!(
r#"
#version 460 core
const vec2[3] VERTICES = {
vec2(-1, -1),
vec2(-1, 3),
vec2( 3, -1),
};
layout(location = 0) out vec2 vk_TexCoord;
void main() {
gl_Position = vec4(VERTICES[gl_VertexIndex], 0, 1);
vk_TexCoord = 0.75 * gl_Position.xy + vec2(0.5);
}
"#,
vert
)
.as_slice(),
),
frag_shader,
],
)?))
}
fn read_image(device: &Arc<Device>, path: impl AsRef<Path>) -> anyhow::Result<Arc<Image>> {
// For another way to loading images, see screen_13_fx::ImageLoader
let gulf_jpg = image::open(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(path))?;
let image = Arc::new(Image::create(
device,
ImageInfo::image_2d(
gulf_jpg.width(),
gulf_jpg.height(),
vk::Format::R8G8B8A8_UNORM,
vk::ImageUsageFlags::SAMPLED | vk::ImageUsageFlags::TRANSFER_DST,
),
)?);
{
let mut render_graph = RenderGraph::new();
let image = render_graph.bind_node(&image);
let image_buf = render_graph.bind_node(Buffer::create_from_slice(
device,
vk::BufferUsageFlags::TRANSFER_SRC,
gulf_jpg.into_rgba8().into_vec(),
)?);
render_graph.copy_buffer_to_image(image_buf, image);
render_graph
.resolve()
.submit(&mut HashPool::new(device), 0, 0)?;
// Note: There is no need to call wait_until_executed() here
}
Ok(image)
}
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Enable Vulkan SDK validation layers
#[arg(long)]
debug: bool,
/// Use HLSL fragment shaders instead of the default (GLSL)
#[arg(long)]
hlsl: bool,
/// Use separate image sampler objects instead of the default (combined image sampler objects)
#[arg(long)]
separate: bool,
}