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
+
1
16
#include " compute.h"
2
17
3
18
#include < stdexcept>
@@ -25,7 +40,7 @@ void Compute::Init(VkDevice& device)
25
40
{
26
41
// size = sizeof(float)*extent.width*extent.height;
27
42
28
- VkFormat format = VK_FORMAT_R32_SFLOAT;
43
+ // VkFormat format = VK_FORMAT_R32_SFLOAT;
29
44
30
45
// assert(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT);
31
46
@@ -43,7 +58,8 @@ void Compute::Init(VkDevice& device)
43
58
44
59
size = extent.width *extent.height *sizeof (float );
45
60
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;
47
63
usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
48
64
49
65
SetupBuffer (device, storageBuffer, storageBufferMemory, size, properties, usage);
@@ -79,23 +95,13 @@ void Compute::Destroy(VkDevice& device)
79
95
vkDestroyFence (device, fence, nullptr );
80
96
}
81
97
82
- void Compute::SetupBuffers (VkDevice& device)
98
+ /* void Compute::SetupBuffers(VkDevice& device)
83
99
{
84
100
85
- }
101
+ }*/
86
102
87
103
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
+ {
99
105
uint32_t uniformIndex = 0 ;
100
106
uint32_t storageIndex = 1 ;
101
107
@@ -125,29 +131,6 @@ void Compute::SetupQueue(VkDevice& device, uint32_t queueFamilyId)
125
131
throw std::runtime_error (" Descriptor set layout creation failed" );
126
132
}
127
133
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
-
151
134
VkDescriptorPoolSize poolSizes[2 ];
152
135
poolSizes[0 ].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
153
136
poolSizes[0 ].descriptorCount = 1 ;
@@ -290,21 +273,21 @@ VkCommandBuffer* Compute::SetupCommandBuffer(VkDevice& device, uint32_t queueFam
290
273
throw std::runtime_error (" Compute command buffer beign failed" );
291
274
}
292
275
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 ;
300
283
301
- /* vkCmdPipelineBarrier(commandBuffer,
284
+ vkCmdPipelineBarrier (commandBuffer,
302
285
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
303
286
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
304
287
0 ,
305
288
0 , nullptr ,
306
289
1 , &memoryBarrier,
307
- 0, nullptr);*/
290
+ 0 , nullptr );
308
291
309
292
vkCmdBindPipeline (commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
310
293
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
331
314
return &commandBuffer;
332
315
}
333
316
334
- /* uint32_t Compute::GetMemoryTypeIndex (VkDevice& device, VkBuffer& buffer, VkPhysicalDeviceMemoryProperties& props, VkMemoryPropertyFlags propFlags, uint32_t& allocSize )
317
+ void Compute::PrintResults (VkDevice& device)
335
318
{
336
- VkMemoryRequirements memRequirements;
319
+ void * data;
320
+ uint32_t size = extent.width *extent.height *sizeof (float );
321
+ vkMapMemory (device, storageBufferMemory, 0 , size, 0 , &data);
337
322
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)
343
324
{
344
- if ((memRequirements.memoryTypeBits & (1 << i)) &&
345
- (props.memoryTypes[i].propertyFlags & propFlags) == propFlags)
325
+ for (uint32_t j = 0 ; j < extent.height ; ++j)
346
326
{
347
- memTypeIndex = i ;
327
+ std::cout << static_cast < float *>(data)[i*j] << " , " ;
348
328
}
329
+
330
+ std::cout << std::endl;
349
331
}
350
332
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
+ }
0 commit comments