Skip to content

Commit 414d720

Browse files
committed
Compute command buffer fixes
1 parent 330e858 commit 414d720

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

compositor.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ bool Compositor::Init(VkDevice& device,
151151

152152
computer->Init(device);
153153

154-
computer->SetupQueue(device);
154+
computer->SetupQueue(device, queueFamilyId);
155155

156-
//computeCommandBuffer = computer->SetupCommandBuffer(device, graphicsQueueIndex, computeQueueIndex);
156+
computeCommandBuffer = computer->SetupCommandBuffer(device, queueFamilyId);
157157

158158
return true;
159159
}
@@ -184,9 +184,17 @@ bool Compositor::Destroy(VkDevice& device)
184184

185185
bool Compositor::Draw(VkDevice& device)
186186
{
187+
VkSubmitInfo submitInfo = {};
188+
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
189+
submitInfo.commandBufferCount = 1;
190+
submitInfo.pCommandBuffers = computeCommandBuffer;
191+
192+
vkQueueSubmit(computeQueue, 1, &submitInfo, VK_NULL_HANDLE);
193+
vkQueueWaitIdle(computeQueue);
194+
187195
transferCommandBuffer = &graphicsEngine->TransferDynamicBuffers(device);
188196

189-
VkSubmitInfo submitInfo = {};
197+
submitInfo = {};
190198
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
191199
submitInfo.commandBufferCount = 1;
192200
submitInfo.pCommandBuffers = transferCommandBuffer;

compute.cpp

+27-22
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void Compute::Init(VkDevice& device)
3434

3535
//SetupImage(device, image, extent, format, imageMemory, properties, usage);
3636

37-
VkDeviceSize size = sizeof(float);
37+
VkDeviceSize size = sizeof(float)*2;
3838

3939
properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
4040
usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
@@ -68,17 +68,23 @@ void Compute::Destroy(VkDevice& device)
6868
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
6969
vkDestroyDescriptorPool(device, descriptorPool, nullptr);
7070

71-
//vkDestroyPipeline(device, pipeline, nullptr);
71+
vkDestroyPipeline(device, pipeline, nullptr);
7272
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
7373
vkDestroyShaderModule(device, shaderModule, nullptr);
74+
75+
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
76+
77+
vkDestroyCommandPool(device, commandPool, nullptr);
78+
79+
vkDestroyFence(device, fence, nullptr);
7480
}
7581

7682
void Compute::SetupBuffers(VkDevice& device)
7783
{
7884

7985
}
8086

81-
void Compute::SetupQueue(VkDevice& device)
87+
void Compute::SetupQueue(VkDevice& device, uint32_t queueFamilyId)
8288
{
8389
/*VkDeviceQueueCreateInfo queueCreateInfo = {};
8490
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
@@ -244,7 +250,7 @@ void Compute::SetupQueue(VkDevice& device)
244250
std::cout << "Pipeline layout creation failed" << std::cout;
245251
}
246252

247-
/*VkComputePipelineCreateInfo pipelineCreateInfo = {};
253+
VkComputePipelineCreateInfo pipelineCreateInfo = {};
248254
pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
249255
pipelineCreateInfo.stage = shaderStageCreateInfo;
250256
pipelineCreateInfo.layout = pipelineLayout;
@@ -253,7 +259,7 @@ void Compute::SetupQueue(VkDevice& device)
253259

254260
VkCommandPoolCreateInfo cmdPoolInfo = {};
255261
cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
256-
cmdPoolInfo.queueFamilyIndex = computeQueueFamilyId;
262+
cmdPoolInfo.queueFamilyIndex = queueFamilyId;
257263
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
258264
result = vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &commandPool);
259265

@@ -265,15 +271,13 @@ void Compute::SetupQueue(VkDevice& device)
265271

266272
vkAllocateCommandBuffers(device, &cmdBufAllocInfo, &commandBuffer);
267273

268-
VkFence fence;
269-
270-
VkFenceCreateInfo fenceCreateInfo;
274+
VkFenceCreateInfo fenceCreateInfo = {};
271275
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
272276
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
273-
vkCreateFence(device, &fenceCreateInfo, nullptr, &fence);*/
277+
vkCreateFence(device, &fenceCreateInfo, nullptr, &fence);
274278
}
275279

