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