redo the linear paths
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_fetch_emit.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 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "pipe/p_util.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_vbuf.h"
37 #include "draw/draw_vertex.h"
38 #include "draw/draw_pt.h"
39 #include "translate/translate.h"
40 #include "translate/translate_cache.h"
41
42 /* The simplest 'middle end' in the new vertex code.
43 *
44 * The responsibilities of a middle end are to:
45 * - perform vertex fetch using
46 * - draw vertex element/buffer state
47 * - a list of fetch indices we received as an input
48 * - run the vertex shader
49 * - cliptest,
50 * - clip coord calculation
51 * - viewport transformation
52 * - if necessary, run the primitive pipeline, passing it:
53 * - a linear array of vertex_header vertices constructed here
54 * - a set of draw indices we received as an input
55 * - otherwise, drive the hw backend,
56 * - allocate space for hardware format vertices
57 * - translate the vertex-shader output vertices to hw format
58 * - calling the backend draw functions.
59 *
60 * For convenience, we provide a helper function to drive the hardware
61 * backend given similar inputs to those required to run the pipeline.
62 *
63 * In the case of passthrough mode, many of these actions are disabled
64 * or noops, so we end up doing:
65 *
66 * - perform vertex fetch
67 * - drive the hw backend
68 *
69 * IE, basically just vertex fetch to post-vs-format vertices,
70 * followed by a call to the backend helper function.
71 */
72
73
74 struct fetch_emit_middle_end {
75 struct draw_pt_middle_end base;
76 struct draw_context *draw;
77
78 struct translate *translate;
79 const struct vertex_info *vinfo;
80
81 /* Cache point size somewhere it's address won't change:
82 */
83 float point_size;
84
85 struct translate_cache *cache;
86 };
87
88
89
90
91 static void fetch_emit_prepare( struct draw_pt_middle_end *middle,
92 unsigned prim,
93 unsigned opt )
94 {
95 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
96 struct draw_context *draw = feme->draw;
97 const struct vertex_info *vinfo;
98 unsigned i, dst_offset;
99 boolean ok;
100 struct translate_key key;
101
102
103 ok = draw->render->set_primitive( draw->render,
104 prim );
105 if (!ok) {
106 assert(0);
107 return;
108 }
109
110 /* Must do this after set_primitive() above:
111 */
112 vinfo = feme->vinfo = draw->render->get_vertex_info(draw->render);
113
114
115
116 /* Transform from API vertices to HW vertices, skipping the
117 * pipeline_vertex intermediate step.
118 */
119 dst_offset = 0;
120 memset(&key, 0, sizeof(key));
121
122 for (i = 0; i < vinfo->num_attribs; i++) {
123 const struct pipe_vertex_element *src = &draw->pt.vertex_element[vinfo->src_index[i]];
124
125 unsigned emit_sz = 0;
126 unsigned input_format = src->src_format;
127 unsigned input_buffer = src->vertex_buffer_index;
128 unsigned input_offset = src->src_offset;
129 unsigned output_format;
130
131 switch (vinfo->emit[i]) {
132 case EMIT_4F:
133 output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
134 emit_sz = 4 * sizeof(float);
135 break;
136 case EMIT_3F:
137 output_format = PIPE_FORMAT_R32G32B32_FLOAT;
138 emit_sz = 3 * sizeof(float);
139 break;
140 case EMIT_2F:
141 output_format = PIPE_FORMAT_R32G32_FLOAT;
142 emit_sz = 2 * sizeof(float);
143 break;
144 case EMIT_1F:
145 output_format = PIPE_FORMAT_R32_FLOAT;
146 emit_sz = 1 * sizeof(float);
147 break;
148 case EMIT_1F_PSIZE:
149 input_format = PIPE_FORMAT_R32_FLOAT;
150 input_buffer = draw->pt.nr_vertex_buffers;
151 input_offset = 0;
152 output_format = PIPE_FORMAT_R32_FLOAT;
153 emit_sz = 1 * sizeof(float);
154 break;
155 default:
156 assert(0);
157 output_format = PIPE_FORMAT_NONE;
158 emit_sz = 0;
159 continue;
160 }
161
162 key.element[i].input_format = input_format;
163 key.element[i].input_buffer = input_buffer;
164 key.element[i].input_offset = input_offset;
165 key.element[i].output_format = output_format;
166 key.element[i].output_offset = dst_offset;
167
168 dst_offset += emit_sz;
169 }
170
171 key.nr_elements = vinfo->num_attribs;
172 key.output_stride = vinfo->size * 4;
173
174 /* Don't bother with caching at this stage:
175 */
176 if (!feme->translate ||
177 memcmp(&feme->translate->key, &key, sizeof(key)) != 0)
178 {
179 feme->translate = translate_cache_find(feme->cache,
180 &key);
181
182
183 feme->translate->set_buffer(feme->translate,
184 draw->pt.nr_vertex_buffers,
185 &feme->point_size,
186 0);
187 }
188
189 feme->point_size = draw->rasterizer->point_size;
190
191 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
192 feme->translate->set_buffer(feme->translate,
193 i,
194 ((char *)draw->pt.user.vbuffer[i] +
195 draw->pt.vertex_buffer[i].buffer_offset),
196 draw->pt.vertex_buffer[i].pitch );
197 }
198 }
199
200
201
202
203
204 static void fetch_emit_run( struct draw_pt_middle_end *middle,
205 const unsigned *fetch_elts,
206 unsigned fetch_count,
207 const ushort *draw_elts,
208 unsigned draw_count )
209 {
210 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
211 struct draw_context *draw = feme->draw;
212 void *hw_verts;
213
214 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
215 */
216 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
217
218 hw_verts = draw->render->allocate_vertices( draw->render,
219 (ushort)feme->translate->key.output_stride,
220 (ushort)fetch_count );
221 if (!hw_verts) {
222 assert(0);
223 return;
224 }
225
226
227 /* Single routine to fetch vertices and emit HW verts.
228 */
229 feme->translate->run_elts( feme->translate,
230 fetch_elts,
231 fetch_count,
232 hw_verts );
233
234 if (0) {
235 unsigned i;
236 for (i = 0; i < fetch_count; i++) {
237 debug_printf("\n\nvertex %d:\n", i);
238 draw_dump_emitted_vertex( feme->vinfo,
239 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
240 }
241 }
242
243 /* XXX: Draw arrays path to avoid re-emitting index list again and
244 * again.
245 */
246 draw->render->draw( draw->render,
247 draw_elts,
248 draw_count );
249
250 /* Done -- that was easy, wasn't it:
251 */
252 draw->render->release_vertices( draw->render,
253 hw_verts,
254 feme->translate->key.output_stride,
255 fetch_count );
256
257 }
258
259
260 static void fetch_emit_run_linear( struct draw_pt_middle_end *middle,
261 unsigned start,
262 unsigned count )
263 {
264 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
265 struct draw_context *draw = feme->draw;
266 void *hw_verts;
267
268 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
269 */
270 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
271
272 hw_verts = draw->render->allocate_vertices( draw->render,
273 (ushort)feme->translate->key.output_stride,
274 (ushort)count );
275 if (!hw_verts) {
276 assert(0);
277 return;
278 }
279
280 /* Single routine to fetch vertices and emit HW verts.
281 */
282 feme->translate->run( feme->translate,
283 start,
284 count,
285 hw_verts );
286
287 if (0) {
288 unsigned i;
289 for (i = 0; i < count; i++) {
290 debug_printf("\n\nvertex %d:\n", i);
291 draw_dump_emitted_vertex( feme->vinfo,
292 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
293 }
294 }
295
296 /* XXX: Draw arrays path to avoid re-emitting index list again and
297 * again.
298 */
299 draw->render->draw_arrays( draw->render,
300 0, /*start*/
301 count );
302
303 /* Done -- that was easy, wasn't it:
304 */
305 draw->render->release_vertices( draw->render,
306 hw_verts,
307 feme->translate->key.output_stride,
308 count );
309
310 }
311
312
313
314 static void fetch_emit_finish( struct draw_pt_middle_end *middle )
315 {
316 /* nothing to do */
317 }
318
319 static void fetch_emit_destroy( struct draw_pt_middle_end *middle )
320 {
321 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
322
323 translate_cache_destroy(feme->cache);
324
325 FREE(middle);
326 }
327
328
329 struct draw_pt_middle_end *draw_pt_fetch_emit( struct draw_context *draw )
330 {
331 struct fetch_emit_middle_end *fetch_emit = CALLOC_STRUCT( fetch_emit_middle_end );
332 if (fetch_emit == NULL)
333 return NULL;
334
335 fetch_emit->cache = translate_cache_create();
336 if (!fetch_emit->cache) {
337 FREE(fetch_emit);
338 return NULL;
339 }
340
341 fetch_emit->base.prepare = fetch_emit_prepare;
342 fetch_emit->base.run = fetch_emit_run;
343 fetch_emit->base.run_linear = fetch_emit_run_linear;
344 fetch_emit->base.finish = fetch_emit_finish;
345 fetch_emit->base.destroy = fetch_emit_destroy;
346
347 fetch_emit->draw = draw;
348
349 return &fetch_emit->base;
350 }
351