Skip to content

Commit c60097c

Browse files
authored
Merge pull request #32 from CapsCollective/feature/remove-foreach
Remove ForEach functions from arrays
2 parents f4b1f08 + d0f7da3 commit c60097c

File tree

13 files changed

+173
-935
lines changed

13 files changed

+173
-935
lines changed

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

Lines changed: 81 additions & 46 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,20 +105,23 @@ 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

104-
void Material::Destroy()
114+
void Material::Free()
105115
{
106116
auto device = Vulkan::Context::GetVkLogicalDevice();
107117

108118
Buffer::DestroyBuffer(buffer);
109119

110-
propertiesSlots.MForEach([&](PropertiesSlot& prop) {
111-
vkDestroyDescriptorSetLayout(device, prop.layout, nullptr);
112-
prop.layout = nullptr;
113-
});
120+
for (auto it = propertiesSlots.CreateFIterator(); it; ++it)
121+
{
122+
vkDestroyDescriptorSetLayout(device, it->layout, nullptr);
123+
it->layout = nullptr;
124+
}
114125

115126
vertexShader.Destroy();
116127
fragmentShader.Destroy();
@@ -151,26 +162,34 @@ uint32_t Material::SetTexture(Hash::StringId id, Texture2D* texture)
151162
texIndex = textureIds.Count();
152163
textureIds.Append(texture->GetId());
153164

154-
propertiesSlots.MForEachI([&](PropertiesSlot& slot, size_t i) {
165+
for (auto it = propertiesSlots.CreateIterator(); it; ++it)
166+
{
167+
auto& slot = *it;
155168
auto& properties = slot.properties;
169+
156170
auto propIdx = FindPropertyIndex(id, slot);
157171

158-
if (propIdx == -1) return;
172+
if (propIdx == -1) continue;
159173

160174
auto info = texture->GetInfo();
161175

162176
texture2DInfos[texIndex] = {info.sampler,
163177
info.imageInfo.view,
164178
ToVkImageLayout(info.imageInfo.layout)};
165179

166-
if (writeSets.Count() > 0) return;
180+
if (writeSets.Count() > 0) break;
167181

168182
auto prop = properties[propIdx];
169183

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

175194
return texIndex;
176195
}
@@ -238,16 +257,19 @@ void Material::Bind(const CommandBuffer& commandBuffer)
238257
graphicsPipeline.BindSets(commandBuffer, perFrameDescriptorSets[frameIndex]);
239258
}
240259

241-
void Material::BindPushConstant(const CommandBuffer& buffer, const void* values)
260+
void Material::BindPushConstant(const CommandBuffer& commandBuffer, const void* values)
242261
{
243-
graphicsPipeline.PushConstants(buffer, pushConstant.type, pushConstant.size, values);
262+
graphicsPipeline.PushConstants(commandBuffer, pushConstant.type, pushConstant.size, values);
244263
}
245264

246265
void Material::Recreate()
247266
{
248267
MSArray<VkDescriptorSetLayout, 10> layouts;
249268

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

252274
graphicsPipeline = Pipeline::Builder()
253275
.WithRenderPass(Context::GetSwapchain().GetRenderPass())
@@ -273,14 +295,15 @@ void Material::Update()
273295

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

279302
if (propertyIndex == -1) return;
280303

281304
auto& setsToWrite = writes[Renderer::GetCurrentFrameIndex()];
282305
Write(setsToWrite);
283-
});
306+
}
284307
}
285308

286309
void Material::WriteSet(uint32_t set, PropertiesSlot& slot)
@@ -290,13 +313,23 @@ void Material::WriteSet(uint32_t set, PropertiesSlot& slot)
290313

291314
auto& properties = slot.properties;
292315

293-
properties.MForEachI([&](Property& prop, size_t i) {
316+
for (auto propIt = properties.CreateIterator(); propIt; ++propIt)
317+
{
318+
auto prop = *propIt;
294319
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-
});
320+
for (auto setIt = perFrameDescriptorSets.CreateFIterator(); setIt; ++setIt)
321+
{
322+
auto sets = *setIt;
323+
if (IsTexture2D(prop.type))
324+
QueueImageUpdate(writes[setIt.GetIndex()], sets[set], propIt.GetIndex());
325+
else
326+
QueuePropertyUpdate(writes[setIt.GetIndex()],
327+
sets[set],
328+
prop.type,
329+
propIt.GetIndex(),
330+
1);
331+
}
332+
}
300333
}
301334

