diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-03-07 02:58:56 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-03-07 02:58:56 +0000 |
| commit | bc9fc76496034a50faf3d7fe7be868d5d62f6663 (patch) | |
| tree | e9438fe09533409c57c9c63b2f69a895e2cf8615 /Graphics/GraphicsEngine | |
| parent | Merge pull request #67 from StarshipVendingMachine/android-vulkan (diff) | |
| parent | Minor comment updates (diff) | |
| download | DiligentCore-bc9fc76496034a50faf3d7fe7be868d5d62f6663.tar.gz DiligentCore-bc9fc76496034a50faf3d7fe7be868d5d62f6663.zip | |
Merged var_type_refactor into master
Diffstat (limited to 'Graphics/GraphicsEngine')
20 files changed, 637 insertions, 381 deletions
diff --git a/Graphics/GraphicsEngine/CMakeLists.txt b/Graphics/GraphicsEngine/CMakeLists.txt index 1fc418f3..25618d84 100644 --- a/Graphics/GraphicsEngine/CMakeLists.txt +++ b/Graphics/GraphicsEngine/CMakeLists.txt @@ -18,6 +18,7 @@ set(INCLUDE include/SamplerBase.h include/ShaderBase.h include/ShaderResourceBindingBase.h + include/ShaderResourceVariableBase.h include/StateObjectsRegistry.h include/SwapChainBase.h include/TextureBase.h @@ -45,6 +46,7 @@ set(INTERFACE interface/Sampler.h interface/Shader.h interface/ShaderResourceBinding.h + interface/ShaderResourceVariable.h interface/SwapChain.h interface/Texture.h interface/TextureView.h diff --git a/Graphics/GraphicsEngine/include/PipelineStateBase.h b/Graphics/GraphicsEngine/include/PipelineStateBase.h index 2683d63c..ddc682ef 100644 --- a/Graphics/GraphicsEngine/include/PipelineStateBase.h +++ b/Graphics/GraphicsEngine/include/PipelineStateBase.h @@ -34,6 +34,7 @@ #include "STDAllocator.h" #include "EngineMemory.h" #include "GraphicsAccessories.h" +#include "StringPool.h" namespace Diligent { @@ -65,6 +66,62 @@ public: m_LayoutElements (PSODesc.GraphicsPipeline.InputLayout.NumElements, LayoutElement{}, STD_ALLOCATOR_RAW_MEM(LayoutElement, GetRawAllocator(), "Allocator for vector<LayoutElement>")), m_NumShaders(0) { + const auto& SrcLayout = PSODesc.ResourceLayout; + size_t StringPoolSize = 0; + if (SrcLayout.Variables != nullptr) + { + for (Uint32 i=0; i < SrcLayout.NumVariables; ++i) + StringPoolSize += strlen(SrcLayout.Variables[i].Name) + 1; + } + + if (SrcLayout.StaticSamplers != nullptr) + { + for (Uint32 i=0; i < SrcLayout.NumStaticSamplers; ++i) + StringPoolSize += strlen(SrcLayout.StaticSamplers[i].SamplerOrTextureName) + 1; + } + m_StringPool.Reserve(StringPoolSize, GetRawAllocator()); + + auto& DstLayout = this->m_Desc.ResourceLayout; + if (SrcLayout.Variables != nullptr) + { + ShaderResourceVariableDesc* Variables = + reinterpret_cast<ShaderResourceVariableDesc*>(ALLOCATE(GetRawAllocator(), "Memory for ShaderResourceVariableDesc array", sizeof(ShaderResourceVariableDesc) * SrcLayout.NumVariables)); + DstLayout.Variables = Variables; + for (Uint32 i=0; i < SrcLayout.NumVariables; ++i) + { + VERIFY(SrcLayout.Variables[i].Name != nullptr, "Variable name can't be null"); + Variables[i] = SrcLayout.Variables[i]; + Variables[i].Name = m_StringPool.CopyString(SrcLayout.Variables[i].Name); + } + } + + if (SrcLayout.StaticSamplers != nullptr) + { + StaticSamplerDesc* StaticSamplers = + reinterpret_cast<StaticSamplerDesc*>(ALLOCATE(GetRawAllocator(), "Memory for StaticSamplerDesc array", sizeof(StaticSamplerDesc) * SrcLayout.NumStaticSamplers)); + DstLayout.StaticSamplers = StaticSamplers; + for (Uint32 i=0; i < SrcLayout.NumStaticSamplers; ++i) + { + VERIFY(SrcLayout.StaticSamplers[i].SamplerOrTextureName != nullptr, "Static sampler or texture name can't be null"); +#ifdef DEVELOPMENT + const auto &BorderColor = SrcLayout.StaticSamplers[i].Desc.BorderColor; + if( !( (BorderColor[0] == 0 && BorderColor[1] == 0 && BorderColor[2] == 0 && BorderColor[3] == 0) || + (BorderColor[0] == 0 && BorderColor[1] == 0 && BorderColor[2] == 0 && BorderColor[3] == 1) || + (BorderColor[0] == 1 && BorderColor[1] == 1 && BorderColor[2] == 1 && BorderColor[3] == 1) ) ) + { + LOG_WARNING_MESSAGE("Static sampler for variable \"", SrcLayout.StaticSamplers[i].SamplerOrTextureName, "\" specifies border color (", + BorderColor[0], ", ", BorderColor[1], ", ", BorderColor[2], ", ", BorderColor[3], "). " + "D3D12 static samplers only allow transparent black (0,0,0,0), opaque black (0,0,0,1) or opaque white (1,1,1,1) as border colors"); + } +#endif + + StaticSamplers[i] = SrcLayout.StaticSamplers[i]; + StaticSamplers[i].SamplerOrTextureName = m_StringPool.CopyString(SrcLayout.StaticSamplers[i].SamplerOrTextureName); + } + } + VERIFY_EXPR(m_StringPool.GetRemainingSize() == 0); + + if (this->m_Desc.IsComputePipeline) { const auto &ComputePipeline = PSODesc.ComputePipeline; @@ -107,6 +164,7 @@ public: if (GraphicsPipeline.pHS) m_ppShaders[m_NumShaders++] = GraphicsPipeline.pHS; if (GraphicsPipeline.pDS) m_ppShaders[m_NumShaders++] = GraphicsPipeline.pDS; } + VERIFY(m_NumShaders > 0, "There must be at least one shader in the Pipeline State"); const auto& InputLayout = PSODesc.GraphicsPipeline.InputLayout; for (size_t Elem = 0; Elem < InputLayout.NumElements; ++Elem) @@ -206,6 +264,12 @@ public: RasterizerStateRegistry.ReportDeletedObject(); DSSRegistry.ReportDeletedObject(); */ + + auto& RawAllocator = GetRawAllocator(); + if (this->m_Desc.ResourceLayout.Variables != nullptr) + RawAllocator.Free(const_cast<ShaderResourceVariableDesc*>(this->m_Desc.ResourceLayout.Variables)); + if (this->m_Desc.ResourceLayout.StaticSamplers != nullptr) + RawAllocator.Free(const_cast<StaticSamplerDesc*>(this->m_Desc.ResourceLayout.StaticSamplers)); } IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_PipelineState, TDeviceObjectBase ) @@ -250,21 +314,19 @@ public: return m_ShaderResourceLayoutHash != ValidatedCast<const PipelineStateBase>(pPSO)->m_ShaderResourceLayoutHash; } - virtual void BindShaderResources( IResourceMapping* pResourceMapping, Uint32 Flags )override - { - for(Uint32 s=0; s < m_NumShaders; ++s) - m_ppShaders[s]->BindResources(pResourceMapping, Flags); - } - protected: + // TODO: rework this std::vector<LayoutElement, STDAllocatorRawMem<LayoutElement> > m_LayoutElements; Uint32 m_BufferSlotsUsed = 0; + Uint32 m_NumShaders = 0; ///< Number of shaders that this PSO uses + // The size of this array must be equal to the // maximum number of buffer slots, because a layout // element can refer to any input slot - std::array<Uint32, MaxBufferSlots> m_Strides = {}; + std::array<Uint32, MaxBufferSlots> m_Strides = {}; // TODO: rework this + StringPool m_StringPool; RefCntAutoPtr<IShader> m_pVS; ///< Strong reference to the vertex shader RefCntAutoPtr<IShader> m_pPS; ///< Strong reference to the pixel shader RefCntAutoPtr<IShader> m_pGS; ///< Strong reference to the geometry shader @@ -272,7 +334,6 @@ protected: RefCntAutoPtr<IShader> m_pHS; ///< Strong reference to the hull shader RefCntAutoPtr<IShader> m_pCS; ///< Strong reference to the compute shader IShader* m_ppShaders[5] = {}; ///< Array of pointers to the shaders used by this PSO - Uint32 m_NumShaders = 0; ///< Number of shaders that this PSO uses size_t m_ShaderResourceLayoutHash = 0;///< Hash computed from the shader resource layout }; diff --git a/Graphics/GraphicsEngine/include/ResourceMappingImpl.h b/Graphics/GraphicsEngine/include/ResourceMappingImpl.h index ae0b4734..8630d764 100644 --- a/Graphics/GraphicsEngine/include/ResourceMappingImpl.h +++ b/Graphics/GraphicsEngine/include/ResourceMappingImpl.h @@ -104,7 +104,7 @@ namespace Diligent ~ResourceMappingImpl(); - 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 IResourceMapping::AddResource() virtual void AddResource( const Char* Name, IDeviceObject* pObject, bool bIsUnique )override final; diff --git a/Graphics/GraphicsEngine/include/ShaderBase.h b/Graphics/GraphicsEngine/include/ShaderBase.h index ff015829..a9c1ae13 100644 --- a/Graphics/GraphicsEngine/include/ShaderBase.h +++ b/Graphics/GraphicsEngine/include/ShaderBase.h @@ -37,12 +37,12 @@ namespace Diligent { -inline SHADER_TYPE GetShaderTypeFromIndex( Int32 Index ) +inline SHADER_TYPE GetShaderTypeFromIndex(Int32 Index) { return static_cast<SHADER_TYPE>(1 << Index); } -inline Int32 GetShaderTypeIndex( SHADER_TYPE Type ) +inline Int32 GetShaderTypeIndex(SHADER_TYPE Type) { Int32 ShaderIndex = PlatformMisc::GetLSB(Type); @@ -70,100 +70,6 @@ static const int HSInd = GetShaderTypeIndex(SHADER_TYPE_HULL); static const int DSInd = GetShaderTypeIndex(SHADER_TYPE_DOMAIN); static const int CSInd = GetShaderTypeIndex(SHADER_TYPE_COMPUTE); -template<typename TNameCompare> -SHADER_VARIABLE_TYPE GetShaderVariableType(SHADER_VARIABLE_TYPE DefaultVariableType, const ShaderVariableDesc* VariableDesc, Uint32 NumVars, TNameCompare NameCompare) -{ - for (Uint32 v = 0; v < NumVars; ++v) - { - const auto &CurrVarDesc = VariableDesc[v]; - if ( NameCompare(CurrVarDesc.Name) ) - { - return CurrVarDesc.Type; - } - } - return DefaultVariableType; -} - -inline SHADER_VARIABLE_TYPE GetShaderVariableType(const Char* Name, SHADER_VARIABLE_TYPE DefaultVariableType, const ShaderVariableDesc* VariableDesc, Uint32 NumVars) -{ - return GetShaderVariableType(DefaultVariableType, VariableDesc, NumVars, - [&](const char *VarName) - { - return strcmp(VarName, Name) == 0; - } - ); -} - -inline SHADER_VARIABLE_TYPE GetShaderVariableType(const Char* Name, const ShaderDesc& ShdrDesc) -{ - return GetShaderVariableType(Name, ShdrDesc.DefaultVariableType, ShdrDesc.VariableDesc, ShdrDesc.NumVariables); -} - -inline SHADER_VARIABLE_TYPE GetShaderVariableType(const String& Name, SHADER_VARIABLE_TYPE DefaultVariableType, const ShaderVariableDesc *VariableDesc, Uint32 NumVars) -{ - return GetShaderVariableType(DefaultVariableType, VariableDesc, NumVars, - [&](const char *VarName) - { - return Name.compare(VarName) == 0; - } - ); -} - -inline SHADER_VARIABLE_TYPE GetShaderVariableType(const String& Name, const ShaderDesc& ShdrDesc) -{ - return GetShaderVariableType(Name, ShdrDesc.DefaultVariableType, ShdrDesc.VariableDesc, ShdrDesc.NumVariables); -} - - -/// Base implementation of a shader variable - -struct ShaderVariableBase : public IShaderVariable -{ - ShaderVariableBase(IObject& Owner) : - // Shader variables are always created as part of the shader, or - // shader resource binding, so we must provide owner pointer to - // the base class constructor - m_Owner(Owner) - { - } - - IObject& GetOwner() - { - return m_Owner; - } - - virtual IReferenceCounters* GetReferenceCounters()const override final - { - return m_Owner.GetReferenceCounters(); - } - - virtual Atomics::Long AddRef()override final - { - return m_Owner.AddRef(); - } - - virtual Atomics::Long Release()override final - { - return m_Owner.Release(); - } - - virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface )override final - { - if( ppInterface == nullptr ) - return; - - *ppInterface = nullptr; - if( IID == IID_ShaderVariable || IID == IID_Unknown ) - { - *ppInterface = this; - (*ppInterface)->AddRef(); - } - } - -protected: - IObject& m_Owner; -}; - /// Template class implementing base functionality for a shader object @@ -184,57 +90,12 @@ public: /// \param ShdrDesc - shader description. /// \param bIsDeviceInternal - flag indicating if the shader is an internal device object and /// must not keep a strong reference to the device. - ShaderBase( IReferenceCounters* pRefCounters, RenderDeviceImplType* pDevice, const ShaderDesc& ShdrDesc, bool bIsDeviceInternal = false ) : - TDeviceObjectBase( pRefCounters, pDevice, ShdrDesc, bIsDeviceInternal ), - m_VariablesDesc (ShdrDesc.NumVariables, ShaderVariableDesc(), STD_ALLOCATOR_RAW_MEM(ShaderVariableDesc, GetRawAllocator(), "Allocator for vector<ShaderVariableDesc>") ), - m_StringPool (ShdrDesc.NumVariables + ShdrDesc.NumStaticSamplers, String(), STD_ALLOCATOR_RAW_MEM(String, GetRawAllocator(), "Allocator for vector<String>")), - m_StaticSamplers(ShdrDesc.NumStaticSamplers, StaticSamplerDesc(), STD_ALLOCATOR_RAW_MEM(StaticSamplerDesc, GetRawAllocator(), "Allocator for vector<StaticSamplerDesc>") ) + ShaderBase(IReferenceCounters* pRefCounters, RenderDeviceImplType* pDevice, const ShaderDesc& ShdrDesc, bool bIsDeviceInternal = false) : + TDeviceObjectBase(pRefCounters, pDevice, ShdrDesc, bIsDeviceInternal) { - auto Str = m_StringPool.begin(); - if(this->m_Desc.VariableDesc) - { - for (Uint32 v = 0; v < this->m_Desc.NumVariables; ++v, ++Str) - { - m_VariablesDesc[v] = this->m_Desc.VariableDesc[v]; - VERIFY(m_VariablesDesc[v].Name != nullptr, "Variable name not provided"); - *Str = m_VariablesDesc[v].Name; - m_VariablesDesc[v].Name = Str->c_str(); - } - this->m_Desc.VariableDesc = m_VariablesDesc.data(); - } - if(this->m_Desc.StaticSamplers) - { - for (Uint32 s = 0; s < this->m_Desc.NumStaticSamplers; ++s, ++Str) - { - m_StaticSamplers[s] = this->m_Desc.StaticSamplers[s]; - VERIFY(m_StaticSamplers[s].SamplerOrTextureName != nullptr, "Static sampler or texture name is not provided"); - *Str = m_StaticSamplers[s].SamplerOrTextureName; - m_StaticSamplers[s].SamplerOrTextureName = Str->c_str(); -#ifdef DEVELOPMENT - const auto &BorderColor = m_StaticSamplers[s].Desc.BorderColor; - if( !( (BorderColor[0] == 0 && BorderColor[1] == 0 && BorderColor[2] == 0 && BorderColor[3] == 0) || - (BorderColor[0] == 0 && BorderColor[1] == 0 && BorderColor[2] == 0 && BorderColor[3] == 1) || - (BorderColor[0] == 1 && BorderColor[1] == 1 && BorderColor[2] == 1 && BorderColor[3] == 1) ) ) - { - LOG_WARNING_MESSAGE("Static sampler for variable \"", *Str , "\" specifies border color (", BorderColor[0], ", ", BorderColor[1], ", ", BorderColor[2], ", ", BorderColor[3], "). D3D12 static samplers only allow transparent black (0,0,0,0), opaque black (0,0,0,1) or opaque white (1,1,1,1) as border colors"); - } -#endif - } - this->m_Desc.StaticSamplers = m_StaticSamplers.data(); - } - - VERIFY_EXPR(Str == m_StringPool.end()); } IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_Shader, TDeviceObjectBase ) - -protected: - /// Shader variable descriptions - std::vector<ShaderVariableDesc, STDAllocatorRawMem<ShaderVariableDesc> > m_VariablesDesc; - /// String pool that is used to hold copies of variable names and static sampler names - std::vector<String, STDAllocatorRawMem<String> > m_StringPool; - /// Static sampler descriptions - std::vector<StaticSamplerDesc, STDAllocatorRawMem<StaticSamplerDesc> > m_StaticSamplers; }; } diff --git a/Graphics/GraphicsEngine/include/ShaderResourceVariableBase.h b/Graphics/GraphicsEngine/include/ShaderResourceVariableBase.h new file mode 100644 index 00000000..2282a3f7 --- /dev/null +++ b/Graphics/GraphicsEngine/include/ShaderResourceVariableBase.h @@ -0,0 +1,161 @@ +/* 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 +/// Implementation of the Diligent::ShaderBase template class + +#include <vector> + +#include "ShaderResourceVariable.h" +#include "PipelineState.h" + +namespace Diligent +{ + +template<typename TNameCompare> +SHADER_RESOURCE_VARIABLE_TYPE GetShaderVariableType(SHADER_TYPE ShaderStage, + SHADER_RESOURCE_VARIABLE_TYPE DefaultVariableType, + const ShaderResourceVariableDesc* Variables, + Uint32 NumVars, + TNameCompare NameCompare) +{ + for (Uint32 v = 0; v < NumVars; ++v) + { + const auto& CurrVarDesc = Variables[v]; + if ( ((CurrVarDesc.ShaderStages & ShaderStage) != 0) && NameCompare(CurrVarDesc.Name) ) + { + return CurrVarDesc.Type; + } + } + return DefaultVariableType; +} + +inline SHADER_RESOURCE_VARIABLE_TYPE GetShaderVariableType(SHADER_TYPE ShaderStage, + const Char* Name, + SHADER_RESOURCE_VARIABLE_TYPE DefaultVariableType, + const ShaderResourceVariableDesc* Variables, + Uint32 NumVars) +{ + return GetShaderVariableType(ShaderStage, DefaultVariableType, Variables, NumVars, + [&](const char* VarName) + { + return strcmp(VarName, Name) == 0; + } + ); +} + +inline SHADER_RESOURCE_VARIABLE_TYPE GetShaderVariableType(SHADER_TYPE ShaderStage, + const Char* Name, + const PipelineResourceLayoutDesc& LayoutDesc) +{ + return GetShaderVariableType(ShaderStage, Name, LayoutDesc.DefaultVariableType, LayoutDesc.Variables, LayoutDesc.NumVariables); +} + +inline SHADER_RESOURCE_VARIABLE_TYPE GetShaderVariableType(SHADER_TYPE ShaderStage, + const String& Name, + SHADER_RESOURCE_VARIABLE_TYPE DefaultVariableType, + const ShaderResourceVariableDesc* Variables, + Uint32 NumVars) +{ + return GetShaderVariableType(ShaderStage, DefaultVariableType, Variables, NumVars, + [&](const char* VarName) + { + return Name.compare(VarName) == 0; + } + ); +} + +inline SHADER_RESOURCE_VARIABLE_TYPE GetShaderVariableType(SHADER_TYPE ShaderStage, + const String& Name, + const PipelineResourceLayoutDesc& LayoutDesc) +{ + return GetShaderVariableType(ShaderStage, Name, LayoutDesc.DefaultVariableType, LayoutDesc.Variables, LayoutDesc.NumVariables); +} + +inline bool IsAllowedType(SHADER_RESOURCE_VARIABLE_TYPE VarType, Uint32 AllowedTypeBits)noexcept +{ + return ((1 << VarType) & AllowedTypeBits) != 0; +} + +inline Uint32 GetAllowedTypeBits(const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, Uint32 NumAllowedTypes)noexcept +{ + if(AllowedVarTypes == nullptr) + return 0xFFFFFFFF; + + Uint32 AllowedTypeBits = 0; + for(Uint32 i=0; i < NumAllowedTypes; ++i) + AllowedTypeBits |= 1 << AllowedVarTypes[i]; + return AllowedTypeBits; +} + +/// Base implementation of a shader variable +struct ShaderVariableBase : public IShaderResourceVariable +{ + ShaderVariableBase(IObject& Owner) : + // Shader variables are always created as part of the shader, or + // shader resource binding, so we must provide owner pointer to + // the base class constructor + m_Owner(Owner) + { + } + + IObject& GetOwner() + { + return m_Owner; + } + + virtual IReferenceCounters* GetReferenceCounters()const override final + { + return m_Owner.GetReferenceCounters(); + } + + virtual Atomics::Long AddRef()override final + { + return m_Owner.AddRef(); + } + + virtual Atomics::Long Release()override final + { + return m_Owner.Release(); + } + + virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface )override final + { + if( ppInterface == nullptr ) + return; + + *ppInterface = nullptr; + if( IID == IID_ShaderResourceVariable || IID == IID_Unknown ) + { + *ppInterface = this; + (*ppInterface)->AddRef(); + } + } + +protected: + IObject& m_Owner; +}; + +} diff --git a/Graphics/GraphicsEngine/interface/Buffer.h b/Graphics/GraphicsEngine/interface/Buffer.h index 3bf17e94..d42c113c 100644 --- a/Graphics/GraphicsEngine/interface/Buffer.h +++ b/Graphics/GraphicsEngine/interface/Buffer.h @@ -171,10 +171,10 @@ class IBuffer : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const Diligent::INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns the buffer description used to create the object - virtual const BufferDesc& GetDesc()const = 0; + virtual const BufferDesc& GetDesc()const override = 0; /// Creates a new buffer view diff --git a/Graphics/GraphicsEngine/interface/BufferView.h b/Graphics/GraphicsEngine/interface/BufferView.h index 0f396dd4..2361a329 100644 --- a/Graphics/GraphicsEngine/interface/BufferView.h +++ b/Graphics/GraphicsEngine/interface/BufferView.h @@ -138,10 +138,10 @@ class IBufferView : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const Diligent::INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns the buffer view description used to create the object - virtual const BufferViewDesc& GetDesc()const = 0; + virtual const BufferViewDesc& GetDesc()const override = 0; /// Returns pointer to the referenced buffer object. diff --git a/Graphics/GraphicsEngine/interface/DeviceContext.h b/Graphics/GraphicsEngine/interface/DeviceContext.h index ed9aa996..46b48337 100644 --- a/Graphics/GraphicsEngine/interface/DeviceContext.h +++ b/Graphics/GraphicsEngine/interface/DeviceContext.h @@ -450,7 +450,7 @@ class IDeviceContext : public IObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details. - virtual void QueryInterface(const Diligent::INTERFACE_ID& IID, IObject** ppInterface) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Sets the pipeline state. diff --git a/Graphics/GraphicsEngine/interface/DeviceObject.h b/Graphics/GraphicsEngine/interface/DeviceObject.h index e74ebdc7..939ad080 100644 --- a/Graphics/GraphicsEngine/interface/DeviceObject.h +++ b/Graphics/GraphicsEngine/interface/DeviceObject.h @@ -41,7 +41,7 @@ class IDeviceObject : public IObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface )override = 0; /// Returns the buffer object description virtual const DeviceObjectAttribs& GetDesc()const = 0; diff --git a/Graphics/GraphicsEngine/interface/Fence.h b/Graphics/GraphicsEngine/interface/Fence.h index 06708e5d..7a7ee8c5 100644 --- a/Graphics/GraphicsEngine/interface/Fence.h +++ b/Graphics/GraphicsEngine/interface/Fence.h @@ -48,10 +48,10 @@ class IFence : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const Diligent::INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns the fence description used to create the object - virtual const FenceDesc& GetDesc()const = 0; + virtual const FenceDesc& GetDesc()const override = 0; /// Returns the last completed value signaled by the GPU virtual Uint64 GetCompletedValue() = 0; diff --git a/Graphics/GraphicsEngine/interface/GraphicsTypes.h b/Graphics/GraphicsEngine/interface/GraphicsTypes.h index d402fb18..8de7d784 100644 --- a/Graphics/GraphicsEngine/interface/GraphicsTypes.h +++ b/Graphics/GraphicsEngine/interface/GraphicsTypes.h @@ -1242,7 +1242,7 @@ namespace Diligent }; /// Engine creation attibutes - struct EngineCreationAttribs + struct EngineCreateInfo { /// Pointer to the raw memory allocator that will be used for all memory allocation/deallocation /// operations in the engine @@ -1252,8 +1252,64 @@ namespace Diligent DebugMessageCallbackType DebugMessageCallback = nullptr; }; + + /// Attributes of the OpenGL-based engine implementation + struct EngineGLCreateInfo : public EngineCreateInfo + { + /// Native window handle + + /// * On Win32 platform, this is a window handle (HWND) + /// * On Android platform, this is a pointer to the native window (ANativeWindow*) + /// * On Linux, this is the native window (Window) + void* pNativeWndHandle = nullptr; + +#if PLATFORM_LINUX + /// For linux platform only, this is the pointer to the display + void* pDisplay = nullptr; +#endif + }; + + + /// 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 specific to D3D11 engine + struct EngineD3D11CreateInfo : public EngineCreateInfo + { + 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; + + EngineD3D11CreateInfo() : + DebugFlags(0) + { +#ifdef _DEBUG + DebugFlags = static_cast<Uint32>(EngineD3D11DebugFlags::VerifyCommittedShaderResources); +#endif + } + }; + /// Attributes specific to D3D12 engine - struct EngineD3D12Attribs : public EngineCreationAttribs + struct EngineD3D12CreateInfo : public EngineCreateInfo { static constexpr Uint32 DefaultAdapterId = 0xFFFFFFFF; @@ -1310,7 +1366,7 @@ namespace Diligent }; /// Attributes specific to Vulkan engine - struct EngineVkAttribs : public EngineCreationAttribs + struct EngineVkCreateInfo : public EngineCreateInfo { /// Enable validation layers. Validation is always enabled in Debug mode bool EnableValidation = false; @@ -1432,6 +1488,14 @@ namespace Diligent Uint32 DynamicHeapPageSize = 256 << 10; }; + + /// Attributes of the Metal-based engine implementation + struct EngineMtlCreateInfo : public EngineCreateInfo + { + + }; + + /// Box struct Box { diff --git a/Graphics/GraphicsEngine/interface/PipelineState.h b/Graphics/GraphicsEngine/interface/PipelineState.h index 4bbbbe9e..3b749f60 100644 --- a/Graphics/GraphicsEngine/interface/PipelineState.h +++ b/Graphics/GraphicsEngine/interface/PipelineState.h @@ -34,6 +34,9 @@ #include "DepthStencilState.h" #include "InputLayout.h" #include "ShaderResourceBinding.h" +#include "ShaderResourceVariable.h" +#include "Shader.h" +#include "Sampler.h" namespace Diligent { @@ -51,14 +54,82 @@ struct SampleDesc SampleDesc()noexcept{} - SampleDesc(Uint8 _Count, - Uint8 _Quality) : + SampleDesc(Uint8 _Count, Uint8 _Quality) noexcept : Count (_Count), Quality (_Quality) {} }; +/// Describes shader variable +struct ShaderResourceVariableDesc +{ + /// Shader stages this resources variable applies to. More than one shader stage can be specified. + SHADER_TYPE ShaderStages = SHADER_TYPE_UNKNOWN; + + /// Shader variable name + const Char* Name = nullptr; + + /// Shader variable type. See Diligent::SHADER_RESOURCE_VARIABLE_TYPE for a list of allowed types + SHADER_RESOURCE_VARIABLE_TYPE Type = SHADER_RESOURCE_VARIABLE_TYPE_STATIC; + + ShaderResourceVariableDesc()noexcept{} + + ShaderResourceVariableDesc(SHADER_TYPE _ShaderStages, const Char* _Name, SHADER_RESOURCE_VARIABLE_TYPE _Type)noexcept : + ShaderStages(_ShaderStages), + Name (_Name), + Type (_Type) + {} +}; + + +/// Static sampler description +struct StaticSamplerDesc +{ + /// Shader stages that this static sampler applies to. More than one shader stage can be specified. + SHADER_TYPE ShaderStages = SHADER_TYPE_UNKNOWN; + + /// The name of the sampler itself or the name of the texture variable that + /// this static sampler is assigned to if combined texture samplers are used. + const Char* SamplerOrTextureName = nullptr; + + /// Sampler description + SamplerDesc Desc; + + StaticSamplerDesc()noexcept{} + + StaticSamplerDesc(SHADER_TYPE _ShaderStages, + const Char* _SamplerOrTextureName, + const SamplerDesc& _Desc)noexcept : + ShaderStages (_ShaderStages), + SamplerOrTextureName(_SamplerOrTextureName), + Desc (_Desc) + {} +}; + + +/// Pipeline layout description +struct PipelineResourceLayoutDesc +{ + /// Default shader resource variable type. This type will be used if shader + /// variable description is not found in the Variables array + /// or if Variables == nullptr + SHADER_RESOURCE_VARIABLE_TYPE DefaultVariableType = SHADER_RESOURCE_VARIABLE_TYPE_STATIC; + + /// Number of elements in Variables array + Uint32 NumVariables = 0; + + /// Array of shader resource variable descriptions + const ShaderResourceVariableDesc* Variables = nullptr; + + /// Number of static samplers in StaticSamplers array + Uint32 NumStaticSamplers = 0; + + /// Array of static sampler descriptions + const StaticSamplerDesc* StaticSamplers = nullptr; +}; + + /// Graphics pipeline state description /// This structure describes the graphics pipeline state and is part of the PipelineStateDesc structure. @@ -150,6 +221,9 @@ struct PipelineStateDesc : DeviceObjectAttribs /// Defines which command queues this pipeline state can be used with Uint64 CommandQueueMask = 1; + /// Pipeline layout description + PipelineResourceLayoutDesc ResourceLayout; + /// Graphics pipeline state description. This memeber is ignored if IsComputePipeline == True GraphicsPipelineDesc GraphicsPipeline; @@ -168,16 +242,53 @@ class IPipelineState : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface )override = 0; + /// Returns the blend state description used to create the object - virtual const PipelineStateDesc& GetDesc()const = 0; + virtual const PipelineStateDesc& GetDesc()const override = 0; + /// Binds resources for all shaders in the pipeline state + /// \param [in] ShaderFlags - Flags that specify shader stages, for which resources will be bound. + /// Any combination of Diligent::SHADER_TYPE may be used. /// \param [in] pResourceMapping - Pointer to the resource mapping interface. /// \param [in] Flags - Additional flags. See Diligent::BIND_SHADER_RESOURCES_FLAGS. - virtual void BindShaderResources( IResourceMapping* pResourceMapping, Uint32 Flags ) = 0; + virtual void BindStaticResources(Uint32 ShaderFlags, IResourceMapping* pResourceMapping, Uint32 Flags) = 0; + + + /// Returns the number of static shader resource variables. + + /// \param [in] ShaderType - Type of the shader. + /// \remark Only static variables (that can be accessed directly through the PSO) are counted. + /// Mutable and dynamic variables are accessed through Shader Resource Binding object. + virtual Uint32 GetStaticVariableCount(SHADER_TYPE ShaderType) const = 0; + + + /// Returns static shader resource variable. If the variable is not found, + /// returns nullptr. + + /// \param [in] ShaderType - Type of the shader to look up the variable. + /// Must be one of Diligent::SHADER_TYPE. + /// \param [in] Name - Name of the variable. + /// \remark The method does not increment the reference counter + /// of the returned interface. + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, const Char* Name) = 0; + + + /// Returns static shader resource variable by its index. + + /// \param [in] ShaderType - Type of the shader to look up the variable. + /// Must be one of Diligent::SHADER_TYPE. + /// \param [in] Index - Shader variable index. The index must be between + /// 0 and the total number of variables returned by + /// GetStaticVariableCount(). + /// \remark Only static shader resource variables can be accessed through this method. + /// Mutable and dynamic variables are accessed through Shader Resource + /// Binding object + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, Uint32 Index) = 0; + /// Creates a shader resource binding object @@ -186,7 +297,8 @@ public: /// \param [in] InitStaticResources - if set to true, the method will initialize static resources in /// the created object, which has the exact same effect as calling /// IShaderResourceBinding::InitializeStaticResources(). - virtual void CreateShaderResourceBinding( IShaderResourceBinding** ppShaderResourceBinding, bool InitStaticResources = false ) = 0; + virtual void CreateShaderResourceBinding(IShaderResourceBinding** ppShaderResourceBinding, bool InitStaticResources = false) = 0; + /// Checks if this pipeline state object is compatible with another PSO diff --git a/Graphics/GraphicsEngine/interface/RenderDevice.h b/Graphics/GraphicsEngine/interface/RenderDevice.h index c844df8a..ece8eda3 100644 --- a/Graphics/GraphicsEngine/interface/RenderDevice.h +++ b/Graphics/GraphicsEngine/interface/RenderDevice.h @@ -57,7 +57,7 @@ class IRenderDevice : public IObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface )override = 0; /// Creates a new buffer object @@ -80,14 +80,13 @@ public: /// Creates a new shader object - /// \param [in] CreationAttribs - Shader creation attributes, see - /// Diligent::ShaderCreationAttribs for details. + /// \param [in] ShaderCI - Shader create info, see Diligent::ShaderCreateInfo for details. /// \param [out] ppShader - Address of the memory location where the pointer to the /// shader interface will be stored. /// The function calls AddRef(), so that the new object will contain /// one refernce. - virtual void CreateShader(const ShaderCreationAttribs& CreationAttribs, - IShader** ppShader) = 0; + virtual void CreateShader(const ShaderCreateInfo& ShaderCI, + IShader** ppShader) = 0; /// Creates a new texture object diff --git a/Graphics/GraphicsEngine/interface/ResourceMapping.h b/Graphics/GraphicsEngine/interface/ResourceMapping.h index c3fb9e57..f8f8d49b 100644 --- a/Graphics/GraphicsEngine/interface/ResourceMapping.h +++ b/Graphics/GraphicsEngine/interface/ResourceMapping.h @@ -83,7 +83,7 @@ namespace Diligent { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface (const INTERFACE_ID& IID, IObject** ppInterface) = 0; + virtual void QueryInterface (const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Adds a resource to the mapping. diff --git a/Graphics/GraphicsEngine/interface/Sampler.h b/Graphics/GraphicsEngine/interface/Sampler.h index c8b3c540..cee595c5 100644 --- a/Graphics/GraphicsEngine/interface/Sampler.h +++ b/Graphics/GraphicsEngine/interface/Sampler.h @@ -170,10 +170,10 @@ class ISampler : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const Diligent::INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns the sampler description used to create the object - virtual const SamplerDesc& GetDesc()const = 0; + virtual const SamplerDesc& GetDesc()const override = 0; }; } diff --git a/Graphics/GraphicsEngine/interface/Shader.h b/Graphics/GraphicsEngine/interface/Shader.h index d9e90d87..c993bf4a 100644 --- a/Graphics/GraphicsEngine/interface/Shader.h +++ b/Graphics/GraphicsEngine/interface/Shader.h @@ -27,9 +27,8 @@ /// Definition of the Diligent::IShader interface and related data structures #include "../../../Primitives/interface/FileStream.h" +#include "../../../Primitives/interface/FlagEnum.h" #include "DeviceObject.h" -#include "ResourceMapping.h" -#include "Sampler.h" namespace Diligent { @@ -38,10 +37,6 @@ namespace Diligent static constexpr INTERFACE_ID IID_Shader = { 0x2989b45c, 0x143d, 0x4886, { 0xb8, 0x9c, 0xc3, 0x27, 0x1c, 0x2d, 0xcc, 0x5d } }; -// {0D57DF3F-977D-4C8F-B64C-6675814BC80C} -static constexpr INTERFACE_ID IID_ShaderVariable = -{ 0xd57df3f, 0x977d, 0x4c8f, { 0xb6, 0x4c, 0x66, 0x75, 0x81, 0x4b, 0xc8, 0xc } }; - /// Describes the shader type enum SHADER_TYPE : Uint32 { @@ -53,6 +48,7 @@ enum SHADER_TYPE : Uint32 SHADER_TYPE_DOMAIN = 0x010, ///< Domain (tessellation evaluation) shader SHADER_TYPE_COMPUTE = 0x020 ///< Compute shader }; +DEFINE_FLAG_ENUM_OPERATORS(SHADER_TYPE); enum SHADER_PROFILE : Uint8 { @@ -76,128 +72,16 @@ enum SHADER_SOURCE_LANGUAGE : Uint32 SHADER_SOURCE_LANGUAGE_GLSL }; -/// Describes shader variable type that is used by ShaderVariableDesc -enum SHADER_VARIABLE_TYPE : Uint8 -{ - /// Shader variable is constant across all shader instances. - /// It must be set *once* directly through IShader::BindResources() or through - /// the shader variable. - SHADER_VARIABLE_TYPE_STATIC = 0, - - /// Shader variable is constant across shader resource bindings instance (see IShaderResourceBinding). - /// It must be set *once* through IShaderResourceBinding::BindResources() or through - /// the shader variable. It cannot be set through IShader interface - SHADER_VARIABLE_TYPE_MUTABLE, - - /// Shader variable is dynamic. It can be set multiple times for every instance of shader resource - /// bindings (see IShaderResourceBinding). It cannot be set through IShader interface - SHADER_VARIABLE_TYPE_DYNAMIC, - - /// Total number of shader variable types - SHADER_VARIABLE_TYPE_NUM_TYPES -}; - - -static_assert(SHADER_VARIABLE_TYPE_STATIC == 0 && SHADER_VARIABLE_TYPE_MUTABLE == 1 && SHADER_VARIABLE_TYPE_DYNAMIC == 2 && SHADER_VARIABLE_TYPE_NUM_TYPES == 3, "BIND_SHADER_RESOURCES_UPDATE_* flags rely on shader variable SHADER_VARIABLE_TYPE_* values being 0,1,2"); -/// Describes flags that can be given to IShader::BindResources(), -/// IPipelineState::BindShaderResources(), and IDeviceContext::BindShaderResources() methods. -enum BIND_SHADER_RESOURCES_FLAGS : Uint32 -{ - /// Indicates that static variable bindings are to be updated. - BIND_SHADER_RESOURCES_UPDATE_STATIC = (0x01 << SHADER_VARIABLE_TYPE_STATIC), - - /// Indicates that mutable variable bindings are to be updated. - BIND_SHADER_RESOURCES_UPDATE_MUTABLE = (0x01 << SHADER_VARIABLE_TYPE_MUTABLE), - - /// Indicates that dynamic variable bindings are to be updated. - BIND_SHADER_RESOURCES_UPDATE_DYNAMIC = (0x01 << SHADER_VARIABLE_TYPE_DYNAMIC), - - /// Indicates that all variable types (static, mutable and dynamic) are to be updated. - /// \note If none of BIND_SHADER_RESOURCES_UPDATE_STATIC, BIND_SHADER_RESOURCES_UPDATE_MUTABLE, - /// and BIND_SHADER_RESOURCES_UPDATE_DYNAMIC flags are set, all variable types are updated - /// as if BIND_SHADER_RESOURCES_UPDATE_ALL was specified. - BIND_SHADER_RESOURCES_UPDATE_ALL = (BIND_SHADER_RESOURCES_UPDATE_STATIC | BIND_SHADER_RESOURCES_UPDATE_MUTABLE | BIND_SHADER_RESOURCES_UPDATE_DYNAMIC), - - /// If this flag is specified, all existing bindings will be preserved and - /// only unresolved ones will be updated. - /// If this flag is not specified, every shader variable will be - /// updated if the mapping contains corresponding resource. - BIND_SHADER_RESOURCES_KEEP_EXISTING = 0x08, - - /// If this flag is specified, all shader bindings are expected - /// to be resolved after the call. If this is not the case, debug message - /// will be displayed. - /// \note Only these variables are verified that are being updated by setting - /// BIND_SHADER_RESOURCES_UPDATE_STATIC, BIND_SHADER_RESOURCES_UPDATE_MUTABLE, and - /// BIND_SHADER_RESOURCES_UPDATE_DYNAMIC flags. - BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED = 0x10 -}; - -/// Describes shader variable -struct ShaderVariableDesc -{ - /// Shader variable name - const Char* Name = nullptr; - - /// Shader variable type. See Diligent::SHADER_VARIABLE_TYPE for a list of allowed types - SHADER_VARIABLE_TYPE Type = SHADER_VARIABLE_TYPE_STATIC; - - ShaderVariableDesc()noexcept{} - - ShaderVariableDesc(const Char* _Name, - SHADER_VARIABLE_TYPE _Type) : - Name(_Name), - Type(_Type) - {} -}; - - -/// Static sampler description -struct StaticSamplerDesc -{ - /// The name of the sampler itself or the name of the texture variable that - /// this static sampler is assigned to if combined texture samplers are used. - const Char* SamplerOrTextureName = nullptr; - - /// Sampler description - SamplerDesc Desc; - - StaticSamplerDesc()noexcept{} - StaticSamplerDesc(const Char* _SamplerOrTextureName, - const SamplerDesc& _Desc)noexcept : - SamplerOrTextureName(_SamplerOrTextureName), - Desc (_Desc) - {} -}; - /// Shader description struct ShaderDesc : DeviceObjectAttribs { - /// Shader type. See Diligent::SHADER_TYPE - SHADER_TYPE ShaderType = SHADER_TYPE_VERTEX; - - Bool bCacheCompiledShader = False; - - SHADER_PROFILE TargetProfile = SHADER_PROFILE_DEFAULT; - - /// Default shader variable type. This type will be used if shader - /// variable description is not found in array VariableDesc points to - /// or if VariableDesc == nullptr - SHADER_VARIABLE_TYPE DefaultVariableType = SHADER_VARIABLE_TYPE_STATIC; + /// Shader type. See Diligent::SHADER_TYPE. + SHADER_TYPE ShaderType = SHADER_TYPE_VERTEX; - /// Array of shader variable descriptions - const ShaderVariableDesc* VariableDesc = nullptr; - - /// Number of elements in VariableDesc array - Uint32 NumVariables = 0; - - /// Number of static samplers in StaticSamplers array - Uint32 NumStaticSamplers = 0; - - /// Array of static sampler descriptions - const StaticSamplerDesc* StaticSamplers = nullptr; + SHADER_PROFILE TargetProfile = SHADER_PROFILE_DEFAULT; }; + /// Shader source stream factory interface class IShaderSourceInputStreamFactory { @@ -219,7 +103,7 @@ struct ShaderMacro }; /// Shader creation attributes -struct ShaderCreationAttribs +struct ShaderCreateInfo { /// Source file path @@ -258,7 +142,7 @@ struct ShaderCreationAttribs /// backend expects SPIRV bytecode. /// The bytecode must contain reflection information. If shaders were compiled /// using fxc, make sure that /Qstrip_reflect option is *not* specified. - /// Also, shaders need to be compiled against 4.0 profile or higher. + /// HLSL shaders need to be compiled against 4.0 profile or higher. const void* ByteCode = nullptr; /// Size of the compiled shader bytecode @@ -305,41 +189,42 @@ struct ShaderCreationAttribs IDataBlob** ppCompilerOutput = nullptr; }; - -/// Shader resource variable -class IShaderVariable : public IObject +/// Describes shader resource type +enum SHADER_RESOURCE_TYPE : Uint8 { -public: - /// Sets the variable to the given value + /// Shader resource type is unknown + SHADER_RESOURCE_TYPE_UNKNOWN = 0, - /// \remark The method performs run-time correctness checks. - /// For instance, shader resource view cannot - /// be assigned to a constant buffer variable. - virtual void Set(IDeviceObject* pObject) = 0; + /// Constant (uniform) buffer + SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, - /// Sets the variable array + /// Shader resource view of a texture (sampled image) + SHADER_RESOURCE_TYPE_TEXTURE_SRV, - /// \param [in] ppObjects - pointer to the array of objects - /// \param [in] FirstElement - first array element to set - /// \param [in] NumElements - number of objects in ppObjects array - /// - /// \remark The method performs run-time correctness checks. - /// For instance, shader resource view cannot - /// be assigned to a constant buffer variable. - virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements) = 0; + /// Shader resource view of a buffer (read-only storage image) + SHADER_RESOURCE_TYPE_BUFFER_SRV, - /// Returns shader variable type - virtual SHADER_VARIABLE_TYPE GetType()const = 0; + /// Unordered access view of a texture (sotrage image) + SHADER_RESOURCE_TYPE_TEXTURE_UAV, - /// Returns array size. For non-array variables returns one. - virtual Uint32 GetArraySize()const = 0; + /// Unordered access view of a buffer (storage buffer) + SHADER_RESOURCE_TYPE_BUFFER_UAV, - /// Returns the variable name - virtual const Char* GetName()const = 0; + /// Sampler (separate sampler) + SHADER_RESOURCE_TYPE_SAMPLER +}; + +/// Shader resource description +struct ShaderResourceDesc +{ + /// Shader resource name + const char* Name = nullptr; - /// Returns variable index that can be used to access the variable through - /// shader or shader resource binding object - virtual Uint32 GetIndex()const = 0; + /// Shader resource type, see Diligent::SHADER_RESOURCE_TYPE. + SHADER_RESOURCE_TYPE Type = SHADER_RESOURCE_TYPE_UNKNOWN; + + /// Array size. For non-array resource this value is 1. + Uint32 ArraySize = 0; }; /// Shader interface @@ -347,42 +232,16 @@ class IShader : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns the shader description - virtual const ShaderDesc& GetDesc()const = 0; - - /// Binds shader resources. - /// \param [in] pResourceMapping - Pointer to IResourceMapping interface to - /// look for resources. - /// \param [in] Flags - Additional flags for the operation. See - /// Diligent::BIND_SHADER_RESOURCES_FLAGS for details. - /// \remark The shader will keep strong references to all resources bound to it. - virtual void BindResources( IResourceMapping* pResourceMapping, Uint32 Flags ) = 0; - - /// Returns an interface to a shader variable. If the shader variable - /// is not found, an interface to a dummy variable will be returned. - - /// \param [in] Name - Name of the variable. - /// \remark The method does not increment the reference counter - /// of the returned interface. - virtual IShaderVariable* GetShaderVariable(const Char* Name) = 0; - - /// Returns the number of shader variables. - - /// \remark Only static variables (that can be accessed directly through the shader) are counted. - /// Mutable and dynamic variables are accessed through Shader Resource Binding object. - virtual Uint32 GetVariableCount() const = 0; - - /// Returns shader variable by its index. - - /// \param [in] Index - Shader variable index. The index must be between - /// 0 and the total number of variables returned by - /// GetVariableCount(). - /// \remark Only static shader variables can be accessed through this method. - /// Mutable and dynamic variables are accessed through Shader Resource - /// Binding object - virtual IShaderVariable* GetShaderVariable(Uint32 Index) = 0; + virtual const ShaderDesc& GetDesc()const override = 0; + + /// Returns the total number of shader resources + virtual Uint32 GetResourceCount()const = 0; + + /// Returns the pointer to the array of shader resources + virtual ShaderResourceDesc GetResource(Uint32 Index)const = 0; }; } diff --git a/Graphics/GraphicsEngine/interface/ShaderResourceBinding.h b/Graphics/GraphicsEngine/interface/ShaderResourceBinding.h index 8671c3f7..0afaa1f1 100644 --- a/Graphics/GraphicsEngine/interface/ShaderResourceBinding.h +++ b/Graphics/GraphicsEngine/interface/ShaderResourceBinding.h @@ -28,6 +28,8 @@ #include "../../../Primitives/interface/Object.h" #include "Shader.h" +#include "ShaderResourceVariable.h" +#include "ResourceMapping.h" namespace Diligent { @@ -44,7 +46,7 @@ class IShaderResourceBinding : public IObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns pointer to the referenced buffer object. @@ -52,20 +54,20 @@ public: /// so Release() must be called to avoid memory leaks. virtual IPipelineState* GetPipelineState() = 0; - /// Binds all resource using the resource mapping + /// Binds mutable and dynamice resources using the resource mapping - /// \param [in] ShaderFlags - Flags for the shader stages, for which resources will be bound. - /// Any combination of Diligent::SHADER_TYPE may be specified. + /// \param [in] ShaderFlags - Flags that specify shader stages, for which resources will be bound. + /// Any combination of Diligent::SHADER_TYPE may be used. /// \param [in] pResMapping - Shader resource mapping, where required resources will be looked up - /// \param [in] Flags - Additional flags. See Diligent::BIND_SHADER_RESOURCES_FLAGS. + /// \param [in] Flags - Additional flags. See Diligent::BIND_SHADER_RESOURCES_FLAGS. virtual void BindResources(Uint32 ShaderFlags, IResourceMapping* pResMapping, Uint32 Flags) = 0; /// Returns variable /// \param [in] ShaderType - Type of the shader to look up the variable. /// Must be one of Diligent::SHADER_TYPE. - /// \param Name - Variable name - virtual IShaderVariable* GetVariable(SHADER_TYPE ShaderType, const char *Name) = 0; + /// \param [in] Name - Variable name + virtual IShaderResourceVariable* GetVariable(SHADER_TYPE ShaderType, const char* Name) = 0; /// Returns the total variable count for the specific shader stage. @@ -79,20 +81,21 @@ public: /// \param [in] ShaderType - Type of the shader to look up the variable. /// Must be one of Diligent::SHADER_TYPE. - /// \param Index - Variable index. The index must be between 0 and the total number - /// of variables in this shader stage as returned by GetVariableCount(). + /// \param [in] Index - Variable index. The index must be between 0 and the total number + /// of variables in this shader stage as returned by + /// IShaderResourceBinding::GetVariableCount(). /// \remark Only mutable and dynamic variables can be accessed through this method. /// Static variables are accessed through the Shader object. - virtual IShaderVariable* GetVariable(SHADER_TYPE ShaderType, Uint32 Index) = 0; + virtual IShaderResourceVariable* GetVariable(SHADER_TYPE ShaderType, Uint32 Index) = 0; /// Initializes static resources - /// If shaders in the pipeline state contain static resources - /// (see Diligent::SHADER_VARIABLE_TYPE_STATIC), this method must be called + /// If the parent pipeline state object contain static resources + /// (see Diligent::SHADER_RESOURCE_VARIABLE_TYPE_STATIC), this method must be called /// once to initialize static resources in this shader resource binding object. /// The method must be called after all static variables are initialized - /// in the shaders. + /// in the PSO. /// \param [in] pPipelineState - Pipeline state to copy static shader resource /// bindings from. The pipeline state must be compatible /// with this shader resource binding object. diff --git a/Graphics/GraphicsEngine/interface/ShaderResourceVariable.h b/Graphics/GraphicsEngine/interface/ShaderResourceVariable.h new file mode 100644 index 00000000..95b6eae5 --- /dev/null +++ b/Graphics/GraphicsEngine/interface/ShaderResourceVariable.h @@ -0,0 +1,134 @@ +/* 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 Diligent::IShaderResourceVariable interface and related data structures + +#include "../../../Primitives/interface/BasicTypes.h" +#include "../../../Primitives/interface/Object.h" +#include "DeviceObject.h" + +namespace Diligent +{ + +// {0D57DF3F-977D-4C8F-B64C-6675814BC80C} +static constexpr INTERFACE_ID IID_ShaderResourceVariable = +{ 0xd57df3f, 0x977d, 0x4c8f, { 0xb6, 0x4c, 0x66, 0x75, 0x81, 0x4b, 0xc8, 0xc } }; + + +/// Describes the type of the shader resource variable +enum SHADER_RESOURCE_VARIABLE_TYPE : Uint8 +{ + /// Shader resource bound to the variable is the same for all SRB instances. + /// It must be set *once* directly through Pipeline State object. + SHADER_RESOURCE_VARIABLE_TYPE_STATIC = 0, + + /// Shader resource bound to the variable is specific to the shader resource binding + /// instance (see Diligent::IShaderResourceBinding). It must be set *once* through + /// Diligent::IShaderResourceBinding interface. It cannot be set through Diligent::IPipelineState + /// interface and cannot be change once bound. + SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, + + /// Shader variable binding is dynamic. It can be set multiple times for every instance of shader resource + /// binding (see Diligent::IShaderResourceBinding). It cannot be set through Diligent::IPipelineState interface. + SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC, + + /// Total number of shader variable types + SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES +}; + + +static_assert(SHADER_RESOURCE_VARIABLE_TYPE_STATIC == 0 && SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE == 1 && SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC == 2 && SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES == 3, "BIND_SHADER_RESOURCES_UPDATE_* flags rely on shader variable SHADER_RESOURCE_VARIABLE_TYPE_* values being 0,1,2"); + +/// Shader resource binding flags +enum BIND_SHADER_RESOURCES_FLAGS : Uint32 +{ + /// Indicates that static shader variable bindings are to be updated. + BIND_SHADER_RESOURCES_UPDATE_STATIC = (0x01 << SHADER_RESOURCE_VARIABLE_TYPE_STATIC), + + /// Indicates that mutable shader variable bindings are to be updated. + BIND_SHADER_RESOURCES_UPDATE_MUTABLE = (0x01 << SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE), + + /// Indicates that dynamic shader variable bindings are to be updated. + BIND_SHADER_RESOURCES_UPDATE_DYNAMIC = (0x01 << SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC), + + /// Indicates that all shader variable types (static, mutable and dynamic) are to be updated. + /// \note If none of BIND_SHADER_RESOURCES_UPDATE_STATIC, BIND_SHADER_RESOURCES_UPDATE_MUTABLE, + /// and BIND_SHADER_RESOURCES_UPDATE_DYNAMIC flags are set, all variable types are updated + /// as if BIND_SHADER_RESOURCES_UPDATE_ALL was specified. + BIND_SHADER_RESOURCES_UPDATE_ALL = (BIND_SHADER_RESOURCES_UPDATE_STATIC | BIND_SHADER_RESOURCES_UPDATE_MUTABLE | BIND_SHADER_RESOURCES_UPDATE_DYNAMIC), + + /// If this flag is specified, all existing bindings will be preserved and + /// only unresolved ones will be updated. + /// If this flag is not specified, every shader variable will be + /// updated if the mapping contains corresponding resource. + BIND_SHADER_RESOURCES_KEEP_EXISTING = 0x08, + + /// If this flag is specified, all shader bindings are expected + /// to be resolved after the call. If this is not the case, debug message + /// will be displayed. + /// \note Only these variables are verified that are being updated by setting + /// BIND_SHADER_RESOURCES_UPDATE_STATIC, BIND_SHADER_RESOURCES_UPDATE_MUTABLE, and + /// BIND_SHADER_RESOURCES_UPDATE_DYNAMIC flags. + BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED = 0x10 +}; + + +/// Shader resource variable +class IShaderResourceVariable : public IObject +{ +public: + /// Binds resource to the variable + + /// \remark The method performs run-time correctness checks. + /// For instance, shader resource view cannot + /// be assigned to a constant buffer variable. + virtual void Set(IDeviceObject* pObject) = 0; + + /// Binds resource array to the variable + + /// \param [in] ppObjects - pointer to the array of objects + /// \param [in] FirstElement - first array element to set + /// \param [in] NumElements - number of objects in ppObjects array + /// + /// \remark The method performs run-time correctness checks. + /// For instance, shader resource view cannot + /// be assigned to a constant buffer variable. + virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements) = 0; + + /// Returns the shader resource variable type + virtual SHADER_RESOURCE_VARIABLE_TYPE GetType()const = 0; + + /// Returns array size. For non-array variables returns one. + virtual Uint32 GetArraySize()const = 0; + + /// Returns the variable name + virtual const Char* GetName()const = 0; + + /// Returns the variable index that can be used to access the variable. + virtual Uint32 GetIndex()const = 0; +}; + +} diff --git a/Graphics/GraphicsEngine/interface/Texture.h b/Graphics/GraphicsEngine/interface/Texture.h index 7c3cb941..273e8f95 100644 --- a/Graphics/GraphicsEngine/interface/Texture.h +++ b/Graphics/GraphicsEngine/interface/Texture.h @@ -273,10 +273,10 @@ class ITexture : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns the texture description used to create the object - virtual const TextureDesc& GetDesc()const = 0; + virtual const TextureDesc& GetDesc()const override = 0; /// Creates a new texture view @@ -297,7 +297,7 @@ public: /// until all views are released.\n /// The function calls AddRef() for the created interface, so it must be released by /// a call to Release() when it is no longer needed. - virtual void CreateView(const struct TextureViewDesc &ViewDesc, class ITextureView **ppView) = 0; + virtual void CreateView(const struct TextureViewDesc& ViewDesc, class ITextureView** ppView) = 0; /// Returns the pointer to the default view. @@ -306,7 +306,7 @@ public: /// /// \note The function does not increase the reference counter for the returned interface, so /// Release() must *NOT* be called. - virtual ITextureView* GetDefaultView( TEXTURE_VIEW_TYPE ViewType ) = 0; + virtual ITextureView* GetDefaultView(TEXTURE_VIEW_TYPE ViewType) = 0; /// Returns native texture handle specific to the underlying graphics API diff --git a/Graphics/GraphicsEngine/interface/TextureView.h b/Graphics/GraphicsEngine/interface/TextureView.h index d77cf87a..dc03314f 100644 --- a/Graphics/GraphicsEngine/interface/TextureView.h +++ b/Graphics/GraphicsEngine/interface/TextureView.h @@ -163,10 +163,10 @@ class ITextureView : public IDeviceObject { public: /// Queries the specific interface, see IObject::QueryInterface() for details - virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface ) = 0; + virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override = 0; /// Returns the texture view description used to create the object - virtual const TextureViewDesc& GetDesc()const = 0; + virtual const TextureViewDesc& GetDesc()const override = 0; /// Sets the texture sampler to use for filtering operations /// when accessing a texture from shaders. Only |
