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
41 #include "main/context.h"
42 #include "main/feedback.h"
43 #include "main/framebuffer.h"
44 #include "main/varray.h"
45
46 #include "util/u_memory.h"
47
48 #include "vbo/vbo.h"
49
50 #include "st_context.h"
51 #include "st_draw.h"
52 #include "st_cb_feedback.h"
53 #include "st_program.h"
54 #include "st_util.h"
55
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58
59 #include "draw/draw_context.h"
60 #include "draw/draw_pipe.h"
61
62
63 /**
64 * This is actually used for both feedback and selection.
65 */
66 struct feedback_stage
67 {
68 struct draw_stage stage; /**< Base class */
69 struct gl_context *ctx; /**< Rendering context */
70 GLboolean reset_stipple_counter;
71 };
72
73
74 /**********************************************************************
75 * GL Feedback functions
76 **********************************************************************/
77
78 static inline struct feedback_stage *
feedback_stage(struct draw_stage * stage)79 feedback_stage( struct draw_stage *stage )
80 {
81 return (struct feedback_stage *)stage;
82 }
83
84
85 static void
feedback_vertex(struct gl_context * ctx,const struct draw_context * draw,const struct vertex_header * v)86 feedback_vertex(struct gl_context *ctx, const struct draw_context *draw,
87 const struct vertex_header *v)
88 {
89 struct gl_vertex_program *stvp =
90 (struct gl_vertex_program *)ctx->VertexProgram._Current;
91 GLfloat win[4];
92 const GLfloat *color, *texcoord;
93 uint8_t slot;
94
95 win[0] = v->data[0][0];
96 if (_mesa_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
97 win[1] = ctx->DrawBuffer->Height - v->data[0][1];
98 else
99 win[1] = v->data[0][1];
100 win[2] = v->data[0][2];
101 win[3] = 1.0F / v->data[0][3];
102
103 /* XXX
104 * When we compute vertex layout, save info about position of the
105 * color and texcoord attribs to use here.
106 */
107
108 slot = stvp->result_to_output[VARYING_SLOT_COL0];
109 if (slot != 0xff)
110 color = v->data[slot];
111 else
112 color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
113
114 slot = stvp->result_to_output[VARYING_SLOT_TEX0];
115 if (slot != 0xff)
116 texcoord = v->data[slot];
117 else
118 texcoord = ctx->Current.Attrib[VERT_ATTRIB_TEX0];
119
120 _mesa_feedback_vertex(ctx, win, color, texcoord);
121 }
122
123
124 static void
feedback_tri(struct draw_stage * stage,struct prim_header * prim)125 feedback_tri( struct draw_stage *stage, struct prim_header *prim )
126 {
127 struct feedback_stage *fs = feedback_stage(stage);
128 struct draw_context *draw = stage->draw;
129 _mesa_feedback_token(fs->ctx, (GLfloat) GL_POLYGON_TOKEN);
130 _mesa_feedback_token(fs->ctx, (GLfloat) 3); /* three vertices */
131 feedback_vertex(fs->ctx, draw, prim->v[0]);
132 feedback_vertex(fs->ctx, draw, prim->v[1]);
133 feedback_vertex(fs->ctx, draw, prim->v[2]);
134 }
135
136
137 static void
feedback_line(struct draw_stage * stage,struct prim_header * prim)138 feedback_line( struct draw_stage *stage, struct prim_header *prim )
139 {
140 struct feedback_stage *fs = feedback_stage(stage);
141 struct draw_context *draw = stage->draw;
142 if (fs->reset_stipple_counter) {
143 _mesa_feedback_token(fs->ctx, (GLfloat) GL_LINE_RESET_TOKEN);
144 fs->reset_stipple_counter = GL_FALSE;
145 }
146 else {
147 _mesa_feedback_token(fs->ctx, (GLfloat) GL_LINE_TOKEN);
148 }
149 feedback_vertex(fs->ctx, draw, prim->v[0]);
150 feedback_vertex(fs->ctx, draw, prim->v[1]);
151 }
152
153
154 static void
feedback_point(struct draw_stage * stage,struct prim_header * prim)155 feedback_point( struct draw_stage *stage, struct prim_header *prim )
156 {
157 struct feedback_stage *fs = feedback_stage(stage);
158 struct draw_context *draw = stage->draw;
159 _mesa_feedback_token(fs->ctx, (GLfloat) GL_POINT_TOKEN);
160 feedback_vertex(fs->ctx, draw, prim->v[0]);
161 }
162
163
164 static void
feedback_flush(struct draw_stage * stage,unsigned flags)165 feedback_flush( struct draw_stage *stage, unsigned flags )
166 {
167 /* no-op */
168 }
169
170
171 static void
feedback_reset_stipple_counter(struct draw_stage * stage)172 feedback_reset_stipple_counter( struct draw_stage *stage )
173 {
174 struct feedback_stage *fs = feedback_stage(stage);
175 fs->reset_stipple_counter = GL_TRUE;
176 }
177
178
179 static void
feedback_destroy(struct draw_stage * stage)180 feedback_destroy( struct draw_stage *stage )
181 {
182 FREE(stage);
183 }
184
185 /**
186 * Create GL feedback drawing stage.
187 */
188 static struct draw_stage *
draw_glfeedback_stage(struct gl_context * ctx,struct draw_context * draw)189 draw_glfeedback_stage(struct gl_context *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.point = feedback_point;
196 fs->stage.line = feedback_line;
197 fs->stage.tri = feedback_tri;
198 fs->stage.flush = feedback_flush;
199 fs->stage.reset_stipple_counter = feedback_reset_stipple_counter;
200 fs->stage.destroy = feedback_destroy;
201 fs->ctx = ctx;
202
203 return &fs->stage;
204 }
205
206
207
208 /**********************************************************************
209 * GL Selection functions
210 **********************************************************************/
211
212 static void
select_tri(struct draw_stage * stage,struct prim_header * prim)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
select_line(struct draw_stage * stage,struct prim_header * prim)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
select_point(struct draw_stage * stage,struct prim_header * prim)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
select_flush(struct draw_stage * stage,unsigned flags)239 select_flush( struct draw_stage *stage, unsigned flags )
240 {
241 /* no-op */
242 }
243
244
245 static void
select_reset_stipple_counter(struct draw_stage * stage)246 select_reset_stipple_counter( struct draw_stage *stage )
247 {
248 /* no-op */
249 }
250
251 static void
select_destroy(struct draw_stage * stage)252 select_destroy( struct draw_stage *stage )
253 {
254 FREE(stage);
255 }
256
257
258 /**
259 * Create GL selection mode drawing stage.
260 */
261 static struct draw_stage *
draw_glselect_stage(struct gl_context * ctx,struct draw_context * draw)262 draw_glselect_stage(struct gl_context *ctx, struct draw_context *draw)
263 {
264 struct feedback_stage *fs = CALLOC_STRUCT(feedback_stage);
265
266 fs->stage.draw = draw;
267 fs->stage.next = NULL;
268 fs->stage.point = select_point;
269 fs->stage.line = select_line;
270 fs->stage.tri = select_tri;
271 fs->stage.flush = select_flush;
272 fs->stage.reset_stipple_counter = select_reset_stipple_counter;
273 fs->stage.destroy = select_destroy;
274 fs->ctx = ctx;
275
276 return &fs->stage;
277 }
278
279
280 void
st_RenderMode(struct gl_context * ctx,GLenum newMode)281 st_RenderMode(struct gl_context *ctx, GLenum newMode )
282 {
283 struct st_context *st = st_context(ctx);
284 struct draw_context *draw = st_get_draw_context(st);
285
286 if (!st->draw)
287 return;
288
289 if (newMode == GL_RENDER) {
290 /* restore normal VBO draw function */
291 st_init_draw_functions(st->screen, &ctx->Driver);
292 }
293 else if (newMode == GL_SELECT) {
294 if (ctx->Const.HardwareAcceleratedSelect)
295 st_init_hw_select_draw_functions(st->screen, &ctx->Driver);
296 else {
297 if (!st->selection_stage)
298 st->selection_stage = draw_glselect_stage(ctx, draw);
299 draw_set_rasterize_stage(draw, st->selection_stage);
300 /* Plug in new vbo draw function */
301 ctx->Driver.DrawGallium = st_feedback_draw_vbo;
302 ctx->Driver.DrawGalliumMultiMode = st_feedback_draw_vbo_multi_mode;
303 }
304 }
305 else {
306 struct gl_program *vp = st->ctx->VertexProgram._Current;
307
308 if (!st->feedback_stage)
309 st->feedback_stage = draw_glfeedback_stage(ctx, draw);
310 draw_set_rasterize_stage(draw, st->feedback_stage);
311 /* Plug in new vbo draw function */
312 ctx->Driver.DrawGallium = st_feedback_draw_vbo;
313 ctx->Driver.DrawGalliumMultiMode = st_feedback_draw_vbo_multi_mode;
314 /* need to generate/use a vertex program that emits pos/color/tex */
315 if (vp)
316 ctx->NewDriverState |= ST_NEW_VERTEX_PROGRAM(ctx, vp);
317 }
318
319 /* Restore geometry shader states when leaving GL_SELECT mode. */
320 if (ctx->RenderMode == GL_SELECT && ctx->Const.HardwareAcceleratedSelect)
321 ctx->NewDriverState |= ST_NEW_GS_SSBOS | ST_NEW_GS_CONSTANTS | ST_NEW_GS_STATE;
322 }
323