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#ifndef _NINE_BUFFER9_H_
25#define _NINE_BUFFER9_H_
26
27#include "device9.h"
28#include "nine_buffer_upload.h"
29#include "nine_state.h"
30#include "resource9.h"
31#include "pipe/p_context.h"
32#include "pipe/p_defines.h"
33#include "pipe/p_state.h"
34#include "util/list.h"
35#include "util/u_box.h"
36#include "util/u_upload_mgr.h"
37
38struct pipe_screen;
39struct pipe_context;
40struct pipe_transfer;
41
42struct NineTransfer {
43    struct pipe_transfer *transfer;
44    bool is_pipe_secondary;
45    struct nine_subbuffer *buf; /* NULL unless subbuffer are used */
46    bool should_destroy_buf; /* If the subbuffer should be destroyed */
47};
48
49struct NineBuffer9
50{
51    struct NineResource9 base;
52
53    /* G3D */
54    struct NineTransfer *maps;
55    int nlocks, nmaps, maxmaps;
56    UINT size;
57
58    int16_t bind_count; /* to Device9->state.stream */
59    /* Whether only discard and nooverwrite were used so far
60     * for this buffer. Allows some optimization. */
61    boolean discard_nooverwrite_only;
62    boolean need_sync_if_nooverwrite;
63    struct nine_subbuffer *buf;
64
65    /* Specific to managed buffers */
66    struct {
67        void *data;
68        boolean dirty;
69        struct pipe_box dirty_box; /* region in the resource to update */
70        struct pipe_box upload_pending_regions; /* region with uploads pending */
71        struct list_head list; /* for update_buffers */
72        struct list_head list2; /* for managed_buffers */
73        unsigned pending_upload; /* for uploads */
74        /* SYSTEMMEM DYNAMIC */
75        bool can_unsynchronized; /* Whether the upload can use nooverwrite */
76        struct pipe_box valid_region; /* Region in the GPU buffer with valid content */
77        struct pipe_box required_valid_region; /* Region that needs to be valid right now. */
78        struct pipe_box filled_region; /* Region in the GPU buffer filled since last discard */
79        unsigned num_worker_thread_syncs;
80        unsigned frame_count_last_discard;
81    } managed;
82};
83static inline struct NineBuffer9 *
84NineBuffer9( void *data )
85{
86    return (struct NineBuffer9 *)data;
87}
88
89HRESULT
90NineBuffer9_ctor( struct NineBuffer9 *This,
91                        struct NineUnknownParams *pParams,
92                        D3DRESOURCETYPE Type,
93                        DWORD Usage,
94                        UINT Size,
95                        D3DPOOL Pool );
96
97void
98NineBuffer9_dtor( struct NineBuffer9 *This );
99
100struct pipe_resource *
101NineBuffer9_GetResource( struct NineBuffer9 *This, unsigned *offset );
102
103HRESULT NINE_WINAPI
104NineBuffer9_Lock( struct NineBuffer9 *This,
105                        UINT OffsetToLock,
106                        UINT SizeToLock,
107                        void **ppbData,
108                        DWORD Flags );
109
110HRESULT NINE_WINAPI
111NineBuffer9_Unlock( struct NineBuffer9 *This );
112
113void
114NineBuffer9_Upload( struct NineBuffer9 *This );
115
116static void inline
117NineBindBufferToDevice( struct NineDevice9 *device,
118                        struct NineBuffer9 **slot,
119                        struct NineBuffer9 *buf )
120{
121    struct NineBuffer9 *old = *slot;
122
123    if (buf) {
124        if ((buf->managed.dirty) && list_is_empty(&buf->managed.list))
125            list_add(&buf->managed.list, &device->update_buffers);
126        buf->bind_count++;
127    }
128    if (old) {
129        old->bind_count--;
130        if (!old->bind_count && old->managed.dirty)
131            list_delinit(&old->managed.list);
132    }
133
134    nine_bind(slot, buf);
135}
136
137void
138NineBuffer9_SetDirty( struct NineBuffer9 *This );
139
140#define BASEBUF_REGISTER_UPDATE(b) { \
141    if ((b)->managed.dirty && (b)->bind_count) \
142        if (list_is_empty(&(b)->managed.list)) \
143            list_add(&(b)->managed.list, &(b)->base.base.device->update_buffers); \
144    }
145
146#endif /* _NINE_BUFFER9_H_ */
147