Skip to content

Commit 829a39f

Browse files
committed
Add compute
1 parent 47a7180 commit 829a39f

File tree

10 files changed

+246
-39
lines changed

10 files changed

+246
-39
lines changed

layer_gpu_timeline/source/layer_device_functions_command_buffer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ VKAPI_ATTR VkResult layer_vkBeginCommandBuffer<user_tag>(
7979
auto& tracker = layer->getStateTracker();
8080
auto& cmdBuffer = tracker.getCommandBuffer(commandBuffer);
8181
cmdBuffer.reset();
82+
cmdBuffer.begin(pBeginInfo->flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
8283

8384
// Release the lock to call into the driver
8485
lock.unlock();

layer_gpu_timeline/source/layer_device_functions_dispatch.cpp

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@
3232

3333
extern std::mutex g_vulkanLock;
3434

35-
static void registerDispatch(
35+
static uint64_t registerDispatch(
3636
Device* layer,
37-
VkCommandBuffer commandBuffer
37+
VkCommandBuffer commandBuffer,
38+
int64_t groupX,
39+
int64_t groupY,
40+
int64_t groupZ
3841
) {
39-
auto& state = layer->getStateTracker();
40-
auto& stats = state.getCommandBuffer(commandBuffer).getStats();
41-
stats.incDispatchCount();
42+
auto& tracker = layer->getStateTracker();
43+
auto& cb = tracker.getCommandBuffer(commandBuffer);
44+
return cb.dispatch(groupX, groupY, groupZ);
4245
}
4346

4447
/* See Vulkan API for documentation. */
@@ -55,11 +58,27 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdDispatch<user_tag>(
5558
std::unique_lock<std::mutex> lock { g_vulkanLock };
5659
auto* layer = Device::retrieve(commandBuffer);
5760

58-
registerDispatch(layer, commandBuffer);
61+
uint64_t tagID = registerDispatch(
62+
layer,
63+
commandBuffer,
64+
static_cast<int64_t>(groupCountX),
65+
static_cast<int64_t>(groupCountY),
66+
static_cast<int64_t>(groupCountZ));
67+
68+
// Emit the unique workload tag into the command stream
69+
std::string tagLabel = formatString("t%" PRIu64, tagID);
70+
VkDebugUtilsLabelEXT tagInfo {
71+
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
72+
.pNext = nullptr,
73+
.pLabelName = tagLabel.c_str(),
74+
.color = { 0.0f, 0.0f, 0.0f, 0.0f }
75+
};
5976

6077
// Release the lock to call into the driver
6178
lock.unlock();
79+
layer->driver.vkCmdBeginDebugUtilsLabelEXT(commandBuffer, &tagInfo);
6280
layer->driver.vkCmdDispatch(commandBuffer, groupCountX, groupCountY, groupCountZ);
81+
layer->driver.vkCmdEndDebugUtilsLabelEXT(commandBuffer);
6382
}
6483

6584
/* See Vulkan API for documentation. */
@@ -79,11 +98,27 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdDispatchBase<user_tag>(
7998
std::unique_lock<std::mutex> lock { g_vulkanLock };
8099
auto* layer = Device::retrieve(commandBuffer);
81100

82-
registerDispatch(layer, commandBuffer);
101+
uint64_t tagID = registerDispatch(
102+
layer,
103+
commandBuffer,
104+
static_cast<int64_t>(groupCountX),
105+
static_cast<int64_t>(groupCountY),
106+
static_cast<int64_t>(groupCountZ));
107+
108+
// Emit the unique workload tag into the command stream
109+
std::string tagLabel = formatString("t%" PRIu64, tagID);
110+
VkDebugUtilsLabelEXT tagInfo {
111+
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
112+
.pNext = nullptr,
113+
.pLabelName = tagLabel.c_str(),
114+
.color = { 0.0f, 0.0f, 0.0f, 0.0f }
115+
};
83116

84117
// Release the lock to call into the driver
85118
lock.unlock();
119+
layer->driver.vkCmdBeginDebugUtilsLabelEXT(commandBuffer, &tagInfo);
86120
layer->driver.vkCmdDispatchBase(commandBuffer, baseGroupX, baseGroupY, baseGroupZ, groupCountX, groupCountY, groupCountZ);
121+
layer->driver.vkCmdEndDebugUtilsLabelEXT(commandBuffer);
87122
}
88123

89124
/* See Vulkan API for documentation. */
@@ -103,11 +138,27 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdDispatchBaseKHR<user_tag>(
103138
std::unique_lock<std::mutex> lock { g_vulkanLock };
104139
auto* layer = Device::retrieve(commandBuffer);
105140

106-
registerDispatch(layer, commandBuffer);
141+
uint64_t tagID = registerDispatch(
142+
layer,
143+
commandBuffer,
144+
static_cast<int64_t>(groupCountX),
145+
static_cast<int64_t>(groupCountY),
146+
static_cast<int64_t>(groupCountZ));
147+
148+
// Emit the unique workload tag into the command stream
149+
std::string tagLabel = formatString("t%" PRIu64, tagID);
150+
VkDebugUtilsLabelEXT tagInfo {
151+
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
152+
.pNext = nullptr,
153+
.pLabelName = tagLabel.c_str(),
154+
.color = { 0.0f, 0.0f, 0.0f, 0.0f }
155+
};
107156

108157
// Release the lock to call into the driver
109158
lock.unlock();
159+
layer->driver.vkCmdBeginDebugUtilsLabelEXT(commandBuffer, &tagInfo);
110160
layer->driver.vkCmdDispatchBaseKHR(commandBuffer, baseGroupX, baseGroupY, baseGroupZ, groupCountX, groupCountY, groupCountZ);
161+
layer->driver.vkCmdEndDebugUtilsLabelEXT(commandBuffer);
111162
}
112163

113164
/* See Vulkan API for documentation. */
@@ -123,9 +174,20 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdDispatchIndirect<user_tag>(
123174
std::unique_lock<std::mutex> lock { g_vulkanLock };
124175
auto* layer = Device::retrieve(commandBuffer);
125176

126-
registerDispatch(layer, commandBuffer);
177+
uint64_t tagID = registerDispatch(layer, commandBuffer, -1, -1, -1);
178+
179+
// Emit the unique workload tag into the command stream
180+
std::string tagLabel = formatString("t%" PRIu64, tagID);
181+
VkDebugUtilsLabelEXT tagInfo {
182+
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
183+
.pNext = nullptr,
184+
.pLabelName = tagLabel.c_str(),
185+
.color = { 0.0f, 0.0f, 0.0f, 0.0f }
186+
};
127187

128188
// Release the lock to call into the driver
129189
lock.unlock();
190+
layer->driver.vkCmdBeginDebugUtilsLabelEXT(commandBuffer, &tagInfo);
130191
layer->driver.vkCmdDispatchIndirect(commandBuffer, buffer, offset);
192+
layer->driver.vkCmdEndDebugUtilsLabelEXT(commandBuffer);
131193
}

layer_gpu_timeline/source/layer_device_functions_render_pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdBeginRenderPass<user_tag>(
168168

169169
// Emit the unique workload tag into the command stream
170170
std::string tagLabel = formatString("t%" PRIu64, tagID);
171-
[[maybe_unused]] VkDebugUtilsLabelEXT tagInfo {
171+
VkDebugUtilsLabelEXT tagInfo {
172172
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT,
173173
.pNext = nullptr,
174174
.pLabelName = tagLabel.c_str(),

lglpy/service_gpu_timeline.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ class GPUTimelineService:
3333
def __init__(self):
3434
self.frame = {
3535
"frame": 0,
36-
"workloads": [
37-
38-
]
36+
"workloads": []
3937
}
4038

4139
# TODO: Make file name configurable
@@ -45,8 +43,6 @@ def get_service_name(self) -> str:
4543
return 'GPUTimeline'
4644

4745
def handle_frame(self, msg):
48-
print(json.dumps(self.frame, indent=4))
49-
5046
# Write frame packet to the file
5147
lastFrame = json.dumps(self.frame).encode('utf-8')
5248
length = struct.pack('<I', len(lastFrame))
@@ -55,11 +51,15 @@ def handle_frame(self, msg):
5551
self.fileHandle.write(lastFrame)
5652

5753
# Reset the local frame state for the next frame
54+
nextFrame = msg['fid']
5855
self.frame = {
59-
'frame': msg['fid'],
56+
'frame': nextFrame,
6057
'workloads': []
6158
}
6259

60+
if nextFrame % 100 == 0:
61+
print(f'Starting frame {nextFrame} ...')
62+
6363
def handle_renderpass(self, msg):
6464
# Find the last workload
6565
lastRenderPass = None
@@ -70,11 +70,17 @@ def handle_renderpass(self, msg):
7070

7171
# Continuation
7272
if lastRenderPass and lastRenderPass['tid'] == msg['tid']:
73-
lastRenderPass['drawCallCount'] += msg['drawCallCount']
73+
# Don't accumulate if tagID is not unique metadata tag
74+
if lastRenderPass['drawCallCount'] != -1:
75+
lastRenderPass['drawCallCount'] += msg['drawCallCount']
7476
# New render pass
7577
else:
7678
self.frame['workloads'].append(msg)
7779

80+
def handle_dispatch(self, msg):
81+
# Find the last workload
82+
self.frame['workloads'].append(msg)
83+
7884
def handle_message(self, message: Message):
7985
payload = message.payload.decode('utf-8')
8086
parsedPayload = json.loads(payload)
@@ -87,6 +93,9 @@ def handle_message(self, message: Message):
8793
elif payloadType == 'renderpass':
8894
self.handle_renderpass(parsedPayload)
8995

96+
elif payloadType == 'dispatch':
97+
self.handle_dispatch(parsedPayload)
98+
9099
else:
91100
assert False, f'Unknown payload type {payloadType}'
92101

source_common/trackers/command_buffer.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ CommandBuffer::CommandBuffer(
4343
/* See header for documentation. */
4444
void CommandBuffer::reset()
4545
{
46+
oneTimeSubmit = false;
4647
stats.reset();
4748
workloads.clear();
4849
workloadCommandStream.clear();
4950
}
5051

51-
/**
52-
* @brief Begin a user debug marker range.
53-
*/
52+
/* See header for documentation. */
53+
void CommandBuffer::begin(
54+
bool _oneTimeSubmit
55+
)
56+
{
57+
oneTimeSubmit = _oneTimeSubmit;
58+
}
59+
60+
/* See header for documentation. */
5461
void CommandBuffer::debugMarkerBegin(
5562
std::string marker
5663
) {
@@ -63,9 +70,7 @@ void CommandBuffer::debugMarkerBegin(
6370
workloadCommandStream.push_back(instr);
6471
}
6572

66-
/**
67-
* @brief End a user debug marker range.
68-
*/
73+
/* See header for documentation. */
6974
void CommandBuffer::debugMarkerEnd()
7075
{
7176
// Add command with empty workload to update queue debug stack on submit
@@ -74,9 +79,7 @@ void CommandBuffer::debugMarkerEnd()
7479
workloadCommandStream.push_back(instr);
7580
}
7681

77-
/**
78-
* @brief End a user render pass.
79-
*/
82+
/* See header for documentation. */
8083
uint64_t CommandBuffer::renderPassBegin(
8184
const RenderPass& renderPass,
8285
uint32_t width,
@@ -99,7 +102,7 @@ uint64_t CommandBuffer::renderPassBegin(
99102
renderPassStartDrawCount = stats.getDrawCallCount();
100103

101104
auto workload = std::make_shared<LCSRenderPass>(
102-
tagID, renderPass, width, height, suspending);
105+
tagID, renderPass, width, height, suspending, oneTimeSubmit);
103106

104107
currentRenderPass = workload;
105108
workloads.push_back(workload);
@@ -111,9 +114,7 @@ uint64_t CommandBuffer::renderPassBegin(
111114
return tagID;
112115
}
113116

114-
/**
115-
* @brief End a user render pass.
116-
*/
117+
/* See header for documentation. */
117118
bool CommandBuffer::renderPassEnd()
118119
{
119120
assert(currentRenderPass);
@@ -130,6 +131,28 @@ bool CommandBuffer::renderPassEnd()
130131
return suspending;
131132
}
132133

134+
/* See header for documentation. */
135+
uint64_t CommandBuffer::dispatch(
136+
int64_t xGroups,
137+
int64_t yGroups,
138+
int64_t zGroups
139+
) {
140+
LAYER_LOG("Creating LCSDispatch workload");
141+
uint64_t tagID = Tracker::LCSWorkload::assignTagID();
142+
stats.incDispatchCount();
143+
144+
// Add a workload to the render pass
145+
auto workload = std::make_shared<LCSDispatch>(
146+
tagID, xGroups, yGroups, zGroups);
147+
workloads.push_back(workload);
148+
149+
// Add a command to the layer-side command stream
150+
auto instr = std::make_pair(LCSOpcode::DISPATCH, workload);
151+
workloadCommandStream.push_back(instr);
152+
153+
return tagID;
154+
}
155+
133156
/* See header for documentation. */
134157
void CommandBuffer::executeCommands(
135158
CommandBuffer& secondary
@@ -142,7 +165,7 @@ void CommandBuffer::executeCommands(
142165
vecAppend(workloadCommandStream, secondary.workloadCommandStream);
143166
}
144167

145-
168+
/* See header for documentation. */
146169
CommandPool::CommandPool(
147170
VkCommandPool _handle) :
148171
handle(_handle)

source_common/trackers/command_buffer.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ class CommandBuffer
114114
*/
115115
bool renderPassEnd();
116116

117+
/**
118+
* @brief Capture a compute dispatch.
119+
*
120+
* @param xGroups Number of groups in X dimension, or -1 if unknown.
121+
* @param yGroups Number of groups in Y dimension, or -1 if unknown.
122+
* @param zGroups Number of groups in Z dimension, or -1 if unknown.
123+
*
124+
* @return Returns the tagID assigned to this workload.
125+
*/
126+
uint64_t dispatch(
127+
int64_t xGroups,
128+
int64_t yGroups,
129+
int64_t zGroups);
130+
117131
/**
118132
* @brief Begin a user debug marker range.
119133
*/
@@ -136,12 +150,25 @@ class CommandBuffer
136150
*/
137151
void reset();
138152

153+
/**
154+
* @brief Begin recording back into the @a Recording state.
155+
*
156+
* @param oneTimeSubmit Is this a one-time submit recording.
157+
*/
158+
void begin(
159+
bool oneTimeSubmit);
160+
139161
private:
140162
/**
141163
* @brief The Vulkan API handle of this command buffer.
142164
*/
143165
const VkCommandBuffer handle;
144166

167+
/**
168+
* @brief Is this command buffer recording one-time-submit?
169+
*/
170+
bool oneTimeSubmit { false };
171+
145172
/**
146173
* @brief The command buffer draw count at the start of the render pass.
147174
*/

0 commit comments

Comments
 (0)