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