Mesa (main): d3d12: Swap the remainder of state tracking to new method

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jul 22 14:58:07 UTC 2022


Module: Mesa
Branch: main
Commit: c6f01d6c45fd82bc07a68ec3a90f0f56c05fde01
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=c6f01d6c45fd82bc07a68ec3a90f0f56c05fde01

Author: Jesse Natalie <jenatali at microsoft.com>
Date:   Wed Jul 20 20:25:29 2022 -0700

d3d12: Swap the remainder of state tracking to new method

Uses a set of d3d12_bo on the context to track which bos are pending
a transition instead of an intrusive linked list, since the bo may
need to be pending on multiple contexts at once.

Reviewed-by: Bill Kristiansen <billkris at microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17688>

---

 src/gallium/drivers/d3d12/D3D12ResourceState.cpp   | 282 ---------------------
 src/gallium/drivers/d3d12/D3D12ResourceState.h     | 201 ---------------
 src/gallium/drivers/d3d12/d3d12_bufmgr.cpp         |   8 +-
 src/gallium/drivers/d3d12/d3d12_bufmgr.h           |   2 -
 src/gallium/drivers/d3d12/d3d12_context.cpp        |  56 +---
 src/gallium/drivers/d3d12/d3d12_context.h          |   4 +
 src/gallium/drivers/d3d12/d3d12_resource.h         |   9 -
 src/gallium/drivers/d3d12/d3d12_resource_state.cpp | 141 +++++++++++
 src/gallium/drivers/d3d12/meson.build              |   1 -
 9 files changed, 147 insertions(+), 557 deletions(-)

