@@ -71,25 +71,7 @@ Material::Material(Shader vertShader, Shader fragShader)
71
71
1 ,
72
72
&set.layout );
73
73
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);
93
75
}
94
76
95
77
// Create Pipeline
@@ -98,7 +80,7 @@ Material::Material(Shader vertShader, Shader fragShader)
98
80
99
81
for (size_t i = 0 ; i < propertiesSlots.Count (); i++) layouts.Append (propertiesSlots[i].layout );
100
82
101
- auto pipeline = Pipeline::Builder ()
83
+ graphicsPipeline = Pipeline::Builder ()
102
84
.WithRenderPass (Context::GetSwapchain ().GetRenderPass ())
103
85
.WithDynamicViewport ()
104
86
.WithDynamicScissor ()
@@ -128,6 +110,22 @@ Material& Material::operator=(Material&& other)
128
110
return *this ;
129
111
}
130
112
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
+
131
129
void Material::AddShader (const Shader& shader, uint64_t & offset)
132
130
{
133
131
auto uniforms = shader.GetUniforms ();
@@ -143,10 +141,11 @@ void Material::AddShader(const Shader& shader, uint64_t& offset)
143
141
144
142
for (size_t j = 0 ; j < slot.properties .Count (); j++)
145
143
{
146
- if (slot.properties [j].id == uniform.id )
144
+ auto & property = slot.properties [j];
145
+ if (property.id == uniform.id )
147
146
{
148
- propertyIdx = j ;
149
- break ;
147
+ property. shaderStage = static_cast <Utils::ShaderType>(property. shaderStage | shader. GetShaderType ()) ;
148
+ return ;
150
149
}
151
150
}
152
151
@@ -169,18 +168,78 @@ void Material::AddShader(const Shader& shader, uint64_t& offset)
169
168
}
170
169
}
171
170
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
+
172
222
void Material::Swap (Material& other)
173
223
{
174
224
auto tmpVertexShader = std::move (vertexShader);
175
225
auto tmpFragmentShader = std::move (fragmentShader);
176
226
auto tmpBufferSize = bufferSize;
227
+ auto tmpBuffer = buffer;
228
+ auto tmpGraphicsPipeline = std::move (graphicsPipeline);
229
+ auto tmpPropertiesSlots = propertiesSlots;
177
230
178
231
vertexShader = std::move (other.vertexShader );
179
232
fragmentShader = std::move (other.fragmentShader );
180
233
bufferSize = other.bufferSize ;
234
+ buffer = other.buffer ;
235
+ graphicsPipeline = std::move (other.graphicsPipeline );
236
+ propertiesSlots = other.propertiesSlots ;
181
237
182
238
other.vertexShader = std::move (tmpVertexShader);
183
239
other.fragmentShader = std::move (tmpFragmentShader);
184
240
other.bufferSize = tmpBufferSize;
241
+ other.buffer = tmpBuffer;
242
+ other.graphicsPipeline = std::move (tmpGraphicsPipeline);
243
+ other.propertiesSlots = tmpPropertiesSlots;
185
244
}
186
245
} // namespace Siege::Vulkan
0 commit comments