Skip to content

Commit 9adc7e9

Browse files
committed
Added move constructor to Pipeline
1 parent e458fe7 commit 9adc7e9

File tree

12 files changed

+179
-36
lines changed

12 files changed

+179
-36
lines changed

engine/render/assets/shaders/diffuseFragShader2.frag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct LightData
1919
vec3 position;
2020
};
2121

22-
layout (set = 0, binding = 1) uniform GlobalData {
22+
layout (binding = 1) uniform GlobalData {
2323
CameraData cameraData;
2424
LightData lightData;
2525
} globalData;

engine/render/assets/shaders/frag.frag

Whitespace-only changes.

engine/render/assets/shaders/simpleShader2.vert

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ struct LightData
2929
vec3 position;
3030
};
3131

32-
layout (std140, set = 0, binding = 0) readonly buffer ObjectBuffer {
32+
layout (std140, binding = 0) readonly buffer ObjectBuffer {
3333
ObjectData objects[];
3434
} objectBuffer;
3535

36-
layout (set = 0, binding = 1) uniform GlobalData {
36+
layout (binding = 1) uniform GlobalData {
3737
CameraData cameraData;
3838
LightData lightData;
3939
} globalData;

engine/render/assets/shaders/vert.vert

Whitespace-only changes.

engine/render/renderer/model/Model.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "../buffer/Buffer.h"
1818
#include "../material/Material.h"
1919
#include "../mesh/Mesh.h"
20+
#include "render/renderer/platform/vulkan/Material.h"
2021

2122
namespace Siege
2223
{
@@ -64,13 +65,24 @@ class Model
6465
{
6566
return material;
6667
}
68+
69+
Vulkan::Material* GetMaterial2()
70+
{
71+
return material2;
72+
}
73+
6774
void UpdateMesh(const Mesh::MeshData& meshData);
6875
void SetMesh(const Mesh::MeshData& meshData);
6976
void SetMaterial(Material* newMaterial)
7077
{
7178
material = newMaterial;
7279
}
7380

81+
void SetMaterial(Vulkan::Material* newMaterial)
82+
{
83+
material2 = newMaterial;
84+
}
85+
7486
bool IsIndexed()
7587
{
7688
return modelMesh.HasIndexBuffer();
@@ -82,6 +94,7 @@ class Model
8294

8395
Mesh modelMesh;
8496
Material* material {nullptr};
97+
Vulkan::Material* material2 {nullptr};
8598
};
8699
} // namespace Siege
87100

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

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,7 @@ Material::Material(Shader vertShader, Shader fragShader)
7171
1,
7272
&set.layout);
7373

74-
VkWriteDescriptorSet writes[set.properties.Count()];
75-
76-
for (size_t j = 0; j < set.properties.Count(); j++)
77-
{
78-
auto& property = set.properties[j];
79-
80-
VkDescriptorBufferInfo bufferInfo = Descriptor::CreateBufferInfo(buffer.buffer, property.offset, property.size);
81-
82-
writes[j] = Descriptor::CreateWriteSet(
83-
j,
84-
set.set,
85-
1,
86-
Utils::ToVkDescriptorType(property.type),
87-
&bufferInfo);
88-
}
89-
90-
Descriptor::WriteSets(Vulkan::Context::GetVkLogicalDevice(),
91-
writes,
92-
set.properties.Count());
74+
WriteSet(set);
9375
}
9476

9577
// Create Pipeline
@@ -98,7 +80,7 @@ Material::Material(Shader vertShader, Shader fragShader)
9880

9981
for(size_t i = 0; i < propertiesSlots.Count(); i++) layouts.Append(propertiesSlots[i].layout);
10082

101-
auto pipeline = Pipeline::Builder()
83+
graphicsPipeline = Pipeline::Builder()
10284
.WithRenderPass(Context::GetSwapchain().GetRenderPass())
10385
.WithDynamicViewport()
10486
.WithDynamicScissor()
@@ -128,6 +110,22 @@ Material& Material::operator=(Material&& other)
128110
return *this;
129111
}
130112

113+
void Material::SetUniformData(Hash::StringId id, uint64_t dataSize, const void* data)
114+
{
115+
// TODO: Clean this up.
116+
for (auto& slot : propertiesSlots)
117+
{
118+
for (auto& property : slot.properties)
119+
{
120+
if (property.id == id)
121+
{
122+
Buffer::CopyData(buffer, dataSize, data, property.offset);
123+
return;
124+
}
125+
}
126+
}
127+
}
128+
131129
void Material::AddShader(const Shader& shader, uint64_t& offset)
132130
{
133131
auto uniforms = shader.GetUniforms();
@@ -143,10 +141,11 @@ void Material::AddShader(const Shader& shader, uint64_t& offset)
143141

144142
for (size_t j = 0; j < slot.properties.Count(); j++)
145143
{
146-
if (slot.properties[j].id == uniform.id)
144+
auto& property = slot.properties[j];
145+
if (property.id == uniform.id)
147146
{
148-
propertyIdx = j;
149-
break;
147+
property.shaderStage = static_cast<Utils::ShaderType>(property.shaderStage | shader.GetShaderType());
148+
return;
150149
}
151150
}
152151

@@ -169,18 +168,78 @@ void Material::AddShader(const Shader& shader, uint64_t& offset)
169168
}
170169
}
171170

