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 const struct draw_vertex_info
*input_verts
,
180 struct draw_vertex_info
*output_verts
)
182 output_verts
->vertex_size
= input_verts
->vertex_size
;
183 output_verts
->stride
= input_verts
->vertex_size
;
184 output_verts
->count
= input_verts
->count
;
185 output_verts
->verts
=
186 (struct vertex_header
*)MALLOC(output_verts
->vertex_size
*
187 align(output_verts
->count
, 4));
189 vshader
->run_linear(vshader
,
190 (const float (*)[4])input_verts
->verts
->data
,
191 ( float (*)[4])output_verts
->verts
->data
,
194 input_verts
->vertex_size
,
195 input_verts
->vertex_size
);
198 static void fetch_pipeline_generic( struct draw_pt_middle_end
*middle
,
199 const struct draw_fetch_info
*fetch_info
,
200 const struct draw_prim_info
*prim_info
)
202 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
203 struct draw_context
*draw
= fpme
->draw
;
204 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
205 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
206 struct draw_prim_info gs_prim_info
;
207 struct draw_vertex_info fetched_vert_info
;
208 struct draw_vertex_info vs_vert_info
;
209 struct draw_vertex_info gs_vert_info
;
210 struct draw_vertex_info
*vert_info
;
211 unsigned opt
= fpme
->opt
;
213 fetched_vert_info
.count
= fetch_info
->count
;
214 fetched_vert_info
.vertex_size
= fpme
->vertex_size
;
215 fetched_vert_info
.stride
= fpme
->vertex_size
;
216 fetched_vert_info
.verts
=
217 (struct vertex_header
*)MALLOC(fpme
->vertex_size
*
218 align(fetch_info
->count
, 4));
219 if (!fetched_vert_info
.verts
) {
224 /* Fetch into our vertex buffer.
226 fetch( fpme
->fetch
, fetch_info
, (char *)fetched_vert_info
.verts
);
228 /* Finished with fetch:
231 vert_info
= &fetched_vert_info
;
233 /* Run the shader, note that this overwrites the data[] parts of
234 * the pipeline verts.
236 if (fpme
->opt
& PT_SHADE
) {
237 draw_vertex_shader_run(vshader
,
238 draw
->pt
.user
.vs_constants
,
242 FREE(vert_info
->verts
);
243 vert_info
= &vs_vert_info
;
246 if ((fpme
->opt
& PT_SHADE
) && gshader
) {
247 draw_geometry_shader_run(gshader
,
248 draw
->pt
.user
.gs_constants
,
254 FREE(vert_info
->verts
);
255 vert_info
= &gs_vert_info
;
256 prim_info
= &gs_prim_info
;
260 /* Stream output needs to be done before clipping.
262 * XXX: Stream output surely needs to respect the prim_info->elt
265 draw_pt_so_emit( fpme
->so_emit
,
269 if (draw_pt_post_vs_run( fpme
->post_vs
,
275 /* Do we need to run the pipeline?
277 if (opt
& PT_PIPELINE
) {
287 FREE(vert_info
->verts
);
290 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
291 const unsigned *fetch_elts
,
292 unsigned fetch_count
,
293 const ushort
*draw_elts
,
294 unsigned draw_count
)
296 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
297 struct draw_fetch_info fetch_info
;
298 struct draw_prim_info prim_info
;
300 fetch_info
.linear
= FALSE
;
301 fetch_info
.start
= 0;
302 fetch_info
.elts
= fetch_elts
;
303 fetch_info
.count
= fetch_count
;
305 prim_info
.linear
= FALSE
;
307 prim_info
.count
= draw_count
;
308 prim_info
.elts
= draw_elts
;
309 prim_info
.prim
= fpme
->input_prim
;
310 prim_info
.primitive_count
= 1;
311 prim_info
.primitive_lengths
= &draw_count
;
313 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
317 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
321 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
322 struct draw_fetch_info fetch_info
;
323 struct draw_prim_info prim_info
;
325 fetch_info
.linear
= TRUE
;
326 fetch_info
.start
= start
;
327 fetch_info
.count
= count
;
328 fetch_info
.elts
= NULL
;
330 prim_info
.linear
= TRUE
;
332 prim_info
.count
= count
;
333 prim_info
.elts
= NULL
;
334 prim_info
.prim
= fpme
->input_prim
;
335 prim_info
.primitive_count
= 1;
336 prim_info
.primitive_lengths
= &count
;
338 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
343 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
346 const ushort
*draw_elts
,
347 unsigned draw_count
)
349 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
350 struct draw_fetch_info fetch_info
;
351 struct draw_prim_info prim_info
;
353 fetch_info
.linear
= TRUE
;
354 fetch_info
.start
= start
;
355 fetch_info
.count
= count
;
356 fetch_info
.elts
= NULL
;
358 prim_info
.linear
= FALSE
;
360 prim_info
.count
= draw_count
;
361 prim_info
.elts
= draw_elts
;
362 prim_info
.prim
= fpme
->input_prim
;
363 prim_info
.primitive_count
= 1;
364 prim_info
.primitive_lengths
= &draw_count
;
366 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
373 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
378 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
380 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
383 draw_pt_fetch_destroy( fpme
->fetch
);
386 draw_pt_emit_destroy( fpme
->emit
);
389 draw_pt_so_emit_destroy( fpme
->so_emit
);
392 draw_pt_post_vs_destroy( fpme
->post_vs
);
398 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
400 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
404 fpme
->base
.prepare
= fetch_pipeline_prepare
;
405 fpme
->base
.run
= fetch_pipeline_run
;
406 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
407 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
408 fpme
->base
.finish
= fetch_pipeline_finish
;
409 fpme
->base
.destroy
= fetch_pipeline_destroy
;
413 fpme
->fetch
= draw_pt_fetch_create( draw
);
417 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
421 fpme
->emit
= draw_pt_emit_create( draw
);
425 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
433 fetch_pipeline_destroy( &fpme
->base
);