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
;
39 const ubyte
*input_buf
[2];
42 const ubyte
**input_buf
;
43 unsigned input_offset
;
44 unsigned output_offset
;
46 void (*emit
)( const float *attrib
, void *ptr
);
47 } translate
[PIPE_MAX_ATTRIBS
];
48 unsigned nr_translate
;
50 unsigned pipeline_vertex_size
;
51 unsigned hw_vertex_size
;
56 static void emit_NULL( const float *attrib
,
61 static void emit_R32_FLOAT( const float *attrib
,
64 float *out
= (float *)ptr
;
68 static void emit_R32G32_FLOAT( const float *attrib
,
71 float *out
= (float *)ptr
;
76 static void emit_R32G32B32_FLOAT( const float *attrib
,
79 float *out
= (float *)ptr
;
85 static void emit_R32G32B32A32_FLOAT( const float *attrib
,
88 float *out
= (float *)ptr
;
95 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
98 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
99 struct draw_context
*draw
= fpme
->draw
;
102 const struct vertex_info
*vinfo
;
107 ok
= draw
->render
->set_primitive(draw
->render
, prim
);
113 /* Must do this after set_primitive() above:
115 vinfo
= draw
->render
->get_vertex_info(draw
->render
);
118 /* In passthrough mode, need to translate from vertex shader
119 * outputs to hw vertices.
122 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
123 unsigned emit_sz
= 0;
124 unsigned src_buffer
= 0;
125 unsigned src_offset
= (sizeof(struct vertex_header
) +
126 vinfo
->src_index
[i
] * 4 * sizeof(float) );
130 switch (vinfo
->emit
[i
]) {
132 fpme
->translate
[i
].emit
= emit_R32G32B32A32_FLOAT
;
133 emit_sz
= 4 * sizeof(float);
136 fpme
->translate
[i
].emit
= emit_R32G32B32_FLOAT
;
137 emit_sz
= 3 * sizeof(float);
140 fpme
->translate
[i
].emit
= emit_R32G32_FLOAT
;
141 emit_sz
= 2 * sizeof(float);
144 fpme
->translate
[i
].emit
= emit_R32_FLOAT
;
145 emit_sz
= 1 * sizeof(float);
148 fpme
->translate
[i
].emit
= emit_R32_FLOAT
;
149 emit_sz
= 1 * sizeof(float);
155 fpme
->translate
[i
].emit
= emit_NULL
;
160 fpme
->translate
[i
].input_buf
= &fpme
->input_buf
[src_buffer
];
161 fpme
->translate
[i
].input_offset
= src_offset
;
162 fpme
->translate
[i
].output_offset
= dst_offset
;
163 dst_offset
+= emit_sz
;
166 fpme
->nr_translate
= vinfo
->num_attribs
;
167 fpme
->hw_vertex_size
= vinfo
->size
* 4;
169 //fpme->pipeline_vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
170 fpme
->pipeline_vertex_size
= MAX_VERTEX_ALLOCATION
;
171 fpme
->hw_vertex_size
= vinfo
->size
* 4;
177 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
178 const unsigned *fetch_elts
,
179 unsigned fetch_count
,
180 const ushort
*draw_elts
,
181 unsigned draw_count
)
183 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
184 struct draw_context
*draw
= fpme
->draw
;
185 struct draw_vertex_shader
*shader
= draw
->vertex_shader
;
186 char *pipeline_verts
;
188 pipeline_verts
= MALLOC(fpme
->pipeline_vertex_size
*
191 if (!pipeline_verts
) {
199 shader
->prepare(shader
, draw
);
200 if (shader
->run(shader
, draw
, fetch_elts
, fetch_count
, pipeline_verts
,
201 fpme
->pipeline_vertex_size
)) {
202 /* Run the pipeline */
203 draw_pt_run_pipeline( fpme
->draw
,
206 fpme
->pipeline_vertex_size
,
215 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
217 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
219 hw_verts
= draw
->render
->allocate_vertices(draw
->render
,
220 (ushort
)fpme
->hw_vertex_size
,
221 (ushort
)fetch_count
);
227 out_buf
= (char *)hw_verts
;
228 fpme
->input_buf
[0] = (const ubyte
*)pipeline_verts
;
229 fpme
->input_buf
[1] = (const ubyte
*)&fpme
->draw
->rasterizer
->point_size
;
231 for (i
= 0; i
< fetch_count
; i
++) {
233 for (j
= 0; j
< fpme
->nr_translate
; j
++) {
235 const float *attrib
= (const float *)( (*fpme
->translate
[i
].input_buf
) +
236 fpme
->translate
[i
].input_offset
);
238 char *dest
= out_buf
+ fpme
->translate
[i
].output_offset
;
240 /*debug_printf("emiting [%f, %f, %f, %f]\n",
241 attrib[0], attrib[1],
242 attrib[2], attrib[3]);*/
244 fpme
->translate
[j
].emit(attrib
, dest
);
247 fpme
->input_buf
[0] += fpme
->pipeline_vertex_size
;
250 draw
->render
->draw(draw
->render
,
254 draw
->render
->release_vertices(draw
->render
,
256 fpme
->hw_vertex_size
,
261 FREE(pipeline_verts
);
266 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
271 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
277 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
279 struct fetch_pipeline_middle_end
*fetch_pipeline
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
281 fetch_pipeline
->base
.prepare
= fetch_pipeline_prepare
;
282 fetch_pipeline
->base
.run
= fetch_pipeline_run
;
283 fetch_pipeline
->base
.finish
= fetch_pipeline_finish
;
284 fetch_pipeline
->base
.destroy
= fetch_pipeline_destroy
;
286 fetch_pipeline
->draw
= draw
;
288 return &fetch_pipeline
->base
;