Merge branch 'gallium-i915-current' into gallium-0.1
[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 translate_key_compare(&feme->translate->key, &key) != 0)
178 {
179 translate_key_sanitize(&key);
180 feme->translate = translate_cache_find(feme->cache,
181 &key);
182
183
184 feme->translate->set_buffer(feme->translate,
185 draw->pt.nr_vertex_buffers,
186 &feme->point_size,
187 0);
188 }
189
190 feme->point_size = draw->rasterizer->point_size;
191
192 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
193 feme->translate->set_buffer(feme->translate,
194 i,
195 ((char *)draw->pt.user.vbuffer[i] +
196 draw->pt.vertex_buffer[i].buffer_offset),
197 draw->pt.vertex_buffer[i].pitch );
198 }
199 }
200
201
202
203
204
205 static void fetch_emit_run( struct draw_pt_middle_end *middle,
206 const unsigned *fetch_elts,
207 unsigned fetch_count,
208 const ushort *draw_elts,
209 unsigned draw_count )
210 {
211 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
212 struct draw_context *draw = feme->draw;
213 void *hw_verts;
214
215 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
216 */
217 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
218
219 hw_verts = draw->render->allocate_vertices( draw->render,
220 (ushort)feme->translate->key.output_stride,
221 (ushort)fetch_count );
222 if (!hw_verts) {
223 assert(0);
224 return;
225 }
226
227
228 /* Single routine to fetch vertices and emit HW verts.
229 */
230 feme->translate->run_elts( feme->translate,
231 fetch_elts,
232 fetch_count,
233 hw_verts );
234
235 if (0) {
236 unsigned i;
237 for (i = 0; i < fetch_count; i++) {
238 debug_printf("\n\nvertex %d:\n", i);
239 draw_dump_emitted_vertex( feme->vinfo,
240 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
241 }
242 }
243
244 /* XXX: Draw arrays path to avoid re-emitting index list again and
245 * again.
246 */
247 draw->render->draw( draw->render,
248 draw_elts,
249 draw_count );
250
251 /* Done -- that was easy, wasn't it:
252 */
253 draw->render->release_vertices( draw->render,
254 hw_verts,
255 feme->translate->key.output_stride,
256 fetch_count );
257
258 }
259
260
261
262 static void fetch_emit_finish( struct draw_pt_middle_end *middle )
263 {
264 /* nothing to do */
265 }
266
267 static void fetch_emit_destroy( struct draw_pt_middle_end *middle )
268 {
269 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
270
271 translate_cache_destroy(feme->cache);
272
273 FREE(middle);
274 }
275
276
277 struct draw_pt_middle_end *draw_pt_fetch_emit( struct draw_context *draw )
278 {
279 struct fetch_emit_middle_end *fetch_emit = CALLOC_STRUCT( fetch_emit_middle_end );
280 if (fetch_emit == NULL)
281 return NULL;
282
283 fetch_emit->cache = translate_cache_create();
284 if (!fetch_emit->cache) {
285 FREE(fetch_emit);
286 return NULL;
287 }
288
289 fetch_emit->base.prepare = fetch_emit_prepare;
290 fetch_emit->base.run = fetch_emit_run;
291 fetch_emit->base.finish = fetch_emit_finish;
292 fetch_emit->base.destroy = fetch_emit_destroy;
293
294 fetch_emit->draw = draw;
295
296 return &fetch_emit->base;
297 }
298