@@ -32,15 +32,26 @@ Material::Material(Shader vertShader, Shader fragShader, bool isWritingDepth) :
32
32
33
33
auto device = Context::GetVkLogicalDevice ();
34
34
35
+ const auto framesCount = Swapchain::MAX_FRAMES_IN_FLIGHT;
36
+
35
37
// Separate uniforms into unique properties
36
38
39
+ bufferUpdates = MHArray<MHArray<UniformBufferUpdate>>(framesCount);
40
+ imageUpdates = MHArray<MHArray<UniformImageUpdate>>(framesCount);
41
+
42
+ for (size_t i = 0 ; i < framesCount; i++)
43
+ {
44
+ bufferUpdates.Append (MHArray<UniformBufferUpdate>(10 ));
45
+ imageUpdates.Append (MHArray<UniformImageUpdate>(MAX_TEXTURES));
46
+ }
47
+
48
+ textureInfos = MHArray<ImageData>(MAX_TEXTURES);
49
+
37
50
uint64_t offset {0 };
38
51
39
52
AddShader (vertexShader, OUT offset);
40
53
AddShader (fragmentShader, OUT offset);
41
54
42
- auto framesCount = Swapchain::MAX_FRAMES_IN_FLIGHT;
43
-
44
55
// Allocate buffer which can store all the data we need
45
56
Buffer::CreateBuffer (bufferSize,
46
57
Vulkan::Utils::STORAGE_BUFFER | Vulkan::Utils::UNIFORM_BUFFER,
@@ -51,8 +62,6 @@ Material::Material(Shader vertShader, Shader fragShader, bool isWritingDepth) :
51
62
// Set the number of frames
52
63
perFrameDescriptorSets = MHArray<MSArray<VkDescriptorSet, MAX_UNIFORM_SETS>>(framesCount);
53
64
54
- writes = MHArray<MSArray<VkWriteDescriptorSet, 10 >>(framesCount);
55
-
56
65
// Create Descriptor Set Layout for Sets
57
66
for (auto slotIt = propertiesSlots.CreateIterator (); slotIt; ++slotIt)
58
67
{
@@ -87,7 +96,7 @@ Material::Material(Shader vertShader, Shader fragShader, bool isWritingDepth) :
87
96
WriteSet (slotIt.GetIndex (), slot);
88
97
}
89
98
90
- for ( auto writeIt = writes. CreateIterator (); writeIt; ++writeIt) Write (*writeIt );
99
+ for ( size_t i = 0 ; i < framesCount; i++) UpdateUniforms (bufferUpdates[i], imageUpdates[i] );
91
100
92
101
// Create Pipeline
93
102
Recreate ();
@@ -153,8 +162,6 @@ uint32_t Material::SetTexture(Hash::StringId id, Texture2D* texture)
153
162
using namespace Utils ;
154
163
using namespace Descriptor ;
155
164
156
- auto & writeSets = writes[Renderer::GetCurrentFrameIndex ()];
157
-
158
165
auto texIndex = FindTextureIndex (texture->GetId ());
159
166
160
167
if (texIndex > -1 ) return texIndex;
@@ -173,15 +180,11 @@ uint32_t Material::SetTexture(Hash::StringId id, Texture2D* texture)
173
180
174
181
auto info = texture->GetInfo ();
175
182
176
- texture2DInfos[texIndex] = {info.sampler ,
177
- info.imageInfo .view ,
178
- ToVkImageLayout (info.imageInfo .layout )};
179
-
180
- if (writeSets.Count () > 0 ) break ;
183
+ textureInfos[texIndex] = {info.sampler , info.imageInfo .view , info.imageInfo .layout };
181
184
182
185
auto prop = properties[propIdx];
183
186
184
- for (auto write = writes .CreateFIterator (); write; ++write)
187
+ for (auto write = imageUpdates .CreateFIterator (); write; ++write)
185
188
{
186
189
QueueImageUpdate (*write,
187
190
perFrameDescriptorSets[write.GetIndex ()][it.GetIndex ()],
@@ -238,10 +241,9 @@ void Material::AddShader(const Shader& shader, uint64_t& offset)
238
241
auto & defaultTexture2DInfo = shader.GetDefaultTexture2DInfo ();
239
242
for (size_t i = 0 ; i < uniform.count ; i++)
240
243
{
241
- texture2DInfos.Append (
242
- {defaultTexture2DInfo.sampler ,
243
- defaultTexture2DInfo.imageInfo .view ,
244
- Utils::ToVkImageLayout (defaultTexture2DInfo.imageInfo .layout )});
244
+ textureInfos.Append ({defaultTexture2DInfo.sampler ,
245
+ defaultTexture2DInfo.imageInfo .view ,
246
+ defaultTexture2DInfo.imageInfo .layout });
245
247
}
246
248
}
247
249
}
@@ -287,11 +289,15 @@ void Material::Update()
287
289
{
288
290
auto currentFrameIdx = Renderer::GetCurrentFrameIndex ();
289
291
290
- auto & targetSets = writes[currentFrameIdx];
292
+ auto & targetBuffers = bufferUpdates[currentFrameIdx];
293
+ auto & targetImages = imageUpdates[currentFrameIdx];
291
294
292
295
using Vulkan::Context;
293
296
294
- Write (targetSets);
297
+ // Write(targetSets);
298
+ UpdateUniforms (targetBuffers, targetImages);
299
+
300
+ targetBuffers.Clear ();
295
301
}
296
302
297
303
void Material::Update (Hash::StringId id)
@@ -302,8 +308,13 @@ void Material::Update(Hash::StringId id)
302
308
303
309
if (propertyIndex == -1 ) return ;
304
310
305
- auto & setsToWrite = writes[Renderer::GetCurrentFrameIndex ()];
306
- Write (setsToWrite);
311
+ auto & buffersToWrite = bufferUpdates[Renderer::GetCurrentFrameIndex ()];
312
+ auto & imagesToWrite = imageUpdates[Renderer::GetCurrentFrameIndex ()];
313
+ // Write(setsToWrite);
314
+ UpdateUniforms (buffersToWrite, imagesToWrite);
315
+
316
+ buffersToWrite.Clear ();
317
+ imagesToWrite.Clear ();
307
318
}
308
319
}
309
320
@@ -317,55 +328,79 @@ void Material::WriteSet(uint32_t set, PropertiesSlot& slot)
317
328
for (auto propIt = properties.CreateIterator (); propIt; ++propIt)
318
329
{
319
330
auto prop = *propIt;
320
- bufferInfos. Append ( CreateBufferInfo (buffer. buffer , prop. offset , prop. size ));
331
+
321
332
for (auto setIt = perFrameDescriptorSets.CreateFIterator (); setIt; ++setIt)
322
333
{
334
+
323
335
auto sets = *setIt;
324
336
if (IsTexture2D (prop.type ))
325
- QueueImageUpdate (writes [setIt.GetIndex ()], sets[set], propIt.GetIndex ());
337
+ QueueImageUpdate (imageUpdates [setIt.GetIndex ()], sets[set], propIt.GetIndex ());
326
338
else
327
- QueuePropertyUpdate (writes [setIt.GetIndex ()],
339
+ QueuePropertyUpdate (bufferUpdates [setIt.GetIndex ()],
328
340
sets[set],
329
341
prop.type ,
330
342
propIt.GetIndex (),
331
- 1 );
343
+ 1 ,
344
+ {buffer.buffer , prop.offset , prop.size });
332
345
}
333
346
}
334
347
}
335
348
336
- void Material::Write (MSArray<VkWriteDescriptorSet, 10 >& sets )
349
+ void Material::UpdateUniforms (MHArray<UniformBufferUpdate>& buffers, MHArray<UniformImageUpdate >& images )
337
350
{
338
- Utils::Descriptor::WriteSets ( Context::GetVkLogicalDevice (), sets. Data (), sets. Count ()) ;
351
+ using namespace Vulkan ::Utils::Descriptor ;
339
352
340
- sets.Clear ();
353
+ MSArray<VkWriteDescriptorSet, 10 > writeSets;
354
+ MSArray<VkDescriptorBufferInfo, MAX_UNIFORM_SETS * 10 > bufferInfs;
355
+ MSArray<VkDescriptorImageInfo, MAX_TEXTURES> imageInfos;
356
+
357
+ for (auto it = buffers.CreateIterator (); it; ++it)
358
+ {
359
+ auto & update = it->update ;
360
+ auto & bufferInfo = it->bufferUpdate ;
361
+
362
+ bufferInfs.Append ({bufferInfo.buffer , bufferInfo.offset , bufferInfo.range });
363
+ writeSets.Append (CreateWriteSet (update.dstBinding , update.set , update.descriptors , Utils::ToVkDescriptorType (update.type ), &bufferInfs.Back ()));
364
+ }
365
+
366
+ for (auto it = textureInfos.CreateIterator (); it; ++it)
367
+ {
368
+ imageInfos.Append ({it->sampler , it->view , Utils::ToVkImageLayout (it->layout )});
369
+ }
370
+
371
+ for (auto it = images.CreateIterator (); it; ++it)
372
+ {
373
+ auto & update = it->update ;
374
+
375
+ writeSets.Append (WriteDescriptorImage (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, update.set , imageInfos.Data (), update.dstBinding , update.descriptors , update.dstIndex ));
376
+ }
377
+
378
+ Utils::Descriptor::WriteSets (Context::GetVkLogicalDevice (), writeSets.Data (), writeSets.Count ());
379
+ buffers.Clear ();
380
+ images.Clear ();
341
381
}
342
382
343
- void Material::QueueImageUpdate (MSArray<VkWriteDescriptorSet, 10 >& writeQueue ,
383
+ void Material::QueueImageUpdate (MHArray<UniformImageUpdate >& imageUpdate ,
344
384
VkDescriptorSet& set,
345
385
uint32_t binding,
346
386
uint32_t count,
347
387
uint32_t index)
348
388
{
349
389
using namespace Vulkan ::Utils::Descriptor;
350
390
351
- writeQueue.Append (WriteDescriptorImage (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
352
- set,
353
- texture2DInfos.Data (),
354
- binding,
355
- count,
356
- index));
391
+ imageUpdate.Append ({{set, binding, index, count, Utils::UniformType::TEXTURE2D}, textureInfos});
357
392
}
358
393
359
- void Material::QueuePropertyUpdate (MSArray<VkWriteDescriptorSet, 10 >& writeQueue ,
394
+ void Material::QueuePropertyUpdate (MHArray<UniformBufferUpdate >& bufferUpdateQueue ,
360
395
VkDescriptorSet& set,
361
396
Utils::UniformType type,
362
397
uint32_t binding,
363
- uint32_t count)
398
+ uint32_t count,
399
+ BufferData bufferInfo)
364
400
{
365
401
using namespace Vulkan ::Utils::Descriptor;
366
402
367
- writeQueue.Append (
368
- CreateWriteSet (binding, set, count, ToVkDescriptorType (type), &bufferInfos[binding]));
403
+ bufferUpdateQueue.Append ({{set, binding, 0 , count, type}, bufferInfo});
369
404
}
370
405
371
406
int32_t Material::FindPropertyIndex (Hash::StringId id, PropertiesSlot& slot)
@@ -405,24 +440,25 @@ void Material::Swap(Material& other)
405
440
auto tmpGraphicsPipeline = std::move (graphicsPipeline);
406
441
auto tmpPropertiesSlots = propertiesSlots;
407
442
auto tmpPerFrameDescriptors = std::move (perFrameDescriptorSets);
408
- auto tmpWrites = std::move (writes);
409
- auto tmpBufferInfos = std::move (bufferInfos);
410
- auto tmpTexture2DInfos = std::move (texture2DInfos);
411
443
auto tmpPushConstant = pushConstant;
412
444
auto tmpIsWritingDepth = isWritingDepth;
413
445
446
+ auto tmpTextureInfos = std::move (textureInfos);
447
+ auto tmpImageUpdates = std::move (imageUpdates);
448
+ auto tmpBufferUpdates = std::move (bufferUpdates);
449
+
414
450
vertexShader = std::move (other.vertexShader );
415
451
fragmentShader = std::move (other.fragmentShader );
416
452
bufferSize = other.bufferSize ;
417
453
buffer = other.buffer ;
418
454
graphicsPipeline = std::move (other.graphicsPipeline );
419
455
propertiesSlots = other.propertiesSlots ;
420
456
perFrameDescriptorSets = std::move (other.perFrameDescriptorSets );
421
- writes = std::move (other.writes );
422
- bufferInfos = std::move (other.bufferInfos );
423
- texture2DInfos = std::move (other.texture2DInfos );
424
457
pushConstant = other.pushConstant ;
425
458
isWritingDepth = other.isWritingDepth ;
459
+ textureInfos = std::move (other.textureInfos );
460
+ imageUpdates = std::move (other.imageUpdates );
461
+ bufferUpdates = std::move (other.bufferUpdates );
426
462
427
463
other.vertexShader = std::move (tmpVertexShader);
428
464
other.fragmentShader = std::move (tmpFragmentShader);
@@ -431,10 +467,11 @@ void Material::Swap(Material& other)
431
467
other.graphicsPipeline = std::move (tmpGraphicsPipeline);
432
468
other.propertiesSlots = tmpPropertiesSlots;
433
469
other.perFrameDescriptorSets = std::move (tmpPerFrameDescriptors);
434
- other.writes = std::move (tmpWrites);
435
- other.bufferInfos = std::move (tmpBufferInfos);
436
- other.texture2DInfos = std::move (tmpTexture2DInfos);
437
470
other.pushConstant = tmpPushConstant;
438
471
other.isWritingDepth = tmpIsWritingDepth;
472
+
473
+ other.textureInfos = std::move (tmpTextureInfos);
474
+ other.imageUpdates = std::move (tmpImageUpdates);
475
+ other.bufferUpdates = std::move (tmpBufferUpdates);
439
476
}
440
477
} // namespace Siege::Vulkan
0 commit comments