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
;
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 /* Add one to num_outputs because the pipeline occasionally tags on
66 * an additional texcoord, eg for AA lines.
68 unsigned nr
= MAX2( vs
->info
.num_inputs
,
69 vs
->info
.num_outputs
+ 1 );
71 /* Scan for instanceID system value.
73 for (i
= 0; i
< vs
->info
.num_inputs
; i
++) {
74 if (vs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_INSTANCEID
) {
75 instance_id_index
= i
;
83 /* Always leave room for the vertex header whether we need it or
84 * not. It's hard to get rid of it in particular because of the
85 * viewport code in draw_pt_post_vs.c.
87 fpme
->vertex_size
= sizeof(struct vertex_header
) + nr
* 4 * sizeof(float);
91 draw_pt_fetch_prepare( fpme
->fetch
,
95 /* XXX: it's not really gl rasterization rules we care about here,
96 * but gl vs dx9 clip spaces.
98 draw_pt_post_vs_prepare( fpme
->post_vs
,
99 (boolean
)draw
->bypass_clipping
,
100 (boolean
)draw
->identity_viewport
,
101 (boolean
)draw
->rasterizer
->gl_rasterization_rules
,
102 (draw
->vs
.edgeflag_output
? true : false) );
104 draw_pt_so_emit_prepare( fpme
->so_emit
, prim
);
106 if (!(opt
& PT_PIPELINE
)) {
107 draw_pt_emit_prepare( fpme
->emit
,
111 *max_vertices
= MAX2( *max_vertices
,
112 DRAW_PIPE_MAX_VERTICES
);
115 *max_vertices
= DRAW_PIPE_MAX_VERTICES
;
118 /* return even number */
119 *max_vertices
= *max_vertices
& ~1;
121 /* No need to prepare the shader.
123 vs
->prepare(vs
, draw
);
128 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
129 const unsigned *fetch_elts
,
130 unsigned fetch_count
,
131 const ushort
*draw_elts
,
132 unsigned draw_count
)
134 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
135 struct draw_context
*draw
= fpme
->draw
;
136 struct draw_vertex_shader
*vshader
= draw
->vs
.vertex_shader
;
137 struct draw_geometry_shader
*gshader
= draw
->gs
.geometry_shader
;
138 unsigned opt
= fpme
->opt
;
139 unsigned alloc_count
= align( fetch_count
, 4 );
141 struct vertex_header
*pipeline_verts
=
142 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
144 if (!pipeline_verts
) {
145 /* Not much we can do here - just skip the rendering.
151 /* Fetch into our vertex buffer
153 draw_pt_fetch_run( fpme
->fetch
,
156 (char *)pipeline_verts
);
158 /* Run the shader, note that this overwrites the data[] parts of
159 * the pipeline verts.
163 vshader
->run_linear(vshader
,
164 (const float (*)[4])pipeline_verts
->data
,
165 ( float (*)[4])pipeline_verts
->data
,
166 draw
->pt
.user
.vs_constants
,
171 draw_geometry_shader_run(gshader
,
172 (const float (*)[4])pipeline_verts
->data
,
173 ( float (*)[4])pipeline_verts
->data
,
174 draw
->pt
.user
.gs_constants
,
180 /* stream output needs to be done before clipping */
181 draw_pt_so_emit( fpme
->so_emit
,
182 (const float (*)[4])pipeline_verts
->data
,
186 if (draw_pt_post_vs_run( fpme
->post_vs
,
194 /* Do we need to run the pipeline?
196 if (opt
& PT_PIPELINE
) {
197 draw_pipeline_run( fpme
->draw
,
206 draw_pt_emit( fpme
->emit
,
207 (const float (*)[4])pipeline_verts
->data
,
215 FREE(pipeline_verts
);
219 static void fetch_pipeline_linear_run( struct draw_pt_middle_end
*middle
,
223 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
224 struct draw_context
*draw
= fpme
->draw
;
225 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
226 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
227 unsigned opt
= fpme
->opt
;
228 unsigned alloc_count
= align( count
, 4 );
230 struct vertex_header
*pipeline_verts
=
231 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
233 if (!pipeline_verts
) {
234 /* Not much we can do here - just skip the rendering.
240 /* Fetch into our vertex buffer
242 draw_pt_fetch_run_linear( fpme
->fetch
,
245 (char *)pipeline_verts
);
247 /* Run the shader, note that this overwrites the data[] parts of
248 * the pipeline verts.
252 shader
->run_linear(shader
,
253 (const float (*)[4])pipeline_verts
->data
,
254 ( float (*)[4])pipeline_verts
->data
,
255 draw
->pt
.user
.vs_constants
,
261 draw_geometry_shader_run(geometry_shader
,
262 (const float (*)[4])pipeline_verts
->data
,
263 ( float (*)[4])pipeline_verts
->data
,
264 draw
->pt
.user
.gs_constants
,
270 /* stream output needs to be done before clipping */
271 draw_pt_so_emit( fpme
->so_emit
,
272 (const float (*)[4])pipeline_verts
->data
,
276 if (draw_pt_post_vs_run( fpme
->post_vs
,
284 /* Do we need to run the pipeline?
286 if (opt
& PT_PIPELINE
) {
287 draw_pipeline_run_linear( fpme
->draw
,
294 draw_pt_emit_linear( fpme
->emit
,
295 (const float (*)[4])pipeline_verts
->data
,
300 FREE(pipeline_verts
);
305 static boolean
fetch_pipeline_linear_run_elts( struct draw_pt_middle_end
*middle
,
308 const ushort
*draw_elts
,
309 unsigned draw_count
)
311 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
312 struct draw_context
*draw
= fpme
->draw
;
313 struct draw_vertex_shader
*shader
= draw
->vs
.vertex_shader
;
314 struct draw_geometry_shader
*geometry_shader
= draw
->gs
.geometry_shader
;
315 unsigned opt
= fpme
->opt
;
316 unsigned alloc_count
= align( count
, 4 );
318 struct vertex_header
*pipeline_verts
=
319 (struct vertex_header
*)MALLOC(fpme
->vertex_size
* alloc_count
);
324 /* Fetch into our vertex buffer
326 draw_pt_fetch_run_linear( fpme
->fetch
,
329 (char *)pipeline_verts
);
331 /* Run the shader, note that this overwrites the data[] parts of
332 * the pipeline verts.
336 shader
->run_linear(shader
,
337 (const float (*)[4])pipeline_verts
->data
,
338 ( float (*)[4])pipeline_verts
->data
,
339 draw
->pt
.user
.vs_constants
,
345 draw_geometry_shader_run(geometry_shader
,
346 (const float (*)[4])pipeline_verts
->data
,
347 ( float (*)[4])pipeline_verts
->data
,
348 draw
->pt
.user
.gs_constants
,
354 /* stream output needs to be done before clipping */
355 draw_pt_so_emit( fpme
->so_emit
,
356 (const float (*)[4])pipeline_verts
->data
,
360 if (draw_pt_post_vs_run( fpme
->post_vs
,
368 /* Do we need to run the pipeline?
370 if (opt
& PT_PIPELINE
) {
371 draw_pipeline_run( fpme
->draw
,
380 draw_pt_emit( fpme
->emit
,
381 (const float (*)[4])pipeline_verts
->data
,
388 FREE(pipeline_verts
);
394 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
399 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
401 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
404 draw_pt_fetch_destroy( fpme
->fetch
);
407 draw_pt_emit_destroy( fpme
->emit
);
410 draw_pt_so_emit_destroy( fpme
->so_emit
);
413 draw_pt_post_vs_destroy( fpme
->post_vs
);
419 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
421 struct fetch_pipeline_middle_end
*fpme
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
425 fpme
->base
.prepare
= fetch_pipeline_prepare
;
426 fpme
->base
.run
= fetch_pipeline_run
;
427 fpme
->base
.run_linear
= fetch_pipeline_linear_run
;
428 fpme
->base
.run_linear_elts
= fetch_pipeline_linear_run_elts
;
429 fpme
->base
.finish
= fetch_pipeline_finish
;
430 fpme
->base
.destroy
= fetch_pipeline_destroy
;
434 fpme
->fetch
= draw_pt_fetch_create( draw
);
438 fpme
->post_vs
= draw_pt_post_vs_create( draw
);
442 fpme
->emit
= draw_pt_emit_create( draw
);
446 fpme
->so_emit
= draw_pt_so_emit_create( draw
);
454 fetch_pipeline_destroy( &fpme
->base
);