Merge branch 'master' into pipe-video
[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_swtnl.h"
33 #include "svga_state.h"
34 #include "svga_swtnl_private.h"
35
36
37
38 enum pipe_error
39 svga_swtnl_draw_vbo(struct svga_context *svga,
40 const struct pipe_draw_info *info)
41 {
42 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS];
43 struct pipe_transfer *ib_transfer = NULL;
44 struct pipe_transfer *cb_transfer = NULL;
45 struct draw_context *draw = svga->swtnl.draw;
46 unsigned i;
47 const void *map;
48 enum pipe_error ret;
49
50 assert(!svga->dirty);
51 assert(svga->state.sw.need_swtnl);
52 assert(draw);
53
54 ret = svga_update_state(svga, SVGA_STATE_SWTNL_DRAW);
55 if (ret) {
56 svga_context_flush(svga, NULL);
57 ret = svga_update_state(svga, SVGA_STATE_SWTNL_DRAW);
58 svga->swtnl.new_vbuf = TRUE;
59 assert(ret == PIPE_OK);
60 }
61
62 /*
63 * Map vertex buffers
64 */
65 for (i = 0; i < svga->curr.num_vertex_buffers; i++) {
66 map = pipe_buffer_map(&svga->pipe,
67 svga->curr.vb[i].buffer,
68 PIPE_TRANSFER_READ,
69 &vb_transfer[i]);
70
71 draw_set_mapped_vertex_buffer(draw, i, map);
72 }
73
74 /* TODO move this to update_swtnl_draw */
75 draw_set_index_buffer(draw, &svga->curr.ib);
76
77 /* Map index buffer, if present */
78 map = NULL;
79 if (info->indexed && svga->curr.ib.buffer) {
80 map = pipe_buffer_map(&svga->pipe, svga->curr.ib.buffer,
81 PIPE_TRANSFER_READ,
82 &ib_transfer);
83 }
84 draw_set_mapped_index_buffer(draw, map);
85
86 if (svga->curr.cb[PIPE_SHADER_VERTEX]) {
87 map = pipe_buffer_map(&svga->pipe,
88 svga->curr.cb[PIPE_SHADER_VERTEX],
89 PIPE_TRANSFER_READ,
90 &cb_transfer);
91 assert(map);
92 draw_set_mapped_constant_buffer(
93 draw, PIPE_SHADER_VERTEX, 0,
94 map,
95 svga->curr.cb[PIPE_SHADER_VERTEX]->width0);
96 }
97
98 draw_vbo(draw, info);
99
100 draw_flush(svga->swtnl.draw);
101
102 /* Ensure the draw module didn't touch this */
103 assert(i == svga->curr.num_vertex_buffers);
104
105 /*
106 * unmap vertex/index buffers
107 */
108 for (i = 0; i < svga->curr.num_vertex_buffers; i++) {
109 pipe_buffer_unmap(&svga->pipe, svga->curr.vb[i].buffer,
110 vb_transfer[i]);
111 draw_set_mapped_vertex_buffer(draw, i, NULL);
112 }
113
114 if (ib_transfer) {
115 pipe_buffer_unmap(&svga->pipe, svga->curr.ib.buffer, ib_transfer);
116 draw_set_mapped_index_buffer(draw, NULL);
117 }
118
119 if (svga->curr.cb[PIPE_SHADER_VERTEX]) {
120 pipe_buffer_unmap(&svga->pipe,
121 svga->curr.cb[PIPE_SHADER_VERTEX],
122 cb_transfer);
123 }
124
125 return ret;
126 }
127
128
129
130
131 boolean svga_init_swtnl( struct svga_context *svga )
132 {
133 svga->swtnl.backend = svga_vbuf_render_create(svga);
134 if(!svga->swtnl.backend)
135 goto fail;
136
137 /*
138 * Create drawing context and plug our rendering stage into it.
139 */
140 svga->swtnl.draw = draw_create(&svga->pipe);
141 if (svga->swtnl.draw == NULL)
142 goto fail;
143
144
145 draw_set_rasterize_stage(svga->swtnl.draw,
146 draw_vbuf_stage( svga->swtnl.draw, svga->swtnl.backend ));
147
148 draw_set_render(svga->swtnl.draw, svga->swtnl.backend);
149
150 draw_install_aaline_stage(svga->swtnl.draw, &svga->pipe);
151 draw_install_aapoint_stage(svga->swtnl.draw, &svga->pipe);
152 draw_install_pstipple_stage(svga->swtnl.draw, &svga->pipe);
153
154 if (debug_get_bool_option("SVGA_SWTNL_FSE", FALSE))
155 draw_set_driver_clipping(svga->swtnl.draw, TRUE, TRUE);
156
157 return TRUE;
158
159 fail:
160 if (svga->swtnl.backend)
161 svga->swtnl.backend->destroy( svga->swtnl.backend );
162
163 if (svga->swtnl.draw)
164 draw_destroy( svga->swtnl.draw );
165
166 return FALSE;
167 }
168
169
170 void svga_destroy_swtnl( struct svga_context *svga )
171 {
172 draw_destroy( svga->swtnl.draw );
173 }