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
40HRESULT
41NineBuffer9_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    info->nr_storage_samples = 0;
125
126    hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE,
127                            Type, Pool, Usage);
128
129    if (FAILED(hr))
130        return hr;
131
132    if (Pool == D3DPOOL_MANAGED) {
133        This->managed.data = align_calloc(
134            nine_format_get_level_alloc_size(This->base.info.format,
135                                             Size, 1, 0), 32);
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);
144    }
145
146    return D3D_OK;
147}
148
149void
150NineBuffer9_dtor( struct NineBuffer9 *This )
151{
152    DBG("This=%p\n", This);
153
154    if (This->maps) {
155        while (This->nmaps) {
156            NineBuffer9_Unlock(This);
157        }
158        FREE(This->maps);
159    }
160
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);
168    }
169
170    if (This->buf)
171        nine_upload_release_buffer(This->base.base.device->buffer_upload, This->buf);
172
173    NineResource9_dtor(&This->base);
174}
175
176struct pipe_resource *
177NineBuffer9_GetResource( struct NineBuffer9 *This, unsigned *offset )
178{
179    if (This->buf)
180        return nine_upload_buffer_resource_and_offset(This->buf, offset);
181    *offset = 0;
182    return NineResource9_GetResource(&This->base);
183}
184
185static void
186NineBuffer9_RebindIfRequired( struct NineBuffer9 *This,
187                              struct NineDevice9 *device )
188{
189    int i;
190
191    if (!This->bind_count)
192        return;
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);
199    }
200    if (device->state.idxbuf == (struct NineIndexBuffer9 *)This)
201        nine_context_set_indices(device, (struct NineIndexBuffer9 *)This);
202}
203
204HRESULT NINE_WINAPI
205NineBuffer9_Lock( struct NineBuffer9 *This,
206                        UINT OffsetToLock,
207                        UINT SizeToLock,
208                        void **ppbData,
209                        DWORD Flags )
210{
211    struct NineDevice9 *device = This->base.base.device;
212    struct pipe_box box;
213    struct pipe_context *pipe;
214    void *data;
215    unsigned usage;
216
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);
220
221    user_assert(ppbData, E_POINTER);
222    user_assert(!(Flags & ~(D3DLOCK_DISCARD |
223                            D3DLOCK_DONOTWAIT |
224                            D3DLOCK_NO_DIRTY_UPDATE |
225                            D3DLOCK_NOSYSLOCK |
226                            D3DLOCK_READONLY |
227                            D3DLOCK_NOOVERWRITE)), D3DERR_INVALIDCALL);
228
229    if (SizeToLock == 0) {
230        SizeToLock = This->size - OffsetToLock;
231        user_warn(OffsetToLock != 0);
232    }
233
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;
241
242    u_box_1d(OffsetToLock, SizeToLock, &box);
243
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);
254            } else
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);
259        }
260        *ppbData = (char *)This->managed.data + OffsetToLock;
261        DBG("returning pointer %p\n", *ppbData);
262        This->nmaps++;
263        return D3D_OK;
264    }
265
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 */
277
278    if (This->base.pool == D3DPOOL_SYSTEMMEM)
279        Flags &= ~(D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
280
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;
285    else
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;
294
295    This->discard_nooverwrite_only &= !!(Flags & (D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE));
296
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));
301        if (newmaps == NULL)
302            return E_OUTOFMEMORY;
303
304        This->maxmaps <<= 1;
305        This->maps = newmaps;
306    }
307
308    if (This->buf && !This->discard_nooverwrite_only) {
309        struct pipe_box src_box;
310        unsigned offset;
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
315         * we will now use.
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);
321
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;
328        else
329            nine_upload_release_buffer(device->buffer_upload, This->buf);
330        This->buf = NULL;
331        /* Rebind buffer */
332        NineBuffer9_RebindIfRequired(This, device);
333    }
334
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;
339
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;
345            else
346                nine_upload_release_buffer(device->buffer_upload, This->buf);
347            This->buf = NULL;
348        }
349
350        if (!This->buf) {
351            This->buf = nine_upload_create_buffer(device->buffer_upload, This->base.info.width0);
352            NineBuffer9_RebindIfRequired(This, device);
353        }
354
355        if (This->buf) {
356            This->maps[This->nmaps].buf = This->buf;
357            This->nmaps++;
358            *ppbData = nine_upload_buffer_get_map(This->buf) + OffsetToLock;
359            return D3D_OK;
360        } else {
361            /* Fallback to normal path, and don't try again */
362            This->discard_nooverwrite_only = false;
363        }
364    }
365
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;
375
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);
380    }
381    This->need_sync_if_nooverwrite = !(Flags & (D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE));
382
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);
389        if (new_res) {
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;
396        }
397    } else if (Flags & D3DLOCK_NOOVERWRITE && device->csmt_active)
398        This->maps[This->nmaps].is_pipe_secondary = TRUE;
399
400    if (This->maps[This->nmaps].is_pipe_secondary)
401        pipe = device->pipe_secondary;
402    else
403        pipe = NineDevice9_GetPipe(device);
404
405    data = pipe->transfer_map(pipe, This->base.resource, 0,
406                              usage, &box, &This->maps[This->nmaps].transfer);
407
408    if (!data) {
409        DBG("pipe::transfer_map failed\n"
410            " usage = %x\n"
411            " box.x = %u\n"
412            " box.width = %u\n",
413            usage, box.x, box.width);
414
415        if (Flags & D3DLOCK_DONOTWAIT)
416            return D3DERR_WASSTILLDRAWING;
417        return D3DERR_INVALIDCALL;
418    }
419
420    DBG("returning pointer %p\n", data);
421    This->nmaps++;
422    *ppbData = data;
423
424    return D3D_OK;
425}
426
427HRESULT NINE_WINAPI
428NineBuffer9_Unlock( struct NineBuffer9 *This )
429{
430    struct NineDevice9 *device = This->base.base.device;
431    struct pipe_context *pipe;
432    DBG("This=%p\n", This);
433
434    user_assert(This->nmaps > 0, D3DERR_INVALIDCALL);
435    This->nmaps--;
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);
445            else
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);
449    }
450    return D3D_OK;
451}
452
453void
454NineBuffer9_SetDirty( struct NineBuffer9 *This )
455{
456    assert(This->base.pool == D3DPOOL_MANAGED);
457
458    This->managed.dirty = TRUE;
459    u_box_1d(0, This->size, &This->managed.dirty_box);
460    BASEBUF_REGISTER_UPDATE(This);
461}
462