Skip to content

Commit 0054ad0

Browse files
committed
Passed in compute buffer to vexter shader
Still a bug in compute shader layout
1 parent 414d720 commit 0054ad0

11 files changed

+155
-91
lines changed

commands.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
116
#include "commands.h"
217

318
#include "types.h"

commands.h

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
116
#ifndef commands_h
217
#define commands_h
318

compositor.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,15 @@ bool Compositor::Init(VkDevice& device,
127127

128128
VkCommandBuffer& dynamicTransferCommandBuffer = graphicsEngine->TransferDynamicBuffers(device);
129129

130-
graphicsEngine->ConstructFrames();
130+
computer = new Compute(grid, memProperties);
131+
132+
computer->Init(device);
133+
134+
computer->SetupQueue(device, queueFamilyId);
135+
136+
computeCommandBuffer = computer->SetupCommandBuffer(device, queueFamilyId);
137+
138+
graphicsEngine->ConstructFrames(computer->GetStorageBuffer());
131139

132140
VkCommandBuffer transferCommandBuffers[] = { staticTransferCommandBuffer, dynamicTransferCommandBuffer };
133141

@@ -147,14 +155,6 @@ bool Compositor::Init(VkDevice& device,
147155
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &waitSemaphore);
148156
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &signalSemaphore);
149157

150-
computer = new Compute(grid, memProperties);
151-
152-
computer->Init(device);
153-
154-
computer->SetupQueue(device, queueFamilyId);
155-
156-
computeCommandBuffer = computer->SetupCommandBuffer(device, queueFamilyId);
157-
158158
return true;
159159
}
160160

@@ -182,6 +182,8 @@ bool Compositor::Destroy(VkDevice& device)
182182
return true;
183183
}
184184

185+
bool once = true;
186+
185187
bool Compositor::Draw(VkDevice& device)
186188
{
187189
VkSubmitInfo submitInfo = {};
@@ -192,6 +194,12 @@ bool Compositor::Draw(VkDevice& device)
192194
vkQueueSubmit(computeQueue, 1, &submitInfo, VK_NULL_HANDLE);
193195
vkQueueWaitIdle(computeQueue);
194196

197+
if(once)
198+
{
199+
computer->PrintResults(device);
200+
once = false;
201+
}
202+
195203
transferCommandBuffer = &graphicsEngine->TransferDynamicBuffers(device);
196204

197205
submitInfo = {};

compute.cpp

+41-66
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
116
#include "compute.h"
217

318
#include <stdexcept>
@@ -25,7 +40,7 @@ void Compute::Init(VkDevice& device)
2540
{
2641
//size = sizeof(float)*extent.width*extent.height;
2742

28-
VkFormat format = VK_FORMAT_R32_SFLOAT;
43+
//VkFormat format = VK_FORMAT_R32_SFLOAT;
2944

3045
//assert(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT);
3146

@@ -43,7 +58,8 @@ void Compute::Init(VkDevice& device)
4358

4459
size = extent.width*extent.height*sizeof(float);
4560

46-
properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
61+
properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
62+
//properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
4763
usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
4864

4965
SetupBuffer(device, storageBuffer, storageBufferMemory, size, properties, usage);
@@ -79,23 +95,13 @@ void Compute::Destroy(VkDevice& device)
7995
vkDestroyFence(device, fence, nullptr);
8096
}
8197

82-
void Compute::SetupBuffers(VkDevice& device)
98+
/*void Compute::SetupBuffers(VkDevice& device)
8399
{
84100
85-
}
101+
}*/
86102

87103
void Compute::SetupQueue(VkDevice& device, uint32_t queueFamilyId)
88-
{
89-
/*VkDeviceQueueCreateInfo queueCreateInfo = {};
90-
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
91-
queueCreateInfo.pNext = NULL;
92-
queueCreateInfo.queueFamilyIndex = queueIndex;
93-
queueCreateInfo.queueCount = 1;
94-
95-
vkGetDeviceQueue(device, queueFamilyId, 0, &queue);*/
96-
97-
//uint32_t computeQueueFamilyId = 0;
98-
104+
{
99105
uint32_t uniformIndex = 0;
100106
uint32_t storageIndex = 1;
101107

@@ -125,29 +131,6 @@ void Compute::SetupQueue(VkDevice& device, uint32_t queueFamilyId)
125131
throw std::runtime_error("Descriptor set layout creation failed");
126132
}
127133

128-
/*VkDescriptorSetLayoutCreateInfo layoutCreateInfo = {};
129-
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
130-
layoutCreateInfo.bindingCount = 1;
131-
layoutCreateInfo.pBindings = &layoutBindings[uniformIndex];
132-
133-
VkResult result = vkCreateDescriptorSetLayout(device, &layoutCreateInfo, nullptr, &descriptorSetLayouts[uniformIndex]);
134-
135-
if (result != VK_SUCCESS)
136-
{
137-
throw std::runtime_error("Descriptor set layout creation failed");
138-
}
139-
140-
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
141-
layoutCreateInfo.bindingCount = 1;
142-
layoutCreateInfo.pBindings = &layoutBindings[storageIndex];
143-
144-
result = vkCreateDescriptorSetLayout(device, &layoutCreateInfo, nullptr, &descriptorSetLayouts[storageIndex]);
145-
146-
if (result != VK_SUCCESS)
147-
{
148-
throw std::runtime_error("Descriptor set layout creation failed");
149-
}*/
150-
151134
VkDescriptorPoolSize poolSizes[2];
152135
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
153136
poolSizes[0].descriptorCount = 1;
@@ -290,21 +273,21 @@ VkCommandBuffer* Compute::SetupCommandBuffer(VkDevice& device, uint32_t queueFam
290273
throw std::runtime_error("Compute command buffer beign failed");
291274
}
292275

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;
276+
memoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
277+
memoryBarrier.buffer = storageBuffer;
278+
memoryBarrier.size = storageBufferSize;
279+
memoryBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
280+
memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
281+
memoryBarrier.srcQueueFamilyIndex = queueFamilyId;
282+
memoryBarrier.dstQueueFamilyIndex = queueFamilyId;
300283

301-
/*vkCmdPipelineBarrier(commandBuffer,
284+
vkCmdPipelineBarrier(commandBuffer,
302285
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
303286
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
304287
0,
305288
0, nullptr,
306289
1, &memoryBarrier,
307-
0, nullptr);*/
290+
0, nullptr);
308291

309292
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
310293
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0, 1, &descriptorSet, 0, 0);
@@ -331,29 +314,21 @@ VkCommandBuffer* Compute::SetupCommandBuffer(VkDevice& device, uint32_t queueFam
331314
return &commandBuffer;
332315
}
333316

