From 10bfdfaee828856af112b785bf4524da41f7e90e Mon Sep 17 00:00:00 2001
From: WinterSnowfall <WinterSnowfall@users.noreply.github.com>
Date: Fri, 14 Feb 2025 00:45:47 +0200
Subject: [PATCH] [d3d8] Set priority only for D3DPOOL_MANAGED resources

---
 src/d3d8/d3d8_buffer.h      |  4 +---
 src/d3d8/d3d8_device.cpp    | 18 +++++++++---------
 src/d3d8/d3d8_device.h      |  4 ++--
 src/d3d8/d3d8_resource.h    | 17 ++++++++++++-----
 src/d3d8/d3d8_subresource.h |  3 ++-
 src/d3d8/d3d8_surface.cpp   |  6 ++++--
 src/d3d8/d3d8_surface.h     |  2 ++
 src/d3d8/d3d8_swapchain.cpp |  2 +-
 src/d3d8/d3d8_texture.cpp   |  9 ++++++---
 src/d3d8/d3d8_texture.h     |  8 ++++++--
 src/d3d8/d3d8_volume.cpp    |  3 ++-
 src/d3d8/d3d8_volume.h      |  1 +
 12 files changed, 48 insertions(+), 29 deletions(-)

diff --git a/src/d3d8/d3d8_buffer.h b/src/d3d8/d3d8_buffer.h
index c260e16ff..6ffcdaeb8 100644
--- a/src/d3d8/d3d8_buffer.h
+++ b/src/d3d8/d3d8_buffer.h
@@ -16,8 +16,7 @@ namespace dxvk {
             Com<D3D9>&&     pBuffer,
             D3DPOOL         Pool,
             DWORD           Usage)
-      : D3D8Resource<D3D9, D3D8> (pDevice, std::move(pBuffer))
-      , m_pool                   (Pool)
+      : D3D8Resource<D3D9, D3D8> (pDevice, Pool, std::move(pBuffer))
       , m_usage                  (Usage) {
       m_options = this->GetParent()->GetOptions();
     }
@@ -52,7 +51,6 @@ namespace dxvk {
   protected:
 
     const D3D8Options* m_options;
-    const D3DPOOL      m_pool;
     const DWORD        m_usage;
 
   };
diff --git a/src/d3d8/d3d8_device.cpp b/src/d3d8/d3d8_device.cpp
index 59b4288f4..47170fee0 100644
--- a/src/d3d8/d3d8_device.cpp
+++ b/src/d3d8/d3d8_device.cpp
@@ -287,7 +287,7 @@ namespace dxvk {
       HRESULT res = GetD3D9()->GetBackBuffer(0, iBackBuffer, (d3d9::D3DBACKBUFFER_TYPE)Type, &pSurface9);
 
       if (likely(SUCCEEDED(res))) {
-        m_backBuffers[iBackBuffer] = new D3D8Surface(this, std::move(pSurface9));
+        m_backBuffers[iBackBuffer] = new D3D8Surface(this, D3DPOOL_DEFAULT, std::move(pSurface9));
         *ppBackBuffer = m_backBuffers[iBackBuffer].ref();
       }
 
@@ -347,7 +347,7 @@ namespace dxvk {
       NULL);
 
     if (likely(SUCCEEDED(res)))
-      *ppTexture = ref(new D3D8Texture2D(this, std::move(pTex9)));
+      *ppTexture = ref(new D3D8Texture2D(this, Pool, std::move(pTex9)));
 
     return res;
   }
@@ -381,7 +381,7 @@ namespace dxvk {
       NULL);
 
     if (likely(SUCCEEDED(res)))
-      *ppVolumeTexture = ref(new D3D8Texture3D(this, std::move(pVolume9)));
+      *ppVolumeTexture = ref(new D3D8Texture3D(this, Pool, std::move(pVolume9)));
 
     return res;
   }
