SpiecsEngine
 
Loading...
Searching...
No Matches

◆ BuildMaterial()

void Spices::Material::BuildMaterial ( bool isAutoRegistry = true)

This interface need to be overwritten by specific material. It defines how we build texture and descriptor set.

Parameters
[in]isAutoRegistryTrue if this material need registry to renderer automatically.
Todo
empty texture.

Registry to renderer if already build.

Create PipelineLayout.

Registry ShaderModule.

If ReBuild, need clear old descripotrset first.

Iter the constantParams and fill it's data to memoryblock.

Return if not valid textureParameter or constantParameter.

Create Material Parameter to store all address and index.

Registry texture to both ResourcePool, BindLessTextureManager, DescriptorSetManager and MaterialParameterBuffer.

Only work with Texture2D now.

Todo
more type support, reflection.

Instance a VkWriteDescriptorSet.

Update DescriptorSet.

Not supported format.

Create ConstantParameter Buffer .

Fill in data to memory block.

Create PipelineLayout.

Registry to renderer if already build.

Create PipelineLayout.

Registry ShaderModule.

If ReBuild, need clear old descripotrset first.

Iter the constantParams and fill it's data to memoryblock.

Return if not valid textureParameter or constantParameter.

Create Material Parameter to store all address and index.

Registry texture to both ResourcePool, BindLessTextureManager, DescriptorSetManager and MaterialParameterBuffer.

Only work with Texture2D now.

Todo
more type support, reflection.

Instance a VkWriteDescriptorSet.

Update DescriptorSet.

Not supported format.

Create ConstantParameter Buffer .

Fill in data to memory block.

Create PipelineLayout.

Definition at line 136 of file Material.cpp.

