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
,
75 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
76 struct draw_context
*draw
= fse
->draw
;
77 unsigned num_vs_inputs
= draw
->vs
.vertex_shader
->info
.num_inputs
;
78 const struct vertex_info
*vinfo
;
82 if (!draw
->render
->set_primitive( draw
->render
,
88 /* Must do this after set_primitive() above:
90 fse
->vinfo
= vinfo
= draw
->render
->get_vertex_info(draw
->render
);
94 fse
->key
.output_stride
= vinfo
->size
* 4;
95 fse
->key
.nr_outputs
= vinfo
->num_attribs
;
96 fse
->key
.nr_inputs
= num_vs_inputs
;
98 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_outputs
, /* outputs - translate to hw format */
99 fse
->key
.nr_inputs
); /* inputs - fetch from api format */
101 fse
->key
.viewport
= !draw
->identity_viewport
;
102 fse
->key
.clip
= !draw
->bypass_clipping
;
105 memset(fse
->key
.element
, 0,
106 fse
->key
.nr_elements
* sizeof(fse
->key
.element
[0]));
108 for (i
= 0; i
< num_vs_inputs
; i
++) {
109 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
110 fse
->key
.element
[i
].in
.format
= src
->src_format
;
112 /* Consider ignoring these, ie make generated programs
113 * independent of this state:
115 fse
->key
.element
[i
].in
.buffer
= src
->vertex_buffer_index
;
116 fse
->key
.element
[i
].in
.offset
= src
->src_offset
;
121 unsigned dst_offset
= 0;
123 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
124 unsigned emit_sz
= 0;
126 switch (vinfo
->emit
[i
]) {
128 emit_sz
= 4 * sizeof(float);
131 emit_sz
= 3 * sizeof(float);
134 emit_sz
= 2 * sizeof(float);
137 emit_sz
= 1 * sizeof(float);
140 emit_sz
= 1 * sizeof(float);
143 emit_sz
= 4 * sizeof(ubyte
);
150 /* The elements in the key correspond to vertex shader output
151 * numbers, not to positions in the hw vertex description --
152 * that's handled by the output_offset field.
154 fse
->key
.element
[i
].out
.format
= vinfo
->emit
[i
];
155 fse
->key
.element
[i
].out
.vs_output
= vinfo
->src_index
[i
];
156 fse
->key
.element
[i
].out
.offset
= dst_offset
;
158 dst_offset
+= emit_sz
;
159 assert(fse
->key
.output_stride
>= dst_offset
);
164 /* Would normally look up a vertex shader and peruse its list of
165 * varients somehow. We omitted that step and put all the
166 * hardcoded "shaders" into an array. We're just making the
167 * assumption that this happens to be a matching shader... ie
168 * you're running isosurf, aren't you?
170 fse
->active
= draw_vs_lookup_varient( draw
->vs
.vertex_shader
,
178 /* Now set buffer pointers:
180 for (i
= 0; i
< num_vs_inputs
; i
++) {
181 unsigned buf
= draw
->pt
.vertex_element
[i
].vertex_buffer_index
;
183 fse
->active
->set_input( fse
->active
,
186 ((const ubyte
*) draw
->pt
.user
.vbuffer
[buf
] +
187 draw
->pt
.vertex_buffer
[buf
].buffer_offset
),
189 draw
->pt
.vertex_buffer
[buf
].pitch
);
192 fse
->active
->set_constants( fse
->active
,
193 (const float (*)[4])draw
->pt
.user
.constants
);
195 fse
->active
->set_viewport( fse
->active
,
207 static void fse_run_linear( struct draw_pt_middle_end
*middle
,
211 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
212 struct draw_context
*draw
= fse
->draw
;
213 unsigned alloc_count
= align(count
, 4);
216 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
218 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
220 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
221 (ushort
)fse
->key
.output_stride
,
222 (ushort
)alloc_count
);
229 /* Single routine to fetch vertices, run shader and emit HW verts.
230 * Clipping is done elsewhere -- either by the API or on hardware,
231 * or for some other reason not required...
233 fse
->active
->run_linear( fse
->active
,
237 /* Draw arrays path to avoid re-emitting index list again and
240 draw
->render
->draw_arrays( draw
->render
,
246 for (i
= 0; i
< count
; i
++) {
247 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__
, i
,
248 fse
->key
.output_stride
,
249 fse
->key
.output_stride
* i
);
251 draw_dump_emitted_vertex( fse
->vinfo
,
252 (const uint8_t *)hw_verts
+ fse
->key
.output_stride
* i
);
257 draw
->render
->release_vertices( draw
->render
,
259 fse
->key
.output_stride
,
265 fse_run(struct draw_pt_middle_end
*middle
,
266 const unsigned *fetch_elts
,
267 unsigned fetch_count
,
268 const ushort
*draw_elts
,
269 unsigned draw_count
)
271 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
272 struct draw_context
*draw
= fse
->draw
;
273 unsigned alloc_count
= align(fetch_count
, 4);
276 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
278 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
280 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
281 (ushort
)fse
->key
.output_stride
,
282 (ushort
)alloc_count
);
289 /* Single routine to fetch vertices, run shader and emit HW verts.
291 fse
->active
->run_elts( fse
->active
,
296 draw
->render
->draw( draw
->render
,
302 for (i
= 0; i
< fetch_count
; i
++) {
303 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__
, i
);
304 draw_dump_emitted_vertex( fse
->vinfo
,
305 (const uint8_t *)hw_verts
+
306 fse
->key
.output_stride
* i
);
311 draw
->render
->release_vertices( draw
->render
,
313 fse
->key
.output_stride
,
319 static void fse_finish( struct draw_pt_middle_end
*middle
)
325 fse_destroy( struct draw_pt_middle_end
*middle
)
330 struct draw_pt_middle_end
*draw_pt_middle_fse( struct draw_context
*draw
)
332 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
336 fse
->base
.prepare
= fse_prepare
;
337 fse
->base
.run
= fse_run
;
338 fse
->base
.run_linear
= fse_run_linear
;
339 fse
->base
.finish
= fse_finish
;
340 fse
->base
.destroy
= fse_destroy
;