git.s-ol.nu ~forks/DiligentCore / fb6e702
Reworked samplers handling in OpenGL assiduous 6 months ago
5 changed file(s) with 111 addition(s) and 112 deletion(s). Raw diff Collapse all Expand all
130130 // Copies static resources from the static resource cache to the destination cache
131131 void CopyStaticResources(ShaderResourceCacheGL& ResourceCache) const;
132132
133 Uint32 GetImmutableSamplerIdx(const ResourceAttribs& Res) const
134 {
135 auto ImtblSamIdx = InvalidImmutableSamplerIndex;
136 if (Res.IsImmutableSamplerAssigned())
137 ImtblSamIdx = Res.SamplerInd;
138 else if (Res.IsSamplerAssigned())
139 {
140 VERIFY_EXPR(GetResourceDesc(Res.SamplerInd).ResourceType == SHADER_RESOURCE_TYPE_SAMPLER);
141 const auto& SamAttribs = GetResourceAttribs(Res.SamplerInd);
142 if (SamAttribs.IsImmutableSamplerAssigned())
143 ImtblSamIdx = SamAttribs.SamplerInd;
144 }
145 VERIFY_EXPR(ImtblSamIdx == InvalidImmutableSamplerIndex || ImtblSamIdx < GetImmutableSamplerCount());
146 return ImtblSamIdx;
147 }
148
133149 private:
134 void CreateLayouts();
150 void CreateLayout();
135151
136152 void Destruct();
137153
156156 GetSSBO(CacheOffset).pBufferView = std::move(pBuffView);
157157 }
158158
159 void CopyTexture(Uint32 Binding, const CachedResourceView& SrcSam)
160 {
161 GetTexture(Binding) = SrcSam;
162 }
163
164 void CopyImage(Uint32 CacheOffset, const CachedResourceView& SrcImg)
165 {
166 GetImage(CacheOffset) = SrcImg;
167 }
168
169159 bool IsUBBound(Uint32 CacheOffset) const
170160 {
171161 if (CacheOffset >= GetUBCount())
106106 m_pResourceAttribs = MemPool.Allocate<ResourceAttribs>(m_Desc.NumResources);
107107 m_ImmutableSamplers = MemPool.ConstructArray<SamplerPtr>(m_Desc.NumImmutableSamplers);
108108
109 CreateLayouts();
109 CreateLayout();
110110
111111 const auto NumStaticResStages = GetNumStaticResStages();
112112 if (NumStaticResStages > 0)
147147 }
148148 }
149149
150 void PipelineResourceSignatureGLImpl::CreateLayouts()
151 {
152 TBindings StaticCounter = {};
150 void PipelineResourceSignatureGLImpl::CreateLayout()
151 {
152 TBindings StaticResCounter = {};
153153
154154 for (Uint32 s = 0; s < m_Desc.NumImmutableSamplers; ++s)
155155 GetDevice()->CreateSampler(m_Desc.ImmutableSamplers[s].Desc, &m_ImmutableSamplers[s]);
161161
162162 if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_SAMPLER)
163163 {
164 Int32 ImtblSamplerIdx = FindImmutableSampler(ResDesc.ShaderStages, ResDesc.Name);
164 const auto ImtblSamplerIdx = FindImmutableSampler(ResDesc.ShaderStages, ResDesc.Name);
165 // Create sampler resource without cache space
165166 new (m_pResourceAttribs + i) ResourceAttribs //
166167 {
167168 ResourceAttribs::InvalidCacheOffset,
168 ImtblSamplerIdx < 0 ? ResourceAttribs::InvalidSamplerInd : static_cast<Uint32>(ImtblSamplerIdx),
169 ImtblSamplerIdx >= 0 //
169 ImtblSamplerIdx == InvalidImmutableSamplerIndex ? ResourceAttribs::InvalidSamplerInd : ImtblSamplerIdx,
170 ImtblSamplerIdx != InvalidImmutableSamplerIndex //
170171 };
171172 }
172173 else
174175 const auto Range = PipelineResourceToBindingRange(ResDesc);
175176 VERIFY_EXPR(Range != BINDING_RANGE_UNKNOWN);
176177
177 Uint32& CacheOffset = m_BindingCount[Range];
178 Uint32 SamplerIdx = ResourceAttribs::InvalidSamplerInd;
179 Int32 ImtblSamplerIdx = -1;
180
178 auto ImtblSamplerIdx = InvalidImmutableSamplerIndex;
179 auto SamplerIdx = ResourceAttribs::InvalidSamplerInd;
181180 if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_SRV)
182181 {
183 ImtblSamplerIdx = FindImmutableSampler(ResDesc.ShaderStages, ResDesc.Name);
184 if (ImtblSamplerIdx >= 0)
185 SamplerIdx = static_cast<Uint32>(ImtblSamplerIdx);
182 // Do not use combined sampler suffix - in OpenGL immutable samplers should be defined defined for textures directly
183 ImtblSamplerIdx = Diligent::FindImmutableSampler(m_Desc.ImmutableSamplers, m_Desc.NumImmutableSamplers,
184 ResDesc.ShaderStages, ResDesc.Name, nullptr);
185 if (ImtblSamplerIdx != InvalidImmutableSamplerIndex)
186 SamplerIdx = ImtblSamplerIdx;
186187 else
187188 SamplerIdx = FindAssignedSampler(ResDesc, ResourceAttribs::InvalidSamplerInd);
188189 }
189190
191 Uint32& CacheOffset = m_BindingCount[Range];
190192 new (m_pResourceAttribs + i) ResourceAttribs //
191193 {
192194 CacheOffset,
193195 SamplerIdx,
194 ImtblSamplerIdx >= 0 // _ImtblSamplerAssigned
196 ImtblSamplerIdx != InvalidImmutableSamplerIndex // _ImtblSamplerAssigned
195197 };
196
197198 CacheOffset += ResDesc.ArraySize;
198199
199200 if (ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
200 StaticCounter[Range] += ResDesc.ArraySize;
201 StaticResCounter[Range] += ResDesc.ArraySize;
201202 }
202203 }
203204
204205 if (m_pStaticResCache)
205206 {
206 m_pStaticResCache->Initialize(StaticCounter, GetRawAllocator());
207 // Set immutable samplers for static resources.
208 const auto ResIdxRange = GetResourceIndexRange(SHADER_RESOURCE_VARIABLE_TYPE_STATIC);
209
210 for (Uint32 r = ResIdxRange.first; r < ResIdxRange.second; ++r)
211 {
212 const auto& ResDesc = GetResourceDesc(r);
213 const auto& ResAttr = GetResourceAttribs(r);
214
215 if (ResDesc.ResourceType != SHADER_RESOURCE_TYPE_TEXTURE_SRV || !ResAttr.IsSamplerAssigned())
216 continue;
217
218 ISampler* pSampler = nullptr;
219 if (ResAttr.IsImmutableSamplerAssigned())
220 {
221 VERIFY_EXPR(ResAttr.SamplerInd < GetImmutableSamplerCount());
222
223 pSampler = m_ImmutableSamplers[ResAttr.SamplerInd].RawPtr();
224 }
225 else
226 {
227 const auto& SampAttr = GetResourceAttribs(ResAttr.SamplerInd);
228 if (!SampAttr.IsImmutableSamplerAssigned())
229 continue;
230
231 pSampler = m_ImmutableSamplers[SampAttr.SamplerInd].RawPtr();
232 }
233
234 if (pSampler != nullptr)
235 {
236 for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd)
237 m_pStaticResCache->SetSampler(ResAttr.CacheOffset + ArrInd, pSampler);
238 }
239 }
240 #ifdef DILIGENT_DEVELOPMENT
241 m_pStaticResCache->SetStaticResourcesInitialized();
242 #endif
207 m_pStaticResCache->Initialize(StaticResCounter, GetRawAllocator());
243208 }
244209 }
245210
418383
419384 // SrcResourceCache contains only static resources.
420385 // DstResourceCache contains static, mutable and dynamic resources.
421 const auto& SrcResourceCache = *m_pStaticResCache;
422 const auto ResIdxRange = GetResourceIndexRange(SHADER_RESOURCE_VARIABLE_TYPE_STATIC);
386 const auto& SrcResourceCache = *m_pStaticResCache;
387 const auto StaticResIdxRange = GetResourceIndexRange(SHADER_RESOURCE_VARIABLE_TYPE_STATIC);
423388
424389 VERIFY_EXPR(SrcResourceCache.GetContentType() == ResourceCacheContentType::Signature);
425390 VERIFY_EXPR(DstResourceCache.GetContentType() == ResourceCacheContentType::SRB);
426391
427 for (Uint32 r = ResIdxRange.first; r < ResIdxRange.second; ++r)
392 for (Uint32 r = StaticResIdxRange.first; r < StaticResIdxRange.second; ++r)
428393 {
429394 const auto& ResDesc = GetResourceDesc(r);
430395 const auto& ResAttr = GetResourceAttribs(r);
463428 if (!SrcCachedRes.pView)
464429 LOG_ERROR_MESSAGE("No resource is assigned to static shader variable '", GetShaderResourcePrintName(ResDesc, ArrInd), "' in pipeline resource signature '", m_Desc.Name, "'.");
465430
466 DstResourceCache.CopyTexture(ResAttr.CacheOffset + ArrInd, SrcCachedRes);
431 if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_SRV ||
432 ResDesc.ResourceType == SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT)
433 {
434 const auto HasImmutableSampler = GetImmutableSamplerIdx(ResAttr) != InvalidImmutableSamplerIndex;
435
436 auto* const pTexViewGl = SrcCachedRes.pView.RawPtr<TextureViewGLImpl>();
437 DstResourceCache.SetTexture(ResAttr.CacheOffset + ArrInd, RefCntAutoPtr<TextureViewGLImpl>{pTexViewGl}, !HasImmutableSampler);
438 if (HasImmutableSampler)
439 {
440 VERIFY(DstResourceCache.GetConstTexture(ResAttr.CacheOffset + ArrInd).pSampler, "Immutable sampler is not initialized in the cache");
441 }
442 }
443 else if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_BUFFER_SRV)
444 {
445 auto* const pViewGl = SrcCachedRes.pView.RawPtr<BufferViewGLImpl>();
446 DstResourceCache.SetTexelBuffer(ResAttr.CacheOffset + ArrInd, RefCntAutoPtr<BufferViewGLImpl>{pViewGl});
447 }
448 else
449 {
450 UNEXPECTED("Unexpected resource type");
451 }
467452 }
468453 break;
469454 case BINDING_RANGE_IMAGE:
473458 if (!SrcCachedRes.pView)
474459 LOG_ERROR_MESSAGE("No resource is assigned to static shader variable '", GetShaderResourcePrintName(ResDesc, ArrInd), "' in pipeline resource signature '", m_Desc.Name, "'.");
475460
476 DstResourceCache.CopyImage(ResAttr.CacheOffset + ArrInd, SrcCachedRes);
461 if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_UAV)
462 {
463 auto* const pTexViewGl = SrcCachedRes.pView.RawPtr<TextureViewGLImpl>();
464 DstResourceCache.SetTexImage(ResAttr.CacheOffset + ArrInd, RefCntAutoPtr<TextureViewGLImpl>{pTexViewGl});
465 }
466 else if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_BUFFER_UAV ||
467 ResDesc.ResourceType == SHADER_RESOURCE_TYPE_BUFFER_SRV)
468 {
469 auto* const pViewGl = SrcCachedRes.pView.RawPtr<BufferViewGLImpl>();
470 DstResourceCache.SetBufImage(ResAttr.CacheOffset + ArrInd, RefCntAutoPtr<BufferViewGLImpl>{pViewGl});
471 }
472 else
473 {
474 UNEXPECTED("Unexpected resource type");
475 }
477476 }
478477 break;
479478 default:
481480 }
482481 }
483482
484 // Copy immutable samplers.
485 for (Uint32 r = 0; r < m_Desc.NumResources; ++r)
486 {
487 const auto& ResDesc = GetResourceDesc(r);
488 const auto& ResAttr = GetResourceAttribs(r);
489
490 if (ResDesc.ResourceType != SHADER_RESOURCE_TYPE_TEXTURE_SRV ||
491 ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
492 continue;
493
494 if (!ResAttr.IsSamplerAssigned())
495 continue;
496
497 ISampler* pSampler = nullptr;
498 if (ResAttr.IsImmutableSamplerAssigned())
499 {
500 VERIFY_EXPR(ResAttr.SamplerInd < GetImmutableSamplerCount());
501
502 pSampler = m_ImmutableSamplers[ResAttr.SamplerInd].RawPtr();
503 }
504 else
505 {
506 const auto& SampAttr = GetResourceAttribs(ResAttr.SamplerInd);
507 if (!SampAttr.IsImmutableSamplerAssigned())
508 continue;
509
510 pSampler = m_ImmutableSamplers[SampAttr.SamplerInd].RawPtr();
511 }
512
513 if (pSampler != nullptr)
514 {
515 for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd)
516 DstResourceCache.SetSampler(ResAttr.CacheOffset + ArrInd, pSampler);
517 }
518 }
519
520483 #ifdef DILIGENT_DEVELOPMENT
521484 DstResourceCache.SetStaticResourcesInitialized();
522485 #endif
525488 void PipelineResourceSignatureGLImpl::InitSRBResourceCache(ShaderResourceCacheGL& ResourceCache)
526489 {
527490 ResourceCache.Initialize(m_BindingCount, m_SRBMemAllocator.GetResourceCacheDataAllocator(0));
491
492 // Initialize immutable samplers
493 for (Uint32 r = 0; r < m_Desc.NumResources; ++r)
494 {
495 const auto& ResDesc = GetResourceDesc(r);
496 const auto& ResAttr = GetResourceAttribs(r);
497
498 if (ResDesc.ResourceType != SHADER_RESOURCE_TYPE_TEXTURE_SRV)
499 continue;
500
501 const auto ImtblSamplerIdx = GetImmutableSamplerIdx(ResAttr);
502 if (ImtblSamplerIdx != InvalidImmutableSamplerIndex)
503 {
504 ISampler* pSampler = m_ImmutableSamplers[ImtblSamplerIdx];
505 VERIFY(pSampler != nullptr, "Immutable sampler is not initialized - this is a bug");
506
507 for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd)
508 ResourceCache.SetSampler(ResAttr.CacheOffset + ArrInd, pSampler);
509 }
510 }
528511 }
529512
530513 bool PipelineResourceSignatureGLImpl::IsCompatibleWith(const PipelineResourceSignatureGLImpl& Other) const
617600 ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, Tex.pView.RawPtr<ITextureView>(), ResourceDim, IsMultisample);
618601 else
619602 ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, Tex.pView.RawPtr<IBufferView>(), ResourceDim, IsMultisample);
603
604 const auto ImmutableSamplerIdx = GetImmutableSamplerIdx(ResAttr);
605 if (ImmutableSamplerIdx != InvalidImmutableSamplerIndex)
606 {
607 VERIFY(Tex.pSampler != nullptr, "Immutable sampler is not initialized in the cache - this is a bug");
608 VERIFY(Tex.pSampler == m_ImmutableSamplers[ImmutableSamplerIdx], "Immutable sampler initialized in the cache is not valid");
609 }
620610 }
621611 break;
622612
268268 {
269269 // We cannot use ValidatedCast<> here as the resource retrieved from the
270270 // resource mapping can be of wrong type
271 RefCntAutoPtr<TextureViewGLImpl> pViewGL(pView, IID_TextureViewGL);
271 RefCntAutoPtr<TextureViewGLImpl> pViewGL{pView, IID_TextureViewGL};
272
273 const auto ImmutableSamplerAssigned = m_ParentManager.m_pSignature->GetImmutableSamplerIdx(Attr) != InvalidImmutableSamplerIndex;
272274 #ifdef DILIGENT_DEVELOPMENT
273275 {
274276 auto& CachedTexSampler = ResourceCache.GetConstTexture(Attr.CacheOffset + ArrayIndex);
275277 VerifyResourceViewBinding(Desc.Name, Desc.ArraySize, Desc.VarType, ArrayIndex,
276278 pView, pViewGL.RawPtr(), {TEXTURE_VIEW_SHADER_RESOURCE},
277279 RESOURCE_DIM_UNDEFINED, false, CachedTexSampler.pView.RawPtr());
278 if (Attr.IsImmutableSamplerAssigned() && ResourceCache.StaticResourcesInitialized())
280 if (ImmutableSamplerAssigned && ResourceCache.GetContentType() == ResourceCacheContentType::SRB)
279281 {
280282 VERIFY(CachedTexSampler.pSampler != nullptr, "Immutable samplers must be initialized by PipelineResourceSignatureGLImpl::InitializeSRBResourceCache!");
281283 }
282284 if (Desc.ResourceType == SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT)
283285 {
284 DEV_CHECK_ERR(!Attr.IsSamplerAssigned(), "Input attachment must not have assigned sampler.");
286 DEV_CHECK_ERR(!ImmutableSamplerAssigned, "Input attachment must not have assigned sampler.");
285287 }
286288 }
287289 #endif
288 ResourceCache.SetTexture(Attr.CacheOffset + ArrayIndex, std::move(pViewGL), !Attr.IsImmutableSamplerAssigned());
290 ResourceCache.SetTexture(Attr.CacheOffset + ArrayIndex, std::move(pViewGL), !ImmutableSamplerAssigned);
289291 }
290292 else if (Desc.ResourceType == SHADER_RESOURCE_TYPE_BUFFER_SRV)
291293 {
610612
611613
612614 #ifdef DILIGENT_DEVELOPMENT
615 // TODO: remove
613616 bool ShaderVariableManagerGL::dvpVerifyBindings(const ShaderResourceCacheGL& ResourceCache) const
614617 {
615618 # define LOG_MISSING_BINDING(VarType, BindInfo, ArrIndex) LOG_ERROR_MESSAGE("No resource is bound to ", VarType, " variable '", GetShaderResourcePrintName(Desc, ArrIndex), "'")
8181 f4Position = Pos[VertId];
8282 }
8383
84 float4 PSMain(in float4 in_f4Color : COLOR,
84 float4 PSMain(in float4 f4Color : COLOR, // Name must match VS output
8585 in float4 f4Position : SV_Position) : SV_Target
8686 {
87 return in_f4Color * VerifyResources();
87 return f4Color * VerifyResources();
8888 }