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 "draw/draw_context.h"
27#include "draw/draw_vbuf.h"
28#include "util/u_inlines.h"
29#include "pipe/p_state.h"
30
31#include "svga_context.h"
32#include "svga_screen.h"
33#include "svga_swtnl.h"
34#include "svga_state.h"
35#include "svga_swtnl_private.h"
36
37
38
39enum pipe_error
40svga_swtnl_draw_vbo(struct svga_context *svga,
41                    const struct pipe_draw_info *info,
42                    unsigned drawid_offset,
43                    const struct pipe_draw_indirect_info *indirect,
44                    const struct pipe_draw_start_count_bias *draw_one)
45{
46   struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = { 0 };
47   struct pipe_transfer *ib_transfer = NULL;
48   struct pipe_transfer *cb_transfer[SVGA_MAX_CONST_BUFS] = { 0 };
49   struct draw_context *draw = svga->swtnl.draw;
50   ASSERTED unsigned old_num_vertex_buffers;
51   unsigned i;
52   const void *map;
53   boolean retried;
54
55   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SWTNLDRAWVBO);
56
57   assert(!svga->dirty);
58   assert(svga->state.sw.need_swtnl);
59   assert(draw);
60
61   /* Make sure that the need_swtnl flag does not go away */
62   svga->state.sw.in_swtnl_draw = TRUE;
63
64   SVGA_RETRY_CHECK(svga, svga_update_state(svga, SVGA_STATE_SWTNL_DRAW), retried);
65   if (retried) {
66      svga->swtnl.new_vbuf = TRUE;
67   }
68
69   /*
70    * Map vertex buffers
71    */
72   for (i = 0; i < svga->curr.num_vertex_buffers; i++) {
73      if (svga->curr.vb[i].buffer.resource) {
74         map = pipe_buffer_map(&svga->pipe,
75                               svga->curr.vb[i].buffer.resource,
76                               PIPE_MAP_READ |
77                               PIPE_MAP_UNSYNCHRONIZED,
78                               &vb_transfer[i]);
79
80         draw_set_mapped_vertex_buffer(draw, i, map, ~0);
81      }
82   }
83   old_num_vertex_buffers = svga->curr.num_vertex_buffers;
84
85   /* Map index buffer, if present */
86   map = NULL;
87   if (info->index_size) {
88      if (info->has_user_indices) {
89         map = (ubyte *) info->index.user;
90      } else {
91         map = pipe_buffer_map(&svga->pipe, info->index.resource,
92                               PIPE_MAP_READ |
93                               PIPE_MAP_UNSYNCHRONIZED, &ib_transfer);
94      }
95      draw_set_indexes(draw,
96                       (const ubyte *) map,
97                       info->index_size, ~0);
98   }
99
100   /* Map constant buffers */
101   for (i = 0; i < ARRAY_SIZE(svga->curr.constbufs[PIPE_SHADER_VERTEX]); ++i) {
102      if (svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer == NULL) {
103         continue;
104      }
105
106      map = pipe_buffer_map(&svga->pipe,
107                            svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer,
108                            PIPE_MAP_READ |
109                            PIPE_MAP_UNSYNCHRONIZED,
110                            &cb_transfer[i]);
111      assert(map);
112      draw_set_mapped_constant_buffer(
113         draw, PIPE_SHADER_VERTEX, i,
114         map,
115         svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer->width0);
116   }
117
118   draw_vbo(draw, info, drawid_offset, indirect, draw_one, 1,
119	    svga->patch_vertices);
120
121   draw_flush(svga->swtnl.draw);
122
123   /* Ensure the draw module didn't touch this */
124   assert(old_num_vertex_buffers == svga->curr.num_vertex_buffers);
125
126   /*
127    * unmap vertex/index buffers
128    */
129   for (i = 0; i < svga->curr.num_vertex_buffers; i++) {
130      if (svga->curr.vb[i].buffer.resource) {
131         pipe_buffer_unmap(&svga->pipe, vb_transfer[i]);
132         draw_set_mapped_vertex_buffer(draw, i, NULL, 0);
133      }
134   }
135
136   if (ib_transfer) {
137      pipe_buffer_unmap(&svga->pipe, ib_transfer);
138      draw_set_indexes(draw, NULL, 0, 0);
139   }
140
141   for (i = 0; i < ARRAY_SIZE(svga->curr.constbufs[PIPE_SHADER_VERTEX]); ++i) {
142      if (svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer) {
143         pipe_buffer_unmap(&svga->pipe, cb_transfer[i]);
144      }
145   }
146
147   /* Now safe to remove the need_swtnl flag in any update_state call */
148   svga->state.sw.in_swtnl_draw = FALSE;
149   svga->dirty |= SVGA_NEW_NEED_PIPELINE | SVGA_NEW_NEED_SWVFETCH;
150
151   SVGA_STATS_TIME_POP(svga_sws(svga));
152   return PIPE_OK;
153}
154
155
156boolean
157svga_init_swtnl(struct svga_context *svga)
158{
159   struct svga_screen *screen = svga_screen(svga->pipe.screen);
160
161   svga->swtnl.backend = svga_vbuf_render_create(svga);
162   if (!svga->swtnl.backend)
163      goto fail;
164
165   /*
166    * Create drawing context and plug our rendering stage into it.
167    */
168   svga->swtnl.draw = draw_create(&svga->pipe);
169   if (svga->swtnl.draw == NULL)
170      goto fail;
171
172
173   draw_set_rasterize_stage(svga->swtnl.draw,
174                 draw_vbuf_stage(svga->swtnl.draw, svga->swtnl.backend));
175
176   draw_set_render(svga->swtnl.draw, svga->swtnl.backend);
177
178   svga->blitter = util_blitter_create(&svga->pipe);
179   if (!svga->blitter)
180      goto fail;
181
182   /* must be done before installing Draw stages */
183   util_blitter_cache_all_shaders(svga->blitter);
184
185   if (!screen->haveLineSmooth)
186      draw_install_aaline_stage(svga->swtnl.draw, &svga->pipe);
187
188   /* enable/disable line stipple stage depending on device caps */
189   draw_enable_line_stipple(svga->swtnl.draw, !screen->haveLineStipple);
190
191   /* always install AA point stage */
192   draw_install_aapoint_stage(svga->swtnl.draw, &svga->pipe);
193
194   /* Set wide line threshold above device limit (so we'll never really use it)
195    */
196   draw_wide_line_threshold(svga->swtnl.draw,
197                            MAX2(screen->maxLineWidth,
198                                 screen->maxLineWidthAA));
199
200   if (debug_get_bool_option("SVGA_SWTNL_FSE", FALSE))
201      draw_set_driver_clipping(svga->swtnl.draw, TRUE, TRUE, TRUE, FALSE);
202
203   return TRUE;
204
205fail:
206   if (svga->blitter)
207      util_blitter_destroy(svga->blitter);
208
209   if (svga->swtnl.backend)
210      svga->swtnl.backend->destroy(svga->swtnl.backend);
211
212   if (svga->swtnl.draw)
213      draw_destroy(svga->swtnl.draw);
214
215   return FALSE;
216}
217
218
219void
220svga_destroy_swtnl(struct svga_context *svga)
221{
222   draw_destroy(svga->swtnl.draw);
223}
224