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
;
55 static int max_out_vertex_count(
56 struct fetch_pipeline_middle_end
*fpme
, int count
)
58 struct draw_context
*draw
= fpme
->draw
;
59 unsigned alloc_count
= align( count
, 4 );
61 if (draw
->gs
.geometry_shader
) {
62 unsigned input_primitives
= count
/ u_vertices_per_prim(fpme
->input_prim
);
63 /* max GS output is number of input primitives * max output
64 * vertices per each invocation */
65 unsigned gs_max_verts
= input_primitives
*
66 draw
->gs
.geometry_shader
->max_output_vertices
;
67 if (gs_max_verts
> count
)
68 alloc_count
= align(gs_max_verts
, 4);
70 /*debug_printf("------- alloc count = %d (input = %d)\n",
71 alloc_count, count);*/
75 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
79 unsigned *max_vertices
)
81 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
82 struct draw_context
*draw
= fpme
->draw
;
83 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
85 unsigned instance_id_index
= ~0;
87 /* Add one to num_outputs because the pipeline occasionally tags on
88 * an additional texcoord, eg for AA lines.
90 unsigned nr
= MAX2( vs
->info
.num_inputs
,
91 vs
->info
.num_outputs
+ 1 );
93 /* Scan for instanceID system value.
95 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
96 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
97 instance_id_index
= i
;
102 fpme
->input_prim
= in_prim
;
103 fpme
->output_prim
= out_prim
;
106 /* Always leave room for the vertex header whether we need it or
107 * not. It's hard to get rid of it in particular because of the
108 * viewport code in draw_pt_post_vs.c.
110 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
114 draw_pt_fetch_prepare( fpme
->fetch
,
118 /* XXX: it's not really gl rasterization rules we care about here,
119 * but gl vs dx9 clip spaces.
121 draw_pt_post_vs_prepare( fpme
->post_vs
,
122 (boolean
)draw
->bypass_clipping
,
123 (boolean
)draw
->identity_viewport
,
124 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
125 (draw
->vs
.edgeflag_output
? true : false) );
127 draw_pt_so_emit_prepare( fpme
->so_emit
, out_prim
);
129 if (!(opt
& PT_PIPELINE
)) {
130 draw_pt_emit_prepare( fpme
->emit
,
134 *max_vertices
= MAX2( *max_vertices
,
135 DRAW_PIPE_MAX_VERTICES
);
138 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
141 /* return even number */
142 *max_vertices
= *max_vertices
& ~1;
144 /* No need to prepare the shader.
146 vs
->prepare(vs
, draw
);
151 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
152 const unsigned *fetch_elts
,
153 unsigned fetch_count
,
154 const ushort
*draw_elts
,
155 unsigned draw_count
)
157 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
158 struct draw_context
*draw
= fpme
->draw
;
159 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
160 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
161 unsigned opt
= fpme
->opt
;
162 struct vertex_header
*pipeline_verts
;
163 unsigned alloc_count
= max_out_vertex_count(fpme
, fetch_count
);
166 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
168 if (!pipeline_verts
) {
169 /* Not much we can do here - just skip the rendering.
175 /* Fetch into our vertex buffer
177 draw_pt_fetch_run( fpme
->fetch
,
180 (char *)pipeline_verts
);
182 /* Run the shader, note that this overwrites the data[] parts of
183 * the pipeline verts.
187 vshader
->run_linear(vshader
,
188 (const float (*)[4])pipeline_verts
->data
,
189 ( float (*)[4])pipeline_verts
->data
,
190 draw
->pt
.user
.vs_constants
,
196 draw_geometry_shader_run(gshader
,
197 (const float (*)[4])pipeline_verts
->data
,
198 ( float (*)[4])pipeline_verts
->data
,
199 draw
->pt
.user
.gs_constants
,
205 /* stream output needs to be done before clipping */
206 draw_pt_so_emit( fpme
->so_emit
,
207 (const float (*)[4])pipeline_verts
->data
,
211 if (draw_pt_post_vs_run( fpme
->post_vs
,
219 /* Do we need to run the pipeline?
221 if (opt
& PT_PIPELINE
) {
222 draw_pipeline_run( fpme
->draw
,
231 draw_pt_emit( fpme
->emit
,
232 (const float (*)[4])pipeline_verts
->data
,
240 FREE(pipeline_verts
);
244 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
248 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
249 struct draw_context
*draw
= fpme
->draw
;
250 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
251 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
252 unsigned opt
= fpme
->opt
;
253 struct vertex_header
*pipeline_verts
;
254 unsigned alloc_count
= max_out_vertex_count(fpme
, count
);
257 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
259 if (!pipeline_verts
) {
260 /* Not much we can do here - just skip the rendering.
266 /* Fetch into our vertex buffer
268 draw_pt_fetch_run_linear( fpme
->fetch
,
271 (char *)pipeline_verts
);
273 /* Run the shader, note that this overwrites the data[] parts of
274 * the pipeline verts.
278 shader
->run_linear(shader
,
279 (const float (*)[4])pipeline_verts
->data
,
280 ( float (*)[4])pipeline_verts
->data
,
281 draw
->pt
.user
.vs_constants
,
287 count
= draw_geometry_shader_run(geometry_shader
,
288 (const float (*)[4])pipeline_verts
->data
,
289 ( float (*)[4])pipeline_verts
->data
,
290 draw
->pt
.user
.gs_constants
,
296 /* stream output needs to be done before clipping */
297 draw_pt_so_emit( fpme
->so_emit
,
298 (const float (*)[4])pipeline_verts
->data
,
302 if (draw_pt_post_vs_run( fpme
->post_vs
,
310 /* Do we need to run the pipeline?
312 if (opt
& PT_PIPELINE
) {
313 draw_pipeline_run_linear( fpme
->draw
,
320 draw_pt_emit_linear( fpme
->emit
,
321 (const float (*)[4])pipeline_verts
->data
,
326 FREE(pipeline_verts
);
331 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
334 const ushort
*draw_elts
,
335 unsigned draw_count
)
337 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
338 struct draw_context
*draw
= fpme
->draw
;
339 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
340 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
341 unsigned opt
= fpme
->opt
;
342 struct vertex_header
*pipeline_verts
;
343 unsigned alloc_count
= max_out_vertex_count(fpme
, count
);
346 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
351 /* Fetch into our vertex buffer
353 draw_pt_fetch_run_linear( fpme
->fetch
,
356 (char *)pipeline_verts
);
358 /* Run the shader, note that this overwrites the data[] parts of
359 * the pipeline verts.
363 shader
->run_linear(shader
,
364 (const float (*)[4])pipeline_verts
->data
,
365 ( float (*)[4])pipeline_verts
->data
,
366 draw
->pt
.user
.vs_constants
,
372 count
= draw_geometry_shader_run(geometry_shader
,
373 (const float (*)[4])pipeline_verts
->data
,
374 ( float (*)[4])pipeline_verts
->data
,
375 draw
->pt
.user
.gs_constants
,
381 /* stream output needs to be done before clipping */
382 draw_pt_so_emit( fpme
->so_emit
,
383 (const float (*)[4])pipeline_verts
->data
,
387 if (draw_pt_post_vs_run( fpme
->post_vs
,
395 /* Do we need to run the pipeline?
397 if (opt
& PT_PIPELINE
) {
398 draw_pipeline_run( fpme
->draw
,
407 draw_pt_emit( fpme
->emit
,
408 (const float (*)[4])pipeline_verts
->data
,
415 FREE(pipeline_verts
);
421 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
426 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
428 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
431 draw_pt_fetch_destroy( fpme
->fetch
);
434 draw_pt_emit_destroy( fpme
->emit
);
437 draw_pt_so_emit_destroy( fpme
->so_emit
);
440 draw_pt_post_vs_destroy( fpme
->post_vs
);
446 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
448 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
452 fpme
->base
.prepare
= fetch_pipeline_prepare
;
453 fpme
->base
.run
= fetch_pipeline_run
;
454 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
455 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
456 fpme
->base
.finish
= fetch_pipeline_finish
;
457 fpme
->base
.destroy
= fetch_pipeline_destroy
;
461 fpme
->fetch
= draw_pt_fetch_create( draw
);
465 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
469 fpme
->emit
= draw_pt_emit_create( draw
);
473 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
481 fetch_pipeline_destroy( &fpme
->base
);