@@ -214,6 +214,11 @@ void CommandEncoder::write_buffer(const std::shared_ptr<Buffer> &buffer,
214214 auto device = device_.lock ();
215215 auto allocation = device->allocate_staging (data_size);
216216
217+ if (!used_staging_buffer_) {
218+ used_staging_buffer_ = true ;
219+ device->increment_staging_encoder ();
220+ }
221+
217222 void *mapped_ptr = device->map_staging (allocation);
218223 memcpy (mapped_ptr, data, data_size);
219224 device->unmap_staging (allocation);
@@ -250,8 +255,13 @@ void CommandEncoder::read_buffer(const std::shared_ptr<Buffer> &buffer,
250255 auto device = device_.lock ();
251256 auto allocation = device->allocate_staging (data_size);
252257
258+ if (!used_staging_buffer_) {
259+ used_staging_buffer_ = true ;
260+ device->increment_staging_encoder ();
261+ }
262+
253263 Command cmd{};
254- cmd.type = CommandType::ReadBuffer ;
264+ cmd.type = CommandType::WriteBuffer ;
255265
256266 auto &args = cmd.args .read_buffer ;
257267 args.buffer = buffer.get ();
@@ -288,6 +298,11 @@ void CommandEncoder::write_texture(const std::shared_ptr<Texture> &texture, Rect
288298 auto device = device_.lock ();
289299 auto allocation = device->allocate_staging (data_size);
290300
301+ if (!used_staging_buffer_) {
302+ used_staging_buffer_ = true ;
303+ device->increment_staging_encoder ();
304+ }
305+
291306 void *mapped_ptr = device->map_staging (allocation);
292307 memcpy (mapped_ptr, src, data_size);
293308 device->unmap_staging (allocation);
@@ -325,6 +340,11 @@ void CommandEncoder::read_texture(const std::shared_ptr<Texture> &texture, RectI
325340 auto device = device_.lock ();
326341 auto allocation = device->allocate_staging (data_size);
327342
343+ if (!used_staging_buffer_) {
344+ used_staging_buffer_ = true ;
345+ device->increment_staging_encoder ();
346+ }
347+
328348 Command cmd{};
329349 cmd.type = CommandType::ReadTexture;
330350
@@ -349,4 +369,19 @@ void CommandEncoder::read_texture(const std::shared_ptr<Texture> &texture, RectI
349369 track_temporary_resource (allocation.buffer );
350370}
351371
372+ void CommandEncoder::invoke_callbacks () {
373+ for (auto &callback : callbacks_) {
374+ callback ();
375+ }
376+
377+ callbacks_.clear ();
378+ temp_buffers_.clear ();
379+
380+ if (used_staging_buffer_) {
381+ if (auto device = device_.lock ()) {
382+ device_.lock ()->decrement_staging_encoder ();
383+ }
384+ }
385+ }
386+
352387} // namespace Pathfinder
0 commit comments