Undo prev changes.
[mesa.git] / src / mesa / state_tracker / st_cb_feedback.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 /**
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/macros.h"
44
45 #include "vbo/vbo.h"
46 #include "vbo/vbo_context.h"
47
48 #include "st_context.h"
49 #include "st_atom.h"
50 #include "st_draw.h"
51 #include "st_cb_feedback.h"
52 #include "st_cb_bufferobjects.h"
53
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "pipe/p_winsys.h"
57 #include "pipe/cso_cache/cso_cache.h"
58 #include "vf/vf.h"
59
60 #include "pipe/draw/draw_context.h"
61 #include "pipe/draw/draw_private.h"
62
63
64 /**
65 * This is actually used for both feedback and selection.
66 */
67 struct feedback_stage
68 {
69 struct draw_stage stage; /**< Base class */
70 GLcontext *ctx; /**< Rendering context */
71 GLboolean reset_stipple_counter;
72 };
73
74
75 /**********************************************************************
76 * GL Feedback functions
77 **********************************************************************/
78
79 static INLINE struct feedback_stage *
80 feedback_stage( struct draw_stage *stage )
81 {
82 return (struct feedback_stage *)stage;
83 }
84
85
86 static void
87 feedback_vertex(GLcontext *ctx, const struct draw_context *draw,
88 const struct vertex_header *v)
89 {
90 const struct st_context *st = ctx->st;
91 GLfloat win[4];
92 const GLfloat *color, *texcoord;
93 const GLfloat ci = 0;
94 GLuint slot;
95
96 /* Recall that Y=0=Top of window for Gallium wincoords */
97 win[0] = v->data[0][0];
98 win[1] = ctx->DrawBuffer->Height - v->data[0][1];
99 win[2] = v->data[0][2];
100 win[3] = 1.0F / v->data[0][3];
101
102 /* XXX
103 * When we compute vertex layout, save info about position of the
104 * color and texcoord attribs to use here.
105 */
106
107 slot = st->vertex_result_to_slot[VERT_RESULT_COL0];
108 if (slot != ~0)
109 color = v->data[slot];
110 else
111 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
112
113 slot = st->vertex_result_to_slot[VERT_RESULT_TEX0];
114 if (slot != ~0)
115 texcoord = v->data[slot];
116 else
117 texcoord = ctx->Current.Attrib[VERT_ATTRIB_TEX0];
118
119 _mesa_feedback_vertex(ctx, win, color, ci, texcoord);
120 }
121
122
123 static void
124 feedback_tri( struct draw_stage *stage, struct prim_header *prim )
125 {
126 struct feedback_stage *fs = feedback_stage(stage);
127 struct draw_context *draw = stage->draw;
128 FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_POLYGON_TOKEN);
129 FEEDBACK_TOKEN(fs->ctx, (GLfloat) 3); /* three vertices */
130 feedback_vertex(fs->ctx, draw, prim->v[0]);
131 feedback_vertex(fs->ctx, draw, prim->v[1]);
132 feedback_vertex(fs->ctx, draw, prim->v[2]);
133 }
134
135
136 static void
137 feedback_line( struct draw_stage *stage, struct prim_header *prim )
138 {
139 struct feedback_stage *fs = feedback_stage(stage);
140 struct draw_context *draw = stage->draw;
141 if (fs->reset_stipple_counter) {
142 FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_LINE_RESET_TOKEN);
143 fs->reset_stipple_counter = GL_FALSE;
144 }
145 else {
146 FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_LINE_TOKEN);
147 }
148 feedback_vertex(fs->ctx, draw, prim->v[0]);
149 feedback_vertex(fs->ctx, draw, prim->v[1]);
150 }
151
152
153 static void
154 feedback_point( struct draw_stage *stage, struct prim_header *prim )
155 {
156 struct feedback_stage *fs = feedback_stage(stage);
157 struct draw_context *draw = stage->draw;
158 FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_POINT_TOKEN);
159 feedback_vertex(fs->ctx, draw, prim->v[0]);
160 }
161
162
163 static void
164 feedback_end( struct draw_stage *stage )
165 {
166 /* no-op */
167 }
168
169
170 static void
171 feedback_begin( struct draw_stage *stage )
172 {
173 /* no-op */
174 }
175
176
177 static void
178 feedback_reset_stipple_counter( struct draw_stage *stage )
179 {
180 struct feedback_stage *fs = feedback_stage(stage);
181 fs->reset_stipple_counter = GL_TRUE;
182 }
183
184
185 /**
186 * Create GL feedback drawing stage.
187 */
188 static struct draw_stage *
189 draw_glfeedback_stage(GLcontext *ctx, struct draw_context *draw)
190 {
191 struct feedback_stage *fs = CALLOC_STRUCT(feedback_stage);
192
193 fs->stage.draw = draw;
194 fs->stage.next = NULL;
195 fs->stage.begin = feedback_begin;
196 fs->stage.point = feedback_point;
197 fs->stage.line = feedback_line;
198 fs->stage.tri = feedback_tri;
199 fs->stage.end = feedback_end;
200 fs->stage.reset_stipple_counter = feedback_reset_stipple_counter;
201 fs->ctx = ctx;
202
203 return &fs->stage;
204 }
205
206
207
208 /**********************************************************************
209 * GL Selection functions
210 **********************************************************************/
211
212 static void
213 select_tri( struct draw_stage *stage, struct prim_header *prim )
214 {
215 struct feedback_stage *fs = feedback_stage(stage);
216 _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
217 _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
218 _mesa_update_hitflag( fs->ctx, prim->v[2]->data[0][2] );
219 }
220
221 static void
222 select_line( struct draw_stage *stage, struct prim_header *prim )
223 {
224 struct feedback_stage *fs = feedback_stage(stage);
225 _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
226 _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
227 }
228
229
230 static void
231 select_point( struct draw_stage *stage, struct prim_header *prim )
232 {
233 struct feedback_stage *fs = feedback_stage(stage);
234 _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
235 }
236
237
238 static void
239 select_begin( struct draw_stage *stage )
240 {
241 /* no-op */
242 }
243
244
245 static void
246 select_end( struct draw_stage *stage )
247 {
248 /* no-op */
249 }
250
251
252 static void
253 select_reset_stipple_counter( struct draw_stage *stage )
254 {
255 /* no-op */
256 }
257
258
259 /**
260 * Create GL selection mode drawing stage.
261 */
262 static struct draw_stage *
263 draw_glselect_stage(GLcontext *ctx, struct draw_context *draw)
264 {
265 struct feedback_stage *fs = CALLOC_STRUCT(feedback_stage);
266
267 fs->stage.draw = draw;
268 fs->stage.next = NULL;
269 fs->stage.begin = select_begin;
270 fs->stage.point = select_point;
271 fs->stage.line = select_line;
272 fs->stage.tri = select_tri;
273 fs->stage.end = select_end;
274 fs->stage.reset_stipple_counter = select_reset_stipple_counter;
275 fs->ctx = ctx;
276
277 return &fs->stage;
278 }
279
280
281 static void
282 st_RenderMode(GLcontext *ctx, GLenum newMode )
283 {
284 struct st_context *st = ctx->st;
285 struct vbo_context *vbo = (struct vbo_context *) ctx->swtnl_im;
286 struct draw_context *draw = st->draw;
287
288 if (newMode == GL_RENDER) {
289 /* restore normal VBO draw function */
290 vbo->draw_prims = st_draw_vbo;
291 }
292 else if (newMode == GL_SELECT) {
293 if (!st->selection_stage)
294 st->selection_stage = draw_glselect_stage(ctx, draw);
295 draw_set_rasterize_stage(draw, st->selection_stage);
296 /* Plug in new vbo draw function */
297 vbo->draw_prims = st_feedback_draw_vbo;
298 }
299 else {
300 if (!st->feedback_stage)
301 st->feedback_stage = draw_glfeedback_stage(ctx, draw);
302 draw_set_rasterize_stage(draw, st->feedback_stage);
303 /* Plug in new vbo draw function */
304 vbo->draw_prims = st_feedback_draw_vbo;
305 /* need to generate/use a vertex program that emits pos/color/tex */
306 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
307 }
308 }
309
310
311
312 void st_init_feedback_functions(struct dd_function_table *functions)
313 {
314 functions->RenderMode = st_RenderMode;
315 }