diff --git a/src/gallium/drivers/d3d12/D3D12ResourceState.cpp b/src/gallium/drivers/d3d12/D3D12ResourceState.cpp
deleted file mode 100644
index ad777507294..00000000000
--- a/src/gallium/drivers/d3d12/D3D12ResourceState.cpp
+++ /dev/null
@@ -1,282 +0,0 @@
-/*
- * Copyright © Microsoft Corporation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * 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.  IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
-
-#include "D3D12ResourceState.h"
-#define UNKNOWN_RESOURCE_STATE (D3D12_RESOURCE_STATES) 0x8000u
-
-//----------------------------------------------------------------------------------------------------------------------------------
-ResourceStateManager::ResourceStateManager()
-{
-   list_inithead(&m_TransitionListHead);
-   // Reserve some space in these vectors upfront. Values are arbitrary.
-   m_vResourceBarriers.reserve(50);
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::TransitionResource(TransitionableResourceState& Resource,
-                                              D3D12_RESOURCE_STATES state)
-{
-   d3d12_set_desired_resource_state(&Resource.m_DesiredState, state);
-   if (!Resource.IsTransitionPending())
-   {
-      list_add(&Resource.m_TransitionListEntry, &m_TransitionListHead);
-   }
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::TransitionSubresource(TransitionableResourceState& Resource,
-                                                 UINT SubresourceIndex,
-                                                 D3D12_RESOURCE_STATES state)
-{
-   d3d12_set_desired_subresource_state(&Resource.m_DesiredState, SubresourceIndex, state);
-   if (!Resource.IsTransitionPending())
-   {
-      list_add(&Resource.m_TransitionListEntry, &m_TransitionListHead);
-   }
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::ApplyResourceTransitionsPreamble(bool IsImplicitDispatch)
-{
-   m_IsImplicitDispatch = IsImplicitDispatch;
-   m_vResourceBarriers.clear();
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-/*static*/ bool ResourceStateManager::TransitionRequired(D3D12_RESOURCE_STATES CurrentState, D3D12_RESOURCE_STATES& DestinationState)
-{
-   // An exact match never needs a transition.
-   if (CurrentState == DestinationState)
-   {
-      return false;
-   }
-
-   if (
-      CurrentState == D3D12_RESOURCE_STATE_COMMON ||
-      DestinationState == D3D12_RESOURCE_STATE_COMMON)
-   {
-      return true;
-   }
-
-   // Current state already contains the destination state, we're good.
-   if ((CurrentState & DestinationState) == DestinationState)
-   {
-      DestinationState = CurrentState;
-      return false;
-   }
-
-   // If the transition involves a write state, then the destination should just be the requested destination.
-   // Otherwise, accumulate read states to minimize future transitions (by triggering the above condition).
-   if (!d3d12_is_write_state(DestinationState) && !d3d12_is_write_state(CurrentState))
-   {
-      DestinationState |= CurrentState;
-   }
-   return true;
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::AddCurrentStateUpdate(TransitionableResourceState& Resource,
-                                                 d3d12_resource_state *CurrentState,
-                                                 UINT SubresourceIndex,
-                                                 const d3d12_subresource_state *NewLogicalState)
-{
-   if (SubresourceIndex == D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES)
-   {
-      d3d12_set_resource_state(CurrentState, NewLogicalState);
-   }
-   else
-   {
-      d3d12_set_subresource_state(CurrentState, SubresourceIndex, NewLogicalState);
-   }
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::ProcessTransitioningResource(ID3D12Resource* pTransitioningResource,
-                                                            TransitionableResourceState& TransitionableResourceState,
-                                                            d3d12_resource_state *CurrentState,
-                                                            UINT NumTotalSubresources,
-                                                            UINT64 ExecutionId)
-{
-   // Figure out the set of subresources that are transitioning
-   auto& DestinationState = TransitionableResourceState.m_DesiredState;
-   bool bAllSubresourcesAtOnce = CurrentState->homogenous && DestinationState.homogenous;
-
-   D3D12_RESOURCE_BARRIER TransitionDesc;
-   memset(&TransitionDesc, 0, sizeof(TransitionDesc));
-   TransitionDesc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
-   TransitionDesc.Transition.pResource = pTransitioningResource;
-
-   UINT numSubresources = bAllSubresourcesAtOnce ? 1 : NumTotalSubresources;
-   for (UINT i = 0; i < numSubresources; ++i)
-   {
-      D3D12_RESOURCE_STATES after = d3d12_get_desired_subresource_state(&DestinationState, i);
-      TransitionDesc.Transition.Subresource = bAllSubresourcesAtOnce ? D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES : i;
-
-      // Is this subresource currently being used, or is it just being iterated over?
-      if (after == UNKNOWN_RESOURCE_STATE)
-      {
-            // This subresource doesn't have any transition requested - move on to the next.
-            continue;
-      }
-
-      // This is a transition into a state that is both write and non-write.
-      // This is invalid according to D3D12. We're venturing into undefined behavior
-      // land, but let's just pick the write state.
-      if (d3d12_is_write_state(after) &&
-         (after & ~RESOURCE_STATE_ALL_WRITE_BITS) != 0)
-      {
-         after &= RESOURCE_STATE_ALL_WRITE_BITS;
-
-         // For now, this is the only way I've seen where this can happen.
-         assert(after == D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
-      }
-
-      ProcessTransitioningSubresourceExplicit(
-         CurrentState,
-         i,
-         after,
-         TransitionableResourceState,
-         TransitionDesc,
-         ExecutionId); // throw( bad_alloc )
-   }
-
-   // Update destination states.
-   // Coalesce destination state to ensure that it's set for the entire resource.
-   d3d12_reset_desired_resource_state(&DestinationState);
-
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::ProcessTransitioningSubresourceExplicit(
-   d3d12_resource_state *CurrentState,
-   UINT SubresourceIndex,
-   D3D12_RESOURCE_STATES after,
-   TransitionableResourceState& TransitionableResourceState,
-   D3D12_RESOURCE_BARRIER& TransitionDesc,
-   UINT64 ExecutionId)
-{
-   // Simultaneous access resources currently in the COMMON
-   // state can be implicitly promoted to any state other state.
-   // Any non-simultaneous-access resources currently in the
-   // COMMON state can still be implicitly  promoted to SRV,
-   // NON_PS_SRV, COPY_SRC, or COPY_DEST.
-   d3d12_subresource_state CurrentLogicalState = *d3d12_get_subresource_state(CurrentState, SubresourceIndex);
-
-   // If the last time this logical state was set was in a different
-   // execution period and is decayable then decay the current state
-   // to COMMON
-   if(ExecutionId != CurrentLogicalState.execution_id && CurrentLogicalState.may_decay)
-   {
-      CurrentLogicalState.state = D3D12_RESOURCE_STATE_COMMON;
-      CurrentLogicalState.is_promoted = false;
-   }
-   bool MayDecay = false;
-   bool IsPromotion = false;
-
-   // If not promotable then StateIfPromoted will be D3D12_RESOURCE_STATE_COMMON
-   auto StateIfPromoted =
-      d3d12_resource_state_if_promoted(after, CurrentState->supports_simultaneous_access, &CurrentLogicalState);
-
-   if (D3D12_RESOURCE_STATE_COMMON == StateIfPromoted)
-   {
-      if (CurrentLogicalState.state == D3D12_RESOURCE_STATE_UNORDERED_ACCESS &&
-          after == D3D12_RESOURCE_STATE_UNORDERED_ACCESS &&
-          m_IsImplicitDispatch)
-      {
-         D3D12_RESOURCE_BARRIER UAVBarrier = { D3D12_RESOURCE_BARRIER_TYPE_UAV };
-         UAVBarrier.UAV.pResource = TransitionDesc.Transition.pResource;
-         m_vResourceBarriers.push_back(UAVBarrier);
-      }
-      else if (TransitionRequired(CurrentLogicalState.state, /*inout*/ after))
-      {
-         // Insert a single concrete barrier (for non-simultaneous access resources).
-         TransitionDesc.Transition.StateBefore = D3D12_RESOURCE_STATES(CurrentLogicalState.state);
-         TransitionDesc.Transition.StateAfter = D3D12_RESOURCE_STATES(after);
-         assert(TransitionDesc.Transition.StateBefore != TransitionDesc.Transition.StateAfter);
-         m_vResourceBarriers.push_back(TransitionDesc); // throw( bad_alloc )
-
-         MayDecay = CurrentState->supports_simultaneous_access && !d3d12_is_write_state(after);
-         IsPromotion = false;
-      }
-   }
-   else
-   {
-      // Handle identity state transition
-      if(after != StateIfPromoted)
-      {
-         after = StateIfPromoted;
-         MayDecay = !d3d12_is_write_state(after);
-         IsPromotion = true;
-      }
-   }
-
-   d3d12_subresource_state NewLogicalState{after, ExecutionId, IsPromotion, MayDecay};
-   AddCurrentStateUpdate(TransitionableResourceState,
-                         CurrentState,
-                         TransitionDesc.Transition.Subresource,
-                         &NewLogicalState);
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::SubmitResourceTransitions(ID3D12GraphicsCommandList *pCommandList)
-{
-   // Submit any pending barriers on source command lists that are not the destination.
-   if (!m_vResourceBarriers.empty())
-   {
-      pCommandList->ResourceBarrier((UINT)m_vResourceBarriers.size(), m_vResourceBarriers.data());
-   }
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::TransitionResource(TransitionableResourceState* pResource, D3D12_RESOURCE_STATES State)
-{
-   ResourceStateManager::TransitionResource(*pResource, State);
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::TransitionSubresource(TransitionableResourceState* pResource, UINT SubresourceIndex, D3D12_RESOURCE_STATES State)
-{
-   ResourceStateManager::TransitionSubresource(*pResource, SubresourceIndex, State);
-}
-
-//----------------------------------------------------------------------------------------------------------------------------------
-void ResourceStateManager::ApplyAllResourceTransitions(ID3D12GraphicsCommandList *pCommandList, UINT64 ExecutionId, bool IsImplicitDispatch)
-{
-   ApplyResourceTransitionsPreamble(IsImplicitDispatch);
-
-   ForEachTransitioningResource([=](TransitionableResourceState& ResourceBase)
-   {
-       TransitionableResourceState& CurResource = static_cast<TransitionableResourceState&>(ResourceBase);
-
-       ID3D12Resource *pResource = CurResource.GetD3D12Resource();
-
-       ProcessTransitioningResource(
-           pResource,
-           CurResource,
-           &CurResource.m_currentState,
-           CurResource.NumSubresources(),
-           ExecutionId);
-   });
-
-   SubmitResourceTransitions(pCommandList);
-}
diff --git a/src/gallium/drivers/d3d12/D3D12ResourceState.h b/src/gallium/drivers/d3d12/D3D12ResourceState.h
deleted file mode 100644
index 83ad793013d..00000000000
--- a/src/gallium/drivers/d3d12/D3D12ResourceState.h
+++ /dev/null
@@ -1,201 +0,0 @@
-/*
- * Copyright © Microsoft Corporation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * 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.  IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
-
-#ifndef D3D12RESOURCESTATE_H
-#define D3D12RESOURCESTATE_H
-
-#include <vector>
-#include <assert.h>
-
-#include "util/list.h"
-
-#include "d3d12_common.h"
-#include "d3d12_resource_state.h"
-
-#if defined(__GNUC__)
-#pragma GCC diagnostic ignored "-Winvalid-offsetof"
-#endif
-
-#define RESOURCE_STATE_VALID_BITS 0x2f3fff
-#define RESOURCE_STATE_VALID_INTERNAL_BITS 0x2fffff
-
-//---------------------------------------------------------------------------------------------------------------------------------
-    
-//==================================================================================================================================
-// TransitionableResourceState
-// A base class that transitionable resources should inherit from.
-//==================================================================================================================================
-struct TransitionableResourceState
-{
-   struct list_head m_TransitionListEntry;
-   struct d3d12_desired_resource_state m_DesiredState;
-   struct d3d12_resource_state m_currentState;
-
-   TransitionableResourceState(ID3D12Resource *pResource, UINT TotalSubresources, bool SupportsSimultaneousAccess) :
-      m_TotalSubresources(TotalSubresources),
-      m_pResource(pResource)
-   {
-      list_inithead(&m_TransitionListEntry);
-      d3d12_desired_resource_state_init(&m_DesiredState, TotalSubresources);
-      d3d12_resource_state_init(&m_currentState, TotalSubresources, SupportsSimultaneousAccess);
-   }
-
-   ~TransitionableResourceState()
-   {
-      d3d12_desired_resource_state_cleanup(&m_DesiredState);
-      if (IsTransitionPending())
-      {
-         list_del(&m_TransitionListEntry);
-      }
-   }
-
-   bool IsTransitionPending() const { return !list_is_empty(&m_TransitionListEntry); }
-
-   UINT NumSubresources() { return m_TotalSubresources; }
-
-   inline ID3D12Resource* GetD3D12Resource() const { return m_pResource; }
-
-private:
-   unsigned m_TotalSubresources;
-
-   ID3D12Resource* m_pResource;
-};
-
-//==================================================================================================================================
-// ResourceStateManager
-// The main business logic for handling resource transitions, including multi-queue sync and shared/exclusive state changes.
-//
-// Requesting a resource to transition simply updates destination state, and ensures it's in a list to be processed later.
-//
-// When processing ApplyAllResourceTransitions, we build up sets of vectors.
-// There's a source one for each command list type, and a single one for the dest because we are applying
-// the resource transitions for a single operation.
-// There's also a vector for "tentative" barriers, which are merged into the destination vector if
-// no flushing occurs as a result of submitting the final barrier operation.
-// 99% of the time, there will only be the source being populated, but sometimes there will be a destination as well.
-// If the source and dest of a transition require different types, we put a (source->COMMON) in the approriate source vector,
-// and a (COMMON->dest) in the destination vector.
-//
-// Once all resources are processed, we:
-// 1. Submit all source barriers, except ones belonging to the destination queue.
-// 2. Flush all source command lists, except ones belonging to the destination queue.
-// 3. Determine if the destination queue is going to be flushed.
-//    If so: Submit source barriers on that command list first, then flush it.
-//    If not: Accumulate source, dest, and tentative barriers so they can be sent to D3D12 in a single API call.
-// 4. Insert waits on the destination queue - deferred waits, and waits for work on other queues.
-// 5. Insert destination barriers.
-//
-// Only once all of this has been done do we update the "current" state of resources,
-// because this is the only way that we know whether or not the destination queue has been flushed,
-// and therefore, we can get the correct fence values to store in the subresources.
-//==================================================================================================================================
-class ResourceStateManager
-{
-protected:
-
-   struct list_head m_TransitionListHead;
-
-   std::vector<D3D12_RESOURCE_BARRIER> m_vResourceBarriers;
-   bool m_IsImplicitDispatch;
-
-public:
-   ResourceStateManager();
-
-   ~ResourceStateManager()
-   {
-      // All resources should be gone by this point, and each resource ensures it is no longer in this list.
-      assert(list_is_empty(&m_TransitionListHead));
-   }
-
-   // Call the D3D12 APIs to perform the resource barriers, command list submission, and command queue sync
-   // that was determined by previous calls to ProcessTransitioningResource.
-   void SubmitResourceTransitions(ID3D12GraphicsCommandList *pCommandList);
-
-   // Transition the entire resource to a particular destination state on a particular command list.
-   void TransitionResource(TransitionableResourceState* pResource,
-                           D3D12_RESOURCE_STATES State);
-   // Transition a single subresource to a particular destination state.
-   void TransitionSubresource(TransitionableResourceState* pResource,
-                              UINT SubresourceIndex,
-                              D3D12_RESOURCE_STATES State);
-
-   // Submit all barriers and queue sync.
-   void ApplyAllResourceTransitions(ID3D12GraphicsCommandList *pCommandList, UINT64 ExecutionId, bool IsImplicitDispatch);
-
-private:
-   // These methods set the destination state of the resource/subresources and ensure it's in the transition list.
-   void TransitionResource(TransitionableResourceState& Resource,
-                           D3D12_RESOURCE_STATES State);
-   void TransitionSubresource(TransitionableResourceState& Resource,
-                              UINT SubresourceIndex,
-                              D3D12_RESOURCE_STATES State);
-
-   // Clear out any state from previous iterations.
-   void ApplyResourceTransitionsPreamble(bool IsImplicitDispatch);
-
-   // What to do with the resource, in the context of the transition list, after processing it.
-   enum class TransitionResult
-   {
-      // There are no more pending transitions that may be processed at a later time (i.e. draw time),
-      // so remove it from the pending transition list.
-      Remove,
-      // There are more transitions to be done, so keep it in the list.
-      Keep
-   };
-
-   // For every entry in the transition list, call a routine.
-   // This routine must return a TransitionResult which indicates what to do with the list.
-   template <typename TFunc>
-   void ForEachTransitioningResource(TFunc&& func)
-   {
-      list_for_each_entry_safe(TransitionableResourceState, pResource, &m_TransitionListHead, m_TransitionListEntry)
-      {
-            func(*pResource);
-            list_delinit(&pResource->m_TransitionListEntry);
-      }
-   }
-
-   // Updates vectors with the operations that should be applied to the requested resource.
-   // May update the destination state of the resource.
-   void ProcessTransitioningResource(ID3D12Resource* pTransitioningResource,
-                                     TransitionableResourceState& TransitionableResourceState,
-                                     d3d12_resource_state *CurrentState,
-                                     UINT NumTotalSubresources,
-                                     UINT64 ExecutionId);
-
-private:
-   // Helpers
-   static bool TransitionRequired(D3D12_RESOURCE_STATES CurrentState, D3D12_RESOURCE_STATES& DestinationState);
-   void AddCurrentStateUpdate(TransitionableResourceState& Resource,
-                              d3d12_resource_state *CurrentState,
-                              UINT SubresourceIndex,
-                              const d3d12_subresource_state *NewLogicalState);
-   void ProcessTransitioningSubresourceExplicit(d3d12_resource_state *CurrentState,
-                                                UINT i,
-                                                D3D12_RESOURCE_STATES after,
-                                                TransitionableResourceState& TransitionableResourceState,
-                                                D3D12_RESOURCE_BARRIER& TransitionDesc,
-                                                UINT64 ExecutionId);
-};
-
-#endif // D3D12RESOURCESTATEH
diff --git a/src/gallium/drivers/d3d12/d3d12_bufmgr.cpp b/src/gallium/drivers/d3d12/d3d12_bufmgr.cpp
index 2f5110e37d1..8d009305474 100644
--- a/src/gallium/drivers/d3d12/d3d12_bufmgr.cpp
+++ b/src/gallium/drivers/d3d12/d3d12_bufmgr.cpp
@@ -26,8 +26,6 @@
 #include "d3d12_format.h"
 #include "d3d12_screen.h"
 
-#include "D3D12ResourceState.h"
-
 #include "pipebuffer/pb_buffer.h"
 #include "pipebuffer/pb_bufmgr.h"
 
@@ -95,14 +93,13 @@ d3d12_bo_wrap_res(struct d3d12_screen *screen, ID3D12Resource *res, enum d3d12_r
    pipe_reference_init(&bo->reference, 1);
    bo->screen = screen;
    bo->res = res;
-   bo->trans_state = new TransitionableResourceState(res, total_subresources, supports_simultaneous_access);
    bo->unique_id = p_atomic_inc_return(&screen->resource_id_generator);
    if (!supports_simultaneous_access)
       d3d12_resource_state_init(&bo->global_state, total_subresources, false);
 
    bo->residency_status = residency;
    bo->last_used_timestamp = 0;
-   screen->dev->GetCopyableFootprints(&desc, 0, bo->trans_state->NumSubresources(), 0, nullptr, nullptr, nullptr, &bo->estimated_size);
+   screen->dev->GetCopyableFootprints(&desc, 0, total_subresources, 0, nullptr, nullptr, nullptr, &bo->estimated_size);
    if (residency != d3d12_evicted) {
       mtx_lock(&screen->submit_mutex);
       list_add(&bo->residency_list_entry, &screen->residency_list);
@@ -168,7 +165,6 @@ d3d12_bo_wrap_buffer(struct d3d12_screen *screen, struct pb_buffer *buf)
    pipe_reference_init(&bo->reference, 1);
    bo->screen = screen;
    bo->buffer = buf;
-   bo->trans_state = NULL; /* State from base BO will be used */
    bo->unique_id = p_atomic_inc_return(&screen->resource_id_generator);
    bo->residency_status = d3d12_evicted;
 
@@ -202,8 +198,6 @@ d3d12_bo_unreference(struct d3d12_bo *bo)
       mtx_unlock(&bo->screen->submit_mutex);
 
       d3d12_resource_state_cleanup(&bo->global_state);
-      if (bo->trans_state)
-         delete bo->trans_state;
       if (bo->res)
          bo->res->Release();
       FREE(bo);
diff --git a/src/gallium/drivers/d3d12/d3d12_bufmgr.h b/src/gallium/drivers/d3d12/d3d12_bufmgr.h
index a84df2cd255..d34076f96e6 100644
--- a/src/gallium/drivers/d3d12/d3d12_bufmgr.h
+++ b/src/gallium/drivers/d3d12/d3d12_bufmgr.h
@@ -34,7 +34,6 @@
 struct d3d12_bufmgr;
 struct d3d12_screen;
 struct pb_manager;
-struct TransitionableResourceState;
 
 enum d3d12_residency_status {
    d3d12_evicted,
@@ -47,7 +46,6 @@ struct d3d12_bo {
    struct d3d12_screen *screen;
    ID3D12Resource *res;
    struct pb_buffer *buffer;
-   struct TransitionableResourceState *trans_state;
    struct d3d12_resource_state global_state;
 
    /* Used as a key in per-context resource state maps,
diff --git a/src/gallium/drivers/d3d12/d3d12_context.cpp b/src/gallium/drivers/d3d12/d3d12_context.cpp
index 4bdd7fe1f01..e36c1bcc84f 100644
--- a/src/gallium/drivers/d3d12/d3d12_context.cpp
+++ b/src/gallium/drivers/d3d12/d3d12_context.cpp
@@ -52,8 +52,6 @@
 #include "util/u_dl.h"
 #include "nir_to_dxil.h"
 
-#include "D3D12ResourceState.h"
-
 #include <dxguids/dxguids.h>
 
 extern "C" {
@@ -113,8 +111,6 @@ d3d12_context_destroy(struct pipe_context *pctx)
    if (pctx->const_uploader)
       u_upload_destroy(pctx->const_uploader);
 
-   delete ctx->resource_state_manager;
-
    FREE(ctx);
 }
 
@@ -1760,7 +1756,7 @@ d3d12_set_shader_images(struct pipe_context *pctx,
    ctx->shader_dirty[shader] |= D3D12_SHADER_DIRTY_IMAGE;
 }
 
-static void
+void
 d3d12_invalidate_context_bindings(struct d3d12_context *ctx, struct d3d12_resource *res) {
    // For each shader type, if the resource is currently bound as CBV, SRV, or UAV
    // set the context shader_dirty bit.
@@ -1968,54 +1964,6 @@ d3d12_flush_cmdlist_and_wait(struct d3d12_context *ctx)
    d3d12_reset_batch(ctx, batch, PIPE_TIMEOUT_INFINITE);
 }
 
-void
-d3d12_transition_resource_state(struct d3d12_context *ctx,
-                                struct d3d12_resource *res,
-                                D3D12_RESOURCE_STATES state,
-                                d3d12_bind_invalidate_option bind_invalidate)
-{
-   TransitionableResourceState *xres = d3d12_transitionable_resource_state(res);
-   
-   if (bind_invalidate == D3D12_BIND_INVALIDATE_FULL)
-      d3d12_invalidate_context_bindings(ctx, res);
-
-   ctx->resource_state_manager->TransitionResource(xres, state);
-}
-
-void
-d3d12_transition_subresources_state(struct d3d12_context *ctx,
-                                    struct d3d12_resource *res,
-                                    uint32_t start_level, uint32_t num_levels,
-                                    uint32_t start_layer, uint32_t num_layers,
-                                    uint32_t start_plane, uint32_t num_planes,
-                                    D3D12_RESOURCE_STATES state,
-                                    d3d12_bind_invalidate_option bind_invalidate)
-{
-   TransitionableResourceState *xres = d3d12_transitionable_resource_state(res);
-
-   if(bind_invalidate == D3D12_BIND_INVALIDATE_FULL)
-      d3d12_invalidate_context_bindings(ctx, res);
-
-   for (uint32_t l = 0; l < num_levels; l++) {
-      const uint32_t level = start_level + l;
-      for (uint32_t a = 0; a < num_layers; a++) {
-         const uint32_t layer = start_layer + a;
-         for( uint32_t p = 0; p < num_planes; p++) {
-            const uint32_t plane = start_plane + p;
-            uint32_t subres_id = level + (layer * res->mip_levels) + plane * (res->mip_levels * res->base.b.array_size);
-            assert(subres_id < xres->NumSubresources());
-            ctx->resource_state_manager->TransitionSubresource(xres, subres_id, state);
-         }
-      }
-   }
-}
-
-void
-d3d12_apply_resource_states(struct d3d12_context *ctx, bool is_implicit_dispatch)
-{
-   ctx->resource_state_manager->ApplyAllResourceTransitions(ctx->cmdlist, ctx->submit_id, is_implicit_dispatch);
-}
-
 static void
 d3d12_clear_render_target(struct pipe_context *pctx,
                           struct pipe_surface *psurf,
@@ -2631,8 +2579,6 @@ d3d12_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
    if (!ctx->blitter)
       return NULL;
 
-   ctx->resource_state_manager = new ResourceStateManager();
-
    if (!d3d12_init_polygon_stipple(&ctx->base)) {
       debug_printf("D3D12: failed to initialize polygon stipple resources\n");
       FREE(ctx);
diff --git a/src/gallium/drivers/d3d12/d3d12_context.h b/src/gallium/drivers/d3d12/d3d12_context.h
index 84465ff2932..2a2643458f1 100644
--- a/src/gallium/drivers/d3d12/d3d12_context.h
+++ b/src/gallium/drivers/d3d12/d3d12_context.h
@@ -182,6 +182,7 @@ struct d3d12_context {
 
    struct util_dynarray recently_destroyed_bos;
    struct util_dynarray barrier_scratch;
+   struct set *pending_barriers_bos;
 
    struct pipe_constant_buffer cbufs[PIPE_SHADER_TYPES][PIPE_MAX_CONSTANT_BUFFERS];
    struct pipe_framebuffer_state fb;
@@ -359,6 +360,9 @@ d3d12_need_zero_one_depth_range(struct d3d12_context *ctx);
 void
 d3d12_init_sampler_view_descriptor(struct d3d12_sampler_view *sampler_view);
 
+void
+d3d12_invalidate_context_bindings(struct d3d12_context *ctx, struct d3d12_resource *res);
+
 #ifdef HAVE_GALLIUM_D3D12_VIDEO
 struct pipe_video_codec* d3d12_video_create_codec( struct pipe_context *context,
                                                 const struct pipe_video_codec *t);
diff --git a/src/gallium/drivers/d3d12/d3d12_resource.h b/src/gallium/drivers/d3d12/d3d12_resource.h
index b9abe42ea7e..956eb75e6ea 100644
--- a/src/gallium/drivers/d3d12/d3d12_resource.h
+++ b/src/gallium/drivers/d3d12/d3d12_resource.h
@@ -101,15 +101,6 @@ d3d12_resource_resource(struct d3d12_resource *res)
    return ret;
 }
 
-static inline struct TransitionableResourceState *
-d3d12_transitionable_resource_state(struct d3d12_resource *res)
-{
-   uint64_t offset;
-   if (!res->bo)
-      return NULL;
-   return d3d12_bo_get_base(res->bo, &offset)->trans_state;
-}
-
 static inline D3D12_GPU_VIRTUAL_ADDRESS
 d3d12_resource_gpu_virtual_address(struct d3d12_resource *res)
 {
diff --git a/src/gallium/drivers/d3d12/d3d12_resource_state.cpp b/src/gallium/drivers/d3d12/d3d12_resource_state.cpp
index 091025723c2..ad19c7f8382 100644
--- a/src/gallium/drivers/d3d12/d3d12_resource_state.cpp
+++ b/src/gallium/drivers/d3d12/d3d12_resource_state.cpp
@@ -24,6 +24,7 @@
 #include "d3d12_bufmgr.h"
 #include "d3d12_context.h"
 #include "d3d12_format.h"
+#include "d3d12_resource.h"
 #include "d3d12_resource_state.h"
 #include "d3d12_screen.h"
 
@@ -205,6 +206,7 @@ void
 d3d12_context_state_table_init(struct d3d12_context *ctx)
 {
    ctx->bo_state_table = _mesa_hash_table_u64_create(nullptr);
+   ctx->pending_barriers_bos = _mesa_pointer_set_create(nullptr);
 }
 
 void
@@ -216,6 +218,7 @@ d3d12_context_state_table_destroy(struct d3d12_context *ctx)
    util_dynarray_fini(&ctx->barrier_scratch);
    if (ctx->state_fixup_cmdlist)
       ctx->state_fixup_cmdlist->Release();
+   _mesa_set_destroy(ctx->pending_barriers_bos, nullptr);
 }
 
 static unsigned
@@ -367,3 +370,141 @@ d3d12_context_state_resolve_submission(struct d3d12_context *ctx, struct d3d12_b
    }
    return needs_execute_fixup;
 }
+
+void
+d3d12_transition_resource_state(struct d3d12_context *ctx,
+                                struct d3d12_resource *res,
+                                D3D12_RESOURCE_STATES state,
+                                d3d12_bind_invalidate_option bind_invalidate)
+{
+   if (bind_invalidate == D3D12_BIND_INVALIDATE_FULL)
+      d3d12_invalidate_context_bindings(ctx, res);
+
+   d3d12_context_state_table_entry *state_entry = find_or_create_state_entry(ctx->bo_state_table, res->bo);
+   d3d12_set_desired_resource_state(&state_entry->desired, state);
+   _mesa_set_add(ctx->pending_barriers_bos, res->bo);
+}
+
+void
+d3d12_transition_subresources_state(struct d3d12_context *ctx,
+                                    struct d3d12_resource *res,
+                                    uint32_t start_level, uint32_t num_levels,
+                                    uint32_t start_layer, uint32_t num_layers,
+                                    uint32_t start_plane, uint32_t num_planes,
+                                    D3D12_RESOURCE_STATES state,
+                                    d3d12_bind_invalidate_option bind_invalidate)
+{
+   if(bind_invalidate == D3D12_BIND_INVALIDATE_FULL)
+      d3d12_invalidate_context_bindings(ctx, res);
+
+   d3d12_context_state_table_entry *state_entry = find_or_create_state_entry(ctx->bo_state_table, res->bo);
+   for (uint32_t l = 0; l < num_levels; l++) {
+      const uint32_t level = start_level + l;
+      for (uint32_t a = 0; a < num_layers; a++) {
+         const uint32_t layer = start_layer + a;
+         for( uint32_t p = 0; p < num_planes; p++) {
+            const uint32_t plane = start_plane + p;
+            uint32_t subres_id = level + (layer * res->mip_levels) + plane * (res->mip_levels * res->base.b.array_size);
+            assert(subres_id < state_entry->desired.num_subresources);
+            d3d12_set_desired_subresource_state(&state_entry->desired, subres_id, state);
+         }
+      }
+   }
+
+   _mesa_set_add(ctx->pending_barriers_bos, res->bo);
+}
+
+void
+d3d12_apply_resource_states(struct d3d12_context *ctx, bool is_implicit_dispatch)
+{
+   set_foreach_remove(ctx->pending_barriers_bos, entry) {
+      d3d12_bo *bo = (d3d12_bo *)entry->key;
+      uint64_t offset;
+      ID3D12Resource *res = d3d12_bo_get_base(bo, &offset)->res;
+
+      d3d12_context_state_table_entry *state_entry = find_or_create_state_entry(ctx->bo_state_table, bo);
+      d3d12_desired_resource_state *destination_state = &state_entry->desired;
+      d3d12_resource_state *current_state = &state_entry->batch_end;
+
+      // Figure out the set of subresources that are transitioning
+      bool all_resources_at_once = current_state->homogenous && destination_state->homogenous;
+
+      D3D12_RESOURCE_BARRIER transition_desc = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION };
+      transition_desc.Transition.pResource = res;
+
+      UINT num_subresources = all_resources_at_once ? 1 : current_state->num_subresources;
+      for (UINT i = 0; i < num_subresources; ++i) {
+         D3D12_RESOURCE_STATES after = d3d12_get_desired_subresource_state(destination_state, i);
+         transition_desc.Transition.Subresource = num_subresources == 1 ? D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES : i;
+
+         // Is this subresource currently being used, or is it just being iterated over?
+         if (after == UNKNOWN_RESOURCE_STATE) {
+            // This subresource doesn't have any transition requested - move on to the next.
+            continue;
+         }
+
+         // This is a transition into a state that is both write and non-write.
+         // This is invalid according to D3D12. We're venturing into undefined behavior
+         // land, but let's just pick the write state.
+         if (d3d12_is_write_state(after) && (after & ~RESOURCE_STATE_ALL_WRITE_BITS) != 0) {
+            after &= RESOURCE_STATE_ALL_WRITE_BITS;
+
+            // For now, this is the only way I've seen where this can happen.
+            assert(after == D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
+         }
+
+         d3d12_subresource_state current_subresource_state = *d3d12_get_subresource_state(current_state, i);
+
+         // If the last time this state was set was in a different execution
+         // period and is decayable then decay the current state to COMMON
+         if (ctx->submit_id != current_subresource_state.execution_id && current_subresource_state.may_decay) {
+            current_subresource_state.state = D3D12_RESOURCE_STATE_COMMON;
+            current_subresource_state.is_promoted = false;
+         }
+         bool may_decay = false;
+         bool is_promotion = false;
+
+         D3D12_RESOURCE_STATES state_if_promoted =
+            d3d12_resource_state_if_promoted(after, current_state->supports_simultaneous_access, &current_subresource_state);
+
+         if (D3D12_RESOURCE_STATE_COMMON == state_if_promoted) {
+            // No promotion
+            if (current_subresource_state.state == D3D12_RESOURCE_STATE_UNORDERED_ACCESS &&
+                after == D3D12_RESOURCE_STATE_UNORDERED_ACCESS &&
+                is_implicit_dispatch) {
+               D3D12_RESOURCE_BARRIER uav_barrier = { D3D12_RESOURCE_BARRIER_TYPE_UAV };
+               uav_barrier.UAV.pResource = res;
+               util_dynarray_append(&ctx->barrier_scratch, D3D12_RESOURCE_BARRIER, uav_barrier);
+            } else if (transition_required(current_subresource_state.state, /*inout*/ &after)) {
+               // Insert a single concrete barrier (for non-simultaneous access resources).
+               transition_desc.Transition.StateBefore = current_subresource_state.state;
+               transition_desc.Transition.StateAfter = after;
+               assert(transition_desc.Transition.StateBefore != transition_desc.Transition.StateAfter);
+               util_dynarray_append(&ctx->barrier_scratch, D3D12_RESOURCE_BARRIER, transition_desc);
+
+               may_decay = current_state->supports_simultaneous_access && !d3d12_is_write_state(after);
+               is_promotion = false;
+            }
+         } else if (after != state_if_promoted) {
+            after = state_if_promoted;
+            may_decay = !d3d12_is_write_state(after);
+            is_promotion = true;
+         }
+
+         d3d12_subresource_state new_subresource_state { after, ctx->submit_id, is_promotion, may_decay };
+         if (num_subresources == 1)
+            d3d12_set_resource_state(current_state, &new_subresource_state);
+         else
+            d3d12_set_subresource_state(current_state, i, &new_subresource_state);
+      }
+
+      // Update destination states.
+      d3d12_reset_desired_resource_state(destination_state);
+   }
+
+   if (ctx->barrier_scratch.size) {
+      ctx->cmdlist->ResourceBarrier(util_dynarray_num_elements(&ctx->barrier_scratch, D3D12_RESOURCE_BARRIER),
+                                    (D3D12_RESOURCE_BARRIER *) ctx->barrier_scratch.data);
+      util_dynarray_clear(&ctx->barrier_scratch);
+   }
+}
diff --git a/src/gallium/drivers/d3d12/meson.build b/src/gallium/drivers/d3d12/meson.build
index 2f09dc250f6..6368735d075 100644
--- a/src/gallium/drivers/d3d12/meson.build
+++ b/src/gallium/drivers/d3d12/meson.build
@@ -45,7 +45,6 @@ files_libd3d12 = files(
   'd3d12_screen.cpp',
   'd3d12_surface.cpp',
   'd3d12_tcs_variant.cpp',
-  'D3D12ResourceState.cpp',
 )
 
 if with_gallium_d3d12_video



More information about the mesa-commit mailing list