2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "resource9.h"
25 #include "nine_helpers.h"
26 #include "nine_defines.h"
28 #include "util/u_inlines.h"
29 #include "util/u_resource.h"
31 #include "pipe/p_screen.h"
33 #define DBG_CHANNEL DBG_RESOURCE
36 NineResource9_ctor( struct NineResource9
*This
,
37 struct NineUnknownParams
*pParams
,
38 struct pipe_resource
*initResource
,
44 struct pipe_screen
*screen
;
47 DBG("This=%p pParams=%p initResource=%p Allocate=%d "
48 "Type=%d Pool=%d Usage=%d\n",
49 This
, pParams
, initResource
, (int) Allocate
,
52 hr
= NineUnknown_ctor(&This
->base
, pParams
);
56 This
->info
.screen
= screen
= This
->base
.device
->screen
;
58 pipe_resource_reference(&This
->resource
, initResource
);
61 assert(!initResource
);
63 /* On Windows it is possible allocation fails when
64 * IDirect3DDevice9::GetAvailableTextureMem() still reports
67 * Some games allocate surfaces
68 * in a loop until they receive D3DERR_OUTOFVIDEOMEMORY to measure
69 * the available texture memory size.
71 * We are not using the drivers VRAM statistics because:
72 * * This would add overhead to each resource allocation.
73 * * Freeing memory is lazy and takes some time, but applications
74 * expects the memory counter to change immediately after allocating
77 * Vertexbuffers and indexbuffers are not accounted !
79 if (This
->info
.target
!= PIPE_BUFFER
) {
80 This
->size
= util_resource_size(&This
->info
);
82 p_atomic_add(&This
->base
.device
->available_texture_mem
, -This
->size
);
83 if (This
->base
.device
->available_texture_mem
<=
84 This
->base
.device
->available_texture_limit
) {
85 return D3DERR_OUTOFVIDEOMEMORY
;
89 DBG("(%p) Creating pipe_resource.\n", This
);
90 This
->resource
= screen
->resource_create(screen
, &This
->info
);
92 return D3DERR_OUTOFVIDEOMEMORY
;
104 NineResource9_dtor( struct NineResource9
*This
)
106 DBG("This=%p\n", This
);
108 /* NOTE: We do have to use refcounting, the driver might
109 * still hold a reference. */
110 pipe_resource_reference(&This
->resource
, NULL
);
112 /* NOTE: size is 0, unless something has actually been allocated */
113 if (This
->base
.device
)
114 p_atomic_add(&This
->base
.device
->available_texture_mem
, This
->size
);
116 NineUnknown_dtor(&This
->base
);
119 struct pipe_resource
*
120 NineResource9_GetResource( struct NineResource9
*This
)
122 return This
->resource
;
126 NineResource9_GetPool( struct NineResource9
*This
)
132 NineResource9_SetPriority( struct NineResource9
*This
,
136 DBG("This=%p, PriorityNew=%d\n", This
, PriorityNew
);
138 if (This
->pool
!= D3DPOOL_MANAGED
|| This
->type
== D3DRTYPE_SURFACE
)
141 prev
= This
->priority
;
142 This
->priority
= PriorityNew
;
147 NineResource9_GetPriority( struct NineResource9
*This
)
149 if (This
->pool
!= D3DPOOL_MANAGED
|| This
->type
== D3DRTYPE_SURFACE
)
152 return This
->priority
;
155 /* NOTE: Don't forget to adjust locked vtable if you change this ! */
157 NineResource9_PreLoad( struct NineResource9
*This
)
159 if (This
->pool
!= D3DPOOL_MANAGED
)
161 /* We don't treat managed vertex or index buffers different from
162 * default ones (are managed vertex buffers even allowed ?), and
163 * the PreLoad for textures is overridden by superclass.
167 D3DRESOURCETYPE NINE_WINAPI
168 NineResource9_GetType( struct NineResource9
*This
)