Merge remote branch 'origin/mesa_7_7_branch'
[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 "pipe/p_inlines.h"
29 #include "pipe/p_state.h"
30 #include "util/u_memory.h"
31
32 #include "svga_context.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_range_elements(struct svga_context *svga,
41 struct pipe_buffer *indexBuffer,
42 unsigned indexSize,
43 unsigned min_index,
44 unsigned max_index,
45 unsigned prim, unsigned start, unsigned count)
46 {
47 struct draw_context *draw = svga->swtnl.draw;
48 unsigned i;
49 const void *map;
50 enum pipe_error ret;
51
52 assert(!svga->dirty);
53 assert(svga->state.sw.need_swtnl);
54 assert(draw);
55
56 ret = svga_update_state(svga, SVGA_STATE_SWTNL_DRAW);
57 if (ret) {
58 svga_context_flush(svga, NULL);
59 ret = svga_update_state(svga, SVGA_STATE_SWTNL_DRAW);
60 svga->swtnl.new_vbuf = TRUE;
61 assert(ret == PIPE_OK);
62 }
63
64 /*
65 * Map vertex buffers
66 */
67 for (i = 0; i < svga->curr.num_vertex_buffers; i++) {
68 map = pipe_buffer_map(svga->pipe.screen,
69 svga->curr.vb[i].buffer,
70 PIPE_BUFFER_USAGE_CPU_READ);
71
72 draw_set_mapped_vertex_buffer(draw, i, map);
73 }
74
75 /* Map index buffer, if present */
76 if (indexBuffer) {
77 map = pipe_buffer_map(svga->pipe.screen, indexBuffer,
78 PIPE_BUFFER_USAGE_CPU_READ);
79
80 draw_set_mapped_element_buffer_range(draw,
81 indexSize,
82 min_index,
83 max_index,
84 map);
85 }
86
87 if (svga->curr.cb[PIPE_SHADER_VERTEX]) {
88 map = pipe_buffer_map(svga->pipe.screen,
89 svga->curr.cb[PIPE_SHADER_VERTEX],
90 PIPE_BUFFER_USAGE_CPU_READ);
91 assert(map);
92 draw_set_mapped_constant_buffer(
93 draw, PIPE_SHADER_VERTEX,
94 map,
95 svga->curr.cb[PIPE_SHADER_VERTEX]->size);
96 }
97
98 draw_arrays(svga->swtnl.draw, prim, start, count);
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.screen, svga->curr.vb[i].buffer);
110 draw_set_mapped_vertex_buffer(draw, i, NULL);
111 }
112
113 if (indexBuffer) {
114 pipe_buffer_unmap(svga->pipe.screen, indexBuffer);
115 draw_set_mapped_element_buffer(draw, 0, NULL);
116 }
117
118 if (svga->curr.cb[PIPE_SHADER_VERTEX]) {
119 pipe_buffer_unmap(svga->pipe.screen,
120 svga->curr.cb[PIPE_SHADER_VERTEX]);
121 }
122
123 return ret;
124 }
125
126
127
128
129 boolean svga_init_swtnl( struct svga_context *svga )
130 {
131 svga->swtnl.backend = svga_vbuf_render_create(svga);
132 if(!svga->swtnl.backend)
133 goto fail;
134
135 /*
136 * Create drawing context and plug our rendering stage into it.
137 */
138 svga->swtnl.draw = draw_create();
139 if (svga->swtnl.draw == NULL)
140 goto fail;
141
142
143 draw_set_rasterize_stage(svga->swtnl.draw,
144 draw_vbuf_stage( svga->swtnl.draw, svga->swtnl.backend ));
145
146 draw_set_render(svga->swtnl.draw, svga->swtnl.backend);
147
148 draw_install_aaline_stage(svga->swtnl.draw, &svga->pipe);
149 draw_install_aapoint_stage(svga->swtnl.draw, &svga->pipe);
150 draw_install_pstipple_stage(svga->swtnl.draw, &svga->pipe);
151
152 draw_set_driver_clipping(svga->swtnl.draw, debug_get_bool_option("SVGA_SWTNL_FSE", FALSE));
153
154 return TRUE;
155
156 fail:
157 if (svga->swtnl.backend)
158 svga->swtnl.backend->destroy( svga->swtnl.backend );
159
160 if (svga->swtnl.draw)
161 draw_destroy( svga->swtnl.draw );
162
163 return FALSE;
164 }
165
166
167 void svga_destroy_swtnl( struct svga_context *svga )
168 {
169 draw_destroy( svga->swtnl.draw );
170 }