r300g: attempt to fix texture corruption on RV505
[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 void
88 softpipe_draw_stream_output(struct pipe_context *pipe, unsigned mode)
89 {
90 struct softpipe_context *sp = softpipe_context(pipe);
91 struct draw_context *draw = sp->draw;
92 const unsigned start = 0;
93 const unsigned count = sp->so_target.so_count[0];
94 void *buf = sp->so_target.buffer[0]->data;
95 int offset = sp->so_target.offset[0];
96
97 if (!softpipe_check_render_cond(sp) ||
98 sp->so_target.num_buffers != 1)
99 return;
100
101 sp->reduced_api_prim = u_reduced_prim(mode);
102
103 if (sp->dirty) {
104 softpipe_update_derived(sp);
105 }
106
107 softpipe_map_transfers(sp);
108
109 /* Map so buffers */
110 if (offset < 0) /* we were appending so start from beginning */
111 offset = 0;
112 buf = (void*)((int32_t*)buf + offset);
113 draw_set_mapped_vertex_buffer(draw, 0, buf);
114
115 draw_set_mapped_element_buffer_range(draw,
116 0, 0,
117 start,
118 start + count - 1,
119 NULL);
120
121 /* draw! */
122 draw_arrays_instanced(draw, mode, start, count, 0, 1);
123
124 /* unmap vertex/index buffers - will cause draw module to flush */
125 draw_set_mapped_vertex_buffer(draw, 0, NULL);
126
127 /*
128 * TODO: Flush only when a user vertex/index buffer is present
129 * (or even better, modify draw module to do this
130 * internally when this condition is seen?)
131 */
132 draw_flush(draw);
133
134 /* Note: leave drawing surfaces mapped */
135 sp->dirty_render_cache = TRUE;
136 }
137
138
139 void
140 softpipe_draw_range_elements(struct pipe_context *pipe,
141 struct pipe_resource *indexBuffer,
142 unsigned indexSize,
143 int indexBias,
144 unsigned min_index,
145 unsigned max_index,
146 unsigned mode, unsigned start, unsigned count)
147 {
148 softpipe_draw_range_elements_instanced(pipe,
149 indexBuffer,
150 indexSize,
151 indexBias,
152 min_index,
153 max_index,
154 mode,
155 start,
156 count,
157 0,
158 1);
159 }
160
161
162 void
163 softpipe_draw_elements(struct pipe_context *pipe,
164 struct pipe_resource *indexBuffer,
165 unsigned indexSize, int indexBias,
166 unsigned mode, unsigned start, unsigned count)
167 {
168 softpipe_draw_range_elements_instanced(pipe,
169 indexBuffer,
170 indexSize,
171 indexBias,
172 0,
173 0xffffffff,
174 mode,
175 start,
176 count,
177 0,
178 1);
179 }
180
181 void
182 softpipe_draw_arrays_instanced(struct pipe_context *pipe,
183 unsigned mode,
184 unsigned start,
185 unsigned count,
186 unsigned startInstance,
187 unsigned instanceCount)
188 {
189 softpipe_draw_range_elements_instanced(pipe,
190 NULL,
191 0,
192 0,
193 0,
194 0xffffffff,
195 mode,
196 start,
197 count,
198 startInstance,
199 instanceCount);
200 }
201
202 void
203 softpipe_draw_elements_instanced(struct pipe_context *pipe,
204 struct pipe_resource *indexBuffer,
205 unsigned indexSize,
206 int indexBias,
207 unsigned mode,
208 unsigned start,
209 unsigned count,
210 unsigned startInstance,
211 unsigned instanceCount)
212 {
213 softpipe_draw_range_elements_instanced(pipe,
214 indexBuffer,
215 indexSize,
216 indexBias,
217 0,
218 0xffffffff,
219 mode,
220 start,
221 count,
222 startInstance,
223 instanceCount);
224 }
225
226 static void
227 softpipe_draw_range_elements_instanced(struct pipe_context *pipe,
228 struct pipe_resource *indexBuffer,
229 unsigned indexSize,
230 int indexBias,
231 unsigned minIndex,
232 unsigned maxIndex,
233 unsigned mode,
234 unsigned start,
235 unsigned count,
236 unsigned startInstance,
237 unsigned instanceCount)
238 {
239 struct softpipe_context *sp = softpipe_context(pipe);
240 struct draw_context *draw = sp->draw;
241 unsigned i;
242
243 if (!softpipe_check_render_cond(sp))
244 return;
245
246 sp->reduced_api_prim = u_reduced_prim(mode);
247
248 if (sp->dirty) {
249 softpipe_update_derived(sp);
250 }
251
252 softpipe_map_transfers(sp);
253
254 /* Map vertex buffers */
255 for (i = 0; i < sp->num_vertex_buffers; i++) {
256 void *buf = softpipe_resource(sp->vertex_buffer[i].buffer)->data;
257 draw_set_mapped_vertex_buffer(draw, i, buf);
258 }
259
260 /* Map index buffer, if present */
261 if (indexBuffer) {
262 void *mapped_indexes = softpipe_resource(indexBuffer)->data;
263 draw_set_mapped_element_buffer_range(draw,
264 indexSize,
265 indexBias,
266 minIndex,
267 maxIndex,
268 mapped_indexes);
269 } else {
270 /* no index/element buffer */
271 draw_set_mapped_element_buffer_range(draw,
272 0, 0,
273 start,
274 start + count - 1,
275 NULL);
276 }
277
278 /* draw! */
279 draw_arrays_instanced(draw, mode, start, count, startInstance, instanceCount);
280
281 /* unmap vertex/index buffers - will cause draw module to flush */
282 for (i = 0; i < sp->num_vertex_buffers; i++) {
283 draw_set_mapped_vertex_buffer(draw, i, NULL);
284 }
285 if (indexBuffer) {
286 draw_set_mapped_element_buffer(draw, 0, 0, NULL);
287 }
288
289 /*
290 * TODO: Flush only when a user vertex/index buffer is present
291 * (or even better, modify draw module to do this
292 * internally when this condition is seen?)
293 */
294 draw_flush(draw);
295
296 /* Note: leave drawing surfaces mapped */
297 sp->dirty_render_cache = TRUE;
298 }