SpiecsEngine
 
Loading...
Searching...
No Matches

◆ UpdateMaterial()

void Spices::Material::UpdateMaterial ( )

Update material data to buffer.

Registry ShaderModule.

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

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.

Registry ShaderModule.

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

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.

Definition at line 369 of file Material.cpp.

370 {
372
373 std::unique_lock<std::mutex> lock(m_Mutex);
374
378 {
379 SPICES_PROFILE_ZONEN("BuildMaterial::Registry ShaderModule");
380
381 for (auto& pair : m_Shaders)
382 {
383 for (int i = 0; i < pair.second.size(); i++)
384 {
385 std::stringstream ss;
386 ss << pair.first << "." << pair.second[i];
387
388 ResourcePool<Shader>::Load<Shader>(ss.str(), pair.second[i], pair.first);
389 }
390 }
391 }
392
396 {
397 SPICES_PROFILE_ZONEN("BuildMaterial::Build Local ConstantParameter Block");
398
400
401 m_ConstantParams.for_each([&](const std::string& k, const ConstantParams& v) {
402 m_Buffermemoryblocks.add_element(k, v.value.paramType);
403 return false;
404 });
405
407 }
408
412 {
413 SPICES_PROFILE_ZONEN("BuildMaterial::Registry texture");
414
415 int tindex = 0;
416 m_TextureParams.for_each([&](const std::string& k, TextureParam& v) {
417
422 if (v.textureType == "Texture2D")
423 {
424 if (v.texturePath == "")
425 {
426 v.index = -1;
427 }
428 else
429 {
430 std::shared_ptr<Texture> texture = ResourcePool<Texture>::Load<Texture2D>(v.texturePath, v.texturePath);
431 v.index = BindLessTextureManager::Registry(v.texturePath);
432
433 auto descriptorSet = DescriptorSetManager::Registry("PreRenderer", SpicesShader::BINDLESS_TEXTURE_SET);
434
438 VkWriteDescriptorSet write {};
439 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
440 write.dstBinding = SpicesShader::BINDLESS_TEXTURE_BINDING;
441 write.dstSet = descriptorSet->Get();
442 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
443 write.pImageInfo = texture->GetResource<VulkanImage>()->GetImageInfo();
444 write.descriptorCount = 1;
445 write.dstArrayElement = v.index;
446
450 vkUpdateDescriptorSets(VulkanRenderBackend::GetState().m_Device, 1, &write, 0, nullptr);
451 }
452
453 m_MaterialParameterBuffer->WriteToBuffer(&v.index, sizeof(int), tindex * sizeof(int));
455 }
456
460 else
461 {
462 SPICES_CORE_ERROR("Material::BuildMaterial(): Invalid textureType.")
463 }
464
465 tindex++;
466 return false;
467 });
468 }
469
473 {
474 SPICES_PROFILE_ZONEN("BuildMaterial::Add ConstantParameter Buffer");
475
476 m_Buffermemoryblocks.for_each([&](const std::string& name, void* pt) {
477 ConstantParam& ref = m_ConstantParams.find_value(name)->value;
478 size_t size = m_TextureParams.size() * sizeof(int) + m_Buffermemoryblocks.item_location(name);
479
483 if (ref.paramType == "float4")
484 {
485 *static_cast<glm::vec4*>(pt) = std::any_cast<glm::vec4>(ref.paramValue);
486 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(glm::vec4), size);
488 }
489 else if (ref.paramType == "float3")
490 {
491 *static_cast<glm::vec3*>(pt) = std::any_cast<glm::vec3>(ref.paramValue);
492 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(glm::vec3), size);
494 }
495 else if (ref.paramType == "float2")
496 {
497 *static_cast<glm::vec2*>(pt) = std::any_cast<glm::vec2>(ref.paramValue);
498 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(glm::vec2), size);
500 }
501 else if (ref.paramType == "float")
502 {
503 *static_cast<float*>(pt) = std::any_cast<float>(ref.paramValue);
504 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(float), size);
506 }
507 else if (ref.paramType == "int")
508 {
509 *static_cast<int*>(pt) = std::any_cast<int>(ref.paramValue);
510 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(int), size);
512 }
513 else if (ref.paramType == "bool")
514 {
515 *static_cast<bool*>(pt) = std::any_cast<bool>(ref.paramValue);
516 m_MaterialParameterBuffer->WriteToBuffer(pt, sizeof(bool), size);
518 }
519 else
520 {
521 SPICES_CORE_ERROR("Material::BuildMaterial(): Invalid paramType.")
522 }
523
524 return false;
525 });
526 }
527 }
#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
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
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.
The container is wrapper of a continue memory block. Used in Material::BuildMaterial(),...