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