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