gallium: add PIPE_SHADER_CAP_GLSL_16BIT_TEMPS for LowerPrecisionTemporaries
[mesa.git] / src / gallium / drivers / svga / svga_resource_buffer.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_cmd.h"
27
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_resource.h"
35
36 #include "svga_context.h"
37 #include "svga_screen.h"
38 #include "svga_resource_buffer.h"
39 #include "svga_resource_buffer_upload.h"
40 #include "svga_winsys.h"
41 #include "svga_debug.h"
42
43
44 /**
45 * Determine what buffers eventually need hardware backing.
46 *
47 * Vertex- and index buffers need hardware backing. Constant buffers
48 * do on vgpu10. Staging texture-upload buffers do when they are
49 * supported.
50 */
51 static inline boolean
52 svga_buffer_needs_hw_storage(const struct svga_screen *ss,
53 const struct pipe_resource *template)
54 {
55 unsigned bind_mask = (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER |
56 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_STREAM_OUTPUT |
57 PIPE_BIND_SHADER_BUFFER | PIPE_BIND_COMMAND_ARGS_BUFFER);
58
59 if (ss->sws->have_vgpu10) {
60 /*
61 * Driver-created upload const0- and staging texture upload buffers
62 * tagged with PIPE_BIND_CUSTOM
63 */
64 bind_mask |= PIPE_BIND_CUSTOM;
65 /**
66 * Uniform buffer objects.
67 * Don't create hardware storage for state-tracker constant buffers,
68 * because we frequently map them for reading and writing, and
69 * the length of those buffers are always small, so it is better
70 * to just use system memory.
71 */
72 }
73
74 if (template->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)
75 return TRUE;
76
77 return !!(template->bind & bind_mask);
78 }
79
80 /**
81 * Create a buffer transfer.
82 *
83 * Unlike texture DMAs (which are written immediately to the command buffer and
84 * therefore inherently serialized with other context operations), for buffers
85 * we try to coalesce multiple range mappings (i.e, multiple calls to this
86 * function) into a single DMA command, for better efficiency in command
87 * processing. This means we need to exercise extra care here to ensure that
88 * the end result is exactly the same as if one DMA was used for every mapped
89 * range.
90 */
91 static void *
92 svga_buffer_transfer_map(struct pipe_context *pipe,
93 struct pipe_resource *resource,
94 unsigned level,
95 unsigned usage,
96 const struct pipe_box *box,
97 struct pipe_transfer **ptransfer)
98 {
99 struct svga_context *svga = svga_context(pipe);
100 struct svga_screen *ss = svga_screen(pipe->screen);
101 struct svga_buffer *sbuf = svga_buffer(resource);
102 struct pipe_transfer *transfer;
103 uint8_t *map = NULL;
104 int64_t begin = svga_get_time(svga);
105
106 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_BUFFERTRANSFERMAP);
107
108 assert(box->y == 0);
109 assert(box->z == 0);
110 assert(box->height == 1);
111 assert(box->depth == 1);
112
113 transfer = MALLOC_STRUCT(pipe_transfer);
114 if (!transfer) {
115 goto done;
116 }
117
118 transfer->resource = resource;
119 transfer->level = level;
120 transfer->usage = usage;
121 transfer->box = *box;
122 transfer->stride = 0;
123 transfer->layer_stride = 0;
124
125 if (usage & PIPE_TRANSFER_WRITE) {
126 /* If we write to the buffer for any reason, free any saved translated
127 * vertices.
128 */
129 pipe_resource_reference(&sbuf->translated_indices.buffer, NULL);
130 }
131
132 if ((usage & PIPE_TRANSFER_READ) && sbuf->dirty &&
133 !sbuf->key.coherent && !svga->swc->force_coherent) {
134
135 /* Host-side buffers can only be dirtied with vgpu10 features
136 * (streamout and buffer copy).
137 */
138 assert(svga_have_vgpu10(svga));
139
140 if (!sbuf->user) {
141 (void) svga_buffer_handle(svga, resource, sbuf->bind_flags);
142 }
143
144 if (sbuf->dma.pending) {
145 svga_buffer_upload_flush(svga, sbuf);
146 svga_context_finish(svga);
147 }
148
149 assert(sbuf->handle);
150
151 SVGA_RETRY(svga, SVGA3D_vgpu10_ReadbackSubResource(svga->swc,
152 sbuf->handle, 0));
153 svga->hud.num_readbacks++;
154
155 svga_context_finish(svga);
156
157 sbuf->dirty = FALSE;
158 }
159
160 if (usage & PIPE_TRANSFER_WRITE) {
161 if ((usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) &&
162 !(resource->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT)) {
163 /*
164 * Flush any pending primitives, finish writing any pending DMA
165 * commands, and tell the host to discard the buffer contents on
166 * the next DMA operation.
167 */
168
169 svga_hwtnl_flush_buffer(svga, resource);
170
171 if (sbuf->dma.pending) {
172 svga_buffer_upload_flush(svga, sbuf);
173
174 /*
175 * Instead of flushing the context command buffer, simply discard
176 * the current hwbuf, and start a new one.
177 * With GB objects, the map operation takes care of this
178 * if passed the PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE flag,
179 * and the old backing store is busy.
180 */
181
182 if (!svga_have_gb_objects(svga))
183 svga_buffer_destroy_hw_storage(ss, sbuf);
184 }
185
186 sbuf->map.num_ranges = 0;
187 sbuf->dma.flags.discard = TRUE;
188 }
189
190 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
191 if (!sbuf->map.num_ranges) {
192 /*
193 * No pending ranges to upload so far, so we can tell the host to
194 * not synchronize on the next DMA command.
195 */
196
197 sbuf->dma.flags.unsynchronized = TRUE;
198 }
199 } else {
200 /*
201 * Synchronizing, so flush any pending primitives, finish writing any
202 * pending DMA command, and ensure the next DMA will be done in order.
203 */
204
205 svga_hwtnl_flush_buffer(svga, resource);
206
207 if (sbuf->dma.pending) {
208 svga_buffer_upload_flush(svga, sbuf);
209
210 if (svga_buffer_has_hw_storage(sbuf)) {
211 /*
212 * We have a pending DMA upload from a hardware buffer, therefore
213 * we need to ensure that the host finishes processing that DMA
214 * command before the gallium frontend can start overwriting the
215 * hardware buffer.
216 *
217 * XXX: This could be avoided by tying the hardware buffer to
218 * the transfer (just as done with textures), which would allow
219 * overlapping DMAs commands to be queued on the same context
220 * buffer. However, due to the likelihood of software vertex
221 * processing, it is more convenient to hold on to the hardware
222 * buffer, allowing to quickly access the contents from the CPU
223 * without having to do a DMA download from the host.
224 */
225
226 if (usage & PIPE_TRANSFER_DONTBLOCK) {
227 /*
228 * Flushing the command buffer here will most likely cause
229 * the map of the hwbuf below to block, so preemptively
230 * return NULL here if DONTBLOCK is set to prevent unnecessary
231 * command buffer flushes.
232 */
233
234 FREE(transfer);
235 goto done;
236 }
237
238 svga_context_flush(svga, NULL);
239 }
240 }
241
242 sbuf->dma.flags.unsynchronized = FALSE;
243 }
244 }
245
246 if (!sbuf->swbuf && !svga_buffer_has_hw_storage(sbuf)) {
247 if (svga_buffer_create_hw_storage(ss, sbuf, sbuf->bind_flags) != PIPE_OK) {
248 /*
249 * We can't create a hardware buffer big enough, so create a malloc
250 * buffer instead.
251 */
252 if (0) {
253 debug_printf("%s: failed to allocate %u KB of DMA, "
254 "splitting DMA transfers\n",
255 __FUNCTION__,
256 (sbuf->b.b.width0 + 1023)/1024);
257 }
258
259 sbuf->swbuf = align_malloc(sbuf->b.b.width0, 16);
260 if (!sbuf->swbuf) {
261 FREE(transfer);
262 goto done;
263 }
264 }
265 }
266
267 if (sbuf->swbuf) {
268 /* User/malloc buffer */
269 map = sbuf->swbuf;
270 }
271 else if (svga_buffer_has_hw_storage(sbuf)) {
272 boolean retry;
273
274 map = SVGA_TRY_MAP(svga_buffer_hw_storage_map
275 (svga, sbuf, transfer->usage, &retry), retry);
276 if (map == NULL && retry) {
277 /*
278 * At this point, svga_buffer_get_transfer() has already
279 * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
280 * for this buffer.
281 */
282 svga_retry_enter(svga);
283 svga_context_flush(svga, NULL);
284 map = svga_buffer_hw_storage_map(svga, sbuf, transfer->usage, &retry);
285 svga_retry_exit(svga);
286 }
287 }
288 else {
289 map = NULL;
290 }
291
292 if (map) {
293 ++sbuf->map.count;
294 map += transfer->box.x;
295 *ptransfer = transfer;
296 } else {
297 FREE(transfer);
298 }
299
300 svga->hud.map_buffer_time += (svga_get_time(svga) - begin);
301
302 done:
303 SVGA_STATS_TIME_POP(svga_sws(svga));
304 return map;
305 }
306
307
308 static void
309 svga_buffer_transfer_flush_region(struct pipe_context *pipe,
310 struct pipe_transfer *transfer,
311 const struct pipe_box *box)
312 {
313 struct svga_screen *ss = svga_screen(pipe->screen);
314 struct svga_buffer *sbuf = svga_buffer(transfer->resource);
315 struct svga_context *svga = svga_context(pipe);
316 unsigned offset = transfer->box.x + box->x;
317 unsigned length = box->width;
318
319 assert(transfer->usage & PIPE_TRANSFER_WRITE);
320 assert(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT);
321
322 if (!(svga->swc->force_coherent || sbuf->key.coherent) || sbuf->swbuf) {
323 mtx_lock(&ss->swc_mutex);
324 svga_buffer_add_range(sbuf, offset, offset + length);
325 mtx_unlock(&ss->swc_mutex);
326 }
327 }
328
329
330 static void
331 svga_buffer_transfer_unmap(struct pipe_context *pipe,
332 struct pipe_transfer *transfer)
333 {
334 struct svga_screen *ss = svga_screen(pipe->screen);
335 struct svga_context *svga = svga_context(pipe);
336 struct svga_buffer *sbuf = svga_buffer(transfer->resource);
337
338 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_BUFFERTRANSFERUNMAP);
339
340 mtx_lock(&ss->swc_mutex);
341
342 assert(sbuf->map.count);
343 if (sbuf->map.count) {
344 --sbuf->map.count;
345 }
346
347 if (svga_buffer_has_hw_storage(sbuf)) {
348
349 /* Note: we may wind up flushing here and unmapping other buffers
350 * which leads to recursively locking ss->swc_mutex.
351 */
352 svga_buffer_hw_storage_unmap(svga, sbuf);
353 }
354
355 if (transfer->usage & PIPE_TRANSFER_WRITE) {
356 if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
357 /*
358 * Mapped range not flushed explicitly, so flush the whole buffer,
359 * and tell the host to discard the contents when processing the DMA
360 * command.
361 */
362
363 SVGA_DBG(DEBUG_DMA, "flushing the whole buffer\n");
364
365 sbuf->dma.flags.discard = TRUE;
366
367 if (!(svga->swc->force_coherent || sbuf->key.coherent) || sbuf->swbuf)
368 svga_buffer_add_range(sbuf, 0, sbuf->b.b.width0);
369 }
370
371 if (sbuf->swbuf &&
372 (!sbuf->bind_flags || (sbuf->bind_flags & PIPE_BIND_CONSTANT_BUFFER))) {
373 /*
374 * Since the constant buffer is in system buffer, we need
375 * to set the constant buffer dirty bits, so that the context
376 * can update the changes in the device.
377 * According to the GL spec, buffer bound to other contexts will
378 * have to be explicitly rebound by the user to have the changes take
379 * into effect.
380 */
381 svga->dirty |= SVGA_NEW_CONST_BUFFER;
382 }
383 }
384
385 mtx_unlock(&ss->swc_mutex);
386 FREE(transfer);
387 SVGA_STATS_TIME_POP(svga_sws(svga));
388 }
389
390
391 static void
392 svga_buffer_destroy(struct pipe_screen *screen,
393 struct pipe_resource *buf)
394 {
395 struct svga_screen *ss = svga_screen(screen);
396 struct svga_buffer *sbuf = svga_buffer(buf);
397
398 assert(!p_atomic_read(&buf->reference.count));
399
400 assert(!sbuf->dma.pending);
401
402 if (sbuf->handle)
403 svga_buffer_destroy_host_surface(ss, sbuf);
404
405 if (sbuf->uploaded.buffer)
406 pipe_resource_reference(&sbuf->uploaded.buffer, NULL);
407
408 if (sbuf->hwbuf)
409 svga_buffer_destroy_hw_storage(ss, sbuf);
410
411 if (sbuf->swbuf && !sbuf->user)
412 align_free(sbuf->swbuf);
413
414 pipe_resource_reference(&sbuf->translated_indices.buffer, NULL);
415
416 ss->hud.total_resource_bytes -= sbuf->size;
417 assert(ss->hud.num_resources > 0);
418 if (ss->hud.num_resources > 0)
419 ss->hud.num_resources--;
420
421 FREE(sbuf);
422 }
423
424
425 struct u_resource_vtbl svga_buffer_vtbl =
426 {
427 u_default_resource_get_handle, /* get_handle */
428 svga_buffer_destroy, /* resource_destroy */
429 svga_buffer_transfer_map, /* transfer_map */
430 svga_buffer_transfer_flush_region, /* transfer_flush_region */
431 svga_buffer_transfer_unmap, /* transfer_unmap */
432 };
433
434
435
436 struct pipe_resource *
437 svga_buffer_create(struct pipe_screen *screen,
438 const struct pipe_resource *template)
439 {
440 struct svga_screen *ss = svga_screen(screen);
441 struct svga_buffer *sbuf;
442 unsigned bind_flags;
443
444 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_CREATEBUFFER);
445
446 sbuf = CALLOC_STRUCT(svga_buffer);
447 if (!sbuf)
448 goto error1;
449
450 sbuf->b.b = *template;
451 sbuf->b.vtbl = &svga_buffer_vtbl;
452 pipe_reference_init(&sbuf->b.b.reference, 1);
453 sbuf->b.b.screen = screen;
454 bind_flags = template->bind & ~PIPE_BIND_CUSTOM;
455
456 list_inithead(&sbuf->surfaces);
457
458 if (bind_flags & PIPE_BIND_CONSTANT_BUFFER) {
459 /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
460 * flag set.
461 */
462 if (ss->sws->have_vgpu10) {
463 bind_flags = PIPE_BIND_CONSTANT_BUFFER;
464 }
465 }
466
467 /* Although svga device only requires constant buffer size to be
468 * in multiples of 16, in order to allow bind_flags promotion,
469 * we are mandating all buffer size to be in multiples of 16.
470 */
471 sbuf->b.b.width0 = align(sbuf->b.b.width0, 16);
472
473 if (svga_buffer_needs_hw_storage(ss, template)) {
474
475 /* If the buffer is not used for constant buffer, set
476 * the vertex/index bind flags as well so that the buffer will be
477 * accepted for those uses.
478 * Note that the PIPE_BIND_ flags we get from the gallium frontend are
479 * just a hint about how the buffer may be used. And OpenGL buffer
480 * object may be used for many different things.
481 * Also note that we do not unconditionally set the streamout
482 * bind flag since streamout buffer is an output buffer and
483 * might have performance implication.
484 */
485 if (!(template->bind & PIPE_BIND_CONSTANT_BUFFER) &&
486 !(template->bind & PIPE_BIND_CUSTOM)) {
487 /* Not a constant- or staging buffer.
488 * The buffer may be used for vertex data or indexes.
489 */
490 bind_flags |= (PIPE_BIND_VERTEX_BUFFER |
491 PIPE_BIND_INDEX_BUFFER);
492
493 /* It may be used for shader resource as well. */
494 bind_flags |= PIPE_BIND_SAMPLER_VIEW;
495 }
496
497 if (svga_buffer_create_host_surface(ss, sbuf, bind_flags) != PIPE_OK)
498 goto error2;
499 }
500 else {
501 sbuf->swbuf = align_malloc(sbuf->b.b.width0, 64);
502 if (!sbuf->swbuf)
503 goto error2;
504
505 /* Since constant buffer is usually small, it is much cheaper to
506 * use system memory for the data just as it is being done for
507 * the default constant buffer.
508 */
509 if ((bind_flags & PIPE_BIND_CONSTANT_BUFFER) || !bind_flags)
510 sbuf->use_swbuf = TRUE;
511 }
512
513 debug_reference(&sbuf->b.b.reference,
514 (debug_reference_descriptor)debug_describe_resource, 0);
515
516 sbuf->bind_flags = bind_flags;
517 sbuf->size = util_resource_size(&sbuf->b.b);
518 ss->hud.total_resource_bytes += sbuf->size;
519
520 ss->hud.num_resources++;
521 SVGA_STATS_TIME_POP(ss->sws);
522
523 return &sbuf->b.b;
524
525 error2:
526 FREE(sbuf);
527 error1:
528 SVGA_STATS_TIME_POP(ss->sws);
529 return NULL;
530 }
531
532
533 struct pipe_resource *
534 svga_user_buffer_create(struct pipe_screen *screen,
535 void *ptr,
536 unsigned bytes,
537 unsigned bind)
538 {
539 struct svga_buffer *sbuf;
540 struct svga_screen *ss = svga_screen(screen);
541
542 sbuf = CALLOC_STRUCT(svga_buffer);
543 if (!sbuf)
544 goto no_sbuf;
545
546 pipe_reference_init(&sbuf->b.b.reference, 1);
547 sbuf->b.vtbl = &svga_buffer_vtbl;
548 sbuf->b.b.screen = screen;
549 sbuf->b.b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
550 sbuf->b.b.usage = PIPE_USAGE_IMMUTABLE;
551 sbuf->b.b.bind = bind;
552 sbuf->b.b.width0 = bytes;
553 sbuf->b.b.height0 = 1;
554 sbuf->b.b.depth0 = 1;
555 sbuf->b.b.array_size = 1;
556
557 sbuf->bind_flags = bind;
558 sbuf->swbuf = ptr;
559 sbuf->user = TRUE;
560
561 debug_reference(&sbuf->b.b.reference,
562 (debug_reference_descriptor)debug_describe_resource, 0);
563
564 ss->hud.num_resources++;
565
566 return &sbuf->b.b;
567
568 no_sbuf:
569 return NULL;
570 }