Path: blob/21.2-virgl/src/microsoft/resource_state_manager/D3D12ResourceState.cpp
4560 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "D3D12ResourceState.h"2425//----------------------------------------------------------------------------------------------------------------------------------26D3D12_RESOURCE_STATES CDesiredResourceState::GetSubresourceState(UINT SubresourceIndex) const27{28if (AreAllSubresourcesSame())29{30SubresourceIndex = 0;31}32return m_spSubresourceStates[SubresourceIndex];33}3435//----------------------------------------------------------------------------------------------------------------------------------36void CDesiredResourceState::UpdateSubresourceState(unsigned SubresourceIndex, D3D12_RESOURCE_STATES state)37{38assert(SubresourceIndex < m_spSubresourceStates.size());39if (m_spSubresourceStates[SubresourceIndex] == UNKNOWN_RESOURCE_STATE ||40state == UNKNOWN_RESOURCE_STATE ||41IsD3D12WriteState(state))42{43m_spSubresourceStates[SubresourceIndex] = state;44}45else46{47// Accumulate read state state bits48m_spSubresourceStates[SubresourceIndex] |= state;49}50}5152//----------------------------------------------------------------------------------------------------------------------------------53void CDesiredResourceState::SetResourceState(D3D12_RESOURCE_STATES state)54{55m_bAllSubresourcesSame = true;56UpdateSubresourceState(0, state);57}5859//----------------------------------------------------------------------------------------------------------------------------------60void CDesiredResourceState::SetSubresourceState(UINT SubresourceIndex, D3D12_RESOURCE_STATES state)61{62if (m_bAllSubresourcesSame && m_spSubresourceStates.size() > 1)63{64std::fill(m_spSubresourceStates.begin() + 1, m_spSubresourceStates.end(), m_spSubresourceStates[0]);65m_bAllSubresourcesSame = false;66}67if (m_spSubresourceStates.size() == 1)68{69SubresourceIndex = 0;70}71UpdateSubresourceState(SubresourceIndex, state);72}7374//----------------------------------------------------------------------------------------------------------------------------------75void CDesiredResourceState::Reset()76{77SetResourceState(UNKNOWN_RESOURCE_STATE);78}7980//----------------------------------------------------------------------------------------------------------------------------------81void CCurrentResourceState::ConvertToSubresourceTracking()82{83if (m_bAllSubresourcesSame && m_spLogicalState.size() > 1)84{85std::fill(m_spLogicalState.begin() + 1, m_spLogicalState.end(), m_spLogicalState[0]);86m_bAllSubresourcesSame = false;87}88}8990//----------------------------------------------------------------------------------------------------------------------------------91CCurrentResourceState::CCurrentResourceState(UINT SubresourceCount, bool bSimultaneousAccess)92: m_bSimultaneousAccess(bSimultaneousAccess)93, m_spLogicalState(SubresourceCount)94{95m_spLogicalState[0] = LogicalState{};96}9798//----------------------------------------------------------------------------------------------------------------------------------99D3D12_RESOURCE_STATES CCurrentResourceState::StateIfPromoted(D3D12_RESOURCE_STATES State, UINT SubresourceIndex)100{101D3D12_RESOURCE_STATES Result = D3D12_RESOURCE_STATE_COMMON;102103if (m_bSimultaneousAccess || !!(State & (104D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE |105D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE |106D3D12_RESOURCE_STATE_COPY_SOURCE |107D3D12_RESOURCE_STATE_COPY_DEST)))108{109auto CurState = GetLogicalSubresourceState(SubresourceIndex);110111// If the current state is COMMON...112if(CurState.State == D3D12_RESOURCE_STATE_COMMON)113{114// ...then promotion is allowed115Result = State;116}117// If the current state is a read state resulting from previous promotion...118else if(CurState.IsPromotedState && !!(CurState.State & D3D12_RESOURCE_STATE_GENERIC_READ))119{120// ...then (accumulated) promotion is allowed121Result = State |= CurState.State;122}123}124125return Result;126}127128129130//----------------------------------------------------------------------------------------------------------------------------------131void CCurrentResourceState::SetLogicalResourceState(LogicalState const& State)132{133m_bAllSubresourcesSame = true;134m_spLogicalState[0] = State;135}136137//----------------------------------------------------------------------------------------------------------------------------------138void CCurrentResourceState::SetLogicalSubresourceState(UINT SubresourceIndex, LogicalState const& State)139{140ConvertToSubresourceTracking();141m_spLogicalState[SubresourceIndex] = State;142}143144//----------------------------------------------------------------------------------------------------------------------------------145auto CCurrentResourceState::GetLogicalSubresourceState(UINT SubresourceIndex) const -> LogicalState const&146{147if (AreAllSubresourcesSame())148{149SubresourceIndex = 0;150}151return m_spLogicalState[SubresourceIndex];152}153154//----------------------------------------------------------------------------------------------------------------------------------155void CCurrentResourceState::Reset()156{157m_bAllSubresourcesSame = true;158m_spLogicalState[0] = LogicalState{};159}160161//----------------------------------------------------------------------------------------------------------------------------------162ResourceStateManager::ResourceStateManager()163{164list_inithead(&m_TransitionListHead);165// Reserve some space in these vectors upfront. Values are arbitrary.166m_vResourceBarriers.reserve(50);167}168169//----------------------------------------------------------------------------------------------------------------------------------170void ResourceStateManager::TransitionResource(TransitionableResourceState& Resource,171D3D12_RESOURCE_STATES state)172{173Resource.m_DesiredState.SetResourceState(state);174if (!Resource.IsTransitionPending())175{176list_add(&Resource.m_TransitionListEntry, &m_TransitionListHead);177}178}179180//----------------------------------------------------------------------------------------------------------------------------------181void ResourceStateManager::TransitionSubresource(TransitionableResourceState& Resource,182UINT SubresourceIndex,183D3D12_RESOURCE_STATES state)184{185Resource.m_DesiredState.SetSubresourceState(SubresourceIndex, state);186if (!Resource.IsTransitionPending())187{188list_add(&Resource.m_TransitionListEntry, &m_TransitionListHead);189}190}191192//----------------------------------------------------------------------------------------------------------------------------------193void ResourceStateManager::ApplyResourceTransitionsPreamble()194{195m_vResourceBarriers.clear();196}197198//----------------------------------------------------------------------------------------------------------------------------------199/*static*/ bool ResourceStateManager::TransitionRequired(D3D12_RESOURCE_STATES CurrentState, D3D12_RESOURCE_STATES& DestinationState)200{201// An exact match never needs a transition.202if (CurrentState == DestinationState)203{204return false;205}206207if (208CurrentState == D3D12_RESOURCE_STATE_COMMON ||209DestinationState == D3D12_RESOURCE_STATE_COMMON)210{211return true;212}213214// Current state already contains the destination state, we're good.215if ((CurrentState & DestinationState) == DestinationState)216{217DestinationState = CurrentState;218return false;219}220221// If the transition involves a write state, then the destination should just be the requested destination.222// Otherwise, accumulate read states to minimize future transitions (by triggering the above condition).223if (!IsD3D12WriteState(DestinationState) && !IsD3D12WriteState(CurrentState))224{225DestinationState |= CurrentState;226}227return true;228}229230//----------------------------------------------------------------------------------------------------------------------------------231void ResourceStateManager::AddCurrentStateUpdate(TransitionableResourceState& Resource,232CCurrentResourceState& CurrentState,233UINT SubresourceIndex,234const CCurrentResourceState::LogicalState &NewLogicalState)235{236if (SubresourceIndex == D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES)237{238CurrentState.SetLogicalResourceState(NewLogicalState);239}240else241{242CurrentState.SetLogicalSubresourceState(SubresourceIndex, NewLogicalState);243}244}245246//----------------------------------------------------------------------------------------------------------------------------------247void ResourceStateManager::ProcessTransitioningResource(ID3D12Resource* pTransitioningResource,248TransitionableResourceState& TransitionableResourceState,249CCurrentResourceState& CurrentState,250UINT NumTotalSubresources,251UINT64 ExecutionId)252{253// Figure out the set of subresources that are transitioning254auto& DestinationState = TransitionableResourceState.m_DesiredState;255bool bAllSubresourcesAtOnce = CurrentState.AreAllSubresourcesSame() && DestinationState.AreAllSubresourcesSame();256257D3D12_RESOURCE_BARRIER TransitionDesc;258memset(&TransitionDesc, 0, sizeof(TransitionDesc));259TransitionDesc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;260TransitionDesc.Transition.pResource = pTransitioningResource;261262UINT numSubresources = bAllSubresourcesAtOnce ? 1 : NumTotalSubresources;263for (UINT i = 0; i < numSubresources; ++i)264{265D3D12_RESOURCE_STATES SubresourceDestinationState = DestinationState.GetSubresourceState(i);266TransitionDesc.Transition.Subresource = bAllSubresourcesAtOnce ? D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES : i;267268// Is this subresource currently being used, or is it just being iterated over?269D3D12_RESOURCE_STATES after = DestinationState.GetSubresourceState(i);270if (after == UNKNOWN_RESOURCE_STATE)271{272// This subresource doesn't have any transition requested - move on to the next.273continue;274}275276ProcessTransitioningSubresourceExplicit(277CurrentState,278i,279SubresourceDestinationState,280after,281TransitionableResourceState,282TransitionDesc,283ExecutionId); // throw( bad_alloc )284}285286// Update destination states.287// Coalesce destination state to ensure that it's set for the entire resource.288DestinationState.SetResourceState(UNKNOWN_RESOURCE_STATE);289290}291292//----------------------------------------------------------------------------------------------------------------------------------293void ResourceStateManager::ProcessTransitioningSubresourceExplicit(294CCurrentResourceState& CurrentState,295UINT SubresourceIndex,296D3D12_RESOURCE_STATES state,297D3D12_RESOURCE_STATES after,298TransitionableResourceState& TransitionableResourceState,299D3D12_RESOURCE_BARRIER& TransitionDesc,300UINT64 ExecutionId)301{302// Simultaneous access resources currently in the COMMON303// state can be implicitly promoted to any state other state.304// Any non-simultaneous-access resources currently in the305// COMMON state can still be implicitly promoted to SRV,306// NON_PS_SRV, COPY_SRC, or COPY_DEST.307CCurrentResourceState::LogicalState CurrentLogicalState = CurrentState.GetLogicalSubresourceState(SubresourceIndex);308309// If the last time this logical state was set was in a different310// execution period and is decayable then decay the current state311// to COMMON312if(ExecutionId != CurrentLogicalState.ExecutionId && CurrentLogicalState.MayDecay)313{314CurrentLogicalState.State = D3D12_RESOURCE_STATE_COMMON;315CurrentLogicalState.IsPromotedState = false;316}317bool MayDecay = false;318bool IsPromotion = false;319320// If not promotable then StateIfPromoted will be D3D12_RESOURCE_STATE_COMMON321auto StateIfPromoted = CurrentState.StateIfPromoted(after, SubresourceIndex);322323if ( D3D12_RESOURCE_STATE_COMMON == StateIfPromoted )324{325if (TransitionRequired(CurrentLogicalState.State, /*inout*/ after))326{327// Insert a single concrete barrier (for non-simultaneous access resources).328TransitionDesc.Transition.StateBefore = D3D12_RESOURCE_STATES(CurrentLogicalState.State);329TransitionDesc.Transition.StateAfter = D3D12_RESOURCE_STATES(after);330assert(TransitionDesc.Transition.StateBefore != TransitionDesc.Transition.StateAfter);331m_vResourceBarriers.push_back(TransitionDesc); // throw( bad_alloc )332333MayDecay = CurrentState.SupportsSimultaneousAccess() && !IsD3D12WriteState(after);334IsPromotion = false;335}336}337else338{339// Handle identity state transition340if(after != StateIfPromoted)341{342after = StateIfPromoted;343MayDecay = !IsD3D12WriteState(after);344IsPromotion = true;345}346}347348CCurrentResourceState::LogicalState NewLogicalState{after, ExecutionId, IsPromotion, MayDecay};349AddCurrentStateUpdate(TransitionableResourceState,350CurrentState,351TransitionDesc.Transition.Subresource,352NewLogicalState);353}354355//----------------------------------------------------------------------------------------------------------------------------------356void ResourceStateManager::SubmitResourceTransitions(ID3D12GraphicsCommandList *pCommandList)357{358// Submit any pending barriers on source command lists that are not the destination.359if (!m_vResourceBarriers.empty())360{361pCommandList->ResourceBarrier((UINT)m_vResourceBarriers.size(), m_vResourceBarriers.data());362}363}364365//----------------------------------------------------------------------------------------------------------------------------------366void ResourceStateManager::TransitionResource(TransitionableResourceState* pResource, D3D12_RESOURCE_STATES State)367{368ResourceStateManager::TransitionResource(*pResource, State);369}370371//----------------------------------------------------------------------------------------------------------------------------------372void ResourceStateManager::TransitionSubresource(TransitionableResourceState* pResource, UINT SubresourceIndex, D3D12_RESOURCE_STATES State)373{374ResourceStateManager::TransitionSubresource(*pResource, SubresourceIndex, State);375}376377//----------------------------------------------------------------------------------------------------------------------------------378void ResourceStateManager::ApplyAllResourceTransitions(ID3D12GraphicsCommandList *pCommandList, UINT64 ExecutionId)379{380ApplyResourceTransitionsPreamble();381382ForEachTransitioningResource([=](TransitionableResourceState& ResourceBase)383{384TransitionableResourceState& CurResource = static_cast<TransitionableResourceState&>(ResourceBase);385386ID3D12Resource *pResource = CurResource.GetD3D12Resource();387388ProcessTransitioningResource(389pResource,390CurResource,391CurResource.GetCurrentState(),392CurResource.NumSubresources(),393ExecutionId);394});395396SubmitResourceTransitions(pCommandList);397}398399400