b1e08a8f4023e94242e5736b7bf8e49ba6940ac5
[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->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 draw->bypass_clipping,
85 draw->identity_viewport,
86 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
102 static void fetch_pipeline_run( struct draw_pt_middle_end *middle,
103 const unsigned *fetch_elts,
104 unsigned fetch_count,
105 const ushort *draw_elts,
106 unsigned draw_count )
107 {
108 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
109 struct draw_context *draw = fpme->draw;
110 struct draw_vertex_shader *shader = draw->vertex_shader;
111 unsigned opt = fpme->opt;
112 unsigned alloc_count = align_int( fetch_count, 4 );
113
114 struct vertex_header *pipeline_verts =
115 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
116
117 if (!pipeline_verts) {
118 /* Not much we can do here - just skip the rendering.
119 */
120 assert(0);
121 return;
122 }
123
124 /* Fetch into our vertex buffer
125 */
126 draw_pt_fetch_run( fpme->fetch,
127 fetch_elts,
128 fetch_count,
129 (char *)pipeline_verts );
130
131 /* Run the shader, note that this overwrites the data[] parts of
132 * the pipeline verts. If there is no shader, ie a bypass shader,
133 * then the inputs == outputs, and are already in the correct
134 * place.
135 */
136 if (opt & PT_SHADE)
137 {
138 shader->run_linear(shader,
139 (const float (*)[4])pipeline_verts->data,
140 ( float (*)[4])pipeline_verts->data,
141 (const float (*)[4])draw->pt.user.constants,
142 fetch_count,
143 fpme->vertex_size,
144 fpme->vertex_size);
145 }
146
147 if (draw_pt_post_vs_run( fpme->post_vs,
148 pipeline_verts,
149 fetch_count,
150 fpme->vertex_size ))
151 {
152 opt |= PT_PIPELINE;
153 }
154
155 /* Do we need to run the pipeline?
156 */
157 if (opt & PT_PIPELINE) {
158 draw_pipeline_run( fpme->draw,
159 fpme->prim,
160 pipeline_verts,
161 fetch_count,
162 fpme->vertex_size,
163 draw_elts,
164 draw_count );
165 }
166 else {
167 draw_pt_emit( fpme->emit,
168 (const float (*)[4])pipeline_verts->data,
169 fetch_count,
170 fpme->vertex_size,
171 draw_elts,
172 draw_count );
173 }
174
175
176 FREE(pipeline_verts);
177 }
178
179
180 static void fetch_pipeline_linear_run( struct draw_pt_middle_end *middle,
181 unsigned fetch_start,
182 unsigned fetch_count,
183 const ushort *draw_elts,
184 unsigned draw_count )
185 {
186 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
187 struct draw_context *draw = fpme->draw;
188 struct draw_vertex_shader *shader = draw->vertex_shader;
189 unsigned opt = fpme->opt;
190 unsigned alloc_count = align_int( fetch_count, 4 );
191
192 struct vertex_header *pipeline_verts =
193 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
194
195 if (!pipeline_verts) {
196 /* Not much we can do here - just skip the rendering.
197 */
198 assert(0);
199 return;
200 }
201
202 /* Fetch into our vertex buffer
203 */
204 draw_pt_fetch_run_linear( fpme->fetch,
205 fetch_start,
206 fetch_count,
207 (char *)pipeline_verts );
208
209 /* Run the shader, note that this overwrites the data[] parts of
210 * the pipeline verts. If there is no shader, ie a bypass shader,
211 * then the inputs == outputs, and are already in the correct
212 * place.
213 */
214 if (opt & PT_SHADE)
215 {
216 shader->run_linear(shader,
217 (const float (*)[4])pipeline_verts->data,
218 ( float (*)[4])pipeline_verts->data,
219 (const float (*)[4])draw->pt.user.constants,
220 fetch_count,
221 fpme->vertex_size,
222 fpme->vertex_size);
223 }
224
225 if (draw_pt_post_vs_run( fpme->post_vs,
226 pipeline_verts,
227 fetch_count,
228 fpme->vertex_size ))
229 {
230 opt |= PT_PIPELINE;
231 }
232
233 /* Do we need to run the pipeline?
234 */
235 if (opt & PT_PIPELINE) {
236 draw_pipeline_run( fpme->draw,
237 fpme->prim,
238 pipeline_verts,
239 fetch_count,
240 fpme->vertex_size,
241 draw_elts,
242 draw_count );
243 }
244 else {
245 draw_pt_emit_linear( fpme->emit,
246 (const float (*)[4])pipeline_verts->data,
247 fetch_count,
248 fpme->vertex_size,
249 0, /*start*/
250 draw_count );
251 }
252
253 FREE(pipeline_verts);
254 }
255
256
257
258 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
259 {
260 /* nothing to do */
261 }
262
263 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
264 {
265 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
266
267 if (fpme->fetch)
268 draw_pt_fetch_destroy( fpme->fetch );
269
270 if (fpme->emit)
271 draw_pt_emit_destroy( fpme->emit );
272
273 if (fpme->post_vs)
274 draw_pt_post_vs_destroy( fpme->post_vs );
275
276 FREE(middle);
277 }
278
279
280 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
281 {
282 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
283 if (!fpme)
284 goto fail;
285
286 fpme->base.prepare = fetch_pipeline_prepare;
287 fpme->base.run = fetch_pipeline_run;
288 fpme->base.run_linear = fetch_pipeline_linear_run;
289 fpme->base.finish = fetch_pipeline_finish;
290 fpme->base.destroy = fetch_pipeline_destroy;
291
292 fpme->draw = draw;
293
294 fpme->fetch = draw_pt_fetch_create( draw );
295 if (!fpme->fetch)
296 goto fail;
297
298 fpme->post_vs = draw_pt_post_vs_create( draw );
299 if (!fpme->post_vs)
300 goto fail;
301
302 fpme->emit = draw_pt_emit_create( draw );
303 if (!fpme->emit)
304 goto fail;
305
306 return &fpme->base;
307
308 fail:
309 if (fpme)
310 fetch_pipeline_destroy( &fpme->base );
311
312 return NULL;
313 }