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