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 **************************************************************************/
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_prim.h"
31 #include "draw/draw_context.h"
32 #include "draw/draw_vbuf.h"
33 #include "draw/draw_vertex.h"
34 #include "draw/draw_pt.h"
35 #include "draw/draw_vs.h"
36 #include "draw/draw_gs.h"
39 struct fetch_pipeline_middle_end
{
40 struct draw_pt_middle_end base
;
41 struct draw_context
*draw
;
44 struct pt_so_emit
*so_emit
;
45 struct pt_fetch
*fetch
;
46 struct pt_post_vs
*post_vs
;
48 unsigned vertex_data_offset
;
54 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
57 unsigned *max_vertices
)
59 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
60 struct draw_context
*draw
= fpme
->draw
;
61 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
63 unsigned instance_id_index
= ~0;
65 unsigned gs_out_prim
= (draw
->gs
.geometry_shader
?
66 draw
->gs
.geometry_shader
->output_primitive
:
69 /* Add one to num_outputs because the pipeline occasionally tags on
70 * an additional texcoord, eg for AA lines.
72 unsigned nr
= MAX2( vs
->info
.num_inputs
,
73 vs
->info
.num_outputs
+ 1 );
75 /* Scan for instanceID system value.
77 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
78 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
79 instance_id_index
= i
;
84 fpme
->input_prim
= prim
;
87 /* Always leave room for the vertex header whether we need it or
88 * not. It's hard to get rid of it in particular because of the
89 * viewport code in draw_pt_post_vs.c.
91 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
95 draw_pt_fetch_prepare( fpme
->fetch
,
99 /* XXX: it's not really gl rasterization rules we care about here,
100 * but gl vs dx9 clip spaces.
102 draw_pt_post_vs_prepare( fpme
->post_vs
,
103 (boolean
)draw
->bypass_clipping
,
104 (boolean
)draw
->identity_viewport
,
105 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
106 (draw
->vs
.edgeflag_output
? TRUE
: FALSE
) );
108 draw_pt_so_emit_prepare( fpme
->so_emit
);
110 if (!(opt
& PT_PIPELINE
)) {
111 draw_pt_emit_prepare( fpme
->emit
,
115 *max_vertices
= MAX2( *max_vertices
,
116 DRAW_PIPE_MAX_VERTICES
);
119 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
122 /* return even number */
123 *max_vertices
= *max_vertices
& ~1;
125 /* No need to prepare the shader.
127 vs
->prepare(vs
, draw
);
131 static void fetch( struct pt_fetch
*fetch
,
132 const struct draw_fetch_info
*fetch_info
,
135 if (fetch_info
->linear
) {
136 draw_pt_fetch_run_linear( fetch
,
142 draw_pt_fetch_run( fetch
,
150 static void pipeline(struct fetch_pipeline_middle_end
*fpme
,
151 const struct draw_vertex_info
*vert_info
,
152 const struct draw_prim_info
*prim_info
)
154 if (prim_info
->linear
)
155 draw_pipeline_run_linear( fpme
->draw
,
159 draw_pipeline_run( fpme
->draw
,
164 static void emit(struct pt_emit
*emit
,
165 const struct draw_vertex_info
*vert_info
,
166 const struct draw_prim_info
*prim_info
)
168 if (prim_info
->linear
) {
169 draw_pt_emit_linear(emit
, vert_info
, prim_info
);
172 draw_pt_emit(emit
, vert_info
, prim_info
);
177 static void draw_vertex_shader_run(struct draw_vertex_shader
*vshader
,
178 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
179 unsigned const_size
[PIPE_MAX_CONSTANT_BUFFERS
],
180 const struct draw_vertex_info
*input_verts
,
181 struct draw_vertex_info
*output_verts
)
183 output_verts
->vertex_size
= input_verts
->vertex_size
;
184 output_verts
->stride
= input_verts
->vertex_size
;
185 output_verts
->count
= input_verts
->count
;
186 output_verts
->verts
=
187 (struct vertex_header
*)MALLOC(output_verts
->vertex_size
*
188 align(output_verts
->count
, 4));
190 vshader
->run_linear(vshader
,
191 (const float (*)[4])input_verts
->verts
->data
,
192 ( float (*)[4])output_verts
->verts
->data
,
196 input_verts
->vertex_size
,
197 input_verts
->vertex_size
);
200 static void fetch_pipeline_generic( struct draw_pt_middle_end
*middle
,
201 const struct draw_fetch_info
*fetch_info
,
202 const struct draw_prim_info
*prim_info
)
204 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
205 struct draw_context
*draw
= fpme
->draw
;
206 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
207 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
208 struct draw_prim_info gs_prim_info
;
209 struct draw_vertex_info fetched_vert_info
;
210 struct draw_vertex_info vs_vert_info
;
211 struct draw_vertex_info gs_vert_info
;
212 struct draw_vertex_info
*vert_info
;
213 unsigned opt
= fpme
->opt
;
215 fetched_vert_info
.count
= fetch_info
->count
;
216 fetched_vert_info
.vertex_size
= fpme
->vertex_size
;
217 fetched_vert_info
.stride
= fpme
->vertex_size
;
218 fetched_vert_info
.verts
=
219 (struct vertex_header
*)MALLOC(fpme
->vertex_size
*
220 align(fetch_info
->count
, 4));
221 if (!fetched_vert_info
.verts
) {
226 /* Fetch into our vertex buffer.
228 fetch( fpme
->fetch
, fetch_info
, (char *)fetched_vert_info
.verts
);
230 /* Finished with fetch:
233 vert_info
= &fetched_vert_info
;
235 /* Run the shader, note that this overwrites the data[] parts of
236 * the pipeline verts.
238 if (fpme
->opt
& PT_SHADE
) {
239 draw_vertex_shader_run(vshader
,
240 draw
->pt
.user
.vs_constants
,
241 draw
->pt
.user
.vs_constants_size
,
245 FREE(vert_info
->verts
);
246 vert_info
= &vs_vert_info
;
249 if ((fpme
->opt
& PT_SHADE
) && gshader
) {
250 draw_geometry_shader_run(gshader
,
251 draw
->pt
.user
.gs_constants
,
252 draw
->pt
.user
.gs_constants_size
,
258 FREE(vert_info
->verts
);
259 vert_info
= &gs_vert_info
;
260 prim_info
= &gs_prim_info
;
264 /* Stream output needs to be done before clipping.
266 * XXX: Stream output surely needs to respect the prim_info->elt
269 draw_pt_so_emit( fpme
->so_emit
,
273 if (draw_pt_post_vs_run( fpme
->post_vs
,
279 /* Do we need to run the pipeline?
281 if (opt
& PT_PIPELINE
) {
291 FREE(vert_info
->verts
);
294 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
295 const unsigned *fetch_elts
,
296 unsigned fetch_count
,
297 const ushort
*draw_elts
,
298 unsigned draw_count
)
300 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
301 struct draw_fetch_info fetch_info
;
302 struct draw_prim_info prim_info
;
304 fetch_info
.linear
= FALSE
;
305 fetch_info
.start
= 0;
306 fetch_info
.elts
= fetch_elts
;
307 fetch_info
.count
= fetch_count
;
309 prim_info
.linear
= FALSE
;
311 prim_info
.count
= draw_count
;
312 prim_info
.elts
= draw_elts
;
313 prim_info
.prim
= fpme
->input_prim
;
314 prim_info
.primitive_count
= 1;
315 prim_info
.primitive_lengths
= &draw_count
;
317 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
321 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
325 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
326 struct draw_fetch_info fetch_info
;
327 struct draw_prim_info prim_info
;
329 fetch_info
.linear
= TRUE
;
330 fetch_info
.start
= start
;
331 fetch_info
.count
= count
;
332 fetch_info
.elts
= NULL
;
334 prim_info
.linear
= TRUE
;
336 prim_info
.count
= count
;
337 prim_info
.elts
= NULL
;
338 prim_info
.prim
= fpme
->input_prim
;
339 prim_info
.primitive_count
= 1;
340 prim_info
.primitive_lengths
= &count
;
342 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
347 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
350 const ushort
*draw_elts
,
351 unsigned draw_count
)
353 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
354 struct draw_fetch_info fetch_info
;
355 struct draw_prim_info prim_info
;
357 fetch_info
.linear
= TRUE
;
358 fetch_info
.start
= start
;
359 fetch_info
.count
= count
;
360 fetch_info
.elts
= NULL
;
362 prim_info
.linear
= FALSE
;
364 prim_info
.count
= draw_count
;
365 prim_info
.elts
= draw_elts
;
366 prim_info
.prim
= fpme
->input_prim
;
367 prim_info
.primitive_count
= 1;
368 prim_info
.primitive_lengths
= &draw_count
;
370 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
377 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
382 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
384 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
387 draw_pt_fetch_destroy( fpme
->fetch
);
390 draw_pt_emit_destroy( fpme
->emit
);
393 draw_pt_so_emit_destroy( fpme
->so_emit
);
396 draw_pt_post_vs_destroy( fpme
->post_vs
);
402 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
404 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
408 fpme
->base
.prepare
= fetch_pipeline_prepare
;
409 fpme
->base
.run
= fetch_pipeline_run
;
410 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
411 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
412 fpme
->base
.finish
= fetch_pipeline_finish
;
413 fpme
->base
.destroy
= fetch_pipeline_destroy
;
417 fpme
->fetch
= draw_pt_fetch_create( draw
);
421 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
425 fpme
->emit
= draw_pt_emit_create( draw
);
429 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
437 fetch_pipeline_destroy( &fpme
->base
);