st/vdpau: Revert "change the order in which filters are applied(v3)"
[mesa.git] / src / gallium / state_trackers / nine / resource9.c
index 15c784f21948343c756e7a6fe45d60d9342fb86b..ade1823dbaef8292848c74ce8045a9c229879385 100644 (file)
 
 #include "util/u_hash_table.h"
 #include "util/u_inlines.h"
+#include "util/u_resource.h"
 
 #include "nine_pdata.h"
 
 #define DBG_CHANNEL DBG_RESOURCE
 
-
 HRESULT
 NineResource9_ctor( struct NineResource9 *This,
                     struct NineUnknownParams *pParams,
@@ -52,9 +52,6 @@ NineResource9_ctor( struct NineResource9 *This,
         This, pParams, initResource, (int) Allocate,
         Type, Pool, Usage);
 
-    DBG("This=%p pParams=%p initResource=%p Allocate=%d Type=%d Pool=%d usage=%d\n",
-        This, pParams, initResource, Allocate, Type, Pool, Usage);
-
     hr = NineUnknown_ctor(&This->base, pParams);
     if (FAILED(hr))
         return hr;
@@ -65,6 +62,33 @@ NineResource9_ctor( struct NineResource9 *This,
 
     if (Allocate) {
         assert(!initResource);
+
+        /* On Windows it is possible allocation fails when
+         * IDirect3DDevice9::GetAvailableTextureMem() still reports
+         * enough free space.
+         *
+         * Some games allocate surfaces
+         * in a loop until they receive D3DERR_OUTOFVIDEOMEMORY to measure
+         * the available texture memory size.
+         *
+         * We are not using the drivers VRAM statistics because:
+         *  * This would add overhead to each resource allocation.
+         *  * Freeing memory is lazy and takes some time, but applications
+         *    expects the memory counter to change immediately after allocating
+         *    or freeing memory.
+         *
+         * Vertexbuffers and indexbuffers are not accounted !
+         */
+        if (This->info.target != PIPE_BUFFER) {
+            This->size = util_resource_size(&This->info);
+
+            This->base.device->available_texture_mem -= This->size;
+            if (This->base.device->available_texture_mem <=
+                    This->base.device->available_texture_limit) {
+                return D3DERR_OUTOFVIDEOMEMORY;
+            }
+        }
+
         DBG("(%p) Creating pipe_resource.\n", This);
         This->resource = screen->resource_create(screen, &This->info);
         if (!This->resource)
@@ -86,6 +110,8 @@ NineResource9_ctor( struct NineResource9 *This,
 void
 NineResource9_dtor( struct NineResource9 *This )
 {
+    DBG("This=%p\n", This);
+
     if (This->pdata) {
         util_hash_table_foreach(This->pdata, ht_guid_delete, NULL);
         util_hash_table_destroy(This->pdata);
@@ -95,6 +121,10 @@ NineResource9_dtor( struct NineResource9 *This )
      * still hold a reference. */
     pipe_resource_reference(&This->resource, NULL);
 
+    /* NOTE: size is 0, unless something has actually been allocated */
+    if (This->base.device)
+        This->base.device->available_texture_mem += This->size;
+
     NineUnknown_dtor(&This->base);
 }
 
@@ -110,7 +140,7 @@ NineResource9_GetPool( struct NineResource9 *This )
     return This->pool;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineResource9_SetPrivateData( struct NineResource9 *This,
                               REFGUID refguid,
                               const void *pData,
@@ -120,9 +150,10 @@ NineResource9_SetPrivateData( struct NineResource9 *This,
     enum pipe_error err;
     struct pheader *header;
     const void *user_data = pData;
+    char guid_str[64];
 
-    DBG("This=%p refguid=%p pData=%p SizeOfData=%u Flags=%x\n",
-        This, refguid, pData, SizeOfData, Flags);
+    DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
+        This, GUID_sprintf(guid_str, refguid), pData, SizeOfData, Flags);
 
     if (Flags & D3DSPD_IUNKNOWN)
         user_assert(SizeOfData == sizeof(IUnknown *), D3DERR_INVALIDCALL);
@@ -144,8 +175,9 @@ NineResource9_SetPrivateData( struct NineResource9 *This,
 
     header->size = SizeOfData;
     memcpy(header->data, user_data, header->size);
+    memcpy(&header->guid, refguid, sizeof(header->guid));
 
-    err = util_hash_table_set(This->pdata, refguid, header);
+    err = util_hash_table_set(This->pdata, &header->guid, header);
     if (err == PIPE_OK) {
         if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header->data); }
         return D3D_OK;
@@ -157,27 +189,30 @@ NineResource9_SetPrivateData( struct NineResource9 *This,
     return D3DERR_DRIVERINTERNALERROR;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineResource9_GetPrivateData( struct NineResource9 *This,
                               REFGUID refguid,
                               void *pData,
                               DWORD *pSizeOfData )
 {
     struct pheader *header;
+    DWORD sizeofdata;
+    char guid_str[64];
 
-    DBG("This=%p refguid=%p pData=%p pSizeOfData=%p\n",
-        This, refguid, pData, pSizeOfData);
-
-    user_assert(pSizeOfData, E_POINTER);
+    DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
+        This, GUID_sprintf(guid_str, refguid), pData, pSizeOfData);
 
     header = util_hash_table_get(This->pdata, refguid);
     if (!header) { return D3DERR_NOTFOUND; }
 
+    user_assert(pSizeOfData, E_POINTER);
+    sizeofdata = *pSizeOfData;
+    *pSizeOfData = header->size;
+
     if (!pData) {
-        *pSizeOfData = header->size;
         return D3D_OK;
     }
-    if (*pSizeOfData < header->size) {
+    if (sizeofdata < header->size) {
         return D3DERR_MOREDATA;
     }
 
@@ -187,13 +222,14 @@ NineResource9_GetPrivateData( struct NineResource9 *This,
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineResource9_FreePrivateData( struct NineResource9 *This,
                                REFGUID refguid )
 {
     struct pheader *header;
+    char guid_str[64];
 
-    DBG("This=%p refguid=%p\n", This, refguid);
+    DBG("This=%p GUID=%s\n", This, GUID_sprintf(guid_str, refguid));
 
     header = util_hash_table_get(This->pdata, refguid);
     if (!header)
@@ -205,26 +241,32 @@ NineResource9_FreePrivateData( struct NineResource9 *This,
     return D3D_OK;
 }
 
-DWORD WINAPI
+DWORD NINE_WINAPI
 NineResource9_SetPriority( struct NineResource9 *This,
                            DWORD PriorityNew )
 {
-    DWORD prev = This->priority;
-
+    DWORD prev;
     DBG("This=%p, PriorityNew=%d\n", This, PriorityNew);
 
+    if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE)
+        return 0;
+
+    prev = This->priority;
     This->priority = PriorityNew;
     return prev;
 }
 
-DWORD WINAPI
+DWORD NINE_WINAPI
 NineResource9_GetPriority( struct NineResource9 *This )
 {
+    if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE)
+        return 0;
+
     return This->priority;
 }
 
 /* NOTE: Don't forget to adjust locked vtable if you change this ! */
-void WINAPI
+void NINE_WINAPI
 NineResource9_PreLoad( struct NineResource9 *This )
 {
     if (This->pool != D3DPOOL_MANAGED)
@@ -235,7 +277,7 @@ NineResource9_PreLoad( struct NineResource9 *This )
      */
 }
 
-D3DRESOURCETYPE WINAPI
+D3DRESOURCETYPE NINE_WINAPI
 NineResource9_GetType( struct NineResource9 *This )
 {
     return This->type;