171+
void Material::Bind(const CommandBuffer& commandBuffer)
172+
{
173+
graphicsPipeline.Bind(commandBuffer);
174+
175+
// TODO: Cache this on the material itself?
176+
StackArray<VkDescriptorSet, 10> setsToBind;
177+
for (auto property : propertiesSlots) setsToBind.Append(property.set);
178+
179+
graphicsPipeline.BindSets(commandBuffer, setsToBind);
180+
}
181+
182+
// TODO: Remove the next function once we incorporate the CommandBuffer class into renderer
183+
void Material::Bind(VkCommandBuffer commandBuffer)
184+
{
185+
graphicsPipeline.Bind(commandBuffer);
186+
187+
// TODO: Cache this on the material itself?
188+
StackArray<VkDescriptorSet, 10> setsToBind;
189+
for (auto property : propertiesSlots) setsToBind.Append(property.set);
190+
191+
graphicsPipeline.BindSets(commandBuffer, setsToBind);
192+
}
193+
194+
void Material::UpdateMaterial()
195+
{
196+
for (size_t i = 0; i < propertiesSlots.Count(); i++) WriteSet(propertiesSlots[i]);
197+
}
198+
199+
void Material::WriteSet(PropertiesSlot& slot)
200+
{
201+
VkWriteDescriptorSet writes[slot.properties.Count()];
202+
203+
for (size_t j = 0; j < slot.properties.Count(); j++)
204+
{
205+
auto& property = slot.properties[j];
206+
207+
VkDescriptorBufferInfo bufferInfo = Descriptor::CreateBufferInfo(buffer.buffer, property.offset, property.size);
208+
209+
writes[j] = Descriptor::CreateWriteSet(
210+
j,
211+
slot.set,
212+
1,
213+
Utils::ToVkDescriptorType(property.type),
214+
&bufferInfo);
215+
}
216+
217+
Descriptor::WriteSets(Vulkan::Context::GetVkLogicalDevice(),
218+
writes,
219+
slot.properties.Count());
220+
}
221+
172222
void Material::Swap(Material& other)
173223
{
174224
auto tmpVertexShader = std::move(vertexShader);
175225
auto tmpFragmentShader = std::move(fragmentShader);
176226
auto tmpBufferSize = bufferSize;
227+
auto tmpBuffer = buffer;
228+
auto tmpGraphicsPipeline = std::move(graphicsPipeline);
229+
auto tmpPropertiesSlots = propertiesSlots;
177230

178231
vertexShader = std::move(other.vertexShader);
179232
fragmentShader = std::move(other.fragmentShader);
180233
bufferSize = other.bufferSize;
234+
buffer = other.buffer;
235+
graphicsPipeline = std::move(other.graphicsPipeline);
236+
propertiesSlots = other.propertiesSlots;
181237

182238
other.vertexShader = std::move(tmpVertexShader);
183239
other.fragmentShader = std::move(tmpFragmentShader);
184240
other.bufferSize = tmpBufferSize;
241+
other.buffer = tmpBuffer;
242+
other.graphicsPipeline = std::move(tmpGraphicsPipeline);
243+
other.propertiesSlots = tmpPropertiesSlots;
185244
}
186245
} // namespace Siege::Vulkan

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef SIEGE_ENGINE_VULKAN_MATERIAL_H
1010
#define SIEGE_ENGINE_VULKAN_MATERIAL_H
1111

12+
#include "CommandBuffer.h"
13+
#include "Pipeline.h"
1214
#include "Shader.h"
1315

1416
namespace Siege::Vulkan
@@ -23,6 +25,12 @@ class Material
2325
~Material();
2426

2527
Material& operator=(Material&& other);
28+
29+
void SetUniformData(Hash::StringId id, uint64_t dataSize, const void* data);
30+
31+
void Bind(const CommandBuffer& commandBuffer);
32+
// TODO: Remove the next function once we incorporate the CommandBuffer class into renderer
33+
void Bind(VkCommandBuffer commandBuffer);
2634
private:
2735

