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 "pipe/p_util.h"
29 #include "draw/draw_context.h"
30 #include "draw/draw_private.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
35 struct fetch_pipeline_middle_end
{
36 struct draw_pt_middle_end base
;
37 struct draw_context
*draw
;
42 void (*fetch
)( const void *from
, float *attrib
);
43 void (*emit
)( const float *attrib
, float **out
);
44 } fetch
[PIPE_MAX_ATTRIBS
];
47 unsigned pipeline_vertex_size
;
48 unsigned hw_vertex_size
;
52 static void emit_R32_FLOAT( const float *attrib
,
55 (*out
)[0] = attrib
[0];
59 static void emit_R32G32_FLOAT( const float *attrib
,
62 (*out
)[0] = attrib
[0];
63 (*out
)[1] = attrib
[1];
67 static void emit_R32G32B32_FLOAT( const float *attrib
,
70 (*out
)[0] = attrib
[0];
71 (*out
)[1] = attrib
[1];
72 (*out
)[2] = attrib
[2];
76 static void emit_R32G32B32A32_FLOAT( const float *attrib
,
79 (*out
)[0] = attrib
[0];
80 (*out
)[1] = attrib
[1];
81 (*out
)[2] = attrib
[2];
82 (*out
)[3] = attrib
[3];
86 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
89 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
90 struct draw_context
*draw
= fpme
->draw
;
93 const struct vertex_info
*vinfo
;
97 ok
= draw
->render
->set_primitive(draw
->render
, prim
);
102 /* Must do this after set_primitive() above:
104 vinfo
= draw
->render
->get_vertex_info(draw
->render
);
106 /* Need to look at vertex shader inputs (we know it is a
107 * passthrough shader, so these define the outputs too). If we
108 * were running a shader, we'd still be looking at the inputs at
111 for (i
= 0; i
< draw
->vertex_shader
->info
.num_inputs
; i
++) {
112 unsigned buf
= draw
->vertex_element
[i
].vertex_buffer_index
;
113 enum pipe_format format
= draw
->vertex_element
[i
].src_format
;
115 fpme
->fetch
[nr
].ptr
= ((const ubyte
*) draw
->user
.vbuffer
[buf
] +
116 draw
->vertex_buffer
[buf
].buffer_offset
+
117 draw
->vertex_element
[i
].src_offset
);
119 fpme
->fetch
[nr
].pitch
= draw
->vertex_buffer
[buf
].pitch
;
120 fpme
->fetch
[nr
].fetch
= draw_get_fetch_func( format
);
122 /* Always do this -- somewhat redundant...
124 fpme
->fetch
[nr
].emit
= emit_R32G32B32A32_FLOAT
;
129 //fpme->pipeline_vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
130 fpme
->pipeline_vertex_size
= (MAX_VERTEX_SIZE
+ 0x0f) & ~0x0f;
131 fpme
->hw_vertex_size
= vinfo
->size
* 4;
137 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
138 const unsigned *fetch_elts
,
139 unsigned fetch_count
,
140 const ushort
*draw_elts
,
141 unsigned draw_count
)
143 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
144 struct draw_context
*draw
= fpme
->draw
;
145 struct draw_vertex_shader
*shader
= draw
->vertex_shader
;
146 char *pipeline_verts
;
149 //debug_printf("fc = %d, VS = %d\n", fetch_count, VS_QUEUE_LENGTH);
150 if (fetch_count
< VS_QUEUE_LENGTH
) {
151 pipeline_verts
= draw
->vs
.vertex_cache
;
153 pipeline_verts
= MALLOC(fpme
->pipeline_vertex_size
*
157 if (!pipeline_verts
) {
165 shader
->prepare(shader
, draw
);
166 if (shader
->run(shader
, draw
, fetch_elts
, fetch_count
, pipeline_verts
)) {
167 /* Run the pipeline */
168 draw_pt_run_pipeline( fpme
->draw
,
171 fpme
->pipeline_vertex_size
,
180 hw_verts
= draw
->render
->allocate_vertices(draw
->render
,
181 (ushort
)fpme
->hw_vertex_size
,
182 (ushort
)fetch_count
);
188 out
= (float *)hw_verts
;
189 for (i
= 0; i
< fetch_count
; i
++) {
190 struct vertex_header
*header
=
191 (struct vertex_header
*)(pipeline_verts
+ (fpme
->pipeline_vertex_size
* i
));
193 for (j
= 0; j
< fpme
->nr_fetch
; j
++) {
194 float *attrib
= header
->data
[j
];
195 /*debug_printf("emiting [%f, %f, %f, %f]\n",
196 attrib[0], attrib[1],
197 attrib[2], attrib[3]);*/
198 fpme
->fetch
[j
].emit(attrib
, &out
);
201 /* XXX: Draw arrays path to avoid re-emitting index list again and
204 draw
->render
->draw(draw
->render
,
208 draw
->render
->release_vertices(draw
->render
,
210 fpme
->hw_vertex_size
,
215 if (pipeline_verts
!= draw
->vs
.vertex_cache
)
216 FREE(pipeline_verts
);
221 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
226 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
232 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
234 struct fetch_pipeline_middle_end
*fetch_pipeline
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
236 fetch_pipeline
->base
.prepare
= fetch_pipeline_prepare
;
237 fetch_pipeline
->base
.run
= fetch_pipeline_run
;
238 fetch_pipeline
->base
.finish
= fetch_pipeline_finish
;
239 fetch_pipeline
->base
.destroy
= fetch_pipeline_destroy
;
241 fetch_pipeline
->draw
= draw
;
243 return &fetch_pipeline
->base
;