87312ae1510c5c26c292e728c86a0b01bd3bb81d
[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 "pipe/internal/p_winsys_screen.h"
37 #include "pipe/p_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, vssize, gssize;
53
54 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
55 if (sp->constants[i].buffer && sp->constants[i].buffer->size)
56 sp->mapped_constants[i] = ws->buffer_map(ws, sp->constants[i].buffer,
57 PIPE_BUFFER_USAGE_CPU_READ);
58 }
59
60 if (sp->constants[PIPE_SHADER_VERTEX].buffer)
61 vssize = sp->constants[PIPE_SHADER_VERTEX].buffer->size;
62 else
63 vssize = 0;
64
65 if (sp->constants[PIPE_SHADER_GEOMETRY].buffer)
66 gssize = sp->constants[PIPE_SHADER_GEOMETRY].buffer->size;
67 else
68 gssize = 0;
69
70 draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_VERTEX,
71 sp->mapped_constants[PIPE_SHADER_VERTEX],
72 vssize);
73 draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_GEOMETRY,
74 sp->mapped_constants[PIPE_SHADER_GEOMETRY],
75 gssize);
76 }
77
78
79 static void
80 softpipe_unmap_constant_buffers(struct softpipe_context *sp)
81 {
82 struct pipe_winsys *ws = sp->pipe.winsys;
83 uint i;
84
85 /* really need to flush all prims since the vert/frag shaders const buffers
86 * are going away now.
87 */
88 draw_flush(sp->draw);
89
90 draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_VERTEX, NULL, 0);
91 draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_GEOMETRY, NULL, 0);
92
93 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
94 if (sp->constants[i].buffer && sp->constants[i].buffer->size)
95 ws->buffer_unmap(ws, sp->constants[i].buffer);
96 sp->mapped_constants[i] = NULL;
97 }
98 }
99
100
101 /**
102 * Draw vertex arrays, with optional indexing.
103 * Basically, map the vertex buffers (and drawing surfaces), then hand off
104 * the drawing to the 'draw' module.
105 */
106 static boolean
107 softpipe_draw_range_elements_instanced(struct pipe_context *pipe,
108 struct pipe_buffer *indexBuffer,
109 unsigned indexSize,
110 unsigned minIndex,
111 unsigned maxIndex,
112 unsigned mode,
113 unsigned start,
114 unsigned count,
115 unsigned startInstance,
116 unsigned instanceCount);
117
118
119 boolean
120 softpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
121 unsigned start, unsigned count)
122 {
123 return softpipe_draw_range_elements_instanced(pipe,
124 NULL,
125 0,
126 0,
127 0xffffffff,
128 mode,
129 start,
130 count,
131 0,
132 1);
133 }
134
135
136 boolean
137 softpipe_draw_range_elements(struct pipe_context *pipe,
138 struct pipe_buffer *indexBuffer,
139 unsigned indexSize,
140 unsigned min_index,
141 unsigned max_index,
142 unsigned mode, unsigned start, unsigned count)
143 {
144 return softpipe_draw_range_elements_instanced(pipe,
145 indexBuffer,
146 indexSize,
147 min_index,
148 max_index,
149 mode,
150 start,
151 count,
152 0,
153 1);
154 }
155
156
157 boolean
158 softpipe_draw_elements(struct pipe_context *pipe,
159 struct pipe_buffer *indexBuffer,
160 unsigned indexSize,
161 unsigned mode, unsigned start, unsigned count)
162 {
163 return softpipe_draw_range_elements_instanced(pipe,
164 indexBuffer,
165 indexSize,
166 0,
167 0xffffffff,
168 mode,
169 start,
170 count,
171 0,
172 1);
173 }
174
175 boolean
176 softpipe_draw_arrays_instanced(struct pipe_context *pipe,
177 unsigned mode,
178 unsigned start,
179 unsigned count,
180 unsigned startInstance,
181 unsigned instanceCount)
182 {
183 return softpipe_draw_range_elements_instanced(pipe,
184 NULL,
185 0,
186 0,
187 0xffffffff,
188 mode,
189 start,
190 count,
191 startInstance,
192 instanceCount);
193 }
194
195 boolean
196 softpipe_draw_elements_instanced(struct pipe_context *pipe,
197 struct pipe_buffer *indexBuffer,
198 unsigned indexSize,
199 unsigned mode,
200 unsigned start,
201 unsigned count,
202 unsigned startInstance,
203 unsigned instanceCount)
204 {
205 return softpipe_draw_range_elements_instanced(pipe,
206 indexBuffer,
207 indexSize,
208 0,
209 0xffffffff,
210 mode,
211 start,
212 count,
213 startInstance,
214 instanceCount);
215 }
216
217 static boolean
218 softpipe_draw_range_elements_instanced(struct pipe_context *pipe,
219 struct pipe_buffer *indexBuffer,
220 unsigned indexSize,
221 unsigned minIndex,
222 unsigned maxIndex,
223 unsigned mode,
224 unsigned start,
225 unsigned count,
226 unsigned startInstance,
227 unsigned instanceCount)
228 {
229 struct softpipe_context *sp = softpipe_context(pipe);
230 struct draw_context *draw = sp->draw;
231 unsigned i;
232
233 if (!softpipe_check_render_cond(sp))
234 return TRUE;
235
236 sp->reduced_api_prim = u_reduced_prim(mode);
237
238 if (sp->dirty) {
239 softpipe_update_derived(sp);
240 }
241
242 softpipe_map_transfers(sp);
243 softpipe_map_constant_buffers(sp);
244
245 /* Map vertex buffers */
246 for (i = 0; i < sp->num_vertex_buffers; i++) {
247 void *buf;
248
249 buf = pipe_buffer_map(pipe->screen,
250 sp->vertex_buffer[i].buffer,
251 PIPE_BUFFER_USAGE_CPU_READ);
252 draw_set_mapped_vertex_buffer(draw, i, buf);
253 }
254
255 /* Map index buffer, if present */
256 if (indexBuffer) {
257 void *mapped_indexes;
258
259 mapped_indexes = pipe_buffer_map(pipe->screen,
260 indexBuffer,
261 PIPE_BUFFER_USAGE_CPU_READ);
262 draw_set_mapped_element_buffer_range(draw,
263 indexSize,
264 minIndex,
265 maxIndex,
266 mapped_indexes);
267 } else {
268 /* no index/element buffer */
269 draw_set_mapped_element_buffer_range(draw,
270 0,
271 start,
272 start + count - 1,
273 NULL);
274 }
275
276 /* draw! */
277 draw_arrays_instanced(draw, mode, start, count, startInstance, instanceCount);
278
279 /* unmap vertex/index buffers - will cause draw module to flush */
280 for (i = 0; i < sp->num_vertex_buffers; i++) {
281 draw_set_mapped_vertex_buffer(draw, i, NULL);
282 pipe_buffer_unmap(pipe->screen, sp->vertex_buffer[i].buffer);
283 }
284 if (indexBuffer) {
285 draw_set_mapped_element_buffer(draw, 0, NULL);
286 pipe_buffer_unmap(pipe->screen, indexBuffer);
287 }
288
289 /* Note: leave drawing surfaces mapped */
290 softpipe_unmap_constant_buffers(sp);
291
292 sp->dirty_render_cache = TRUE;
293
294 return TRUE;
295 }