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
;
53 static void emit_R32_FLOAT( const float *attrib
,
56 (*out
)[0] = attrib
[0];
60 static void emit_R32G32_FLOAT( const float *attrib
,
63 (*out
)[0] = attrib
[0];
64 (*out
)[1] = attrib
[1];
68 static void emit_R32G32B32_FLOAT( const float *attrib
,
71 (*out
)[0] = attrib
[0];
72 (*out
)[1] = attrib
[1];
73 (*out
)[2] = attrib
[2];
77 static void emit_R32G32B32A32_FLOAT( const float *attrib
,
80 (*out
)[0] = attrib
[0];
81 (*out
)[1] = attrib
[1];
82 (*out
)[2] = attrib
[2];
83 (*out
)[3] = attrib
[3];
87 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
90 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
91 struct draw_context
*draw
= fpme
->draw
;
94 const struct vertex_info
*vinfo
;
98 ok
= draw
->render
->set_primitive(draw
->render
, prim
);
103 /* Must do this after set_primitive() above:
105 vinfo
= draw
->render
->get_vertex_info(draw
->render
);
107 /* Need to look at vertex shader inputs (we know it is a
108 * passthrough shader, so these define the outputs too). If we
109 * were running a shader, we'd still be looking at the inputs at
112 for (i
= 0; i
< draw
->vertex_shader
->info
.num_inputs
; i
++) {
113 unsigned buf
= draw
->vertex_element
[i
].vertex_buffer_index
;
114 enum pipe_format format
= draw
->vertex_element
[i
].src_format
;
116 fpme
->fetch
[nr
].ptr
= ((const ubyte
*) draw
->user
.vbuffer
[buf
] +
117 draw
->vertex_buffer
[buf
].buffer_offset
+
118 draw
->vertex_element
[i
].src_offset
);
120 fpme
->fetch
[nr
].pitch
= draw
->vertex_buffer
[buf
].pitch
;
121 fpme
->fetch
[nr
].fetch
= draw_get_fetch_func( format
);
123 /* Always do this -- somewhat redundant...
125 fpme
->fetch
[nr
].emit
= emit_R32G32B32A32_FLOAT
;
130 //fpme->pipeline_vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
131 fpme
->pipeline_vertex_size
= MAX_VERTEX_ALLOCATION
;
132 fpme
->hw_vertex_size
= vinfo
->size
* 4;
138 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
139 const unsigned *fetch_elts
,
140 unsigned fetch_count
,
141 const ushort
*draw_elts
,
142 unsigned draw_count
)
144 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
145 struct draw_context
*draw
= fpme
->draw
;
146 struct draw_vertex_shader
*shader
= draw
->vertex_shader
;
147 char *pipeline_verts
;
149 pipeline_verts
= MALLOC(fpme
->pipeline_vertex_size
*
152 if (!pipeline_verts
) {
160 shader
->prepare(shader
, draw
);
161 if (shader
->run(shader
, draw
, fetch_elts
, fetch_count
, pipeline_verts
,
162 fpme
->pipeline_vertex_size
)) {
163 /* Run the pipeline */
164 draw_pt_run_pipeline( fpme
->draw
,
167 fpme
->pipeline_vertex_size
,
176 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
178 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
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 FREE(pipeline_verts
);
220 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
225 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
231 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
233 struct fetch_pipeline_middle_end
*fetch_pipeline
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
235 fetch_pipeline
->base
.prepare
= fetch_pipeline_prepare
;
236 fetch_pipeline
->base
.run
= fetch_pipeline_run
;
237 fetch_pipeline
->base
.finish
= fetch_pipeline_finish
;
238 fetch_pipeline
->base
.destroy
= fetch_pipeline_destroy
;
240 fetch_pipeline
->draw
= draw
;
242 return &fetch_pipeline
->base
;