@@ -129,6 +129,59 @@ GLTF_PBR_Renderer::GLTF_PBR_Renderer(IRenderDevice* pDevice,
129129 }
130130}
131131
132+ static RefCntAutoPtr<ITextureView> GetPBRTextureSRV (ITexture* pTexture,
133+ PBR_Renderer::TEXTURE_ATTRIB_ID ID,
134+ PBR_Renderer::CreateInfo::TEX_COLOR_CONVERSION_MODE ConversionMode)
135+ {
136+ if (pTexture == nullptr )
137+ {
138+ UNEXPECTED (" Texture is null" );
139+ return {};
140+ }
141+
142+ const TextureDesc& TexDesc = pTexture->GetDesc ();
143+ TEXTURE_FORMAT ViewFmt = TexDesc.Format ;
144+ static_assert (PBR_Renderer::TEXTURE_ATTRIB_ID_COUNT == 17 , " Did you add a new texture attribute? It may need to be handled here." );
145+ if ((ConversionMode == PBR_Renderer::CreateInfo::TEX_COLOR_CONVERSION_MODE_NONE) &&
146+ (ID == PBR_Renderer::TEXTURE_ATTRIB_ID_BASE_COLOR ||
147+ ID == PBR_Renderer::TEXTURE_ATTRIB_ID_EMISSIVE ||
148+ ID == PBR_Renderer::TEXTURE_ATTRIB_ID_SHEEN_COLOR))
149+ {
150+ const TextureFormatAttribs& FmtInfo = GetTextureFormatAttribs (ViewFmt);
151+ if (FmtInfo.ComponentType != COMPONENT_TYPE_UNORM_SRGB)
152+ {
153+ if (FmtInfo.IsTypeless )
154+ {
155+ ViewFmt = TypelessFormatToSRGB (ViewFmt);
156+ }
157+ else
158+ {
159+ LOG_WARNING_MESSAGE (" Unable to create sRGB view for texture '" , pTexture->GetDesc ().Name ,
160+ " ' as its format (" , FmtInfo.Name ,
161+ " ) is not typeless. Expect images to be too bright. To fix this, either use "
162+ " TEX_COLOR_CONVERSION_MODE_SRGB_TO_LINEAR or use typeless format for the texture." );
163+ }
164+ }
165+ }
166+
167+ RefCntAutoPtr<ITextureView> pTexSRV;
168+
169+ if (TexDesc.Type == RESOURCE_DIM_TEX_2D_ARRAY && TexDesc.Format == ViewFmt)
170+ {
171+ pTexSRV = pTexture->GetDefaultView (TEXTURE_VIEW_SHADER_RESOURCE);
172+ }
173+ else
174+ {
175+ TextureViewDesc SRVDesc;
176+ SRVDesc.ViewType = TEXTURE_VIEW_SHADER_RESOURCE;
177+ SRVDesc.TextureDim = RESOURCE_DIM_TEX_2D_ARRAY;
178+ SRVDesc.Format = ViewFmt;
179+ pTexture->CreateView (SRVDesc, &pTexSRV);
180+ }
181+
182+ return pTexSRV;
183+ }
184+
132185void GLTF_PBR_Renderer::InitMaterialSRB (GLTF::Model& Model,
133186 GLTF::Material& Material,
134187 IBuffer* pFrameAttribs,
@@ -158,15 +211,7 @@ void GLTF_PBR_Renderer::InitMaterialSRB(GLTF::Model& Model,
158211 {
159212 if (ITexture* pTexture = Model.GetTexture (TexIdx))
160213 {
161- if (pTexture->GetDesc ().Type == RESOURCE_DIM_TEX_2D_ARRAY)
162- pTexSRV = pTexture->GetDefaultView (TEXTURE_VIEW_SHADER_RESOURCE);
163- else
164- {
165- TextureViewDesc SRVDesc;
166- SRVDesc.ViewType = TEXTURE_VIEW_SHADER_RESOURCE;
167- SRVDesc.TextureDim = RESOURCE_DIM_TEX_2D_ARRAY;
168- pTexture->CreateView (SRVDesc, &pTexSRV);
169- }
214+ pTexSRV = GetPBRTextureSRV (pTexture, ID, m_Settings.TexColorConversionMode );
170215 }
171216 }
172217
@@ -249,7 +294,14 @@ void GLTF_PBR_Renderer::CreateResourceCacheSRB(IRenderDevice* pDevice,
249294 TEXTURE_FORMAT Fmt = CacheUseInfo.AtlasFormats [ID];
250295 if (ITexture* pTexture = CacheUseInfo.pResourceMgr ->UpdateTexture (Fmt, pDevice, pCtx))
251296 {
252- this ->SetMaterialTexture (pSRB, pTexture->GetDefaultView (TEXTURE_VIEW_SHADER_RESOURCE), ID);
297+ if (RefCntAutoPtr<ITextureView> pTexSRV = GetPBRTextureSRV (pTexture, ID, m_Settings.TexColorConversionMode ))
298+ {
299+ this ->SetMaterialTexture (pSRB, pTexSRV, ID);
300+ }
301+ else
302+ {
303+ UNEXPECTED (" Failed to get SRV for atlas '" , pTexture->GetDesc ().Name , " '" );
304+ }
253305 }
254306 };
255307
0 commit comments