Skip to content

Commit 19a92f3

Browse files
committed
Removed usage of ForEach function across codebase
1 parent f4b1f08 commit 19a92f3

File tree

4 files changed

+132
-54
lines changed

4 files changed

+132
-54
lines changed

engine/render/renderer/platform/vulkan/Material.cpp

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,40 @@ Material::Material(Shader vertShader, Shader fragShader) :
5454
writes = MHArray<MSArray<VkWriteDescriptorSet, 10>>(framesCount);
5555

5656
// Create Descriptor Set Layout for Sets
57-
propertiesSlots.MForEachI([&](PropertiesSlot& slot, size_t i) {
57+
for (auto slotIt = propertiesSlots.CreateIterator(); slotIt; ++slotIt)
58+
{
5859
MSArray<VkDescriptorSetLayoutBinding, 10> bindings;
5960

61+
auto& slot = *slotIt;
62+
6063
auto& properties = slot.properties;
6164

62-
properties.MForEachI([&](Property& prop, size_t j) {
63-
bindings.Append(CreateLayoutBinding(j, prop.count, prop.type, prop.shaderStage));
64-
prop.binding = j;
65-
});
65+
for (auto propIt = properties.CreateIterator(); propIt; ++propIt)
66+
{
67+
auto& prop = *propIt;
68+
bindings.Append(
69+
CreateLayoutBinding(propIt.GetIndex(), prop.count, prop.type, prop.shaderStage));
70+
prop.binding = propIt.GetIndex();
71+
}
6672

6773
CC_ASSERT(CreateLayout(device, OUT slot.layout, bindings.Data(), properties.Count()),
6874
"Failed to create descriptor set!")
6975

70-
perFrameDescriptorSets.ForEach(MSA_IT(VkDescriptorSet, MAX_UNIFORM_SETS, sets) {
76+
for (auto setIt = perFrameDescriptorSets.CreateFIterator(); setIt; ++setIt)
77+
{
78+
auto& sets = *setIt;
7179
sets.Append(VK_NULL_HANDLE);
7280
AllocateSets(device,
7381
OUT & sets.Back(),
7482
DescriptorPool::GetDescriptorPool(),
7583
1,
7684
&slot.layout);
77-
});
85+
}
7886

79-
WriteSet(i, slot);
80-
});
87+
WriteSet(slotIt.GetIndex(), slot);
88+
}
8189

82-
writes.MForEach(MSA_IT(VkWriteDescriptorSet, 10, sets) { Write(sets); });
90+
for (auto writeIt = writes.CreateIterator(); writeIt; ++writeIt) Write(*writeIt);
8391

8492
// Create Pipeline
8593
Recreate();
@@ -97,8 +105,10 @@ Material::~Material()
97105

98106
Buffer::DestroyBuffer(buffer);
99107

100-
propertiesSlots.ForEach(
101-
[&](PropertiesSlot& prop) { vkDestroyDescriptorSetLayout(device, prop.layout, nullptr); });
108+
for (auto it = propertiesSlots.CreateFIterator(); it; ++it)
109+
{
110+
vkDestroyDescriptorSetLayout(device, (*it).layout, nullptr);
111+
}
102112
}
103113

104114
void Material::Destroy()
@@ -107,10 +117,12 @@ void Material::Destroy()
107117

108118
Buffer::DestroyBuffer(buffer);
109119

110-
propertiesSlots.MForEach([&](PropertiesSlot& prop) {
120+
for (auto it = propertiesSlots.CreateFIterator(); it; ++it)
121+
{
122+
auto& prop = *it;
111123
vkDestroyDescriptorSetLayout(device, prop.layout, nullptr);
112124
prop.layout = nullptr;
113-
});
125+
}
114126

115127
vertexShader.Destroy();
116128
fragmentShader.Destroy();
@@ -151,26 +163,34 @@ uint32_t Material::SetTexture(Hash::StringId id, Texture2D* texture)
151163
texIndex = textureIds.Count();
152164
textureIds.Append(texture->GetId());
153165

154-
propertiesSlots.MForEachI([&](PropertiesSlot& slot, size_t i) {
166+
for (auto it = propertiesSlots.CreateIterator(); it; ++it)
167+
{
168+
auto& slot = *it;
155169
auto& properties = slot.properties;
170+
156171
auto propIdx = FindPropertyIndex(id, slot);
157172

158-
if (propIdx == -1) return;
173+
if (propIdx == -1) continue;
159174

160175
auto info = texture->GetInfo();
161176

162177
texture2DInfos[texIndex] = {info.sampler,
163178
info.imageInfo.view,
164179
ToVkImageLayout(info.imageInfo.layout)};
165180

166-
if (writeSets.Count() > 0) return;
181+
if (writeSets.Count() > 0) break;
167182

168183
auto prop = properties[propIdx];
169184

170-
writes.ForEachI(MSA_IT_I(VkWriteDescriptorSet, 10, sets, j) {
171-
QueueImageUpdate(sets, perFrameDescriptorSets[j][i], prop.binding, prop.count, 0);
172-
});
173-
});
185+
for (auto write = writes.CreateFIterator(); write; ++write)
186+
{
187+
QueueImageUpdate(*write,
188+
perFrameDescriptorSets[write.GetIndex()][it.GetIndex()],
189+
prop.binding,
190+
prop.count,
191+
0);
192+
}
193+
}
174194

