gallium: add pipe_resource::nr_storage_samples, and set it same as nr_samples
[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_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
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 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
149 void
150 NineBuffer9_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
176 struct pipe_resource *
177 NineBuffer9_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
185 static void
186 NineBuffer9_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
204 HRESULT NINE_WINAPI
205 NineBuffer9_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 u_box_1d(OffsetToLock, SizeToLock, &box);
235
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);
246 } else
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);
251 }
252 *ppbData = (char *)This->managed.data + OffsetToLock;
253 DBG("returning pointer %p\n", *ppbData);
254 This->nmaps++;
255 return D3D_OK;
256 }
257
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 */
269
270 if (This->base.pool == D3DPOOL_SYSTEMMEM)
271 Flags &= ~D3DLOCK_DISCARD;
272
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;
277 else
278 usage = PIPE_TRANSFER_READ_WRITE;
279 if (Flags & D3DLOCK_DONOTWAIT && !(This->base.usage & D3DUSAGE_DYNAMIC))
280 usage |= PIPE_TRANSFER_DONTBLOCK;
281
282 This->discard_nooverwrite_only &= !!(Flags & (D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE));
283
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));
288 if (newmaps == NULL)
289 return E_OUTOFMEMORY;
290
291 This->maxmaps <<= 1;
292 This->maps = newmaps;
293 }
294
295 if (This->buf && !This->discard_nooverwrite_only) {
296 struct pipe_box src_box;
297 unsigned offset;
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
302 * we will now use.
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);
308
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;
315 else
316 nine_upload_release_buffer(device->buffer_upload, This->buf);
317 This->buf = NULL;
318 /* Rebind buffer */
319 NineBuffer9_RebindIfRequired(This, device);
320 }
321
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;
326
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;
332 else
333 nine_upload_release_buffer(device->buffer_upload, This->buf);
334 This->buf = NULL;
335 }
336
337 if (!This->buf) {
338 This->buf = nine_upload_create_buffer(device->buffer_upload, This->base.info.width0);
339 NineBuffer9_RebindIfRequired(This, device);
340 }
341
342 if (This->buf) {
343 This->maps[This->nmaps].buf = This->buf;
344 This->nmaps++;
345 *ppbData = nine_upload_buffer_get_map(This->buf) + OffsetToLock;
346 return D3D_OK;
347 } else {
348 /* Fallback to normal path, and don't try again */
349 This->discard_nooverwrite_only = false;
350 }
351 }
352
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);
359 if (new_res) {
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;
366 }
367 } else if (Flags & D3DLOCK_NOOVERWRITE && device->csmt_active)
368 This->maps[This->nmaps].is_pipe_secondary = TRUE;
369
370 if (This->maps[This->nmaps].is_pipe_secondary)
371 pipe = device->pipe_secondary;
372 else
373 pipe = NineDevice9_GetPipe(device);
374
375 data = pipe->transfer_map(pipe, This->base.resource, 0,
376 usage, &box, &This->maps[This->nmaps].transfer);
377
378 if (!data) {
379 DBG("pipe::transfer_map failed\n"
380 " usage = %x\n"
381 " box.x = %u\n"
382 " box.width = %u\n",
383 usage, box.x, box.width);
384
385 if (Flags & D3DLOCK_DONOTWAIT)
386 return D3DERR_WASSTILLDRAWING;
387 return D3DERR_INVALIDCALL;
388 }
389
390 DBG("returning pointer %p\n", data);
391 This->nmaps++;
392 *ppbData = data;
393
394 return D3D_OK;
395 }
396
397 HRESULT NINE_WINAPI
398 NineBuffer9_Unlock( struct NineBuffer9 *This )
399 {
400 struct NineDevice9 *device = This->base.base.device;
401 struct pipe_context *pipe;
402 DBG("This=%p\n", This);
403
404 user_assert(This->nmaps > 0, D3DERR_INVALIDCALL);
405 This->nmaps--;
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);
415 else
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);
419 }
420 return D3D_OK;
421 }
422
423 void
424 NineBuffer9_SetDirty( struct NineBuffer9 *This )
425 {
426 assert(This->base.pool == D3DPOOL_MANAGED);
427
428 This->managed.dirty = TRUE;
429 u_box_1d(0, This->size, &This->managed.dirty_box);
430 BASEBUF_REGISTER_UPDATE(This);
431 }