276-
VkCommandBuffer* Compute::SetupCommandBuffer(VkDevice& device, uint32_t graphicsQueueFamilyId, uint32_t computeQueueFamilyId)
280+
VkCommandBuffer* Compute::SetupCommandBuffer(VkDevice& device, uint32_t queueFamilyId)
277281
{
278282
VkCommandBufferBeginInfo beginInfo = {};
279283
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@@ -286,15 +290,15 @@ VkCommandBuffer* Compute::SetupCommandBuffer(VkDevice& device, uint32_t graphics
286290
throw std::runtime_error("Compute command buffer beign failed");
287291
}
288292

289-
/*memoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
290-
memoryBarrier.buffer = storageBuffer;
291-
memoryBarrier.size = storageBufferSize;
292-
memoryBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
293-
memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
294-
memoryBarrier.srcQueueFamilyIndex = graphicsQueueFamilyId;
295-
memoryBarrier.dstQueueFamilyIndex = computeQueueFamilyId;
293+
//memoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
294+
//memoryBarrier.buffer = storageBuffer;
295+
//memoryBarrier.size = storageBufferSize;
296+
//memoryBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
297+
//memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
298+
//memoryBarrier.srcQueueFamilyIndex = graphicsQueueFamilyId;
299+
//memoryBarrier.dstQueueFamilyIndex = computeQueueFamilyId;
296300

297-
vkCmdPipelineBarrier(commandBuffer,
301+
/*vkCmdPipelineBarrier(commandBuffer,
298302
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
299303
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
300304
0,
@@ -306,20 +310,21 @@ VkCommandBuffer* Compute::SetupCommandBuffer(VkDevice& device, uint32_t graphics
306310
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0, 1, &descriptorSet, 0, 0);
307311
vkCmdDispatch(commandBuffer, extent.width, extent.height, 1);
308312

309-
/*memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
313+
memoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
314+
memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
310315
memoryBarrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
311316
memoryBarrier.buffer = storageBuffer;
312317
memoryBarrier.size = storageBufferSize;
313-
memoryBarrier.srcQueueFamilyIndex = computeQueueFamilyId;
314-
memoryBarrier.dstQueueFamilyIndex = graphicsQueueFamilyId;
318+
memoryBarrier.srcQueueFamilyIndex = queueFamilyId;
319+
memoryBarrier.dstQueueFamilyIndex = queueFamilyId;
315320

316321
vkCmdPipelineBarrier(commandBuffer,
317322
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
318323
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
319324
0,
320325
0, nullptr,
321326
1, &memoryBarrier,
322-
0, nullptr);*/
327+
0, nullptr);
323328

324329
vkEndCommandBuffer(commandBuffer);
325330

compute.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ class Compute : Commands
1515

1616
void Init(VkDevice& device);
1717
void Destroy(VkDevice& device);
18-
void SetupQueue(VkDevice& device);
19-
VkCommandBuffer* SetupCommandBuffer(VkDevice& device, uint32_t graphicsQueueFamilyId, uint32_t computeQueueFamilyId);
18+
void SetupQueue(VkDevice& device, uint32_t queueFamilyId);
19+
VkCommandBuffer* SetupCommandBuffer(VkDevice& device, uint32_t queueFamilyId);
2020

2121
private:
2222
//uint32_t GetMemoryTypeIndex(VkDevice& device, VkImage& image, VkPhysicalDeviceMemoryProperties& props, VkMemoryPropertyFlags propFlags, uint32_t& allocSize);
2323

2424
void SetupBuffers(VkDevice& device);
25-
void SetupCommandBuffer();
25+
//void SetupCommandBuffer();
2626
//std::function<uint32_t(VkDevice& device, VkBuffer& buffer, VkPhysicalDeviceMemoryProperties& props, VkMemoryPropertyFlags properties, uint32_t& allocSize)> GetMemoryTypeIndexCallback;
2727

2828
VkShaderModule shaderModule;
2929
VkPipeline pipeline;
3030
VkPipelineLayout pipelineLayout;
3131

3232
VkCommandBuffer commandBuffer;
33-
VkBufferMemoryBarrier memoryBarrier;
33+
VkBufferMemoryBarrier memoryBarrier = {};
3434
//VkDeviceMemory memory;
3535

3636
//VkImage image;
@@ -55,6 +55,8 @@ class Compute : Commands
5555

5656
uint32_t uniformBufferSize;
5757
uint32_t storageBufferSize;
58+
59+
VkFence fence;
5860
};
5961

6062
#endif

shader.comp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
#extension GL_ARB_shading_language_420pack : enable
55

66
// Binding 0 : Position storage buffer
7-
layout(std140, binding = 0) buffer Height
7+
layout(std140, binding = 1) buffer Height
88
{
99
float height[];
1010
};
1111

1212
layout (local_size_x = 16, local_size_y = 16) in;
1313

14-
layout (binding = 1) uniform UBO
14+
layout (binding = 0) uniform UBO
1515
{
1616
float time;
1717
float dx;

0 commit comments

Comments
 (0)