b2acc36bf7af2672c58f2b699071872cd960e3de
[mesa.git] / src / gallium / drivers / softpipe / sp_draw_arrays.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Author:
29 * Brian Paul
30 * Keith Whitwell
31 */
32
33
34 #include "pipe/p_defines.h"
35 #include "pipe/p_context.h"
36 #include "util/u_simple_screen.h"
37 #include "util/u_inlines.h"
38 #include "util/u_prim.h"
39
40 #include "sp_context.h"
41 #include "sp_query.h"
42 #include "sp_state.h"
43
44 #include "draw/draw_context.h"
45
46
47
48 static void
49 softpipe_map_constant_buffers(struct softpipe_context *sp)
50 {
51 struct pipe_winsys *ws = sp->pipe.winsys;
52 uint i;
53
54 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
55 uint j;
56
57 for (j = 0; j < PIPE_MAX_CONSTANT_BUFFERS; j++) {
58 if (sp->constants[i][j] && sp->constants[i][j]->size) {
59 sp->mapped_constants[i][j] = ws->buffer_map(ws,
60 sp->constants[i][j],
61 PIPE_BUFFER_USAGE_CPU_READ);
62 }
63 }
64 }
65
66 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
67 if (sp->constants[PIPE_SHADER_VERTEX][i]) {
68 draw_set_mapped_constant_buffer(sp->draw,
69 PIPE_SHADER_VERTEX,
70 i,
71 sp->mapped_constants[PIPE_SHADER_VERTEX][i],
72 sp->constants[PIPE_SHADER_VERTEX][i]->size);
73 }
74 if (sp->constants[PIPE_SHADER_GEOMETRY][i]) {
75 draw_set_mapped_constant_buffer(sp->draw,
76 PIPE_SHADER_GEOMETRY,
77 i,
78 sp->mapped_constants[PIPE_SHADER_GEOMETRY][i],
79 sp->constants[PIPE_SHADER_GEOMETRY][i]->size);
80 }
81 }
82 }
83
84
85 static void
86 softpipe_unmap_constant_buffers(struct softpipe_context *sp)
87 {
88 struct pipe_winsys *ws = sp->pipe.winsys;
89 uint i;
90
91 /* really need to flush all prims since the vert/frag shaders const buffers
92 * are going away now.
93 */
94 draw_flush(sp->draw);
95
96 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
97 draw_set_mapped_constant_buffer(sp->draw,
98 PIPE_SHADER_VERTEX,
99 i,
100 NULL,
101 0);
102 draw_set_mapped_constant_buffer(sp->draw,
103 PIPE_SHADER_GEOMETRY,
104 i,
105 NULL,
106 0);
107 }
108
109 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
110 uint j;
111
112 for (j = 0; j < PIPE_MAX_CONSTANT_BUFFERS; j++) {
113 if (sp->constants[i][j] && sp->constants[i][j]->size) {
114 ws->buffer_unmap(ws, sp->constants[i][j]);
115 }
116 sp->mapped_constants[i][j] = NULL;
117 }
118 }
119 }
120
121
122 /**
123 * Draw vertex arrays, with optional indexing.
124 * Basically, map the vertex buffers (and drawing surfaces), then hand off
125 * the drawing to the 'draw' module.
126 */
127 static void
128 softpipe_draw_range_elements_instanced(struct pipe_context *pipe,
129 struct pipe_buffer *indexBuffer,
130 unsigned indexSize,
131 unsigned minIndex,
132 unsigned maxIndex,
133 unsigned mode,
134 unsigned start,
135 unsigned count,
136 unsigned startInstance,
137 unsigned instanceCount);
138
139
140 void
141 softpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
142 unsigned start, unsigned count)
143 {
144 softpipe_draw_range_elements_instanced(pipe,
145 NULL,
146 0,
147 0,
148 0xffffffff,
149 mode,
150 start,
151 count,
152 0,
153 1);
154 }
155
156
157 void
158 softpipe_draw_range_elements(struct pipe_context *pipe,
159 struct pipe_buffer *indexBuffer,
160 unsigned indexSize,
161 unsigned min_index,
162 unsigned max_index,
163 unsigned mode, unsigned start, unsigned count)
164 {
165 softpipe_draw_range_elements_instanced(pipe,
166 indexBuffer,
167 indexSize,
168 min_index,
169 max_index,
170 mode,
171 start,
172 count,
173 0,
174 1);
175 }
176
177
178 void
179 softpipe_draw_elements(struct pipe_context *pipe,
180 struct pipe_buffer *indexBuffer,
181 unsigned indexSize,
182 unsigned mode, unsigned start, unsigned count)
183 {
184 softpipe_draw_range_elements_instanced(pipe,
185 indexBuffer,
186 indexSize,
187 0,
188 0xffffffff,
189 mode,
190 start,
191 count,
192 0,
193 1);
194 }
195
196 void
197 softpipe_draw_arrays_instanced(struct pipe_context *pipe,
198 unsigned mode,
199 unsigned start,
200 unsigned count,
201 unsigned startInstance,
202 unsigned instanceCount)
203 {
204 softpipe_draw_range_elements_instanced(pipe,
205 NULL,
206 0,
207 0,
208 0xffffffff,
209 mode,
210 start,
211 count,
212 startInstance,
213 instanceCount);
214 }
215
216 void
217 softpipe_draw_elements_instanced(struct pipe_context *pipe,
218 struct pipe_buffer *indexBuffer,
219 unsigned indexSize,
220 unsigned mode,
221 unsigned start,
222 unsigned count,
223 unsigned startInstance,
224 unsigned instanceCount)
225 {
226 softpipe_draw_range_elements_instanced(pipe,
227 indexBuffer,
228 indexSize,
229 0,
230 0xffffffff,
231 mode,
232 start,
233 count,
234 startInstance,
235 instanceCount);
236 }
237
238 static void
239 softpipe_draw_range_elements_instanced(struct pipe_context *pipe,
240 struct pipe_buffer *indexBuffer,
241 unsigned indexSize,
242 unsigned minIndex,
243 unsigned maxIndex,
244 unsigned mode,
245 unsigned start,
246 unsigned count,
247 unsigned startInstance,
248 unsigned instanceCount)
249 {
250 struct softpipe_context *sp = softpipe_context(pipe);
251 struct draw_context *draw = sp->draw;
252 unsigned i;
253
254 if (!softpipe_check_render_cond(sp))
255 return;
256
257 sp->reduced_api_prim = u_reduced_prim(mode);
258
259 if (sp->dirty) {
260 softpipe_update_derived(sp);
261 }
262
263 softpipe_map_transfers(sp);
264 softpipe_map_constant_buffers(sp);
265
266 /* Map vertex buffers */
267 for (i = 0; i < sp->num_vertex_buffers; i++) {
268 void *buf;
269
270 buf = pipe_buffer_map(pipe->screen,
271 sp->vertex_buffer[i].buffer,
272 PIPE_BUFFER_USAGE_CPU_READ);
273 draw_set_mapped_vertex_buffer(draw, i, buf);
274 }
275
276 /* Map index buffer, if present */
277 if (indexBuffer) {
278 void *mapped_indexes;
279
280 mapped_indexes = pipe_buffer_map(pipe->screen,
281 indexBuffer,
282 PIPE_BUFFER_USAGE_CPU_READ);
283 draw_set_mapped_element_buffer_range(draw,
284 indexSize,
285 minIndex,
286 maxIndex,
287 mapped_indexes);
288 } else {
289 /* no index/element buffer */
290 draw_set_mapped_element_buffer_range(draw,
291 0,
292 start,
293 start + count - 1,
294 NULL);
295 }
296
297 /* draw! */
298 draw_arrays_instanced(draw, mode, start, count, startInstance, instanceCount);
299
300 /* unmap vertex/index buffers - will cause draw module to flush */
301 for (i = 0; i < sp->num_vertex_buffers; i++) {
302 draw_set_mapped_vertex_buffer(draw, i, NULL);
303 pipe_buffer_unmap(pipe->screen, sp->vertex_buffer[i].buffer);
304 }
305 if (indexBuffer) {
306 draw_set_mapped_element_buffer(draw, 0, NULL);
307 pipe_buffer_unmap(pipe->screen, indexBuffer);
308 }
309
310 /* Note: leave drawing surfaces mapped */
311 softpipe_unmap_constant_buffers(sp);
312
313 sp->dirty_render_cache = TRUE;
314 }