draw: move vertex header init out of fetch_shade_pipeline.c
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_fetch_shade_pipeline.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
34
35 struct fetch_pipeline_middle_end {
36 struct draw_pt_middle_end base;
37 struct draw_context *draw;
38
39 struct {
40 const ubyte *ptr;
41 unsigned pitch;
42 void (*fetch)( const void *from, float *attrib);
43 void (*emit)( const float *attrib, float **out );
44 } fetch[PIPE_MAX_ATTRIBS];
45
46 unsigned nr_fetch;
47 unsigned pipeline_vertex_size;
48 unsigned hw_vertex_size;
49 unsigned prim;
50 };
51
52 static void emit_R32_FLOAT( const float *attrib,
53 float **out )
54 {
55 (*out)[0] = attrib[0];
56 (*out) += 1;
57 }
58
59 static void emit_R32G32_FLOAT( const float *attrib,
60 float **out )
61 {
62 (*out)[0] = attrib[0];
63 (*out)[1] = attrib[1];
64 (*out) += 2;
65 }
66
67 static void emit_R32G32B32_FLOAT( const float *attrib,
68 float **out )
69 {
70 (*out)[0] = attrib[0];
71 (*out)[1] = attrib[1];
72 (*out)[2] = attrib[2];
73 (*out) += 3;
74 }
75
76 static void emit_R32G32B32A32_FLOAT( const float *attrib,
77 float **out )
78 {
79 (*out)[0] = attrib[0];
80 (*out)[1] = attrib[1];
81 (*out)[2] = attrib[2];
82 (*out)[3] = attrib[3];
83 (*out) += 4;
84 }
85
86 static void fetch_pipeline_prepare( struct draw_pt_middle_end *middle,
87 unsigned prim )
88 {
89 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
90 struct draw_context *draw = fpme->draw;
91 unsigned i, nr = 0;
92 boolean ok;
93 const struct vertex_info *vinfo;
94
95 fpme->prim = prim;
96
97 ok = draw->render->set_primitive(draw->render, prim);
98 if (!ok) {
99 assert(0);
100 return;
101 }
102 /* Must do this after set_primitive() above:
103 */
104 vinfo = draw->render->get_vertex_info(draw->render);
105
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
109 * this point.
110 */
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;
114
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);
118
119 fpme->fetch[nr].pitch = draw->vertex_buffer[buf].pitch;
120 fpme->fetch[nr].fetch = draw_get_fetch_func( format );
121
122 /* Always do this -- somewhat redundant...
123 */
124 fpme->fetch[nr].emit = emit_R32G32B32A32_FLOAT;
125 nr++;
126 }
127
128 fpme->nr_fetch = nr;
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;
132 }
133
134
135
136
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 )
142 {
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;
147 unsigned i;
148
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;
152 } else {
153 pipeline_verts = MALLOC(fpme->pipeline_vertex_size *
154 fetch_count);
155 }
156
157 if (!pipeline_verts) {
158 assert(0);
159 return;
160 }
161
162
163 /* Shade
164 */
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,
169 fpme->prim,
170 pipeline_verts,
171 fpme->pipeline_vertex_size,
172 fetch_count,
173 draw_elts,
174 draw_count );
175 } else {
176 unsigned i, j;
177 void *hw_verts;
178 float *out;
179
180 hw_verts = draw->render->allocate_vertices(draw->render,
181 (ushort)fpme->hw_vertex_size,
182 (ushort)fetch_count);
183 if (!hw_verts) {
184 assert(0);
185 return;
186 }
187
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));
192
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);
199 }
200 }
201 /* XXX: Draw arrays path to avoid re-emitting index list again and
202 * again.
203 */
204 draw->render->draw(draw->render,
205 draw_elts,
206 draw_count);
207
208 draw->render->release_vertices(draw->render,
209 hw_verts,
210 fpme->hw_vertex_size,
211 fetch_count);
212 }
213
214
215 if (pipeline_verts != draw->vs.vertex_cache)
216 FREE(pipeline_verts);
217 }
218
219
220
221 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
222 {
223 /* nothing to do */
224 }
225
226 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
227 {
228 FREE(middle);
229 }
230
231
232 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
233 {
234 struct fetch_pipeline_middle_end *fetch_pipeline = CALLOC_STRUCT( fetch_pipeline_middle_end );
235
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;
240
241 fetch_pipeline->draw = draw;
242
243 return &fetch_pipeline->base;
244 }