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 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
);
130 static void fetch( struct pt_fetch
*fetch
,
131 const struct draw_fetch_info
*fetch_info
,
134 if (fetch_info
->linear
) {
135 draw_pt_fetch_run_linear( fetch
,
141 draw_pt_fetch_run( fetch
,
149 static void pipeline(struct fetch_pipeline_middle_end
*fpme
,
150 const struct draw_vertex_info
*vert_info
,
151 const struct draw_prim_info
*prim_info
)
153 if (prim_info
->linear
)
154 draw_pipeline_run_linear( fpme
->draw
,
158 draw_pipeline_run( fpme
->draw
,
163 static void emit(struct pt_emit
*emit
,
164 const struct draw_vertex_info
*vert_info
,
165 const struct draw_prim_info
*prim_info
)
167 if (prim_info
->linear
) {
168 draw_pt_emit_linear(emit
, vert_info
, prim_info
);
171 draw_pt_emit(emit
, vert_info
, prim_info
);
176 static void draw_vertex_shader_run(struct draw_vertex_shader
*vshader
,
177 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
178 const struct draw_vertex_info
*input_verts
,
179 struct draw_vertex_info
*output_verts
)
181 output_verts
->vertex_size
= input_verts
->vertex_size
;
182 output_verts
->stride
= input_verts
->vertex_size
;
183 output_verts
->count
= input_verts
->count
;
184 output_verts
->verts
=
185 (struct vertex_header
*)MALLOC(output_verts
->vertex_size
*
186 output_verts
->count
);
188 vshader
->run_linear(vshader
,
189 (const float (*)[4])input_verts
->verts
->data
,
190 ( float (*)[4])output_verts
->verts
->data
,
193 input_verts
->vertex_size
,
194 input_verts
->vertex_size
);
197 static void fetch_pipeline_generic( struct draw_pt_middle_end
*middle
,
198 const struct draw_fetch_info
*fetch_info
,
199 const struct draw_prim_info
*prim_info
)
201 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
202 struct draw_context
*draw
= fpme
->draw
;
203 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
204 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
205 struct draw_prim_info gs_prim_info
;
206 struct draw_vertex_info fetched_vert_info
;
207 struct draw_vertex_info vs_vert_info
;
208 struct draw_vertex_info gs_vert_info
;
209 struct draw_vertex_info
*vert_info
;
211 fetched_vert_info
.count
= fetch_info
->count
;
212 fetched_vert_info
.vertex_size
= fpme
->vertex_size
;
213 fetched_vert_info
.verts
=
214 (struct vertex_header
*)MALLOC(fetched_vert_info
.vertex_size
*
216 if (!fetched_vert_info
.verts
) {
221 /* Fetch into our vertex buffer.
223 fetch( fpme
->fetch
, fetch_info
, (char *)fetched_vert_info
.verts
);
225 /* Finished with fetch:
228 vert_info
= &fetched_vert_info
;
230 /* Run the shader, note that this overwrites the data[] parts of
231 * the pipeline verts.
233 if (fpme
->opt
& PT_SHADE
) {
234 draw_vertex_shader_run(vshader
,
235 draw
->pt
.user
.vs_constants
,
239 FREE(vert_info
->verts
);
240 vert_info
= &vs_vert_info
;
243 if ((fpme
->opt
& PT_SHADE
) && gshader
) {
244 draw_geometry_shader_run(gshader
,
245 draw
->pt
.user
.gs_constants
,
252 FREE(vert_info
->verts
);
253 vert_info
= &gs_vert_info
;
254 prim_info
= &gs_prim_info
;
258 /* Stream output needs to be done before clipping.
260 * XXX: Stream output surely needs to respect the prim_info->elt
263 draw_pt_so_emit( fpme
->so_emit
,
267 if (draw_pt_post_vs_run( fpme
->post_vs
,
273 /* Do we need to run the pipeline?
275 if (opt
& PT_PIPELINE
) {
276 pipeline( fpme
->draw
,
288 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
289 const unsigned *fetch_elts
,
290 unsigned fetch_count
,
291 const ushort
*draw_elts
,
292 unsigned draw_count
)
294 struct draw_fetch_info fetch_info
;
295 struct draw_prim_info prim_info
;
297 fetch_info
.linear
= FALSE
;
298 fetch_info
.start
= 0;
299 fetch_info
.elts
= fetch_elts
;
300 fetch_info
.count
= fetch_count
;
302 prim_info
.linear
= FALSE
;
304 prim_info
.count
= draw_count
;
305 prim_info
.elts
= draw_elts
;
307 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
311 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
315 struct draw_fetch_info fetch_info
;
316 struct draw_prim_info prim_info
;
318 fetch_info
.linear
= TRUE
;
319 fetch_info
.start
= start
;
320 fetch_info
.count
= count
;
321 fetch_info
.elts
= NULL
;
323 prim_info
.linear
= TRUE
;
325 prim_info
.count
= count
;
326 prim_info
.elts
= NULL
;
328 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
333 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
336 const ushort
*draw_elts
,
337 unsigned draw_count
)
339 struct draw_fetch_info fetch_info
;
340 struct draw_prim_info prim_info
;
342 fetch_info
.linear
= TRUE
;
343 fetch_info
.start
= start
;
344 fetch_info
.count
= count
;
345 fetch_info
.elts
= NULL
;
347 prim_info
.linear
= FALSE
;
349 prim_info
.count
= draw_count
;
350 prim_info
.elts
= draw_elts
;
352 fetch_pipeline_generic( middle
, &fetch_info
, &prim_info
);
357 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
362 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
364 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
367 draw_pt_fetch_destroy( fpme
->fetch
);
370 draw_pt_emit_destroy( fpme
->emit
);
373 draw_pt_so_emit_destroy( fpme
->so_emit
);
376 draw_pt_post_vs_destroy( fpme
->post_vs
);
382 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
384 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
388 fpme
->base
.prepare
= fetch_pipeline_prepare
;
389 fpme
->base
.run
= fetch_pipeline_run
;
390 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
391 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
392 fpme
->base
.finish
= fetch_pipeline_finish
;
393 fpme
->base
.destroy
= fetch_pipeline_destroy
;
397 fpme
->fetch
= draw_pt_fetch_create( draw
);
401 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
405 fpme
->emit
= draw_pt_emit_create( draw
);
409 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
417 fetch_pipeline_destroy( &fpme
->base
);