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_buffer_upload.h"
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 #include "pipe/p_defines.h"
34 #include "pipe/p_format.h"
35 #include "util/u_box.h"
36 #include "util/u_inlines.h"
38 #define DBG_CHANNEL (DBG_INDEXBUFFER|DBG_VERTEXBUFFER)
41 NineBuffer9_ctor( struct NineBuffer9
*This
,
42 struct NineUnknownParams
*pParams
,
48 struct pipe_resource
*info
= &This
->base
.info
;
51 DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This
, Size
, Usage
, Pool
);
53 user_assert(Pool
!= D3DPOOL_SCRATCH
, D3DERR_INVALIDCALL
);
55 This
->maps
= MALLOC(sizeof(struct NineTransfer
));
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 /* When Writeonly is not set, we don't want to enable the
106 This
->discard_nooverwrite_only
= !!(Usage
& D3DUSAGE_WRITEONLY
) &&
107 pParams
->device
->buffer_upload
;
108 /* if (pDesc->Usage & D3DUSAGE_DONOTCLIP) { } */
109 /* if (pDesc->Usage & D3DUSAGE_NONSECURE) { } */
110 /* if (pDesc->Usage & D3DUSAGE_NPATCHES) { } */
111 /* if (pDesc->Usage & D3DUSAGE_POINTS) { } */
112 /* if (pDesc->Usage & D3DUSAGE_RTPATCHES) { } */
113 /* The buffer must be usable with both sw and hw
114 * vertex processing. It is expected to be slower with hw. */
115 if (Usage
& D3DUSAGE_SOFTWAREPROCESSING
)
116 info
->usage
= PIPE_USAGE_STAGING
;
117 /* if (pDesc->Usage & D3DUSAGE_TEXTAPI) { } */
121 info
->array_size
= 1;
122 info
->last_level
= 0;
123 info
->nr_samples
= 0;
124 info
->nr_storage_samples
= 0;
126 hr
= NineResource9_ctor(&This
->base
, pParams
, NULL
, TRUE
,
132 if (Pool
== D3DPOOL_MANAGED
) {
133 This
->managed
.data
= align_calloc(
134 nine_format_get_level_alloc_size(This
->base
.info
.format
,
136 if (!This
->managed
.data
)
137 return E_OUTOFMEMORY
;
138 memset(This
->managed
.data
, 0, Size
);
139 This
->managed
.dirty
= TRUE
;
140 u_box_1d(0, Size
, &This
->managed
.dirty_box
);
141 list_inithead(&This
->managed
.list
);
142 list_inithead(&This
->managed
.list2
);
143 list_add(&This
->managed
.list2
, &pParams
->device
->managed_buffers
);
150 NineBuffer9_dtor( struct NineBuffer9
*This
)
152 DBG("This=%p\n", This
);
155 while (This
->nmaps
) {
156 NineBuffer9_Unlock(This
);
161 if (This
->base
.pool
== D3DPOOL_MANAGED
) {
162 if (This
->managed
.data
)
163 align_free(This
->managed
.data
);
164 if (This
->managed
.list
.prev
!= NULL
&& This
->managed
.list
.next
!= NULL
)
165 list_del(&This
->managed
.list
);
166 if (This
->managed
.list2
.prev
!= NULL
&& This
->managed
.list2
.next
!= NULL
)
167 list_del(&This
->managed
.list2
);
171 nine_upload_release_buffer(This
->base
.base
.device
->buffer_upload
, This
->buf
);
173 NineResource9_dtor(&This
->base
);
176 struct pipe_resource
*
177 NineBuffer9_GetResource( struct NineBuffer9
*This
, unsigned *offset
)
180 return nine_upload_buffer_resource_and_offset(This
->buf
, offset
);
182 return NineResource9_GetResource(&This
->base
);
186 NineBuffer9_RebindIfRequired( struct NineBuffer9
*This
,
187 struct NineDevice9
*device
)
191 if (!This
->bind_count
)
193 for (i
= 0; i
< device
->caps
.MaxStreams
; i
++) {
194 if (device
->state
.stream
[i
] == (struct NineVertexBuffer9
*)This
)
195 nine_context_set_stream_source(device
, i
,
196 (struct NineVertexBuffer9
*)This
,
197 device
->state
.vtxbuf
[i
].buffer_offset
,
198 device
->state
.vtxbuf
[i
].stride
);
200 if (device
->state
.idxbuf
== (struct NineIndexBuffer9
*)This
)
201 nine_context_set_indices(device
, (struct NineIndexBuffer9
*)This
);
205 NineBuffer9_Lock( struct NineBuffer9
*This
,
211 struct NineDevice9
*device
= This
->base
.base
.device
;
213 struct pipe_context
*pipe
;
217 DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
218 This
, This
->base
.resource
,
219 OffsetToLock
, SizeToLock
, Flags
);
221 user_assert(ppbData
, E_POINTER
);
222 user_assert(!(Flags
& ~(D3DLOCK_DISCARD
|
224 D3DLOCK_NO_DIRTY_UPDATE
|
227 D3DLOCK_NOOVERWRITE
)), D3DERR_INVALIDCALL
);
229 if (SizeToLock
== 0) {
230 SizeToLock
= This
->size
- OffsetToLock
;
231 user_warn(OffsetToLock
!= 0);
234 /* Write out of bound seems to have to be taken into account for these.
235 * TODO: Do more tests (is it only at buffer first lock ? etc).
236 * Since these buffers are supposed to be locked once and never
237 * writen again (MANAGED or DYNAMIC is used for the other uses cases),
238 * performance should be unaffected. */
239 if (!(This
->base
.usage
& D3DUSAGE_DYNAMIC
) && This
->base
.pool
!= D3DPOOL_MANAGED
)
240 SizeToLock
= This
->size
- OffsetToLock
;
242 u_box_1d(OffsetToLock
, SizeToLock
, &box
);
244 if (This
->base
.pool
== D3DPOOL_MANAGED
) {
245 /* READONLY doesn't dirty the buffer */
246 /* Tests on Win: READONLY doesn't wait for the upload */
247 if (!(Flags
& D3DLOCK_READONLY
)) {
248 if (!This
->managed
.dirty
) {
249 assert(list_is_empty(&This
->managed
.list
));
250 This
->managed
.dirty
= TRUE
;
251 This
->managed
.dirty_box
= box
;
252 if (p_atomic_read(&This
->managed
.pending_upload
))
253 nine_csmt_process(This
->base
.base
.device
);
255 u_box_union_2d(&This
->managed
.dirty_box
, &This
->managed
.dirty_box
, &box
);
256 /* Tests trying to draw while the buffer is locked show that
257 * MANAGED buffers are made dirty at Lock time */
258 BASEBUF_REGISTER_UPDATE(This
);
260 *ppbData
= (char *)This
->managed
.data
+ OffsetToLock
;
261 DBG("returning pointer %p\n", *ppbData
);
266 /* Driver ddi doc: READONLY is never passed to the device. So it can only
267 * have effect on things handled by the driver (MANAGED pool for example).
268 * Msdn doc: DISCARD and NOOVERWRITE are only for DYNAMIC.
269 * ATI doc: You can use DISCARD and NOOVERWRITE without DYNAMIC.
270 * Msdn doc: D3DLOCK_DONOTWAIT is not among the valid flags for buffers.
271 * Our tests: On win 7 nvidia, D3DLOCK_DONOTWAIT does return
272 * D3DERR_WASSTILLDRAWING if the resource is in use, except for DYNAMIC.
273 * Our tests: some apps do use both DISCARD and NOOVERWRITE at the same
274 * time. On windows it seems to return different pointer, thus indicating
275 * DISCARD is taken into account.
276 * Our tests: SYSTEMMEM doesn't DISCARD */
278 if (This
->base
.pool
== D3DPOOL_SYSTEMMEM
)
279 Flags
&= ~(D3DLOCK_DISCARD
| D3DLOCK_NOOVERWRITE
);
281 if (Flags
& D3DLOCK_DISCARD
)
282 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
283 else if (Flags
& D3DLOCK_NOOVERWRITE
)
284 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_UNSYNCHRONIZED
;
286 /* Do not ask for READ if writeonly and default pool (should be safe enough,
287 * as the doc says app shouldn't expect reading to work with writeonly).
288 * Ignore for Systemmem as it has special behaviours. */
289 usage
= ((This
->base
.usage
& D3DUSAGE_WRITEONLY
) && This
->base
.pool
== D3DPOOL_DEFAULT
) ?
290 PIPE_TRANSFER_WRITE
:
291 PIPE_TRANSFER_READ_WRITE
;
292 if (Flags
& D3DLOCK_DONOTWAIT
&& !(This
->base
.usage
& D3DUSAGE_DYNAMIC
))
293 usage
|= PIPE_TRANSFER_DONTBLOCK
;
295 This
->discard_nooverwrite_only
&= !!(Flags
& (D3DLOCK_DISCARD
| D3DLOCK_NOOVERWRITE
));
297 if (This
->nmaps
== This
->maxmaps
) {
298 struct NineTransfer
*newmaps
=
299 REALLOC(This
->maps
, sizeof(struct NineTransfer
)*This
->maxmaps
,
300 sizeof(struct NineTransfer
)*(This
->maxmaps
<< 1));
302 return E_OUTOFMEMORY
;
305 This
->maps
= newmaps
;
308 if (This
->buf
&& !This
->discard_nooverwrite_only
) {
309 struct pipe_box src_box
;
311 struct pipe_resource
*src_res
;
312 DBG("Disabling nine_subbuffer for a buffer having"
313 "used a nine_subbuffer buffer\n");
314 /* Copy buffer content to the buffer resource, which
316 * Note: The behaviour may be different from what is expected
317 * with double lock. However applications can't really make expectations
318 * about double locks, and don't really use them, so that's ok. */
319 src_res
= nine_upload_buffer_resource_and_offset(This
->buf
, &offset
);
320 u_box_1d(offset
, This
->size
, &src_box
);
322 pipe
= NineDevice9_GetPipe(device
);
323 pipe
->resource_copy_region(pipe
, This
->base
.resource
, 0, 0, 0, 0,
324 src_res
, 0, &src_box
);
325 /* Release previous resource */
326 if (This
->nmaps
>= 1)
327 This
->maps
[This
->nmaps
-1].should_destroy_buf
= true;
329 nine_upload_release_buffer(device
->buffer_upload
, This
->buf
);
332 NineBuffer9_RebindIfRequired(This
, device
);
335 This
->maps
[This
->nmaps
].transfer
= NULL
;
336 This
->maps
[This
->nmaps
].is_pipe_secondary
= false;
337 This
->maps
[This
->nmaps
].buf
= NULL
;
338 This
->maps
[This
->nmaps
].should_destroy_buf
= false;
340 if (This
->discard_nooverwrite_only
) {
341 if (This
->buf
&& (Flags
& D3DLOCK_DISCARD
)) {
342 /* Release previous buffer */
343 if (This
->nmaps
>= 1)
344 This
->maps
[This
->nmaps
-1].should_destroy_buf
= true;
346 nine_upload_release_buffer(device
->buffer_upload
, This
->buf
);
351 This
->buf
= nine_upload_create_buffer(device
->buffer_upload
, This
->base
.info
.width0
);
352 NineBuffer9_RebindIfRequired(This
, device
);
356 This
->maps
[This
->nmaps
].buf
= This
->buf
;
358 *ppbData
= nine_upload_buffer_get_map(This
->buf
) + OffsetToLock
;
361 /* Fallback to normal path, and don't try again */
362 This
->discard_nooverwrite_only
= false;
366 /* Previous mappings may need pending commands to write to the
367 * buffer (staging buffer for example). Before a NOOVERWRITE,
368 * we thus need a finish, to guarantee any upload is finished.
369 * Note for discard_nooverwrite_only we don't need to do this
370 * check as neither discard nor nooverwrite have issues there */
371 if (This
->need_sync_if_nooverwrite
&& !(Flags
& D3DLOCK_DISCARD
) &&
372 (Flags
& D3DLOCK_NOOVERWRITE
)) {
373 struct pipe_screen
*screen
= NineDevice9_GetScreen(device
);
374 struct pipe_fence_handle
*fence
= NULL
;
376 pipe
= NineDevice9_GetPipe(device
);
377 pipe
->flush(pipe
, &fence
, 0);
378 (void) screen
->fence_finish(screen
, NULL
, fence
, PIPE_TIMEOUT_INFINITE
);
379 screen
->fence_reference(screen
, &fence
, NULL
);
381 This
->need_sync_if_nooverwrite
= !(Flags
& (D3DLOCK_DISCARD
| D3DLOCK_NOOVERWRITE
));
383 /* When csmt is active, we want to avoid stalls as much as possible,
384 * and thus we want to create a new resource on discard and map it
385 * with the secondary pipe, instead of waiting on the main pipe. */
386 if (Flags
& D3DLOCK_DISCARD
&& device
->csmt_active
) {
387 struct pipe_screen
*screen
= NineDevice9_GetScreen(device
);
388 struct pipe_resource
*new_res
= screen
->resource_create(screen
, &This
->base
.info
);
390 /* Use the new resource */
391 pipe_resource_reference(&This
->base
.resource
, new_res
);
392 pipe_resource_reference(&new_res
, NULL
);
393 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_UNSYNCHRONIZED
;
394 NineBuffer9_RebindIfRequired(This
, device
);
395 This
->maps
[This
->nmaps
].is_pipe_secondary
= TRUE
;
397 } else if (Flags
& D3DLOCK_NOOVERWRITE
&& device
->csmt_active
)
398 This
->maps
[This
->nmaps
].is_pipe_secondary
= TRUE
;
400 if (This
->maps
[This
->nmaps
].is_pipe_secondary
)
401 pipe
= device
->pipe_secondary
;
403 pipe
= NineDevice9_GetPipe(device
);
405 data
= pipe
->transfer_map(pipe
, This
->base
.resource
, 0,
406 usage
, &box
, &This
->maps
[This
->nmaps
].transfer
);
409 DBG("pipe::transfer_map failed\n"
413 usage
, box
.x
, box
.width
);
415 if (Flags
& D3DLOCK_DONOTWAIT
)
416 return D3DERR_WASSTILLDRAWING
;
417 return D3DERR_INVALIDCALL
;
420 DBG("returning pointer %p\n", data
);
428 NineBuffer9_Unlock( struct NineBuffer9
*This
)
430 struct NineDevice9
*device
= This
->base
.base
.device
;
431 struct pipe_context
*pipe
;
432 DBG("This=%p\n", This
);
434 user_assert(This
->nmaps
> 0, D3DERR_INVALIDCALL
);
436 if (This
->base
.pool
!= D3DPOOL_MANAGED
) {
437 if (!This
->maps
[This
->nmaps
].buf
) {
438 pipe
= This
->maps
[This
->nmaps
].is_pipe_secondary
?
439 device
->pipe_secondary
:
440 nine_context_get_pipe_acquire(device
);
441 pipe
->transfer_unmap(pipe
, This
->maps
[This
->nmaps
].transfer
);
442 /* We need to flush in case the driver does implicit copies */
443 if (This
->maps
[This
->nmaps
].is_pipe_secondary
)
444 pipe
->flush(pipe
, NULL
, 0);
446 nine_context_get_pipe_release(device
);
447 } else if (This
->maps
[This
->nmaps
].should_destroy_buf
)
448 nine_upload_release_buffer(device
->buffer_upload
, This
->maps
[This
->nmaps
].buf
);
454 NineBuffer9_SetDirty( struct NineBuffer9
*This
)
456 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
458 This
->managed
.dirty
= TRUE
;
459 u_box_1d(0, This
->size
, &This
->managed
.dirty_box
);
460 BASEBUF_REGISTER_UPDATE(This
);