gallium: Push down the gl_vertex_array inputs into gallium.
[mesa.git] / src / mesa / state_tracker / st_cb_feedback.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 /**
29 * GL_SELECT and GL_FEEDBACK render modes.
30 * Basically, we use a private instance of the 'draw' module for doing
31 * selection/feedback. It would be nice to use the transform_feedback
32 * hardware feature, but it's defined as happening pre-clip and we want
33 * post-clipped primitives. Also, there's concerns about the efficiency
34 * of using the hardware for this anyway.
35 *
36 * Authors:
37 * Brian Paul
38 */
39
40 #include "main/imports.h"
41 #include "main/context.h"
42 #include "main/feedback.h"
43 #include "main/varray.h"
44
45 #include "vbo/vbo.h"
46
47 #include "st_context.h"
48 #include "st_draw.h"
49 #include "st_cb_feedback.h"
50 #include "st_program.h"
51
52 #include "pipe/p_context.h"
53 #include "pipe/p_defines.h"
54
55 #include "draw/draw_context.h"
56 #include "draw/draw_pipe.h"
57
58
59 /**
60 * This is actually used for both feedback and selection.
61 */
62 struct feedback_stage
63 {
64 struct draw_stage stage; /**< Base class */
65 struct gl_context *ctx; /**< Rendering context */
66 GLboolean reset_stipple_counter;
67 };
68
69
70 /**********************************************************************
71 * GL Feedback functions
72 **********************************************************************/
73
74 static inline struct feedback_stage *
75 feedback_stage( struct draw_stage *stage )
76 {
77 return (struct feedback_stage *)stage;
78 }
79
80
81 static void
82 feedback_vertex(struct gl_context *ctx, const struct draw_context *draw,
83 const struct vertex_header *v)
84 {
85 const struct st_context *st = st_context(ctx);
86 GLfloat win[4];
87 const GLfloat *color, *texcoord;
88 GLuint slot;
89
90 win[0] = v->data[0][0];
91 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
92 win[1] = ctx->DrawBuffer->Height - v->data[0][1];
93 else
94 win[1] = v->data[0][1];
95 win[2] = v->data[0][2];
96 win[3] = 1.0F / v->data[0][3];
97
98 /* XXX
99 * When we compute vertex layout, save info about position of the
100 * color and texcoord attribs to use here.
101 */
102
103 slot = st->vp->result_to_output[VARYING_SLOT_COL0];
104 if (slot != ~0U)
105 color = v->data[slot];
106 else
107 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
108
109 slot = st->vp->result_to_output[VARYING_SLOT_TEX0];
110 if (slot != ~0U)
111 texcoord = v->data[slot];
112 else
113 texcoord = ctx->Current.Attrib[VERT_ATTRIB_TEX0];
114
115 _mesa_feedback_vertex(ctx, win, color, texcoord);
116 }
117
118
119 static void
120 feedback_tri( struct draw_stage *stage, struct prim_header *prim )
121 {
122 struct feedback_stage *fs = feedback_stage(stage);
123 struct draw_context *draw = stage->draw;
124 _mesa_feedback_token(fs->ctx, (GLfloat) GL_POLYGON_TOKEN);
125 _mesa_feedback_token(fs->ctx, (GLfloat) 3); /* three vertices */
126 feedback_vertex(fs->ctx, draw, prim->v[0]);
127 feedback_vertex(fs->ctx, draw, prim->v[1]);
128 feedback_vertex(fs->ctx, draw, prim->v[2]);
129 }
130
131
132 static void
133 feedback_line( struct draw_stage *stage, struct prim_header *prim )
134 {
135 struct feedback_stage *fs = feedback_stage(stage);
136 struct draw_context *draw = stage->draw;
137 if (fs->reset_stipple_counter) {
138 _mesa_feedback_token(fs->ctx, (GLfloat) GL_LINE_RESET_TOKEN);
139 fs->reset_stipple_counter = GL_FALSE;
140 }
141 else {
142 _mesa_feedback_token(fs->ctx, (GLfloat) GL_LINE_TOKEN);
143 }
144 feedback_vertex(fs->ctx, draw, prim->v[0]);
145 feedback_vertex(fs->ctx, draw, prim->v[1]);
146 }
147
148
149 static void
150 feedback_point( struct draw_stage *stage, struct prim_header *prim )
151 {
152 struct feedback_stage *fs = feedback_stage(stage);
153 struct draw_context *draw = stage->draw;
154 _mesa_feedback_token(fs->ctx, (GLfloat) GL_POINT_TOKEN);
155 feedback_vertex(fs->ctx, draw, prim->v[0]);
156 }
157
158
159 static void
160 feedback_flush( struct draw_stage *stage, unsigned flags )
161 {
162 /* no-op */
163 }
164
165
166 static void
167 feedback_reset_stipple_counter( struct draw_stage *stage )
168 {
169 struct feedback_stage *fs = feedback_stage(stage);
170 fs->reset_stipple_counter = GL_TRUE;
171 }
172
173
174 static void
175 feedback_destroy( struct draw_stage *stage )
176 {
177 /* no-op */
178 }
179
180 /**
181 * Create GL feedback drawing stage.
182 */
183 static struct draw_stage *
184 draw_glfeedback_stage(struct gl_context *ctx, struct draw_context *draw)
185 {
186 struct feedback_stage *fs = ST_CALLOC_STRUCT(feedback_stage);
187
188 fs->stage.draw = draw;
189 fs->stage.next = NULL;
190 fs->stage.point = feedback_point;
191 fs->stage.line = feedback_line;
192 fs->stage.tri = feedback_tri;
193 fs->stage.flush = feedback_flush;
194 fs->stage.reset_stipple_counter = feedback_reset_stipple_counter;
195 fs->stage.destroy = feedback_destroy;
196 fs->ctx = ctx;
197
198 return &fs->stage;
199 }
200
201
202
203 /**********************************************************************
204 * GL Selection functions
205 **********************************************************************/
206
207 static void
208 select_tri( struct draw_stage *stage, struct prim_header *prim )
209 {
210 struct feedback_stage *fs = feedback_stage(stage);
211 _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
212 _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
213 _mesa_update_hitflag( fs->ctx, prim->v[2]->data[0][2] );
214 }
215
216 static void
217 select_line( struct draw_stage *stage, struct prim_header *prim )
218 {
219 struct feedback_stage *fs = feedback_stage(stage);
220 _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
221 _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
222 }
223
224
225 static void
226 select_point( struct draw_stage *stage, struct prim_header *prim )
227 {
228 struct feedback_stage *fs = feedback_stage(stage);
229 _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
230 }
231
232
233 static void
234 select_flush( struct draw_stage *stage, unsigned flags )
235 {
236 /* no-op */
237 }
238
239
240 static void
241 select_reset_stipple_counter( struct draw_stage *stage )
242 {
243 /* no-op */
244 }
245
246 static void
247 select_destroy( struct draw_stage *stage )
248 {
249 /* no-op */
250 }
251
252
253 /**
254 * Create GL selection mode drawing stage.
255 */
256 static struct draw_stage *
257 draw_glselect_stage(struct gl_context *ctx, struct draw_context *draw)
258 {
259 struct feedback_stage *fs = ST_CALLOC_STRUCT(feedback_stage);
260
261 fs->stage.draw = draw;
262 fs->stage.next = NULL;
263 fs->stage.point = select_point;
264 fs->stage.line = select_line;
265 fs->stage.tri = select_tri;
266 fs->stage.flush = select_flush;
267 fs->stage.reset_stipple_counter = select_reset_stipple_counter;
268 fs->stage.destroy = select_destroy;
269 fs->ctx = ctx;
270
271 return &fs->stage;
272 }
273
274
275 static void
276 feedback_draw_vbo(struct gl_context *ctx,
277 const struct _mesa_prim *prims,
278 GLuint nr_prims,
279 const struct _mesa_index_buffer *ib,
280 GLboolean index_bounds_valid,
281 GLuint min_index,
282 GLuint max_index,
283 struct gl_transform_feedback_object *tfb_vertcount,
284 unsigned stream,
285 struct gl_buffer_object *indirect)
286 {
287 struct st_context *st = st_context(ctx);
288
289 /* The initial pushdown of the inputs array into the drivers */
290 _mesa_set_drawing_arrays(ctx, st->draw_arrays.inputs);
291 _vbo_update_inputs(ctx, &st->draw_arrays);
292
293 /* The above needs to happen outside of st_feedback_draw_vbo,
294 * since st_RasterPossets _DrawArrays and does not want that to be
295 * overwritten by _mesa_set_drawing_arrays.
296 */
297 st_feedback_draw_vbo(ctx, prims, nr_prims, ib, index_bounds_valid,
298 min_index, max_index, tfb_vertcount,
299 stream, indirect);
300 }
301
302
303 static void
304 st_RenderMode(struct gl_context *ctx, GLenum newMode )
305 {
306 struct st_context *st = st_context(ctx);
307 struct draw_context *draw = st_get_draw_context(st);
308
309 if (!st->draw)
310 return;
311
312 if (newMode == GL_RENDER) {
313 /* restore normal VBO draw function */
314 st_init_draw_functions(&ctx->Driver);
315 }
316 else if (newMode == GL_SELECT) {
317 if (!st->selection_stage)
318 st->selection_stage = draw_glselect_stage(ctx, draw);
319 draw_set_rasterize_stage(draw, st->selection_stage);
320 /* Plug in new vbo draw function */
321 ctx->Driver.Draw = feedback_draw_vbo;
322 }
323 else {
324 struct gl_program *vp = st->ctx->VertexProgram._Current;
325
326 if (!st->feedback_stage)
327 st->feedback_stage = draw_glfeedback_stage(ctx, draw);
328 draw_set_rasterize_stage(draw, st->feedback_stage);
329 /* Plug in new vbo draw function */
330 ctx->Driver.Draw = feedback_draw_vbo;
331 /* need to generate/use a vertex program that emits pos/color/tex */
332 if (vp)
333 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, st_vertex_program(vp));
334 }
335 }
336
337
338
339 void st_init_feedback_functions(struct dd_function_table *functions)
340 {
341 functions->RenderMode = st_RenderMode;
342 }