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