175195
return texIndex;
176196
}
@@ -238,16 +258,19 @@ void Material::Bind(const CommandBuffer& commandBuffer)
238258
graphicsPipeline.BindSets(commandBuffer, perFrameDescriptorSets[frameIndex]);
239259
}
240260

241-
void Material::BindPushConstant(const CommandBuffer& buffer, const void* values)
261+
void Material::BindPushConstant(const CommandBuffer& commandBuffer, const void* values)
242262
{
243-
graphicsPipeline.PushConstants(buffer, pushConstant.type, pushConstant.size, values);
263+
graphicsPipeline.PushConstants(commandBuffer, pushConstant.type, pushConstant.size, values);
244264
}
245265

246266
void Material::Recreate()
247267
{
248268
MSArray<VkDescriptorSetLayout, 10> layouts;
249269

250-
propertiesSlots.MForEach([&](PropertiesSlot& slot) { layouts.Append(slot.layout); });
270+
for (auto it = propertiesSlots.CreateIterator(); it; ++it)
271+
{
272+
layouts.Append((*it).layout);
273+
}
251274

252275
graphicsPipeline = Pipeline::Builder()
253276
.WithRenderPass(Context::GetSwapchain().GetRenderPass())
@@ -273,14 +296,15 @@ void Material::Update()
273296

274297
void Material::Update(Hash::StringId id)
275298
{
276-
propertiesSlots.ForEachI([&](PropertiesSlot& slot, size_t i) {
277-
auto propertyIndex = FindPropertyIndex(id, slot);
299+
for (auto it = propertiesSlots.CreateFIterator(); it; ++it)
300+
{
301+
auto propertyIndex = FindPropertyIndex(id, *it);
278302

279303
if (propertyIndex == -1) return;
280304

281305
auto& setsToWrite = writes[Renderer::GetCurrentFrameIndex()];
282306
Write(setsToWrite);
283-
});
307+
}
284308
}
285309

286310
void Material::WriteSet(uint32_t set, PropertiesSlot& slot)
@@ -290,13 +314,23 @@ void Material::WriteSet(uint32_t set, PropertiesSlot& slot)
290314

291315
auto& properties = slot.properties;
292316

293-
properties.MForEachI([&](Property& prop, size_t i) {
317+
for (auto propIt = properties.CreateIterator(); propIt; ++propIt)
318+
{
319+
auto prop = *propIt;
294320
bufferInfos.Append(CreateBufferInfo(buffer.buffer, prop.offset, prop.size));
295-
perFrameDescriptorSets.ForEachI(MSA_IT_I(VkDescriptorSet, MAX_UNIFORM_SETS, sets, j) {
296-
if (IsTexture2D(prop.type)) QueueImageUpdate(writes[j], sets[set], i);
297-
else QueuePropertyUpdate(writes[j], sets[set], prop.type, i, 1);
298-
});
299-
});
321+
for (auto setIt = perFrameDescriptorSets.CreateFIterator(); setIt; ++setIt)
322+
{
323+
auto sets = *setIt;
324+
if (IsTexture2D(prop.type))
325+
QueueImageUpdate(writes[setIt.GetIndex()], sets[set], propIt.GetIndex());
326+
else
327+
QueuePropertyUpdate(writes[setIt.GetIndex()],
328+
sets[set],
329+
prop.type,
330+
propIt.GetIndex(),
331+
1);
332+
}
333+
}
300334
}
301335

