1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "pipe/p_util.h"
35 #include "draw/draw_context.h"
36 #include "draw/draw_private.h"
37 #include "draw/draw_vbuf.h"
38 #include "draw/draw_vertex.h"
39 #include "draw/draw_pt.h"
40 #include "draw/draw_vs.h"
42 #include "translate/translate.h"
44 struct fetch_shade_emit
;
47 /* Prototype fetch, shade, emit-hw-verts all in one go.
49 struct fetch_shade_emit
{
50 struct draw_pt_middle_end base
;
51 struct draw_context
*draw
;
56 const float *constants
;
57 unsigned pitch
[PIPE_MAX_ATTRIBS
];
58 const ubyte
*src
[PIPE_MAX_ATTRIBS
];
61 struct draw_vs_varient_key key
;
62 struct draw_vs_varient
*active
;
65 const struct vertex_info
*vinfo
;
71 static void fse_prepare( struct draw_pt_middle_end
*middle
,
74 unsigned *max_vertices
)
76 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
77 struct draw_context
*draw
= fse
->draw
;
78 unsigned num_vs_inputs
= draw
->vs
.vertex_shader
->info
.num_inputs
;
79 const struct vertex_info
*vinfo
;
83 if (!draw
->render
->set_primitive( draw
->render
,
89 /* Must do this after set_primitive() above:
91 fse
->vinfo
= vinfo
= draw
->render
->get_vertex_info(draw
->render
);
95 fse
->key
.output_stride
= vinfo
->size
* 4;
96 fse
->key
.nr_outputs
= vinfo
->num_attribs
;
97 fse
->key
.nr_inputs
= num_vs_inputs
;
99 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_outputs
, /* outputs - translate to hw format */
100 fse
->key
.nr_inputs
); /* inputs - fetch from api format */
102 fse
->key
.viewport
= !draw
->identity_viewport
;
103 fse
->key
.clip
= !draw
->bypass_clipping
;
106 memset(fse
->key
.element
, 0,
107 fse
->key
.nr_elements
* sizeof(fse
->key
.element
[0]));
109 for (i
= 0; i
< num_vs_inputs
; i
++) {
110 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
111 fse
->key
.element
[i
].in
.format
= src
->src_format
;
113 /* Consider ignoring these, ie make generated programs
114 * independent of this state:
116 fse
->key
.element
[i
].in
.buffer
= src
->vertex_buffer_index
;
117 fse
->key
.element
[i
].in
.offset
= src
->src_offset
;
122 unsigned dst_offset
= 0;
124 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
125 unsigned emit_sz
= 0;
127 switch (vinfo
->emit
[i
]) {
129 emit_sz
= 4 * sizeof(float);
132 emit_sz
= 3 * sizeof(float);
135 emit_sz
= 2 * sizeof(float);
138 emit_sz
= 1 * sizeof(float);
141 emit_sz
= 1 * sizeof(float);
144 emit_sz
= 4 * sizeof(ubyte
);
151 /* The elements in the key correspond to vertex shader output
152 * numbers, not to positions in the hw vertex description --
153 * that's handled by the output_offset field.
155 fse
->key
.element
[i
].out
.format
= vinfo
->emit
[i
];
156 fse
->key
.element
[i
].out
.vs_output
= vinfo
->src_index
[i
];
157 fse
->key
.element
[i
].out
.offset
= dst_offset
;
159 dst_offset
+= emit_sz
;
160 assert(fse
->key
.output_stride
>= dst_offset
);
165 /* Would normally look up a vertex shader and peruse its list of
166 * varients somehow. We omitted that step and put all the
167 * hardcoded "shaders" into an array. We're just making the
168 * assumption that this happens to be a matching shader... ie
169 * you're running isosurf, aren't you?
171 fse
->active
= draw_vs_lookup_varient( draw
->vs
.vertex_shader
,
179 /* Now set buffer pointers:
181 for (i
= 0; i
< num_vs_inputs
; i
++) {
182 unsigned buf
= draw
->pt
.vertex_element
[i
].vertex_buffer_index
;
184 fse
->active
->set_input( fse
->active
,
187 ((const ubyte
*) draw
->pt
.user
.vbuffer
[buf
] +
188 draw
->pt
.vertex_buffer
[buf
].buffer_offset
),
190 draw
->pt
.vertex_buffer
[buf
].pitch
);
193 *max_vertices
= (draw
->render
->max_vertex_buffer_bytes
/
196 /* Return an even number of verts.
197 * This prevents "parity" errors when splitting long triangle strips which
198 * can lead to front/back culling mix-ups.
199 * Every other triangle in a strip has an alternate front/back orientation
200 * so splitting at an odd position can cause the orientation of subsequent
201 * triangles to get reversed.
203 *max_vertices
= *max_vertices
& ~1;
205 /* Probably need to do this somewhere (or fix exec shader not to
209 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
210 vs
->prepare(vs
, draw
);
223 static void fse_run_linear( struct draw_pt_middle_end
*middle
,
227 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
228 struct draw_context
*draw
= fse
->draw
;
231 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
233 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
235 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
236 (ushort
)fse
->key
.output_stride
,
244 /* Single routine to fetch vertices, run shader and emit HW verts.
245 * Clipping is done elsewhere -- either by the API or on hardware,
246 * or for some other reason not required...
248 fse
->active
->run_linear( fse
->active
,
252 /* Draw arrays path to avoid re-emitting index list again and
255 draw
->render
->draw_arrays( draw
->render
,
261 for (i
= 0; i
< count
; i
++) {
262 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__
, i
,
263 fse
->key
.output_stride
,
264 fse
->key
.output_stride
* i
);
266 draw_dump_emitted_vertex( fse
->vinfo
,
267 (const uint8_t *)hw_verts
+ fse
->key
.output_stride
* i
);
272 draw
->render
->release_vertices( draw
->render
,
274 fse
->key
.output_stride
,
280 fse_run(struct draw_pt_middle_end
*middle
,
281 const unsigned *fetch_elts
,
282 unsigned fetch_count
,
283 const ushort
*draw_elts
,
284 unsigned draw_count
)
286 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
287 struct draw_context
*draw
= fse
->draw
;
290 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
292 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
294 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
295 (ushort
)fse
->key
.output_stride
,
296 (ushort
)fetch_count
);
303 /* Single routine to fetch vertices, run shader and emit HW verts.
305 fse
->active
->run_elts( fse
->active
,
310 draw
->render
->draw( draw
->render
,
316 for (i
= 0; i
< fetch_count
; i
++) {
317 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__
, i
);
318 draw_dump_emitted_vertex( fse
->vinfo
,
319 (const uint8_t *)hw_verts
+
320 fse
->key
.output_stride
* i
);
325 draw
->render
->release_vertices( draw
->render
,
327 fse
->key
.output_stride
,
334 static void fse_run_linear_elts( struct draw_pt_middle_end
*middle
,
337 const ushort
*draw_elts
,
338 unsigned draw_count
)
340 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
341 struct draw_context
*draw
= fse
->draw
;
344 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
346 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
348 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
349 (ushort
)fse
->key
.output_stride
,
357 /* Single routine to fetch vertices, run shader and emit HW verts.
358 * Clipping is done elsewhere -- either by the API or on hardware,
359 * or for some other reason not required...
361 fse
->active
->run_linear( fse
->active
,
366 draw
->render
->draw( draw
->render
,
372 draw
->render
->release_vertices( draw
->render
,
374 fse
->key
.output_stride
,
380 static void fse_finish( struct draw_pt_middle_end
*middle
)
386 fse_destroy( struct draw_pt_middle_end
*middle
)
391 struct draw_pt_middle_end
*draw_pt_middle_fse( struct draw_context
*draw
)
393 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
397 fse
->base
.prepare
= fse_prepare
;
398 fse
->base
.run
= fse_run
;
399 fse
->base
.run_linear
= fse_run_linear
;
400 fse
->base
.run_linear_elts
= fse_run_linear_elts
;
401 fse
->base
.finish
= fse_finish
;
402 fse
->base
.destroy
= fse_destroy
;