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
use {
super::Op,
crate::{
color::AlphaColor,
gpu::{
driver::{CommandPool, Device, Driver, Fence},
Lease, Pool, Texture2d,
},
},
gfx_hal::{
command::{ClearValue, CommandBuffer, CommandBufferFlags, Level},
format::Aspects,
image::{Access, Layout, SubresourceRange},
pool::CommandPool as _,
pso::PipelineStage,
queue::{CommandQueue as _, Submission},
Backend,
},
gfx_impl::Backend as _Backend,
std::{
any::Any,
iter::{empty, once},
},
};
pub struct ClearOp {
clear_value: ClearValue,
cmd_buf: <_Backend as Backend>::CommandBuffer,
cmd_pool: Lease<CommandPool>,
driver: Driver,
fence: Lease<Fence>,
pool: Option<Lease<Pool>>,
texture: Texture2d,
}
impl ClearOp {
#[must_use]
pub(crate) fn new(
#[cfg(feature = "debug-names")] name: &str,
driver: &Driver,
mut pool: Lease<Pool>,
texture: &Texture2d,
) -> Self {
let family = Device::queue_family(&driver.borrow());
let mut cmd_pool = pool.cmd_pool(driver, family);
Self {
clear_value: AlphaColor::rgba(0, 0, 0, 0).into(),
cmd_buf: unsafe { cmd_pool.allocate_one(Level::Primary) },
cmd_pool,
driver: Driver::clone(driver),
fence: pool.fence(
#[cfg(feature = "debug-names")]
name,
driver,
),
pool: Some(pool),
texture: Texture2d::clone(texture),
}
}
#[must_use]
pub fn with_value<C>(&mut self, clear_value: C) -> &mut Self
where
C: Into<ClearValue>,
{
self.clear_value = clear_value.into();
self
}
pub fn record(&mut self) {
unsafe {
self.submit();
}
}
unsafe fn submit(&mut self) {
trace!("submit");
let mut device = self.driver.borrow_mut();
let mut texture = self.texture.borrow_mut();
self.cmd_buf
.begin_primary(CommandBufferFlags::ONE_TIME_SUBMIT);
texture.set_layout(
&mut self.cmd_buf,
Layout::TransferDstOptimal,
PipelineStage::TRANSFER,
Access::TRANSFER_WRITE,
);
self.cmd_buf.clear_image(
texture.as_ref(),
Layout::TransferDstOptimal,
self.clear_value,
&[SubresourceRange {
aspects: Aspects::COLOR,
..Default::default()
}],
);
self.cmd_buf.finish();
Device::queue_mut(&mut device).submit(
Submission {
command_buffers: once(&self.cmd_buf),
wait_semaphores: empty(),
signal_semaphores: empty::<&<_Backend as Backend>::Semaphore>(),
},
Some(&self.fence),
);
}
}
impl Drop for ClearOp {
fn drop(&mut self) {
self.wait();
}
}
impl Op for ClearOp {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn take_pool(&mut self) -> Option<Lease<Pool>> {
self.pool.take()
}
fn wait(&self) {
Fence::wait(&self.fence);
}
}