From f57fc688a659087f5e85d8533f61c4a1fd914810 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 2 Mar 2019 22:43:26 -0800 Subject: Reworked ShaderD3D11Impl to comply with the updated API --- .../include/RenderDeviceD3D11Impl.h | 3 +- .../GraphicsEngineD3D11/include/ShaderD3D11Impl.h | 33 ++---------------- .../include/ShaderResourceLayoutD3D11.h | 40 +++++++++++++--------- .../GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp | 30 ++++------------ 4 files changed, 33 insertions(+), 73 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h index 95548464..0e7f66ad 100644 --- a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h @@ -49,7 +49,7 @@ public: virtual void CreateBuffer(const BufferDesc& BuffDesc, const BufferData* pBuffData, IBuffer** ppBuffer)override final; - virtual void CreateShader(const ShaderCreationAttribs& ShaderCreationAttribs, IShader** ppShader)override final; + virtual void CreateShader(const ShaderCreateInfo& ShaderCI, IShader** ppShader)override final; virtual void CreateTexture(const TextureDesc& TexDesc, const TextureData* pData, ITexture** ppTexture)override final; @@ -81,3 +81,4 @@ private: }; } + diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h index 5bafbb1f..c5005c4e 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h @@ -50,31 +50,10 @@ public: ShaderD3D11Impl(IReferenceCounters* pRefCounters, class RenderDeviceD3D11Impl* pRenderDeviceD3D11, - const ShaderCreationAttribs& CreationAttribs); + const ShaderCreateInfo& ShaderCI); ~ShaderD3D11Impl(); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject** ppInterface )override final; - - virtual void BindResources( IResourceMapping* pResourceMapping, Uint32 Flags )override final - { - m_StaticResLayout.BindResources(pResourceMapping, Flags, m_StaticResCache); - } - - virtual IShaderVariable* GetShaderVariable( const Char* Name )override final - { - return m_StaticResLayout.GetShaderVariable(Name); - } - - virtual Uint32 GetVariableCount() const override final - { - return m_StaticResLayout.GetTotalResourceCount(); - } - - virtual IShaderVariable* GetShaderVariable(Uint32 Index)override final - { - return m_StaticResLayout.GetShaderVariable(Index); - } - + IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_ShaderD3D11, TShaderBase); virtual ID3D11DeviceChild* GetD3D11Shader()override final { @@ -83,20 +62,12 @@ public: ID3DBlob* GetBytecode(){return m_pShaderByteCode;} - ShaderResourceLayoutD3D11& GetStaticResourceLayout(){return m_StaticResLayout;} - const std::shared_ptr& GetResources()const{return m_pShaderResources;} Uint32 GetShaderTypeIndex()const{return m_ShaderTypeIndex;} private: /// D3D11 shader CComPtr m_pShader; - // ShaderResources class instance must be referenced through the shared pointer, because - // it is referenced by ShaderResourceLayoutD3D11 class instances - std::shared_ptr m_pShaderResources; - - ShaderResourceCacheD3D11 m_StaticResCache; - ShaderResourceLayoutD3D11 m_StaticResLayout; Uint32 m_ShaderTypeIndex; // VS == 0, PS == 1, GS == 2, HS == 3, DS == 4, CS == 5 }; diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h index 61970f9d..b6079b0c 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h @@ -54,11 +54,11 @@ public: ShaderResourceLayoutD3D11& operator = ( ShaderResourceLayoutD3D11&&) = delete; static size_t GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, - const SHADER_VARIABLE_TYPE* VarTypes, + const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, Uint32 NumVarTypes); void Initialize(std::shared_ptr pSrcResources, - const SHADER_VARIABLE_TYPE* VarTypes, + const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, Uint32 NumVarTypes, ShaderResourceCacheD3D11& ResourceCache, IMemoryAllocator& ResCacheDataAllocator, @@ -71,8 +71,9 @@ public: struct ConstBuffBindInfo final : ShaderVariableD3D11Base { ConstBuffBindInfo( const D3DShaderResourceAttribs& ResourceAttribs, - ShaderResourceLayoutD3D11& ParentResLayout ) : - ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs) + ShaderResourceLayoutD3D11& ParentResLayout, + SHADER_RESOURCE_VARIABLE_TYPE VariableType) : + ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType) {} // Non-virtual function __forceinline void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex); @@ -91,8 +92,9 @@ public: { TexSRVBindInfo( const D3DShaderResourceAttribs& _TextureAttribs, Uint32 _SamplerIndex, - ShaderResourceLayoutD3D11& ParentResLayout) : - ShaderVariableD3D11Base(ParentResLayout, _TextureAttribs), + ShaderResourceLayoutD3D11& ParentResLayout, + SHADER_RESOURCE_VARIABLE_TYPE VariableType) : + ShaderVariableD3D11Base(ParentResLayout, _TextureAttribs, VariableType), SamplerIndex(_SamplerIndex) {} @@ -117,8 +119,9 @@ public: struct TexUAVBindInfo final : ShaderVariableD3D11Base { TexUAVBindInfo( const D3DShaderResourceAttribs& ResourceAttribs, - ShaderResourceLayoutD3D11& ParentResLayout ) : - ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs) + ShaderResourceLayoutD3D11& ParentResLayout, + SHADER_RESOURCE_VARIABLE_TYPE VariableType ) : + ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType) {} // Provide non-virtual function @@ -137,8 +140,9 @@ public: struct BuffUAVBindInfo final : ShaderVariableD3D11Base { BuffUAVBindInfo( const D3DShaderResourceAttribs& ResourceAttribs, - ShaderResourceLayoutD3D11& ParentResLayout ) : - ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs) + ShaderResourceLayoutD3D11& ParentResLayout, + SHADER_RESOURCE_VARIABLE_TYPE VariableType ) : + ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType) {} // Non-virtual function @@ -157,8 +161,9 @@ public: struct BuffSRVBindInfo final : ShaderVariableD3D11Base { BuffSRVBindInfo( const D3DShaderResourceAttribs& ResourceAttribs, - ShaderResourceLayoutD3D11& ParentResLayout ) : - ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs) + ShaderResourceLayoutD3D11& ParentResLayout, + SHADER_RESOURCE_VARIABLE_TYPE VariableType ) : + ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType) {} // Non-virtual function @@ -177,8 +182,9 @@ public: struct SamplerBindInfo final : ShaderVariableD3D11Base { SamplerBindInfo( const D3DShaderResourceAttribs& ResourceAttribs, - ShaderResourceLayoutD3D11& ParentResLayout ) : - ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs) + ShaderResourceLayoutD3D11& ParentResLayout, + SHADER_RESOURCE_VARIABLE_TYPE VariableType ) : + ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType) {} // Non-virtual function @@ -202,8 +208,8 @@ public: bool dvpVerifyBindings()const; #endif - IShaderVariable* GetShaderVariable( const Char* Name ); - IShaderVariable* GetShaderVariable( Uint32 Index ); + IShaderResourceVariable* GetShaderVariable( const Char* Name ); + IShaderResourceVariable* GetShaderVariable( Uint32 Index ); __forceinline SHADER_TYPE GetShaderType()const{return m_pResources->GetShaderType();} IObject& GetOwner(){return m_Owner;} @@ -280,7 +286,7 @@ private: } template - IShaderVariable* GetResourceByName( const Char* Name ); + IShaderResourceVariable* GetResourceByName( const Char* Name ); templateGetD3D11Device(); - switch(CreationAttribs.Desc.ShaderType) + switch (ShaderCI.Desc.ShaderType) { #define CREATE_SHADER(SHADER_NAME, ShaderName)\ @@ -75,30 +74,13 @@ ShaderD3D11Impl::ShaderD3D11Impl(IReferenceCounters* pRefCounters, DEV_CHECK_ERR(SUCCEEDED(hr), "Failed to set shader name"); } - // Load shader resources - auto &Allocator = GetRawAllocator(); - auto *pRawMem = ALLOCATE(Allocator, "Allocator for ShaderResources", sizeof(ShaderResourcesD3D11)); - auto *pResources = new (pRawMem) ShaderResourcesD3D11(pRenderDeviceD3D11, m_pShaderByteCode, m_Desc, CreationAttribs.UseCombinedTextureSamplers ? CreationAttribs.CombinedSamplerSuffix : nullptr); - m_pShaderResources.reset(pResources, STDDeleterRawMem(Allocator)); - - // Clone only static resources that will be set directly in the shader - SHADER_VARIABLE_TYPE VarTypes[] = {SHADER_VARIABLE_TYPE_STATIC}; - // The method will also initialize resource cache to have enough space to hold static variables only! - m_StaticResLayout.Initialize(m_pShaderResources, VarTypes, _countof(VarTypes), m_StaticResCache, GetRawAllocator(), GetRawAllocator()); - - // This is not required, but still... - m_pShaderResources->SetStaticSamplers(m_StaticResCache); - // Byte code is only required for the vertex shader to create input layout - if( CreationAttribs.Desc.ShaderType != SHADER_TYPE_VERTEX ) + if( ShaderCI.Desc.ShaderType != SHADER_TYPE_VERTEX ) m_pShaderByteCode.Release(); } ShaderD3D11Impl::~ShaderD3D11Impl() { - m_StaticResCache.Destroy(GetRawAllocator()); } -IMPLEMENT_QUERY_INTERFACE( ShaderD3D11Impl, IID_ShaderD3D11, TShaderBase ) - } -- cgit v1.2.3 From 91dbc2c143a371bf0584e6c6a3cd2feb16535f8b Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 2 Mar 2019 23:01:28 -0800 Subject: Reworked ShaderResourcesD3D11 to comply with the updated API --- .../include/ShaderResourcesD3D11.h | 8 ---- .../src/ShaderResourcesD3D11.cpp | 51 ++-------------------- 2 files changed, 3 insertions(+), 56 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h index da8d4a34..a241b486 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h @@ -113,8 +113,6 @@ public: const Char* GetShaderName() const { return m_ShaderName; } - void SetStaticSamplers(class ShaderResourceCacheD3D11& ResourceCache)const; - private: using MaxBindPointType = Int8; @@ -132,12 +130,6 @@ private: // ShaderResourcesD3D11 is part of the ShaderD3D11Impl object, so we can simply // reference shader name without the need to copy it const Char* const m_ShaderName; - - using StaticSamplerAttribs = std::pair>; - using StaticSamplerVector = std::vector>; - void InitStaticSamplers(StaticSamplerVector&& StaticSamplers); - - StaticSamplerVector m_StaticSamplers; }; } diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp index af72e1a0..78dfcf63 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp @@ -38,8 +38,7 @@ ShaderResourcesD3D11::ShaderResourcesD3D11(RenderDeviceD3D11Impl* pDeviceD3D11Im const ShaderDesc& ShdrDesc, const char* CombinedSamplerSuffix) : ShaderResources(ShdrDesc.ShaderType), - m_ShaderName(ShdrDesc.Name), - m_StaticSamplers(STD_ALLOCATOR_RAW_MEM(StaticSamplerAttribs, GetRawAllocator(), "Allocator for vector")) + m_ShaderName(ShdrDesc.Name) { class NewResourceHandler { @@ -51,8 +50,7 @@ ShaderResourcesD3D11::ShaderResourcesD3D11(RenderDeviceD3D11Impl* pDeviceD3D11Im pDeviceD3D11Impl (_pDeviceD3D11Impl), ShdrDesc (_ShdrDesc), CombinedSamplerSuffix(_CombinedSamplerSuffix), - Resources (_Resources), - m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerAttribs, GetRawAllocator(), "Allocator for vector")) + Resources (_Resources) {} void OnNewCB(const D3DShaderResourceAttribs& CBAttribs) @@ -83,24 +81,6 @@ ShaderResourcesD3D11::ShaderResourcesD3D11(RenderDeviceD3D11Impl* pDeviceD3D11Im { VERIFY( SamplerAttribs.BindPoint + SamplerAttribs.BindCount-1 <= MaxAllowedBindPoint, "Sampler bind point exceeds supported range" ); Resources.m_MaxSamplerBindPoint = std::max(Resources.m_MaxSamplerBindPoint, static_cast(SamplerAttribs.BindPoint + SamplerAttribs.BindCount-1)); - - if (SamplerAttribs.IsStaticSampler()) - { - // Find attributes of this static sampler - Uint32 ssd = 0; - for (; ssd < ShdrDesc.NumStaticSamplers; ++ssd) - { - const auto& StaticSamplerDesc = ShdrDesc.StaticSamplers[ssd]; - if (StreqSuff(SamplerAttribs.Name, StaticSamplerDesc.SamplerOrTextureName, CombinedSamplerSuffix)) - { - RefCntAutoPtr pSampler; - pDeviceD3D11Impl->CreateSampler(StaticSamplerDesc.Desc, &pSampler); - m_StaticSamplers.emplace_back(SamplerAttribs, std::move(pSampler)); - break; - } - } - VERIFY(ssd < ShdrDesc.NumStaticSamplers, "Unable to find sampler '", SamplerAttribs.Name, "' in the list of static samplers. This should never happen and likely indicates a bug in static sampler enumeration code."); - } } void OnNewTexSRV(const D3DShaderResourceAttribs& TexAttribs) @@ -111,7 +91,6 @@ ShaderResourcesD3D11::ShaderResourcesD3D11(RenderDeviceD3D11Impl* pDeviceD3D11Im ~NewResourceHandler() { - Resources.InitStaticSamplers(std::move(m_StaticSamplers)); } private: @@ -119,42 +98,20 @@ ShaderResourcesD3D11::ShaderResourcesD3D11(RenderDeviceD3D11Impl* pDeviceD3D11Im const ShaderDesc& ShdrDesc; const char* CombinedSamplerSuffix; ShaderResourcesD3D11& Resources; - - StaticSamplerVector m_StaticSamplers; }; Initialize( pShaderBytecode, NewResourceHandler{pDeviceD3D11Impl, ShdrDesc, CombinedSamplerSuffix, *this}, - ShdrDesc, + ShdrDesc.Name, CombinedSamplerSuffix); } -void ShaderResourcesD3D11::InitStaticSamplers(StaticSamplerVector&& StaticSamplers) -{ - m_StaticSamplers.reserve(StaticSamplers.size()); - for(auto& Sam : StaticSamplers) - m_StaticSamplers.emplace_back(std::move(Sam)); -} ShaderResourcesD3D11::~ShaderResourcesD3D11() { } -void ShaderResourcesD3D11::SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache)const -{ - auto NumCachedSamplers = ResourceCache.GetSamplerCount(); - for (const auto& SS : m_StaticSamplers) - { - const auto& SamAttribs = SS.first; - VERIFY_EXPR(SamAttribs.IsStaticSampler()); - auto* pSamplerD3D11Impl = const_cast(SS.second.RawPtr()); - // Limiting EndBindPoint is required when initializing static samplers in a Shader's static cache - auto EndBindPoint = std::min( static_cast(SamAttribs.BindPoint) + SamAttribs.BindCount, NumCachedSamplers); - for(Uint32 BindPoint = SamAttribs.BindPoint; BindPoint < EndBindPoint; ++BindPoint ) - ResourceCache.SetSampler(BindPoint, pSamplerD3D11Impl); - } -} #ifdef DEVELOPMENT static String DbgMakeResourceName(const D3DShaderResourceAttribs& Attr, Uint32 BindPoint) @@ -188,8 +145,6 @@ void ShaderResourcesD3D11::dvpVerifyCommittedResources(ID3D11Buffer* ResourceCache.GetUAVArrays (CachedUAVResources, d3d11UAVs); ProcessResources( - nullptr, 0, - [&](const D3DShaderResourceAttribs& cb, Uint32) { for (auto BindPoint = cb.BindPoint; BindPoint < cb.BindPoint + cb.BindCount; ++BindPoint) -- cgit v1.2.3 From 6f479d4aa4e49b1ac4b1a6fbfc8b52dbd3bf58d6 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 2 Mar 2019 23:57:57 -0800 Subject: Partially reworked ShaderResourceLayoutD3D11 --- .../include/ShaderResourceLayoutD3D11.h | 13 +- .../src/ShaderResourceLayoutD3D11.cpp | 224 +++++++++++---------- 2 files changed, 124 insertions(+), 113 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h index b6079b0c..c6dd714b 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h @@ -53,9 +53,10 @@ public: ShaderResourceLayoutD3D11 ( ShaderResourceLayoutD3D11&&) = default; ShaderResourceLayoutD3D11& operator = ( ShaderResourceLayoutD3D11&&) = delete; - static size_t GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, - const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, - Uint32 NumVarTypes); + static size_t GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, + const PipelineResourceLayoutDesc& ResourceLayout, + const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, + Uint32 NumAllowedTypes); void Initialize(std::shared_ptr pSrcResources, const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, @@ -198,6 +199,12 @@ public: } __forceinline bool IsBound(Uint32 ArrayIndex)const; + + bool IsStaticSampler()const + { + UNEXPECTED("Not implemented"); + return false; + } }; // dbgResourceCache is only used for sanity check and as a remainder that the resource cache must be alive diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index 5321d5e3..c6cf6f6b 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -77,11 +77,13 @@ ShaderResourceLayoutD3D11::~ShaderResourceLayoutD3D11() ); } -size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, - const SHADER_VARIABLE_TYPE* VarTypes, - Uint32 NumVarTypes) + +size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, + const PipelineResourceLayoutDesc& ResourceLayout, + const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, + Uint32 NumAllowedTypes) { - auto ResCounters = SrcResources.CountResources(VarTypes, NumVarTypes); + auto ResCounters = SrcResources.CountResources(ResourceLayout, SrcResources.GetShaderType(), SrcResources.GetCombinedSamplerSuffix(), AllowedVarTypes, NumAllowedTypes); auto MemSize = ResCounters.NumCBs * sizeof(ConstBuffBindInfo) + ResCounters.NumTexSRVs * sizeof(TexSRVBindInfo) + ResCounters.NumTexUAVs * sizeof(TexUAVBindInfo) + @@ -91,6 +93,7 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D return MemSize; } +#if 0 void ShaderResourceLayoutD3D11::Initialize(std::shared_ptr pSrcResources, const SHADER_VARIABLE_TYPE* VarTypes, Uint32 NumVarTypes, @@ -199,7 +202,7 @@ void ShaderResourceLayoutD3D11::Initialize(std::shared_ptr(AssignedSamplerIndex); - if (strcmp(Sampler.Attribs.Name, AssignedSamplerAttribs.Name) == 0) + if (strcmp(Sampler.m_Attribs.Name, AssignedSamplerAttribs.Name) == 0) break; } VERIFY(AssignedSamplerIndex < NumSamplers, "Unable to find assigned sampler"); @@ -256,6 +259,7 @@ void ShaderResourceLayoutD3D11::Initialize(std::shared_ptrInitialize(NumCBSlots, NumSRVSlots, NumSamplerSlots, NumUAVSlots, ResCacheDataAllocator); } } +#endif void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache) { @@ -296,7 +300,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache HandleResources( [&](const ConstBuffBindInfo& cb) { - for(auto CBSlot = cb.Attribs.BindPoint; CBSlot < cb.Attribs.BindPoint+cb.Attribs.BindCount; ++CBSlot) + for(auto CBSlot = cb.m_Attribs.BindPoint; CBSlot < cb.m_Attribs.BindPoint+cb.m_Attribs.BindCount; ++CBSlot) { VERIFY_EXPR(CBSlot < m_pResourceCache->GetCBCount() && CBSlot < DstCache.GetCBCount()); DstCBs [CBSlot] = CachedCBs[CBSlot]; @@ -306,7 +310,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const TexSRVBindInfo& ts) { - for(auto SRVSlot = ts.Attribs.BindPoint; SRVSlot < ts.Attribs.BindPoint + ts.Attribs.BindCount; ++SRVSlot) + for(auto SRVSlot = ts.m_Attribs.BindPoint; SRVSlot < ts.m_Attribs.BindPoint + ts.m_Attribs.BindCount; ++SRVSlot) { VERIFY_EXPR(SRVSlot < m_pResourceCache->GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); DstSRVResources[SRVSlot] = CachedSRVResources[SRVSlot]; @@ -316,7 +320,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const TexUAVBindInfo& uav) { - for(auto UAVSlot = uav.Attribs.BindPoint; UAVSlot < uav.Attribs.BindPoint + uav.Attribs.BindCount; ++UAVSlot) + for(auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) { VERIFY_EXPR(UAVSlot < m_pResourceCache->GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); DstUAVResources[UAVSlot] = CachedUAVResources[UAVSlot]; @@ -326,7 +330,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const BuffSRVBindInfo& srv) { - for(auto SRVSlot = srv.Attribs.BindPoint; SRVSlot < srv.Attribs.BindPoint + srv.Attribs.BindCount; ++SRVSlot) + for(auto SRVSlot = srv.m_Attribs.BindPoint; SRVSlot < srv.m_Attribs.BindPoint + srv.m_Attribs.BindCount; ++SRVSlot) { VERIFY_EXPR(SRVSlot < m_pResourceCache->GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); DstSRVResources[SRVSlot] = CachedSRVResources[SRVSlot]; @@ -336,7 +340,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const BuffUAVBindInfo& uav) { - for(auto UAVSlot = uav.Attribs.BindPoint; UAVSlot < uav.Attribs.BindPoint + uav.Attribs.BindCount; ++UAVSlot) + for(auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) { VERIFY_EXPR(UAVSlot < m_pResourceCache->GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); DstUAVResources[UAVSlot] = CachedUAVResources[UAVSlot]; @@ -346,8 +350,8 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const SamplerBindInfo& sam) { - VERIFY(!sam.Attribs.IsStaticSampler(), "Variables are not created for static samplers"); - for(auto SamSlot = sam.Attribs.BindPoint; SamSlot < sam.Attribs.BindPoint + sam.Attribs.BindCount; ++SamSlot) + VERIFY(!sam.IsStaticSampler(), "Variables are not created for static samplers"); + for(auto SamSlot = sam.m_Attribs.BindPoint; SamSlot < sam.m_Attribs.BindPoint + sam.m_Attribs.BindCount; ++SamSlot) { VERIFY_EXPR(SamSlot < m_pResourceCache->GetSamplerCount() && SamSlot < DstCache.GetSamplerCount()); DstSamplers [SamSlot] = CachedSamplers[SamSlot]; @@ -360,7 +364,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache #define LOG_RESOURCE_BINDING_ERROR(ResType, pResource, Attribs, ArrayInd, ShaderName, ...)\ do{ \ const auto* ResName = pResource->GetDesc().Name; \ - if(Attribs.BindCount>1) \ + if (Attribs.BindCount>1) \ LOG_ERROR_MESSAGE( "Failed to bind ", ResType, " '", ResName, "' to variable '", Attribs.Name,\ "[", ArrayInd, "]' in shader '", ShaderName, "'. ", __VA_ARGS__ ); \ else \ @@ -372,7 +376,7 @@ void ShaderResourceLayoutD3D11::ConstBuffBindInfo::BindResource(IDeviceObject* p Uint32 ArrayIndex) { VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); - DEV_CHECK_ERR(ArrayIndex < Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", Attribs.Name, "'. Max allowed index: ", Attribs.BindCount); + DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the @@ -380,26 +384,26 @@ void ShaderResourceLayoutD3D11::ConstBuffBindInfo::BindResource(IDeviceObject* p RefCntAutoPtr pBuffD3D11Impl(pBuffer, IID_BufferD3D11); #ifdef DEVELOPMENT if (pBuffer && !pBuffD3D11Impl) - LOG_RESOURCE_BINDING_ERROR("buffer", pBuffer, Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorrect resource type: buffer is expected."); + LOG_RESOURCE_BINDING_ERROR("buffer", pBuffer, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorrect resource type: buffer is expected."); if (pBuffD3D11Impl && (pBuffD3D11Impl->GetDesc().BindFlags & BIND_UNIFORM_BUFFER) == 0) { - LOG_RESOURCE_BINDING_ERROR("buffer", pBuffer, Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Buffer was not created with BIND_UNIFORM_BUFFER flag."); + LOG_RESOURCE_BINDING_ERROR("buffer", pBuffer, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Buffer was not created with BIND_UNIFORM_BUFFER flag."); pBuffD3D11Impl.Release(); } - if (Attribs.GetVariableType() != SHADER_VARIABLE_TYPE_DYNAMIC) + if (GetType() != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) { - auto& CachedCB = ResourceCache.GetCB(Attribs.BindPoint + ArrayIndex); + auto& CachedCB = ResourceCache.GetCB(m_Attribs.BindPoint + ArrayIndex); if (CachedCB.pBuff != nullptr && CachedCB.pBuff != pBuffD3D11Impl) { - auto VarTypeStr = GetShaderVariableTypeLiteralName(Attribs.GetVariableType()); - LOG_ERROR_MESSAGE( "Non-null constant buffer is already bound to ", VarTypeStr, " shader variable '", Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); + auto VarTypeStr = GetShaderVariableTypeLiteralName(GetType()); + LOG_ERROR_MESSAGE( "Non-null constant buffer is already bound to ", VarTypeStr, " shader variable '", m_Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); } } #endif - ResourceCache.SetCB(Attribs.BindPoint + ArrayIndex, std::move(pBuffD3D11Impl) ); + ResourceCache.SetCB(m_Attribs.BindPoint + ArrayIndex, std::move(pBuffD3D11Impl) ); } @@ -408,9 +412,9 @@ bool ShaderResourceLayoutD3D11::ConstBuffBindInfo::IsBound(Uint32 ArrayIndex) { auto* pResourceCache = m_ParentResLayout.m_pResourceCache; VERIFY(pResourceCache, "Resource cache is null"); - VERIFY_EXPR(ArrayIndex < Attribs.BindCount); + VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - return pResourceCache->IsCBBound(Attribs.BindPoint + ArrayIndex); + return pResourceCache->IsCBBound(m_Attribs.BindPoint + ArrayIndex); } @@ -446,7 +450,7 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie Uint32 ArrayIndex) { VERIFY(m_ParentResLayout.m_pResourceCache, "Resource cache is null"); - DEV_CHECK_ERR(ArrayIndex < Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", Attribs.Name, "'. Max allowed index: ", Attribs.BindCount); + DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the @@ -454,17 +458,17 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie RefCntAutoPtr pViewD3D11(pView, IID_TextureViewD3D11); #ifdef DEVELOPMENT if (pView && !pViewD3D11) - LOG_RESOURCE_BINDING_ERROR("resource", pView, Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: texture view is expected."); - if (pViewD3D11 && !dbgVerifyViewType("texture view", pViewD3D11.RawPtr(), Attribs, ArrayIndex, TEXTURE_VIEW_SHADER_RESOURCE, m_ParentResLayout.GetShaderName())) + LOG_RESOURCE_BINDING_ERROR("resource", pView, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: texture view is expected."); + if (pViewD3D11 && !dbgVerifyViewType("texture view", pViewD3D11.RawPtr(), m_Attribs, ArrayIndex, TEXTURE_VIEW_SHADER_RESOURCE, m_ParentResLayout.GetShaderName())) pViewD3D11.Release(); - if( Attribs.GetVariableType() != SHADER_VARIABLE_TYPE_DYNAMIC) + if( GetType() != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) { - auto& CachedSRV = ResourceCache.GetSRV(Attribs.BindPoint + ArrayIndex); + auto& CachedSRV = ResourceCache.GetSRV(m_Attribs.BindPoint + ArrayIndex); if (CachedSRV.pView != nullptr && CachedSRV.pView != pViewD3D11) { - auto VarTypeStr = GetShaderVariableTypeLiteralName(Attribs.GetVariableType()); - LOG_ERROR_MESSAGE( "Non-null texture SRV is already bound to ", VarTypeStr, " shader variable '", Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); + auto VarTypeStr = GetShaderVariableTypeLiteralName(GetType()); + LOG_ERROR_MESSAGE( "Non-null texture SRV is already bound to ", VarTypeStr, " shader variable '", m_Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); } } #endif @@ -472,9 +476,9 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie if (ValidSamplerAssigned()) { auto& Sampler = m_ParentResLayout.GetResource(SamplerIndex); - VERIFY(!Sampler.Attribs.IsStaticSampler(), "Static samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); - VERIFY_EXPR(Sampler.Attribs.BindCount == Attribs.BindCount || Sampler.Attribs.BindCount == 1); - auto SamplerBindPoint = Sampler.Attribs.BindPoint + (Sampler.Attribs.BindCount != 1 ? ArrayIndex : 0); + VERIFY(!Sampler.IsStaticSampler(), "Static samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); + VERIFY_EXPR(Sampler.m_Attribs.BindCount == m_Attribs.BindCount || Sampler.m_Attribs.BindCount == 1); + auto SamplerBindPoint = Sampler.m_Attribs.BindPoint + (Sampler.m_Attribs.BindCount != 1 ? ArrayIndex : 0); SamplerD3D11Impl* pSamplerD3D11Impl = nullptr; if (pViewD3D11) @@ -483,37 +487,37 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie #ifdef DEVELOPMENT if (pSamplerD3D11Impl == nullptr) { - if(Sampler.Attribs.BindCount > 1) - LOG_ERROR_MESSAGE( "Failed to bind sampler to variable '", Sampler.Attribs.Name, "[", ArrayIndex,"]'. Sampler is not set in the texture view '", pViewD3D11->GetDesc().Name, "'" ); + if(Sampler.m_Attribs.BindCount > 1) + LOG_ERROR_MESSAGE( "Failed to bind sampler to variable '", Sampler.m_Attribs.Name, "[", ArrayIndex,"]'. Sampler is not set in the texture view '", pViewD3D11->GetDesc().Name, "'" ); else - LOG_ERROR_MESSAGE( "Failed to bind sampler to variable '", Sampler.Attribs.Name, "'. Sampler is not set in the texture view '", pViewD3D11->GetDesc().Name, "'" ); + LOG_ERROR_MESSAGE( "Failed to bind sampler to variable '", Sampler.m_Attribs.Name, "'. Sampler is not set in the texture view '", pViewD3D11->GetDesc().Name, "'" ); } #endif } #ifdef DEVELOPMENT - if (Sampler.Attribs.GetVariableType() != SHADER_VARIABLE_TYPE_DYNAMIC) + if (Sampler.GetType() != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) { auto& CachedSampler = ResourceCache.GetSampler(SamplerBindPoint); if (CachedSampler.pSampler != nullptr && CachedSampler.pSampler != pSamplerD3D11Impl) { - auto VarTypeStr = GetShaderVariableTypeLiteralName(Attribs.GetVariableType()); - LOG_ERROR_MESSAGE( "Non-null sampler is already bound to ", VarTypeStr, " shader variable '", Sampler.Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another sampler or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); + auto VarTypeStr = GetShaderVariableTypeLiteralName(GetType()); + LOG_ERROR_MESSAGE( "Non-null sampler is already bound to ", VarTypeStr, " shader variable '", Sampler.m_Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another sampler or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); } } #endif ResourceCache.SetSampler(SamplerBindPoint, pSamplerD3D11Impl); } - ResourceCache.SetTexSRV(Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); + ResourceCache.SetTexSRV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); } void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSampler, Uint32 ArrayIndex) { VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); - DEV_CHECK_ERR(ArrayIndex < Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", Attribs.Name, "'. Max allowed index: ", Attribs.BindCount); + DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; - VERIFY(!Attribs.IsStaticSampler(), "Cannot bind sampler to a static sampler"); + VERIFY(!IsStaticSampler(), "Cannot bind sampler to a static sampler"); // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -521,33 +525,33 @@ void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSa #ifdef DEVELOPMENT if (pSampler && !pSamplerD3D11) - LOG_RESOURCE_BINDING_ERROR("sampler", pSampler, Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: sampler is expected."); + LOG_RESOURCE_BINDING_ERROR("sampler", pSampler, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: sampler is expected."); - if (Attribs.ValidTexSRVAssigned()) + if (m_Attribs.ValidTexSRVAssigned()) { - auto* TexSRVName = m_ParentResLayout.m_pResources->GetTexSRV(Attribs.GetTexSRVId()).Name; - LOG_WARNING_MESSAGE("Texture sampler sampler '", Attribs.Name, "' is assigned to texture SRV '", TexSRVName, "' and should not be accessed directly. The sampler is initialized when texture SRV is set to '", TexSRVName, "' variable."); + auto* TexSRVName = m_ParentResLayout.m_pResources->GetTexSRV(m_Attribs.GetTexSRVId()).Name; + LOG_WARNING_MESSAGE("Texture sampler sampler '", m_Attribs.Name, "' is assigned to texture SRV '", TexSRVName, "' and should not be accessed directly. The sampler is initialized when texture SRV is set to '", TexSRVName, "' variable."); } - if (Attribs.GetVariableType() != SHADER_VARIABLE_TYPE_DYNAMIC) + if (GetType() != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) { - auto& CachedSampler = ResourceCache.GetSampler(Attribs.BindPoint + ArrayIndex); + auto& CachedSampler = ResourceCache.GetSampler(m_Attribs.BindPoint + ArrayIndex); if( CachedSampler.pSampler != nullptr && CachedSampler.pSampler != pSamplerD3D11) { - auto VarTypeStr = GetShaderVariableTypeLiteralName(Attribs.GetVariableType()); - LOG_ERROR_MESSAGE( "Non-null sampler is already bound to ", VarTypeStr, " shader variable '", Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another sampler or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); + auto VarTypeStr = GetShaderVariableTypeLiteralName(GetType()); + LOG_ERROR_MESSAGE( "Non-null sampler is already bound to ", VarTypeStr, " shader variable '", m_Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another sampler or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); } } #endif - ResourceCache.SetSampler(Attribs.BindPoint + ArrayIndex, std::move(pSamplerD3D11)); + ResourceCache.SetSampler(m_Attribs.BindPoint + ArrayIndex, std::move(pSamplerD3D11)); } void ShaderResourceLayoutD3D11::BuffSRVBindInfo::BindResource(IDeviceObject* pView, Uint32 ArrayIndex) { VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); - DEV_CHECK_ERR(ArrayIndex < Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", Attribs.Name, "'. Max allowed index: ", Attribs.BindCount); + DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the @@ -555,22 +559,22 @@ void ShaderResourceLayoutD3D11::BuffSRVBindInfo::BindResource(IDeviceObject* pVi RefCntAutoPtr pViewD3D11(pView, IID_BufferViewD3D11); #ifdef DEVELOPMENT if (pView && !pViewD3D11) - LOG_RESOURCE_BINDING_ERROR("resource", pView, Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: buffer view is expected."); - if (pViewD3D11 && !dbgVerifyViewType("buffer view", pViewD3D11.RawPtr(), Attribs, ArrayIndex, BUFFER_VIEW_SHADER_RESOURCE, m_ParentResLayout.GetShaderName())) + LOG_RESOURCE_BINDING_ERROR("resource", pView, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: buffer view is expected."); + if (pViewD3D11 && !dbgVerifyViewType("buffer view", pViewD3D11.RawPtr(), m_Attribs, ArrayIndex, BUFFER_VIEW_SHADER_RESOURCE, m_ParentResLayout.GetShaderName())) pViewD3D11.Release(); - if (Attribs.GetVariableType() != SHADER_VARIABLE_TYPE_DYNAMIC) + if (GetType() != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) { - auto& CachedSRV = ResourceCache.GetSRV(Attribs.BindPoint + ArrayIndex); + auto& CachedSRV = ResourceCache.GetSRV(m_Attribs.BindPoint + ArrayIndex); if (CachedSRV.pView != nullptr && CachedSRV.pView != pViewD3D11) { - auto VarTypeStr = GetShaderVariableTypeLiteralName(Attribs.GetVariableType()); - LOG_ERROR_MESSAGE( "Non-null buffer SRV is already bound to ", VarTypeStr, " shader variable '", Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); + auto VarTypeStr = GetShaderVariableTypeLiteralName(GetType()); + LOG_ERROR_MESSAGE( "Non-null buffer SRV is already bound to ", VarTypeStr, " shader variable '", m_Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); } } #endif - ResourceCache.SetBufSRV(Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); + ResourceCache.SetBufSRV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); } @@ -578,7 +582,7 @@ void ShaderResourceLayoutD3D11::TexUAVBindInfo::BindResource(IDeviceObject* pVie Uint32 ArrayIndex) { VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); - DEV_CHECK_ERR(ArrayIndex < Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", Attribs.Name, "'. Max allowed index: ", Attribs.BindCount); + DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the @@ -586,22 +590,22 @@ void ShaderResourceLayoutD3D11::TexUAVBindInfo::BindResource(IDeviceObject* pVie RefCntAutoPtr pViewD3D11(pView, IID_TextureViewD3D11); #ifdef DEVELOPMENT if (pView && !pViewD3D11) - LOG_RESOURCE_BINDING_ERROR("resource", pView, Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: texture view is expected."); - if (pViewD3D11 && !dbgVerifyViewType("texture view", pViewD3D11.RawPtr(), Attribs, ArrayIndex, TEXTURE_VIEW_UNORDERED_ACCESS, m_ParentResLayout.GetShaderName())) + LOG_RESOURCE_BINDING_ERROR("resource", pView, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: texture view is expected."); + if (pViewD3D11 && !dbgVerifyViewType("texture view", pViewD3D11.RawPtr(), m_Attribs, ArrayIndex, TEXTURE_VIEW_UNORDERED_ACCESS, m_ParentResLayout.GetShaderName())) pViewD3D11.Release(); - if (Attribs.GetVariableType() != SHADER_VARIABLE_TYPE_DYNAMIC) + if (GetType() != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) { - auto& CachedUAV = ResourceCache.GetUAV(Attribs.BindPoint + ArrayIndex); + auto& CachedUAV = ResourceCache.GetUAV(m_Attribs.BindPoint + ArrayIndex); if (CachedUAV.pView != nullptr && CachedUAV.pView != pViewD3D11) { - auto VarTypeStr = GetShaderVariableTypeLiteralName(Attribs.GetVariableType()); - LOG_ERROR_MESSAGE( "Non-null texture UAV is already bound to ", VarTypeStr, " shader variable '", Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); + auto VarTypeStr = GetShaderVariableTypeLiteralName(GetType()); + LOG_ERROR_MESSAGE( "Non-null texture UAV is already bound to ", VarTypeStr, " shader variable '", m_Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); } } #endif - ResourceCache.SetTexUAV(Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); + ResourceCache.SetTexUAV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); } @@ -609,7 +613,7 @@ void ShaderResourceLayoutD3D11::BuffUAVBindInfo::BindResource(IDeviceObject* pVi Uint32 ArrayIndex) { VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); - DEV_CHECK_ERR(ArrayIndex < Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", Attribs.Name, "'. Max allowed index: ", Attribs.BindCount); + DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the @@ -617,22 +621,22 @@ void ShaderResourceLayoutD3D11::BuffUAVBindInfo::BindResource(IDeviceObject* pVi RefCntAutoPtr pViewD3D11(pView, IID_BufferViewD3D11); #ifdef DEVELOPMENT if (pView && !pViewD3D11) - LOG_RESOURCE_BINDING_ERROR("resource", pView, Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: buffer view is expected."); - if (pViewD3D11 && !dbgVerifyViewType("buffer view", pViewD3D11.RawPtr(), Attribs, ArrayIndex, BUFFER_VIEW_UNORDERED_ACCESS, m_ParentResLayout.GetShaderName()) ) + LOG_RESOURCE_BINDING_ERROR("resource", pView, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: buffer view is expected."); + if (pViewD3D11 && !dbgVerifyViewType("buffer view", pViewD3D11.RawPtr(), m_Attribs, ArrayIndex, BUFFER_VIEW_UNORDERED_ACCESS, m_ParentResLayout.GetShaderName()) ) pViewD3D11.Release(); - if (Attribs.GetVariableType() != SHADER_VARIABLE_TYPE_DYNAMIC) + if (GetType() != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) { - auto& CachedUAV = ResourceCache.GetUAV(Attribs.BindPoint + ArrayIndex); + auto& CachedUAV = ResourceCache.GetUAV(m_Attribs.BindPoint + ArrayIndex); if (CachedUAV.pView != nullptr && CachedUAV.pView != pViewD3D11) { - auto VarTypeStr = GetShaderVariableTypeLiteralName(Attribs.GetVariableType()); - LOG_ERROR_MESSAGE( "Non-null buffer UAV is already bound to ", VarTypeStr, " shader variable '", Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); + auto VarTypeStr = GetShaderVariableTypeLiteralName(GetType()); + LOG_ERROR_MESSAGE( "Non-null buffer UAV is already bound to ", VarTypeStr, " shader variable '", m_Attribs.GetPrintName(ArrayIndex), "' in shader '", m_ParentResLayout.GetShaderName(), "'. Attempting to bind another resource or null is an error and may cause unpredicted behavior. Use another shader resource binding instance or label the variable as dynamic." ); } } #endif - ResourceCache.SetBufUAV(Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); + ResourceCache.SetBufUAV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); } @@ -640,9 +644,9 @@ bool ShaderResourceLayoutD3D11::TexSRVBindInfo::IsBound(Uint32 ArrayIndex)const { auto* pResourceCache = m_ParentResLayout.m_pResourceCache; VERIFY(pResourceCache != nullptr, "Resource cache is null"); - VERIFY_EXPR(ArrayIndex < Attribs.BindCount); + VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - return pResourceCache->IsSRVBound(Attribs.BindPoint + ArrayIndex, true); + return pResourceCache->IsSRVBound(m_Attribs.BindPoint + ArrayIndex, true); } @@ -650,36 +654,36 @@ bool ShaderResourceLayoutD3D11::BuffSRVBindInfo::IsBound(Uint32 ArrayIndex)const { auto* pResourceCache = m_ParentResLayout.m_pResourceCache; VERIFY(pResourceCache, "Resource cache is null"); - VERIFY_EXPR(ArrayIndex < Attribs.BindCount); + VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - return pResourceCache->IsSRVBound(Attribs.BindPoint + ArrayIndex, false); + return pResourceCache->IsSRVBound(m_Attribs.BindPoint + ArrayIndex, false); } bool ShaderResourceLayoutD3D11::TexUAVBindInfo::IsBound(Uint32 ArrayIndex)const { auto* pResourceCache = m_ParentResLayout.m_pResourceCache; VERIFY(pResourceCache, "Resource cache is null"); - VERIFY_EXPR(ArrayIndex < Attribs.BindCount); + VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - return pResourceCache->IsUAVBound(Attribs.BindPoint + ArrayIndex, true); + return pResourceCache->IsUAVBound(m_Attribs.BindPoint + ArrayIndex, true); } bool ShaderResourceLayoutD3D11::BuffUAVBindInfo::IsBound(Uint32 ArrayIndex)const { auto* pResourceCache = m_ParentResLayout.m_pResourceCache; VERIFY(pResourceCache, "Resource cache is null"); - VERIFY_EXPR(ArrayIndex < Attribs.BindCount); + VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - return pResourceCache->IsUAVBound(Attribs.BindPoint + ArrayIndex, false); + return pResourceCache->IsUAVBound(m_Attribs.BindPoint + ArrayIndex, false); } bool ShaderResourceLayoutD3D11::SamplerBindInfo::IsBound(Uint32 ArrayIndex)const { auto* pResourceCache = m_ParentResLayout.m_pResourceCache; VERIFY(pResourceCache, "Resource cache is null"); - VERIFY_EXPR(ArrayIndex < Attribs.BindCount); + VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - return pResourceCache->IsSamplerBound(Attribs.BindPoint + ArrayIndex); + return pResourceCache->IsSamplerBound(m_Attribs.BindPoint + ArrayIndex); } @@ -697,15 +701,15 @@ public: template void Bind( ResourceType &Res) { - if ( (Flags & (1 << Res.Attribs.GetVariableType())) == 0 ) + if ( (Flags & (1 << Res.GetType())) == 0 ) return; - for (Uint16 elem=0; elem < Res.Attribs.BindCount; ++elem) + for (Uint16 elem=0; elem < Res.m_Attribs.BindCount; ++elem) { if ( (Flags & BIND_SHADER_RESOURCES_KEEP_EXISTING) && Res.IsBound(elem) ) continue; - const auto* VarName = Res.Attribs.Name; + const auto* VarName = Res.m_Attribs.Name; RefCntAutoPtr pRes; ResourceMapping.GetResource( VarName, &pRes, elem ); if (pRes) @@ -776,20 +780,20 @@ void ShaderResourceLayoutD3D11::BindResources( IResourceMapping* pResourceMappin } template -IShaderVariable* ShaderResourceLayoutD3D11::GetResourceByName( const Char* Name ) +IShaderResourceVariable* ShaderResourceLayoutD3D11::GetResourceByName( const Char* Name ) { auto NumResources = GetNumResources(); for (Uint32 res = 0; res < NumResources; ++res) { auto& Resource = GetResource(res); - if (strcmp(Resource.Attribs.Name, Name) == 0) + if (strcmp(Resource.m_Attribs.Name, Name) == 0) return &Resource; } return nullptr; } -IShaderVariable* ShaderResourceLayoutD3D11::GetShaderVariable(const Char* Name) +IShaderResourceVariable* ShaderResourceLayoutD3D11::GetShaderVariable(const Char* Name) { if(auto* pCB = GetResourceByName(Name)) return pCB; @@ -900,7 +904,7 @@ public: } template - IShaderVariable* TryResource() + IShaderResourceVariable* TryResource() { #ifdef _DEBUG VERIFY(Layout.GetResourceOffset() >= dbgPreviousResourceOffset, "Resource types are processed out of order!"); @@ -924,7 +928,7 @@ private: #endif }; -IShaderVariable* ShaderResourceLayoutD3D11::GetShaderVariable( Uint32 Index ) +IShaderResourceVariable* ShaderResourceLayoutD3D11::GetShaderVariable( Uint32 Index ) { ShaderVariableLocator VarLocator(*this, Index); @@ -975,11 +979,11 @@ do{ \ const_cast(this)->HandleResources( [&](const ConstBuffBindInfo& cb) { - for (Uint32 BindPoint = cb.Attribs.BindPoint; BindPoint < Uint32{cb.Attribs.BindPoint} + cb.Attribs.BindCount; ++BindPoint) + for (Uint32 BindPoint = cb.m_Attribs.BindPoint; BindPoint < Uint32{cb.m_Attribs.BindPoint} + cb.m_Attribs.BindCount; ++BindPoint) { if (!m_pResourceCache->IsCBBound(BindPoint)) { - LOG_MISSING_BINDING("constant buffer", cb.Attribs, BindPoint); + LOG_MISSING_BINDING("constant buffer", cb.m_Attribs, BindPoint); BindingsOK = false; } } @@ -987,27 +991,27 @@ do{ \ [&](const TexSRVBindInfo& ts) { - for (Uint32 BindPoint = ts.Attribs.BindPoint; BindPoint < Uint32{ts.Attribs.BindPoint} + ts.Attribs.BindCount; ++BindPoint) + for (Uint32 BindPoint = ts.m_Attribs.BindPoint; BindPoint < Uint32{ts.m_Attribs.BindPoint} + ts.m_Attribs.BindCount; ++BindPoint) { if (!m_pResourceCache->IsSRVBound(BindPoint, true)) { - LOG_MISSING_BINDING("texture", ts.Attribs, BindPoint); + LOG_MISSING_BINDING("texture", ts.m_Attribs, BindPoint); BindingsOK = false; } if (ts.ValidSamplerAssigned()) { const auto& Sampler = GetConstResource(ts.SamplerIndex); - VERIFY_EXPR(Sampler.Attribs.BindCount == ts.Attribs.BindCount || Sampler.Attribs.BindCount == 1); + VERIFY_EXPR(Sampler.m_Attribs.BindCount == ts.m_Attribs.BindCount || Sampler.m_Attribs.BindCount == 1); // Verify that if single sampler is used for all texture array elements, all samplers set in the resource views are consistent - if (ts.Attribs.BindCount > 1 && Sampler.Attribs.BindCount == 1) + if (ts.m_Attribs.BindCount > 1 && Sampler.m_Attribs.BindCount == 1) { ShaderResourceCacheD3D11::CachedSampler* pCachedSamplers = nullptr; ID3D11SamplerState** ppCachedD3D11Samplers = nullptr; m_pResourceCache->GetSamplerArrays(pCachedSamplers, ppCachedD3D11Samplers); - VERIFY_EXPR(Sampler.Attribs.BindPoint < m_pResourceCache->GetSamplerCount()); - const auto& CachedSampler = pCachedSamplers[Sampler.Attribs.BindPoint]; + VERIFY_EXPR(Sampler.m_Attribs.BindPoint < m_pResourceCache->GetSamplerCount()); + const auto& CachedSampler = pCachedSamplers[Sampler.m_Attribs.BindPoint]; ShaderResourceCacheD3D11::CachedResource* pCachedResources = nullptr; ID3D11ShaderResourceView** ppCachedD3D11Resources = nullptr; @@ -1020,7 +1024,7 @@ do{ \ auto* pSampler = pTexView->GetSampler(); if (pSampler != nullptr && pSampler != CachedSampler.pSampler.RawPtr()) { - LOG_ERROR_MESSAGE( "All elements of texture array '", ts.Attribs.Name, "' in shader '", GetShaderName(), "' share the same sampler. However, the sampler set in view for element ", BindPoint - ts.Attribs.BindPoint, " does not match bound sampler. This may cause incorrect behavior on GL platform." ); + LOG_ERROR_MESSAGE( "All elements of texture array '", ts.m_Attribs.Name, "' in shader '", GetShaderName(), "' share the same sampler. However, the sampler set in view for element ", BindPoint - ts.m_Attribs.BindPoint, " does not match bound sampler. This may cause incorrect behavior on GL platform." ); } } } @@ -1030,11 +1034,11 @@ do{ \ [&](const TexUAVBindInfo& uav) { - for (Uint32 BindPoint = uav.Attribs.BindPoint; BindPoint < Uint32{uav.Attribs.BindPoint} + uav.Attribs.BindCount; ++BindPoint) + for (Uint32 BindPoint = uav.m_Attribs.BindPoint; BindPoint < Uint32{uav.m_Attribs.BindPoint} + uav.m_Attribs.BindCount; ++BindPoint) { if (!m_pResourceCache->IsUAVBound(BindPoint, true)) { - LOG_MISSING_BINDING("texture UAV", uav.Attribs, BindPoint); + LOG_MISSING_BINDING("texture UAV", uav.m_Attribs, BindPoint); BindingsOK = false; } } @@ -1042,11 +1046,11 @@ do{ \ [&](const BuffSRVBindInfo& buf) { - for (Uint32 BindPoint = buf.Attribs.BindPoint; BindPoint < Uint32{buf.Attribs.BindPoint} + buf.Attribs.BindCount; ++BindPoint) + for (Uint32 BindPoint = buf.m_Attribs.BindPoint; BindPoint < Uint32{buf.m_Attribs.BindPoint} + buf.m_Attribs.BindCount; ++BindPoint) { if (!m_pResourceCache->IsSRVBound(BindPoint, false)) { - LOG_MISSING_BINDING("buffer", buf.Attribs, BindPoint); + LOG_MISSING_BINDING("buffer", buf.m_Attribs, BindPoint); BindingsOK = false; } } @@ -1054,11 +1058,11 @@ do{ \ [&](const BuffUAVBindInfo& uav) { - for (Uint32 BindPoint = uav.Attribs.BindPoint; BindPoint < Uint32{uav.Attribs.BindPoint} + uav.Attribs.BindCount; ++BindPoint) + for (Uint32 BindPoint = uav.m_Attribs.BindPoint; BindPoint < Uint32{uav.m_Attribs.BindPoint} + uav.m_Attribs.BindCount; ++BindPoint) { if (!m_pResourceCache->IsUAVBound(BindPoint, false)) { - LOG_MISSING_BINDING("buffer UAV", uav.Attribs, BindPoint); + LOG_MISSING_BINDING("buffer UAV", uav.m_Attribs, BindPoint); BindingsOK = false; } } @@ -1066,11 +1070,11 @@ do{ \ [&](const SamplerBindInfo& sam) { - for (Uint32 BindPoint = sam.Attribs.BindPoint; BindPoint < Uint32{sam.Attribs.BindPoint} + sam.Attribs.BindCount; ++BindPoint) + for (Uint32 BindPoint = sam.m_Attribs.BindPoint; BindPoint < Uint32{sam.m_Attribs.BindPoint} + sam.m_Attribs.BindCount; ++BindPoint) { if (!m_pResourceCache->IsSamplerBound(BindPoint)) { - LOG_MISSING_BINDING("sampler", sam.Attribs, BindPoint); + LOG_MISSING_BINDING("sampler", sam.m_Attribs, BindPoint); BindingsOK = false; } } -- cgit v1.2.3 From 04330a7e1d0198e721fd8d10379ab751b6826a35 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 3 Mar 2019 13:16:34 -0800 Subject: Cleaned up QueryInterface declarations --- .../GraphicsEngineD3D11/include/BufferD3D11Impl.h | 2 +- .../GraphicsEngineD3D11/include/BufferViewD3D11Impl.h | 2 +- .../include/DeviceContextD3D11Impl.h | 2 +- .../include/PipelineStateD3D11Impl.h | 19 ++++++++++++++++++- .../include/RenderDeviceD3D11Impl.h | 2 +- .../GraphicsEngineD3D11/include/SamplerD3D11Impl.h | 2 +- .../GraphicsEngineD3D11/include/SwapChainD3D11Impl.h | 2 +- .../GraphicsEngineD3D11/include/TextureBaseD3D11.h | 2 +- .../include/TextureViewD3D11Impl.h | 2 +- .../src/DeviceContextD3D11Impl.cpp | 4 ++-- 10 files changed, 28 insertions(+), 11 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h index 8b39f607..71b91345 100644 --- a/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h @@ -58,7 +58,7 @@ public: ~BufferD3D11Impl(); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual ID3D11Buffer* GetD3D11Buffer()override final{ return m_pd3d11Buffer; } diff --git a/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h index 208dd1ee..d705536e 100644 --- a/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h @@ -48,7 +48,7 @@ public: ID3D11View* pD3D11View, bool bIsDefaultView); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface ) final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject **ppInterface) final; virtual ID3D11View* GetD3D11View()override final { diff --git a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h index df013869..d9a0e44d 100755 --- a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h @@ -59,7 +59,7 @@ public: ID3D11DeviceContext* pd3d11DeviceContext, const struct EngineD3D11Attribs& EngineAttribs, bool bIsDeferred); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual void SetPipelineState(IPipelineState* pPipelineState)override final; diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index af93c8eb..04e0edc0 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -48,7 +48,7 @@ public: const PipelineStateDesc& PipelineDesc); ~PipelineStateD3D11Impl(); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; /// Implementation of the IPipelineStateD3D11::GetD3D11BlendState() method. virtual ID3D11BlendState* GetD3D11BlendState()override final; @@ -77,12 +77,29 @@ public: return m_SRBMemAllocator; } + const ShaderResourceLayoutD3D11& GetStaticResourceLayout(Uint32 s)const + { + VERIFY_EXPR(s < m_NumShaders); + return m_pStaticResourceLayouts[s]; + } + + ShaderResourceCacheD3D11& GetStaticResourceCache(Uint32 s) + { + VERIFY_EXPR(s < m_NumShaders); + return m_pStaticResourceCaches[s]; + } + + private: CComPtr m_pd3d11BlendState; CComPtr m_pd3d11RasterizerState; CComPtr m_pd3d11DepthStencilState; CComPtr m_pd3d11InputLayout; + // The caches are indexed by the shader order in the PSO, not shader index + ShaderResourceCacheD3D11* m_pStaticResourceCaches = nullptr; + ShaderResourceLayoutD3D11* m_pStaticResourceLayouts= nullptr; + // SRB memory allocator must be defined before the default shader res binding SRBMemoryAllocator m_SRBMemAllocator; }; diff --git a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h index 0e7f66ad..87189352 100644 --- a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h @@ -45,7 +45,7 @@ public: const EngineD3D11Attribs& EngineAttribs, ID3D11Device* pd3d11Device, Uint32 NumDeferredContexts ); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual void CreateBuffer(const BufferDesc& BuffDesc, const BufferData* pBuffData, IBuffer** ppBuffer)override final; diff --git a/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h index 5745bf18..4cac7884 100644 --- a/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h @@ -46,7 +46,7 @@ public: const SamplerDesc& SamplerDesc); ~SamplerD3D11Impl(); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject** ppInterface ) final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface) override final; virtual ID3D11SamplerState* GetD3D11SamplerState()override final{ return m_pd3dSampler; } diff --git a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h index 7dd8680e..98d92676 100644 --- a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h @@ -47,7 +47,7 @@ public: void* pNativeWndHandle); ~SwapChainD3D11Impl(); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual void Present(Uint32 SyncInterval)override final; virtual void Resize( Uint32 NewWidth, Uint32 NewHeight )override final; diff --git a/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h b/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h index 56ef5bc3..1333bd8e 100644 --- a/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h @@ -51,7 +51,7 @@ public: const TextureData* pInitData = nullptr); ~TextureBaseD3D11(); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual ID3D11Resource* GetD3D11Texture()override final{ return m_pd3d11Texture; } diff --git a/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h index 3fcea2da..423b6dee 100644 --- a/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h @@ -49,7 +49,7 @@ public: ID3D11View* pD3D11View, bool bIsDefaultView); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual ID3D11View* GetD3D11View()override final { diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 03327e15..0c205533 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -214,7 +214,7 @@ namespace Diligent #ifdef DEVELOPMENT auto* pShaderD3D11 = ValidatedCast(ppdbgShaders[s]); - VERIFY_EXPR( ShaderTypeInd == static_cast(pShaderD3D11->GetShaderTypeIndex()) ); + VERIFY_EXPR( ShaderTypeInd == static_cast(GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType)) ); #endif auto& Cache = pShaderResBindingD3D11->GetResourceCache(s); @@ -360,7 +360,7 @@ namespace Diligent #ifdef DEVELOPMENT auto* pShaderD3D11 = ValidatedCast(ppdbgShaders[s]); - VERIFY_EXPR( ShaderTypeInd == static_cast(pShaderD3D11->GetShaderTypeIndex()) ); + VERIFY_EXPR( ShaderTypeInd == static_cast(GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType)) ); #endif auto& Cache = pShaderResBindingD3D11->GetResourceCache(s); -- cgit v1.2.3 From 2808b9f671e29e27a4d04f6c8ea21c40d8c573eb Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 3 Mar 2019 18:09:24 -0800 Subject: Almost completed refactoring d3d11 backend to comply with the new API --- .../include/PipelineStateD3D11Impl.h | 10 + .../GraphicsEngineD3D11/include/ShaderD3D11Impl.h | 16 +- .../include/ShaderResourceBindingD3D11Impl.h | 16 +- .../include/ShaderResourceCacheD3D11.h | 4 +- .../include/ShaderResourceLayoutD3D11.h | 105 +++++--- .../include/ShaderResourcesD3D11.h | 21 +- .../src/DeviceContextD3D11Impl.cpp | 27 +- .../src/PipelineStateD3D11Impl.cpp | 109 +++++++- .../src/RenderDeviceD3D11Impl.cpp | 6 +- .../GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp | 9 +- .../src/ShaderResourceBindingD3D11Impl.cpp | 56 ++-- .../src/ShaderResourceLayoutD3D11.cpp | 292 +++++++++++---------- .../src/ShaderResourcesD3D11.cpp | 3 +- 13 files changed, 421 insertions(+), 253 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index 04e0edc0..583190aa 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -72,6 +72,14 @@ public: virtual bool IsCompatibleWith(const IPipelineState *pPSO)const override final; + virtual void BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags)override final; + + virtual Uint32 GetStaticVariableCount(SHADER_TYPE ShaderType) const override final; + + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, const Char* Name) override final; + + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, Uint32 Index) override final; + SRBMemoryAllocator& GetSRBMemoryAllocator() { return m_SRBMemAllocator; @@ -102,6 +110,8 @@ private: // SRB memory allocator must be defined before the default shader res binding SRBMemoryAllocator m_SRBMemAllocator; + + Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; }; } diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h index c5005c4e..35a50a08 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h @@ -55,6 +55,16 @@ public: IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_ShaderD3D11, TShaderBase); + virtual Uint32 GetResourceCount()const override final + { + return m_pShaderResources->GetTotalResources(); + } + + virtual ShaderResourceDesc GetResource(Uint32 Index)const override final + { + return m_pShaderResources->GetShaderResourceDesc(Index); + } + virtual ID3D11DeviceChild* GetD3D11Shader()override final { return m_pShader; @@ -62,13 +72,15 @@ public: ID3DBlob* GetBytecode(){return m_pShaderByteCode;} - Uint32 GetShaderTypeIndex()const{return m_ShaderTypeIndex;} + const std::shared_ptr& GetD3D11Resources()const{return m_pShaderResources;} private: /// D3D11 shader CComPtr m_pShader; - Uint32 m_ShaderTypeIndex; // VS == 0, PS == 1, GS == 2, HS == 3, DS == 4, CS == 5 + // ShaderResources class instance must be referenced through the shared pointer, because + // it is referenced by ShaderResourceLayoutD3D11 class instances + std::shared_ptr m_pShaderResources; }; } diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h index 9f175f1d..b3274b21 100755 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h @@ -48,15 +48,15 @@ public: bool IsInternal); ~ShaderResourceBindingD3D11Impl(); - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject** ppInterface )override final; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual void BindResources(Uint32 ShaderFlags, IResourceMapping* pResMapping, Uint32 Flags)override final; - virtual IShaderVariable* GetVariable(SHADER_TYPE ShaderType, const char *Name)override final; + virtual IShaderResourceVariable* GetVariable(SHADER_TYPE ShaderType, const char *Name)override final; virtual Uint32 GetVariableCount(SHADER_TYPE ShaderType) const override final; - virtual IShaderVariable* GetVariable(SHADER_TYPE ShaderType, Uint32 Index)override final; + virtual IShaderResourceVariable* GetVariable(SHADER_TYPE ShaderType, Uint32 Index)override final; virtual void InitializeStaticResources(const IPipelineState* pPipelineState)override final; @@ -74,14 +74,14 @@ public: private: // The caches are indexed by the shader order in the PSO, not shader index - ShaderResourceCacheD3D11* m_pBoundResourceCaches = nullptr; - ShaderResourceLayoutD3D11* m_pResourceLayouts = nullptr; + ShaderResourceCacheD3D11* m_pBoundResourceCaches = nullptr; + ShaderResourceLayoutD3D11* m_pResourceLayouts = nullptr; - Int8 m_ShaderTypeIndex[6] = {}; + Int8 m_ShaderTypeIndex[6] = {}; // Resource layout index in m_ResourceLayouts[] array for every shader stage - Int8 m_ResourceLayoutIndex[6]; - Uint8 m_NumActiveShaders = 0; + Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; + Uint8 m_NumActiveShaders = 0; bool m_bIsStaticResourcesBound = false; }; diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h index 02cde863..f25dff7c 100755 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h @@ -117,8 +117,8 @@ public: static size_t GetRequriedMemorySize(const class ShaderResourcesD3D11& Resources); - void Initialize(const class ShaderResourcesD3D11& Resources, class IMemoryAllocator &MemAllocator); - void Initialize(Uint32 CBCount, Uint32 SRVCount, Uint32 SamplerCount, Uint32 UAVCount, class IMemoryAllocator &MemAllocator); + void Initialize(const class ShaderResourcesD3D11& Resources, class IMemoryAllocator& MemAllocator); + void Initialize(Uint32 CBCount, Uint32 SRVCount, Uint32 SamplerCount, Uint32 UAVCount, class IMemoryAllocator& MemAllocator); void Destroy(class IMemoryAllocator& MemAllocator); diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h index c6dd714b..6666777b 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h @@ -41,10 +41,19 @@ class IMemoryAllocator; /// Diligent::ShaderResourceLayoutD3D11 class /// http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-layout/ +// sizeof(ShaderResourceLayoutD3D11) == 64 (x64) class ShaderResourceLayoutD3D11 { public: - ShaderResourceLayoutD3D11(IObject& Owner); + ShaderResourceLayoutD3D11(IObject& Owner, + IRenderDevice* pRenderDevice, + std::shared_ptr pSrcResources, + const PipelineResourceLayoutDesc& ResourceLayout, + const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes, + ShaderResourceCacheD3D11& ResourceCache, + IMemoryAllocator& ResCacheDataAllocator, + IMemoryAllocator& ResLayoutDataAllocator); ~ShaderResourceLayoutD3D11(); // No copies, only moves are allowed @@ -58,14 +67,7 @@ public: const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, Uint32 NumAllowedTypes); - void Initialize(std::shared_ptr pSrcResources, - const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, - Uint32 NumVarTypes, - ShaderResourceCacheD3D11& ResourceCache, - IMemoryAllocator& ResCacheDataAllocator, - IMemoryAllocator& ResLayoutDataAllocator); - - void CopyResources(ShaderResourceCacheD3D11& DstCache); + void CopyResources(ShaderResourceCacheD3D11& DstCache)const; using ShaderVariableD3D11Base = ShaderVariableD3DBase; @@ -86,7 +88,7 @@ public: BindResource(ppObjects[elem], FirstElement+elem); } - __forceinline bool IsBound(Uint32 ArrayIndex); + __forceinline bool IsBound(Uint32 ArrayIndex)const; }; struct TexSRVBindInfo final : ShaderVariableD3D11Base @@ -184,8 +186,10 @@ public: { SamplerBindInfo( const D3DShaderResourceAttribs& ResourceAttribs, ShaderResourceLayoutD3D11& ParentResLayout, - SHADER_RESOURCE_VARIABLE_TYPE VariableType ) : - ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType) + SHADER_RESOURCE_VARIABLE_TYPE VariableType, + RefCntAutoPtr _pStaticSampler) : + ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType), + pStaticSampler(std::move(_pStaticSampler)) {} // Non-virtual function @@ -199,17 +203,15 @@ public: } __forceinline bool IsBound(Uint32 ArrayIndex)const; - - bool IsStaticSampler()const - { - UNEXPECTED("Not implemented"); - return false; - } + + RefCntAutoPtr pStaticSampler; }; // dbgResourceCache is only used for sanity check and as a remainder that the resource cache must be alive // while Layout is alive void BindResources( IResourceMapping* pResourceMapping, Uint32 Flags, const ShaderResourceCacheD3D11& dbgResourceCache ); + + void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache)const; #ifdef DEVELOPMENT bool dvpVerifyBindings()const; @@ -246,28 +248,34 @@ public: template<> Uint32 GetNumResources () const { return GetNumBufUAVs(); } template<> Uint32 GetNumResources () const { return GetNumSamplers(); } -private: - const Char* GetShaderName()const { return m_pResources->GetShaderName(); } - // No need to use shared pointer, as the resource cache is either part of the same - // ShaderD3D11Impl object, or ShaderResourceBindingD3D11Impl object - ShaderResourceCacheD3D11* m_pResourceCache = nullptr; +private: + +/* 0 */ IObject& m_Owner; +/* 8 */ std::shared_ptr m_pResources; - std::unique_ptr > m_ResourceBuffer; + // No need to use shared pointer, as the resource cache is either part of the same + // ShaderD3D11Impl object, or ShaderResourceBindingD3D11Impl object +/*24*/ ShaderResourceCacheD3D11& m_ResourceCache; - // Offsets in bytes - using OffsetType = Uint16; - OffsetType m_TexSRVsOffset = 0; - OffsetType m_TexUAVsOffset = 0; - OffsetType m_BuffSRVsOffset = 0; - OffsetType m_BuffUAVsOffset = 0; - OffsetType m_SamplerOffset = 0; - OffsetType m_MemorySize = 0; +/*32*/ std::unique_ptr > m_ResourceBuffer; + // Offsets in bytes + using OffsetType = Uint16; +/*48*/ OffsetType m_TexSRVsOffset = 0; +/*50*/ OffsetType m_TexUAVsOffset = 0; +/*52*/ OffsetType m_BuffSRVsOffset = 0; +/*54*/ OffsetType m_BuffUAVsOffset = 0; +/*56*/ OffsetType m_SamplerOffset = 0; +/*58*/ OffsetType m_MemorySize = 0; +/*60 - 64*/ +/*64*/ // End of data + + template OffsetType GetResourceOffset()const; template<> OffsetType GetResourceOffset() const { return 0; } template<> OffsetType GetResourceOffset () const { return m_TexSRVsOffset; } @@ -327,8 +335,37 @@ private: HandleSampler(GetResource(s)); } - std::shared_ptr m_pResources; - IObject& m_Owner; + template + void HandleConstResources(THandleCB HandleCB, + THandleTexSRV HandleTexSRV, + THandleTexUAV HandleTexUAV, + THandleBufSRV HandleBufSRV, + THandleBufUAV HandleBufUAV, + THandleSampler HandleSampler)const + { + for (Uint32 cb = 0; cb < GetNumResources(); ++cb) + HandleCB(GetConstResource(cb)); + + for (Uint32 t = 0; t < GetNumResources(); ++t) + HandleTexSRV(GetConstResource(t)); + + for (Uint32 u = 0; u < GetNumResources(); ++u) + HandleTexUAV(GetConstResource(u)); + + for (Uint32 s = 0; s < GetNumResources(); ++s) + HandleBufSRV(GetConstResource(s)); + + for (Uint32 u = 0; u < GetNumResources(); ++u) + HandleBufUAV(GetConstResource(u)); + + for (Uint32 s = 0; s < GetNumResources(); ++s) + HandleSampler(GetConstResource(s)); + } friend class ShaderVariableIndexLocator; friend class ShaderVariableLocator; diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h index a241b486..c2c3a34d 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.h @@ -28,7 +28,7 @@ // ShaderResourcesD3D11 are created by ShaderD3D11Impl instances. They are then referenced by ShaderResourceLayoutD3D11 objects, which are in turn -// created by instances of ShaderResourceBindingsD3D11Impl (and ShaderD3D11Impl too) +// created by instances of ShaderResourceBindingsD3D11Impl and PipelineStateD3D11Impl // // _________________ // | | @@ -64,13 +64,17 @@ // | | shared_ptr | | shared_ptr| | | | | // | ShaderD3D11Impl |--------------->| ShaderResourcesD3D11 |<---------------| ShaderResourceLayoutD3D11 |<-----| ShaderResourceBindingD3D11Impl | // |_________________| |______________________| | |___________________________| |________________________________| -// | A | -// V | | -// ________ | | _______ ________________________________ +// A | +// | | +// __________ | | _______ ________________________________ // | | shared_ptr | | | | | | // | ShaderResourceLayoutD3D11 |------------------- ----| ShaderResourceLayoutD3D11 |<-----| ShaderResourceBindingD3D11Impl | // |___________________________| |___________________________| |________________________________| -// +// A +// ___________|______________ +// | | +// | PipelineStateD3D11Impl | +// |__________________________| // #include @@ -111,8 +115,7 @@ public: class ShaderResourceCacheD3D11& ResourceCache)const; #endif - const Char* GetShaderName() const { return m_ShaderName; } - + private: using MaxBindPointType = Int8; @@ -126,10 +129,6 @@ private: static_assert(D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT-1 <= MaxAllowedBindPoint, "Not enough bits to represent max SRV slot"); static_assert(D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT-1 <= MaxAllowedBindPoint, "Not enough bits to represent max Sampler slot"); static_assert(D3D11_PS_CS_UAV_REGISTER_COUNT-1 <= MaxAllowedBindPoint, "Not enough bits to represent max UAV slot"); - - // ShaderResourcesD3D11 is part of the ShaderD3D11Impl object, so we can simply - // reference shader name without the need to copy it - const Char* const m_ShaderName; }; } diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 0c205533..f531ab9a 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -151,19 +151,16 @@ namespace Diligent VERIFY_EXPR(pPSO != nullptr); static_assert(TransitionResources || CommitResources, "At least one of TransitionResources or CommitResources flags is expected to be true"); -#ifdef DEVELOPMENT - auto pdbgPipelineStateD3D11 = ValidatedCast( pPSO ); - auto ppdbgShaders = pdbgPipelineStateD3D11->GetShaders(); -#endif + auto* pPipelineStateD3D11 = ValidatedCast(pPSO); if (pShaderResourceBinding == nullptr) { #ifdef DEVELOPMENT bool ResourcesPresent = false; - for (Uint32 s = 0; s < pdbgPipelineStateD3D11->GetNumShaders(); ++s) + for (Uint32 s = 0; s < pPipelineStateD3D11->GetNumShaders(); ++s) { - auto* pShaderD3D11 = ValidatedCast(ppdbgShaders[s]); - if (pShaderD3D11->GetResources()->GetTotalResources() > 0) + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); + if (pShaderD3D11->GetD3D11Resources()->GetTotalResources() > 0) ResourcesPresent = true; } @@ -179,7 +176,7 @@ namespace Diligent auto pShaderResBindingD3D11 = ValidatedCast(pShaderResourceBinding); #ifdef DEVELOPMENT - if (pdbgPipelineStateD3D11->IsIncompatibleWith(pShaderResourceBinding->GetPipelineState())) + if (pPipelineStateD3D11->IsIncompatibleWith(pShaderResourceBinding->GetPipelineState())) { LOG_ERROR_MESSAGE("Shader resource binding does not match Pipeline State"); return; @@ -187,16 +184,14 @@ namespace Diligent #endif auto NumShaders = pShaderResBindingD3D11->GetNumActiveShaders(); - VERIFY(NumShaders == pdbgPipelineStateD3D11->GetNumShaders(), "Number of active shaders in shader resource binding is not consistent with the number of shaders in the pipeline state"); + VERIFY(NumShaders == pPipelineStateD3D11->GetNumShaders(), "Number of active shaders in shader resource binding is not consistent with the number of shaders in the pipeline state"); #ifdef DEVELOPMENT bool StaticResourcesPresent = false; for (Uint32 s = 0; s < NumShaders; ++s) { - pShaderResBindingD3D11->GetResourceLayout(s).dvpVerifyBindings(); - // Static resource bindings are verified in BindStaticShaderResources() - auto* pShaderD3D11 = ValidatedCast(ppdbgShaders[s]); - if (pShaderD3D11->GetStaticResourceLayout().GetTotalResourceCount() > 0) + const auto& StaticResLayout = pPipelineStateD3D11->GetStaticResourceLayout(s); + if (StaticResLayout.GetTotalResourceCount() > 0) StaticResourcesPresent = true; } // Static resource bindings are verified in BindStaticShaderResources() @@ -213,7 +208,7 @@ namespace Diligent auto ShaderTypeInd = pShaderResBindingD3D11->GetActiveShaderTypeIndex(s); #ifdef DEVELOPMENT - auto* pShaderD3D11 = ValidatedCast(ppdbgShaders[s]); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); VERIFY_EXPR( ShaderTypeInd == static_cast(GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType)) ); #endif @@ -359,7 +354,7 @@ namespace Diligent auto ShaderTypeInd = pShaderResBindingD3D11->GetActiveShaderTypeIndex(s); #ifdef DEVELOPMENT - auto* pShaderD3D11 = ValidatedCast(ppdbgShaders[s]); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); VERIFY_EXPR( ShaderTypeInd == static_cast(GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType)) ); #endif @@ -595,7 +590,7 @@ namespace Diligent if (CommitResources && (m_DebugFlags & (Uint32)EngineD3D11DebugFlags::VerifyCommittedShaderResources) != 0) { // Use full resource layout to verify that all required resources are committed - pShaderD3D11->GetResources()->dvpVerifyCommittedResources( + pShaderD3D11->GetD3D11Resources()->dvpVerifyCommittedResources( m_CommittedD3D11CBs[ShaderTypeInd], m_CommittedD3D11SRVs[ShaderTypeInd], m_CommittedD3D11SRVResources[ShaderTypeInd], diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 90828884..eec91b20 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -27,6 +27,7 @@ #include "RenderDeviceD3D11Impl.h" #include "ShaderResourceBindingD3D11Impl.h" #include "EngineMemory.h" +#include "ShaderD3D11Impl.h" namespace Diligent { @@ -50,7 +51,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun { LOG_ERROR_AND_THROW(GetShaderTypeLiteralName(SHADER_TYPE_COMPUTE), " shader is expeceted while ", GetShaderTypeLiteralName(m_pCS->GetDesc().ShaderType), " provided"); } - m_ShaderResourceLayoutHash = pCS->GetResources()->GetHash(); + m_ShaderResourceLayoutHash = pCS->GetD3D11Resources()->GetHash(); } else { @@ -64,7 +65,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun LOG_ERROR_AND_THROW( GetShaderTypeLiteralName(ExpectedType), " shader is expeceted while ", GetShaderTypeLiteralName(m_p##ShortName->GetDesc().ShaderType)," provided" ); \ } \ if(pShader!=nullptr) \ - HashCombine(m_ShaderResourceLayoutHash, pShader->GetResources()->GetHash() ); \ + HashCombine(m_ShaderResourceLayoutHash, pShader->GetD3D11Resources()->GetHash() ); \ } INIT_SHADER(VS, SHADER_TYPE_VERTEX); @@ -110,19 +111,51 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun } } - if(PipelineDesc.SRBAllocationGranularity > 1) + auto* pStaticResLayoutRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", m_NumShaders * sizeof(ShaderResourceLayoutD3D11)); + m_pStaticResourceLayouts = reinterpret_cast(pStaticResLayoutRawMem); + + auto* pResCacheRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceCacheD3D11", m_NumShaders * sizeof(ShaderResourceCacheD3D11)); + m_pStaticResourceCaches = reinterpret_cast(pResCacheRawMem); + + const auto& ResourceLayout = PipelineDesc.ResourceLayout; + std::array ShaderResLayoutDataSizes = {}; + std::array ShaderResCacheDataSizes = {}; + for (Uint32 s = 0; s < m_NumShaders; ++s) { - std::array ShaderResLayoutDataSizes = {}; - std::array ShaderResCacheDataSizes = {}; - for (Uint32 s = 0; s < m_NumShaders; ++s) + auto* pShader = GetShader(s); + const auto& ShaderResources = *pShader->GetD3D11Resources(); + + new (m_pStaticResourceCaches+s) ShaderResourceCacheD3D11; + // Do not initialize the cache as this will be performed by the resource layout + + // Shader resource layout will only contain dynamic and mutable variables + const SHADER_RESOURCE_VARIABLE_TYPE StaticVarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_STATIC}; + new (m_pStaticResourceLayouts + s) + ShaderResourceLayoutD3D11 + { + *this, + pRenderDeviceD3D11, + pShader->GetD3D11Resources(), + m_Desc.ResourceLayout, + StaticVarTypes, + _countof(StaticVarTypes), + m_pStaticResourceCaches[s], + GetRawAllocator(), + GetRawAllocator() + }; + + m_pStaticResourceLayouts[s].SetStaticSamplers(m_pStaticResourceCaches[s]); + + if (PipelineDesc.SRBAllocationGranularity > 1) { - auto* pShader = GetShader(s); - const auto& ShaderResources = *pShader->GetResources(); - std::array AllowedVarTypes = { SHADER_VARIABLE_TYPE_MUTABLE, SHADER_VARIABLE_TYPE_DYNAMIC }; - ShaderResLayoutDataSizes[s] = ShaderResourceLayoutD3D11::GetRequiredMemorySize(ShaderResources, AllowedVarTypes.data(), static_cast(AllowedVarTypes.size())); + const SHADER_RESOURCE_VARIABLE_TYPE SRBVarTypes[] = { SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC }; + ShaderResLayoutDataSizes[s] = ShaderResourceLayoutD3D11::GetRequiredMemorySize(ShaderResources, ResourceLayout, SRBVarTypes, _countof(SRBVarTypes)); ShaderResCacheDataSizes[s] = ShaderResourceCacheD3D11::GetRequriedMemorySize(ShaderResources); } + } + if (PipelineDesc.SRBAllocationGranularity > 1) + { m_SRBMemAllocator.Initialize(PipelineDesc.SRBAllocationGranularity, m_NumShaders, ShaderResLayoutDataSizes.data(), m_NumShaders, ShaderResCacheDataSizes.data()); } } @@ -130,6 +163,18 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun PipelineStateD3D11Impl::~PipelineStateD3D11Impl() { + for (Uint32 s = 0; s < m_NumShaders; ++s) + { + m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); + m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); + } + GetRawAllocator().Free(m_pStaticResourceCaches); + + for (Uint32 l = 0; l < m_NumShaders; ++l) + { + m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); + } + GetRawAllocator().Free(m_pStaticResourceLayouts); } IMPLEMENT_QUERY_INTERFACE( PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase ) @@ -183,11 +228,11 @@ bool PipelineStateD3D11Impl::IsCompatibleWith(const IPipelineState* pPSO)const { auto* pShader0 = GetShader(s); auto* pShader1 = pPSOD3D11->GetShader(s); - if (pShader0->GetShaderTypeIndex() != pShader1->GetShaderTypeIndex()) + if (pShader0->GetDesc().ShaderType != pShader1->GetDesc().ShaderType) return false; - const ShaderResourcesD3D11* pRes0 = pShader0->GetResources().get(); - const ShaderResourcesD3D11* pRes1 = pShader1->GetResources().get(); - if (!pRes0->IsCompatibleWith(*pRes1)) + const auto& Res0 = *pShader0->GetD3D11Resources(); + const auto& Res1 = *pShader1->GetD3D11Resources(); + if (!Res0.IsCompatibleWith(Res1)) return false; } @@ -236,4 +281,40 @@ ID3D11ComputeShader* PipelineStateD3D11Impl::GetD3D11ComputeShader() return static_cast(pCSD3D11->GetD3D11Shader()); } + +void PipelineStateD3D11Impl::BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags) +{ + for (Uint32 s=0; s < m_NumShaders; ++s) + { + m_pStaticResourceLayouts[s].BindResources(pResourceMapping, Flags, m_pStaticResourceCaches[s]); + } +} + +Uint32 PipelineStateD3D11Impl::GetStaticVariableCount(SHADER_TYPE ShaderType) const +{ + const auto LayoutInd = m_ResourceLayoutIndex[GetShaderTypeIndex(ShaderType)]; + if (LayoutInd < 0) + return 0; + + return m_pStaticResourceLayouts[LayoutInd].GetTotalResourceCount(); +} + +IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticShaderVariable(SHADER_TYPE ShaderType, const Char* Name) +{ + const auto LayoutInd = m_ResourceLayoutIndex[GetShaderTypeIndex(ShaderType)]; + if (LayoutInd < 0) + return nullptr; + + return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Name); +} + +IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticShaderVariable(SHADER_TYPE ShaderType, Uint32 Index) +{ + const auto LayoutInd = m_ResourceLayoutIndex[GetShaderTypeIndex(ShaderType)]; + if (LayoutInd < 0) + return nullptr; + + return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Index); +} + } diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp index 2d08f430..86eae24a 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp @@ -134,13 +134,13 @@ void RenderDeviceD3D11Impl :: CreateBuffer(const BufferDesc& BuffDesc, const Buf ); } -void RenderDeviceD3D11Impl :: CreateShader(const ShaderCreationAttribs& ShaderCreationAttribs, IShader** ppShader) +void RenderDeviceD3D11Impl :: CreateShader(const ShaderCreateInfo& ShaderCI, IShader** ppShader) { - CreateDeviceObject( "shader", ShaderCreationAttribs.Desc, ppShader, + CreateDeviceObject( "shader", ShaderCI.Desc, ppShader, [&]() { ShaderD3D11Impl* pShaderD3D11( NEW_RC_OBJ(m_ShaderObjAllocator, "ShaderD3D11Impl instance", ShaderD3D11Impl) - (this, ShaderCreationAttribs ) ); + (this, ShaderCI) ); pShaderD3D11->QueryInterface( IID_Shader, reinterpret_cast(ppShader) ); OnCreateDeviceObject( pShaderD3D11 ); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp index 3af91c50..677cd40c 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp @@ -34,8 +34,7 @@ ShaderD3D11Impl::ShaderD3D11Impl(IReferenceCounters* pRefCounters, RenderDeviceD3D11Impl* pRenderDeviceD3D11, const ShaderCreateInfo& ShaderCI) : TShaderBase(pRefCounters, pRenderDeviceD3D11, ShaderCI.Desc), - ShaderD3DBase(ShaderCI), - m_ShaderTypeIndex(Diligent::GetShaderTypeIndex(ShaderCI.Desc.ShaderType)) + ShaderD3DBase(ShaderCI) { auto *pDeviceD3D11 = pRenderDeviceD3D11->GetD3D11Device(); switch (ShaderCI.Desc.ShaderType) @@ -74,6 +73,12 @@ ShaderD3D11Impl::ShaderD3D11Impl(IReferenceCounters* pRefCounters, DEV_CHECK_ERR(SUCCEEDED(hr), "Failed to set shader name"); } + // Load shader resources + auto& Allocator = GetRawAllocator(); + auto* pRawMem = ALLOCATE(Allocator, "Allocator for ShaderResources", sizeof(ShaderResourcesD3D11)); + auto* pResources = new (pRawMem) ShaderResourcesD3D11(pRenderDeviceD3D11, m_pShaderByteCode, m_Desc, ShaderCI.UseCombinedTextureSamplers ? ShaderCI.CombinedSamplerSuffix : nullptr); + m_pShaderResources.reset(pResources, STDDeleterRawMem(Allocator)); + // Byte code is only required for the vertex shader to create input layout if( ShaderCI.Desc.ShaderType != SHADER_TYPE_VERTEX ) m_pShaderByteCode.Release(); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 3cce0055..0091cc54 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -37,24 +37,22 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( IReferenceCounte TBase( pRefCounters, pPSO, IsInternal ), m_bIsStaticResourcesBound(false) { - for(size_t s=0; s < _countof(m_ResourceLayoutIndex); ++s) - m_ResourceLayoutIndex[s] = -1; + m_NumActiveShaders = static_cast(pPSO->GetNumShaders()); - auto ppShaders = pPSO->GetShaders(); - m_NumActiveShaders = static_cast( pPSO->GetNumShaders() ); - - auto *pResLayoutRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", m_NumActiveShaders * sizeof(ShaderResourceLayoutD3D11)); + auto* pResLayoutRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", m_NumActiveShaders * sizeof(ShaderResourceLayoutD3D11)); m_pResourceLayouts = reinterpret_cast(pResLayoutRawMem); - auto *pResCacheRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceCacheD3D11", m_NumActiveShaders * sizeof(ShaderResourceCacheD3D11)); + auto* pResCacheRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceCacheD3D11", m_NumActiveShaders * sizeof(ShaderResourceCacheD3D11)); m_pBoundResourceCaches = reinterpret_cast(pResCacheRawMem); + + auto* pRenderDevice = pPSO->GetDevice(); + const auto& PSODesc = pPSO->GetDesc(); // Reserve memory for resource layouts for (Uint8 s = 0; s < m_NumActiveShaders; ++s) { - auto *pShaderD3D11 = ValidatedCast(ppShaders[s]); - auto ShaderInd = pShaderD3D11->GetShaderTypeIndex(); - VERIFY_EXPR(static_cast(ShaderInd) == GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType)); + auto* pShaderD3D11 = pPSO->GetShader(s); + auto ShaderInd = GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType); auto& SRBMemAllocator = pPSO->GetSRBMemoryAllocator(); auto& ResCacheDataAllocator = SRBMemAllocator.GetResourceCacheDataAllocator(s); @@ -62,17 +60,29 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( IReferenceCounte // Initialize resource cache to have enough space to contain all shader resources, including static ones // Static resources are copied before resources are committed - const auto& Resources = *pShaderD3D11->GetResources(); + const auto& Resources = *pShaderD3D11->GetD3D11Resources(); new (m_pBoundResourceCaches+s) ShaderResourceCacheD3D11; m_pBoundResourceCaches[s].Initialize(Resources, ResCacheDataAllocator); // Shader resource layout will only contain dynamic and mutable variables // http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-cache#Shader-Resource-Cache-Initialization - SHADER_VARIABLE_TYPE VarTypes[] = {SHADER_VARIABLE_TYPE_MUTABLE, SHADER_VARIABLE_TYPE_DYNAMIC}; - new (m_pResourceLayouts + s) ShaderResourceLayoutD3D11(*this); - m_pResourceLayouts[s].Initialize(pShaderD3D11->GetResources(), VarTypes, _countof(VarTypes), m_pBoundResourceCaches[s], ResCacheDataAllocator, ResLayoutDataAllocator); - - Resources.SetStaticSamplers(m_pBoundResourceCaches[s]); + SHADER_RESOURCE_VARIABLE_TYPE VarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; + new (m_pResourceLayouts + s) + ShaderResourceLayoutD3D11 + { + *this, + pRenderDevice, + pShaderD3D11->GetD3D11Resources(), + PSODesc.ResourceLayout, + VarTypes, + _countof(VarTypes), + m_pBoundResourceCaches[s], + ResCacheDataAllocator, + ResLayoutDataAllocator + }; + + m_pResourceLayouts[s].SetStaticSamplers(m_pBoundResourceCaches[s]); + pPSO->GetStaticResourceLayout(s).SetStaticSamplers(m_pBoundResourceCaches[s]); m_ResourceLayoutIndex[ShaderInd] = s; m_ShaderTypeIndex[s] = static_cast(ShaderInd); @@ -99,7 +109,7 @@ ShaderResourceBindingD3D11Impl::~ShaderResourceBindingD3D11Impl() IMPLEMENT_QUERY_INTERFACE( ShaderResourceBindingD3D11Impl, IID_ShaderResourceBindingD3D11, TBase ) -void ShaderResourceBindingD3D11Impl::BindResources(Uint32 ShaderFlags, IResourceMapping *pResMapping, Uint32 Flags) +void ShaderResourceBindingD3D11Impl::BindResources(Uint32 ShaderFlags, IResourceMapping* pResMapping, Uint32 Flags) { for(Uint32 ResLayoutInd = 0; ResLayoutInd < m_NumActiveShaders; ++ResLayoutInd) { @@ -135,9 +145,10 @@ void ShaderResourceBindingD3D11Impl::InitializeStaticResources(const IPipelineSt for (Uint32 shader = 0; shader < NumShaders; ++shader) { + const auto& StaticResLayout = pPSOD3D11->GetStaticResourceLayout(shader); auto* pShaderD3D11 = ValidatedCast(ppShaders[shader]); #ifdef DEVELOPMENT - if (!pShaderD3D11->GetStaticResourceLayout().dvpVerifyBindings()) + if (!StaticResLayout.dvpVerifyBindings()) { LOG_ERROR_MESSAGE("Static resources in SRB of PSO '", pPSOD3D11->GetDesc().Name, "' will not be successfully initialized " "because not all static resource bindings in shader '", pShaderD3D11->GetDesc().Name, "' are valid. " @@ -147,17 +158,18 @@ void ShaderResourceBindingD3D11Impl::InitializeStaticResources(const IPipelineSt #endif #ifdef _DEBUG - auto ShaderTypeInd = pShaderD3D11->GetShaderTypeIndex(); + auto ShaderTypeInd = GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType); auto ResourceLayoutInd = m_ResourceLayoutIndex[ShaderTypeInd]; VERIFY_EXPR(ResourceLayoutInd == static_cast(shader) ); #endif - pShaderD3D11->GetStaticResourceLayout().CopyResources( m_pBoundResourceCaches[shader] ); + StaticResLayout.CopyResources(m_pBoundResourceCaches[shader]); + //StaticResLayout.SetStaticSamplers(m_pBoundResourceCaches[shader]); } m_bIsStaticResourcesBound = true; } -IShaderVariable* ShaderResourceBindingD3D11Impl::GetVariable(SHADER_TYPE ShaderType, const char* Name) +IShaderResourceVariable* ShaderResourceBindingD3D11Impl::GetVariable(SHADER_TYPE ShaderType, const char* Name) { auto Ind = GetShaderTypeIndex(ShaderType); VERIFY_EXPR(Ind >= 0 && Ind < _countof(m_ResourceLayoutIndex)); @@ -185,7 +197,7 @@ Uint32 ShaderResourceBindingD3D11Impl::GetVariableCount(SHADER_TYPE ShaderType) return m_pResourceLayouts[ResLayoutIndex].GetTotalResourceCount(); } -IShaderVariable* ShaderResourceBindingD3D11Impl::GetVariable(SHADER_TYPE ShaderType, Uint32 Index) +IShaderResourceVariable* ShaderResourceBindingD3D11Impl::GetVariable(SHADER_TYPE ShaderType, Uint32 Index) { auto Ind = GetShaderTypeIndex(ShaderType); VERIFY_EXPR(Ind >= 0 && Ind < _countof(m_ResourceLayoutIndex)); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index c6cf6f6b..6839e29d 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -33,14 +33,11 @@ #include "TextureViewD3D11.h" #include "SamplerD3D11Impl.h" #include "ShaderD3D11Impl.h" +#include "ShaderResourceVariableBase.h" namespace Diligent { -ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& Owner) : - m_Owner(Owner) -{ -} ShaderResourceLayoutD3D11::~ShaderResourceLayoutD3D11() { @@ -83,7 +80,7 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, Uint32 NumAllowedTypes) { - auto ResCounters = SrcResources.CountResources(ResourceLayout, SrcResources.GetShaderType(), SrcResources.GetCombinedSamplerSuffix(), AllowedVarTypes, NumAllowedTypes); + auto ResCounters = SrcResources.CountResources(ResourceLayout, AllowedVarTypes, NumAllowedTypes); auto MemSize = ResCounters.NumCBs * sizeof(ConstBuffBindInfo) + ResCounters.NumTexSRVs * sizeof(TexSRVBindInfo) + ResCounters.NumTexUAVs * sizeof(TexUAVBindInfo) + @@ -93,23 +90,26 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D return MemSize; } -#if 0 -void ShaderResourceLayoutD3D11::Initialize(std::shared_ptr pSrcResources, - const SHADER_VARIABLE_TYPE* VarTypes, - Uint32 NumVarTypes, - ShaderResourceCacheD3D11& ResourceCache, - IMemoryAllocator& ResCacheDataAllocator, - IMemoryAllocator& ResLayoutDataAllocator) + +ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& Owner, + IRenderDevice* pRenderDevice, + std::shared_ptr pSrcResources, + const PipelineResourceLayoutDesc& ResourceLayout, + const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes, + ShaderResourceCacheD3D11& ResourceCache, + IMemoryAllocator& ResCacheDataAllocator, + IMemoryAllocator& ResLayoutDataAllocator) : + m_Owner (Owner), + m_pResources (std::move(pSrcResources)), + m_ResourceCache (ResourceCache) { // http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-layout#Shader-Resource-Layout-Initialization - m_pResources = std::move(pSrcResources); - m_pResourceCache = &ResourceCache; - - auto AllowedTypeBits = GetAllowedTypeBits(VarTypes, NumVarTypes); + const auto AllowedTypeBits = GetAllowedTypeBits(VarTypes, NumVarTypes); // Count total number of resources of allowed types - auto ResCounters = m_pResources->CountResources(VarTypes, NumVarTypes); + auto ResCounters = m_pResources->CountResources(ResourceLayout, VarTypes, NumVarTypes); // Initialize offsets size_t CurrentOffset = 0; @@ -122,7 +122,7 @@ void ShaderResourceLayoutD3D11::Initialize(std::shared_ptr >(pRawMem, ResLayoutDataAllocator); } @@ -158,33 +158,41 @@ void ShaderResourceLayoutD3D11::Initialize(std::shared_ptrProcessResources( - VarTypes, NumVarTypes, - [&](const D3DShaderResourceAttribs& CB, Uint32) { - VERIFY_EXPR( CB.IsAllowedType(AllowedTypeBits) ); - - // Initialize current CB in place, increment CB counter - new (&GetResource(cb++)) ConstBuffBindInfo( CB, *this ); - NumCBSlots = std::max(NumCBSlots, Uint32{CB.BindPoint} + Uint32{CB.BindCount}); + auto VarType = m_pResources->FindVariableType(CB, ResourceLayout); + if (IsAllowedType(VarType, AllowedTypeBits)) + { + // Initialize current CB in place, increment CB counter + new (&GetResource(cb++)) ConstBuffBindInfo(CB, *this, VarType); + NumCBSlots = std::max(NumCBSlots, Uint32{CB.BindPoint} + Uint32{CB.BindCount}); + } }, [&](const D3DShaderResourceAttribs& Sampler, Uint32) { - VERIFY_EXPR(Sampler.IsAllowedType(AllowedTypeBits)); - - // Skip static samplers as they are initialized in the resource cache - if (!Sampler.IsStaticSampler()) + auto VarType = m_pResources->FindVariableType(Sampler, ResourceLayout); + if (IsAllowedType(VarType, AllowedTypeBits)) { + auto StaticSamplerInd = m_pResources->FindStaticSampler(Sampler, ResourceLayout); + RefCntAutoPtr pStaticSampler; + if (StaticSamplerInd >= 0) + { + const auto& StaticSamplerDesc = ResourceLayout.StaticSamplers[StaticSamplerInd]; + pRenderDevice->CreateSampler(StaticSamplerDesc.Desc, &pStaticSampler); + } // Initialize current sampler in place, increment sampler counter - new (&GetResource(sam++)) SamplerBindInfo( Sampler, *this ); + new (&GetResource(sam++)) SamplerBindInfo(Sampler, *this, VarType, std::move(pStaticSampler)); NumSamplerSlots = std::max(NumSamplerSlots, Uint32{Sampler.BindPoint} + Uint32{Sampler.BindCount}); } }, [&](const D3DShaderResourceAttribs& TexSRV, Uint32) { - VERIFY_EXPR( TexSRV.IsAllowedType(AllowedTypeBits) ); + auto VarType = m_pResources->FindVariableType(TexSRV, ResourceLayout); + if (!IsAllowedType(VarType, AllowedTypeBits)) + return; + auto NumSamplers = GetNumSamplers(); VERIFY(sam == NumSamplers, "All samplers must be initialized before texture SRVs"); @@ -192,53 +200,66 @@ void ShaderResourceLayoutD3D11::Initialize(std::shared_ptrGetSampler(TexSRV.GetSamplerId()); - DEV_CHECK_ERR(AssignedSamplerAttribs.GetVariableType() == TexSRV.GetVariableType(), - "The type (", GetShaderVariableTypeLiteralName(TexSRV.GetVariableType()),") of texture SRV variable '", TexSRV.Name, - "' is not consistent with the type (", GetShaderVariableTypeLiteralName(AssignedSamplerAttribs.GetVariableType()), + auto AssignedSamplerType = m_pResources->FindVariableType(AssignedSamplerAttribs, ResourceLayout); + DEV_CHECK_ERR(AssignedSamplerType == VarType, + "The type (", GetShaderVariableTypeLiteralName(VarType),") of texture SRV variable '", TexSRV.Name, + "' is not consistent with the type (", GetShaderVariableTypeLiteralName(AssignedSamplerType), ") of the sampler '", AssignedSamplerAttribs.Name, "' that is assigned to it"); - // Do not assign static sampler to texture SRV as it is initialized directly in the shader resource cache - if (!AssignedSamplerAttribs.IsStaticSampler()) + + bool SamplerFound = false; + for (AssignedSamplerIndex = 0; AssignedSamplerIndex < NumSamplers && !SamplerFound; ++AssignedSamplerIndex) { - for (AssignedSamplerIndex = 0; AssignedSamplerIndex < NumSamplers; ++AssignedSamplerIndex) + const auto& Sampler = GetResource(AssignedSamplerIndex); + SamplerFound = strcmp(Sampler.m_Attribs.Name, AssignedSamplerAttribs.Name) == 0; + if (SamplerFound) { - const auto& Sampler = GetResource(AssignedSamplerIndex); - if (strcmp(Sampler.m_Attribs.Name, AssignedSamplerAttribs.Name) == 0) + if (Sampler.pStaticSampler) + { + // Do not assign static samplers to texture SRV + AssignedSamplerIndex = TexSRVBindInfo::InvalidSamplerIndex; break; + } } - VERIFY(AssignedSamplerIndex < NumSamplers, "Unable to find assigned sampler"); } + VERIFY(SamplerFound, "Unable to find sampler assigned to texture SRV '", TexSRV.Name, "'"); } // Initialize tex SRV in place, increment counter of tex SRVs - new (&GetResource(texSrv++)) TexSRVBindInfo( TexSRV, AssignedSamplerIndex, *this ); + new (&GetResource(texSrv++)) TexSRVBindInfo(TexSRV, AssignedSamplerIndex, *this, VarType); NumSRVSlots = std::max(NumSRVSlots, Uint32{TexSRV.BindPoint} + Uint32{TexSRV.BindCount}); }, [&](const D3DShaderResourceAttribs& TexUAV, Uint32) { - VERIFY_EXPR( TexUAV.IsAllowedType(AllowedTypeBits) ); - - // Initialize tex UAV in place, increment counter of tex UAVs - new (&GetResource(texUav++)) TexUAVBindInfo( TexUAV, *this ); - NumUAVSlots = std::max(NumUAVSlots, Uint32{TexUAV.BindPoint} + Uint32{TexUAV.BindCount}); + auto VarType = m_pResources->FindVariableType(TexUAV, ResourceLayout); + if (IsAllowedType(VarType, AllowedTypeBits)) + { + // Initialize tex UAV in place, increment counter of tex UAVs + new (&GetResource(texUav++)) TexUAVBindInfo(TexUAV, *this, VarType); + NumUAVSlots = std::max(NumUAVSlots, Uint32{TexUAV.BindPoint} + Uint32{TexUAV.BindCount}); + } }, [&](const D3DShaderResourceAttribs& BuffSRV, Uint32) { - VERIFY_EXPR(BuffSRV.IsAllowedType(AllowedTypeBits)); - - // Initialize buff SRV in place, increment counter of buff SRVs - new (&GetResource(bufSrv++)) BuffSRVBindInfo( BuffSRV, *this ); - NumSRVSlots = std::max(NumSRVSlots, Uint32{BuffSRV.BindPoint} + Uint32{BuffSRV.BindCount}); + auto VarType = m_pResources->FindVariableType(BuffSRV, ResourceLayout); + if (IsAllowedType(VarType, AllowedTypeBits)) + { + // Initialize buff SRV in place, increment counter of buff SRVs + new (&GetResource(bufSrv++)) BuffSRVBindInfo(BuffSRV, *this, VarType); + NumSRVSlots = std::max(NumSRVSlots, Uint32{BuffSRV.BindPoint} + Uint32{BuffSRV.BindCount}); + } }, [&](const D3DShaderResourceAttribs& BuffUAV, Uint32) { - VERIFY_EXPR(BuffUAV.IsAllowedType(AllowedTypeBits)); - - // Initialize buff UAV in place, increment counter of buff UAVs - new (&GetResource(bufUav++)) BuffUAVBindInfo( BuffUAV, *this ); - NumUAVSlots = std::max(NumUAVSlots, Uint32{BuffUAV.BindPoint} + Uint32{BuffUAV.BindCount}); + auto VarType = m_pResources->FindVariableType(BuffUAV, ResourceLayout); + if (IsAllowedType(VarType, AllowedTypeBits)) + { + // Initialize buff UAV in place, increment counter of buff UAVs + new (&GetResource(bufUav++)) BuffUAVBindInfo(BuffUAV, *this, VarType); + NumUAVSlots = std::max(NumUAVSlots, Uint32{BuffUAV.BindPoint} + Uint32{BuffUAV.BindCount}); + } } ); @@ -253,22 +274,19 @@ void ShaderResourceLayoutD3D11::Initialize(std::shared_ptrIsInitialized()) + if (!m_ResourceCache.IsInitialized()) { // NOTE that here we are using max bind points required to cache only the shader variables of allowed types! - m_pResourceCache->Initialize(NumCBSlots, NumSRVSlots, NumSamplerSlots, NumUAVSlots, ResCacheDataAllocator); + m_ResourceCache.Initialize(NumCBSlots, NumSRVSlots, NumSamplerSlots, NumUAVSlots, ResCacheDataAllocator); } } -#endif -void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache) +void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache)const { - VERIFY(m_pResourceCache, "Resource cache must not be null"); - - VERIFY( DstCache.GetCBCount() >= m_pResourceCache->GetCBCount(), "Dst cache is not large enough to contain all CBs" ); - VERIFY( DstCache.GetSRVCount() >= m_pResourceCache->GetSRVCount(), "Dst cache is not large enough to contain all SRVs" ); - VERIFY( DstCache.GetSamplerCount() >= m_pResourceCache->GetSamplerCount(), "Dst cache is not large enough to contain all samplers" ); - VERIFY( DstCache.GetUAVCount() >= m_pResourceCache->GetUAVCount(), "Dst cache is not large enough to contain all UAVs" ); + VERIFY( DstCache.GetCBCount() >= m_ResourceCache.GetCBCount(), "Dst cache is not large enough to contain all CBs" ); + VERIFY( DstCache.GetSRVCount() >= m_ResourceCache.GetSRVCount(), "Dst cache is not large enough to contain all SRVs" ); + VERIFY( DstCache.GetSamplerCount() >= m_ResourceCache.GetSamplerCount(), "Dst cache is not large enough to contain all samplers" ); + VERIFY( DstCache.GetUAVCount() >= m_ResourceCache.GetUAVCount(), "Dst cache is not large enough to contain all UAVs" ); ShaderResourceCacheD3D11::CachedCB* CachedCBs = nullptr; ID3D11Buffer** d3d11CBs = nullptr; @@ -278,10 +296,10 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache ID3D11SamplerState** d3d11Samplers = nullptr; ShaderResourceCacheD3D11::CachedResource* CachedUAVResources = nullptr; ID3D11UnorderedAccessView** d3d11UAVs = nullptr; - m_pResourceCache->GetCBArrays (CachedCBs, d3d11CBs); - m_pResourceCache->GetSRVArrays (CachedSRVResources, d3d11SRVs); - m_pResourceCache->GetSamplerArrays(CachedSamplers, d3d11Samplers); - m_pResourceCache->GetUAVArrays (CachedUAVResources, d3d11UAVs); + m_ResourceCache.GetCBArrays (CachedCBs, d3d11CBs); + m_ResourceCache.GetSRVArrays (CachedSRVResources, d3d11SRVs); + m_ResourceCache.GetSamplerArrays(CachedSamplers, d3d11Samplers); + m_ResourceCache.GetUAVArrays (CachedUAVResources, d3d11UAVs); ShaderResourceCacheD3D11::CachedCB* DstCBs = nullptr; @@ -297,12 +315,12 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache DstCache.GetSamplerArrays(DstSamplers, DstD3D11Samplers); DstCache.GetUAVArrays (DstUAVResources, DstD3D11UAVs); - HandleResources( + HandleConstResources( [&](const ConstBuffBindInfo& cb) { for(auto CBSlot = cb.m_Attribs.BindPoint; CBSlot < cb.m_Attribs.BindPoint+cb.m_Attribs.BindCount; ++CBSlot) { - VERIFY_EXPR(CBSlot < m_pResourceCache->GetCBCount() && CBSlot < DstCache.GetCBCount()); + VERIFY_EXPR(CBSlot < m_ResourceCache.GetCBCount() && CBSlot < DstCache.GetCBCount()); DstCBs [CBSlot] = CachedCBs[CBSlot]; DstD3D11CBs[CBSlot] = d3d11CBs [CBSlot]; } @@ -312,7 +330,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache { for(auto SRVSlot = ts.m_Attribs.BindPoint; SRVSlot < ts.m_Attribs.BindPoint + ts.m_Attribs.BindCount; ++SRVSlot) { - VERIFY_EXPR(SRVSlot < m_pResourceCache->GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); + VERIFY_EXPR(SRVSlot < m_ResourceCache.GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); DstSRVResources[SRVSlot] = CachedSRVResources[SRVSlot]; DstD3D11SRVs [SRVSlot] = d3d11SRVs [SRVSlot]; } @@ -322,7 +340,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache { for(auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) { - VERIFY_EXPR(UAVSlot < m_pResourceCache->GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); + VERIFY_EXPR(UAVSlot < m_ResourceCache.GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); DstUAVResources[UAVSlot] = CachedUAVResources[UAVSlot]; DstD3D11UAVs [UAVSlot] = d3d11UAVs [UAVSlot]; } @@ -332,7 +350,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache { for(auto SRVSlot = srv.m_Attribs.BindPoint; SRVSlot < srv.m_Attribs.BindPoint + srv.m_Attribs.BindCount; ++SRVSlot) { - VERIFY_EXPR(SRVSlot < m_pResourceCache->GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); + VERIFY_EXPR(SRVSlot < m_ResourceCache.GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); DstSRVResources[SRVSlot] = CachedSRVResources[SRVSlot]; DstD3D11SRVs [SRVSlot] = d3d11SRVs [SRVSlot]; } @@ -342,7 +360,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache { for(auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) { - VERIFY_EXPR(UAVSlot < m_pResourceCache->GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); + VERIFY_EXPR(UAVSlot < m_ResourceCache.GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); DstUAVResources[UAVSlot] = CachedUAVResources[UAVSlot]; DstD3D11UAVs [UAVSlot] = d3d11UAVs [UAVSlot]; } @@ -350,10 +368,10 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const SamplerBindInfo& sam) { - VERIFY(!sam.IsStaticSampler(), "Variables are not created for static samplers"); + VERIFY(!sam.pStaticSampler, "Variables are not created for static samplers"); for(auto SamSlot = sam.m_Attribs.BindPoint; SamSlot < sam.m_Attribs.BindPoint + sam.m_Attribs.BindCount; ++SamSlot) { - VERIFY_EXPR(SamSlot < m_pResourceCache->GetSamplerCount() && SamSlot < DstCache.GetSamplerCount()); + VERIFY_EXPR(SamSlot < m_ResourceCache.GetSamplerCount() && SamSlot < DstCache.GetSamplerCount()); DstSamplers [SamSlot] = CachedSamplers[SamSlot]; DstD3D11Samplers[SamSlot] = d3d11Samplers [SamSlot]; } @@ -361,6 +379,24 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache ); } +void ShaderResourceLayoutD3D11::SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache)const +{ + auto NumCachedSamplers = ResourceCache.GetSamplerCount(); + for (Uint32 s = 0; s < GetNumResources(); ++s) + { + auto& Sampler = GetConstResource(s); + if (Sampler.pStaticSampler) + { + const auto& SamAttribs = Sampler.m_Attribs; + auto* pSamplerD3D11Impl = const_cast(Sampler.pStaticSampler.RawPtr()); + // Limiting EndBindPoint is required when initializing static samplers in a Shader's static cache + auto EndBindPoint = std::min( static_cast(SamAttribs.BindPoint) + SamAttribs.BindCount, NumCachedSamplers); + for (Uint32 BindPoint = SamAttribs.BindPoint; BindPoint < EndBindPoint; ++BindPoint ) + ResourceCache.SetSampler(BindPoint, pSamplerD3D11Impl); + } + } +} + #define LOG_RESOURCE_BINDING_ERROR(ResType, pResource, Attribs, ArrayInd, ShaderName, ...)\ do{ \ const auto* ResName = pResource->GetDesc().Name; \ @@ -375,9 +411,8 @@ do{ void ShaderResourceLayoutD3D11::ConstBuffBindInfo::BindResource(IDeviceObject* pBuffer, Uint32 ArrayIndex) { - VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); - auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; + auto& ResourceCache = m_ParentResLayout.m_ResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -408,13 +443,10 @@ void ShaderResourceLayoutD3D11::ConstBuffBindInfo::BindResource(IDeviceObject* p -bool ShaderResourceLayoutD3D11::ConstBuffBindInfo::IsBound(Uint32 ArrayIndex) +bool ShaderResourceLayoutD3D11::ConstBuffBindInfo::IsBound(Uint32 ArrayIndex)const { - auto* pResourceCache = m_ParentResLayout.m_pResourceCache; - VERIFY(pResourceCache, "Resource cache is null"); VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - - return pResourceCache->IsCBBound(m_Attribs.BindPoint + ArrayIndex); + return m_ParentResLayout.m_ResourceCache.IsCBBound(m_Attribs.BindPoint + ArrayIndex); } @@ -449,9 +481,8 @@ bool dbgVerifyViewType( const char* ViewTypeName, void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pView, Uint32 ArrayIndex) { - VERIFY(m_ParentResLayout.m_pResourceCache, "Resource cache is null"); DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); - auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; + auto& ResourceCache = m_ParentResLayout.m_ResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -476,7 +507,7 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie if (ValidSamplerAssigned()) { auto& Sampler = m_ParentResLayout.GetResource(SamplerIndex); - VERIFY(!Sampler.IsStaticSampler(), "Static samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); + VERIFY(!Sampler.pStaticSampler, "Static samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); VERIFY_EXPR(Sampler.m_Attribs.BindCount == m_Attribs.BindCount || Sampler.m_Attribs.BindCount == 1); auto SamplerBindPoint = Sampler.m_Attribs.BindPoint + (Sampler.m_Attribs.BindCount != 1 ? ArrayIndex : 0); @@ -514,10 +545,9 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSampler, Uint32 ArrayIndex) { - VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); - auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; - VERIFY(!IsStaticSampler(), "Cannot bind sampler to a static sampler"); + auto& ResourceCache = m_ParentResLayout.m_ResourceCache; + VERIFY(!pStaticSampler, "Cannot bind sampler to a static sampler"); // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -550,9 +580,8 @@ void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSa void ShaderResourceLayoutD3D11::BuffSRVBindInfo::BindResource(IDeviceObject* pView, Uint32 ArrayIndex) { - VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); - auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; + auto& ResourceCache = m_ParentResLayout.m_ResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -581,9 +610,8 @@ void ShaderResourceLayoutD3D11::BuffSRVBindInfo::BindResource(IDeviceObject* pVi void ShaderResourceLayoutD3D11::TexUAVBindInfo::BindResource(IDeviceObject* pView, Uint32 ArrayIndex) { - VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); - auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; + auto& ResourceCache = m_ParentResLayout.m_ResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -612,9 +640,8 @@ void ShaderResourceLayoutD3D11::TexUAVBindInfo::BindResource(IDeviceObject* pVie void ShaderResourceLayoutD3D11::BuffUAVBindInfo::BindResource(IDeviceObject* pView, Uint32 ArrayIndex) { - VERIFY(m_ParentResLayout.m_pResourceCache != nullptr, "Resource cache is null"); DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); - auto& ResourceCache = *m_ParentResLayout.m_pResourceCache; + auto& ResourceCache = m_ParentResLayout.m_ResourceCache; // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -642,48 +669,33 @@ void ShaderResourceLayoutD3D11::BuffUAVBindInfo::BindResource(IDeviceObject* pVi bool ShaderResourceLayoutD3D11::TexSRVBindInfo::IsBound(Uint32 ArrayIndex)const { - auto* pResourceCache = m_ParentResLayout.m_pResourceCache; - VERIFY(pResourceCache != nullptr, "Resource cache is null"); VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - - return pResourceCache->IsSRVBound(m_Attribs.BindPoint + ArrayIndex, true); + return m_ParentResLayout.m_ResourceCache.IsSRVBound(m_Attribs.BindPoint + ArrayIndex, true); } bool ShaderResourceLayoutD3D11::BuffSRVBindInfo::IsBound(Uint32 ArrayIndex)const { - auto* pResourceCache = m_ParentResLayout.m_pResourceCache; - VERIFY(pResourceCache, "Resource cache is null"); VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - - return pResourceCache->IsSRVBound(m_Attribs.BindPoint + ArrayIndex, false); + return m_ParentResLayout.m_ResourceCache.IsSRVBound(m_Attribs.BindPoint + ArrayIndex, false); } bool ShaderResourceLayoutD3D11::TexUAVBindInfo::IsBound(Uint32 ArrayIndex)const { - auto* pResourceCache = m_ParentResLayout.m_pResourceCache; - VERIFY(pResourceCache, "Resource cache is null"); VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - - return pResourceCache->IsUAVBound(m_Attribs.BindPoint + ArrayIndex, true); + return m_ParentResLayout.m_ResourceCache.IsUAVBound(m_Attribs.BindPoint + ArrayIndex, true); } bool ShaderResourceLayoutD3D11::BuffUAVBindInfo::IsBound(Uint32 ArrayIndex)const { - auto* pResourceCache = m_ParentResLayout.m_pResourceCache; - VERIFY(pResourceCache, "Resource cache is null"); VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - - return pResourceCache->IsUAVBound(m_Attribs.BindPoint + ArrayIndex, false); + return m_ParentResLayout.m_ResourceCache.IsUAVBound(m_Attribs.BindPoint + ArrayIndex, false); } bool ShaderResourceLayoutD3D11::SamplerBindInfo::IsBound(Uint32 ArrayIndex)const { - auto* pResourceCache = m_ParentResLayout.m_pResourceCache; - VERIFY(pResourceCache, "Resource cache is null"); VERIFY_EXPR(ArrayIndex < m_Attribs.BindCount); - - return pResourceCache->IsSamplerBound(m_Attribs.BindPoint + ArrayIndex); + return m_ParentResLayout.m_ResourceCache.IsSamplerBound(m_Attribs.BindPoint + ArrayIndex); } @@ -699,7 +711,7 @@ public: } template - void Bind( ResourceType &Res) + void Bind( ResourceType& Res) { if ( (Flags & (1 << Res.GetType())) == 0 ) return; @@ -732,7 +744,7 @@ private: void ShaderResourceLayoutD3D11::BindResources( IResourceMapping* pResourceMapping, Uint32 Flags, const ShaderResourceCacheD3D11& dbgResourceCache ) { - VERIFY(&dbgResourceCache == m_pResourceCache, "Resource cache does not match the cache provided at initialization"); + VERIFY(&dbgResourceCache == &m_ResourceCache, "Resource cache does not match the cache provided at initialization"); if (pResourceMapping == nullptr) { @@ -812,8 +824,16 @@ IShaderResourceVariable* ShaderResourceLayoutD3D11::GetShaderVariable(const Char if (!m_pResources->IsUsingCombinedTextureSamplers()) { - if(auto* pSampler = GetResourceByName(Name)) - return pSampler; + auto NumSamplers = GetNumResources(); + for (Uint32 s = 0; s < NumSamplers; ++s) + { + auto& Sampler = GetResource(s); + if (strcmp(Sampler.m_Attribs.Name, Name) == 0) + { + // Do not return static samplers + return Sampler.pStaticSampler ? nullptr : &Sampler; + } + } } return nullptr; @@ -971,17 +991,15 @@ do{ \ LOG_ERROR_MESSAGE( "No resource is bound to ", VarType, " variable '", Attrs.Name, "[", BindPt-Attrs.BindPoint, "]' in shader '", GetShaderName(), "'" );\ }while(false) - m_pResourceCache->dbgVerifyCacheConsistency(); + m_ResourceCache.dbgVerifyCacheConsistency(); bool BindingsOK = true; - // Use const_cast to avoid duplication of the HandleResources() function - // The function actually changes nothing - const_cast(this)->HandleResources( + HandleConstResources( [&](const ConstBuffBindInfo& cb) { for (Uint32 BindPoint = cb.m_Attribs.BindPoint; BindPoint < Uint32{cb.m_Attribs.BindPoint} + cb.m_Attribs.BindCount; ++BindPoint) { - if (!m_pResourceCache->IsCBBound(BindPoint)) + if (!m_ResourceCache.IsCBBound(BindPoint)) { LOG_MISSING_BINDING("constant buffer", cb.m_Attribs, BindPoint); BindingsOK = false; @@ -993,7 +1011,7 @@ do{ \ { for (Uint32 BindPoint = ts.m_Attribs.BindPoint; BindPoint < Uint32{ts.m_Attribs.BindPoint} + ts.m_Attribs.BindCount; ++BindPoint) { - if (!m_pResourceCache->IsSRVBound(BindPoint, true)) + if (!m_ResourceCache.IsSRVBound(BindPoint, true)) { LOG_MISSING_BINDING("texture", ts.m_Attribs, BindPoint); BindingsOK = false; @@ -1009,14 +1027,14 @@ do{ \ { ShaderResourceCacheD3D11::CachedSampler* pCachedSamplers = nullptr; ID3D11SamplerState** ppCachedD3D11Samplers = nullptr; - m_pResourceCache->GetSamplerArrays(pCachedSamplers, ppCachedD3D11Samplers); - VERIFY_EXPR(Sampler.m_Attribs.BindPoint < m_pResourceCache->GetSamplerCount()); + m_ResourceCache.GetSamplerArrays(pCachedSamplers, ppCachedD3D11Samplers); + VERIFY_EXPR(Sampler.m_Attribs.BindPoint < m_ResourceCache.GetSamplerCount()); const auto& CachedSampler = pCachedSamplers[Sampler.m_Attribs.BindPoint]; ShaderResourceCacheD3D11::CachedResource* pCachedResources = nullptr; ID3D11ShaderResourceView** ppCachedD3D11Resources = nullptr; - m_pResourceCache->GetSRVArrays(pCachedResources, ppCachedD3D11Resources); - VERIFY_EXPR(BindPoint < m_pResourceCache->GetSRVCount()); + m_ResourceCache.GetSRVArrays(pCachedResources, ppCachedD3D11Resources); + VERIFY_EXPR(BindPoint < m_ResourceCache.GetSRVCount()); auto& CachedResource = pCachedResources[BindPoint]; if (CachedResource.pView) { @@ -1036,7 +1054,7 @@ do{ \ { for (Uint32 BindPoint = uav.m_Attribs.BindPoint; BindPoint < Uint32{uav.m_Attribs.BindPoint} + uav.m_Attribs.BindCount; ++BindPoint) { - if (!m_pResourceCache->IsUAVBound(BindPoint, true)) + if (!m_ResourceCache.IsUAVBound(BindPoint, true)) { LOG_MISSING_BINDING("texture UAV", uav.m_Attribs, BindPoint); BindingsOK = false; @@ -1048,7 +1066,7 @@ do{ \ { for (Uint32 BindPoint = buf.m_Attribs.BindPoint; BindPoint < Uint32{buf.m_Attribs.BindPoint} + buf.m_Attribs.BindCount; ++BindPoint) { - if (!m_pResourceCache->IsSRVBound(BindPoint, false)) + if (!m_ResourceCache.IsSRVBound(BindPoint, false)) { LOG_MISSING_BINDING("buffer", buf.m_Attribs, BindPoint); BindingsOK = false; @@ -1060,7 +1078,7 @@ do{ \ { for (Uint32 BindPoint = uav.m_Attribs.BindPoint; BindPoint < Uint32{uav.m_Attribs.BindPoint} + uav.m_Attribs.BindCount; ++BindPoint) { - if (!m_pResourceCache->IsUAVBound(BindPoint, false)) + if (!m_ResourceCache.IsUAVBound(BindPoint, false)) { LOG_MISSING_BINDING("buffer UAV", uav.m_Attribs, BindPoint); BindingsOK = false; @@ -1072,7 +1090,7 @@ do{ \ { for (Uint32 BindPoint = sam.m_Attribs.BindPoint; BindPoint < Uint32{sam.m_Attribs.BindPoint} + sam.m_Attribs.BindCount; ++BindPoint) { - if (!m_pResourceCache->IsSamplerBound(BindPoint)) + if (!m_ResourceCache.IsSamplerBound(BindPoint)) { LOG_MISSING_BINDING("sampler", sam.m_Attribs, BindPoint); BindingsOK = false; diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp index 78dfcf63..a12a8ff6 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp @@ -37,8 +37,7 @@ ShaderResourcesD3D11::ShaderResourcesD3D11(RenderDeviceD3D11Impl* pDeviceD3D11Im ID3DBlob* pShaderBytecode, const ShaderDesc& ShdrDesc, const char* CombinedSamplerSuffix) : - ShaderResources(ShdrDesc.ShaderType), - m_ShaderName(ShdrDesc.Name) + ShaderResources(ShdrDesc.ShaderType) { class NewResourceHandler { -- cgit v1.2.3 From c3ce1b6cc0214c0c67ac6c75b742b92b18af30d8 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 3 Mar 2019 20:50:04 -0800 Subject: Final changes to complete d3d11 backend refactor --- .../include/PipelineStateD3D11Impl.h | 5 + .../include/ShaderResourceLayoutD3D11.h | 11 +-- .../src/PipelineStateD3D11Impl.cpp | 58 +++++++++++- .../src/ShaderResourceBindingD3D11Impl.cpp | 7 +- .../src/ShaderResourceLayoutD3D11.cpp | 103 +++++++++------------ 5 files changed, 110 insertions(+), 74 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index 583190aa..2cbe0ebc 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -97,8 +97,10 @@ public: return m_pStaticResourceCaches[s]; } + void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd)const; private: + CComPtr m_pd3d11BlendState; CComPtr m_pd3d11RasterizerState; CComPtr m_pd3d11DepthStencilState; @@ -112,6 +114,9 @@ private: SRBMemoryAllocator m_SRBMemAllocator; Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; + + Uint16 m_StaticSamplerOffsets[MaxShadersInPipeline+1] = {}; + std::vector< std::pair< const D3DShaderResourceAttribs&, RefCntAutoPtr > > m_StaticSamplers; }; } diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h index 6666777b..bfc39727 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h @@ -46,7 +46,6 @@ class ShaderResourceLayoutD3D11 { public: ShaderResourceLayoutD3D11(IObject& Owner, - IRenderDevice* pRenderDevice, std::shared_ptr pSrcResources, const PipelineResourceLayoutDesc& ResourceLayout, const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, @@ -186,10 +185,8 @@ public: { SamplerBindInfo( const D3DShaderResourceAttribs& ResourceAttribs, ShaderResourceLayoutD3D11& ParentResLayout, - SHADER_RESOURCE_VARIABLE_TYPE VariableType, - RefCntAutoPtr _pStaticSampler) : - ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType), - pStaticSampler(std::move(_pStaticSampler)) + SHADER_RESOURCE_VARIABLE_TYPE VariableType) : + ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs, VariableType) {} // Non-virtual function @@ -203,16 +200,12 @@ public: } __forceinline bool IsBound(Uint32 ArrayIndex)const; - - RefCntAutoPtr pStaticSampler; }; // dbgResourceCache is only used for sanity check and as a remainder that the resource cache must be alive // while Layout is alive void BindResources( IResourceMapping* pResourceMapping, Uint32 Flags, const ShaderResourceCacheD3D11& dbgResourceCache ); - void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache)const; - #ifdef DEVELOPMENT bool dvpVerifyBindings()const; #endif diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index eec91b20..09754861 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -118,6 +118,20 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun m_pStaticResourceCaches = reinterpret_cast(pResCacheRawMem); const auto& ResourceLayout = PipelineDesc.ResourceLayout; + +#ifdef DEVELOPMENT + { + const ShaderResources* pResources[MaxShadersInPipeline] = {}; + for (Uint32 s = 0; s < m_NumShaders; ++s) + { + auto* pShader = GetShader(s); + pResources[s] = &(*pShader->GetD3D11Resources()); + } + ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, m_NumShaders); + } +#endif + + decltype(m_StaticSamplers) StaticSamplers; std::array ShaderResLayoutDataSizes = {}; std::array ShaderResCacheDataSizes = {}; for (Uint32 s = 0; s < m_NumShaders; ++s) @@ -134,7 +148,6 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun ShaderResourceLayoutD3D11 { *this, - pRenderDeviceD3D11, pShader->GetD3D11Resources(), m_Desc.ResourceLayout, StaticVarTypes, @@ -144,7 +157,20 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun GetRawAllocator() }; - m_pStaticResourceLayouts[s].SetStaticSamplers(m_pStaticResourceCaches[s]); + // Initialize static samplers + for(Uint32 sam = 0; sam < ShaderResources.GetNumSamplers(); ++sam) + { + const auto& SamplerAttribs = ShaderResources.GetSampler(sam); + auto SrcStaticSamplerInd = ShaderResources.FindStaticSampler(SamplerAttribs, ResourceLayout); + if (SrcStaticSamplerInd >= 0) + { + const auto& SrcStaticSamplerInfo = ResourceLayout.StaticSamplers[SrcStaticSamplerInd]; + RefCntAutoPtr pStaticSampler; + pRenderDeviceD3D11->CreateSampler(SrcStaticSamplerInfo.Desc, &pStaticSampler); + StaticSamplers.emplace_back(SamplerAttribs, std::move(pStaticSampler)); + } + } + m_StaticSamplerOffsets[s + 1] = static_cast(StaticSamplers.size()); if (PipelineDesc.SRBAllocationGranularity > 1) { @@ -152,12 +178,25 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun ShaderResLayoutDataSizes[s] = ShaderResourceLayoutD3D11::GetRequiredMemorySize(ShaderResources, ResourceLayout, SRBVarTypes, _countof(SRBVarTypes)); ShaderResCacheDataSizes[s] = ShaderResourceCacheD3D11::GetRequriedMemorySize(ShaderResources); } + + auto ShaderInd = GetShaderTypeIndex(pShader->GetDesc().ShaderType); + m_ResourceLayoutIndex[ShaderInd] = static_cast(s); } if (PipelineDesc.SRBAllocationGranularity > 1) { m_SRBMemAllocator.Initialize(PipelineDesc.SRBAllocationGranularity, m_NumShaders, ShaderResLayoutDataSizes.data(), m_NumShaders, ShaderResCacheDataSizes.data()); } + + m_StaticSamplers.reserve(StaticSamplers.size()); + for(auto& Sam : StaticSamplers) + m_StaticSamplers.emplace_back(std::move(Sam)); + + for (Uint32 s = 0; s < m_NumShaders; ++s) + { + // Initialize static samplers in the static resource cache to avoid warning messages + SetStaticSamplers(m_pStaticResourceCaches[s], s); + } } @@ -317,4 +356,19 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticShaderVariable(SHADER_ return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Index); } +void PipelineStateD3D11Impl::SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd)const +{ + auto NumCachedSamplers = ResourceCache.GetSamplerCount(); + for (Uint32 s = m_StaticSamplerOffsets[ShaderInd]; s < m_StaticSamplerOffsets[ShaderInd+1]; ++s) + { + auto& SamplerInfo = m_StaticSamplers[s]; + const auto& SamAttribs = SamplerInfo.first; + auto* pSamplerD3D11Impl = SamplerInfo.second.RawPtr(); + // Limiting EndBindPoint is required when initializing static samplers in a Shader's static cache + auto EndBindPoint = std::min( static_cast(SamAttribs.BindPoint) + SamAttribs.BindCount, NumCachedSamplers); + for (Uint32 BindPoint = SamAttribs.BindPoint; BindPoint < EndBindPoint; ++BindPoint ) + ResourceCache.SetSampler(BindPoint, pSamplerD3D11Impl); + } +} + } diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 0091cc54..a99da5de 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -45,7 +45,6 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( IReferenceCounte auto* pResCacheRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceCacheD3D11", m_NumActiveShaders * sizeof(ShaderResourceCacheD3D11)); m_pBoundResourceCaches = reinterpret_cast(pResCacheRawMem); - auto* pRenderDevice = pPSO->GetDevice(); const auto& PSODesc = pPSO->GetDesc(); // Reserve memory for resource layouts @@ -71,7 +70,6 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( IReferenceCounte ShaderResourceLayoutD3D11 { *this, - pRenderDevice, pShaderD3D11->GetD3D11Resources(), PSODesc.ResourceLayout, VarTypes, @@ -81,9 +79,6 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( IReferenceCounte ResLayoutDataAllocator }; - m_pResourceLayouts[s].SetStaticSamplers(m_pBoundResourceCaches[s]); - pPSO->GetStaticResourceLayout(s).SetStaticSamplers(m_pBoundResourceCaches[s]); - m_ResourceLayoutIndex[ShaderInd] = s; m_ShaderTypeIndex[s] = static_cast(ShaderInd); } @@ -163,7 +158,7 @@ void ShaderResourceBindingD3D11Impl::InitializeStaticResources(const IPipelineSt VERIFY_EXPR(ResourceLayoutInd == static_cast(shader) ); #endif StaticResLayout.CopyResources(m_pBoundResourceCaches[shader]); - //StaticResLayout.SetStaticSamplers(m_pBoundResourceCaches[shader]); + pPSOD3D11->SetStaticSamplers(m_pBoundResourceCaches[shader], shader); } m_bIsStaticResourcesBound = true; diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index 6839e29d..12ca400a 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -80,7 +80,9 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, Uint32 NumAllowedTypes) { - auto ResCounters = SrcResources.CountResources(ResourceLayout, AllowedVarTypes, NumAllowedTypes); + // Skip static samplers as they are initialized directly in the resource cache by the PSO + constexpr bool CountStaticSamplers = false; + auto ResCounters = SrcResources.CountResources(ResourceLayout, AllowedVarTypes, NumAllowedTypes, CountStaticSamplers); auto MemSize = ResCounters.NumCBs * sizeof(ConstBuffBindInfo) + ResCounters.NumTexSRVs * sizeof(TexSRVBindInfo) + ResCounters.NumTexUAVs * sizeof(TexUAVBindInfo) + @@ -92,7 +94,6 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& Owner, - IRenderDevice* pRenderDevice, std::shared_ptr pSrcResources, const PipelineResourceLayoutDesc& ResourceLayout, const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, @@ -109,7 +110,9 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& const auto AllowedTypeBits = GetAllowedTypeBits(VarTypes, NumVarTypes); // Count total number of resources of allowed types - auto ResCounters = m_pResources->CountResources(ResourceLayout, VarTypes, NumVarTypes); + // Skip static samplers as they are initialized directly in the resource cache by the PSO + constexpr bool CountStaticSamplers = false; + auto ResCounters = m_pResources->CountResources(ResourceLayout, VarTypes, NumVarTypes, CountStaticSamplers); // Initialize offsets size_t CurrentOffset = 0; @@ -175,14 +178,13 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& if (IsAllowedType(VarType, AllowedTypeBits)) { auto StaticSamplerInd = m_pResources->FindStaticSampler(Sampler, ResourceLayout); - RefCntAutoPtr pStaticSampler; if (StaticSamplerInd >= 0) { - const auto& StaticSamplerDesc = ResourceLayout.StaticSamplers[StaticSamplerInd]; - pRenderDevice->CreateSampler(StaticSamplerDesc.Desc, &pStaticSampler); + // Skip static samplers as they are initialized directly in the resource cache by the PSO + return; } // Initialize current sampler in place, increment sampler counter - new (&GetResource(sam++)) SamplerBindInfo(Sampler, *this, VarType, std::move(pStaticSampler)); + new (&GetResource(sam++)) SamplerBindInfo(Sampler, *this, VarType); NumSamplerSlots = std::max(NumSamplerSlots, Uint32{Sampler.BindPoint} + Uint32{Sampler.BindCount}); } }, @@ -207,21 +209,33 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& ") of the sampler '", AssignedSamplerAttribs.Name, "' that is assigned to it"); bool SamplerFound = false; - for (AssignedSamplerIndex = 0; AssignedSamplerIndex < NumSamplers && !SamplerFound; ++AssignedSamplerIndex) + for (AssignedSamplerIndex = 0; AssignedSamplerIndex < NumSamplers; ++AssignedSamplerIndex) { const auto& Sampler = GetResource(AssignedSamplerIndex); SamplerFound = strcmp(Sampler.m_Attribs.Name, AssignedSamplerAttribs.Name) == 0; if (SamplerFound) + break; // Otherwise AssignedSamplerIndex will be incremented + } + + if (!SamplerFound) + { + AssignedSamplerIndex = TexSRVBindInfo::InvalidSamplerIndex; +#ifdef _DEBUG + if (m_pResources->FindStaticSampler(AssignedSamplerAttribs, ResourceLayout) < 0) { - if (Sampler.pStaticSampler) - { - // Do not assign static samplers to texture SRV - AssignedSamplerIndex = TexSRVBindInfo::InvalidSamplerIndex; - break; - } + LOG_ERROR("Unable to find non-static sampler assigned to texture SRV '", TexSRV.Name, "'. This seems to be a bug."); + } +#endif + } + else + { +#ifdef _DEBUG + if (m_pResources->FindStaticSampler(AssignedSamplerAttribs, ResourceLayout) >= 0) + { + LOG_ERROR("Static sampler '", AssignedSamplerAttribs.Name, "' is assigned to texture SRV '", TexSRV.Name, "'. This seems to be a bug."); } +#endif } - VERIFY(SamplerFound, "Unable to find sampler assigned to texture SRV '", TexSRV.Name, "'"); } // Initialize tex SRV in place, increment counter of tex SRVs @@ -318,7 +332,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache HandleConstResources( [&](const ConstBuffBindInfo& cb) { - for(auto CBSlot = cb.m_Attribs.BindPoint; CBSlot < cb.m_Attribs.BindPoint+cb.m_Attribs.BindCount; ++CBSlot) + for (auto CBSlot = cb.m_Attribs.BindPoint; CBSlot < cb.m_Attribs.BindPoint+cb.m_Attribs.BindCount; ++CBSlot) { VERIFY_EXPR(CBSlot < m_ResourceCache.GetCBCount() && CBSlot < DstCache.GetCBCount()); DstCBs [CBSlot] = CachedCBs[CBSlot]; @@ -328,7 +342,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const TexSRVBindInfo& ts) { - for(auto SRVSlot = ts.m_Attribs.BindPoint; SRVSlot < ts.m_Attribs.BindPoint + ts.m_Attribs.BindCount; ++SRVSlot) + for (auto SRVSlot = ts.m_Attribs.BindPoint; SRVSlot < ts.m_Attribs.BindPoint + ts.m_Attribs.BindCount; ++SRVSlot) { VERIFY_EXPR(SRVSlot < m_ResourceCache.GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); DstSRVResources[SRVSlot] = CachedSRVResources[SRVSlot]; @@ -338,7 +352,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const TexUAVBindInfo& uav) { - for(auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) + for (auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) { VERIFY_EXPR(UAVSlot < m_ResourceCache.GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); DstUAVResources[UAVSlot] = CachedUAVResources[UAVSlot]; @@ -348,7 +362,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const BuffSRVBindInfo& srv) { - for(auto SRVSlot = srv.m_Attribs.BindPoint; SRVSlot < srv.m_Attribs.BindPoint + srv.m_Attribs.BindCount; ++SRVSlot) + for (auto SRVSlot = srv.m_Attribs.BindPoint; SRVSlot < srv.m_Attribs.BindPoint + srv.m_Attribs.BindCount; ++SRVSlot) { VERIFY_EXPR(SRVSlot < m_ResourceCache.GetSRVCount() && SRVSlot < DstCache.GetSRVCount()); DstSRVResources[SRVSlot] = CachedSRVResources[SRVSlot]; @@ -358,7 +372,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const BuffUAVBindInfo& uav) { - for(auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) + for (auto UAVSlot = uav.m_Attribs.BindPoint; UAVSlot < uav.m_Attribs.BindPoint + uav.m_Attribs.BindCount; ++UAVSlot) { VERIFY_EXPR(UAVSlot < m_ResourceCache.GetUAVCount() && UAVSlot < DstCache.GetUAVCount()); DstUAVResources[UAVSlot] = CachedUAVResources[UAVSlot]; @@ -368,8 +382,8 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const SamplerBindInfo& sam) { - VERIFY(!sam.pStaticSampler, "Variables are not created for static samplers"); - for(auto SamSlot = sam.m_Attribs.BindPoint; SamSlot < sam.m_Attribs.BindPoint + sam.m_Attribs.BindCount; ++SamSlot) + //VERIFY(!sam.IsStaticSampler, "Variables are not created for static samplers"); + for (auto SamSlot = sam.m_Attribs.BindPoint; SamSlot < sam.m_Attribs.BindPoint + sam.m_Attribs.BindCount; ++SamSlot) { VERIFY_EXPR(SamSlot < m_ResourceCache.GetSamplerCount() && SamSlot < DstCache.GetSamplerCount()); DstSamplers [SamSlot] = CachedSamplers[SamSlot]; @@ -379,24 +393,6 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache ); } -void ShaderResourceLayoutD3D11::SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache)const -{ - auto NumCachedSamplers = ResourceCache.GetSamplerCount(); - for (Uint32 s = 0; s < GetNumResources(); ++s) - { - auto& Sampler = GetConstResource(s); - if (Sampler.pStaticSampler) - { - const auto& SamAttribs = Sampler.m_Attribs; - auto* pSamplerD3D11Impl = const_cast(Sampler.pStaticSampler.RawPtr()); - // Limiting EndBindPoint is required when initializing static samplers in a Shader's static cache - auto EndBindPoint = std::min( static_cast(SamAttribs.BindPoint) + SamAttribs.BindCount, NumCachedSamplers); - for (Uint32 BindPoint = SamAttribs.BindPoint; BindPoint < EndBindPoint; ++BindPoint ) - ResourceCache.SetSampler(BindPoint, pSamplerD3D11Impl); - } - } -} - #define LOG_RESOURCE_BINDING_ERROR(ResType, pResource, Attribs, ArrayInd, ShaderName, ...)\ do{ \ const auto* ResName = pResource->GetDesc().Name; \ @@ -507,7 +503,7 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie if (ValidSamplerAssigned()) { auto& Sampler = m_ParentResLayout.GetResource(SamplerIndex); - VERIFY(!Sampler.pStaticSampler, "Static samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); + //VERIFY(!Sampler.IsStaticSampler, "Static samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); VERIFY_EXPR(Sampler.m_Attribs.BindCount == m_Attribs.BindCount || Sampler.m_Attribs.BindCount == 1); auto SamplerBindPoint = Sampler.m_Attribs.BindPoint + (Sampler.m_Attribs.BindCount != 1 ? ArrayIndex : 0); @@ -547,7 +543,7 @@ void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSa { DEV_CHECK_ERR(ArrayIndex < m_Attribs.BindCount, "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount); auto& ResourceCache = m_ParentResLayout.m_ResourceCache; - VERIFY(!pStaticSampler, "Cannot bind sampler to a static sampler"); + //VERIFY(!IsStaticSampler, "Cannot bind sampler to a static sampler"); // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -807,33 +803,26 @@ IShaderResourceVariable* ShaderResourceLayoutD3D11::GetResourceByName( const Cha IShaderResourceVariable* ShaderResourceLayoutD3D11::GetShaderVariable(const Char* Name) { - if(auto* pCB = GetResourceByName(Name)) + if (auto* pCB = GetResourceByName(Name)) return pCB; - if(auto* pTexSRV = GetResourceByName(Name)) + if (auto* pTexSRV = GetResourceByName(Name)) return pTexSRV; - if(auto* pTexUAV = GetResourceByName(Name)) + if (auto* pTexUAV = GetResourceByName(Name)) return pTexUAV; - if(auto* pBuffSRV = GetResourceByName(Name)) + if (auto* pBuffSRV = GetResourceByName(Name)) return pBuffSRV; - if(auto* pBuffUAV = GetResourceByName(Name)) + if (auto* pBuffUAV = GetResourceByName(Name)) return pBuffUAV; if (!m_pResources->IsUsingCombinedTextureSamplers()) { - auto NumSamplers = GetNumResources(); - for (Uint32 s = 0; s < NumSamplers; ++s) - { - auto& Sampler = GetResource(s); - if (strcmp(Sampler.m_Attribs.Name, Name) == 0) - { - // Do not return static samplers - return Sampler.pStaticSampler ? nullptr : &Sampler; - } - } + // Static samplers are never created in the resource layout + if (auto* pSampler = GetResourceByName(Name)) + return pSampler; } return nullptr; -- cgit v1.2.3 From 05aa92315c10fe7e3a2bb84d69026933e1831f95 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 3 Mar 2019 20:59:33 -0800 Subject: Updated static sampler array in PipelineStateD3D11Impl to use raw allocator --- .../GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h | 12 +++++++++++- Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp | 9 +++++---- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index 2cbe0ebc..71830432 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -116,7 +116,17 @@ private: Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; Uint16 m_StaticSamplerOffsets[MaxShadersInPipeline+1] = {}; - std::vector< std::pair< const D3DShaderResourceAttribs&, RefCntAutoPtr > > m_StaticSamplers; + struct StaticSamplerInfo + { + const D3DShaderResourceAttribs& Attribs; + RefCntAutoPtr pSampler; + StaticSamplerInfo(const D3DShaderResourceAttribs& _Attribs, + RefCntAutoPtr _pSampler) : + Attribs (_Attribs), + pSampler (std::move(_pSampler)) + {} + }; + std::vector< StaticSamplerInfo, STDAllocatorRawMem > m_StaticSamplers; }; } diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 09754861..84bc1ef5 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -36,7 +36,8 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun RenderDeviceD3D11Impl* pRenderDeviceD3D11, const PipelineStateDesc& PipelineDesc) : TPipelineStateBase(pRefCounters, pRenderDeviceD3D11, PipelineDesc), - m_SRBMemAllocator(GetRawAllocator()) + m_SRBMemAllocator(GetRawAllocator()), + m_StaticSamplers(STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")) { if (PipelineDesc.IsComputePipeline) { @@ -131,7 +132,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun } #endif - decltype(m_StaticSamplers) StaticSamplers; + decltype(m_StaticSamplers) StaticSamplers(STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")); std::array ShaderResLayoutDataSizes = {}; std::array ShaderResCacheDataSizes = {}; for (Uint32 s = 0; s < m_NumShaders; ++s) @@ -362,8 +363,8 @@ void PipelineStateD3D11Impl::SetStaticSamplers(ShaderResourceCacheD3D11& Resourc for (Uint32 s = m_StaticSamplerOffsets[ShaderInd]; s < m_StaticSamplerOffsets[ShaderInd+1]; ++s) { auto& SamplerInfo = m_StaticSamplers[s]; - const auto& SamAttribs = SamplerInfo.first; - auto* pSamplerD3D11Impl = SamplerInfo.second.RawPtr(); + const auto& SamAttribs = SamplerInfo.Attribs; + auto* pSamplerD3D11Impl = SamplerInfo.pSampler.RawPtr(); // Limiting EndBindPoint is required when initializing static samplers in a Shader's static cache auto EndBindPoint = std::min( static_cast(SamAttribs.BindPoint) + SamAttribs.BindCount, NumCachedSamplers); for (Uint32 BindPoint = SamAttribs.BindPoint; BindPoint < EndBindPoint; ++BindPoint ) -- cgit v1.2.3 From c72191a465edd01905181aa47cc57e47fcc29b9e Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Mon, 4 Mar 2019 19:17:32 -0800 Subject: Completed D3D12 back-end refactor to comply with the new API --- .../include/PipelineStateD3D11Impl.h | 25 ++++++++++++---------- .../src/ShaderResourceLayoutD3D11.cpp | 8 +++---- .../src/ShaderResourcesD3D11.cpp | 4 ++-- 3 files changed, 20 insertions(+), 17 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index 71830432..81e03ca2 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -50,6 +50,20 @@ public: virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; + + virtual void BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags)override final; + + virtual Uint32 GetStaticVariableCount(SHADER_TYPE ShaderType) const override final; + + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, const Char* Name) override final; + + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, Uint32 Index) override final; + + virtual void CreateShaderResourceBinding( IShaderResourceBinding **ppShaderResourceBinding, bool InitStaticResources )override final; + + virtual bool IsCompatibleWith(const IPipelineState *pPSO)const override final; + + /// Implementation of the IPipelineStateD3D11::GetD3D11BlendState() method. virtual ID3D11BlendState* GetD3D11BlendState()override final; @@ -68,17 +82,6 @@ public: virtual ID3D11HullShader* GetD3D11HullShader()override final; virtual ID3D11ComputeShader* GetD3D11ComputeShader()override final; - virtual void CreateShaderResourceBinding( IShaderResourceBinding **ppShaderResourceBinding, bool InitStaticResources )override final; - - virtual bool IsCompatibleWith(const IPipelineState *pPSO)const override final; - - virtual void BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags)override final; - - virtual Uint32 GetStaticVariableCount(SHADER_TYPE ShaderType) const override final; - - virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, const Char* Name) override final; - - virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, Uint32 Index) override final; SRBMemoryAllocator& GetSRBMemoryAllocator() { diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index 12ca400a..262902b0 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -199,9 +199,9 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& VERIFY(sam == NumSamplers, "All samplers must be initialized before texture SRVs"); Uint32 AssignedSamplerIndex = TexSRVBindInfo::InvalidSamplerIndex; - if (TexSRV.ValidSamplerAssigned()) + if (TexSRV.IsCombinedWithSampler()) { - const auto& AssignedSamplerAttribs = m_pResources->GetSampler(TexSRV.GetSamplerId()); + const auto& AssignedSamplerAttribs = m_pResources->GetCombinedSampler(TexSRV); auto AssignedSamplerType = m_pResources->FindVariableType(AssignedSamplerAttribs, ResourceLayout); DEV_CHECK_ERR(AssignedSamplerType == VarType, "The type (", GetShaderVariableTypeLiteralName(VarType),") of texture SRV variable '", TexSRV.Name, @@ -553,9 +553,9 @@ void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSa if (pSampler && !pSamplerD3D11) LOG_RESOURCE_BINDING_ERROR("sampler", pSampler, m_Attribs, ArrayIndex, m_ParentResLayout.GetShaderName(), "Incorect resource type: sampler is expected."); - if (m_Attribs.ValidTexSRVAssigned()) + if (m_Attribs.IsCombinedWithTexSRV()) { - auto* TexSRVName = m_ParentResLayout.m_pResources->GetTexSRV(m_Attribs.GetTexSRVId()).Name; + auto* TexSRVName = m_ParentResLayout.m_pResources->GetCombinedTextureSRV(m_Attribs).Name; LOG_WARNING_MESSAGE("Texture sampler sampler '", m_Attribs.Name, "' is assigned to texture SRV '", TexSRVName, "' and should not be accessed directly. The sampler is initialized when texture SRV is set to '", TexSRVName, "' variable."); } diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp index a12a8ff6..b80cbccd 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp @@ -252,9 +252,9 @@ void ShaderResourcesD3D11::dvpVerifyCommittedResources(ID3D11Buffer* } } - if (tex.ValidSamplerAssigned()) + if (tex.IsCombinedWithSampler()) { - const auto& SamAttribs = GetSampler( tex.GetSamplerId() ); + const auto& SamAttribs = GetCombinedSampler(tex); VERIFY_EXPR(SamAttribs.IsValidBindPoint()); VERIFY_EXPR(SamAttribs.BindCount == 1 || SamAttribs.BindCount == tex.BindCount); } -- cgit v1.2.3 From f551f6353eaf4efe9b7f26d48822b96ebb0706a0 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Mon, 4 Mar 2019 22:38:04 -0800 Subject: Updated D3D11, D3D12, and Vk EngineFactory headers & structures --- Graphics/GraphicsEngineD3D11/CMakeLists.txt | 5 +- .../include/DeviceContextD3D11Impl.h | 12 +- .../include/RenderDeviceD3D11Impl.h | 13 +- .../interface/EngineD3D11Attribs.h | 70 ---- .../interface/EngineFactoryD3D11.h | 129 +++++++ .../interface/RenderDeviceFactoryD3D11.h | 131 ------- Graphics/GraphicsEngineD3D11/readme.md | 2 +- .../src/DeviceContextD3D11Impl.cpp | 13 +- .../GraphicsEngineD3D11/src/EngineFactoryD3D11.cpp | 382 +++++++++++++++++++++ .../src/RenderDeviceD3D11Impl.cpp | 10 +- .../src/RenderDeviceFactoryD3D11.cpp | 382 --------------------- 11 files changed, 537 insertions(+), 612 deletions(-) delete mode 100644 Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h create mode 100644 Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h delete mode 100644 Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h create mode 100644 Graphics/GraphicsEngineD3D11/src/EngineFactoryD3D11.cpp delete mode 100644 Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/CMakeLists.txt b/Graphics/GraphicsEngineD3D11/CMakeLists.txt index b263d602..f54fa680 100644 --- a/Graphics/GraphicsEngineD3D11/CMakeLists.txt +++ b/Graphics/GraphicsEngineD3D11/CMakeLists.txt @@ -34,11 +34,10 @@ set(INTERFACE interface/BufferD3D11.h interface/BufferViewD3D11.h interface/DeviceContextD3D11.h - interface/EngineD3D11Attribs.h + interface/EngineFactoryD3D11.h interface/FenceD3D11.h interface/PipelineStateD3D11.h interface/RenderDeviceD3D11.h - interface/RenderDeviceFactoryD3D11.h interface/SamplerD3D11.h interface/ShaderD3D11.h interface/ShaderResourceBindingD3D11.h @@ -54,11 +53,11 @@ set(SRC src/CommandListD3D11Impl.cpp src/D3D11TypeConversions.cpp src/DeviceContextD3D11Impl.cpp + src/EngineFactoryD3D11.cpp src/FenceD3D11Impl.cpp src/GUIDDef.cpp src/PipelineStateD3D11Impl.cpp src/RenderDeviceD3D11Impl.cpp - src/RenderDeviceFactoryD3D11.cpp src/SamplerD3D11Impl.cpp src/ShaderD3D11Impl.cpp src/ShaderResourceBindingD3D11Impl.cpp diff --git a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h index d9a0e44d..01a54eeb 100755 --- a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h @@ -53,12 +53,12 @@ class DeviceContextD3D11Impl final : public DeviceContextBase; - DeviceContextD3D11Impl(IReferenceCounters* pRefCounters, - IMemoryAllocator& Allocator, - IRenderDevice* pDevice, - ID3D11DeviceContext* pd3d11DeviceContext, - const struct EngineD3D11Attribs& EngineAttribs, - bool bIsDeferred); + DeviceContextD3D11Impl(IReferenceCounters* pRefCounters, + IMemoryAllocator& Allocator, + IRenderDevice* pDevice, + ID3D11DeviceContext* pd3d11DeviceContext, + const struct EngineD3D11CreateInfo& EngineAttribs, + bool bIsDeferred); virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual void SetPipelineState(IPipelineState* pPipelineState)override final; diff --git a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h index 87189352..b325554f 100644 --- a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h @@ -29,7 +29,6 @@ #include "RenderDeviceD3D11.h" #include "RenderDeviceD3DBase.h" #include "DeviceContextD3D11.h" -#include "EngineD3D11Attribs.h" namespace Diligent { @@ -40,11 +39,11 @@ class RenderDeviceD3D11Impl final : public RenderDeviceD3DBase; - RenderDeviceD3D11Impl( IReferenceCounters* pRefCounters, - IMemoryAllocator& RawMemAllocator, - const EngineD3D11Attribs& EngineAttribs, - ID3D11Device* pd3d11Device, - Uint32 NumDeferredContexts ); + RenderDeviceD3D11Impl( IReferenceCounters* pRefCounters, + IMemoryAllocator& RawMemAllocator, + const EngineD3D11CreateInfo& EngineAttribs, + ID3D11Device* pd3d11Device, + Uint32 NumDeferredContexts ); virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; virtual void CreateBuffer(const BufferDesc& BuffDesc, const BufferData* pBuffData, IBuffer** ppBuffer)override final; @@ -74,7 +73,7 @@ public: private: virtual void TestTextureFormat( TEXTURE_FORMAT TexFormat )override final; - EngineD3D11Attribs m_EngineAttribs; + EngineD3D11CreateInfo m_EngineAttribs; /// D3D11 device CComPtr m_pd3d11Device; diff --git a/Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h b/Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h deleted file mode 100644 index a5208ef5..00000000 --- a/Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright 2015-2019 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -#pragma once - -/// \file -/// Definition of the Engine D3D11 attribs - -#include "../../GraphicsEngine/interface/GraphicsTypes.h" - -namespace Diligent -{ - /// Debug flags that can be specified when creating Direct3D11-based engine implementation. - /// - /// \sa CreateDeviceAndContextsD3D11Type, CreateSwapChainD3D11Type, LoadGraphicsEngineD3D11 - enum class EngineD3D11DebugFlags : Uint32 - { - /// Before executing draw/dispatch command, verify that - /// all required shader resources are bound to the device context - VerifyCommittedShaderResources = 0x01, - - /// Verify that all committed cotext resources are relevant, - /// i.e. they are consistent with the committed resource cache. - /// This is very expensive operation and should generally not be - /// necessary. - VerifyCommittedResourceRelevance = 0x02 - }; - - /// Attributes of the Direct3D11-based engine implementation - struct EngineD3D11Attribs : public EngineCreationAttribs - { - static constexpr Uint32 DefaultAdapterId = 0xFFFFFFFF; - - /// Id of the hardware adapter the engine should be initialized on - Uint32 AdapterId = DefaultAdapterId; - - /// Debug flags. See Diligent::EngineD3D11DebugFlags for a list of allowed values. - /// - /// \sa CreateDeviceAndContextsD3D11Type, CreateSwapChainD3D11Type, LoadGraphicsEngineD3D11 - Uint32 DebugFlags; - - EngineD3D11Attribs() : - DebugFlags(0) - { - #ifdef _DEBUG - DebugFlags = static_cast(EngineD3D11DebugFlags::VerifyCommittedShaderResources); - #endif - } - }; -} diff --git a/Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h b/Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h new file mode 100644 index 00000000..dceab777 --- /dev/null +++ b/Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h @@ -0,0 +1,129 @@ +/* Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +/// \file +/// Declaration of functions that initialize Direct3D11-based engine implementation + +#include + +#include "../../GraphicsEngine/interface/RenderDevice.h" +#include "../../GraphicsEngine/interface/DeviceContext.h" +#include "../../GraphicsEngine/interface/SwapChain.h" + +#if PLATFORM_UNIVERSAL_WINDOWS && defined(ENGINE_DLL) +# include "../../../Common/interface/StringTools.h" +#endif + +namespace Diligent +{ + +class IEngineFactoryD3D11 +{ +public: + virtual void CreateDeviceAndContextsD3D11(const EngineD3D11CreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppContexts, + Uint32 NumDeferredContexts ) = 0; + + virtual void CreateSwapChainD3D11( IRenderDevice* pDevice, + IDeviceContext* pImmediateContext, + const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, + void* pNativeWndHandle, + ISwapChain** ppSwapChain ) = 0; + + virtual void AttachToD3D11Device(void* pd3d11NativeDevice, + void* pd3d11ImmediateContext, + const EngineD3D11CreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppContexts, + Uint32 NumDeferredContexts) = 0; + + virtual void EnumerateHardwareAdapters(Uint32& NumAdapters, + HardwareAdapterAttribs* Adapters) = 0; + + virtual void EnumerateDisplayModes(Uint32 AdapterId, + Uint32 OutputId, + TEXTURE_FORMAT Format, + Uint32& NumDisplayModes, + DisplayModeAttribs* DisplayModes) = 0; +}; + +#if ENGINE_DLL + + typedef IEngineFactoryD3D11* (*GetEngineFactoryD3D11Type)(); + + static bool LoadGraphicsEngineD3D11(GetEngineFactoryD3D11Type &GetFactoryFunc) + { + GetFactoryFunc = nullptr; + std::string LibName = "GraphicsEngineD3D11_"; + +#if _WIN64 + LibName += "64"; +#else + LibName += "32"; +#endif + +#ifdef _DEBUG + LibName += "d"; +#else + LibName += "r"; +#endif + + LibName += ".dll"; +#if PLATFORM_WIN32 + auto hModule = LoadLibraryA( LibName.c_str() ); +#elif PLATFORM_UNIVERSAL_WINDOWS + auto hModule = LoadPackagedLibrary(WidenString(LibName).c_str(), 0); +#else +# error Unexpected platform +#endif + if( hModule == NULL ) + { + std::stringstream ss; + ss << "Failed to load " << LibName << " library.\n"; + OutputDebugStringA(ss.str().c_str()); + return false; + } + + GetFactoryFunc = reinterpret_cast( GetProcAddress(hModule, "GetEngineFactoryD3D11") ); + if( GetFactoryFunc == NULL ) + { + std::stringstream ss; + ss << "Failed to load GetEngineFactoryD3D11() from " << LibName << " library.\n"; + OutputDebugStringA(ss.str().c_str()); + FreeLibrary( hModule ); + return false; + } + + return true; + } +#else + + IEngineFactoryD3D11* GetEngineFactoryD3D11(); + +#endif + +} diff --git a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h b/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h deleted file mode 100644 index a4dd8476..00000000 --- a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h +++ /dev/null @@ -1,131 +0,0 @@ -/* Copyright 2015-2019 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -#pragma once - -/// \file -/// Declaration of functions that initialize Direct3D11-based engine implementation - -#include - -#include "../../GraphicsEngine/interface/RenderDevice.h" -#include "../../GraphicsEngine/interface/DeviceContext.h" -#include "../../GraphicsEngine/interface/SwapChain.h" - -#if PLATFORM_UNIVERSAL_WINDOWS && defined(ENGINE_DLL) -# include "../../../Common/interface/StringTools.h" -#endif - -#include "EngineD3D11Attribs.h" - -namespace Diligent -{ - -class IEngineFactoryD3D11 -{ -public: - virtual void CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, - IRenderDevice** ppDevice, - IDeviceContext** ppContexts, - Uint32 NumDeferredContexts ) = 0; - - virtual void CreateSwapChainD3D11( IRenderDevice* pDevice, - IDeviceContext* pImmediateContext, - const SwapChainDesc& SCDesc, - const FullScreenModeDesc& FSDesc, - void* pNativeWndHandle, - ISwapChain** ppSwapChain ) = 0; - - virtual void AttachToD3D11Device(void* pd3d11NativeDevice, - void* pd3d11ImmediateContext, - const EngineD3D11Attribs& EngineAttribs, - IRenderDevice** ppDevice, - IDeviceContext** ppContexts, - Uint32 NumDeferredContexts) = 0; - - virtual void EnumerateHardwareAdapters(Uint32& NumAdapters, - HardwareAdapterAttribs* Adapters) = 0; - - virtual void EnumerateDisplayModes(Uint32 AdapterId, - Uint32 OutputId, - TEXTURE_FORMAT Format, - Uint32& NumDisplayModes, - DisplayModeAttribs* DisplayModes) = 0; -}; - -#if ENGINE_DLL - - typedef IEngineFactoryD3D11* (*GetEngineFactoryD3D11Type)(); - - static bool LoadGraphicsEngineD3D11(GetEngineFactoryD3D11Type &GetFactoryFunc) - { - GetFactoryFunc = nullptr; - std::string LibName = "GraphicsEngineD3D11_"; - -#if _WIN64 - LibName += "64"; -#else - LibName += "32"; -#endif - -#ifdef _DEBUG - LibName += "d"; -#else - LibName += "r"; -#endif - - LibName += ".dll"; -#if PLATFORM_WIN32 - auto hModule = LoadLibraryA( LibName.c_str() ); -#elif PLATFORM_UNIVERSAL_WINDOWS - auto hModule = LoadPackagedLibrary(WidenString(LibName).c_str(), 0); -#else -# error Unexpected platform -#endif - if( hModule == NULL ) - { - std::stringstream ss; - ss << "Failed to load " << LibName << " library.\n"; - OutputDebugStringA(ss.str().c_str()); - return false; - } - - GetFactoryFunc = reinterpret_cast( GetProcAddress(hModule, "GetEngineFactoryD3D11") ); - if( GetFactoryFunc == NULL ) - { - std::stringstream ss; - ss << "Failed to load GetEngineFactoryD3D11() from " << LibName << " library.\n"; - OutputDebugStringA(ss.str().c_str()); - FreeLibrary( hModule ); - return false; - } - - return true; - } -#else - - IEngineFactoryD3D11* GetEngineFactoryD3D11(); - -#endif - -} diff --git a/Graphics/GraphicsEngineD3D11/readme.md b/Graphics/GraphicsEngineD3D11/readme.md index 3a9dab60..121d4a49 100644 --- a/Graphics/GraphicsEngineD3D11/readme.md +++ b/Graphics/GraphicsEngineD3D11/readme.md @@ -8,7 +8,7 @@ Implementation of Direct3D11 back-end The following code snippet shows how to initialize Diligent Engine in D3D11 mode. ```cpp -#include "RenderDeviceFactoryD3D11.h" +#include "EngineFactoryD3D11.h" using namespace Diligent; // ... diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index f531ab9a..f177586d 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -34,7 +34,6 @@ #include "PipelineStateD3D11Impl.h" #include "SwapChainD3D11.h" #include "ShaderResourceBindingD3D11Impl.h" -#include "EngineD3D11Attribs.h" #include "EngineD3D11Defines.h" #include "CommandListD3D11Impl.h" #include "RenderDeviceD3D11Impl.h" @@ -42,12 +41,12 @@ namespace Diligent { - DeviceContextD3D11Impl::DeviceContextD3D11Impl( IReferenceCounters* pRefCounters, - IMemoryAllocator& Allocator, - IRenderDevice* pDevice, - ID3D11DeviceContext* pd3d11DeviceContext, - const struct EngineD3D11Attribs& EngineAttribs, - bool bIsDeferred ) : + DeviceContextD3D11Impl::DeviceContextD3D11Impl( IReferenceCounters* pRefCounters, + IMemoryAllocator& Allocator, + IRenderDevice* pDevice, + ID3D11DeviceContext* pd3d11DeviceContext, + const struct EngineD3D11CreateInfo& EngineAttribs, + bool bIsDeferred ) : TDeviceContextBase(pRefCounters, pDevice, bIsDeferred), m_pd3d11DeviceContext( pd3d11DeviceContext ), m_DebugFlags(EngineAttribs.DebugFlags), diff --git a/Graphics/GraphicsEngineD3D11/src/EngineFactoryD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/EngineFactoryD3D11.cpp new file mode 100644 index 00000000..d69a94ee --- /dev/null +++ b/Graphics/GraphicsEngineD3D11/src/EngineFactoryD3D11.cpp @@ -0,0 +1,382 @@ +/* Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +/// \file +/// Routines that initialize D3D11-based engine implementation + +#include "pch.h" +#include "EngineFactoryD3D11.h" +#include "RenderDeviceD3D11Impl.h" +#include "DeviceContextD3D11Impl.h" +#include "SwapChainD3D11Impl.h" +#include "D3D11TypeConversions.h" +#include "EngineMemory.h" +#include "EngineFactoryD3DBase.h" +#include +#include + +namespace Diligent +{ + +/// Engine factory for D3D11 implementation +class EngineFactoryD3D11Impl : public EngineFactoryD3DBase +{ +public: + static EngineFactoryD3D11Impl* GetInstance() + { + static EngineFactoryD3D11Impl TheFactory; + return &TheFactory; + } + + void CreateDeviceAndContextsD3D11(const EngineD3D11CreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppContexts, + Uint32 NumDeferredContexts)override final; + + void CreateSwapChainD3D11(IRenderDevice* pDevice, + IDeviceContext* pImmediateContext, + const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, + void* pNativeWndHandle, + ISwapChain** ppSwapChain)override final; + + void AttachToD3D11Device(void* pd3d11NativeDevice, + void* pd3d11ImmediateContext, + const EngineD3D11CreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppContexts, + Uint32 NumDeferredContexts)override final; +}; + + +#if defined(_DEBUG) +// Check for SDK Layer support. +inline bool SdkLayersAvailable() +{ + HRESULT hr = D3D11CreateDevice( + nullptr, + D3D_DRIVER_TYPE_NULL, // There is no need to create a real hardware device. + 0, + D3D11_CREATE_DEVICE_DEBUG, // Check for the SDK layers. + nullptr, // Any feature level will do. + 0, + D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps. + nullptr, // No need to keep the D3D device reference. + nullptr, // No need to know the feature level. + nullptr // No need to keep the D3D device context reference. + ); + + return SUCCEEDED(hr); +} +#endif + +/// Creates render device and device contexts for Direct3D11-based engine implementation + +/// \param [in] EngineCI - Engine creation attributes. +/// \param [out] ppDevice - Address of the memory location where pointer to +/// the created device will be written +/// \param [out] ppContexts - Address of the memory location where pointers to +/// the contexts will be written. Pointer to the immediate +/// context goes at position 0. If NumDeferredContexts > 0, +/// pointers to the deferred contexts go afterwards. +/// \param [in] NumDeferredContexts - Number of deferred contexts. If non-zero number +/// of deferred contexts is requested, pointers to the +/// contexts are written to ppContexts array starting +/// at position 1 +void EngineFactoryD3D11Impl::CreateDeviceAndContextsD3D11(const EngineD3D11CreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppContexts, + Uint32 NumDeferredContexts ) +{ + if (EngineCI.DebugMessageCallback != nullptr) + SetDebugMessageCallback(EngineCI.DebugMessageCallback); + + VERIFY( ppDevice && ppContexts, "Null pointer provided" ); + if( !ppDevice || !ppContexts ) + return; + + *ppDevice = nullptr; + memset(ppContexts, 0, sizeof(*ppContexts) * (1+NumDeferredContexts)); + + // This flag adds support for surfaces with a different color channel ordering + // than the API default. It is required for compatibility with Direct2D. + // D3D11_CREATE_DEVICE_BGRA_SUPPORT; + UINT creationFlags = 0; + +#if defined(_DEBUG) + if (SdkLayersAvailable()) + { + // If the project is in a debug build, enable debugging via SDK Layers with this flag. + creationFlags |= D3D11_CREATE_DEVICE_DEBUG; + } +#endif + + // This array defines the set of DirectX hardware feature levels this app will support. + // Note the ordering should be preserved. + // Don't forget to declare your application's minimum required feature level in its + // description. All applications are assumed to support 9.1 unless otherwise stated. + D3D_FEATURE_LEVEL featureLevels[] = + { +#if PLATFORM_UNIVERSAL_WINDOWS + D3D_FEATURE_LEVEL_11_1, +#endif + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, + D3D_FEATURE_LEVEL_9_3, + D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1 + }; + + // Create the Direct3D 11 API device object and a corresponding context. + CComPtr pd3d11Device; + CComPtr pd3d11Context; + + CComPtr hardwareAdapter; + if (EngineCI.AdapterId != EngineD3D11CreateInfo::DefaultAdapterId) + { + auto Adapters = FindCompatibleAdapters(); + if (EngineCI.AdapterId < Adapters.size()) + hardwareAdapter = Adapters[EngineCI.AdapterId]; + else + { + LOG_ERROR_AND_THROW(EngineCI.AdapterId, " is not a valid hardware adapter id. Total number of compatible adapters available on this system: ", Adapters.size()); + } + } + + D3D_FEATURE_LEVEL d3dFeatureLevel = D3D_FEATURE_LEVEL_11_0; + HRESULT hr = D3D11CreateDevice( + hardwareAdapter, // Specify nullptr to use the default adapter. + hardwareAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, // If no adapter specified, request hardware graphics driver. + 0, // Should be 0 unless the driver is D3D_DRIVER_TYPE_SOFTWARE. + creationFlags, // Set debug and Direct2D compatibility flags. + featureLevels, // List of feature levels this app can support. + ARRAYSIZE(featureLevels), // Size of the list above. + D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps. + &pd3d11Device, // Returns the Direct3D device created. + &d3dFeatureLevel, // Returns feature level of device created. + &pd3d11Context // Returns the device immediate context. + ); + + if (FAILED(hr)) + { + // If the initialization fails, fall back to the WARP device. + // For more information on WARP, see: + // http://go.microsoft.com/fwlink/?LinkId=286690 + hr = D3D11CreateDevice( + nullptr, + D3D_DRIVER_TYPE_WARP, // Create a WARP device instead of a hardware device. + 0, + creationFlags, + featureLevels, + ARRAYSIZE(featureLevels), + D3D11_SDK_VERSION, + &pd3d11Device, + &d3dFeatureLevel, + &pd3d11Context + ); + LOG_ERROR("Failed to create D3D11 native device and immediate context"); + return; + } + + AttachToD3D11Device(pd3d11Device, pd3d11Context, EngineCI, ppDevice, ppContexts, NumDeferredContexts); +} + + +/// Attaches to existing D3D11 render device and immediate context + +/// \param [in] pd3d11NativeDevice - pointer to native D3D11 device +/// \param [in] pd3d11ImmediateContext - pointer to native D3D11 immediate context +/// \param [in] EngineCI - Engine creation attributes. +/// \param [out] ppDevice - Address of the memory location where pointer to +/// the created device will be written +/// \param [out] ppContexts - Address of the memory location where pointers to +/// the contexts will be written. Pointer to the immediate +/// context goes at position 0. If NumDeferredContexts > 0, +/// pointers to deferred contexts go afterwards. +/// \param [in] NumDeferredContexts - Number of deferred contexts. If non-zero number +/// of deferred contexts is requested, pointers to the +/// contexts are written to ppContexts array starting +/// at position 1 +void EngineFactoryD3D11Impl::AttachToD3D11Device(void* pd3d11NativeDevice, + void* pd3d11ImmediateContext, + const EngineD3D11CreateInfo& EngineCI, + IRenderDevice** ppDevice, + IDeviceContext** ppContexts, + Uint32 NumDeferredContexts) +{ + if (EngineCI.DebugMessageCallback != nullptr) + SetDebugMessageCallback(EngineCI.DebugMessageCallback); + + VERIFY( ppDevice && ppContexts, "Null pointer provided" ); + if( !ppDevice || !ppContexts ) + return; + + try + { + ID3D11Device *pd3d11Device = reinterpret_cast(pd3d11NativeDevice); + ID3D11DeviceContext *pd3d11ImmediateCtx = reinterpret_cast(pd3d11ImmediateContext); + + SetRawAllocator(EngineCI.pRawMemAllocator); + auto &RawAlloctor = GetRawAllocator(); + RenderDeviceD3D11Impl *pRenderDeviceD3D11(NEW_RC_OBJ(RawAlloctor, "RenderDeviceD3D11Impl instance", RenderDeviceD3D11Impl) + (RawAlloctor, EngineCI, pd3d11Device, NumDeferredContexts)); + pRenderDeviceD3D11->QueryInterface(IID_RenderDevice, reinterpret_cast(ppDevice)); + + RefCntAutoPtr pDeviceContextD3D11(NEW_RC_OBJ(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl) + (RawAlloctor, pRenderDeviceD3D11, pd3d11ImmediateCtx, EngineCI, false)); + // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceD3D11 will + // keep a weak reference to the context + pDeviceContextD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast(ppContexts)); + pRenderDeviceD3D11->SetImmediateContext(pDeviceContextD3D11); + + for (Uint32 DeferredCtx = 0; DeferredCtx < NumDeferredContexts; ++DeferredCtx) + { + CComPtr pd3d11DeferredCtx; + HRESULT hr = pd3d11Device->CreateDeferredContext(0, &pd3d11DeferredCtx); + CHECK_D3D_RESULT_THROW(hr, "Failed to create D3D11 deferred context"); + RefCntAutoPtr pDeferredCtxD3D11( + NEW_RC_OBJ(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl) + (RawAlloctor, pRenderDeviceD3D11, pd3d11DeferredCtx, EngineCI, true)); + // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceD3D12 will + // keep a weak reference to the context + pDeferredCtxD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast(ppContexts + 1 + DeferredCtx)); + pRenderDeviceD3D11->SetDeferredContext(DeferredCtx, pDeferredCtxD3D11); + } + } + catch( const std::runtime_error & ) + { + if( *ppDevice ) + { + (*ppDevice)->Release(); + *ppDevice = nullptr; + } + for(Uint32 ctx=0; ctx < 1 + NumDeferredContexts; ++ctx) + { + if( ppContexts[ctx] != nullptr ) + { + ppContexts[ctx]->Release(); + ppContexts[ctx] = nullptr; + } + } + + LOG_ERROR( "Failed to initialize D3D11 device and contexts" ); + } +} + + +/// Creates a swap chain for Direct3D11-based engine implementation + +/// \param [in] pDevice - Pointer to the render device +/// \param [in] pImmediateContext - Pointer to the immediate device context +/// \param [in] SCDesc - Swap chain description +/// \param [in] FSDesc - Fullscreen mode description +/// \param [in] pNativeWndHandle - Platform-specific native handle of the window +/// the swap chain will be associated with: +/// * On Win32 platform, this should be window handle (HWND) +/// * On Universal Windows Platform, this should be reference to the +/// core window (Windows::UI::Core::CoreWindow) +/// +/// \param [out] ppSwapChain - Address of the memory location where pointer to the new +/// swap chain will be written +void EngineFactoryD3D11Impl::CreateSwapChainD3D11(IRenderDevice* pDevice, + IDeviceContext* pImmediateContext, + const SwapChainDesc& SCDesc, + const FullScreenModeDesc& FSDesc, + void* pNativeWndHandle, + ISwapChain** ppSwapChain) +{ + VERIFY( ppSwapChain, "Null pointer provided" ); + if( !ppSwapChain ) + return; + + *ppSwapChain = nullptr; + + try + { + auto *pDeviceD3D11 = ValidatedCast( pDevice ); + auto *pDeviceContextD3D11 = ValidatedCast(pImmediateContext); + auto &RawMemAllocator = GetRawAllocator(); + + auto *pSwapChainD3D11 = NEW_RC_OBJ(RawMemAllocator, "SwapChainD3D11Impl instance", SwapChainD3D11Impl) + (SCDesc, FSDesc, pDeviceD3D11, pDeviceContextD3D11, pNativeWndHandle); + pSwapChainD3D11->QueryInterface( IID_SwapChain, reinterpret_cast(ppSwapChain) ); + + pDeviceContextD3D11->SetSwapChain(pSwapChainD3D11); + // Bind default render target + pDeviceContextD3D11->SetRenderTargets( 0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION ); + // Set default viewport + pDeviceContextD3D11->SetViewports( 1, nullptr, 0, 0 ); + + auto NumDeferredCtx = pDeviceD3D11->GetNumDeferredContexts(); + for (size_t ctx = 0; ctx < NumDeferredCtx; ++ctx) + { + if (auto pDeferredCtx = pDeviceD3D11->GetDeferredContext(ctx)) + { + auto *pDeferredCtxD3D11 = pDeferredCtx.RawPtr(); + pDeferredCtxD3D11->SetSwapChain(pSwapChainD3D11); + // Do not bind default render target and viewport to be + // consistent with D3D12 + //// Bind default render target + //pDeferredCtxD3D11->SetRenderTargets( 0, nullptr, nullptr ); + //// Set default viewport + //pDeferredCtxD3D11->SetViewports( 1, nullptr, 0, 0 ); + } + } + } + catch( const std::runtime_error & ) + { + if( *ppSwapChain ) + { + (*ppSwapChain)->Release(); + *ppSwapChain = nullptr; + } + + LOG_ERROR( "Failed to create the swap chain" ); + } +} + +#ifdef DOXYGEN +/// Loads Direct3D11-based engine implementation and exports factory functions +/// \param [out] GetFactoryFunc - Pointer to the function that returns factory for D3D11 engine implementation +/// See EngineFactoryD3D11Impl. +/// \remarks Depending on the configuration and platform, the function loads different dll: +/// Platform\\Configuration | Debug | Release +/// --------------------------|-------------------------------|---------------------------- +/// x86 | GraphicsEngineD3D11_32d.dll | GraphicsEngineD3D11_32r.dll +/// x64 | GraphicsEngineD3D11_64d.dll | GraphicsEngineD3D11_64r.dll +/// +void LoadGraphicsEngineD3D11(GetEngineFactoryD3D11Type &GetFactoryFunc) +{ + // This function is only required because DoxyGen refuses to generate documentation for a static function when SHOW_FILES==NO + #error This function must never be compiled; +} +#endif + + +IEngineFactoryD3D11* GetEngineFactoryD3D11() +{ + return EngineFactoryD3D11Impl::GetInstance(); +} + +} diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp index 86eae24a..04f54ad5 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp @@ -40,11 +40,11 @@ namespace Diligent { -RenderDeviceD3D11Impl :: RenderDeviceD3D11Impl(IReferenceCounters* pRefCounters, - IMemoryAllocator& RawMemAllocator, - const EngineD3D11Attribs& EngineAttribs, - ID3D11Device* pd3d11Device, - Uint32 NumDeferredContexts) : +RenderDeviceD3D11Impl :: RenderDeviceD3D11Impl(IReferenceCounters* pRefCounters, + IMemoryAllocator& RawMemAllocator, + const EngineD3D11CreateInfo& EngineAttribs, + ID3D11Device* pd3d11Device, + Uint32 NumDeferredContexts) : TRenderDeviceBase { pRefCounters, diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp deleted file mode 100644 index 7e5ff13f..00000000 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp +++ /dev/null @@ -1,382 +0,0 @@ -/* Copyright 2015-2019 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -/// \file -/// Routines that initialize D3D11-based engine implementation - -#include "pch.h" -#include "RenderDeviceFactoryD3D11.h" -#include "RenderDeviceD3D11Impl.h" -#include "DeviceContextD3D11Impl.h" -#include "SwapChainD3D11Impl.h" -#include "D3D11TypeConversions.h" -#include "EngineMemory.h" -#include "EngineFactoryD3DBase.h" -#include -#include - -namespace Diligent -{ - -/// Engine factory for D3D11 implementation -class EngineFactoryD3D11Impl : public EngineFactoryD3DBase -{ -public: - static EngineFactoryD3D11Impl* GetInstance() - { - static EngineFactoryD3D11Impl TheFactory; - return &TheFactory; - } - - void CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, - IRenderDevice** ppDevice, - IDeviceContext** ppContexts, - Uint32 NumDeferredContexts )override final; - - void CreateSwapChainD3D11( IRenderDevice* pDevice, - IDeviceContext* pImmediateContext, - const SwapChainDesc& SCDesc, - const FullScreenModeDesc& FSDesc, - void* pNativeWndHandle, - ISwapChain** ppSwapChain )override final; - - void AttachToD3D11Device(void* pd3d11NativeDevice, - void* pd3d11ImmediateContext, - const EngineD3D11Attribs& EngineAttribs, - IRenderDevice** ppDevice, - IDeviceContext** ppContexts, - Uint32 NumDeferredContexts)override final; -}; - - -#if defined(_DEBUG) -// Check for SDK Layer support. -inline bool SdkLayersAvailable() -{ - HRESULT hr = D3D11CreateDevice( - nullptr, - D3D_DRIVER_TYPE_NULL, // There is no need to create a real hardware device. - 0, - D3D11_CREATE_DEVICE_DEBUG, // Check for the SDK layers. - nullptr, // Any feature level will do. - 0, - D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps. - nullptr, // No need to keep the D3D device reference. - nullptr, // No need to know the feature level. - nullptr // No need to keep the D3D device context reference. - ); - - return SUCCEEDED(hr); -} -#endif - -/// Creates render device and device contexts for Direct3D11-based engine implementation - -/// \param [in] EngineAttribs - Engine creation attributes. -/// \param [out] ppDevice - Address of the memory location where pointer to -/// the created device will be written -/// \param [out] ppContexts - Address of the memory location where pointers to -/// the contexts will be written. Pointer to the immediate -/// context goes at position 0. If NumDeferredContexts > 0, -/// pointers to the deferred contexts go afterwards. -/// \param [in] NumDeferredContexts - Number of deferred contexts. If non-zero number -/// of deferred contexts is requested, pointers to the -/// contexts are written to ppContexts array starting -/// at position 1 -void EngineFactoryD3D11Impl::CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, - IRenderDevice** ppDevice, - IDeviceContext** ppContexts, - Uint32 NumDeferredContexts ) -{ - if (EngineAttribs.DebugMessageCallback != nullptr) - SetDebugMessageCallback(EngineAttribs.DebugMessageCallback); - - VERIFY( ppDevice && ppContexts, "Null pointer provided" ); - if( !ppDevice || !ppContexts ) - return; - - *ppDevice = nullptr; - memset(ppContexts, 0, sizeof(*ppContexts) * (1+NumDeferredContexts)); - - // This flag adds support for surfaces with a different color channel ordering - // than the API default. It is required for compatibility with Direct2D. - // D3D11_CREATE_DEVICE_BGRA_SUPPORT; - UINT creationFlags = 0; - -#if defined(_DEBUG) - if (SdkLayersAvailable()) - { - // If the project is in a debug build, enable debugging via SDK Layers with this flag. - creationFlags |= D3D11_CREATE_DEVICE_DEBUG; - } -#endif - - // This array defines the set of DirectX hardware feature levels this app will support. - // Note the ordering should be preserved. - // Don't forget to declare your application's minimum required feature level in its - // description. All applications are assumed to support 9.1 unless otherwise stated. - D3D_FEATURE_LEVEL featureLevels[] = - { -#if PLATFORM_UNIVERSAL_WINDOWS - D3D_FEATURE_LEVEL_11_1, -#endif - D3D_FEATURE_LEVEL_11_0, - D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, - D3D_FEATURE_LEVEL_9_3, - D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1 - }; - - // Create the Direct3D 11 API device object and a corresponding context. - CComPtr pd3d11Device; - CComPtr pd3d11Context; - - CComPtr hardwareAdapter; - if(EngineAttribs.AdapterId != EngineD3D11Attribs::DefaultAdapterId) - { - auto Adapters = FindCompatibleAdapters(); - if (EngineAttribs.AdapterId < Adapters.size()) - hardwareAdapter = Adapters[EngineAttribs.AdapterId]; - else - { - LOG_ERROR_AND_THROW(EngineAttribs.AdapterId, " is not a valid hardware adapter id. Total number of compatible adapters available on this system: ", Adapters.size()); - } - } - - D3D_FEATURE_LEVEL d3dFeatureLevel = D3D_FEATURE_LEVEL_11_0; - HRESULT hr = D3D11CreateDevice( - hardwareAdapter, // Specify nullptr to use the default adapter. - hardwareAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, // If no adapter specified, request hardware graphics driver. - 0, // Should be 0 unless the driver is D3D_DRIVER_TYPE_SOFTWARE. - creationFlags, // Set debug and Direct2D compatibility flags. - featureLevels, // List of feature levels this app can support. - ARRAYSIZE(featureLevels), // Size of the list above. - D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps. - &pd3d11Device, // Returns the Direct3D device created. - &d3dFeatureLevel, // Returns feature level of device created. - &pd3d11Context // Returns the device immediate context. - ); - - if (FAILED(hr)) - { - // If the initialization fails, fall back to the WARP device. - // For more information on WARP, see: - // http://go.microsoft.com/fwlink/?LinkId=286690 - hr = D3D11CreateDevice( - nullptr, - D3D_DRIVER_TYPE_WARP, // Create a WARP device instead of a hardware device. - 0, - creationFlags, - featureLevels, - ARRAYSIZE(featureLevels), - D3D11_SDK_VERSION, - &pd3d11Device, - &d3dFeatureLevel, - &pd3d11Context - ); - LOG_ERROR("Failed to create D3D11 native device and immediate context"); - return; - } - - AttachToD3D11Device(pd3d11Device, pd3d11Context, EngineAttribs, ppDevice, ppContexts, NumDeferredContexts); -} - - -/// Attaches to existing D3D11 render device and immediate context - -/// \param [in] pd3d11NativeDevice - pointer to native D3D11 device -/// \param [in] pd3d11ImmediateContext - pointer to native D3D11 immediate context -/// \param [in] EngineAttribs - Engine creation attributes. -/// \param [out] ppDevice - Address of the memory location where pointer to -/// the created device will be written -/// \param [out] ppContexts - Address of the memory location where pointers to -/// the contexts will be written. Pointer to the immediate -/// context goes at position 0. If NumDeferredContexts > 0, -/// pointers to deferred contexts go afterwards. -/// \param [in] NumDeferredContexts - Number of deferred contexts. If non-zero number -/// of deferred contexts is requested, pointers to the -/// contexts are written to ppContexts array starting -/// at position 1 -void EngineFactoryD3D11Impl::AttachToD3D11Device(void* pd3d11NativeDevice, - void* pd3d11ImmediateContext, - const EngineD3D11Attribs& EngineAttribs, - IRenderDevice** ppDevice, - IDeviceContext** ppContexts, - Uint32 NumDeferredContexts) -{ - if (EngineAttribs.DebugMessageCallback != nullptr) - SetDebugMessageCallback(EngineAttribs.DebugMessageCallback); - - VERIFY( ppDevice && ppContexts, "Null pointer provided" ); - if( !ppDevice || !ppContexts ) - return; - - try - { - ID3D11Device *pd3d11Device = reinterpret_cast(pd3d11NativeDevice); - ID3D11DeviceContext *pd3d11ImmediateCtx = reinterpret_cast(pd3d11ImmediateContext); - - SetRawAllocator(EngineAttribs.pRawMemAllocator); - auto &RawAlloctor = GetRawAllocator(); - RenderDeviceD3D11Impl *pRenderDeviceD3D11(NEW_RC_OBJ(RawAlloctor, "RenderDeviceD3D11Impl instance", RenderDeviceD3D11Impl) - (RawAlloctor, EngineAttribs, pd3d11Device, NumDeferredContexts)); - pRenderDeviceD3D11->QueryInterface(IID_RenderDevice, reinterpret_cast(ppDevice)); - - RefCntAutoPtr pDeviceContextD3D11(NEW_RC_OBJ(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl) - (RawAlloctor, pRenderDeviceD3D11, pd3d11ImmediateCtx, EngineAttribs, false)); - // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceD3D11 will - // keep a weak reference to the context - pDeviceContextD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast(ppContexts)); - pRenderDeviceD3D11->SetImmediateContext(pDeviceContextD3D11); - - for (Uint32 DeferredCtx = 0; DeferredCtx < NumDeferredContexts; ++DeferredCtx) - { - CComPtr pd3d11DeferredCtx; - HRESULT hr = pd3d11Device->CreateDeferredContext(0, &pd3d11DeferredCtx); - CHECK_D3D_RESULT_THROW(hr, "Failed to create D3D11 deferred context"); - RefCntAutoPtr pDeferredCtxD3D11( - NEW_RC_OBJ(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl) - (RawAlloctor, pRenderDeviceD3D11, pd3d11DeferredCtx, EngineAttribs, true)); - // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceD3D12 will - // keep a weak reference to the context - pDeferredCtxD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast(ppContexts + 1 + DeferredCtx)); - pRenderDeviceD3D11->SetDeferredContext(DeferredCtx, pDeferredCtxD3D11); - } - } - catch( const std::runtime_error & ) - { - if( *ppDevice ) - { - (*ppDevice)->Release(); - *ppDevice = nullptr; - } - for(Uint32 ctx=0; ctx < 1 + NumDeferredContexts; ++ctx) - { - if( ppContexts[ctx] != nullptr ) - { - ppContexts[ctx]->Release(); - ppContexts[ctx] = nullptr; - } - } - - LOG_ERROR( "Failed to initialize D3D11 device and contexts" ); - } -} - - -/// Creates a swap chain for Direct3D11-based engine implementation - -/// \param [in] pDevice - Pointer to the render device -/// \param [in] pImmediateContext - Pointer to the immediate device context -/// \param [in] SCDesc - Swap chain description -/// \param [in] FSDesc - Fullscreen mode description -/// \param [in] pNativeWndHandle - Platform-specific native handle of the window -/// the swap chain will be associated with: -/// * On Win32 platform, this should be window handle (HWND) -/// * On Universal Windows Platform, this should be reference to the -/// core window (Windows::UI::Core::CoreWindow) -/// -/// \param [out] ppSwapChain - Address of the memory location where pointer to the new -/// swap chain will be written -void EngineFactoryD3D11Impl::CreateSwapChainD3D11( IRenderDevice* pDevice, - IDeviceContext* pImmediateContext, - const SwapChainDesc& SCDesc, - const FullScreenModeDesc& FSDesc, - void* pNativeWndHandle, - ISwapChain** ppSwapChain ) -{ - VERIFY( ppSwapChain, "Null pointer provided" ); - if( !ppSwapChain ) - return; - - *ppSwapChain = nullptr; - - try - { - auto *pDeviceD3D11 = ValidatedCast( pDevice ); - auto *pDeviceContextD3D11 = ValidatedCast(pImmediateContext); - auto &RawMemAllocator = GetRawAllocator(); - - auto *pSwapChainD3D11 = NEW_RC_OBJ(RawMemAllocator, "SwapChainD3D11Impl instance", SwapChainD3D11Impl) - (SCDesc, FSDesc, pDeviceD3D11, pDeviceContextD3D11, pNativeWndHandle); - pSwapChainD3D11->QueryInterface( IID_SwapChain, reinterpret_cast(ppSwapChain) ); - - pDeviceContextD3D11->SetSwapChain(pSwapChainD3D11); - // Bind default render target - pDeviceContextD3D11->SetRenderTargets( 0, nullptr, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION ); - // Set default viewport - pDeviceContextD3D11->SetViewports( 1, nullptr, 0, 0 ); - - auto NumDeferredCtx = pDeviceD3D11->GetNumDeferredContexts(); - for (size_t ctx = 0; ctx < NumDeferredCtx; ++ctx) - { - if (auto pDeferredCtx = pDeviceD3D11->GetDeferredContext(ctx)) - { - auto *pDeferredCtxD3D11 = pDeferredCtx.RawPtr(); - pDeferredCtxD3D11->SetSwapChain(pSwapChainD3D11); - // Do not bind default render target and viewport to be - // consistent with D3D12 - //// Bind default render target - //pDeferredCtxD3D11->SetRenderTargets( 0, nullptr, nullptr ); - //// Set default viewport - //pDeferredCtxD3D11->SetViewports( 1, nullptr, 0, 0 ); - } - } - } - catch( const std::runtime_error & ) - { - if( *ppSwapChain ) - { - (*ppSwapChain)->Release(); - *ppSwapChain = nullptr; - } - - LOG_ERROR( "Failed to create the swap chain" ); - } -} - -#ifdef DOXYGEN -/// Loads Direct3D11-based engine implementation and exports factory functions -/// \param [out] GetFactoryFunc - Pointer to the function that returns factory for D3D11 engine implementation -/// See EngineFactoryD3D11Impl. -/// \remarks Depending on the configuration and platform, the function loads different dll: -/// Platform\\Configuration | Debug | Release -/// --------------------------|-------------------------------|---------------------------- -/// x86 | GraphicsEngineD3D11_32d.dll | GraphicsEngineD3D11_32r.dll -/// x64 | GraphicsEngineD3D11_64d.dll | GraphicsEngineD3D11_64r.dll -/// -void LoadGraphicsEngineD3D11(GetEngineFactoryD3D11Type &GetFactoryFunc) -{ - // This function is only required because DoxyGen refuses to generate documentation for a static function when SHOW_FILES==NO - #error This function must never be compiled; -} -#endif - - -IEngineFactoryD3D11* GetEngineFactoryD3D11() -{ - return EngineFactoryD3D11Impl::GetInstance(); -} - -} -- cgit v1.2.3 From bdbca67b0e03baec3b9dd803e803d267210e892b Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Mon, 4 Mar 2019 23:19:07 -0800 Subject: Added ShaderFlags parameter to IPipelineState::BindStaticResources --- Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h | 2 +- Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index 81e03ca2..c56d8479 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -51,7 +51,7 @@ public: virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; - virtual void BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags)override final; + virtual void BindStaticResources(Uint32 ShaderFlags, IResourceMapping* pResourceMapping, Uint32 Flags)override final; virtual Uint32 GetStaticVariableCount(SHADER_TYPE ShaderType) const override final; diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 84bc1ef5..d9cc1c21 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -322,11 +322,13 @@ ID3D11ComputeShader* PipelineStateD3D11Impl::GetD3D11ComputeShader() } -void PipelineStateD3D11Impl::BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags) +void PipelineStateD3D11Impl::BindStaticResources(Uint32 ShaderFlags, IResourceMapping* pResourceMapping, Uint32 Flags) { for (Uint32 s=0; s < m_NumShaders; ++s) { - m_pStaticResourceLayouts[s].BindResources(pResourceMapping, Flags, m_pStaticResourceCaches[s]); + auto& StaticResLayout = m_pStaticResourceLayouts[s]; + if ( (ShaderFlags & StaticResLayout.GetShaderType()) != 0 ) + StaticResLayout.BindResources(pResourceMapping, Flags, m_pStaticResourceCaches[s]); } } -- cgit v1.2.3