@@ -414,7 +414,7 @@ namespace dxvk {
       NULL);
 
     if (likely(SUCCEEDED(res)))
-      *ppCubeTexture = ref(new D3D8TextureCube(this, std::move(pCube9)));
+      *ppCubeTexture = ref(new D3D8TextureCube(this, Pool, std::move(pCube9)));
 
     return res;
   }
@@ -493,7 +493,7 @@ namespace dxvk {
       NULL);
 
     if (likely(SUCCEEDED(res)))
-      *ppSurface = ref(new D3D8Surface(this, std::move(pSurf9)));
+      *ppSurface = ref(new D3D8Surface(this, D3DPOOL_DEFAULT, std::move(pSurf9)));
 
     return res;
   }
@@ -526,7 +526,7 @@ namespace dxvk {
       NULL);
 
     if (likely(SUCCEEDED(res)))
-      *ppSurface = ref(new D3D8Surface(this, std::move(pSurf9)));
+      *ppSurface = ref(new D3D8Surface(this, D3DPOOL_DEFAULT, std::move(pSurf9)));
 
     return res;
   }
@@ -558,7 +558,7 @@ namespace dxvk {
       NULL);
 
     if (likely(SUCCEEDED(res)))
-      *ppSurface = ref(new D3D8Surface(this, std::move(pSurf)));
+      *ppSurface = ref(new D3D8Surface(this, pool, std::move(pSurf)));
 
     return res;
   }
