64c79359139ae878dd23c237fbd46aecb04a63eb
[mesa.git] / src / gallium / state_trackers / nine / buffer9.c
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_helpers.h"
27 #include "nine_pipe.h"
28
29 #include "pipe/p_screen.h"
30 #include "pipe/p_context.h"
31 #include "pipe/p_state.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_format.h"
34 #include "util/u_box.h"
35
36 #define DBG_CHANNEL (DBG_INDEXBUFFER|DBG_VERTEXBUFFER)
37
38 HRESULT
39 NineBuffer9_ctor( struct NineBuffer9 *This,
40 struct NineUnknownParams *pParams,
41 D3DRESOURCETYPE Type,
42 DWORD Usage,
43 UINT Size,
44 D3DPOOL Pool )
45 {
46 struct pipe_resource *info = &This->base.info;
47 HRESULT hr;
48
49 DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This, Size, Usage, Pool);
50
51 user_assert(Pool != D3DPOOL_SCRATCH, D3DERR_INVALIDCALL);
52
53 This->maps = MALLOC(sizeof(struct pipe_transfer *));
54 if (!This->maps)
55 return E_OUTOFMEMORY;
56 This->nmaps = 0;
57 This->maxmaps = 1;
58 This->size = Size;
59
60 info->screen = pParams->device->screen;
61 info->target = PIPE_BUFFER;
62 info->format = PIPE_FORMAT_R8_UNORM;
63 info->width0 = Size;
64 info->flags = 0;
65
66 /* Note: WRITEONLY is just tip for resource placement, the resource
67 * can still be read (but slower). */
68 info->bind = PIPE_BIND_VERTEX_BUFFER;
69
70 /* It is hard to find clear information on where to place the buffer in
71 * memory depending on the flag.
72 * MSDN: resources are static, except for those with DYNAMIC, thus why you
73 * can only use DISCARD on them.
74 * ATI doc: The driver has the liberty it wants for having things static
75 * or not.
76 * MANAGED: Ram + uploads to Vram copy at unlock (msdn and nvidia doc say
77 * at first draw call using the buffer)
78 * DEFAULT + Usage = 0 => System memory backing for easy read access
79 * (That doc is very unclear on the details, like whether some copies to
80 * vram copy are involved or not).
81 * DEFAULT + WRITEONLY => Vram
82 * DEFAULT + WRITEONLY + DYNAMIC => Either Vram buffer or GTT_WC, depending on what the driver wants.
83 */
84 if (Pool == D3DPOOL_SYSTEMMEM)
85 info->usage = PIPE_USAGE_STAGING;
86 else if (Pool == D3DPOOL_MANAGED)
87 info->usage = PIPE_USAGE_DEFAULT;
88 else if (Usage & D3DUSAGE_DYNAMIC && Usage & D3DUSAGE_WRITEONLY)
89 info->usage = PIPE_USAGE_STREAM;
90 else if (Usage & D3DUSAGE_WRITEONLY)
91 info->usage = PIPE_USAGE_DEFAULT;
92 /* For the remaining two, PIPE_USAGE_STAGING would probably be
93 * a good fit according to the doc. However it seems rather a mistake
94 * from apps to use these (mistakes that do really happen). Try
95 * to put the flags that are the best compromise between the real
96 * behaviour and what buggy apps should get for better performance. */
97 else if (Usage & D3DUSAGE_DYNAMIC)
98 info->usage = PIPE_USAGE_STREAM;
99 else
100 info->usage = PIPE_USAGE_DYNAMIC;
101
102 /* if (pDesc->Usage & D3DUSAGE_DONOTCLIP) { } */
103 /* if (pDesc->Usage & D3DUSAGE_NONSECURE) { } */
104 /* if (pDesc->Usage & D3DUSAGE_NPATCHES) { } */
105 /* if (pDesc->Usage & D3DUSAGE_POINTS) { } */
106 /* if (pDesc->Usage & D3DUSAGE_RTPATCHES) { } */
107 /* The buffer must be usable with both sw and hw
108 * vertex processing. It is expected to be slower with hw. */
109 if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
110 info->usage = PIPE_USAGE_STAGING;
111 /* if (pDesc->Usage & D3DUSAGE_TEXTAPI) { } */
112
113 info->height0 = 1;
114 info->depth0 = 1;
115 info->array_size = 1;
116 info->last_level = 0;
117 info->nr_samples = 0;
118
119 hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE,
120 Type, Pool, Usage);
121
122 if (FAILED(hr))
123 return hr;
124
125 if (Pool == D3DPOOL_MANAGED) {
126 This->managed.data = align_calloc(
127 nine_format_get_level_alloc_size(This->base.info.format,
128 Size, 1, 0), 32);
129 if (!This->managed.data)
130 return E_OUTOFMEMORY;
131 memset(This->managed.data, 0, Size);
132 This->managed.dirty = TRUE;
133 u_box_1d(0, Size, &This->managed.dirty_box);
134 list_inithead(&This->managed.list);
135 list_inithead(&This->managed.list2);
136 list_add(&This->managed.list, &pParams->device->update_buffers);
137 list_add(&This->managed.list2, &pParams->device->managed_buffers);
138 }
139
140 return D3D_OK;
141 }
142
143 void
144 NineBuffer9_dtor( struct NineBuffer9 *This )
145 {
146 DBG("This=%p\n", This);
147
148 if (This->maps) {
149 while (This->nmaps) {
150 NineBuffer9_Unlock(This);
151 }
152 FREE(This->maps);
153 }
154
155 if (This->base.pool == D3DPOOL_MANAGED) {
156 if (This->managed.data)
157 align_free(This->managed.data);
158 if (This->managed.list.prev != NULL && This->managed.list.next != NULL)
159 list_del(&This->managed.list);
160 if (This->managed.list2.prev != NULL && This->managed.list2.next != NULL)
161 list_del(&This->managed.list2);
162 }
163
164 NineResource9_dtor(&This->base);
165 }
166
167 struct pipe_resource *
168 NineBuffer9_GetResource( struct NineBuffer9 *This )
169 {
170 return NineResource9_GetResource(&This->base);
171 }
172
173 HRESULT NINE_WINAPI
174 NineBuffer9_Lock( struct NineBuffer9 *This,
175 UINT OffsetToLock,
176 UINT SizeToLock,
177 void **ppbData,
178 DWORD Flags )
179 {
180 struct pipe_box box;
181 struct pipe_context *pipe;
182 void *data;
183 unsigned usage;
184
185 DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
186 This, This->base.resource,
187 OffsetToLock, SizeToLock, Flags);
188
189 user_assert(ppbData, E_POINTER);
190 user_assert(!(Flags & ~(D3DLOCK_DISCARD |
191 D3DLOCK_DONOTWAIT |
192 D3DLOCK_NO_DIRTY_UPDATE |
193 D3DLOCK_NOSYSLOCK |
194 D3DLOCK_READONLY |
195 D3DLOCK_NOOVERWRITE)), D3DERR_INVALIDCALL);
196
197 if (SizeToLock == 0) {
198 SizeToLock = This->size - OffsetToLock;
199 user_warn(OffsetToLock != 0);
200 }
201
202 u_box_1d(OffsetToLock, SizeToLock, &box);
203
204 if (This->base.pool == D3DPOOL_MANAGED) {
205 /* READONLY doesn't dirty the buffer */
206 if (!(Flags & D3DLOCK_READONLY)) {
207 if (!This->managed.dirty) {
208 assert(LIST_IS_EMPTY(&This->managed.list));
209 This->managed.dirty = TRUE;
210 This->managed.dirty_box = box;
211 } else {
212 u_box_union_2d(&This->managed.dirty_box, &This->managed.dirty_box, &box);
213 /* Do not upload while we are locking, we'll add it back later */
214 if (!LIST_IS_EMPTY(&This->managed.list))
215 list_delinit(&This->managed.list);
216 }
217 }
218 *ppbData = (char *)This->managed.data + OffsetToLock;
219 DBG("returning pointer %p\n", *ppbData);
220 This->nmaps++;
221 return D3D_OK;
222 }
223
224 /* Driver ddi doc: READONLY is never passed to the device. So it can only
225 * have effect on things handled by the driver (MANAGED pool for example).
226 * Msdn doc: DISCARD and NOOVERWRITE are only for DYNAMIC.
227 * ATI doc: You can use DISCARD and NOOVERWRITE without DYNAMIC.
228 * Msdn doc: D3DLOCK_DONOTWAIT is not among the valid flags for buffers.
229 * Our tests: On win 7 nvidia, D3DLOCK_DONOTWAIT does return
230 * D3DERR_WASSTILLDRAWING if the resource is in use, except for DYNAMIC.
231 * Our tests: some apps do use both DISCARD and NOOVERWRITE at the same
232 * time. On windows it seems to return different pointer, thus indicating
233 * DISCARD is taken into account. */
234
235 if (Flags & D3DLOCK_DISCARD)
236 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
237 else if (Flags & D3DLOCK_NOOVERWRITE)
238 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED;
239 else
240 usage = PIPE_TRANSFER_READ_WRITE;
241 if (Flags & D3DLOCK_DONOTWAIT && !(This->base.usage & D3DUSAGE_DYNAMIC))
242 usage |= PIPE_TRANSFER_DONTBLOCK;
243
244 if (This->nmaps == This->maxmaps) {
245 struct pipe_transfer **newmaps =
246 REALLOC(This->maps, sizeof(struct pipe_transfer *)*This->maxmaps,
247 sizeof(struct pipe_transfer *)*(This->maxmaps << 1));
248 if (newmaps == NULL)
249 return E_OUTOFMEMORY;
250
251 This->maxmaps <<= 1;
252 This->maps = newmaps;
253 }
254
255 pipe = NineDevice9_GetPipe(This->base.base.device);
256 data = pipe->transfer_map(pipe, This->base.resource, 0,
257 usage, &box, &This->maps[This->nmaps]);
258
259 if (!data) {
260 DBG("pipe::transfer_map failed\n"
261 " usage = %x\n"
262 " box.x = %u\n"
263 " box.width = %u\n",
264 usage, box.x, box.width);
265
266 if (Flags & D3DLOCK_DONOTWAIT)
267 return D3DERR_WASSTILLDRAWING;
268 return D3DERR_INVALIDCALL;
269 }
270
271 DBG("returning pointer %p\n", data);
272 This->nmaps++;
273 *ppbData = data;
274
275 return D3D_OK;
276 }
277
278 HRESULT NINE_WINAPI
279 NineBuffer9_Unlock( struct NineBuffer9 *This )
280 {
281 struct pipe_context *pipe;
282 DBG("This=%p\n", This);
283
284 user_assert(This->nmaps > 0, D3DERR_INVALIDCALL);
285 if (This->base.pool != D3DPOOL_MANAGED) {
286 pipe = NineDevice9_GetPipe(This->base.base.device);
287 pipe->transfer_unmap(pipe, This->maps[--(This->nmaps)]);
288 } else {
289 This->nmaps--;
290 /* TODO: Fix this to upload at the first draw call needing the data,
291 * instead of at the next draw call */
292 if (!This->nmaps && This->managed.dirty && LIST_IS_EMPTY(&This->managed.list))
293 list_add(&This->managed.list, &This->base.base.device->update_buffers);
294 }
295 return D3D_OK;
296 }
297
298 void
299 NineBuffer9_SetDirty( struct NineBuffer9 *This )
300 {
301 assert(This->base.pool == D3DPOOL_MANAGED);
302
303 if (!This->managed.dirty) {
304 assert(LIST_IS_EMPTY(&This->managed.list));
305 list_add(&This->managed.list, &This->base.base.device->update_buffers);
306 This->managed.dirty = TRUE;
307 }
308 u_box_1d(0, This->size, &This->managed.dirty_box);
309 }