Merge branch 'gallium-i915-current' 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->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
181 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
182 {
183 /* nothing to do */
184 }
185
186 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
187 {
188 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
189
190 if (fpme->fetch)
191 draw_pt_fetch_destroy( fpme->fetch );
192
193 if (fpme->emit)
194 draw_pt_emit_destroy( fpme->emit );
195
196 if (fpme->post_vs)
197 draw_pt_post_vs_destroy( fpme->post_vs );
198
199 FREE(middle);
200 }
201
202
203 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
204 {
205 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
206 if (!fpme)
207 goto fail;
208
209 fpme->base.prepare = fetch_pipeline_prepare;
210 fpme->base.run = fetch_pipeline_run;
211 fpme->base.finish = fetch_pipeline_finish;
212 fpme->base.destroy = fetch_pipeline_destroy;
213
214 fpme->draw = draw;
215
216 fpme->fetch = draw_pt_fetch_create( draw );
217 if (!fpme->fetch)
218 goto fail;
219
220 fpme->post_vs = draw_pt_post_vs_create( draw );
221 if (!fpme->post_vs)
222 goto fail;
223
224 fpme->emit = draw_pt_emit_create( draw );
225 if (!fpme->emit)
226 goto fail;
227
228 return &fpme->base;
229
230 fail:
231 if (fpme)
232 fetch_pipeline_destroy( &fpme->base );
233
234 return NULL;
235 }