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