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 u_box_1d(OffsetToLock
, SizeToLock
, &box
);
236 if (This
->base
.pool
== D3DPOOL_MANAGED
) {
237 /* READONLY doesn't dirty the buffer */
238 /* Tests on Win: READONLY doesn't wait for the upload */
239 if (!(Flags
& D3DLOCK_READONLY
)) {
240 if (!This
->managed
.dirty
) {
241 assert(LIST_IS_EMPTY(&This
->managed
.list
));
242 This
->managed
.dirty
= TRUE
;
243 This
->managed
.dirty_box
= box
;
244 if (p_atomic_read(&This
->managed
.pending_upload
))
245 nine_csmt_process(This
->base
.base
.device
);
247 u_box_union_2d(&This
->managed
.dirty_box
, &This
->managed
.dirty_box
, &box
);
248 /* Tests trying to draw while the buffer is locked show that
249 * MANAGED buffers are made dirty at Lock time */
250 BASEBUF_REGISTER_UPDATE(This
);
252 *ppbData
= (char *)This
->managed
.data
+ OffsetToLock
;
253 DBG("returning pointer %p\n", *ppbData
);
258 /* Driver ddi doc: READONLY is never passed to the device. So it can only
259 * have effect on things handled by the driver (MANAGED pool for example).
260 * Msdn doc: DISCARD and NOOVERWRITE are only for DYNAMIC.
261 * ATI doc: You can use DISCARD and NOOVERWRITE without DYNAMIC.
262 * Msdn doc: D3DLOCK_DONOTWAIT is not among the valid flags for buffers.
263 * Our tests: On win 7 nvidia, D3DLOCK_DONOTWAIT does return
264 * D3DERR_WASSTILLDRAWING if the resource is in use, except for DYNAMIC.
265 * Our tests: some apps do use both DISCARD and NOOVERWRITE at the same
266 * time. On windows it seems to return different pointer, thus indicating
267 * DISCARD is taken into account.
268 * Our tests: SYSTEMMEM doesn't DISCARD */
270 if (This
->base
.pool
== D3DPOOL_SYSTEMMEM
)
271 Flags
&= ~D3DLOCK_DISCARD
;
273 if (Flags
& D3DLOCK_DISCARD
)
274 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
275 else if (Flags
& D3DLOCK_NOOVERWRITE
)
276 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_UNSYNCHRONIZED
;
278 usage
= PIPE_TRANSFER_READ_WRITE
;
279 if (Flags
& D3DLOCK_DONOTWAIT
&& !(This
->base
.usage
& D3DUSAGE_DYNAMIC
))
280 usage
|= PIPE_TRANSFER_DONTBLOCK
;
282 This
->discard_nooverwrite_only
&= !!(Flags
& (D3DLOCK_DISCARD
| D3DLOCK_NOOVERWRITE
));
284 if (This
->nmaps
== This
->maxmaps
) {
285 struct NineTransfer
*newmaps
=
286 REALLOC(This
->maps
, sizeof(struct NineTransfer
)*This
->maxmaps
,
287 sizeof(struct NineTransfer
)*(This
->maxmaps
<< 1));
289 return E_OUTOFMEMORY
;
292 This
->maps
= newmaps
;
295 if (This
->buf
&& !This
->discard_nooverwrite_only
) {
296 struct pipe_box src_box
;
298 struct pipe_resource
*src_res
;
299 DBG("Disabling nine_subbuffer for a buffer having"
300 "used a nine_subbuffer buffer\n");
301 /* Copy buffer content to the buffer resource, which
303 * Note: The behaviour may be different from what is expected
304 * with double lock. However applications can't really make expectations
305 * about double locks, and don't really use them, so that's ok. */
306 src_res
= nine_upload_buffer_resource_and_offset(This
->buf
, &offset
);
307 u_box_1d(offset
, This
->size
, &src_box
);
309 pipe
= NineDevice9_GetPipe(device
);
310 pipe
->resource_copy_region(pipe
, This
->base
.resource
, 0, 0, 0, 0,
311 src_res
, 0, &src_box
);
312 /* Release previous resource */
313 if (This
->nmaps
>= 1)
314 This
->maps
[This
->nmaps
-1].should_destroy_buf
= true;
316 nine_upload_release_buffer(device
->buffer_upload
, This
->buf
);
319 NineBuffer9_RebindIfRequired(This
, device
);
322 This
->maps
[This
->nmaps
].transfer
= NULL
;
323 This
->maps
[This
->nmaps
].is_pipe_secondary
= false;
324 This
->maps
[This
->nmaps
].buf
= NULL
;
325 This
->maps
[This
->nmaps
].should_destroy_buf
= false;
327 if (This
->discard_nooverwrite_only
) {
328 if (This
->buf
&& (Flags
& D3DLOCK_DISCARD
)) {
329 /* Release previous buffer */
330 if (This
->nmaps
>= 1)
331 This
->maps
[This
->nmaps
-1].should_destroy_buf
= true;
333 nine_upload_release_buffer(device
->buffer_upload
, This
->buf
);
338 This
->buf
= nine_upload_create_buffer(device
->buffer_upload
, This
->base
.info
.width0
);
339 NineBuffer9_RebindIfRequired(This
, device
);
343 This
->maps
[This
->nmaps
].buf
= This
->buf
;
345 *ppbData
= nine_upload_buffer_get_map(This
->buf
) + OffsetToLock
;
348 /* Fallback to normal path, and don't try again */
349 This
->discard_nooverwrite_only
= false;
353 /* When csmt is active, we want to avoid stalls as much as possible,
354 * and thus we want to create a new resource on discard and map it
355 * with the secondary pipe, instead of waiting on the main pipe. */
356 if (Flags
& D3DLOCK_DISCARD
&& device
->csmt_active
) {
357 struct pipe_screen
*screen
= NineDevice9_GetScreen(device
);
358 struct pipe_resource
*new_res
= screen
->resource_create(screen
, &This
->base
.info
);
360 /* Use the new resource */
361 pipe_resource_reference(&This
->base
.resource
, new_res
);
362 pipe_resource_reference(&new_res
, NULL
);
363 usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_UNSYNCHRONIZED
;
364 NineBuffer9_RebindIfRequired(This
, device
);
365 This
->maps
[This
->nmaps
].is_pipe_secondary
= TRUE
;
367 } else if (Flags
& D3DLOCK_NOOVERWRITE
&& device
->csmt_active
)
368 This
->maps
[This
->nmaps
].is_pipe_secondary
= TRUE
;
370 if (This
->maps
[This
->nmaps
].is_pipe_secondary
)
371 pipe
= device
->pipe_secondary
;
373 pipe
= NineDevice9_GetPipe(device
);
375 data
= pipe
->transfer_map(pipe
, This
->base
.resource
, 0,
376 usage
, &box
, &This
->maps
[This
->nmaps
].transfer
);
379 DBG("pipe::transfer_map failed\n"
383 usage
, box
.x
, box
.width
);
385 if (Flags
& D3DLOCK_DONOTWAIT
)
386 return D3DERR_WASSTILLDRAWING
;
387 return D3DERR_INVALIDCALL
;
390 DBG("returning pointer %p\n", data
);
398 NineBuffer9_Unlock( struct NineBuffer9
*This
)
400 struct NineDevice9
*device
= This
->base
.base
.device
;
401 struct pipe_context
*pipe
;
402 DBG("This=%p\n", This
);
404 user_assert(This
->nmaps
> 0, D3DERR_INVALIDCALL
);
406 if (This
->base
.pool
!= D3DPOOL_MANAGED
) {
407 if (!This
->maps
[This
->nmaps
].buf
) {
408 pipe
= This
->maps
[This
->nmaps
].is_pipe_secondary
?
409 device
->pipe_secondary
:
410 nine_context_get_pipe_acquire(device
);
411 pipe
->transfer_unmap(pipe
, This
->maps
[This
->nmaps
].transfer
);
412 /* We need to flush in case the driver does implicit copies */
413 if (This
->maps
[This
->nmaps
].is_pipe_secondary
)
414 pipe
->flush(pipe
, NULL
, 0);
416 nine_context_get_pipe_release(device
);
417 } else if (This
->maps
[This
->nmaps
].should_destroy_buf
)
418 nine_upload_release_buffer(device
->buffer_upload
, This
->maps
[This
->nmaps
].buf
);
424 NineBuffer9_SetDirty( struct NineBuffer9
*This
)
426 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
428 This
->managed
.dirty
= TRUE
;
429 u_box_1d(0, This
->size
, &This
->managed
.dirty_box
);
430 BASEBUF_REGISTER_UPDATE(This
);