• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3  *
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:
10  *
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
13  * Software.
14  *
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. */
22 
23 #include "resource9.h"
24 #include "device9.h"
25 #include "nine_helpers.h"
26 #include "nine_defines.h"
27 
28 #include "util/u_inlines.h"
29 #include "util/u_resource.h"
30 
31 #include "pipe/p_screen.h"
32 
33 #define DBG_CHANNEL DBG_RESOURCE
34 
35 HRESULT
NineResource9_ctor(struct NineResource9 * This,struct NineUnknownParams * pParams,struct pipe_resource * initResource,BOOL Allocate,D3DRESOURCETYPE Type,D3DPOOL Pool,DWORD Usage)36 NineResource9_ctor( struct NineResource9 *This,
37                     struct NineUnknownParams *pParams,
38                     struct pipe_resource *initResource,
39                     BOOL Allocate,
40                     D3DRESOURCETYPE Type,
41                     D3DPOOL Pool,
42                     DWORD Usage)
43 {
44     struct pipe_screen *screen;
45     HRESULT hr;
46 
47     DBG("This=%p pParams=%p initResource=%p Allocate=%d "
48         "Type=%d Pool=%d Usage=%d\n",
49         This, pParams, initResource, (int) Allocate,
50         Type, Pool, Usage);
51 
52     hr = NineUnknown_ctor(&This->base, pParams);
53     if (FAILED(hr))
54         return hr;
55 
56     This->info.screen = screen = This->base.device->screen;
57     if (initResource)
58         pipe_resource_reference(&This->resource, initResource);
59 
60     if (Allocate) {
61         assert(!initResource);
62 
63         /* On Windows it is possible allocation fails when
64          * IDirect3DDevice9::GetAvailableTextureMem() still reports
65          * enough free space.
66          *
67          * Some games allocate surfaces
68          * in a loop until they receive D3DERR_OUTOFVIDEOMEMORY to measure
69          * the available texture memory size.
70          *
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
75          *    or freeing memory.
76          *
77          * Vertexbuffers and indexbuffers are not accounted !
78          */
79         if (This->info.target != PIPE_BUFFER) {
80             This->size = util_resource_size(&This->info);
81 
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;
86             }
87         }
88 
89         DBG("(%p) Creating pipe_resource.\n", This);
90         This->resource = screen->resource_create(screen, &This->info);
91         if (!This->resource)
92             return D3DERR_OUTOFVIDEOMEMORY;
93     }
94 
95     This->type = Type;
96     This->pool = Pool;
97     This->usage = Usage;
98     This->priority = 0;
99 
100     return D3D_OK;
101 }
102 
103 void
NineResource9_dtor(struct NineResource9 * This)104 NineResource9_dtor( struct NineResource9 *This )
105 {
106     DBG("This=%p\n", This);
107 
108     /* NOTE: We do have to use refcounting, the driver might
109      * still hold a reference. */
110     pipe_resource_reference(&This->resource, NULL);
111 
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);
115 
116     NineUnknown_dtor(&This->base);
117 }
118 
119 struct pipe_resource *
NineResource9_GetResource(struct NineResource9 * This)120 NineResource9_GetResource( struct NineResource9 *This )
121 {
122     return This->resource;
123 }
124 
125 D3DPOOL
NineResource9_GetPool(struct NineResource9 * This)126 NineResource9_GetPool( struct NineResource9 *This )
127 {
128     return This->pool;
129 }
130 
131 DWORD NINE_WINAPI
NineResource9_SetPriority(struct NineResource9 * This,DWORD PriorityNew)132 NineResource9_SetPriority( struct NineResource9 *This,
133                            DWORD PriorityNew )
134 {
135     DWORD prev;
136     DBG("This=%p, PriorityNew=%d\n", This, PriorityNew);
137 
138     if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE)
139         return 0;
140 
141     prev = This->priority;
142     This->priority = PriorityNew;
143     return prev;
144 }
145 
146 DWORD NINE_WINAPI
NineResource9_GetPriority(struct NineResource9 * This)147 NineResource9_GetPriority( struct NineResource9 *This )
148 {
149     if (This->pool != D3DPOOL_MANAGED || This->type == D3DRTYPE_SURFACE)
150         return 0;
151 
152     return This->priority;
153 }
154 
155 /* NOTE: Don't forget to adjust locked vtable if you change this ! */
156 void NINE_WINAPI
NineResource9_PreLoad(struct NineResource9 * This)157 NineResource9_PreLoad( struct NineResource9 *This )
158 {
159     if (This->pool != D3DPOOL_MANAGED)
160         return;
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.
164      */
165 }
166 
167 D3DRESOURCETYPE NINE_WINAPI
NineResource9_GetType(struct NineResource9 * This)168 NineResource9_GetType( struct NineResource9 *This )
169 {
170     return This->type;
171 }
172