|
2 | 2 |
|
3 | 3 | namespace MauRen |
4 | 4 | { |
5 | | - // ! THIS IS NOT SAFE TO CALL DURING A FRAME, HAS TO BE HANDLED IF WE WANT THAT |
6 | 5 | void VulkanDescriptorContext::BindTexture(uint32_t destLocation, VkImageView imageView, VkImageLayout imageLayout) |
7 | 6 | { |
8 | 7 | VkDescriptorImageInfo imageInfo{}; |
9 | 8 | imageInfo.imageLayout = imageLayout; |
10 | 9 | imageInfo.imageView = imageView; |
11 | 10 | imageInfo.sampler = VK_NULL_HANDLE; |
12 | 11 |
|
13 | | - auto const deviceContext{ VulkanDeviceContextManager::GetInstance().GetDeviceContext() }; |
14 | | - |
15 | 12 | for (size_t i{ 0 }; i < MAX_FRAMES_IN_FLIGHT; ++i) |
16 | 13 | { |
17 | | - VkWriteDescriptorSet descriptorWrite{}; |
18 | | - descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
19 | | - descriptorWrite.dstSet = m_DescriptorSets[i]; |
20 | | - descriptorWrite.dstBinding = TEXTURE_BINDING_SLOT; |
21 | | - descriptorWrite.dstArrayElement = destLocation; |
22 | | - |
23 | | - ME_ASSERT(destLocation < MAX_TEXTURES); |
24 | | - |
25 | | - descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
26 | | - descriptorWrite.descriptorCount = 1; |
27 | | - |
28 | | - descriptorWrite.pImageInfo = &imageInfo; |
29 | | - |
30 | | - vkUpdateDescriptorSets(deviceContext->GetLogicalDevice(), 1, &descriptorWrite, 0, nullptr); |
| 14 | + m_DescriptorSetUpdates[i].emplace_back( |
| 15 | + DescriptorSetUpdate::CreateImageUpdate( |
| 16 | + m_DescriptorSets[i], |
| 17 | + TEXTURE_BINDING_SLOT, |
| 18 | + destLocation, |
| 19 | + VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, |
| 20 | + { imageInfo } |
| 21 | + ) |
| 22 | + ); |
31 | 23 | } |
32 | 24 | } |
33 | 25 |
|
34 | | - // ! THIS IS NOT SAFE TO CALL DURING A FRAME, HAS TO BE HANDLED IF WE WANT THAT |
35 | 26 | void VulkanDescriptorContext::BindMaterialBuffer(VkDescriptorBufferInfo bufferInfo, uint32_t frame) |
36 | 27 | { |
37 | | - auto const deviceContext{ VulkanDeviceContextManager::GetInstance().GetDeviceContext() }; |
38 | | - |
39 | | - // Update descriptor set |
40 | | - VkWriteDescriptorSet descriptorWrite{}; |
41 | | - descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
42 | | - descriptorWrite.dstSet = m_DescriptorSets[frame]; |
43 | | - |
44 | | - descriptorWrite.dstBinding = MATERIAL_DATA_BINDING_SLOT; |
45 | | - descriptorWrite.dstArrayElement = 0; |
46 | | - descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
47 | | - descriptorWrite.descriptorCount = 1; |
48 | | - descriptorWrite.pBufferInfo = &bufferInfo; |
49 | | - |
50 | | - vkUpdateDescriptorSets(deviceContext->GetLogicalDevice(), 1, &descriptorWrite, 0, nullptr); |
| 28 | + m_DescriptorSetUpdates[frame].emplace_back( |
| 29 | + DescriptorSetUpdate::CreateBufferUpdate( |
| 30 | + m_DescriptorSets[frame], |
| 31 | + MATERIAL_DATA_BINDING_SLOT, |
| 32 | + 0, |
| 33 | + VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 34 | + { bufferInfo } |
| 35 | + ) |
| 36 | + ); |
51 | 37 | } |
52 | 38 |
|
53 | | - // ! THIS IS NOT SAFE TO CALL DURING A FRAME, HAS TO BE HANDLED IF WE WANT THAT |
54 | 39 | void VulkanDescriptorContext::BindLightBuffer(VkDescriptorBufferInfo bufferInfo, uint32_t frame) |
55 | 40 | { |
56 | | - auto const deviceContext{ VulkanDeviceContextManager::GetInstance().GetDeviceContext() }; |
57 | | - |
58 | | - // Update descriptor set |
59 | | - VkWriteDescriptorSet descriptorWrite{}; |
60 | | - descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
61 | | - descriptorWrite.dstSet = m_DescriptorSets[frame]; |
62 | | - |
63 | | - descriptorWrite.dstBinding = LIGHT_BUFFER_SLOT; |
64 | | - descriptorWrite.dstArrayElement = 0; |
65 | | - descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
66 | | - descriptorWrite.descriptorCount = 1; |
67 | | - descriptorWrite.pBufferInfo = &bufferInfo; |
68 | | - |
69 | | - vkUpdateDescriptorSets(deviceContext->GetLogicalDevice(), 1, &descriptorWrite, 0, nullptr); |
| 41 | + m_DescriptorSetUpdates[frame].emplace_back( |
| 42 | + DescriptorSetUpdate::CreateBufferUpdate( |
| 43 | + m_DescriptorSets[frame], |
| 44 | + LIGHT_BUFFER_SLOT, |
| 45 | + 0, |
| 46 | + VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 47 | + { bufferInfo } |
| 48 | + ) |
| 49 | + ); |
70 | 50 | } |
71 | 51 |
|
72 | | - // ! THIS IS NOT SAFE TO CALL DURING A FRAME, HAS TO BE HANDLED IF WE WANT THAT |
73 | 52 | void VulkanDescriptorContext::BindShadowMap(uint32_t destLocation, VkImageView imageView, VkImageLayout imageLayout) |
74 | 53 | { |
75 | 54 | VkDescriptorImageInfo imageInfo{}; |
76 | 55 | imageInfo.imageLayout = imageLayout; |
77 | 56 | imageInfo.imageView = imageView; |
78 | 57 | imageInfo.sampler = VK_NULL_HANDLE; |
79 | 58 |
|
80 | | - auto const deviceContext{ VulkanDeviceContextManager::GetInstance().GetDeviceContext() }; |
| 59 | + ME_ASSERT(destLocation < MAX_SHADOW_MAPS); |
81 | 60 |
|
82 | 61 | for (size_t i{ 0 }; i < MAX_FRAMES_IN_FLIGHT; ++i) |
83 | 62 | { |
84 | | - VkWriteDescriptorSet descriptorWrite{}; |
85 | | - descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
86 | | - descriptorWrite.dstSet = m_DescriptorSets[i]; |
87 | | - descriptorWrite.dstBinding = SHADOW_MAPS_SLOT; |
88 | | - descriptorWrite.dstArrayElement = destLocation; |
89 | | - |
90 | | - ME_ASSERT(destLocation < MAX_SHADOW_MAPS); |
91 | | - |
92 | | - descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
93 | | - descriptorWrite.descriptorCount = 1; |
94 | | - |
95 | | - descriptorWrite.pImageInfo = &imageInfo; |
96 | | - |
97 | | - vkUpdateDescriptorSets(deviceContext->GetLogicalDevice(), 1, &descriptorWrite, 0, nullptr); |
| 63 | + m_DescriptorSetUpdates[i].emplace_back( |
| 64 | + DescriptorSetUpdate::CreateImageUpdate( |
| 65 | + m_DescriptorSets[i], |
| 66 | + SHADOW_MAPS_SLOT, |
| 67 | + destLocation, |
| 68 | + VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, |
| 69 | + { imageInfo } |
| 70 | + ) |
| 71 | + ); |
98 | 72 | } |
99 | 73 | } |
100 | 74 |
|
101 | 75 | void VulkanDescriptorContext::BindShadowMapSampler(VkSampler sampler) |
102 | 76 | { |
103 | | - auto const deviceContext{ VulkanDeviceContextManager::GetInstance().GetDeviceContext() }; |
104 | | - |
105 | 77 | VkDescriptorImageInfo imageInfo{}; |
106 | 78 | imageInfo.sampler = sampler; |
107 | 79 |
|
108 | 80 | for (size_t i{ 0 }; i < MAX_FRAMES_IN_FLIGHT; ++i) |
109 | 81 | { |
110 | | - VkWriteDescriptorSet samplerWrite{}; |
111 | | - samplerWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
112 | | - samplerWrite.dstSet = m_DescriptorSets[i]; |
113 | | - samplerWrite.dstBinding = SHADOW_MAP_SAMPLER; |
114 | | - samplerWrite.dstArrayElement = 0; |
115 | | - samplerWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; |
116 | | - samplerWrite.descriptorCount = 1; |
117 | | - samplerWrite.pImageInfo = &imageInfo; |
118 | | - |
119 | | - vkUpdateDescriptorSets(deviceContext->GetLogicalDevice(), 1, &samplerWrite, 0, nullptr); |
| 82 | + m_DescriptorSetUpdates[i].emplace_back( |
| 83 | + DescriptorSetUpdate::CreateImageUpdate( |
| 84 | + m_DescriptorSets[i], |
| 85 | + SHADOW_MAP_SAMPLER, |
| 86 | + 0, |
| 87 | + VK_DESCRIPTOR_TYPE_SAMPLER, |
| 88 | + { imageInfo } |
| 89 | + ) |
| 90 | + ); |
120 | 91 | } |
121 | 92 | } |
122 | 93 |
|
| 94 | + void VulkanDescriptorContext::BindMeshInstanceDataBuffer(VkDescriptorBufferInfo bufferInfo, uint32_t frame) |
| 95 | + { |
| 96 | + m_DescriptorSetUpdates[frame].emplace_back( |
| 97 | + DescriptorSetUpdate::CreateBufferUpdate( |
| 98 | + m_DescriptorSets[frame], |
| 99 | + MESH_INSTANCE_DATA_BINDING_SLOT, |
| 100 | + 0, |
| 101 | + VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 102 | + { bufferInfo } |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
123 | 107 | void VulkanDescriptorContext::CreateDescriptorSetLayout() |
124 | 108 | { |
125 | 109 | VkDescriptorSetLayoutBinding uboLayoutBinding{}; |
@@ -502,4 +486,41 @@ namespace MauRen |
502 | 486 | VulkanUtils::SafeDestroy(deviceContext->GetLogicalDevice(), m_DescriptorPool, nullptr); |
503 | 487 | VulkanUtils::SafeDestroy(deviceContext->GetLogicalDevice(), m_DescriptorSetLayout, nullptr); |
504 | 488 | } |
| 489 | + |
| 490 | + void VulkanDescriptorContext::ProcessDescriptorUpdateQueue(uint32_t frame) noexcept |
| 491 | + { |
| 492 | + auto const deviceContext{ VulkanDeviceContextManager::GetInstance().GetDeviceContext() }; |
| 493 | + |
| 494 | + auto& updates{ m_DescriptorSetUpdates[frame] }; |
| 495 | + |
| 496 | + for (auto const& update : updates) |
| 497 | + { |
| 498 | + VkWriteDescriptorSet write{}; |
| 499 | + write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 500 | + write.dstSet = update.targetSet; |
| 501 | + write.dstBinding = update.dstBinding; |
| 502 | + write.dstArrayElement = update.dstArrayElement; |
| 503 | + write.descriptorType = update.descriptorType; |
| 504 | + |
| 505 | + switch (update.type) |
| 506 | + { |
| 507 | + case DescriptorSetUpdate::EType::Buffer: |
| 508 | + write.pBufferInfo = std::get<std::vector<VkDescriptorBufferInfo>>(update.descriptorData).data(); |
| 509 | + write.descriptorCount = static_cast<uint32_t>(std::size(std::get<std::vector<VkDescriptorBufferInfo>>(update.descriptorData))); |
| 510 | + break; |
| 511 | + case DescriptorSetUpdate::EType::Image: |
| 512 | + write.pImageInfo = std::get<std::vector<VkDescriptorImageInfo>>(update.descriptorData).data(); |
| 513 | + write.descriptorCount = static_cast<uint32_t>(std::size(std::get<std::vector<VkDescriptorImageInfo>>(update.descriptorData))); |
| 514 | + break; |
| 515 | + case DescriptorSetUpdate::EType::TexelBuffer: |
| 516 | + write.pTexelBufferView = std::get<std::vector<VkBufferView>>(update.descriptorData).data(); |
| 517 | + write.descriptorCount = static_cast<uint32_t>(std::size(std::get<std::vector<VkBufferView>>(update.descriptorData))); |
| 518 | + break; |
| 519 | + } |
| 520 | + |
| 521 | + vkUpdateDescriptorSets(deviceContext->GetLogicalDevice(), 1, &write, 0, nullptr); |
| 522 | + } |
| 523 | + |
| 524 | + updates.clear(); |
| 525 | + } |
505 | 526 | } |
0 commit comments