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