2836
struct Property
@@ -45,6 +53,11 @@ class Material
4553
void Swap(Material& other);
4654
void AddShader(const Shader& shader, uint64_t& offset);
4755

56+
void UpdateMaterial();
57+
void WriteSet(PropertiesSlot& slot);
58+
59+
Pipeline graphicsPipeline;
60+
4861
Shader vertexShader;
4962
Shader fragmentShader;
5063

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,45 @@ void Pipeline::Bind(const CommandBuffer& commandBuffer)
283283
{
284284
vkCmdBindPipeline(commandBuffer.Get(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
285285
}
286+
287+
void Pipeline::BindSets(const CommandBuffer& commandBuffer, StackArray<VkDescriptorSet, 10> sets)
288+
{
289+
vkCmdBindDescriptorSets(commandBuffer.Get(),
290+
VK_PIPELINE_BIND_POINT_GRAPHICS,
291+
layout,
292+
0,
293+
sets.Count(),
294+
sets.Data(),
295+
0,
296+
nullptr);
297+
}
298+
299+
// TODO: Remove the next two functions once we incorporate the CommandBuffer class into renderer
300+
void Pipeline::Bind(VkCommandBuffer commandBuffer)
301+
{
302+
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
303+
}
304+
void Pipeline::BindSets(VkCommandBuffer commandBuffer, StackArray<VkDescriptorSet, 10> sets)
305+
{
306+
vkCmdBindDescriptorSets(commandBuffer,
307+
VK_PIPELINE_BIND_POINT_GRAPHICS,
308+
layout,
309+
0,
310+
sets.Count(),
311+
sets.Data(),
312+
0,
313+
nullptr);
314+
}
315+
316+
void Pipeline::Swap(Pipeline& other)
317+
{
318+
auto tmpPipelineLayout = layout;
319+
auto tmpPipeline = pipeline;
320+
321+
layout = other.layout;
322+
pipeline = other.pipeline;
323+
324+
other.pipeline = tmpPipeline;
325+
other.layout = tmpPipelineLayout;
326+
}
286327
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,23 @@ class Pipeline
6666
};
6767

6868
Pipeline() = default;
69+
Pipeline(Pipeline&& other) noexcept { Swap(other); }
6970
~Pipeline();
7071

72+
Pipeline& operator=(Pipeline&& other) noexcept
73+
{
74+
Swap(other);
75+
return *this;
76+
}
77+
7178
void Bind(const CommandBuffer& commandBuffer);
79+
void BindSets(const CommandBuffer& commandBuffer, StackArray<VkDescriptorSet, 10> sets);
7280

81+
// TODO: Remove the next two functions once we incorporate the CommandBuffer class into renderer
82+
void Bind(VkCommandBuffer commandBuffer);
83+
void BindSets(VkCommandBuffer, StackArray<VkDescriptorSet, 10> sets);
7384
private:
85+
void Swap(Pipeline& other);
7486
VkPipelineLayout layout {nullptr};
7587
VkPipeline pipeline {nullptr};
7688
};

engine/render/renderer/renderer/ModelRenderer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void ModelRenderer::Initialise(const String& globalDataAttributeName,
1919
const uint64_t& globalDataSize)
2020
{
2121
globalDataId = INTERN_STR(globalDataAttributeName);
22-
transformId = INTERN_STR("objectBuffer");
22+
transformId = INTERN_STR("transforms");
2323
}
2424

2525
void ModelRenderer::Destroy() {}
@@ -46,14 +46,14 @@ void ModelRenderer::Render(VkCommandBuffer& commandBuffer,
4646
{
4747
auto& model = models.Get(i);
4848

49-
if (currentMaterial != model->GetMaterial())
49+
if (currentMaterial2 != model->GetMaterial2())
5050
{
51-
currentMaterial = model->GetMaterial();
52-
currentMaterial->SetUniformData(transformId,
51+
currentMaterial2 = model->GetMaterial2();
52+
currentMaterial2->SetUniformData(transformId,
5353
sizeof(transforms[0]) * transforms.Count(),
5454
transforms.Data());
55-
currentMaterial->SetUniformData(globalDataId, globalDataSize, globalData);
56-
currentMaterial->Bind(commandBuffer);
55+
currentMaterial2->SetUniformData(globalDataId, globalDataSize, globalData);
56+
currentMaterial2->Bind(commandBuffer);
5757
}
5858

5959
if (currentModel != model)
@@ -67,6 +67,7 @@ void ModelRenderer::Render(VkCommandBuffer& commandBuffer,
6767

6868
currentModel = nullptr;
6969
currentMaterial = nullptr;
70+
currentMaterial2 = nullptr;
7071
}
7172

7273
void ModelRenderer::Flush()

0 commit comments

Comments
 (0)