302336
void Material::Write(MSArray<VkWriteDescriptorSet, 10>& sets)
@@ -337,26 +371,28 @@ void Material::QueuePropertyUpdate(MSArray<VkWriteDescriptorSet, 10>& writeQueue
337371
int32_t Material::FindPropertyIndex(Hash::StringId id, PropertiesSlot& slot)
338372
{
339373
int32_t foundIdx {-1};
340-
slot.properties.MForEachI([&](Property& property, size_t i) {
341-
if (property.id == id)
374+
for (auto it = slot.properties.CreateIterator(); it; ++it)
375+
{
376+
if ((*it).id == id)
342377
{
343-
foundIdx = i;
344-
return;
378+
foundIdx = it.GetIndex();
379+
break;
345380
}
346-
});
381+
}
347382
return foundIdx;
348383
}
349384

350385
int32_t Material::FindTextureIndex(Hash::StringId id)
351386
{
352387
int32_t texExists = -1;
353-
textureIds.MForEachI([&](Hash::StringId& texId, size_t i) {
354-
if (id == texId)
388+
for (auto it = textureIds.CreateIterator(); it; ++it)
389+
{
390+
if (id == *it)
355391
{
356-
texExists = i;
357-
return;
392+
texExists = it.GetIndex();
393+
break;
358394
}
359-
});
395+
}
360396
return texExists;
361397
}
362398

engine/render/renderer/platform/vulkan/Material.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Material
9494
*/
9595
void Bind(const CommandBuffer& commandBuffer);
9696

97-
void BindPushConstant(const CommandBuffer& buffer, const void* values);
97+
void BindPushConstant(const CommandBuffer& commandBuffer, const void* values);
9898

9999
/**
100100
* Recreates the Material pipeline (used primarily for window size changes)

engine/render/renderer/platform/vulkan/Pipeline.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,30 @@ Pipeline Pipeline::Builder::Build()
9999
VkVertexInputAttributeDescription attributeDescriptions[vertexShader->GetTotalAttributeCount()];
100100

101101
uint32_t processedAttributes = 0;
102-
vertShaderVertices.MForEachI([&](const Shader::VertexBinding& binding, size_t i) {
103-
inputDescriptions[i] = {static_cast<uint32_t>(i),
104-
binding.stride,
105-
VK_VERTEX_INPUT_RATE_VERTEX};
102+
103+
for (auto vertIt = vertShaderVertices.CreateIterator(); vertIt; ++vertIt)
104+
{
105+
auto index = vertIt.GetIndex();
106+
auto& binding = *vertIt;
107+
inputDescriptions[index] = {static_cast<uint32_t>(index),
108+
binding.stride,
109+
VK_VERTEX_INPUT_RATE_VERTEX};
106110

107111
auto& attributes = binding.attributes;
108112

109-
attributes.MForEachI([&](const Shader::VertexAttribute& vertex, size_t j) {
110-
size_t attributeIndex = j + processedAttributes;
113+
for (auto attrIt = attributes.CreateIterator(); attrIt; ++attrIt)
114+
{
115+
auto& vertex = *attrIt;
116+
auto attributeIndex = attrIt.GetIndex() + processedAttributes;
111117

112-
attributeDescriptions[attributeIndex] = {static_cast<uint32_t>(j),
113-
static_cast<uint32_t>(i),
118+
attributeDescriptions[attributeIndex] = {static_cast<uint32_t>(attrIt.GetIndex()),
119+
static_cast<uint32_t>(vertIt.GetIndex()),
114120
Utils::ToVkFormat(vertex.type),
115121
vertex.offset};
116-
});
122+
}
117123

118124
processedAttributes += attributes.Count();
119-
});
125+
}
120126

121127
auto vertexInputCreateInfo =
122128
CreateVertexInputInfo(static_cast<uint32_t>(vertexShader->GetTotalAttributeCount()),

engine/utils/collections/Iterators.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ class Iter
8888
return ptr->Data() != other.ptr->Data() && index != other.index;
8989
}
9090

91+
/**
92+
* Returns the index currently held by the iterator
93+
* @return returns the current index the iterator is on
94+
*/
95+
inline constexpr size_t GetIndex() const
96+
{
97+
return index;
98+
}
99+
91100
private:
92101

93102
/**
@@ -174,6 +183,15 @@ class ConstIter
174183
return ptr->Data() != other.ptr->Data() && index != other.index;
175184
}
176185

186+
/**
187+
* Returns the index currently held by the iterator
188+
* @return returns the current index the iterator is on
189+
*/
190+
inline constexpr size_t GetIndex() const
191+
{
192+
return index;
193+
}
194+
177195
private:
178196

179197
/**
@@ -270,6 +288,15 @@ class MIter
270288
return ptr->Data() != other.ptr->Data() && index != other.index;
271289
}
272290

291+
/**
292+
* Returns the index currently held by the iterator
293+
* @return returns the current index the iterator is on
294+
*/
295+
inline constexpr size_t GetIndex() const
296+
{
297+
return index;
298+
}
299+
273300
private:
274301

275302
/**
@@ -368,6 +395,15 @@ class CMIter
368395
return ptr->Data() != other.ptr->Data() && index != other.index;
369396
}
370397

398+
/**
399+
* Returns the index currently held by the iterator
400+
* @return returns the current index the iterator is on
401+
*/
402+
inline constexpr size_t GetIndex() const
403+
{
404+
return index;
405+
}
406+
371407
private:
372408

373409
/**

0 commit comments

Comments
 (0)