334-
/*uint32_t Compute::GetMemoryTypeIndex(VkDevice& device, VkBuffer& buffer, VkPhysicalDeviceMemoryProperties& props, VkMemoryPropertyFlags propFlags, uint32_t& allocSize)
317+
void Compute::PrintResults(VkDevice& device)
335318
{
336-
VkMemoryRequirements memRequirements;
319+
void* data;
320+
uint32_t size = extent.width*extent.height*sizeof(float);
321+
vkMapMemory(device, storageBufferMemory, 0, size, 0, &data);
337322

338-
vkGetImageMemoryRequirements(device, buffer, &memRequirements);
339-
340-
uint32_t memTypeIndex = InvalidIndex;
341-
342-
for (uint32_t i = 0; i < props.memoryTypeCount; ++i)
323+
for(uint32_t i = 0; i < extent.width; ++i)
343324
{
344-
if ((memRequirements.memoryTypeBits & (1 << i)) &&
345-
(props.memoryTypes[i].propertyFlags & propFlags) == propFlags)
325+
for(uint32_t j = 0; j < extent.height; ++j)
346326
{
347-
memTypeIndex = i;
327+
std::cout << static_cast<float*>(data)[i*j] << ", ";
348328
}
329+
330+
std::cout << std::endl;
349331
}
350332

351-
if (memTypeIndex == InvalidIndex)
352-
{
353-
throw std::runtime_error("Memory property combination not supported");
354-
}
355-
356-
allocSize = memRequirements.size;
357-
358-
return memTypeIndex;
359-
}*/
333+
vkUnmapMemory(device, storageBufferMemory);
334+
}

compute.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
116
#ifndef compute_h
217
#define compute_h
318

@@ -18,10 +33,14 @@ class Compute : Commands
1833
void SetupQueue(VkDevice& device, uint32_t queueFamilyId);
1934
VkCommandBuffer* SetupCommandBuffer(VkDevice& device, uint32_t queueFamilyId);
2035

36+
VkBuffer& GetStorageBuffer() { return storageBuffer; }
37+
38+
void PrintResults(VkDevice& device);
39+
2140
private:
2241
//uint32_t GetMemoryTypeIndex(VkDevice& device, VkImage& image, VkPhysicalDeviceMemoryProperties& props, VkMemoryPropertyFlags propFlags, uint32_t& allocSize);
2342

24-
void SetupBuffers(VkDevice& device);
43+
//void SetupBuffers(VkDevice& device);
2544
//void SetupCommandBuffer();
2645
//std::function<uint32_t(VkDevice& device, VkBuffer& buffer, VkPhysicalDeviceMemoryProperties& props, VkMemoryPropertyFlags properties, uint32_t& allocSize)> GetMemoryTypeIndexCallback;
2746

makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ system.o: system.h system.cpp
3333
commands.o: commands.h commands.cpp
3434
g++ $(CFLAGS) $(DEFINES) $(INCLUDE) -c commands.cpp -o $@
3535

36-
renderer.o: renderer.h renderer.cpp commands.o
36+
renderer.o: renderer.h renderer.cpp commands.h
3737
g++ $(CFLAGS) $(DEFINES) $(INCLUDE) -c renderer.cpp -o $@
3838

39-
compute.o: compute.h compute.cpp
39+
compute.o: compute.h compute.cpp commands.h
4040
g++ $(CFLAGS) $(DEFINES) $(INCLUDE) -c compute.cpp -o $@
4141