302335
void Material::Write(MSArray<VkWriteDescriptorSet, 10>& sets)
@@ -337,26 +370,28 @@ void Material::QueuePropertyUpdate(MSArray<VkWriteDescriptorSet, 10>& writeQueue
337370
int32_t Material::FindPropertyIndex(Hash::StringId id, PropertiesSlot& slot)
338371
{
339372
int32_t foundIdx {-1};
340-
slot.properties.MForEachI([&](Property& property, size_t i) {
341-
if (property.id == id)
373+
for (auto it = slot.properties.CreateIterator(); it; ++it)
374+
{
375+
if (it->id == id)
342376
{
343-
foundIdx = i;
344-
return;
377+
foundIdx = it.GetIndex();
378+
break;
345379
}
346-
});
380+
}
347381
return foundIdx;
348382
}
349383

350384
int32_t Material::FindTextureIndex(Hash::StringId id)
351385
{
352386
int32_t texExists = -1;
353-
textureIds.MForEachI([&](Hash::StringId& texId, size_t i) {
354-
if (id == texId)
387+
for (auto it = textureIds.CreateIterator(); it; ++it)
388+
{
389+
if (id == *it)
355390
{
356-
texExists = i;
357-
return;
391+
texExists = it.GetIndex();
392+
break;
358393
}
359-
});
394+
}
360395
return texExists;
361396
}
362397

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Material
6363
*/
6464
~Material();
6565

66-
void Destroy();
66+
void Free();
6767

6868
/**
6969
* A movement assignment operator
@@ -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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,29 @@ 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& binding = *vertIt;
106+
inputDescriptions[vertIt.GetIndex()] = {static_cast<uint32_t>(vertIt.GetIndex()),
107+
binding.stride,
108+
VK_VERTEX_INPUT_RATE_VERTEX};
106109

107110
auto& attributes = binding.attributes;
108111

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

112-
attributeDescriptions[attributeIndex] = {static_cast<uint32_t>(j),
113-
static_cast<uint32_t>(i),
117+
attributeDescriptions[attributeIndex] = {static_cast<uint32_t>(attrIt.GetIndex()),
118+
static_cast<uint32_t>(vertIt.GetIndex()),
114119
Utils::ToVkFormat(vertex.type),
115120
vertex.offset};
116-
});
121+
}
117122

118123
processedAttributes += attributes.Count();
119-
});
124+
}
120125

121126
auto vertexInputCreateInfo =
122127
CreateVertexInputInfo(static_cast<uint32_t>(vertexShader->GetTotalAttributeCount()),

engine/render/renderer/renderer/DebugRenderer3D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void DebugRenderer3D::Initialise(const String& globalDataAttributeName,
3939

4040
void DebugRenderer3D::Destroy()
4141
{
42-
lineMaterial.Destroy();
42+
lineMaterial.Free();
4343
lineModel.DestroyModel();
4444
}
4545

engine/render/renderer/renderer/LightRenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void LightRenderer::Initialise(const String& globalDataAttributeName,
3535

3636
void LightRenderer::Destroy()
3737
{
38-
lightMaterial.Destroy();
38+
lightMaterial.Free();
3939
lightModel.DestroyModel();
4040
}
4141

engine/render/renderer/renderer/Renderer2D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void Renderer2D::DestroyRenderer2D()
102102
{
103103
quadModel.DestroyModel();
104104
defaultTexture.Free();
105-
defaultMaterial.Destroy();
105+
defaultMaterial.Free();
106106
}
107107

108108
void Renderer2D::Flush()

engine/utils/collections/ArrayUtils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include <initializer_list>
1919
#include <utility>
2020

21-
#define LAMBDA(...) [&](__VA_ARGS__)
22-
2321
namespace Siege
2422
{
2523
class ArrayUtils

0 commit comments

Comments
 (0)