gallium: Implement draw_vbo and set_index_buffer for all drivers.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_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_prim.h"
37 #include "util/u_draw_quad.h"
38
39 #include "lp_context.h"
40 #include "lp_state.h"
41
42 #include "draw/draw_context.h"
43
44
45
46 /**
47 * Draw vertex arrays, with optional indexing, optional instancing.
48 * All the other drawing functions are implemented in terms of this function.
49 * Basically, map the vertex buffers (and drawing surfaces), then hand off
50 * the drawing to the 'draw' module.
51 */
52 static void
53 llvmpipe_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
54 {
55 struct llvmpipe_context *lp = llvmpipe_context(pipe);
56 struct draw_context *draw = lp->draw;
57 void *mapped_indices = NULL;
58 unsigned i;
59
60 if (lp->dirty)
61 llvmpipe_update_derived( lp );
62
63 /*
64 * Map vertex buffers
65 */
66 for (i = 0; i < lp->num_vertex_buffers; i++) {
67 void *buf = llvmpipe_resource_data(lp->vertex_buffer[i].buffer);
68 draw_set_mapped_vertex_buffer(draw, i, buf);
69 }
70
71 /* Map index buffer, if present */
72 if (info->indexed && lp->index_buffer.buffer) {
73 mapped_indices = llvmpipe_resource_data(lp->index_buffer.buffer);
74 mapped_indices += lp->index_buffer.offset;
75 }
76
77 draw_set_mapped_element_buffer_range(draw, (mapped_indices) ?
78 lp->index_buffer.index_size : 0,
79 info->index_bias,
80 info->min_index,
81 info->max_index,
82 mapped_indices);
83
84 llvmpipe_prepare_vertex_sampling(lp,
85 lp->num_vertex_sampler_views,
86 lp->vertex_sampler_views);
87
88 /* draw! */
89 draw_arrays_instanced(draw, info->mode, info->start, info->count,
90 info->start_instance, info->instance_count);
91
92 /*
93 * unmap vertex/index buffers
94 */
95 for (i = 0; i < lp->num_vertex_buffers; i++) {
96 draw_set_mapped_vertex_buffer(draw, i, NULL);
97 }
98 if (mapped_indices) {
99 draw_set_mapped_element_buffer(draw, 0, 0, NULL);
100 }
101 llvmpipe_cleanup_vertex_sampling(lp);
102
103 /*
104 * TODO: Flush only when a user vertex/index buffer is present
105 * (or even better, modify draw module to do this
106 * internally when this condition is seen?)
107 */
108 draw_flush(draw);
109 }
110
111
112 static void
113 llvmpipe_draw_range_elements_instanced(struct pipe_context *pipe,
114 struct pipe_resource *indexBuffer,
115 unsigned indexSize,
116 int indexBias,
117 unsigned minIndex,
118 unsigned maxIndex,
119 unsigned mode,
120 unsigned start,
121 unsigned count,
122 unsigned startInstance,
123 unsigned instanceCount)
124 {
125 struct llvmpipe_context *lp = llvmpipe_context(pipe);
126 struct pipe_draw_info info;
127 struct pipe_index_buffer saved_ib, ib;
128
129 util_draw_init_info(&info);
130 info.mode = mode;
131 info.start = start;
132 info.count = count;
133 info.start_instance = startInstance;
134 info.instance_count = instanceCount;
135
136 info.index_bias = indexBias;
137 info.min_index = minIndex;
138 info.max_index = maxIndex;
139
140 if (indexBuffer) {
141 info.indexed = TRUE;
142 saved_ib = lp->index_buffer;
143
144 ib.buffer = indexBuffer;
145 ib.offset = 0;
146 ib.index_size = indexSize;
147 pipe->set_index_buffer(pipe, &ib);
148 }
149
150 llvmpipe_draw_vbo(pipe, &info);
151
152 if (indexBuffer)
153 pipe->set_index_buffer(pipe, &saved_ib);
154 }
155
156 static void
157 llvmpipe_draw_arrays_instanced(struct pipe_context *pipe,
158 unsigned mode,
159 unsigned start,
160 unsigned count,
161 unsigned startInstance,
162 unsigned instanceCount)
163 {
164 llvmpipe_draw_range_elements_instanced(pipe,
165 NULL, /* no indexBuffer */
166 0, 0, /* indexSize, indexBias */
167 0, ~0, /* minIndex, maxIndex */
168 mode,
169 start,
170 count,
171 startInstance,
172 instanceCount);
173 }
174
175
176 static void
177 llvmpipe_draw_elements_instanced(struct pipe_context *pipe,
178 struct pipe_resource *indexBuffer,
179 unsigned indexSize,
180 int indexBias,
181 unsigned mode,
182 unsigned start,
183 unsigned count,
184 unsigned startInstance,
185 unsigned instanceCount)
186 {
187 llvmpipe_draw_range_elements_instanced(pipe,
188 indexBuffer,
189 indexSize, indexBias,
190 0, ~0, /* minIndex, maxIndex */
191 mode,
192 start,
193 count,
194 startInstance,
195 instanceCount);
196 }
197
198
199 static void
200 llvmpipe_draw_elements(struct pipe_context *pipe,
201 struct pipe_resource *indexBuffer,
202 unsigned indexSize,
203 int indexBias,
204 unsigned mode,
205 unsigned start,
206 unsigned count)
207 {
208 llvmpipe_draw_range_elements_instanced(pipe,
209 indexBuffer,
210 indexSize, indexBias,
211 0, 0xffffffff, /* min, maxIndex */
212 mode, start, count,
213 0, /* startInstance */
214 1); /* instanceCount */
215 }
216
217
218 static void
219 llvmpipe_draw_range_elements(struct pipe_context *pipe,
220 struct pipe_resource *indexBuffer,
221 unsigned indexSize,
222 int indexBias,
223 unsigned min_index,
224 unsigned max_index,
225 unsigned mode,
226 unsigned start,
227 unsigned count)
228 {
229 llvmpipe_draw_range_elements_instanced(pipe,
230 indexBuffer,
231 indexSize, indexBias,
232 min_index, max_index,
233 mode, start, count,
234 0, /* startInstance */
235 1); /* instanceCount */
236 }
237
238
239 static void
240 llvmpipe_draw_arrays(struct pipe_context *pipe,
241 unsigned mode,
242 unsigned start,
243 unsigned count)
244 {
245 llvmpipe_draw_range_elements_instanced(pipe,
246 NULL, /* indexBuffer */
247 0, /* indexSize */
248 0, /* indexBias */
249 0, ~0, /* min, maxIndex */
250 mode, start, count,
251 0, /* startInstance */
252 1); /* instanceCount */
253 }
254
255
256 void
257 llvmpipe_init_draw_funcs(struct llvmpipe_context *llvmpipe)
258 {
259 llvmpipe->pipe.draw_arrays = llvmpipe_draw_arrays;
260 llvmpipe->pipe.draw_elements = llvmpipe_draw_elements;
261 llvmpipe->pipe.draw_range_elements = llvmpipe_draw_range_elements;
262 llvmpipe->pipe.draw_arrays_instanced = llvmpipe_draw_arrays_instanced;
263 llvmpipe->pipe.draw_elements_instanced = llvmpipe_draw_elements_instanced;
264
265 llvmpipe->pipe.draw_vbo = llvmpipe_draw_vbo;
266 }