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
;
96 emit_B8G8R8A8_UNORM( const float *attrib
, void *ptr
)
98 ubyte
*out
= (ubyte
*)ptr
;
99 out
[2] = float_to_ubyte(attrib
[0]);
100 out
[1] = float_to_ubyte(attrib
[1]);
101 out
[0] = float_to_ubyte(attrib
[2]);
102 out
[3] = float_to_ubyte(attrib
[3]);
105 static void fetch_pipeline_prepare( struct draw_pt_middle_end
*middle
,
108 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
109 struct draw_context
*draw
= fpme
->draw
;
112 const struct vertex_info
*vinfo
;
117 ok
= draw
->render
->set_primitive(draw
->render
, prim
);
123 /* Must do this after set_primitive() above:
125 vinfo
= draw
->render
->get_vertex_info(draw
->render
);
128 /* In passthrough mode, need to translate from vertex shader
129 * outputs to hw vertices.
132 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
133 unsigned emit_sz
= 0;
134 unsigned src_buffer
= 0;
135 unsigned src_offset
= (sizeof(struct vertex_header
) +
136 vinfo
->src_index
[i
] * 4 * sizeof(float) );
140 switch (vinfo
->emit
[i
]) {
142 fpme
->translate
[i
].emit
= emit_R32G32B32A32_FLOAT
;
143 emit_sz
= 4 * sizeof(float);
146 fpme
->translate
[i
].emit
= emit_R32G32B32_FLOAT
;
147 emit_sz
= 3 * sizeof(float);
150 fpme
->translate
[i
].emit
= emit_R32G32_FLOAT
;
151 emit_sz
= 2 * sizeof(float);
154 fpme
->translate
[i
].emit
= emit_R32_FLOAT
;
155 emit_sz
= 1 * sizeof(float);
158 fpme
->translate
[i
].emit
= emit_R32_FLOAT
;
159 emit_sz
= 1 * sizeof(float);
164 fpme
->translate
[i
].emit
= emit_B8G8R8A8_UNORM
;
165 emit_sz
= 4 * sizeof(ubyte
);
168 fpme
->translate
[i
].emit
= emit_NULL
;
173 fpme
->translate
[i
].input_buf
= &fpme
->input_buf
[src_buffer
];
174 fpme
->translate
[i
].input_offset
= src_offset
;
175 fpme
->translate
[i
].output_offset
= dst_offset
;
176 dst_offset
+= emit_sz
;
179 fpme
->nr_translate
= vinfo
->num_attribs
;
180 fpme
->hw_vertex_size
= vinfo
->size
* 4;
182 //fpme->pipeline_vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
183 fpme
->pipeline_vertex_size
= MAX_VERTEX_ALLOCATION
;
184 fpme
->hw_vertex_size
= vinfo
->size
* 4;
190 static void fetch_pipeline_run( struct draw_pt_middle_end
*middle
,
191 const unsigned *fetch_elts
,
192 unsigned fetch_count
,
193 const ushort
*draw_elts
,
194 unsigned draw_count
)
196 struct fetch_pipeline_middle_end
*fpme
= (struct fetch_pipeline_middle_end
*)middle
;
197 struct draw_context
*draw
= fpme
->draw
;
198 struct draw_vertex_shader
*shader
= draw
->vertex_shader
;
199 char *pipeline_verts
;
201 pipeline_verts
= MALLOC(fpme
->pipeline_vertex_size
*
204 if (!pipeline_verts
) {
212 shader
->prepare(shader
, draw
);
213 if (shader
->run(shader
, draw
, fetch_elts
, fetch_count
, pipeline_verts
,
214 fpme
->pipeline_vertex_size
)) {
215 /* Run the pipeline */
216 draw_pt_run_pipeline( fpme
->draw
,
219 fpme
->pipeline_vertex_size
,
228 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
230 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
232 hw_verts
= draw
->render
->allocate_vertices(draw
->render
,
233 (ushort
)fpme
->hw_vertex_size
,
234 (ushort
)fetch_count
);
240 out_buf
= (char *)hw_verts
;
241 fpme
->input_buf
[0] = (const ubyte
*)pipeline_verts
;
242 fpme
->input_buf
[1] = (const ubyte
*)&fpme
->draw
->rasterizer
->point_size
;
244 for (i
= 0; i
< fetch_count
; i
++) {
246 for (j
= 0; j
< fpme
->nr_translate
; j
++) {
248 const float *attrib
= (const float *)( (*fpme
->translate
[j
].input_buf
) +
249 fpme
->translate
[j
].input_offset
);
251 char *dest
= out_buf
+ fpme
->translate
[j
].output_offset
;
254 debug_printf("emiting [%f, %f, %f, %f]\n",
255 attrib
[0], attrib
[1],
256 attrib
[2], attrib
[3]);
258 fpme
->translate
[j
].emit(attrib
, dest
);
261 fpme
->input_buf
[0] += fpme
->pipeline_vertex_size
;
262 out_buf
+= fpme
->hw_vertex_size
;
265 draw
->render
->draw(draw
->render
,
269 draw
->render
->release_vertices(draw
->render
,
271 fpme
->hw_vertex_size
,
276 FREE(pipeline_verts
);
281 static void fetch_pipeline_finish( struct draw_pt_middle_end
*middle
)
286 static void fetch_pipeline_destroy( struct draw_pt_middle_end
*middle
)
292 struct draw_pt_middle_end
*draw_pt_fetch_pipeline_or_emit( struct draw_context
*draw
)
294 struct fetch_pipeline_middle_end
*fetch_pipeline
= CALLOC_STRUCT( fetch_pipeline_middle_end
);
296 fetch_pipeline
->base
.prepare
= fetch_pipeline_prepare
;
297 fetch_pipeline
->base
.run
= fetch_pipeline_run
;
298 fetch_pipeline
->base
.finish
= fetch_pipeline_finish
;
299 fetch_pipeline
->base
.destroy
= fetch_pipeline_destroy
;
301 fetch_pipeline
->draw
= draw
;
303 return &fetch_pipeline
->base
;