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