@@ -1064,7 +1064,7 @@ namespace dxvk {
       HRESULT res = GetD3D9()->GetRenderTarget(0, &pRT9); // use RT index 0
 
       if (likely(SUCCEEDED(res))) {
-        m_renderTarget = new D3D8Surface(this, std::move(pRT9));
+        m_renderTarget = new D3D8Surface(this, D3DPOOL_DEFAULT, std::move(pRT9));
         *ppRenderTarget = m_renderTarget.ref();
       }
 
@@ -1088,7 +1088,7 @@ namespace dxvk {
       HRESULT res = GetD3D9()->GetDepthStencilSurface(&pStencil9);
 
       if (likely(SUCCEEDED(res))) {
-        m_depthStencil = new D3D8Surface(this, std::move(pStencil9));
+        m_depthStencil = new D3D8Surface(this, D3DPOOL_DEFAULT, std::move(pStencil9));
         *ppZStencilSurface = m_depthStencil.ref();
       }
 
diff --git a/src/d3d8/d3d8_device.h b/src/d3d8/d3d8_device.h
index 7ccbd8bae..ce5c4e58a 100644
--- a/src/d3d8/d3d8_device.h
+++ b/src/d3d8/d3d8_device.h
@@ -399,14 +399,14 @@ namespace dxvk {
       for (UINT i = 0; i < m_presentParams.BackBufferCount; i++) {
         Com<d3d9::IDirect3DSurface9> pSurface9;
         GetD3D9()->GetBackBuffer(0, i, d3d9::D3DBACKBUFFER_TYPE_MONO, &pSurface9);
-        m_backBuffers[i] = new D3D8Surface(this, std::move(pSurface9));
+        m_backBuffers[i] = new D3D8Surface(this, D3DPOOL_DEFAULT, std::move(pSurface9));
       }
 
       Com<d3d9::IDirect3DSurface9> pStencil9;
       // This call will fail if the D3D9 device is created without
       // the EnableAutoDepthStencil presentation parameter set to TRUE.
       HRESULT res = GetD3D9()->GetDepthStencilSurface(&pStencil9);
-      m_autoDepthStencil = FAILED(res) ? nullptr : new D3D8Surface(this, std::move(pStencil9));
+      m_autoDepthStencil = FAILED(res) ? nullptr : new D3D8Surface(this, D3DPOOL_DEFAULT, std::move(pStencil9));
 
       m_renderTarget = m_backBuffers[0];
       m_depthStencil = m_autoDepthStencil;
diff --git a/src/d3d8/d3d8_resource.h b/src/d3d8/d3d8_resource.h
index 9053a973a..0299b0bf7 100644
--- a/src/d3d8/d3d8_resource.h
+++ b/src/d3d8/d3d8_resource.h
@@ -18,8 +18,9 @@ namespace dxvk {
 
   public:
 
-    D3D8Resource(D3D8Device* pDevice, Com<D3D9>&& Object)
+    D3D8Resource(D3D8Device* pDevice, D3DPOOL Pool, Com<D3D9>&& Object)
       : D3D8DeviceChild<D3D9, D3D8>(pDevice, std::move(Object))
+      , m_pool                     ( Pool )
       , m_priority                 ( 0 ) { }
 
     HRESULT STDMETHODCALLTYPE SetPrivateData(
@@ -79,9 +80,14 @@ namespace dxvk {
     }
 
     DWORD STDMETHODCALLTYPE SetPriority(DWORD PriorityNew) {
-      DWORD oldPriority = m_priority;
-      m_priority = PriorityNew;
-      return oldPriority;
+      // Priority can only be set for D3DPOOL_MANAGED resources
+      if (likely(m_pool == D3DPOOL_MANAGED)) {
+        DWORD oldPriority = m_priority;
+        m_priority = PriorityNew;
+        return oldPriority;
+      }
+
+      return m_priority;
     }
 
     DWORD STDMETHODCALLTYPE GetPriority() {
@@ -99,7 +105,8 @@ namespace dxvk {
 
   protected:
 
-    DWORD m_priority;
+    const D3DPOOL        m_pool;
+          DWORD          m_priority;
 
   private:
 
diff --git a/src/d3d8/d3d8_subresource.h b/src/d3d8/d3d8_subresource.h
index 7b0c0edd6..9cafcdce7 100644
--- a/src/d3d8/d3d8_subresource.h
+++ b/src/d3d8/d3d8_subresource.h
@@ -16,9 +16,10 @@ namespace dxvk {
 
     D3D8Subresource(
             D3D8Device*             pDevice,
+      const D3DPOOL                 Pool,
             Com<D3D9>&&             Object,
             IDirect3DBaseTexture8*  pBaseTexture)
-    : Resource(pDevice, std::move(Object)),
+    : Resource(pDevice, Pool, std::move(Object)),
       m_container(pBaseTexture) {
     }
 
diff --git a/src/d3d8/d3d8_surface.cpp b/src/d3d8/d3d8_surface.cpp
index eb5c910fb..7f8b14b79 100644
--- a/src/d3d8/d3d8_surface.cpp
+++ b/src/d3d8/d3d8_surface.cpp
@@ -7,16 +7,18 @@ namespace dxvk {
 
   D3D8Surface::D3D8Surface(
           D3D8Device*                     pDevice,
+    const D3DPOOL                         Pool,
           IDirect3DBaseTexture8*          pTexture,
           Com<d3d9::IDirect3DSurface9>&&  pSurface)
-    : D3D8SurfaceBase (pDevice, std::move(pSurface), pTexture) {
+    : D3D8SurfaceBase (pDevice, Pool, std::move(pSurface), pTexture) {
   }
 
   // A surface does not need to be attached to a texture
   D3D8Surface::D3D8Surface(
           D3D8Device*                     pDevice,
+    const D3DPOOL                         Pool,
           Com<d3d9::IDirect3DSurface9>&&  pSurface)
-    : D3D8Surface (pDevice, nullptr, std::move(pSurface)) {
+    : D3D8Surface (pDevice, Pool, nullptr, std::move(pSurface)) {
   }
 
   HRESULT STDMETHODCALLTYPE D3D8Surface::GetDesc(D3DSURFACE_DESC* pDesc) {
diff --git a/src/d3d8/d3d8_surface.h b/src/d3d8/d3d8_surface.h
index 377dbc299..71db36963 100644
--- a/src/d3d8/d3d8_surface.h
+++ b/src/d3d8/d3d8_surface.h
@@ -15,11 +15,13 @@ namespace dxvk {
 
     D3D8Surface(
             D3D8Device*                     pDevice,
+      const D3DPOOL                         Pool,
             IDirect3DBaseTexture8*          pTexture,
             Com<d3d9::IDirect3DSurface9>&&  pSurface);
 
     D3D8Surface(
             D3D8Device*                     pDevice,
+      const D3DPOOL                         Pool,
             Com<d3d9::IDirect3DSurface9>&&  pSurface);
 
     HRESULT STDMETHODCALLTYPE GetDesc(D3DSURFACE_DESC* pDesc) final;
diff --git a/src/d3d8/d3d8_swapchain.cpp b/src/d3d8/d3d8_swapchain.cpp
index 95ebaf47a..844cc5fec 100644
--- a/src/d3d8/d3d8_swapchain.cpp
+++ b/src/d3d8/d3d8_swapchain.cpp
@@ -27,7 +27,7 @@ namespace dxvk {
       HRESULT res = GetD3D9()->GetBackBuffer(BackBuffer, (d3d9::D3DBACKBUFFER_TYPE)Type, &pSurface9);
 
       if (likely(SUCCEEDED(res))) {
-        m_backBuffers[BackBuffer] = new D3D8Surface(GetParent(), std::move(pSurface9));
+        m_backBuffers[BackBuffer] = new D3D8Surface(GetParent(), D3DPOOL_DEFAULT, std::move(pSurface9));
         *ppBackBuffer = m_backBuffers[BackBuffer].ref();
       }
 
diff --git a/src/d3d8/d3d8_texture.cpp b/src/d3d8/d3d8_texture.cpp
index 35a63facd..273ec38c3 100644
--- a/src/d3d8/d3d8_texture.cpp
+++ b/src/d3d8/d3d8_texture.cpp
@@ -8,8 +8,9 @@ namespace dxvk {
 
   D3D8Texture2D::D3D8Texture2D(
           D3D8Device*                    pDevice,
+    const D3DPOOL                        Pool,
           Com<d3d9::IDirect3DTexture9>&& pTexture)
-    : D3D8Texture2DBase(pDevice, std::move(pTexture), pTexture->GetLevelCount()) {
+    : D3D8Texture2DBase(pDevice, Pool, std::move(pTexture), pTexture->GetLevelCount()) {
   }
 
   D3DRESOURCETYPE STDMETHODCALLTYPE D3D8Texture2D::GetType() { return D3DRTYPE_TEXTURE; }
@@ -51,8 +52,9 @@ namespace dxvk {
 
   D3D8Texture3D::D3D8Texture3D(
           D3D8Device*                           pDevice,
+    const D3DPOOL                               Pool,
           Com<d3d9::IDirect3DVolumeTexture9>&&  pVolumeTexture)
-    : D3D8Texture3DBase(pDevice, std::move(pVolumeTexture), pVolumeTexture->GetLevelCount()) {}
+    : D3D8Texture3DBase(pDevice, Pool, std::move(pVolumeTexture), pVolumeTexture->GetLevelCount()) {}
 
   D3DRESOURCETYPE STDMETHODCALLTYPE D3D8Texture3D::GetType() { return D3DRTYPE_VOLUMETEXTURE; }
 
@@ -98,8 +100,9 @@ namespace dxvk {
 
   D3D8TextureCube::D3D8TextureCube(
           D3D8Device*                         pDevice,
+    const D3DPOOL                             Pool,
           Com<d3d9::IDirect3DCubeTexture9>&&  pTexture)
-    : D3D8TextureCubeBase(pDevice, std::move(pTexture), pTexture->GetLevelCount() * CUBE_FACES) {
+    : D3D8TextureCubeBase(pDevice, Pool, std::move(pTexture), pTexture->GetLevelCount() * CUBE_FACES) {
   }
 
   D3DRESOURCETYPE STDMETHODCALLTYPE D3D8TextureCube::GetType() { return D3DRTYPE_CUBETEXTURE; }
diff --git a/src/d3d8/d3d8_texture.h b/src/d3d8/d3d8_texture.h
index 189293173..0cc8ba713 100644
--- a/src/d3d8/d3d8_texture.h
+++ b/src/d3d8/d3d8_texture.h
@@ -21,9 +21,10 @@ namespace dxvk {
 
     D3D8BaseTexture(
             D3D8Device*                         pDevice,
+      const D3DPOOL                             Pool,
             Com<D3D9>&&                         pBaseTexture,
             UINT                                SubresourceCount)
-        : D3D8Resource<D3D9, D3D8> ( pDevice, std::move(pBaseTexture) ) {
+        : D3D8Resource<D3D9, D3D8> ( pDevice, Pool, std::move(pBaseTexture) ) {
       m_subresources.resize(SubresourceCount, nullptr);
     }
 
@@ -74,7 +75,7 @@ namespace dxvk {
           Com<SubresourceType9> subresource = LookupSubresource(Index);
 
           // Cache the subresource
-          m_subresources[Index] = new SubresourceType(this->m_parent, this, std::move(subresource));
+          m_subresources[Index] = new SubresourceType(this->m_parent, this->m_pool, this, std::move(subresource));
         } catch (HRESULT res) {
           return res;
         }
@@ -112,6 +113,7 @@ namespace dxvk {
 
     D3D8Texture2D(
             D3D8Device*                    pDevice,
+      const D3DPOOL                        Pool,
             Com<d3d9::IDirect3DTexture9>&& pTexture);
 
     D3DRESOURCETYPE STDMETHODCALLTYPE GetType() final;
@@ -139,6 +141,7 @@ namespace dxvk {
 
     D3D8Texture3D(
             D3D8Device*                           pDevice,
+      const D3DPOOL                               Pool,
             Com<d3d9::IDirect3DVolumeTexture9>&&  pVolumeTexture);
 
     D3DRESOURCETYPE STDMETHODCALLTYPE GetType() final;
@@ -166,6 +169,7 @@ namespace dxvk {
 
     D3D8TextureCube(
             D3D8Device*                         pDevice,
+      const D3DPOOL                             Pool,
             Com<d3d9::IDirect3DCubeTexture9>&&  pTexture);
 
     D3DRESOURCETYPE STDMETHODCALLTYPE GetType() final;
diff --git a/src/d3d8/d3d8_volume.cpp b/src/d3d8/d3d8_volume.cpp
index d53266d77..f7e40595a 100644
--- a/src/d3d8/d3d8_volume.cpp
+++ b/src/d3d8/d3d8_volume.cpp
@@ -6,9 +6,10 @@ namespace dxvk {
 
   D3D8Volume::D3D8Volume(
           D3D8Device*                   pDevice,
+    const D3DPOOL                       Pool,
           IDirect3DVolumeTexture8*      pTexture,
           Com<d3d9::IDirect3DVolume9>&& pVolume)
-    : D3D8VolumeBase(pDevice, std::move(pVolume), pTexture) {
+    : D3D8VolumeBase(pDevice, Pool, std::move(pVolume), pTexture) {
   }
 
   HRESULT STDMETHODCALLTYPE D3D8Volume::GetDesc(D3DVOLUME_DESC* pDesc) {
diff --git a/src/d3d8/d3d8_volume.h b/src/d3d8/d3d8_volume.h
index cd67d555a..baad95eb4 100644
--- a/src/d3d8/d3d8_volume.h
+++ b/src/d3d8/d3d8_volume.h
@@ -11,6 +11,7 @@ namespace dxvk {
 
     D3D8Volume(
             D3D8Device*                   pDevice,
+      const D3DPOOL                       Pool,
             IDirect3DVolumeTexture8*      pTexture,
             Com<d3d9::IDirect3DVolume9>&& pVolume);