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
;
56 * Prepare/validate middle part of the vertex pipeline.
57 * NOTE: if you change this function, also look at the LLVM
58 * function llvm_middle_end_prepare() for similar changes.
60 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
63 unsigned *max_vertices
)
65 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
66 struct draw_context
*draw
= fpme
->draw
;
67 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
68 struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
70 unsigned instance_id_index
= ~0;
72 unsigned gs_out_prim
= (gs
? gs
->output_primitive
: prim
);
74 /* Add one to num_outputs because the pipeline occasionally tags on
75 * an additional texcoord, eg for AA lines.
77 unsigned nr
= MAX2( vs
->info
.num_inputs
,
78 vs
->info
.num_outputs
+ 1 );
81 nr
= MAX2(nr
, gs
->info
.num_outputs
+ 1);
84 /* Scan for instanceID system value.
86 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
87 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
88 instance_id_index
= i
;
93 fpme
->input_prim
= prim
;
96 /* Always leave room for the vertex header whether we need it or
97 * not. It's hard to get rid of it in particular because of the
98 * viewport code in draw_pt_post_vs.c.
100 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
104 draw_pt_fetch_prepare( fpme
->fetch
,
108 /* XXX: it's not really gl rasterization rules we care about here,
109 * but gl vs dx9 clip spaces.
111 draw_pt_post_vs_prepare( fpme
->post_vs
,
116 draw
->identity_viewport
,
117 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
118 (draw
->vs
.edgeflag_output
? TRUE
: FALSE
) );
120 draw_pt_so_emit_prepare( fpme
->so_emit
, FALSE
);
122 if (!(opt
& PT_PIPELINE
)) {
123 draw_pt_emit_prepare( fpme
->emit
,
127 *max_vertices
= MAX2( *max_vertices
, 4096 );
130 /* limit max fetches by limiting max_vertices */
131 *max_vertices
= 4096;
134 /* No need to prepare the shader.
136 vs
->prepare(vs
, draw
);
141 fetch_pipeline_bind_parameters(struct draw_pt_middle_end
*middle
)
143 /* No-op since the vertex shader executor and drawing pipeline
144 * just grab the constants, viewport, etc. from the draw context state.
149 static void fetch( struct pt_fetch
*fetch
,
150 const struct draw_fetch_info
*fetch_info
,
153 if (fetch_info
->linear
) {
154 draw_pt_fetch_run_linear( fetch
,
160 draw_pt_fetch_run( fetch
,
168 static void pipeline(struct fetch_pipeline_middle_end
*fpme
,
169 const struct draw_vertex_info
*vert_info
,
170 const struct draw_prim_info
*prim_info
)
172 if (prim_info
->linear
)
173 draw_pipeline_run_linear( fpme
->draw
,
177 draw_pipeline_run( fpme
->draw
,
182 static void emit(struct pt_emit
*emit
,
183 const struct draw_vertex_info
*vert_info
,
184 const struct draw_prim_info
*prim_info
)
186 if (prim_info
->linear
) {
187 draw_pt_emit_linear(emit
, vert_info
, prim_info
);
190 draw_pt_emit(emit
, vert_info
, prim_info
);
195 static void draw_vertex_shader_run(struct draw_vertex_shader
*vshader
,
196 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
197 unsigned const_size
[PIPE_MAX_CONSTANT_BUFFERS
],
198 const struct draw_vertex_info
*input_verts
,
199 struct draw_vertex_info
*output_verts
)
201 output_verts
->vertex_size
= input_verts
->vertex_size
;
202 output_verts
->stride
= input_verts
->vertex_size
;
203 output_verts
->count
= input_verts
->count
;
204 output_verts
->verts
=
205 (struct vertex_header
*)MALLOC(output_verts
->vertex_size
*
206 align(output_verts
->count
, 4));
208 vshader
->run_linear(vshader
,
209 (const float (*)[4])input_verts
->verts
->data
,
210 ( float (*)[4])output_verts
->verts
->data
,
214 input_verts
->vertex_size
,
215 input_verts
->vertex_size
);
218 static void fetch_pipeline_generic( struct draw_pt_middle_end
*middle
,
219 const struct draw_fetch_info
*fetch_info
,
220 const struct draw_prim_info
*prim_info
)
222 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
223 struct draw_context
*draw
= fpme
->draw
;
224 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
225 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
226 struct draw_prim_info gs_prim_info
;
227 struct draw_vertex_info fetched_vert_info
;
228 struct draw_vertex_info vs_vert_info
;
229 struct draw_vertex_info gs_vert_info
;
230 struct draw_vertex_info
*vert_info
;
231 unsigned opt
= fpme
->opt
;
233 fetched_vert_info
.count
= fetch_info
->count
;
234 fetched_vert_info
.vertex_size
= fpme
->vertex_size
;
235 fetched_vert_info
.stride
= fpme
->vertex_size
;
236 fetched_vert_info
.verts
=
237 (struct vertex_header
*)MALLOC(fpme
->vertex_size
*
238 align(fetch_info
->count
, 4));
239 if (!fetched_vert_info
.verts
) {
243 if (draw
->collect_statistics
) {
244 draw
->statistics
.ia_vertices
+= fetch_info
->count
;
245 draw
->statistics
.ia_primitives
+=
246 u_decomposed_prims_for_vertices(prim_info
->prim
, fetch_info
->count
);
247 draw
->statistics
.vs_invocations
+= fetch_info
->count
;
250 /* Fetch into our vertex buffer.
252 fetch( fpme
->fetch
, fetch_info
, (char *)fetched_vert_info
.verts
);
254 /* Finished with fetch:
257 vert_info
= &fetched_vert_info
;
259 /* Run the shader, note that this overwrites the data[] parts of
260 * the pipeline verts.
262 if (fpme
->opt
& PT_SHADE
) {
263 draw_vertex_shader_run(vshader
,
264 draw
->pt
.user
.vs_constants
,
265 draw
->pt
.user
.vs_constants_size
,
269 FREE(vert_info
->verts
);
270 vert_info
= &vs_vert_info
;
273 if ((fpme
->opt
& PT_SHADE
) && gshader
) {
274 draw_geometry_shader_run(gshader
,
275 draw
->pt
.user
.gs_constants
,
276 draw
->pt
.user
.gs_constants_size
,
283 FREE(vert_info
->verts
);
284 vert_info
= &gs_vert_info
;
285 prim_info
= &gs_prim_info
;
289 /* Stream output needs to be done before clipping.
291 * XXX: Stream output surely needs to respect the prim_info->elt
294 draw_pt_so_emit( fpme
->so_emit
, vert_info
, prim_info
);
297 * if there's no position, need to stop now, or the latter stages
298 * will try to access non-existent position output.
300 if (draw_current_shader_position_output(draw
) != -1) {
302 if (draw_pt_post_vs_run( fpme
->post_vs
, vert_info
))
307 /* Do we need to run the pipeline?
309 if (opt
& PT_PIPELINE
) {
310 pipeline( fpme
, vert_info
, prim_info
);
313 emit( fpme
->emit
, vert_info
, prim_info
);
316 FREE(vert_info
->verts
);
319 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
320 const unsigned *fetch_elts
,
321 unsigned fetch_count
,
322 const ushort
*draw_elts
,
324 unsigned prim_flags
)
326 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
327 struct draw_fetch_info fetch_info
;
328 struct draw_prim_info prim_info
;
330 fetch_info
.linear
= FALSE
;
331 fetch_info
.start
= 0;
332 fetch_info
.elts
= fetch_elts
;
333 fetch_info
.count
= fetch_count
;
335 prim_info
.linear
= FALSE
;
337 prim_info
.count
= draw_count
;
338 prim_info
.elts
= draw_elts
;
339 prim_info
.prim
= fpme
->input_prim
;
340 prim_info
.flags
= prim_flags
;
341 prim_info
.primitive_count
= 1;
342 prim_info
.primitive_lengths
= &draw_count
;
344 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
348 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
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
= TRUE
;
364 prim_info
.count
= count
;
365 prim_info
.elts
= NULL
;
366 prim_info
.prim
= fpme
->input_prim
;
367 prim_info
.flags
= prim_flags
;
368 prim_info
.primitive_count
= 1;
369 prim_info
.primitive_lengths
= &count
;
371 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
376 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
379 const ushort
*draw_elts
,
381 unsigned prim_flags
)
383 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
384 struct draw_fetch_info fetch_info
;
385 struct draw_prim_info prim_info
;
387 fetch_info
.linear
= TRUE
;
388 fetch_info
.start
= start
;
389 fetch_info
.count
= count
;
390 fetch_info
.elts
= NULL
;
392 prim_info
.linear
= FALSE
;
394 prim_info
.count
= draw_count
;
395 prim_info
.elts
= draw_elts
;
396 prim_info
.prim
= fpme
->input_prim
;
397 prim_info
.flags
= prim_flags
;
398 prim_info
.primitive_count
= 1;
399 prim_info
.primitive_lengths
= &draw_count
;
401 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
408 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
413 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
415 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
418 draw_pt_fetch_destroy( fpme
->fetch
);
421 draw_pt_emit_destroy( fpme
->emit
);
424 draw_pt_so_emit_destroy( fpme
->so_emit
);
427 draw_pt_post_vs_destroy( fpme
->post_vs
);
433 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
435 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
439 fpme
->base
.prepare
= fetch_pipeline_prepare
;
440 fpme
->base
.bind_parameters
= fetch_pipeline_bind_parameters
;
441 fpme
->base
.run
= fetch_pipeline_run
;
442 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
443 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
444 fpme
->base
.finish
= fetch_pipeline_finish
;
445 fpme
->base
.destroy
= fetch_pipeline_destroy
;
449 fpme
->fetch
= draw_pt_fetch_create( draw
);
453 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
457 fpme
->emit
= draw_pt_emit_create( draw
);
461 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
469 fetch_pipeline_destroy( &fpme
->base
);