Merge remote branch 'origin/7.8'
[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_inlines.h"
37 #include "util/u_prim.h"
38
39 #include "sp_context.h"
40 #include "sp_query.h"
41 #include "sp_state.h"
42 #include "sp_texture.h"
43
44 #include "draw/draw_context.h"
45
46
47
48
49
50
51 /**
52 * Draw vertex arrays, with optional indexing.
53 * Basically, map the vertex buffers (and drawing surfaces), then hand off
54 * the drawing to the 'draw' module.
55 */
56 static void
57 softpipe_draw_range_elements_instanced(struct pipe_context *pipe,
58 struct pipe_resource *indexBuffer,
59 unsigned indexSize,
60 int indexBias,
61 unsigned minIndex,
62 unsigned maxIndex,
63 unsigned mode,
64 unsigned start,
65 unsigned count,
66 unsigned startInstance,
67 unsigned instanceCount);
68
69
70 void
71 softpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
72 unsigned start, unsigned count)
73 {
74 softpipe_draw_range_elements_instanced(pipe,
75 NULL,
76 0,
77 0,
78 0,
79 0xffffffff,
80 mode,
81 start,
82 count,
83 0,
84 1);
85 }
86
87
88 void
89 softpipe_draw_range_elements(struct pipe_context *pipe,
90 struct pipe_resource *indexBuffer,
91 unsigned indexSize,
92 int indexBias,
93 unsigned min_index,
94 unsigned max_index,
95 unsigned mode, unsigned start, unsigned count)
96 {
97 softpipe_draw_range_elements_instanced(pipe,
98 indexBuffer,
99 indexSize,
100 indexBias,
101 min_index,
102 max_index,
103 mode,
104 start,
105 count,
106 0,
107 1);
108 }
109
110
111 void
112 softpipe_draw_elements(struct pipe_context *pipe,
113 struct pipe_resource *indexBuffer,
114 unsigned indexSize, int indexBias,
115 unsigned mode, unsigned start, unsigned count)
116 {
117 softpipe_draw_range_elements_instanced(pipe,
118 indexBuffer,
119 indexSize,
120 indexBias,
121 0,
122 0xffffffff,
123 mode,
124 start,
125 count,
126 0,
127 1);
128 }
129
130 void
131 softpipe_draw_arrays_instanced(struct pipe_context *pipe,
132 unsigned mode,
133 unsigned start,
134 unsigned count,
135 unsigned startInstance,
136 unsigned instanceCount)
137 {
138 softpipe_draw_range_elements_instanced(pipe,
139 NULL,
140 0,
141 0,
142 0,
143 0xffffffff,
144 mode,
145 start,
146 count,
147 startInstance,
148 instanceCount);
149 }
150
151 void
152 softpipe_draw_elements_instanced(struct pipe_context *pipe,
153 struct pipe_resource *indexBuffer,
154 unsigned indexSize,
155 int indexBias,
156 unsigned mode,
157 unsigned start,
158 unsigned count,
159 unsigned startInstance,
160 unsigned instanceCount)
161 {
162 softpipe_draw_range_elements_instanced(pipe,
163 indexBuffer,
164 indexSize,
165 indexBias,
166 0,
167 0xffffffff,
168 mode,
169 start,
170 count,
171 startInstance,
172 instanceCount);
173 }
174
175 static void
176 softpipe_draw_range_elements_instanced(struct pipe_context *pipe,
177 struct pipe_resource *indexBuffer,
178 unsigned indexSize,
179 int indexBias,
180 unsigned minIndex,
181 unsigned maxIndex,
182 unsigned mode,
183 unsigned start,
184 unsigned count,
185 unsigned startInstance,
186 unsigned instanceCount)
187 {
188 struct softpipe_context *sp = softpipe_context(pipe);
189 struct draw_context *draw = sp->draw;
190 unsigned i;
191
192 if (!softpipe_check_render_cond(sp))
193 return;
194
195 sp->reduced_api_prim = u_reduced_prim(mode);
196
197 if (sp->dirty) {
198 softpipe_update_derived(sp);
199 }
200
201 softpipe_map_transfers(sp);
202
203 /* Map vertex buffers */
204 for (i = 0; i < sp->num_vertex_buffers; i++) {
205 void *buf = softpipe_resource(sp->vertex_buffer[i].buffer)->data;
206 draw_set_mapped_vertex_buffer(draw, i, buf);
207 }
208
209 /* Map index buffer, if present */
210 if (indexBuffer) {
211 void *mapped_indexes = softpipe_resource(indexBuffer)->data;
212 draw_set_mapped_element_buffer_range(draw,
213 indexSize,
214 indexBias,
215 minIndex,
216 maxIndex,
217 mapped_indexes);
218 } else {
219 /* no index/element buffer */
220 draw_set_mapped_element_buffer_range(draw,
221 0, 0,
222 start,
223 start + count - 1,
224 NULL);
225 }
226
227 /* draw! */
228 draw_arrays_instanced(draw, mode, start, count, startInstance, instanceCount);
229
230 /* unmap vertex/index buffers - will cause draw module to flush */
231 for (i = 0; i < sp->num_vertex_buffers; i++) {
232 draw_set_mapped_vertex_buffer(draw, i, NULL);
233 }
234 if (indexBuffer) {
235 draw_set_mapped_element_buffer(draw, 0, 0, NULL);
236 }
237
238 /*
239 * TODO: Flush only when a user vertex/index buffer is present
240 * (or even better, modify draw module to do this
241 * internally when this condition is seen?)
242 */
243 draw_flush(draw);
244
245 /* Note: leave drawing surfaces mapped */
246 sp->dirty_render_cache = TRUE;
247 }