137 {
139
140 std::unique_lock<std::mutex> lock(m_Mutex);
141
145 {
146 SPICES_PROFILE_ZONEN("BuildMaterial::RegistryToRenderer");
147
148 if (m_AlreadyBuild)
149 {
153 {
154 std::vector<std::string> sv = StringLibrary::SplitString(m_MaterialPath, '.');
155 auto renderer = RendererManager::GetRenderer(sv[0]);
156 renderer->RegistryMaterial(m_MaterialPath, sv[1]);
157 }
158
159 return;
160 }
161
162 m_AlreadyBuild = true;
163 }
164
168 {
169 SPICES_PROFILE_ZONEN("BuildMaterial::Registry ShaderModule");
170
171 for (auto& pair : m_Shaders)
172 {
173 for (int i = 0; i < pair.second.size(); i++)
174 {
175 std::stringstream ss;
176 ss << pair.second[i] << "." << pair.first;
177
178 ResourcePool<Shader>::Load<Shader>(ss.str(), pair.second[i], pair.first);
179 }
180 }
181 }
182
186 {
187 SPICES_PROFILE_ZONEN("BuildMaterial::Clean Up old descripotrset");
188
191 }
192
196 {
197 SPICES_PROFILE_ZONEN("BuildMaterial::Build Local ConstantParameter Block");
198
199 m_ConstantParams.for_each([&](const std::string& k, const ConstantParams& v) {
200 m_Buffermemoryblocks.add_element(k, v.value.paramType);
201 return false;
202 });
203
205 }
206
210 uint64_t size = m_TextureParams.size() * sizeof(int) + m_Buffermemoryblocks.get_bytes();
211 if (size == 0)
212 {
213 if (isAutoRegistry)
214 {
215 const std::vector<std::string> sv = StringLibrary::SplitString(m_MaterialPath, '.');
216 auto renderer = RendererManager::GetRenderer(sv[0]);
217 renderer->RegistryMaterial(m_MaterialPath, sv[1]);
218 }
219
220 return;
221 }
222
226 {
227 SPICES_PROFILE_ZONEN("BuildMaterial::Create Material Parameter Buffer");
228
229 m_MaterialParameterBuffer = std::make_unique<VulkanBuffer>(
231 "MaterialParameterBuffer",
232 size,
233 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
234 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
235 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
236 );
237 }
238
242 {
243 SPICES_PROFILE_ZONEN("BuildMaterial::Registry texture");
244
245 int tindex = 0;
246 m_TextureParams.for_each([&](const std::string& k, TextureParam& v) {
247
252 if (v.textureType == "Texture2D")
253 {
254 if(v.texturePath == "")
255 {
256 v.index = -1;
257 }
258 else
259 {
260 const std::shared_ptr<Texture> texture = ResourcePool<Texture>::Load<Texture2D>(v.texturePath, v.texturePath);
261 v.index = BindLessTextureManager::Registry(v.texturePath);
262
263 const auto descriptorSet = DescriptorSetManager::Registry("PreRenderer", SpicesShader::BINDLESS_TEXTURE_SET);
264
268 VkWriteDescriptorSet write {};
269 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
270 write.dstBinding = SpicesShader::BINDLESS_TEXTURE_BINDING;
271 write.dstSet = descriptorSet->Get();
272 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
273 write.pImageInfo = texture->GetResource<VulkanImage>()->GetImageInfo();
274 write.descriptorCount = 1;
275 write.dstArrayElement = v.index;
276
280 vkUpdateDescriptorSets(VulkanRenderBackend::GetState().m_Device, 1, &write, 0, nullptr);
281 }
282
283 m_MaterialParameterBuffer->WriteToBuffer(&v.index, sizeof(int), tindex * sizeof(int));
285 }
286
290 else
291 {
292 SPICES_CORE_ERROR("Material::BuildMaterial(): Invalid textureType.");
293 }
294
295 tindex++;
296 return false;
297 });
298 }
299
303 {
304 SPICES_PROFILE_ZONEN("BuildMaterial::Add ConstantParameter Buffer");
305
306 m_Buffermemoryblocks.for_each([&](const std::string& name, void* pt) {
307 ConstantParam& ref = m_ConstantParams.find_value(name)->value;
308 size_t size = m_TextureParams.size() * sizeof(int) + m_Buffermemoryblocks.item_location(name);
309
313 if (ref.paramType == "float4")
314 {
315 *static_cast<glm::vec4*>(pt) = std::any_cast<glm::vec4>(ref.paramValue);
316 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(glm::vec4), size);
318 }
319 else if (ref.paramType == "float3")
320 {
321 *static_cast<glm::vec3*>(pt) = std::any_cast<glm::vec3>(ref.paramValue);
322 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(glm::vec3), size);
324 }
325 else if (ref.paramType == "float2")
326 {
327 *static_cast<glm::vec2*>(pt) = std::any_cast<glm::vec2>(ref.paramValue);
328 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(glm::vec2), size);
330 }
331 else if (ref.paramType == "float")
332 {
333 *static_cast<float*>(pt) = std::any_cast<float>(ref.paramValue);
334 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(float), size);
336 }
337 else if (ref.paramType == "int")
338 {
339 *static_cast<int*>(pt) = std::any_cast<int>(ref.paramValue);
340 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(int), size);
342 }
343 else if (ref.paramType == "bool")
344 {
345 *static_cast<bool*>(pt) = std::any_cast<bool>(ref.paramValue);
346 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(bool), size);
348 }
349 else
350 {
351 SPICES_CORE_ERROR("Material::BuildMaterial(): Invalid paramType.")
352 }
353
354 return false;
355 });
356 }
357
361 if (isAutoRegistry)
362 {
363 std::vector<std::string> sv = StringLibrary::SplitString(m_MaterialPath, '.');
364 auto renderer = RendererManager::GetRenderer(sv[0]);
365 renderer->RegistryMaterial(m_MaterialPath, sv[1]);
366 }
367 }
#define SPICES_PROFILE_ZONEN(...)
#define SPICES_PROFILE_ZONE
scl::linked_unordered_map< std::string, ConstantParams > m_ConstantParams
Constant parameters. Key: parameter name, Value: parameter value.
Definition Material.h:226
bool m_AlreadyBuild
True if this material already build a buffer.
Definition Material.h:252
std::unordered_map< std::string, std::vector< std::string > > m_Shaders
Shader path Key: shader usage, Value: shader file name.
Definition Material.h:212
scl::runtime_memory_block m_Buffermemoryblocks
m_Buffers's c++ data container. Key: set, Value: scl::runtime_memory_block.
Definition Material.h:232
scl::linked_unordered_map< std::string, TextureParam > m_TextureParams
Texture parameters. Key: parameter name, Value: parameter value.
Definition Material.h:219
std::mutex m_Mutex
Mutex of this material.
Definition Material.h:257
std::unique_ptr< VulkanBuffer > m_MaterialParameterBuffer
Definition Material.h:237
std::string m_MaterialPath
Definition Material.h:206
static std::shared_ptr< Renderer > GetRenderer(const std::string &name)
static std::vector< std::string > SplitString(const std::string &input, char delimiter)
Split a string to a string vector container use a char.
static VulkanState & GetState()
Get VulkanState in use.
void for_each(const std::function< bool(const std::string &name, void *pt)> &fn) const
Iter the object_ and call explain_element() to filling data.
void build()
Malloc a memory to begin_.
void add_element(const std::string &name, const std::string &type)
Add a element to object_, means a memory block will be occupied with given param type.
size_t item_location(const std::string &name)
Get item location in blocks.
size_t get_bytes() const
Get the bytes_.
The container is wrapper of a continue memory block. Used in Material::BuildMaterial(),...

References m_AlreadyBuild.