diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-10-19 03:28:09 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-10-19 03:28:09 +0000 |
| commit | f93288162c398987d238e4ead5186d8b2000fc5a (patch) | |
| tree | 4905a6ae3e1780f4562fa7531b2b1f86efdda592 /Graphics | |
| parent | Implemented separate samplers in D3D12 (diff) | |
| download | DiligentCore-f93288162c398987d238e4ead5186d8b2000fc5a.tar.gz DiligentCore-f93288162c398987d238e4ead5186d8b2000fc5a.zip | |
Updated BIND_SHADER_RESOURCES_* flags
Diffstat (limited to 'Graphics')
5 files changed, 80 insertions, 51 deletions
diff --git a/Graphics/GraphicsEngine/interface/Shader.h b/Graphics/GraphicsEngine/interface/Shader.h index 01d7155b..dc0b29be 100644 --- a/Graphics/GraphicsEngine/interface/Shader.h +++ b/Graphics/GraphicsEngine/interface/Shader.h @@ -76,26 +76,6 @@ enum SHADER_SOURCE_LANGUAGE : Uint32 SHADER_SOURCE_LANGUAGE_GLSL }; -/// Describes flags that can be supplied to IShader::BindResources() -/// and IDeviceContext::BindShaderResources(). -enum BIND_SHADER_RESOURCES_FLAGS : Uint32 -{ - /// Reset all bindings. If this flag is specified, all existing bindings will be - /// broken. By default all existing bindings are preserved. - BIND_SHADER_RESOURCES_RESET_BINDINGS = 0x01, - - /// If this flag is specified, only unresolved bindings will be updated. - /// All resolved bindings will keep their original values. - /// If this flag is not specified, every shader variable will be - /// updated if the mapping contains corresponding resource. - BIND_SHADER_RESOURCES_UPDATE_UNRESOLVED = 0x02, - - /// If this flag is specified, all shader bindings are expected - /// to be resolved after the call. If this is not the case, debug error - /// will be displayed. - BIND_SHADER_RESOURCES_ALL_RESOLVED = 0x04 -}; - /// Describes shader variable type that is used by ShaderVariableDesc enum SHADER_VARIABLE_TYPE : Uint8 { @@ -117,6 +97,42 @@ enum SHADER_VARIABLE_TYPE : Uint8 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 { diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index 00efe57d..22580688 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -692,25 +692,25 @@ public: template<typename ResourceType>
void Bind( ResourceType &Res)
{
- for(Uint16 elem=0; elem < Res.Attribs.BindCount; ++elem)
- {
- if( Flags & BIND_SHADER_RESOURCES_RESET_BINDINGS )
- Res.BindResource(nullptr, elem);
+ if ( (Flags & (1 << Res.Attribs.GetVariableType())) == 0 )
+ return;
- if( (Flags & BIND_SHADER_RESOURCES_UPDATE_UNRESOLVED) && Res.IsBound(elem) )
- return;
+ for (Uint16 elem=0; elem < Res.Attribs.BindCount; ++elem)
+ {
+ if ( (Flags & BIND_SHADER_RESOURCES_KEEP_EXISTING) && Res.IsBound(elem) )
+ continue;
const auto* VarName = Res.Attribs.Name;
RefCntAutoPtr<IDeviceObject> pRes;
ResourceMapping.GetResource( VarName, &pRes, elem );
- if( pRes )
+ if (pRes)
{
// Call non-virtual function
Res.BindResource(pRes, elem);
}
else
{
- if( (Flags & BIND_SHADER_RESOURCES_ALL_RESOLVED) && !Res.IsBound(elem) )
+ if ( (Flags & BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED) && !Res.IsBound(elem) )
LOG_ERROR_MESSAGE( "Cannot bind resource to shader variable \"", VarName, "\": resource view not found in the resource mapping" );
}
}
@@ -730,6 +730,9 @@ void ShaderResourceLayoutD3D11::BindResources( IResourceMapping* pResourceMappin LOG_ERROR_MESSAGE( "Failed to bind resources in shader \"", GetShaderName(), "\": resource mapping is null" );
return;
}
+
+ if ( (Flags & BIND_SHADER_RESOURCES_UPDATE_ALL) == 0 )
+ Flags |= BIND_SHADER_RESOURCES_UPDATE_ALL;
BindResourceHelper BindResHelper(*pResourceMapping, Flags);
diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp index f9930073..8688ef59 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp @@ -169,30 +169,33 @@ void ShaderVariableManagerD3D12::BindResources( IResourceMapping* pResourceMappi VERIFY_EXPR(m_pResourceCache != nullptr); DEV_CHECK_ERR(pResourceMapping != nullptr, "Failed to bind resources: resource mapping is null"); - for(Uint32 v=0; v < m_NumVariables; ++v) + if ( (Flags & BIND_SHADER_RESOURCES_UPDATE_ALL) == 0 ) + Flags |= BIND_SHADER_RESOURCES_UPDATE_ALL; + + for (Uint32 v=0; v < m_NumVariables; ++v) { auto &Var = m_pVariables[v]; const auto& Res = Var.m_Resource; - for(Uint32 ArrInd = 0; ArrInd < Res.Attribs.BindCount; ++ArrInd) - { - if (Flags & BIND_SHADER_RESOURCES_RESET_BINDINGS) - Res.BindResource(nullptr, ArrInd, *m_pResourceCache); + if ( (Flags & (1 << Res.Attribs.GetVariableType())) == 0 ) + continue; - if( (Flags & BIND_SHADER_RESOURCES_UPDATE_UNRESOLVED) && Res.IsBound(ArrInd, *m_pResourceCache) ) - return; + for (Uint32 ArrInd = 0; ArrInd < Res.Attribs.BindCount; ++ArrInd) + { + if( (Flags & BIND_SHADER_RESOURCES_KEEP_EXISTING) && Res.IsBound(ArrInd, *m_pResourceCache) ) + continue; RefCntAutoPtr<IDeviceObject> pObj; VERIFY_EXPR(pResourceMapping != nullptr); pResourceMapping->GetResource( Res.Attribs.Name, &pObj, ArrInd ); - if( pObj ) + if ( pObj ) { // Call non-virtual function Res.BindResource(pObj, ArrInd, *m_pResourceCache); } else { - if( (Flags & BIND_SHADER_RESOURCES_ALL_RESOLVED) && !Res.IsBound(ArrInd, *m_pResourceCache) ) + if( (Flags & BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED) && !Res.IsBound(ArrInd, *m_pResourceCache) ) LOG_ERROR_MESSAGE( "Cannot bind resource to shader variable \"", Res.Attribs.GetPrintName(ArrInd), "\": resource view not found in the resource mapping" ); } } diff --git a/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp b/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp index a845c613..cb2cbe36 100644 --- a/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/GLProgramResources.cpp @@ -428,16 +428,17 @@ namespace Diligent template<typename TResArrayType> void BindResourcesHelper(TResArrayType &ResArr, IResourceMapping *pResourceMapping, Uint32 Flags) { - for( auto res = ResArr.begin(); res != ResArr.end(); ++res ) + for (auto& res : ResArr) { - auto &Name = res->Name; - for(Uint32 ArrInd = 0; ArrInd < res->pResources.size(); ++ArrInd) + if ( (Flags & (1 << res.VarType)) == 0 ) + continue; + + auto &Name = res.Name; + for(Uint32 ArrInd = 0; ArrInd < res.pResources.size(); ++ArrInd) { - auto &CurrResource = res->pResources[ArrInd]; - if( Flags & BIND_SHADER_RESOURCES_RESET_BINDINGS ) - CurrResource.Release(); + auto &CurrResource = res.pResources[ArrInd]; - if( (Flags & BIND_SHADER_RESOURCES_UPDATE_UNRESOLVED) && CurrResource ) + if( (Flags & BIND_SHADER_RESOURCES_KEEP_EXISTING) && CurrResource ) continue; // Skip already resolved resources RefCntAutoPtr<IDeviceObject> pNewRes; @@ -445,13 +446,13 @@ namespace Diligent if (pNewRes != nullptr) { - if(res->VarType == SHADER_VARIABLE_TYPE_STATIC && CurrResource != nullptr && CurrResource != pNewRes ) + if(res.VarType == SHADER_VARIABLE_TYPE_STATIC && CurrResource != nullptr && CurrResource != pNewRes ) LOG_ERROR_MESSAGE( "Updating binding for static variable \"", Name, "\" is invalid and may result in an undefined behavior" ); CurrResource = pNewRes; } else { - if ( CurrResource == nullptr && (Flags & BIND_SHADER_RESOURCES_ALL_RESOLVED) ) + if ( CurrResource == nullptr && (Flags & BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED) ) LOG_ERROR_MESSAGE("Resource \"", Name, "\" is not found in the resource mapping"); } } @@ -463,6 +464,9 @@ namespace Diligent if( !pResourceMapping ) return; + if ( (Flags & BIND_SHADER_RESOURCES_UPDATE_ALL) == 0 ) + Flags |= BIND_SHADER_RESOURCES_UPDATE_ALL; + BindResourcesHelper( m_UniformBlocks, pResourceMapping, Flags ); BindResourcesHelper( m_Samplers, pResourceMapping, Flags ); BindResourcesHelper( m_Images, pResourceMapping, Flags ); diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp index f1019e7c..0751cd50 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp @@ -159,6 +159,9 @@ void ShaderVariableManagerVk::BindResources( IResourceMapping* pResourceMapping, return; } + if ( (Flags & BIND_SHADER_RESOURCES_UPDATE_ALL) == 0 ) + Flags |= BIND_SHADER_RESOURCES_UPDATE_ALL; + for(Uint32 v=0; v < m_NumVariables; ++v) { auto &Var = m_pVariables[v]; @@ -168,12 +171,12 @@ void ShaderVariableManagerVk::BindResources( IResourceMapping* pResourceMapping, if(Res.SpirvAttribs.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateSampler && Res.SpirvAttribs.StaticSamplerInd >= 0) continue; + if ( (Flags & (1 << Res.SpirvAttribs.VarType)) == 0 ) + continue; + for(Uint32 ArrInd = 0; ArrInd < Res.SpirvAttribs.ArraySize; ++ArrInd) { - if( Flags & BIND_SHADER_RESOURCES_RESET_BINDINGS ) - Res.BindResource(nullptr, ArrInd, *m_pResourceCache); - - if( (Flags & BIND_SHADER_RESOURCES_UPDATE_UNRESOLVED) && Res.IsBound(ArrInd, *m_pResourceCache) ) + if( (Flags & BIND_SHADER_RESOURCES_KEEP_EXISTING) && Res.IsBound(ArrInd, *m_pResourceCache) ) continue; const auto* VarName = Res.SpirvAttribs.Name; @@ -185,7 +188,7 @@ void ShaderVariableManagerVk::BindResources( IResourceMapping* pResourceMapping, } else { - if( (Flags & BIND_SHADER_RESOURCES_ALL_RESOLVED) && !Res.IsBound(ArrInd, *m_pResourceCache) ) + if( (Flags & BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED) && !Res.IsBound(ArrInd, *m_pResourceCache) ) LOG_ERROR_MESSAGE( "Cannot bind resource to shader variable \"", Res.SpirvAttribs.GetPrintName(ArrInd), "\": resource view not found in the resource mapping" ); } } |
