git.s-ol.nu ~forks/DiligentFX / d6ac3dc
GLTF PBR renderer: updated material shader attribs handling assiduous 10 months ago
4 changed file(s) with 30 addition(s) and 81 deletion(s). Raw diff Collapse all Expand all
183183 GLTFNodeShaderTransforms ShaderTransforms;
184184
185185 /// GLTF material shader information
186 GLTFMaterialShaderInfo MaterialShaderInfo;
186 GLTF::Material::ShaderAttribs MaterialShaderInfo;
187187
188188 /// Index type for indexed primitives, or VT_UNDEFINED for non-indexed ones
189189 VALUE_TYPE IndexType = VT_UNDEFINED;
245245 Macros.AddShaderMacro("GLTF_PBR_USE_AO", m_Settings.UseAO);
246246 Macros.AddShaderMacro("GLTF_PBR_USE_EMISSIVE", m_Settings.UseEmissive);
247247 Macros.AddShaderMacro("USE_TEXTURE_ATLAS", m_Settings.UseTextureAtals);
248 Macros.AddShaderMacro("PBR_WORKFLOW_METALLIC_ROUGHNESS", GLTF::Material::PBR_WORKFLOW_METALL_ROUGH);
249 Macros.AddShaderMacro("PBR_WORKFLOW_SPECULAR_GLOSINESS", GLTF::Material::PBR_WORKFLOW_SPEC_GLOSS);
248250 ShaderCI.Macros = Macros;
249251 RefCntAutoPtr<IShader> pVS;
250252 {
424426 {
425427 ITextureView* pTexSRV = nullptr;
426428
427 auto TexIdx = Material.Textures[TexId].Index;
429 auto TexIdx = Material.TextureIds[TexId];
428430 if (TexIdx >= 0)
429431 {
430432 if (auto* pTexture = Model.GetTexture(TexIdx, nullptr, nullptr))
438440 pVar->Set(pTexSRV);
439441 };
440442
441 GLTF::Material::TEXTURE_ID BaseColorTexId = GLTF::Material::TEXTURE_ID_BASE_COLOR;
442 GLTF::Material::TEXTURE_ID PhysDescTexId = GLTF::Material::TEXTURE_ID_METALL_ROUGHNESS;
443 if (Material.workflow == GLTF::Material::PbrWorkflow::MetallicRoughness)
444 {
445 }
446 else if (Material.workflow == GLTF::Material::PbrWorkflow::SpecularGlossiness)
447 {
448 BaseColorTexId = GLTF::Material::TEXTURE_ID_DIFFUSE;
449 PhysDescTexId = GLTF::Material::TEXTURE_ID_SPEC_GLOSS;
450 }
451 else
452 {
453 UNEXPECTED("Unexpected workflow");
454 }
455
456 SetTexture(BaseColorTexId, m_pWhiteTexSRV, "g_ColorMap");
457 SetTexture(PhysDescTexId, m_pWhiteTexSRV, "g_PhysicalDescriptorMap");
443 SetTexture(GLTF::Material::TEXTURE_ID_BASE_COLOR, m_pWhiteTexSRV, "g_ColorMap");
444 SetTexture(GLTF::Material::TEXTURE_ID_PHYSICAL_DESC, m_pWhiteTexSRV, "g_PhysicalDescriptorMap");
458445 SetTexture(GLTF::Material::TEXTURE_ID_NORMAL_MAP, m_pDefaultNormalMapSRV, "g_NormalMap");
459446 if (m_Settings.UseAO)
460447 {
816803 }
817804
818805 {
819 GLTFMaterialShaderInfo* pMaterialInfo = nullptr;
806 GLTF::Material::ShaderAttribs* pMaterialAttribs = nullptr;
820807 if (RenderNodeCallback == nullptr)
821808 {
822809 struct GLTFAttribs
823810 {
824 GLTFRendererShaderParameters RenderParameters;
825 GLTFMaterialShaderInfo MaterialInfo;
811 GLTFRendererShaderParameters RenderParameters;
812 GLTF::Material::ShaderAttribs MaterialInfo;
813 static_assert(sizeof(GLTFMaterialShaderInfo) == sizeof(GLTF::Material::ShaderAttribs),
814 "The sizeof(GLTFMaterialShaderInfo) is incosistent with sizeof(GLTF::Material::ShaderAttribs)");
826815 };
827816 static_assert(sizeof(GLTFAttribs) <= 256, "Size of dynamic GLTFAttribs buffer exceeds 256 bytes. "
828817 "It may be worth trying to reduce the size or just live with it.");
829818
830819 GLTFAttribs* pGLTFAttribs = nullptr;
831820 pCtx->MapBuffer(Renderer.m_GLTFAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD, reinterpret_cast<PVoid&>(pGLTFAttribs));
832 pMaterialInfo = &pGLTFAttribs->MaterialInfo;
821 pMaterialAttribs = &pGLTFAttribs->MaterialInfo;
833822
834823 auto& ShaderParams = pGLTFAttribs->RenderParameters;
835824
844833 }
845834 else
846835 {
847 pMaterialInfo = &NodeRI.MaterialShaderInfo;
848 }
849
850 pMaterialInfo->EmissiveFactor = material.EmissiveFactor;
851
852 pMaterialInfo->NormalTextureUVSelector = material.Textures[GLTF::Material::TEXTURE_ID_NORMAL_MAP].CoordSet;
853 pMaterialInfo->OcclusionTextureUVSelector = material.Textures[GLTF::Material::TEXTURE_ID_OCCLUSION].CoordSet;
854 pMaterialInfo->EmissiveTextureUVSelector = material.Textures[GLTF::Material::TEXTURE_ID_EMISSIVE].CoordSet;
855
856 pMaterialInfo->UseAlphaMask = material.AlphaMode == GLTF::Material::ALPHAMODE_MASK ? 1 : 0;
857 pMaterialInfo->AlphaMaskCutoff = material.AlphaCutoff;
858
859 auto GetUVScaleBias = [&](GLTF::Material::TEXTURE_ID TexId) //
860 {
861 auto TexIndex = material.Textures[TexId].Index;
862 return (TexIndex >= 0) ?
863 GLTFModel.GetUVScaleBias(TexIndex) :
864 float4{1, 1, 0, 0};
865 };
866
867 pMaterialInfo->NormalMapUVScaleBias = GetUVScaleBias(GLTF::Material::TEXTURE_ID_NORMAL_MAP);
868 pMaterialInfo->OcclusionUVScaleBias = GetUVScaleBias(GLTF::Material::TEXTURE_ID_OCCLUSION);
869 pMaterialInfo->EmissiveUVScaleBias = GetUVScaleBias(GLTF::Material::TEXTURE_ID_EMISSIVE);
870
871 GLTF::Material::TEXTURE_ID BaseColorTexId = GLTF::Material::TEXTURE_ID_BASE_COLOR;
872 GLTF::Material::TEXTURE_ID PhysDescTexId = GLTF::Material::TEXTURE_ID_METALL_ROUGHNESS;
873 // TODO: glTF specs states that metallic roughness should be preferred, even if specular glosiness is present
874 if (material.workflow == GLTF::Material::PbrWorkflow::MetallicRoughness)
875 {
876 // Metallic roughness workflow
877 pMaterialInfo->Workflow = PBR_WORKFLOW_METALLIC_ROUGHNESS;
878 pMaterialInfo->MetallicFactor = material.MetallicFactor;
879 pMaterialInfo->RoughnessFactor = material.RoughnessFactor;
880 pMaterialInfo->BaseColorFactor = material.BaseColorFactor;
881 }
882 else if (material.workflow == GLTF::Material::PbrWorkflow::SpecularGlossiness)
883 {
884 // Specular glossiness workflow
885 pMaterialInfo->Workflow = PBR_WORKFLOW_SPECULAR_GLOSINESS;
886 pMaterialInfo->SpecularFactor = material.SpecularFactor;
887 pMaterialInfo->BaseColorFactor = material.DiffuseFactor;
888
889 BaseColorTexId = GLTF::Material::TEXTURE_ID_DIFFUSE;
890 PhysDescTexId = GLTF::Material::TEXTURE_ID_SPEC_GLOSS;
891 }
892 else
893 {
894 UNEXPECTED("Unexpected workflow");
895 }
896
897 pMaterialInfo->BaseColorTextureUVSelector = material.Textures[BaseColorTexId].CoordSet;
898 pMaterialInfo->PhysicalDescriptorTextureUVSelector = material.Textures[PhysDescTexId].CoordSet;
899 pMaterialInfo->BaseColorUVScaleBias = GetUVScaleBias(BaseColorTexId);
900 pMaterialInfo->PhysicalDescriptorUVScaleBias = GetUVScaleBias(PhysDescTexId);
836 pMaterialAttribs = &NodeRI.MaterialShaderInfo;
837 }
838
839 *pMaterialAttribs = material.Attribs;
901840
902841 if (RenderNodeCallback == nullptr)
903842 {
1515 # define MAX_NUM_JOINTS 128u
1616 #endif
1717
18 #define PBR_WORKFLOW_METALLIC_ROUGHNESS 0
19 #define PBR_WORKFLOW_SPECULAR_GLOSINESS 1
18 #ifndef PBR_WORKFLOW_METALLIC_ROUGHNESS
19 # define PBR_WORKFLOW_METALLIC_ROUGHNESS 0
20 #endif
21
22 #ifndef PBR_WORKFLOW_SPECULAR_GLOSINESS
23 # define PBR_WORKFLOW_SPECULAR_GLOSINESS 1
24 #endif
2025
2126 struct GLTFNodeShaderTransforms
2227 {
1515 "# define MAX_NUM_JOINTS 128u\n"
1616 "#endif\n"
1717 "\n"
18 "#define PBR_WORKFLOW_METALLIC_ROUGHNESS 0\n"
19 "#define PBR_WORKFLOW_SPECULAR_GLOSINESS 1\n"
18 "#ifndef PBR_WORKFLOW_METALLIC_ROUGHNESS\n"
19 "# define PBR_WORKFLOW_METALLIC_ROUGHNESS 0\n"
20 "#endif\n"
21 "\n"
22 "#ifndef PBR_WORKFLOW_SPECULAR_GLOSINESS\n"
23 "# define PBR_WORKFLOW_SPECULAR_GLOSINESS 1\n"
24 "#endif\n"
2025 "\n"
2126 "struct GLTFNodeShaderTransforms\n"
2227 "{\n"