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 "draw/draw_context.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
34 #include "draw/draw_vs.h"
35 #include "draw/draw_gs.h"
38 struct fetch_pipeline_middle_end
{
39 struct draw_pt_middle_end base
;
40 struct draw_context
*draw
;
43 struct pt_so_emit
*so_emit
;
44 struct pt_fetch
*fetch
;
45 struct pt_post_vs
*post_vs
;
47 unsigned vertex_data_offset
;
55 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
59 unsigned *max_vertices
)
61 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
62 struct draw_context
*draw
= fpme
->draw
;
63 struct draw_vertex_shader
*vs
= draw
->vs
.vertex_shader
;
65 unsigned instance_id_index
= ~0;
67 /* Add one to num_outputs because the pipeline occasionally tags on
68 * an additional texcoord, eg for AA lines.
70 unsigned nr
= MAX2( vs
->info
.num_inputs
,
71 vs
->info
.num_outputs
+ 1 );
73 /* Scan for instanceID system value.
75 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
76 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
77 instance_id_index
= i
;
82 fpme
->input_prim
= in_prim
;
83 fpme
->output_prim
= out_prim
;
86 /* Always leave room for the vertex header whether we need it or
87 * not. It's hard to get rid of it in particular because of the
88 * viewport code in draw_pt_post_vs.c.
90 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
94 draw_pt_fetch_prepare( fpme
->fetch
,
98 /* XXX: it's not really gl rasterization rules we care about here,
99 * but gl vs dx9 clip spaces.
101 draw_pt_post_vs_prepare( fpme
->post_vs
,
102 (boolean
)draw
->bypass_clipping
,
103 (boolean
)draw
->identity_viewport
,
104 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
105 (draw
->vs
.edgeflag_output
? true : false) );
107 draw_pt_so_emit_prepare( fpme
->so_emit
, out_prim
);
109 if (!(opt
& PT_PIPELINE
)) {
110 draw_pt_emit_prepare( fpme
->emit
,
114 *max_vertices
= MAX2( *max_vertices
,
115 DRAW_PIPE_MAX_VERTICES
);
118 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
121 /* return even number */
122 *max_vertices
= *max_vertices
& ~1;
124 /* No need to prepare the shader.
126 vs
->prepare(vs
, draw
);
131 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
132 const unsigned *fetch_elts
,
133 unsigned fetch_count
,
134 const ushort
*draw_elts
,
135 unsigned draw_count
)
137 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
138 struct draw_context
*draw
= fpme
->draw
;
139 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
140 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
141 unsigned opt
= fpme
->opt
;
142 struct vertex_header
*pipeline_verts
;
143 unsigned alloc_count
= align( fetch_count
, 4 );
145 if (draw
->gs
.geometry_shader
&&
146 draw
->gs
.geometry_shader
->max_output_vertices
> fetch_count
) {
147 alloc_count
= align(draw
->gs
.geometry_shader
->max_output_vertices
, 4);
151 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
153 if (!pipeline_verts
) {
154 /* Not much we can do here - just skip the rendering.
160 /* Fetch into our vertex buffer
162 draw_pt_fetch_run( fpme
->fetch
,
165 (char *)pipeline_verts
);
167 /* Run the shader, note that this overwrites the data[] parts of
168 * the pipeline verts.
172 vshader
->run_linear(vshader
,
173 (const float (*)[4])pipeline_verts
->data
,
174 ( float (*)[4])pipeline_verts
->data
,
175 draw
->pt
.user
.vs_constants
,
181 draw_geometry_shader_run(gshader
,
182 (const float (*)[4])pipeline_verts
->data
,
183 ( float (*)[4])pipeline_verts
->data
,
184 draw
->pt
.user
.gs_constants
,
190 /* stream output needs to be done before clipping */
191 draw_pt_so_emit( fpme
->so_emit
,
192 (const float (*)[4])pipeline_verts
->data
,
196 if (draw_pt_post_vs_run( fpme
->post_vs
,
204 /* Do we need to run the pipeline?
206 if (opt
& PT_PIPELINE
) {
207 draw_pipeline_run( fpme
->draw
,
216 draw_pt_emit( fpme
->emit
,
217 (const float (*)[4])pipeline_verts
->data
,
225 FREE(pipeline_verts
);
229 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
233 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
234 struct draw_context
*draw
= fpme
->draw
;
235 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
236 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
237 unsigned opt
= fpme
->opt
;
238 struct vertex_header
*pipeline_verts
;
239 unsigned alloc_count
= align( count
, 4 );
241 if (geometry_shader
&& geometry_shader
->max_output_vertices
> count
) {
242 alloc_count
= align(geometry_shader
->max_output_vertices
, 4);
246 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
248 if (!pipeline_verts
) {
249 /* Not much we can do here - just skip the rendering.
255 /* Fetch into our vertex buffer
257 draw_pt_fetch_run_linear( fpme
->fetch
,
260 (char *)pipeline_verts
);
262 /* Run the shader, note that this overwrites the data[] parts of
263 * the pipeline verts.
267 shader
->run_linear(shader
,
268 (const float (*)[4])pipeline_verts
->data
,
269 ( float (*)[4])pipeline_verts
->data
,
270 draw
->pt
.user
.vs_constants
,
276 count
= draw_geometry_shader_run(geometry_shader
,
277 (const float (*)[4])pipeline_verts
->data
,
278 ( float (*)[4])pipeline_verts
->data
,
279 draw
->pt
.user
.gs_constants
,
285 /* stream output needs to be done before clipping */
286 draw_pt_so_emit( fpme
->so_emit
,
287 (const float (*)[4])pipeline_verts
->data
,
291 if (draw_pt_post_vs_run( fpme
->post_vs
,
299 /* Do we need to run the pipeline?
301 if (opt
& PT_PIPELINE
) {
302 draw_pipeline_run_linear( fpme
->draw
,
309 draw_pt_emit_linear( fpme
->emit
,
310 (const float (*)[4])pipeline_verts
->data
,
315 FREE(pipeline_verts
);
320 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
323 const ushort
*draw_elts
,
324 unsigned draw_count
)
326 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
327 struct draw_context
*draw
= fpme
->draw
;
328 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
329 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
330 unsigned opt
= fpme
->opt
;
331 struct vertex_header
*pipeline_verts
;
332 unsigned alloc_count
= align( count
, 4 );
334 if (draw
->gs
.geometry_shader
&&
335 draw
->gs
.geometry_shader
->max_output_vertices
> count
) {
336 alloc_count
= align(draw
->gs
.geometry_shader
->max_output_vertices
, 4);
340 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
345 /* Fetch into our vertex buffer
347 draw_pt_fetch_run_linear( fpme
->fetch
,
350 (char *)pipeline_verts
);
352 /* Run the shader, note that this overwrites the data[] parts of
353 * the pipeline verts.
357 shader
->run_linear(shader
,
358 (const float (*)[4])pipeline_verts
->data
,
359 ( float (*)[4])pipeline_verts
->data
,
360 draw
->pt
.user
.vs_constants
,
366 count
= draw_geometry_shader_run(geometry_shader
,
367 (const float (*)[4])pipeline_verts
->data
,
368 ( float (*)[4])pipeline_verts
->data
,
369 draw
->pt
.user
.gs_constants
,
375 /* stream output needs to be done before clipping */
376 draw_pt_so_emit( fpme
->so_emit
,
377 (const float (*)[4])pipeline_verts
->data
,
381 if (draw_pt_post_vs_run( fpme
->post_vs
,
389 /* Do we need to run the pipeline?
391 if (opt
& PT_PIPELINE
) {
392 draw_pipeline_run( fpme
->draw
,
401 draw_pt_emit( fpme
->emit
,
402 (const float (*)[4])pipeline_verts
->data
,
409 FREE(pipeline_verts
);
415 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
420 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
422 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
425 draw_pt_fetch_destroy( fpme
->fetch
);
428 draw_pt_emit_destroy( fpme
->emit
);
431 draw_pt_so_emit_destroy( fpme
->so_emit
);
434 draw_pt_post_vs_destroy( fpme
->post_vs
);
440 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
442 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
446 fpme
->base
.prepare
= fetch_pipeline_prepare
;
447 fpme
->base
.run
= fetch_pipeline_run
;
448 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
449 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
450 fpme
->base
.finish
= fetch_pipeline_finish
;
451 fpme
->base
.destroy
= fetch_pipeline_destroy
;
455 fpme
->fetch
= draw_pt_fetch_create( draw
);
459 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
463 fpme
->emit
= draw_pt_emit_create( draw
);
467 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
475 fetch_pipeline_destroy( &fpme
->base
);