Skip to content

Commit 4dd5964

Browse files
authored
Add basic transfer costing layer_gpu_timeline (#30)
Adds transfer command cost (in pixels or bytes) for all transfers that do not require separate tracking of image or buffer resources.
1 parent 7cc743a commit 4dd5964

File tree

1 file changed

+163
-24
lines changed

1 file changed

+163
-24
lines changed

layer_gpu_timeline/source/layer_device_functions_transfer.cpp

Lines changed: 163 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,19 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdFillBuffer<user_tag>(
9292
std::unique_lock<std::mutex> lock { g_vulkanLock };
9393
auto* layer = Device::retrieve(commandBuffer);
9494

95+
// Compute the size of the transfer
96+
// TODO: Add buffer tracking so we can turn VK_WHOLE_SIZE into bytes
97+
int64_t byteCount = static_cast<int64_t>(size);
98+
if (size == VK_WHOLE_SIZE)
99+
{
100+
byteCount = -2;
101+
}
102+
95103
uint64_t tagID = registerBufferTransfer(
96104
layer,
97105
commandBuffer,
98106
"Fill buffer",
99-
-1);
107+
byteCount);
100108

101109
// Release the lock to call into the driver
102110
lock.unlock();
@@ -121,11 +129,15 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdClearColorImage<user_tag>(
121129
std::unique_lock<std::mutex> lock { g_vulkanLock };
122130
auto* layer = Device::retrieve(commandBuffer);
123131

132+
// Compute the size of the transfer
133+
// TODO: Add image tracking so we can turn image and pRanges into pixels
134+
int64_t pixelCount = -1;
135+
124136
uint64_t tagID = registerImageTransfer(
125137
layer,
126138
commandBuffer,
127139
"Clear image",
128-
-1);
140+
pixelCount);
129141

130142
// Release the lock to call into the driver
131143
lock.unlock();
@@ -150,11 +162,15 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdClearDepthStencilImage<user_tag>(
150162
std::unique_lock<std::mutex> lock { g_vulkanLock };
151163
auto* layer = Device::retrieve(commandBuffer);
152164

165+
// Compute the size of the transfer
166+
// TODO: Add image tracking so we can turn image and pRanges into pixels
167+
int64_t pixelCount = -1;
168+
153169
uint64_t tagID = registerImageTransfer(
154170
layer,
155171
commandBuffer,
156172
"Clear image",
157-
-1);
173+
pixelCount);
158174

159175
// Release the lock to call into the driver
160176
lock.unlock();
@@ -178,11 +194,18 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyBuffer<user_tag>(
178194
std::unique_lock<std::mutex> lock { g_vulkanLock };
179195
auto* layer = Device::retrieve(commandBuffer);
180196

197+
// Compute the size of the transfer
198+
int64_t byteCount = 0;
199+
for (uint32_t i = 0; i < regionCount; i++)
200+
{
201+
byteCount += static_cast<int64_t>(pRegions[i].size);
202+
}
203+
181204
uint64_t tagID = registerBufferTransfer(
182205
layer,
183206
commandBuffer,
184207
"Copy buffer",
185-
-1);
208+
byteCount);
186209

187210
// Release the lock to call into the driver
188211
lock.unlock();
@@ -203,11 +226,18 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyBuffer2<user_tag>(
203226
std::unique_lock<std::mutex> lock { g_vulkanLock };
204227
auto* layer = Device::retrieve(commandBuffer);
205228

229+
// Compute the size of the transfer
230+
int64_t byteCount = 0;
231+
for (uint32_t i = 0; i < pCopyBufferInfo->regionCount; i++)
232+
{
233+
byteCount += static_cast<int64_t>(pCopyBufferInfo->pRegions[i].size);
234+
}
235+
206236
uint64_t tagID = registerBufferTransfer(
207237
layer,
208238
commandBuffer,
209239
"Copy buffer",
210-
-1);
240+
byteCount);
211241

212242
// Release the lock to call into the driver
213243
lock.unlock();
@@ -228,11 +258,18 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyBuffer2KHR<user_tag>(
228258
std::unique_lock<std::mutex> lock { g_vulkanLock };
229259
auto* layer = Device::retrieve(commandBuffer);
230260

261+
// Compute the size of the transfer
262+
int64_t byteCount = 0;
263+
for (uint32_t i = 0; i < pCopyBufferInfo->regionCount; i++)
264+
{
265+
byteCount += static_cast<int64_t>(pCopyBufferInfo->pRegions[i].size);
266+
}
267+
231268
uint64_t tagID = registerBufferTransfer(
232269
layer,
233270
commandBuffer,
234271
"Copy buffer",
235-
-1);
272+
byteCount);
236273

237274
// Release the lock to call into the driver
238275
lock.unlock();
@@ -257,11 +294,21 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyBufferToImage<user_tag>(
257294
std::unique_lock<std::mutex> lock { g_vulkanLock };
258295
auto* layer = Device::retrieve(commandBuffer);
259296

297+
// Compute the size of the transfer
298+
int64_t pixelCount = 0;
299+
for (uint32_t i = 0; i < regionCount; i++)
300+
{
301+
int64_t rPixelCount = static_cast<int64_t>(pRegions[i].imageExtent.width)
302+
* static_cast<int64_t>(pRegions[i].imageExtent.height)
303+
* static_cast<int64_t>(pRegions[i].imageExtent.depth);
304+
pixelCount += rPixelCount;
305+
}
306+
260307
uint64_t tagID = registerImageTransfer(
261308
layer,
262309
commandBuffer,
263-
"Copy image",
264-
-1);
310+
"Copy buffer to image",
311+
pixelCount);
265312

266313
// Release the lock to call into the driver
267314
lock.unlock();
@@ -282,11 +329,21 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyBufferToImage2<user_tag>(
282329
std::unique_lock<std::mutex> lock { g_vulkanLock };
283330
auto* layer = Device::retrieve(commandBuffer);
284331

332+
// Compute the size of the transfer
333+
int64_t pixelCount = 0;
334+
for (uint32_t i = 0; i < pCopyBufferToImageInfo->regionCount; i++)
335+
{
336+
int64_t rPixelCount = static_cast<int64_t>(pCopyBufferToImageInfo->pRegions[i].imageExtent.width)
337+
* static_cast<int64_t>(pCopyBufferToImageInfo->pRegions[i].imageExtent.height)
338+
* static_cast<int64_t>(pCopyBufferToImageInfo->pRegions[i].imageExtent.depth);
339+
pixelCount += rPixelCount;
340+
}
341+
285342
uint64_t tagID = registerImageTransfer(
286343
layer,
287344
commandBuffer,
288-
"Copy image",
289-
-1);
345+
"Copy buffer to image",
346+
pixelCount);
290347

291348
// Release the lock to call into the driver
292349
lock.unlock();
@@ -307,11 +364,21 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyBufferToImage2KHR<user_tag>(
307364
std::unique_lock<std::mutex> lock { g_vulkanLock };
308365
auto* layer = Device::retrieve(commandBuffer);
309366

367+
// Compute the size of the transfer
368+
int64_t pixelCount = 0;
369+
for (uint32_t i = 0; i < pCopyBufferToImageInfo->regionCount; i++)
370+
{
371+
int64_t rPixelCount = static_cast<int64_t>(pCopyBufferToImageInfo->pRegions[i].imageExtent.width)
372+
* static_cast<int64_t>(pCopyBufferToImageInfo->pRegions[i].imageExtent.height)
373+
* static_cast<int64_t>(pCopyBufferToImageInfo->pRegions[i].imageExtent.depth);
374+
pixelCount += rPixelCount;
375+
}
376+
310377
uint64_t tagID = registerImageTransfer(
311378
layer,
312379
commandBuffer,
313-
"Copy image",
314-
-1);
380+
"Copy buffer to image",
381+
pixelCount);
315382

316383
// Release the lock to call into the driver
317384
lock.unlock();
@@ -337,11 +404,21 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyImage<user_tag>(
337404
std::unique_lock<std::mutex> lock { g_vulkanLock };
338405
auto* layer = Device::retrieve(commandBuffer);
339406

407+
// Compute the size of the transfer
408+
int64_t pixelCount = 0;
409+
for (uint32_t i = 0; i < regionCount; i++)
410+
{
411+
int64_t rPixelCount = static_cast<int64_t>(pRegions[i].extent.width)
412+
* static_cast<int64_t>(pRegions[i].extent.height)
413+
* static_cast<int64_t>(pRegions[i].extent.depth);
414+
pixelCount += rPixelCount;
415+
}
416+
340417
uint64_t tagID = registerImageTransfer(
341418
layer,
342419
commandBuffer,
343420
"Copy image",
344-
-1);
421+
pixelCount);
345422

346423
// Release the lock to call into the driver
347424
lock.unlock();
@@ -362,11 +439,21 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyImage2<user_tag>(
362439
std::unique_lock<std::mutex> lock { g_vulkanLock };
363440
auto* layer = Device::retrieve(commandBuffer);
364441

442+
// Compute the size of the transfer
443+
int64_t pixelCount = 0;
444+
for (uint32_t i = 0; i < pCopyImageInfo->regionCount; i++)
445+
{
446+
int64_t rPixelCount = static_cast<int64_t>(pCopyImageInfo->pRegions[i].extent.width)
447+
* static_cast<int64_t>(pCopyImageInfo->pRegions[i].extent.height)
448+
* static_cast<int64_t>(pCopyImageInfo->pRegions[i].extent.depth);
449+
pixelCount += rPixelCount;
450+
}
451+
365452
uint64_t tagID = registerImageTransfer(
366453
layer,
367454
commandBuffer,
368455
"Copy image",
369-
-1);
456+
pixelCount);
370457

371458
// Release the lock to call into the driver
372459
lock.unlock();
@@ -387,11 +474,21 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyImage2KHR<user_tag>(
387474
std::unique_lock<std::mutex> lock { g_vulkanLock };
388475
auto* layer = Device::retrieve(commandBuffer);
389476

477+
// Compute the size of the transfer
478+
int64_t pixelCount = 0;
479+
for (uint32_t i = 0; i < pCopyImageInfo->regionCount; i++)
480+
{
481+
int64_t rPixelCount = static_cast<int64_t>(pCopyImageInfo->pRegions[i].extent.width)
482+
* static_cast<int64_t>(pCopyImageInfo->pRegions[i].extent.height)
483+
* static_cast<int64_t>(pCopyImageInfo->pRegions[i].extent.depth);
484+
pixelCount += rPixelCount;
485+
}
486+
390487
uint64_t tagID = registerImageTransfer(
391488
layer,
392489
commandBuffer,
393490
"Copy image",
394-
-1);
491+
pixelCount);
395492

396493
// Release the lock to call into the driver
397494
lock.unlock();
@@ -416,11 +513,25 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyImageToBuffer<user_tag>(
416513
std::unique_lock<std::mutex> lock { g_vulkanLock };
417514
auto* layer = Device::retrieve(commandBuffer);
418515

419-
uint64_t tagID = registerBufferTransfer(
516+
// Compute the size of the transfer
517+
int64_t pixelCount = 0;
518+
for (uint32_t i = 0; i < regionCount; i++)
519+
{
520+
int64_t rPixelCount = static_cast<int64_t>(pRegions[i].imageExtent.width)
521+
* static_cast<int64_t>(pRegions[i].imageExtent.height)
522+
* static_cast<int64_t>(pRegions[i].imageExtent.depth);
523+
pixelCount += rPixelCount;
524+
}
525+
526+
// TODO: Our usual convention is to mark the transfer using the destination
527+
// type, which means this should be a bufferTransfer reporting size in
528+
// bytes. Without image tracking we only have pixels, so for now we report
529+
// as "Copy image" and report size in pixels.
530+
uint64_t tagID = registerImageTransfer(
420531
layer,
421532
commandBuffer,
422-
"Copy buffer",
423-
-1);
533+
"Copy image to buffer",
534+
pixelCount);
424535

425536
// Release the lock to call into the driver
426537
lock.unlock();
@@ -441,11 +552,25 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyImageToBuffer2<user_tag>(
441552
std::unique_lock<std::mutex> lock { g_vulkanLock };
442553
auto* layer = Device::retrieve(commandBuffer);
443554

444-
uint64_t tagID = registerBufferTransfer(
555+
// Compute the size of the transfer
556+
int64_t pixelCount = 0;
557+
for (uint32_t i = 0; i < pCopyImageToBufferInfo->regionCount; i++)
558+
{
559+
int64_t rPixelCount = static_cast<int64_t>(pCopyImageToBufferInfo->pRegions[i].imageExtent.width)
560+
* static_cast<int64_t>(pCopyImageToBufferInfo->pRegions[i].imageExtent.height)
561+
* static_cast<int64_t>(pCopyImageToBufferInfo->pRegions[i].imageExtent.depth);
562+
pixelCount += rPixelCount;
563+
}
564+
565+
// TODO: Our usual convention is to mark the transfer using the destination
566+
// type, which means this should be a bufferTransfer reporting size in
567+
// bytes. Without image tracking we only have pixels, so for now we report
568+
// as "Copy image" and report size in pixels.
569+
uint64_t tagID = registerImageTransfer(
445570
layer,
446571
commandBuffer,
447-
"Copy buffer",
448-
-1);
572+
"Copy image to buffer",
573+
pixelCount);
449574

450575
// Release the lock to call into the driver
451576
lock.unlock();
@@ -466,11 +591,25 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyImageToBuffer2KHR<user_tag>(
466591
std::unique_lock<std::mutex> lock { g_vulkanLock };
467592
auto* layer = Device::retrieve(commandBuffer);
468593

469-
uint64_t tagID = registerBufferTransfer(
594+
// Compute the size of the transfer
595+
int64_t pixelCount = 0;
596+
for (uint32_t i = 0; i < pCopyImageToBufferInfo->regionCount; i++)
597+
{
598+
int64_t rPixelCount = static_cast<int64_t>(pCopyImageToBufferInfo->pRegions[i].imageExtent.width)
599+
* static_cast<int64_t>(pCopyImageToBufferInfo->pRegions[i].imageExtent.height)
600+
* static_cast<int64_t>(pCopyImageToBufferInfo->pRegions[i].imageExtent.depth);
601+
pixelCount += rPixelCount;
602+
}
603+
604+
// TODO: Our usual convention is to mark the transfer using the destination
605+
// type, which means this should be a bufferTransfer reporting size in
606+
// bytes. Without image tracking we only have pixels, so for now we report
607+
// as "Copy image" and report size in pixels.
608+
uint64_t tagID = registerImageTransfer(
470609
layer,
471610
commandBuffer,
472-
"Copy buffer",
473-
-1);
611+
"Copy image to buffer",
612+
pixelCount);
474613

475614
// Release the lock to call into the driver
476615
lock.unlock();

0 commit comments

Comments
 (0)