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
;
68 static void fse_prepare( struct draw_pt_middle_end
*middle
,
72 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
73 struct draw_context
*draw
= fse
->draw
;
74 unsigned num_vs_inputs
= draw
->vs
.vertex_shader
->info
.num_inputs
;
75 const struct vertex_info
*vinfo
;
77 boolean need_psize
= 0;
80 if (!draw
->render
->set_primitive( draw
->render
,
86 /* Must do this after set_primitive() above:
88 vinfo
= draw
->render
->get_vertex_info(draw
->render
);
92 fse
->key
.output_stride
= vinfo
->size
* 4;
93 fse
->key
.nr_outputs
= vinfo
->num_attribs
;
94 fse
->key
.nr_inputs
= num_vs_inputs
;
96 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_outputs
, /* outputs - translate to hw format */
97 fse
->key
.nr_inputs
); /* inputs - fetch from api format */
99 fse
->key
.viewport
= !draw
->identity_viewport
;
100 fse
->key
.clip
= !draw
->bypass_clipping
;
103 memset(fse
->key
.element
, 0,
104 fse
->key
.nr_elements
* sizeof(fse
->key
.element
[0]));
106 for (i
= 0; i
< num_vs_inputs
; i
++) {
107 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
108 fse
->key
.element
[i
].in
.format
= src
->src_format
;
110 /* Consider ignoring these, ie make generated programs
111 * independent of this state:
113 fse
->key
.element
[i
].in
.buffer
= src
->vertex_buffer_index
;
114 fse
->key
.element
[i
].in
.offset
= src
->src_offset
;
119 unsigned dst_offset
= 0;
121 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
122 unsigned emit_sz
= 0;
123 unsigned output_format
= PIPE_FORMAT_NONE
;
124 unsigned vs_output
= vinfo
->src_index
[i
];
126 switch (vinfo
->emit
[i
]) {
128 output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
129 emit_sz
= 4 * sizeof(float);
132 output_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
133 emit_sz
= 3 * sizeof(float);
136 output_format
= PIPE_FORMAT_R32G32_FLOAT
;
137 emit_sz
= 2 * sizeof(float);
140 output_format
= PIPE_FORMAT_R32_FLOAT
;
141 emit_sz
= 1 * sizeof(float);
145 output_format
= PIPE_FORMAT_R32_FLOAT
;
146 emit_sz
= 1 * sizeof(float);
147 vs_output
= vinfo
->num_attribs
+ 1;
150 output_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
151 emit_sz
= 4 * sizeof(ubyte
);
158 /* The elements in the key correspond to vertex shader output
159 * numbers, not to positions in the hw vertex description --
160 * that's handled by the output_offset field.
162 fse
->key
.element
[vs_output
].out
.format
= output_format
;
163 fse
->key
.element
[vs_output
].out
.offset
= dst_offset
;
165 dst_offset
+= emit_sz
;
166 assert(fse
->key
.output_stride
>= dst_offset
);
170 /* To make psize work, really need to tell the vertex shader to
171 * copy that value from input->output. For 'translate' this was
172 * implicit for all elements.
176 unsigned input
= num_vs_inputs
+ 1;
177 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[i
];
178 fse
->key
.element
[i
].input_format
= PIPE_FORMAT_R32_FLOAT
;
179 fse
->key
.element
[i
].input_buffer
= 0; //nr_buffers + 1;
180 fse
->key
.element
[i
].input_offset
= 0;
182 fse
->key
.nr_inputs
+= 1;
183 fse
->key
.nr_elements
= MAX2(fse
->key
.nr_inputs
,
184 fse
->key
.nr_outputs
);
189 /* Would normally look up a vertex shader and peruse its list of
190 * varients somehow. We omitted that step and put all the
191 * hardcoded "shaders" into an array. We're just making the
192 * assumption that this happens to be a matching shader... ie
193 * you're running isosurf, aren't you?
195 fse
->active
= draw_vs_lookup_varient( draw
->vs
.vertex_shader
,
203 /* Now set buffer pointers:
205 for (i
= 0; i
< num_vs_inputs
; i
++) {
206 unsigned buf
= draw
->pt
.vertex_element
[i
].vertex_buffer_index
;
208 fse
->active
->set_input( fse
->active
,
211 ((const ubyte
*) draw
->pt
.user
.vbuffer
[buf
] +
212 draw
->pt
.vertex_buffer
[buf
].buffer_offset
),
214 draw
->pt
.vertex_buffer
[buf
].pitch
);
217 fse
->active
->set_constants( fse
->active
,
218 (const float (*)[4])draw
->pt
.user
.constants
);
220 fse
->active
->set_viewport( fse
->active
,
232 static void fse_run_linear( struct draw_pt_middle_end
*middle
,
236 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
237 struct draw_context
*draw
= fse
->draw
;
238 unsigned alloc_count
= align(count
, 4);
241 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
243 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
245 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
246 (ushort
)fse
->key
.output_stride
,
247 (ushort
)alloc_count
);
254 /* Single routine to fetch vertices, run shader and emit HW verts.
255 * Clipping is done elsewhere -- either by the API or on hardware,
256 * or for some other reason not required...
258 fse
->active
->run_linear( fse
->active
,
262 /* Draw arrays path to avoid re-emitting index list again and
265 draw
->render
->draw_arrays( draw
->render
,
270 draw
->render
->release_vertices( draw
->render
,
272 fse
->key
.output_stride
,
278 fse_run(struct draw_pt_middle_end
*middle
,
279 const unsigned *fetch_elts
,
280 unsigned fetch_count
,
281 const ushort
*draw_elts
,
282 unsigned draw_count
)
284 struct fetch_shade_emit
*fse
= (struct fetch_shade_emit
*)middle
;
285 struct draw_context
*draw
= fse
->draw
;
286 unsigned alloc_count
= align(fetch_count
, 4);
289 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
291 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
293 hw_verts
= draw
->render
->allocate_vertices( draw
->render
,
294 (ushort
)fse
->key
.output_stride
,
295 (ushort
)alloc_count
);
302 /* Single routine to fetch vertices, run shader and emit HW verts.
304 fse
->active
->run_elts( fse
->active
,
309 draw
->render
->draw( draw
->render
,
313 draw
->render
->release_vertices( draw
->render
,
315 fse
->key
.output_stride
,
321 static void fse_finish( struct draw_pt_middle_end
*middle
)
327 fse_destroy( struct draw_pt_middle_end
*middle
)
332 struct draw_pt_middle_end
*draw_pt_middle_fse( struct draw_context
*draw
)
334 struct fetch_shade_emit
*fse
= CALLOC_STRUCT(fetch_shade_emit
);
338 fse
->base
.prepare
= fse_prepare
;
339 fse
->base
.run
= fse_run
;
340 fse
->base
.run_linear
= fse_run_linear
;
341 fse
->base
.finish
= fse_finish
;
342 fse
->base
.destroy
= fse_destroy
;