|
179 | 179 | <span id="171">171</span>
|
180 | 180 | <span id="172">172</span>
|
181 | 181 | <span id="173">173</span>
|
| 182 | +<span id="174">174</span> |
| 183 | +<span id="175">175</span> |
| 184 | +<span id="176">176</span> |
| 185 | +<span id="177">177</span> |
| 186 | +<span id="178">178</span> |
| 187 | +<span id="179">179</span> |
| 188 | +<span id="180">180</span> |
| 189 | +<span id="181">181</span> |
| 190 | +<span id="182">182</span> |
| 191 | +<span id="183">183</span> |
182 | 192 | </pre><pre class="rust"><code><span class="kw">use</span> <span class="kw">crate</span>::{
|
183 | 193 | <span class="ident">util::align_to</span>, <span class="ident">Buffer</span>, <span class="ident">BufferAddress</span>, <span class="ident">BufferDescriptor</span>, <span class="ident">BufferSize</span>, <span class="ident">BufferUsages</span>,
|
184 | 194 | <span class="ident">BufferViewMut</span>, <span class="ident">CommandEncoder</span>, <span class="ident">Device</span>, <span class="ident">MapMode</span>,
|
|
269 | 279 | .<span class="ident">position</span>(<span class="op">|</span><span class="ident">chunk</span><span class="op">|</span> <span class="ident">chunk</span>.<span class="ident">offset</span> <span class="op">+</span> <span class="ident">size</span>.<span class="ident">get</span>() <span class="op"><</span><span class="op">=</span> <span class="ident">chunk</span>.<span class="ident">size</span>)
|
270 | 280 | {
|
271 | 281 | <span class="self">self</span>.<span class="ident">active_chunks</span>.<span class="ident">swap_remove</span>(<span class="ident">index</span>)
|
272 |
| - } <span class="kw">else</span> <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">index</span>) <span class="op">=</span> <span class="self">self</span> |
273 |
| - .<span class="ident">free_chunks</span> |
274 |
| - .<span class="ident">iter</span>() |
275 |
| - .<span class="ident">position</span>(<span class="op">|</span><span class="ident">chunk</span><span class="op">|</span> <span class="ident">size</span>.<span class="ident">get</span>() <span class="op"><</span><span class="op">=</span> <span class="ident">chunk</span>.<span class="ident">size</span>) |
276 |
| - { |
277 |
| - <span class="self">self</span>.<span class="ident">free_chunks</span>.<span class="ident">swap_remove</span>(<span class="ident">index</span>) |
278 | 282 | } <span class="kw">else</span> {
|
279 |
| - <span class="kw">let</span> <span class="ident">size</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">chunk_size</span>.<span class="ident">max</span>(<span class="ident">size</span>.<span class="ident">get</span>()); |
280 |
| - <span class="ident">Chunk</span> { |
281 |
| - <span class="ident">buffer</span>: <span class="ident">Arc::new</span>(<span class="ident">device</span>.<span class="ident">create_buffer</span>(<span class="kw-2">&</span><span class="ident">BufferDescriptor</span> { |
282 |
| - <span class="ident">label</span>: <span class="prelude-val">Some</span>(<span class="string">"(wgpu internal) StagingBelt staging buffer"</span>), |
| 283 | + <span class="self">self</span>.<span class="ident">receive_chunks</span>(); <span class="comment">// ensure self.free_chunks is up to date</span> |
| 284 | + |
| 285 | + <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">index</span>) <span class="op">=</span> <span class="self">self</span> |
| 286 | + .<span class="ident">free_chunks</span> |
| 287 | + .<span class="ident">iter</span>() |
| 288 | + .<span class="ident">position</span>(<span class="op">|</span><span class="ident">chunk</span><span class="op">|</span> <span class="ident">size</span>.<span class="ident">get</span>() <span class="op"><</span><span class="op">=</span> <span class="ident">chunk</span>.<span class="ident">size</span>) |
| 289 | + { |
| 290 | + <span class="self">self</span>.<span class="ident">free_chunks</span>.<span class="ident">swap_remove</span>(<span class="ident">index</span>) |
| 291 | + } <span class="kw">else</span> { |
| 292 | + <span class="kw">let</span> <span class="ident">size</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">chunk_size</span>.<span class="ident">max</span>(<span class="ident">size</span>.<span class="ident">get</span>()); |
| 293 | + <span class="ident">Chunk</span> { |
| 294 | + <span class="ident">buffer</span>: <span class="ident">Arc::new</span>(<span class="ident">device</span>.<span class="ident">create_buffer</span>(<span class="kw-2">&</span><span class="ident">BufferDescriptor</span> { |
| 295 | + <span class="ident">label</span>: <span class="prelude-val">Some</span>(<span class="string">"(wgpu internal) StagingBelt staging buffer"</span>), |
| 296 | + <span class="ident">size</span>, |
| 297 | + <span class="ident">usage</span>: <span class="ident">BufferUsages::MAP_WRITE</span> <span class="op">|</span> <span class="ident">BufferUsages::COPY_SRC</span>, |
| 298 | + <span class="ident">mapped_at_creation</span>: <span class="bool-val">true</span>, |
| 299 | + })), |
283 | 300 | <span class="ident">size</span>,
|
284 |
| - <span class="ident">usage</span>: <span class="ident">BufferUsages::MAP_WRITE</span> <span class="op">|</span> <span class="ident">BufferUsages::COPY_SRC</span>, |
285 |
| - <span class="ident">mapped_at_creation</span>: <span class="bool-val">true</span>, |
286 |
| - })), |
287 |
| - <span class="ident">size</span>, |
288 |
| - <span class="ident">offset</span>: <span class="number">0</span>, |
| 301 | + <span class="ident">offset</span>: <span class="number">0</span>, |
| 302 | + } |
289 | 303 | }
|
290 | 304 | };
|
291 | 305 |
|
|
323 | 337 | <span class="doccomment">/// [`StagingBelt::write_buffer()`] are submitted. Additional calls are harmless.</span>
|
324 | 338 | <span class="doccomment">/// Not calling this as soon as possible may result in increased buffer memory usage.</span>
|
325 | 339 | <span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">recall</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) {
|
326 |
| - <span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Ok</span>(<span class="kw-2">mut</span> <span class="ident">chunk</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">receiver</span>.<span class="ident">try_recv</span>() { |
327 |
| - <span class="ident">chunk</span>.<span class="ident">offset</span> <span class="op">=</span> <span class="number">0</span>; |
328 |
| - <span class="self">self</span>.<span class="ident">free_chunks</span>.<span class="ident">push</span>(<span class="ident">chunk</span>); |
329 |
| - } |
| 340 | + <span class="self">self</span>.<span class="ident">receive_chunks</span>(); |
330 | 341 |
|
331 | 342 | <span class="kw">let</span> <span class="ident">sender</span> <span class="op">=</span> <span class="kw-2">&</span><span class="self">self</span>.<span class="ident">sender</span>;
|
332 | 343 | <span class="kw">for</span> <span class="ident">chunk</span> <span class="kw">in</span> <span class="self">self</span>.<span class="ident">closed_chunks</span>.<span class="ident">drain</span>(..) {
|
|
340 | 351 | });
|
341 | 352 | }
|
342 | 353 | }
|
| 354 | + |
| 355 | + <span class="doccomment">/// Move all chunks that the GPU is done with (and are now mapped again)</span> |
| 356 | + <span class="doccomment">/// from `self.receiver` to `self.free_chunks`.</span> |
| 357 | + <span class="kw">fn</span> <span class="ident">receive_chunks</span>(<span class="kw-2">&mut</span> <span class="self">self</span>) { |
| 358 | + <span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Ok</span>(<span class="kw-2">mut</span> <span class="ident">chunk</span>) <span class="op">=</span> <span class="self">self</span>.<span class="ident">receiver</span>.<span class="ident">try_recv</span>() { |
| 359 | + <span class="ident">chunk</span>.<span class="ident">offset</span> <span class="op">=</span> <span class="number">0</span>; |
| 360 | + <span class="self">self</span>.<span class="ident">free_chunks</span>.<span class="ident">push</span>(<span class="ident">chunk</span>); |
| 361 | + } |
| 362 | + } |
343 | 363 | }
|
344 | 364 |
|
345 | 365 | <span class="kw">impl</span> <span class="ident">fmt::Debug</span> <span class="kw">for</span> <span class="ident">StagingBelt</span> {
|
|
0 commit comments