• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3  * Copyright 2015 Patrick Rudolph <siro@das-labor.org>
4  *
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:
11  *
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
14  * Software.
15  *
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. */
23 
24 #include "buffer9.h"
25 #include "device9.h"
26 #include "nine_buffer_upload.h"
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
29 
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"
37 
38 #define DBG_CHANNEL (DBG_INDEXBUFFER|DBG_VERTEXBUFFER)
39 
40 HRESULT
NineBuffer9_ctor(struct NineBuffer9 * This,struct NineUnknownParams * pParams,D3DRESOURCETYPE Type,DWORD Usage,UINT Size,D3DPOOL Pool)41 NineBuffer9_ctor( struct NineBuffer9 *This,
42                         struct NineUnknownParams *pParams,
43                         D3DRESOURCETYPE Type,
44                         DWORD Usage,
45                         UINT Size,
46                         D3DPOOL Pool )
47 {
48     struct pipe_resource *info = &This->base.info;
49     HRESULT hr;
50 
51     DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This, Size, Usage, Pool);
52 
53     user_assert(Pool != D3DPOOL_SCRATCH, D3DERR_INVALIDCALL);
54 
55     This->maps = MALLOC(sizeof(struct NineTransfer));
56     if (!This->maps)
57         return E_OUTOFMEMORY;
58     This->nmaps = 0;
59     This->maxmaps = 1;
60     This->size = Size;
61 
62     info->screen = pParams->device->screen;
63     info->target = PIPE_BUFFER;
64     info->format = PIPE_FORMAT_R8_UNORM;
65     info->width0 = Size;
66     info->flags = 0;
67 
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;
71 
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
77      *   or not.
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.
85      */
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;
101     else
102         info->usage = PIPE_USAGE_DYNAMIC;
103 
104     /* When Writeonly is not set, we don't want to enable the
105      * optimizations */
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) { } */
118 
119     info->height0 = 1;
120     info->depth0 = 1;
121     info->array_size = 1;
122     info->last_level = 0;
123     info->nr_samples = 0;
124 
125     hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE,
126                             Type, Pool, Usage);
127 
128     if (FAILED(hr))
129         return hr;
130 
131     if (Pool == D3DPOOL_MANAGED) {
132         This->managed.data = align_calloc(
133             nine_format_get_level_alloc_size(This->base.info.format,
134                                              Size, 1, 0), 32);
135         if (!This->managed.data)
136             return E_OUTOFMEMORY;
137         memset(This->managed.data, 0, Size);
138         This->managed.dirty = TRUE;
139         u_box_1d(0, Size, &This->managed.dirty_box);
140         list_inithead(&This->managed.list);
141         list_inithead(&This->managed.list2);
142         list_add(&This->managed.list2, &pParams->device->managed_buffers);
143     }
144 
145     return D3D_OK;
146 }
147 
148 void
NineBuffer9_dtor(struct NineBuffer9 * This)149 NineBuffer9_dtor( struct NineBuffer9 *This )
150 {
151     DBG("This=%p\n", This);
152 
153     if (This->maps) {
154         while (This->nmaps) {
155             NineBuffer9_Unlock(This);
156         }
157         FREE(This->maps);
158     }
159 
160     if (This->base.pool == D3DPOOL_MANAGED) {
161         if (This->managed.data)
162             align_free(This->managed.data);
163         if (This->managed.list.prev != NULL && This->managed.list.next != NULL)
164             list_del(&This->managed.list);
165         if (This->managed.list2.prev != NULL && This->managed.list2.next != NULL)
166             list_del(&This->managed.list2);
167     }
168 
169     if (This->buf)
170         nine_upload_release_buffer(This->base.base.device->buffer_upload, This->buf);
171 
172     NineResource9_dtor(&This->base);
173 }
174 
175 struct pipe_resource *
NineBuffer9_GetResource(struct NineBuffer9 * This,unsigned * offset)176 NineBuffer9_GetResource( struct NineBuffer9 *This, unsigned *offset )
177 {
178     if (This->buf)
179         return nine_upload_buffer_resource_and_offset(This->buf, offset);
180     *offset = 0;
181     return NineResource9_GetResource(&This->base);
182 }
183 
184 static void
NineBuffer9_RebindIfRequired(struct NineBuffer9 * This,struct NineDevice9 * device)185 NineBuffer9_RebindIfRequired( struct NineBuffer9 *This,
186                               struct NineDevice9 *device )
187 {
188     int i;
189 
190     if (!This->bind_count)
191         return;
192     for (i = 0; i < device->caps.MaxStreams; i++) {
193         if (device->state.stream[i] == (struct NineVertexBuffer9 *)This)
194             nine_context_set_stream_source(device, i,
195                                            (struct NineVertexBuffer9 *)This,
196                                            device->state.vtxbuf[i].buffer_offset,
197                                            device->state.vtxbuf[i].stride);
198     }
199     if (device->state.idxbuf == (struct NineIndexBuffer9 *)This)
200         nine_context_set_indices(device, (struct NineIndexBuffer9 *)This);
201 }
202 
203 HRESULT NINE_WINAPI
NineBuffer9_Lock(struct NineBuffer9 * This,UINT OffsetToLock,UINT SizeToLock,void ** ppbData,DWORD Flags)204 NineBuffer9_Lock( struct NineBuffer9 *This,
205                         UINT OffsetToLock,
206                         UINT SizeToLock,
207                         void **ppbData,
208                         DWORD Flags )
209 {
210     struct NineDevice9 *device = This->base.base.device;
211     struct pipe_box box;
212     struct pipe_context *pipe;
213     void *data;
214     unsigned usage;
215 
216     DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
217         This, This->base.resource,
218         OffsetToLock, SizeToLock, Flags);
219 
220     user_assert(ppbData, E_POINTER);
221     user_assert(!(Flags & ~(D3DLOCK_DISCARD |
222                             D3DLOCK_DONOTWAIT |
223                             D3DLOCK_NO_DIRTY_UPDATE |
224                             D3DLOCK_NOSYSLOCK |
225                             D3DLOCK_READONLY |
226                             D3DLOCK_NOOVERWRITE)), D3DERR_INVALIDCALL);
227 
228     if (SizeToLock == 0) {
229         SizeToLock = This->size - OffsetToLock;
230         user_warn(OffsetToLock != 0);
231     }
232 
233     u_box_1d(OffsetToLock, SizeToLock, &box);
234 
235     if (This->base.pool == D3DPOOL_MANAGED) {
236         /* READONLY doesn't dirty the buffer */
237         /* Tests on Win: READONLY doesn't wait for the upload */
238         if (!(Flags & D3DLOCK_READONLY)) {
239             if (!This->managed.dirty) {
240                 assert(LIST_IS_EMPTY(&This->managed.list));
241                 This->managed.dirty = TRUE;
242                 This->managed.dirty_box = box;
243                 if (p_atomic_read(&This->managed.pending_upload))
244                     nine_csmt_process(This->base.base.device);
245             } else
246                 u_box_union_2d(&This->managed.dirty_box, &This->managed.dirty_box, &box);
247             /* Tests trying to draw while the buffer is locked show that
248              * MANAGED buffers are made dirty at Lock time */
249             BASEBUF_REGISTER_UPDATE(This);
250         }
251         *ppbData = (char *)This->managed.data + OffsetToLock;
252         DBG("returning pointer %p\n", *ppbData);
253         This->nmaps++;
254         return D3D_OK;
255     }
256 
257     /* Driver ddi doc: READONLY is never passed to the device. So it can only
258      * have effect on things handled by the driver (MANAGED pool for example).
259      * Msdn doc: DISCARD and NOOVERWRITE are only for DYNAMIC.
260      * ATI doc: You can use DISCARD and NOOVERWRITE without DYNAMIC.
261      * Msdn doc: D3DLOCK_DONOTWAIT is not among the valid flags for buffers.
262      * Our tests: On win 7 nvidia, D3DLOCK_DONOTWAIT does return
263      * D3DERR_WASSTILLDRAWING if the resource is in use, except for DYNAMIC.
264      * Our tests: some apps do use both DISCARD and NOOVERWRITE at the same
265      * time. On windows it seems to return different pointer, thus indicating
266      * DISCARD is taken into account.
267      * Our tests: SYSTEMMEM doesn't DISCARD */
268 
269     if (This->base.pool == D3DPOOL_SYSTEMMEM)
270         Flags &= ~D3DLOCK_DISCARD;
271 
272     if (Flags & D3DLOCK_DISCARD)
273         usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
274     else if (Flags & D3DLOCK_NOOVERWRITE)
275         usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED;
276     else
277         usage = PIPE_TRANSFER_READ_WRITE;
278     if (Flags & D3DLOCK_DONOTWAIT && !(This->base.usage & D3DUSAGE_DYNAMIC))
279         usage |= PIPE_TRANSFER_DONTBLOCK;
280 
281     This->discard_nooverwrite_only &= !!(Flags & (D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE));
282 
283     if (This->nmaps == This->maxmaps) {
284         struct NineTransfer *newmaps =
285             REALLOC(This->maps, sizeof(struct NineTransfer)*This->maxmaps,
286                     sizeof(struct NineTransfer)*(This->maxmaps << 1));
287         if (newmaps == NULL)
288             return E_OUTOFMEMORY;
289 
290         This->maxmaps <<= 1;
291         This->maps = newmaps;
292     }
293 
294     if (This->buf && !This->discard_nooverwrite_only) {
295         struct pipe_box src_box;
296         unsigned offset;
297         struct pipe_resource *src_res;
298         DBG("Disabling nine_subbuffer for a buffer having"
299             "used a nine_subbuffer buffer\n");
300         /* Copy buffer content to the buffer resource, which
301          * we will now use.
302          * Note: The behaviour may be different from what is expected
303          * with double lock. However applications can't really make expectations
304          * about double locks, and don't really use them, so that's ok. */
305         src_res = nine_upload_buffer_resource_and_offset(This->buf, &offset);
306         u_box_1d(offset, This->size, &src_box);
307 
308         pipe = NineDevice9_GetPipe(device);
309         pipe->resource_copy_region(pipe, This->base.resource, 0, 0, 0, 0,
310                                    src_res, 0, &src_box);
311         /* Release previous resource */
312         if (This->nmaps >= 1)
313             This->maps[This->nmaps-1].should_destroy_buf = true;
314         else
315             nine_upload_release_buffer(device->buffer_upload, This->buf);
316         This->buf = NULL;
317         /* Rebind buffer */
318         NineBuffer9_RebindIfRequired(This, device);
319     }
320 
321     This->maps[This->nmaps].transfer = NULL;
322     This->maps[This->nmaps].is_pipe_secondary = false;
323     This->maps[This->nmaps].buf = NULL;
324     This->maps[This->nmaps].should_destroy_buf = false;
325 
326     if (This->discard_nooverwrite_only) {
327         if (This->buf && (Flags & D3DLOCK_DISCARD)) {
328             /* Release previous buffer */
329             if (This->nmaps >= 1)
330                 This->maps[This->nmaps-1].should_destroy_buf = true;
331             else
332                 nine_upload_release_buffer(device->buffer_upload, This->buf);
333             This->buf = NULL;
334         }
335 
336         if (!This->buf) {
337             This->buf = nine_upload_create_buffer(device->buffer_upload, This->base.info.width0);
338             NineBuffer9_RebindIfRequired(This, device);
339         }
340 
341         if (This->buf) {
342             This->maps[This->nmaps].buf = This->buf;
343             This->nmaps++;
344             *ppbData = nine_upload_buffer_get_map(This->buf) + OffsetToLock;
345             return D3D_OK;
346         } else {
347             /* Fallback to normal path, and don't try again */
348             This->discard_nooverwrite_only = false;
349         }
350     }
351 
352     /* When csmt is active, we want to avoid stalls as much as possible,
353      * and thus we want to create a new resource on discard and map it
354      * with the secondary pipe, instead of waiting on the main pipe. */
355     if (Flags & D3DLOCK_DISCARD && device->csmt_active) {
356         struct pipe_screen *screen = NineDevice9_GetScreen(device);
357         struct pipe_resource *new_res = screen->resource_create(screen, &This->base.info);
358         if (new_res) {
359             /* Use the new resource */
360             pipe_resource_reference(&This->base.resource, new_res);
361             pipe_resource_reference(&new_res, NULL);
362             usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED;
363             NineBuffer9_RebindIfRequired(This, device);
364             This->maps[This->nmaps].is_pipe_secondary = TRUE;
365         }
366     } else if (Flags & D3DLOCK_NOOVERWRITE && device->csmt_active)
367         This->maps[This->nmaps].is_pipe_secondary = TRUE;
368 
369     if (This->maps[This->nmaps].is_pipe_secondary)
370         pipe = device->pipe_secondary;
371     else
372         pipe = NineDevice9_GetPipe(device);
373 
374     data = pipe->transfer_map(pipe, This->base.resource, 0,
375                               usage, &box, &This->maps[This->nmaps].transfer);
376 
377     if (!data) {
378         DBG("pipe::transfer_map failed\n"
379             " usage = %x\n"
380             " box.x = %u\n"
381             " box.width = %u\n",
382             usage, box.x, box.width);
383 
384         if (Flags & D3DLOCK_DONOTWAIT)
385             return D3DERR_WASSTILLDRAWING;
386         return D3DERR_INVALIDCALL;
387     }
388 
389     DBG("returning pointer %p\n", data);
390     This->nmaps++;
391     *ppbData = data;
392 
393     return D3D_OK;
394 }
395 
396 HRESULT NINE_WINAPI
NineBuffer9_Unlock(struct NineBuffer9 * This)397 NineBuffer9_Unlock( struct NineBuffer9 *This )
398 {
399     struct NineDevice9 *device = This->base.base.device;
400     struct pipe_context *pipe;
401     DBG("This=%p\n", This);
402 
403     user_assert(This->nmaps > 0, D3DERR_INVALIDCALL);
404     This->nmaps--;
405     if (This->base.pool != D3DPOOL_MANAGED) {
406         if (!This->maps[This->nmaps].buf) {
407             pipe = This->maps[This->nmaps].is_pipe_secondary ?
408                 device->pipe_secondary :
409                 nine_context_get_pipe_acquire(device);
410             pipe->transfer_unmap(pipe, This->maps[This->nmaps].transfer);
411             /* We need to flush in case the driver does implicit copies */
412             if (This->maps[This->nmaps].is_pipe_secondary)
413                 pipe->flush(pipe, NULL, 0);
414             else
415                 nine_context_get_pipe_release(device);
416         } else if (This->maps[This->nmaps].should_destroy_buf)
417             nine_upload_release_buffer(device->buffer_upload, This->maps[This->nmaps].buf);
418     }
419     return D3D_OK;
420 }
421 
422 void
NineBuffer9_SetDirty(struct NineBuffer9 * This)423 NineBuffer9_SetDirty( struct NineBuffer9 *This )
424 {
425     assert(This->base.pool == D3DPOOL_MANAGED);
426 
427     This->managed.dirty = TRUE;
428     u_box_1d(0, This->size, &This->managed.dirty_box);
429     BASEBUF_REGISTER_UPDATE(This);
430 }
431