@@ -38,8 +38,9 @@ namespace Diligent
3838namespace
3939{
4040
41- WGPUTextureDescriptor TextureDescToWGPUTextureDescriptor (const TextureDesc& Desc,
42- RenderDeviceWebGPUImpl* pRenderDevice) noexcept
41+ WGPUTextureDescriptor TextureDescToWGPUTextureDescriptor (const TextureDesc& Desc,
42+ RenderDeviceWebGPUImpl* pRenderDevice,
43+ std::vector<WGPUTextureFormat>& TextureViewFormats) noexcept
4344{
4445 WGPUTextureDescriptor wgpuTextureDesc{};
4546
@@ -48,7 +49,7 @@ WGPUTextureDescriptor TextureDescToWGPUTextureDescriptor(const TextureDesc&
4849 if (Desc.Type == RESOURCE_DIM_TEX_CUBE_ARRAY)
4950 DEV_CHECK_ERR (Desc.ArraySize % 6 == 0 , " Cube texture arrays are expected to have a number of array slices that is a multiple of 6" );
5051
51- const auto & FmtInfo = pRenderDevice->GetTextureFormatInfoExt (SRGBFormatToUnorm (Desc.Format ));
52+ const TextureFormatInfoExt & FmtInfo = pRenderDevice->GetTextureFormatInfoExt (SRGBFormatToUnorm (Desc.Format ));
5253
5354 if (Desc.IsArray ())
5455 wgpuTextureDesc.size .depthOrArrayLayers = Desc.ArraySize ;
@@ -86,16 +87,53 @@ WGPUTextureDescriptor TextureDescToWGPUTextureDescriptor(const TextureDesc&
8687 LOG_ERROR_AND_THROW (" Automatic mipmap generation isn't supported for " , GetTextureFormatAttribs (Desc.Format ).Name , " as the format doesn't support linear filtering" );
8788 }
8889
89- if (IsSRGBFormat (Desc.Format ) && wgpuTextureDesc.usage & WGPUTextureUsage_StorageBinding)
90- wgpuTextureDesc.format = TextureFormatToWGPUFormat (SRGBFormatToUnorm (Desc.Format ));
91- else
92- wgpuTextureDesc.format = TextureFormatToWGPUFormat (Desc.Format );
90+ const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs (Desc.Format );
91+
92+ std::unordered_set<TEXTURE_FORMAT> ViewFormatSet;
93+ if (FmtAttribs.IsTypeless )
94+ {
95+ auto InsertViewFormat = [&](TEXTURE_VIEW_TYPE ViewType) {
96+ TEXTURE_FORMAT Format = GetDefaultTextureViewFormat (Desc, ViewType);
97+ ViewFormatSet.insert (Format);
98+ if (ViewType == TEXTURE_VIEW_RENDER_TARGET || ViewType == TEXTURE_VIEW_SHADER_RESOURCE)
99+ ViewFormatSet.insert (UnormFormatToSRGB (Format));
100+ };
101+
102+ if (Desc.BindFlags & BIND_DEPTH_STENCIL)
103+ LOG_ERROR_AND_THROW (" Depth-stencil textures must have a specific format and cannot be typeless in WebGPU" );
104+ if (Desc.BindFlags & BIND_UNORDERED_ACCESS)
105+ InsertViewFormat (TEXTURE_VIEW_UNORDERED_ACCESS);
106+ if (Desc.BindFlags & BIND_RENDER_TARGET)
107+ InsertViewFormat (TEXTURE_VIEW_RENDER_TARGET);
108+ if (Desc.BindFlags & BIND_SHADER_RESOURCE)
109+ InsertViewFormat (TEXTURE_VIEW_SHADER_RESOURCE);
110+ }
111+
112+ const bool IsSRGBWithMipsAndStorageBinding =
113+ IsSRGBFormat (Desc.Format ) &&
114+ (Desc.MiscFlags & MISC_TEXTURE_FLAG_GENERATE_MIPS) &&
115+ (wgpuTextureDesc.usage & WGPUTextureUsage_StorageBinding);
93116
94- wgpuTextureDesc.mipLevelCount = Desc.MipLevels ;
95- wgpuTextureDesc.sampleCount = Desc.SampleCount ;
96- wgpuTextureDesc.size .width = Desc.GetWidth ();
97- wgpuTextureDesc.size .height = Desc.GetHeight ();
98- wgpuTextureDesc.label = Desc.Name ;
117+ wgpuTextureDesc.format = IsSRGBWithMipsAndStorageBinding ?
118+ TextureFormatToWGPUFormat (SRGBFormatToUnorm (Desc.Format )) :
119+ TextureFormatToWGPUFormat (Desc.Format );
120+
121+ if (IsSRGBWithMipsAndStorageBinding)
122+ {
123+ ViewFormatSet.insert (Desc.Format );
124+ ViewFormatSet.insert (SRGBFormatToUnorm (Desc.Format ));
125+ }
126+
127+ for (const auto & TextureViewFmt : ViewFormatSet)
128+ TextureViewFormats.push_back (TextureFormatToWGPUFormat (TextureViewFmt));
129+
130+ wgpuTextureDesc.viewFormats = TextureViewFormats.data ();
131+ wgpuTextureDesc.viewFormatCount = TextureViewFormats.size ();
132+ wgpuTextureDesc.mipLevelCount = Desc.MipLevels ;
133+ wgpuTextureDesc.sampleCount = Desc.SampleCount ;
134+ wgpuTextureDesc.size .width = Desc.GetWidth ();
135+ wgpuTextureDesc.size .height = Desc.GetHeight ();
136+ wgpuTextureDesc.label = Desc.Name ;
99137
100138 return wgpuTextureDesc;
101139}
@@ -120,6 +158,8 @@ WGPUTextureViewDescriptor TextureViewDescToWGPUTextureViewDescriptor(const Textu
120158 wgpuTextureViewDesc.dimension = ResourceDimensionToWGPUTextureViewDimension (ViewDesc.TextureDim );
121159 wgpuTextureViewDesc.baseMipLevel = ViewDesc.MostDetailedMip ;
122160 wgpuTextureViewDesc.mipLevelCount = ViewDesc.NumMipLevels ;
161+ if (!(TexDesc.BindFlags & BIND_DEPTH_STENCIL))
162+ wgpuTextureViewDesc.format = TextureFormatToWGPUFormat (ViewDesc.Format );
123163
124164 if (IsTextureArray (ViewDesc))
125165 {
@@ -133,7 +173,6 @@ WGPUTextureViewDescriptor TextureViewDescToWGPUTextureViewDescriptor(const Textu
133173 }
134174
135175 const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs (ViewDesc.Format );
136-
137176 if (ViewDesc.ViewType == TEXTURE_VIEW_DEPTH_STENCIL || ViewDesc.ViewType == TEXTURE_VIEW_READ_ONLY_DEPTH_STENCIL)
138177 {
139178 if (FmtAttribs.ComponentType == COMPONENT_TYPE_DEPTH)
@@ -263,7 +302,8 @@ TextureWebGPUImpl::TextureWebGPUImpl(IReferenceCounters* pRefCounters,
263302
264303 if (m_Desc.Usage == USAGE_IMMUTABLE || m_Desc.Usage == USAGE_DEFAULT || m_Desc.Usage == USAGE_DYNAMIC)
265304 {
266- WGPUTextureDescriptor wgpuTextureDesc = TextureDescToWGPUTextureDescriptor (m_Desc, pDevice);
305+ std::vector<WGPUTextureFormat> TextureViewFormats;
306+ WGPUTextureDescriptor wgpuTextureDesc = TextureDescToWGPUTextureDescriptor (m_Desc, pDevice, TextureViewFormats);
267307 m_wgpuTexture.Reset (wgpuDeviceCreateTexture (pDevice->GetWebGPUDevice (), &wgpuTextureDesc));
268308 if (!m_wgpuTexture)
269309 LOG_ERROR_AND_THROW (" Failed to create WebGPU texture " , " '" , m_Desc.Name ? m_Desc.Name : " " , ' \' ' );
0 commit comments