1287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell/**********************************************************
2287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell *
4287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * Permission is hereby granted, free of charge, to any person
5287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * obtaining a copy of this software and associated documentation
6287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * files (the "Software"), to deal in the Software without
7287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * restriction, including without limitation the rights to use, copy,
8287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * modify, merge, publish, distribute, sublicense, and/or sell copies
9287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * of the Software, and to permit persons to whom the Software is
10287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * furnished to do so, subject to the following conditions:
11287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell *
12287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * The above copyright notice and this permission notice shall be
13287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * included in all copies or substantial portions of the Software.
14287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell *
15287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * SOFTWARE.
23287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell *
24287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell **********************************************************/
25287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
26287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "svga_cmd.h"
27287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
28287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "pipe/p_state.h"
29287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "pipe/p_defines.h"
30287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "util/u_inlines.h"
31287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "os/os_thread.h"
32287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "util/u_math.h"
33287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "util/u_memory.h"
34ac114c682413065a5b8baa678733555e94e43120Brian Paul#include "util/u_resource.h"
35287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
36287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "svga_context.h"
37287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "svga_screen.h"
38287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "svga_resource_buffer.h"
39287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "svga_resource_buffer_upload.h"
40287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "svga_winsys.h"
41287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "svga_debug.h"
42287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
43287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
44287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell/**
45287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * Vertex and index buffers need hardware backing.  Constant buffers
46287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * do not.  No other types of buffers currently supported.
47287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell */
48a2a1a5805fd617e7f3cc8be44dd79b50da07ebb9Ilia Mirkinstatic inline boolean
49287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellsvga_buffer_needs_hw_storage(unsigned usage)
50287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
51e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   return (usage & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER |
52e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul                    PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_STREAM_OUTPUT)) != 0;
53287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
54287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
55287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
56ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca/**
57e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca * Create a buffer transfer.
58ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca *
59ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca * Unlike texture DMAs (which are written immediately to the command buffer and
60ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca * therefore inherently serialized with other context operations), for buffers
61ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca * we try to coalesce multiple range mappings (i.e, multiple calls to this
62ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca * function) into a single DMA command, for better efficiency in command
63ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca * processing.  This means we need to exercise extra care here to ensure that
64ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca * the end result is exactly the same as if one DMA was used for every mapped
65ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca * range.
66ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca */
67369e46888904c6d379b8b477d9242cff1608e30eMarek Olšákstatic void *
68369e46888904c6d379b8b477d9242cff1608e30eMarek Olšáksvga_buffer_transfer_map(struct pipe_context *pipe,
69e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca                         struct pipe_resource *resource,
70e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca                         unsigned level,
716bea33008e42040335d5a44359be291f0b5ac24fBrian Paul                         unsigned usage,
72369e46888904c6d379b8b477d9242cff1608e30eMarek Olšák                         const struct pipe_box *box,
73369e46888904c6d379b8b477d9242cff1608e30eMarek Olšák                         struct pipe_transfer **ptransfer)
74287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
75ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca   struct svga_context *svga = svga_context(pipe);
76ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca   struct svga_screen *ss = svga_screen(pipe->screen);
77e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   struct svga_buffer *sbuf = svga_buffer(resource);
78e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   struct pipe_transfer *transfer;
792e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   uint8_t *map = NULL;
80f934117bbb9d58f0f5d9cfb4397fec9c6371ed64Brian Paul   int64_t begin = svga_get_time(svga);
81e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
822e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_BUFFERTRANSFERMAP);
832e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee
84e96d7a1489c1e4ddd66fc5c88c0fc603e1483e32Brian Paul   assert(box->y == 0);
85e96d7a1489c1e4ddd66fc5c88c0fc603e1483e32Brian Paul   assert(box->z == 0);
86e96d7a1489c1e4ddd66fc5c88c0fc603e1483e32Brian Paul   assert(box->height == 1);
87e96d7a1489c1e4ddd66fc5c88c0fc603e1483e32Brian Paul   assert(box->depth == 1);
88e96d7a1489c1e4ddd66fc5c88c0fc603e1483e32Brian Paul
891e5eb79d9a7464482189d7ee8c0f495a31b7f0a3Brian Paul   transfer = MALLOC_STRUCT(pipe_transfer);
9013eb5f596bc8ece3d1805b388aa53917e6158d7bEdward O'Callaghan   if (!transfer) {
912e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee      goto done;
92e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   }
93e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
94e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   transfer->resource = resource;
95e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   transfer->level = level;
96e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   transfer->usage = usage;
97e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   transfer->box = *box;
981e5eb79d9a7464482189d7ee8c0f495a31b7f0a3Brian Paul   transfer->stride = 0;
991e5eb79d9a7464482189d7ee8c0f495a31b7f0a3Brian Paul   transfer->layer_stride = 0;
100287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
1014f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul   if (usage & PIPE_TRANSFER_WRITE) {
1024f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul      /* If we write to the buffer for any reason, free any saved translated
1034f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul       * vertices.
1044f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul       */
1054f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul      pipe_resource_reference(&sbuf->translated_indices.buffer, NULL);
1064f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul   }
1074f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul
108e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   if ((usage & PIPE_TRANSFER_READ) && sbuf->dirty) {
10988a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      enum pipe_error ret;
11088a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul
11188a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      /* Host-side buffers can only be dirtied with vgpu10 features
11288a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul       * (streamout and buffer copy).
113e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       */
11488a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      assert(svga_have_vgpu10(svga));
115e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul
11688a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      if (!sbuf->user) {
11788a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul         (void) svga_buffer_handle(svga, resource);
11888a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      }
11979e343b36a729afb8086b99e4bf15d8c444887c1Charmaine Lee
12088a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      if (sbuf->dma.pending > 0) {
12188a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul         svga_buffer_upload_flush(svga, sbuf);
122e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul         svga_context_finish(svga);
12388a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      }
12488a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul
12588a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      assert(sbuf->handle);
126e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul
12788a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      ret = SVGA3D_vgpu10_ReadbackSubResource(svga->swc, sbuf->handle, 0);
12888a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      if (ret != PIPE_OK) {
12988a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul         svga_context_flush(svga, NULL);
13088a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul         ret = SVGA3D_vgpu10_ReadbackSubResource(svga->swc, sbuf->handle, 0);
13188a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul         assert(ret == PIPE_OK);
132e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      }
13388a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul
13488a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      svga->hud.num_readbacks++;
13588a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul
13688a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      svga_context_finish(svga);
13788a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul
13888a344253c2091f7935b3dfc7fe3be99b76033a3Brian Paul      sbuf->dirty = FALSE;
139e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   }
140e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul
141ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca   if (usage & PIPE_TRANSFER_WRITE) {
142ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca      if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
143ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         /*
1441e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca          * Flush any pending primitives, finish writing any pending DMA
1451e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca          * commands, and tell the host to discard the buffer contents on
1461e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca          * the next DMA operation.
147ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca          */
148ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
1491e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca         svga_hwtnl_flush_buffer(svga, resource);
1501e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca
151ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         if (sbuf->dma.pending) {
152ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca            svga_buffer_upload_flush(svga, sbuf);
153ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
154ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca            /*
155ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca             * Instead of flushing the context command buffer, simply discard
156ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca             * the current hwbuf, and start a new one.
15772b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul             * With GB objects, the map operation takes care of this
15872b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul             * if passed the PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE flag,
15972b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul             * and the old backing store is busy.
160ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca             */
161ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
16272b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul            if (!svga_have_gb_objects(svga))
16372b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul               svga_buffer_destroy_hw_storage(ss, sbuf);
164ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         }
165ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
166ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         sbuf->map.num_ranges = 0;
167ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         sbuf->dma.flags.discard = TRUE;
168ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca      }
169ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
170ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca      if (usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
171ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         if (!sbuf->map.num_ranges) {
172ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca            /*
173ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca             * No pending ranges to upload so far, so we can tell the host to
174ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca             * not synchronize on the next DMA command.
175ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca             */
176ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
177ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca            sbuf->dma.flags.unsynchronized = TRUE;
178ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         }
179ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca      } else {
180ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         /*
1811e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca          * Synchronizing, so flush any pending primitives, finish writing any
1821e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca          * pending DMA command, and ensure the next DMA will be done in order.
183ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca          */
184ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
1851e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca         svga_hwtnl_flush_buffer(svga, resource);
1861e9d8934a1a56e888f07a3571038a3f9205a72aeJosé Fonseca
187ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         if (sbuf->dma.pending) {
188ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca            svga_buffer_upload_flush(svga, sbuf);
189ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
19072b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul            if (svga_buffer_has_hw_storage(sbuf)) {
191ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca               /*
192ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * We have a pending DMA upload from a hardware buffer, therefore
193ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * we need to ensure that the host finishes processing that DMA
194ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * command before the state tracker can start overwriting the
195ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * hardware buffer.
196ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                *
197ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * XXX: This could be avoided by tying the hardware buffer to
198ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * the transfer (just as done with textures), which would allow
199ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * overlapping DMAs commands to be queued on the same context
200ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * buffer. However, due to the likelihood of software vertex
201ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * processing, it is more convenient to hold on to the hardware
202ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * buffer, allowing to quickly access the contents from the CPU
203ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                * without having to do a DMA download from the host.
204ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                */
205ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
206ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca               if (usage & PIPE_TRANSFER_DONTBLOCK) {
207ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                  /*
208ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                   * Flushing the command buffer here will most likely cause
209ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                   * the map of the hwbuf below to block, so preemptively
210ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                   * return NULL here if DONTBLOCK is set to prevent unnecessary
211ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                   * command buffer flushes.
212ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca                   */
213ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
214e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca                  FREE(transfer);
2152e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee                  goto done;
216ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca               }
217ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
218ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca               svga_context_flush(svga, NULL);
219ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca            }
220ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         }
221ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
222ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca         sbuf->dma.flags.unsynchronized = FALSE;
223ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca      }
224ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca   }
225ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca
22672b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul   if (!sbuf->swbuf && !svga_buffer_has_hw_storage(sbuf)) {
227287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      if (svga_buffer_create_hw_storage(ss, sbuf) != PIPE_OK) {
228287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell         /*
229287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell          * We can't create a hardware buffer big enough, so create a malloc
230287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell          * buffer instead.
231287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell          */
2324b6c9b799b769863286461de1ab045cea8484cbaBrian Paul         if (0) {
2334b6c9b799b769863286461de1ab045cea8484cbaBrian Paul            debug_printf("%s: failed to allocate %u KB of DMA, "
2344b6c9b799b769863286461de1ab045cea8484cbaBrian Paul                         "splitting DMA transfers\n",
2354b6c9b799b769863286461de1ab045cea8484cbaBrian Paul                         __FUNCTION__,
2364b6c9b799b769863286461de1ab045cea8484cbaBrian Paul                         (sbuf->b.b.width0 + 1023)/1024);
2374b6c9b799b769863286461de1ab045cea8484cbaBrian Paul         }
238287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
239287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell         sbuf->swbuf = align_malloc(sbuf->b.b.width0, 16);
240e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca         if (!sbuf->swbuf) {
241e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca            FREE(transfer);
2422e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee            goto done;
243e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca         }
244287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      }
245287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
246287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
247287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   if (sbuf->swbuf) {
248287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      /* User/malloc buffer */
249287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      map = sbuf->swbuf;
250287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
25172b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul   else if (svga_buffer_has_hw_storage(sbuf)) {
25272b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul      boolean retry;
253e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
25472b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul      map = svga_buffer_hw_storage_map(svga, sbuf, transfer->usage, &retry);
25572b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul      if (map == NULL && retry) {
25672b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul         /*
25772b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul          * At this point, svga_buffer_get_transfer() has already
25872b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul          * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
25972b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul          * for this buffer.
26072b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul          */
26172b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul         svga_context_flush(svga, NULL);
26272b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul         map = svga_buffer_hw_storage_map(svga, sbuf, transfer->usage, &retry);
26372b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul      }
264287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
265287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   else {
266287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      map = NULL;
267287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
268287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
269e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   if (map) {
270287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      ++sbuf->map.count;
271e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca      map += transfer->box.x;
272369e46888904c6d379b8b477d9242cff1608e30eMarek Olšák      *ptransfer = transfer;
2732aa783318dd40d9ba82b541c1af144f3103b0888Vinson Lee   } else {
2742aa783318dd40d9ba82b541c1af144f3103b0888Vinson Lee      FREE(transfer);
275287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
27670c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
277f934117bbb9d58f0f5d9cfb4397fec9c6371ed64Brian Paul   svga->hud.map_buffer_time += (svga_get_time(svga) - begin);
2789bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende
2792e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Leedone:
2802e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   SVGA_STATS_TIME_POP(svga_sws(svga));
281287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   return map;
282287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
283287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
284287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
285e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonsecastatic void
286e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonsecasvga_buffer_transfer_flush_region( struct pipe_context *pipe,
287e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca                                   struct pipe_transfer *transfer,
288e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca                                   const struct pipe_box *box)
289287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
290ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca   struct svga_screen *ss = svga_screen(pipe->screen);
291e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   struct svga_buffer *sbuf = svga_buffer(transfer->resource);
292e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
293e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   unsigned offset = transfer->box.x + box->x;
294e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   unsigned length = box->width;
295e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
296e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   assert(transfer->usage & PIPE_TRANSFER_WRITE);
297e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   assert(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT);
298e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
299287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   pipe_mutex_lock(ss->swc_mutex);
300e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   svga_buffer_add_range(sbuf, offset, offset + length);
301287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   pipe_mutex_unlock(ss->swc_mutex);
302287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
303287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
304e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
305e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonsecastatic void
306e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonsecasvga_buffer_transfer_unmap( struct pipe_context *pipe,
307e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca                            struct pipe_transfer *transfer )
308287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
309ef33c82bfde788733aabc5dcc9647416c31ae254José Fonseca   struct svga_screen *ss = svga_screen(pipe->screen);
31072b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul   struct svga_context *svga = svga_context(pipe);
311e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   struct svga_buffer *sbuf = svga_buffer(transfer->resource);
31270c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
3132e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_BUFFERTRANSFERUNMAP);
3142e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee
315287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   pipe_mutex_lock(ss->swc_mutex);
31670c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
317287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   assert(sbuf->map.count);
318e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   if (sbuf->map.count) {
319287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      --sbuf->map.count;
320e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   }
321287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
32272b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul   if (svga_buffer_has_hw_storage(sbuf)) {
32372b0e959fc38cf4f01d8aaeabe7336cc88588f90Brian Paul      svga_buffer_hw_storage_unmap(svga, sbuf);
324e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   }
325287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
326e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   if (transfer->usage & PIPE_TRANSFER_WRITE) {
327e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca      if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
328b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca         /*
329b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca          * Mapped range not flushed explicitly, so flush the whole buffer,
330b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca          * and tell the host to discard the contents when processing the DMA
331b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca          * command.
332b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca          */
333b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca
334287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell         SVGA_DBG(DEBUG_DMA, "flushing the whole buffer\n");
33570c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
336b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca         sbuf->dma.flags.discard = TRUE;
337b0fff8d17eb65368db018f6ccc226482ccf46763José Fonseca
338287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell         svga_buffer_add_range(sbuf, 0, sbuf->b.b.width0);
339287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      }
340287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
341287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
342287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   pipe_mutex_unlock(ss->swc_mutex);
343e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca   FREE(transfer);
3442e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   SVGA_STATS_TIME_POP(svga_sws(svga));
345e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca}
346e08a3d674ad577fca24b69dd02b3f97b2e6b68d2José Fonseca
347287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
348287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellstatic void
349287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellsvga_buffer_destroy( struct pipe_screen *screen,
350287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell		     struct pipe_resource *buf )
351287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
352287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   struct svga_screen *ss = svga_screen(screen);
353287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   struct svga_buffer *sbuf = svga_buffer( buf );
354287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
355287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   assert(!p_atomic_read(&buf->reference.count));
35670c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
357287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   assert(!sbuf->dma.pending);
358287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
35970c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   if (sbuf->handle)
360287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      svga_buffer_destroy_host_surface(ss, sbuf);
36170c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
36270c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   if (sbuf->uploaded.buffer)
363287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      pipe_resource_reference(&sbuf->uploaded.buffer, NULL);
364287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
36570c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   if (sbuf->hwbuf)
366287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      svga_buffer_destroy_hw_storage(ss, sbuf);
36770c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
36870c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   if (sbuf->swbuf && !sbuf->user)
369287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      align_free(sbuf->swbuf);
37070c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
3714f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul   pipe_resource_reference(&sbuf->translated_indices.buffer, NULL);
3724f74b379aa05e133cdd19865662eceacee4f63f4Brian Paul
3739bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende   ss->hud.total_resource_bytes -= sbuf->size;
3749bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende   assert(ss->hud.num_resources > 0);
3759bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende   if (ss->hud.num_resources > 0)
3769bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende      ss->hud.num_resources--;
377ac114c682413065a5b8baa678733555e94e43120Brian Paul
378287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   FREE(sbuf);
379287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
380287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
381287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
38270c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paulstruct u_resource_vtbl svga_buffer_vtbl =
383287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
384287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   u_default_resource_get_handle,      /* get_handle */
385287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   svga_buffer_destroy,		     /* resource_destroy */
386287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   svga_buffer_transfer_map,	     /* transfer_map */
387287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   svga_buffer_transfer_flush_region,  /* transfer_flush_region */
388287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   svga_buffer_transfer_unmap,	     /* transfer_unmap */
389287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell};
390287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
391287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
392287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
393287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellstruct pipe_resource *
394287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellsvga_buffer_create(struct pipe_screen *screen,
395287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell		   const struct pipe_resource *template)
396287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
397287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   struct svga_screen *ss = svga_screen(screen);
398287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   struct svga_buffer *sbuf;
39970c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
4002e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_CREATEBUFFER);
4012e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee
402287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf = CALLOC_STRUCT(svga_buffer);
40370c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   if (!sbuf)
404287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      goto error1;
40570c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
406287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b = *template;
407287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.vtbl = &svga_buffer_vtbl;
408287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   pipe_reference_init(&sbuf->b.b.reference, 1);
409287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b.screen = screen;
410e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   sbuf->bind_flags = template->bind;
411e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul
412e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   if (template->bind & PIPE_BIND_CONSTANT_BUFFER) {
413e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
414e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       * flag set.
415e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       */
416e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      if (ss->sws->have_vgpu10) {
417e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul         sbuf->bind_flags = PIPE_BIND_CONSTANT_BUFFER;
418e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul
419e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul         /* Constant buffer size needs to be in multiples of 16. */
420e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul         sbuf->b.b.width0 = align(sbuf->b.b.width0, 16);
421e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      }
422e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   }
423287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
42470c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   if (svga_buffer_needs_hw_storage(template->bind)) {
425e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul
426e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      /* If the buffer will be used for vertex/index/stream data, set all
427e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       * the flags so that the buffer will be accepted for all those uses.
428e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       * Note that the PIPE_BIND_ flags we get from the state tracker are
429e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       * just a hint about how the buffer may be used.  And OpenGL buffer
430e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       * object may be used for many different things.
431e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul       */
432e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      if (!(template->bind & PIPE_BIND_CONSTANT_BUFFER)) {
433e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul         /* Not a constant buffer.  The buffer may be used for vertex data,
434e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul          * indexes or stream-out.
435e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul          */
436e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul         sbuf->bind_flags |= (PIPE_BIND_VERTEX_BUFFER |
437e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul                              PIPE_BIND_INDEX_BUFFER);
438e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul         if (ss->sws->have_vgpu10)
439e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul            sbuf->bind_flags |= PIPE_BIND_STREAM_OUTPUT;
440e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      }
441e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul
44270c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul      if (svga_buffer_create_host_surface(ss, sbuf) != PIPE_OK)
443287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell         goto error2;
444287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
445287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   else {
446e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul      sbuf->swbuf = align_malloc(sbuf->b.b.width0, 64);
44770c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul      if (!sbuf->swbuf)
448287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell         goto error2;
449287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   }
45070c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
45199d955263ba4665462f8ebd8ed643019c2cd9621Jakob Bornecrantz   debug_reference(&sbuf->b.b.reference,
45299d955263ba4665462f8ebd8ed643019c2cd9621Jakob Bornecrantz                   (debug_reference_descriptor)debug_describe_resource, 0);
45399d955263ba4665462f8ebd8ed643019c2cd9621Jakob Bornecrantz
454e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   sbuf->size = util_resource_size(&sbuf->b.b);
4559bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende   ss->hud.total_resource_bytes += sbuf->size;
4569bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende
4579bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende   ss->hud.num_resources++;
4582e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   SVGA_STATS_TIME_POP(ss->sws);
459ac114c682413065a5b8baa678733555e94e43120Brian Paul
46070c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   return &sbuf->b.b;
461287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
462287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellerror2:
463287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   FREE(sbuf);
464287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellerror1:
4652e1cfcc431471c68ba79c9323716bed7da79c909Charmaine Lee   SVGA_STATS_TIME_POP(ss->sws);
466287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   return NULL;
467287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
468287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
46970c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
470287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellstruct pipe_resource *
471287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellsvga_user_buffer_create(struct pipe_screen *screen,
472287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell                        void *ptr,
473287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell                        unsigned bytes,
474287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell			unsigned bind)
475287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
476287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   struct svga_buffer *sbuf;
4779bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende   struct svga_screen *ss = svga_screen(screen);
47870c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
479287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf = CALLOC_STRUCT(svga_buffer);
48070c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   if (!sbuf)
481287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      goto no_sbuf;
48270c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
483287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   pipe_reference_init(&sbuf->b.b.reference, 1);
484287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.vtbl = &svga_buffer_vtbl;
485287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b.screen = screen;
486287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
487a2a01853f3f40b4ef8b3f01503391877960bdaeeBrian Paul   sbuf->b.b.usage = PIPE_USAGE_IMMUTABLE;
488287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b.bind = bind;
489287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b.width0 = bytes;
490287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b.height0 = 1;
491287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->b.b.depth0 = 1;
4924c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger   sbuf->b.b.array_size = 1;
493287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
494e054251ed11e25a080f64b92db9334c9b07c8c76Brian Paul   sbuf->bind_flags = bind;
495287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->swbuf = ptr;
496287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   sbuf->user = TRUE;
49799d955263ba4665462f8ebd8ed643019c2cd9621Jakob Bornecrantz
49899d955263ba4665462f8ebd8ed643019c2cd9621Jakob Bornecrantz   debug_reference(&sbuf->b.b.reference,
49999d955263ba4665462f8ebd8ed643019c2cd9621Jakob Bornecrantz                   (debug_reference_descriptor)debug_describe_resource, 0);
50070c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul
5019bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende   ss->hud.num_resources++;
5029bc7e3105aeadbe360ca9f060c50a181d3fa7a3dNeha Bhende
50370c4cde453bc12be5262b88dcb26e97dcb8e0507Brian Paul   return &sbuf->b.b;
504287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
505287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellno_sbuf:
506287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   return NULL;
507287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
508287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
509287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
510287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
511