draw: remove another debug assert on failover to generic vs varient
[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_vbuf.h"
31 #include "draw/draw_vertex.h"
32 #include "draw/draw_pt.h"
33 #include "draw/draw_vs.h"
34 #include "translate/translate.h"
35
36
37 struct fetch_pipeline_middle_end {
38 struct draw_pt_middle_end base;
39 struct draw_context *draw;
40
41 struct pt_emit *emit;
42 struct pt_fetch *fetch;
43 struct pt_post_vs *post_vs;
44
45 unsigned vertex_data_offset;
46 unsigned vertex_size;
47 unsigned prim;
48 unsigned opt;
49 };
50
51
52 static void fetch_pipeline_prepare( struct draw_pt_middle_end *middle,
53 unsigned prim,
54 unsigned opt,
55 unsigned *max_vertices )
56 {
57 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
58 struct draw_context *draw = fpme->draw;
59 struct draw_vertex_shader *vs = draw->vs.vertex_shader;
60
61 /* Add one to num_outputs because the pipeline occasionally tags on
62 * an additional texcoord, eg for AA lines.
63 */
64 unsigned nr = MAX2( vs->info.num_inputs,
65 vs->info.num_outputs + 1 );
66
67 fpme->prim = prim;
68 fpme->opt = opt;
69
70 /* Always leave room for the vertex header whether we need it or
71 * not. It's hard to get rid of it in particular because of the
72 * viewport code in draw_pt_post_vs.c.
73 */
74 fpme->vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
75
76
77
78 draw_pt_fetch_prepare( fpme->fetch,
79 fpme->vertex_size );
80
81 /* XXX: it's not really gl rasterization rules we care about here,
82 * but gl vs dx9 clip spaces.
83 */
84 draw_pt_post_vs_prepare( fpme->post_vs,
85 (boolean)draw->bypass_clipping,
86 (boolean)draw->identity_viewport,
87 (boolean)draw->rasterizer->gl_rasterization_rules );
88
89
90 if (!(opt & PT_PIPELINE)) {
91 draw_pt_emit_prepare( fpme->emit,
92 prim,
93 max_vertices );
94
95 *max_vertices = MAX2( *max_vertices,
96 DRAW_PIPE_MAX_VERTICES );
97 }
98 else {
99 *max_vertices = DRAW_PIPE_MAX_VERTICES;
100 }
101
102 /* No need to prepare the shader.
103 */
104 vs->prepare(vs, draw);
105 }
106
107
108
109 static void fetch_pipeline_run( struct draw_pt_middle_end *middle,
110 const unsigned *fetch_elts,
111 unsigned fetch_count,
112 const ushort *draw_elts,
113 unsigned draw_count )
114 {
115 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
116 struct draw_context *draw = fpme->draw;
117 struct draw_vertex_shader *shader = draw->vs.vertex_shader;
118 unsigned opt = fpme->opt;
119 unsigned alloc_count = align_int( fetch_count, 4 );
120
121 struct vertex_header *pipeline_verts =
122 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
123
124 if (!pipeline_verts) {
125 /* Not much we can do here - just skip the rendering.
126 */
127 assert(0);
128 return;
129 }
130
131 /* Fetch into our vertex buffer
132 */
133 draw_pt_fetch_run( fpme->fetch,
134 fetch_elts,
135 fetch_count,
136 (char *)pipeline_verts );
137
138 /* Run the shader, note that this overwrites the data[] parts of
139 * the pipeline verts. If there is no shader, ie a bypass shader,
140 * then the inputs == outputs, and are already in the correct
141 * place.
142 */
143 if (opt & PT_SHADE)
144 {
145 shader->run_linear(shader,
146 (const float (*)[4])pipeline_verts->data,
147 ( float (*)[4])pipeline_verts->data,
148 (const float (*)[4])draw->pt.user.constants,
149 fetch_count,
150 fpme->vertex_size,
151 fpme->vertex_size);
152 }
153
154 if (draw_pt_post_vs_run( fpme->post_vs,
155 pipeline_verts,
156 fetch_count,
157 fpme->vertex_size ))
158 {
159 opt |= PT_PIPELINE;
160 }
161
162 /* Do we need to run the pipeline?
163 */
164 if (opt & PT_PIPELINE) {
165 draw_pipeline_run( fpme->draw,
166 fpme->prim,
167 pipeline_verts,
168 fetch_count,
169 fpme->vertex_size,
170 draw_elts,
171 draw_count );
172 }
173 else {
174 draw_pt_emit( fpme->emit,
175 (const float (*)[4])pipeline_verts->data,
176 fetch_count,
177 fpme->vertex_size,
178 draw_elts,
179 draw_count );
180 }
181
182
183 FREE(pipeline_verts);
184 }
185
186
187 static void fetch_pipeline_linear_run( struct draw_pt_middle_end *middle,
188 unsigned start,
189 unsigned count)
190 {
191 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
192 struct draw_context *draw = fpme->draw;
193 struct draw_vertex_shader *shader = draw->vs.vertex_shader;
194 unsigned opt = fpme->opt;
195 unsigned alloc_count = align_int( count, 4 );
196
197 struct vertex_header *pipeline_verts =
198 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
199
200 if (!pipeline_verts) {
201 /* Not much we can do here - just skip the rendering.
202 */
203 assert(0);
204 return;
205 }
206
207 /* Fetch into our vertex buffer
208 */
209 draw_pt_fetch_run_linear( fpme->fetch,
210 start,
211 count,
212 (char *)pipeline_verts );
213
214 /* Run the shader, note that this overwrites the data[] parts of
215 * the pipeline verts. If there is no shader, ie a bypass shader,
216 * then the inputs == outputs, and are already in the correct
217 * place.
218 */
219 if (opt & PT_SHADE)
220 {
221 shader->run_linear(shader,
222 (const float (*)[4])pipeline_verts->data,
223 ( float (*)[4])pipeline_verts->data,
224 (const float (*)[4])draw->pt.user.constants,
225 count,
226 fpme->vertex_size,
227 fpme->vertex_size);
228 }
229
230 if (draw_pt_post_vs_run( fpme->post_vs,
231 pipeline_verts,
232 count,
233 fpme->vertex_size ))
234 {
235 opt |= PT_PIPELINE;
236 }
237
238 /* Do we need to run the pipeline?
239 */
240 if (opt & PT_PIPELINE) {
241 draw_pipeline_run_linear( fpme->draw,
242 fpme->prim,
243 pipeline_verts,
244 count,
245 fpme->vertex_size);
246 }
247 else {
248 draw_pt_emit_linear( fpme->emit,
249 (const float (*)[4])pipeline_verts->data,
250 count,
251 fpme->vertex_size,
252 0, /*start*/
253 count );
254 }
255
256 FREE(pipeline_verts);
257 }
258
259
260
261 static void fetch_pipeline_linear_run_elts( struct draw_pt_middle_end *middle,
262 unsigned start,
263 unsigned count,
264 const ushort *draw_elts,
265 unsigned draw_count )
266 {
267 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
268 struct draw_context *draw = fpme->draw;
269 struct draw_vertex_shader *shader = draw->vs.vertex_shader;
270 unsigned opt = fpme->opt;
271 unsigned alloc_count = align_int( count, 4 );
272
273 struct vertex_header *pipeline_verts =
274 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
275
276 if (!pipeline_verts) {
277 /* Not much we can do here - just skip the rendering.
278 */
279 assert(0);
280 return;
281 }
282
283 /* Fetch into our vertex buffer
284 */
285 draw_pt_fetch_run_linear( fpme->fetch,
286 start,
287 count,
288 (char *)pipeline_verts );
289
290 /* Run the shader, note that this overwrites the data[] parts of
291 * the pipeline verts. If there is no shader, ie a bypass shader,
292 * then the inputs == outputs, and are already in the correct
293 * place.
294 */
295 if (opt & PT_SHADE)
296 {
297 shader->run_linear(shader,
298 (const float (*)[4])pipeline_verts->data,
299 ( float (*)[4])pipeline_verts->data,
300 (const float (*)[4])draw->pt.user.constants,
301 count,
302 fpme->vertex_size,
303 fpme->vertex_size);
304 }
305
306 if (draw_pt_post_vs_run( fpme->post_vs,
307 pipeline_verts,
308 count,
309 fpme->vertex_size ))
310 {
311 opt |= PT_PIPELINE;
312 }
313
314 /* Do we need to run the pipeline?
315 */
316 if (opt & PT_PIPELINE) {
317 draw_pipeline_run( fpme->draw,
318 fpme->prim,
319 pipeline_verts,
320 count,
321 fpme->vertex_size,
322 draw_elts,
323 draw_count );
324 }
325 else {
326 draw_pt_emit( fpme->emit,
327 (const float (*)[4])pipeline_verts->data,
328 count,
329 fpme->vertex_size,
330 draw_elts,
331 draw_count );
332 }
333
334 FREE(pipeline_verts);
335 }
336
337
338
339 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
340 {
341 /* nothing to do */
342 }
343
344 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
345 {
346 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
347
348 if (fpme->fetch)
349 draw_pt_fetch_destroy( fpme->fetch );
350
351 if (fpme->emit)
352 draw_pt_emit_destroy( fpme->emit );
353
354 if (fpme->post_vs)
355 draw_pt_post_vs_destroy( fpme->post_vs );
356
357 FREE(middle);
358 }
359
360
361 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
362 {
363 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
364 if (!fpme)
365 goto fail;
366
367 fpme->base.prepare = fetch_pipeline_prepare;
368 fpme->base.run = fetch_pipeline_run;
369 fpme->base.run_linear = fetch_pipeline_linear_run;
370 fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
371 fpme->base.finish = fetch_pipeline_finish;
372 fpme->base.destroy = fetch_pipeline_destroy;
373
374 fpme->draw = draw;
375
376 fpme->fetch = draw_pt_fetch_create( draw );
377 if (!fpme->fetch)
378 goto fail;
379
380 fpme->post_vs = draw_pt_post_vs_create( draw );
381 if (!fpme->post_vs)
382 goto fail;
383
384 fpme->emit = draw_pt_emit_create( draw );
385 if (!fpme->emit)
386 goto fail;
387
388 return &fpme->base;
389
390 fail:
391 if (fpme)
392 fetch_pipeline_destroy( &fpme->base );
393
394 return NULL;
395 }