4242
controller.o: controller.h controller.cpp
4343
g++ $(CFLAGS) $(DEFINES) $(INCLUDE) -c controller.cpp -o $@
4444

45-
compositor.o: compositor.h compositor.cpp
45+
compositor.o: compositor.h compositor.cpp renderer.h compute.h
4646
g++ $(CFLAGS) $(DEFINES) $(INCLUDE) -c compositor.cpp -o $@
4747

4848
test: vulkan

renderer.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Renderer::Renderer(VkExtent2D& extent, const VkExtent3D& gridDim, VkPhysicalDevi
5353
framebuffers = static_cast<VkFramebuffer*>(malloc(sizeof(VkFramebuffer)*numFBOs));
5454
drawCommandBuffers = static_cast<VkCommandBuffer*>(malloc(sizeof(VkCommandBuffer)*numDrawCmdBuffers));
5555
attributeDescriptions = static_cast<VkVertexInputAttributeDescription*>(malloc(sizeof(VkVertexInputAttributeDescription)*numAttrDesc));
56+
bindingDescriptions = static_cast<VkVertexInputBindingDescription*>(malloc(sizeof(VkVertexInputBindingDescription)*numBindDesc));
5657
}
5758

5859
Renderer::~Renderer()
@@ -62,6 +63,7 @@ Renderer::~Renderer()
6263
free(framebuffers);
6364
free(drawCommandBuffers);
6465
free(attributeDescriptions);
66+
free(bindingDescriptions);
6567
}
6668

6769
bool Renderer::Init(VkDevice& device, const VkFormat& surfaceFormat, const VkImageView* imageViews, uint32_t queueFamilyId)
@@ -183,9 +185,9 @@ bool Renderer::Init(VkDevice& device, const VkFormat& surfaceFormat, const VkIma
183185

184186
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
185187
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
186-
vertexInputInfo.vertexBindingDescriptionCount = 1;
188+
vertexInputInfo.vertexBindingDescriptionCount = 2;
187189
vertexInputInfo.vertexAttributeDescriptionCount = numAttrDesc;
188-
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
190+
vertexInputInfo.pVertexBindingDescriptions = bindingDescriptions;
189191
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions;
190192

191193
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
@@ -447,7 +449,7 @@ bool Renderer::Destroy(VkDevice& device)
447449
return true;
448450
}
449451

450-
void Renderer::ConstructFrames()
452+
void Renderer::ConstructFrames(VkBuffer& heightBuffer)
451453
{
452454
bool result = false;
453455

@@ -469,6 +471,8 @@ void Renderer::ConstructFrames()
469471

470472
VkDeviceSize offsets[] = {0};
471473

474+
VkBuffer buffers[] = { vertexBuffer, heightBuffer };
475+
472476
assert(numDrawCmdBuffers == numFBOs);
473477

474478
for (uint32_t i = 0; i < numDrawCmdBuffers; ++i)
@@ -479,7 +483,7 @@ void Renderer::ConstructFrames()
479483
vkCmdBeginRenderPass(drawCommandBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
480484
vkCmdBindDescriptorSets(drawCommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
481485
vkCmdBindPipeline(drawCommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
482-
vkCmdBindVertexBuffers(drawCommandBuffers[i], 0, 1, &vertexBuffer, offsets);
486+
vkCmdBindVertexBuffers(drawCommandBuffers[i], 0, 2, buffers, offsets);
483487
vkCmdBindIndexBuffer(drawCommandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16);
484488
vkCmdDrawIndexed(drawCommandBuffers[i], numIndices, 1, 0, 0, 0);
485489
vkCmdEndRenderPass(drawCommandBuffers[i]);
@@ -495,9 +499,13 @@ void Renderer::ConstructFrames()
495499

496500
bool Renderer::SetupShaderParameters(VkDevice& device)
497501
{
498-
bindingDescription.binding = 0;
499-
bindingDescription.stride = sizeof(float[3]) * 2;
500-
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
502+
bindingDescriptions[0].binding = 0;
503+
bindingDescriptions[0].stride = sizeof(float[3]) * 2;
504+
bindingDescriptions[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
505+
506+
bindingDescriptions[1].binding = 1;
507+
bindingDescriptions[1].stride = sizeof(float);
508+
bindingDescriptions[1].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
501509

502510
attributeDescriptions[0].binding = 0;
503511
attributeDescriptions[0].location = 0;
@@ -508,6 +516,11 @@ bool Renderer::SetupShaderParameters(VkDevice& device)
508516
attributeDescriptions[1].location = 1;
509517
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
510518
attributeDescriptions[1].offset = sizeof(float[3]);
519+
520+
attributeDescriptions[2].binding = 1;
521+
attributeDescriptions[2].location = 2;
522+
attributeDescriptions[2].format = VK_FORMAT_R32_SFLOAT;
523+
attributeDescriptions[2].offset = 0;
511524

512525
uboLayoutBinding.binding = 0;
513526
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;

0 commit comments

Comments
 (0)