2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 * Copyright 2015 Patrick Rudolph <siro@das-labor.org>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
26 #include "nine_helpers.h"
27 #include "nine_pipe.h"
29 #include "pipe/p_screen.h"
30 #include "pipe/p_context.h"
31 #include "pipe/p_state.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_format.h"
34 #include "util/u_box.h"
36 #define DBG_CHANNEL (DBG_INDEXBUFFER|DBG_VERTEXBUFFER)
39 NineBuffer9_ctor( struct NineBuffer9
*This
,
40 struct NineUnknownParams
*pParams
,
46 struct pipe_resource
*info
= &This
->base
.info
;
49 DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This
, Size
, Usage
, Pool
);
51 user_assert(Pool
!= D3DPOOL_SCRATCH
, D3DERR_INVALIDCALL
);
53 This
->maps
= MALLOC(sizeof(struct pipe_transfer
*));
60 This
->pipe
= pParams
->device
->pipe
;
62 info
->screen
= pParams
->device
->screen
;
63 info
->target
= PIPE_BUFFER
;
64 info
->format
= PIPE_FORMAT_R8_UNORM
;
68 /* Note: WRITEONLY is just tip for resource placement, the resource
69 * can still be read (but slower). */
70 info
->bind
= PIPE_BIND_VERTEX_BUFFER
;
72 /* It is hard to find clear information on where to place the buffer in
73 * memory depending on the flag.
74 * MSDN: resources are static, except for those with DYNAMIC, thus why you
75 * can only use DISCARD on them.
76 * ATI doc: The driver has the liberty it wants for having things static
78 * MANAGED: Ram + uploads to Vram copy at unlock (msdn and nvidia doc say
79 * at first draw call using the buffer)
80 * DEFAULT + Usage = 0 => System memory backing for easy read access
81 * (That doc is very unclear on the details, like whether some copies to
82 * vram copy are involved or not).
83 * DEFAULT + WRITEONLY => Vram
84 * DEFAULT + WRITEONLY + DYNAMIC => Either Vram buffer or GTT_WC, depending on what the driver wants.
86 if (Pool
== D3DPOOL_SYSTEMMEM
)
87 info
->usage
= PIPE_USAGE_STAGING
;
88 else if (Pool
== D3DPOOL_MANAGED
)
89 info
->usage
= PIPE_USAGE_DEFAULT
;
90 else if (Usage
& D3DUSAGE_DYNAMIC
&& Usage
& D3DUSAGE_WRITEONLY
)
91 info
->usage
= PIPE_USAGE_STREAM
;
92 else if (Usage
& D3DUSAGE_WRITEONLY
)
93 info
->usage
= PIPE_USAGE_DEFAULT
;
94 /* For the remaining two, PIPE_USAGE_STAGING would probably be
95 * a good fit according to the doc. However it seems rather a mistake
96 * from apps to use these (mistakes that do really happen). Try
97 * to put the flags that are the best compromise between the real
98 * behaviour and what buggy apps should get for better performance. */
99 else if (Usage
& D3DUSAGE_DYNAMIC
)
100 info
->usage
= PIPE_USAGE_STREAM
;
102 info
->usage
= PIPE_USAGE_DYNAMIC
;
104 /* if (pDesc->Usage & D3DUSAGE_DONOTCLIP) { } */
105 /* if (pDesc->Usage & D3DUSAGE_NONSECURE) { } */
106 /* if (pDesc->Usage & D3DUSAGE_NPATCHES) { } */
107 /* if (pDesc->Usage & D3DUSAGE_POINTS) { } */
108 /* if (pDesc->Usage & D3DUSAGE_RTPATCHES) { } */
109 if (Usage
& D3DUSAGE_SOFTWAREPROCESSING
)
110 DBG("Application asked for Software Vertex Processing, "
111 "but this is unimplemented\n");
112 /* if (pDesc->Usage & D3DUSAGE_TEXTAPI) { } */
116 info
->array_size
= 1;
117 info
->last_level
= 0;
118 info
->nr_samples
= 0;
120 hr
= NineResource9_ctor(&This
->base
, pParams
, NULL
, TRUE
,
126 if (Pool
== D3DPOOL_MANAGED
) {
127 This
->managed
.data
= align_malloc(
128 nine_format_get_level_alloc_size(This
->base
.info
.format
,
130 if (!This
->managed
.data
)
131 return E_OUTOFMEMORY
;
132 memset(This
->managed
.data
, 0, Size
);
133 This
->managed
.dirty
= TRUE
;
134 u_box_1d(0, Size
, &This
->managed
.dirty_box
);
135 list_inithead(&This
->managed
.list
);
136 list_inithead(&This
->managed
.list2
);
137 list_add(&This
->managed
.list
, &pParams
->device
->update_buffers
);
138 list_add(&This
->managed
.list2
, &pParams
->device
->managed_buffers
);
145 NineBuffer9_dtor( struct NineBuffer9
*This
)
147 DBG("This=%p\n", This
);
150 while (This
->nmaps
) {
151 NineBuffer9_Unlock(This
);
156 if (This
->base
.pool
== D3DPOOL_MANAGED
) {
157 if (This
->managed
.data
)
158 align_free(This
->managed
.data
);
159 if (This
->managed
.list
.prev
!= NULL
&& This
->managed
.list
.next
!= NULL
)
160 list_del(&This
->managed
.list
);
161 if (This
->managed
.list2
.prev
!= NULL
&& This
->managed
.list2
.next
!= NULL
)
162 list_del(&This
->managed
.list2
);
165 NineResource9_dtor(&This
->base
);
168 struct pipe_resource
*
169 NineBuffer9_GetResource( struct NineBuffer9
*This
)
171 return NineResource9_GetResource(&This
->base
);
175 NineBuffer9_Lock( struct NineBuffer9
*This
,
185 DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
186 This
, This
->base
.resource
,
187 OffsetToLock
, SizeToLock
, Flags
);
189 user_assert(ppbData
, E_POINTER
);
190 user_assert(!(Flags
& ~(D3DLOCK_DISCARD
|
192 D3DLOCK_NO_DIRTY_UPDATE
|
195 D3DLOCK_NOOVERWRITE
)), D3DERR_INVALIDCALL
);
197 if (SizeToLock
== 0) {
198 SizeToLock
= This
->size
- OffsetToLock
;
199 user_warn(OffsetToLock
!= 0);
202 u_box_1d(OffsetToLock
, SizeToLock
, &box
);
204 if (This
->base
.pool
== D3DPOOL_MANAGED
) {
205 /* READONLY doesn't dirty the buffer */
206 if (!(Flags
& D3DLOCK_READONLY
)) {
207 if (!This
->managed
.dirty
) {
208 assert(LIST_IS_EMPTY(&This
->managed
.list
));
209 This
->managed
.dirty
= TRUE
;
210 This
->managed
.dirty_box
= box
;
212 u_box_union_2d(&This
->managed
.dirty_box
, &This
->managed
.dirty_box
, &box
);
213 /* Do not upload while we are locking, we'll add it back later */
214 if (!LIST_IS_EMPTY(&This
->managed
.list
))
215 list_delinit(&This
->managed
.list
);
218 *ppbData
= (char *)This
->managed
.data
+ OffsetToLock
;
219 DBG("returning pointer %p\n", *ppbData
);
224 /* Driver ddi doc: READONLY is never passed to the device. So it can only
225 * have effect on things handled by the driver (MANAGED pool for example).
226 * Msdn doc: DISCARD and NOOVERWRITE are only for DYNAMIC.
227 * ATI doc: You can use DISCARD and NOOVERWRITE without DYNAMIC.
228 * Msdn doc: D3DLOCK_DONOTWAIT is not among the valid flags for buffers.
229 * Our tests: On win 7 nvidia, D3DLOCK_DONOTWAIT does return
230 * D3DERR_WASSTILLDRAWING if the resource is in use, except for DYNAMIC.
231 * Our tests: some apps do use both DISCARD and NOOVERWRITE at the same
232 * time. On windows it seems to return different pointer, thus indicating
233 * DISCARD is taken into account. */
235 if (Flags
& D3DLOCK_DISCARD
)
236 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
237 else if (Flags
& D3DLOCK_NOOVERWRITE
)
238 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_UNSYNCHRONIZED
;
240 usage
= PIPE_TRANSFER_READ_WRITE
;
241 if (Flags
& D3DLOCK_DONOTWAIT
&& !(This
->base
.usage
& D3DUSAGE_DYNAMIC
))
242 usage
|= PIPE_TRANSFER_DONTBLOCK
;
244 if (This
->nmaps
== This
->maxmaps
) {
245 struct pipe_transfer
**newmaps
=
246 REALLOC(This
->maps
, sizeof(struct pipe_transfer
*)*This
->maxmaps
,
247 sizeof(struct pipe_transfer
*)*(This
->maxmaps
<< 1));
249 return E_OUTOFMEMORY
;
252 This
->maps
= newmaps
;
255 data
= This
->pipe
->transfer_map(This
->pipe
, This
->base
.resource
, 0,
256 usage
, &box
, &This
->maps
[This
->nmaps
]);
259 DBG("pipe::transfer_map failed\n"
263 usage
, box
.x
, box
.width
);
265 if (Flags
& D3DLOCK_DONOTWAIT
)
266 return D3DERR_WASSTILLDRAWING
;
267 return D3DERR_INVALIDCALL
;
270 DBG("returning pointer %p\n", data
);
278 NineBuffer9_Unlock( struct NineBuffer9
*This
)
280 DBG("This=%p\n", This
);
282 user_assert(This
->nmaps
> 0, D3DERR_INVALIDCALL
);
283 if (This
->base
.pool
!= D3DPOOL_MANAGED
)
284 This
->pipe
->transfer_unmap(This
->pipe
, This
->maps
[--(This
->nmaps
)]);
287 /* TODO: Fix this to upload at the first draw call needing the data,
288 * instead of at the next draw call */
289 if (!This
->nmaps
&& This
->managed
.dirty
&& LIST_IS_EMPTY(&This
->managed
.list
))
290 list_add(&This
->managed
.list
, &This
->base
.base
.device
->update_buffers
);
296 NineBuffer9_SetDirty( struct NineBuffer9
*This
)
298 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
300 if (!This
->managed
.dirty
) {
301 assert(LIST_IS_EMPTY(&This
->managed
.list
));
302 list_add(&This
->managed
.list
, &This
->base
.base
.device
->update_buffers
);
303 This
->managed
.dirty
= TRUE
;
305 u_box_1d(0, This
->size
, &This
->managed
.dirty_box
);