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/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#include "st_util.h"
52
53#include "pipe/p_context.h"
54#include "pipe/p_defines.h"
55
56#include "draw/draw_context.h"
57#include "draw/draw_pipe.h"
58
59
60/**
61 * This is actually used for both feedback and selection.
62 */
63struct feedback_stage
64{
65   struct draw_stage stage;   /**< Base class */
66   struct gl_context *ctx;            /**< Rendering context */
67   GLboolean reset_stipple_counter;
68};
69
70
71/**********************************************************************
72 * GL Feedback functions
73 **********************************************************************/
74
75static inline struct feedback_stage *
76feedback_stage( struct draw_stage *stage )
77{
78   return (struct feedback_stage *)stage;
79}
80
81
82static void
83feedback_vertex(struct gl_context *ctx, const struct draw_context *draw,
84                const struct vertex_header *v)
85{
86   const struct st_context *st = st_context(ctx);
87   struct st_vertex_program *stvp = (struct st_vertex_program *)st->vp;
88   GLfloat win[4];
89   const GLfloat *color, *texcoord;
90   ubyte slot;
91
92   win[0] = v->data[0][0];
93   if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
94      win[1] = ctx->DrawBuffer->Height - v->data[0][1];
95   else
96      win[1] = 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 = stvp->result_to_output[VARYING_SLOT_COL0];
106   if (slot != 0xff)
107      color = v->data[slot];
108   else
109      color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
110
111   slot = stvp->result_to_output[VARYING_SLOT_TEX0];
112   if (slot != 0xff)
113      texcoord = v->data[slot];
114   else
115      texcoord = ctx->Current.Attrib[VERT_ATTRIB_TEX0];
116
117   _mesa_feedback_vertex(ctx, win, color, texcoord);
118}
119
120
121static void
122feedback_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   _mesa_feedback_token(fs->ctx, (GLfloat) GL_POLYGON_TOKEN);
127   _mesa_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
134static void
135feedback_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      _mesa_feedback_token(fs->ctx, (GLfloat) GL_LINE_RESET_TOKEN);
141      fs->reset_stipple_counter = GL_FALSE;
142   }
143   else {
144      _mesa_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
151static void
152feedback_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   _mesa_feedback_token(fs->ctx, (GLfloat) GL_POINT_TOKEN);
157   feedback_vertex(fs->ctx, draw, prim->v[0]);
158}
159
160
161static void
162feedback_flush( struct draw_stage *stage, unsigned flags )
163{
164   /* no-op */
165}
166
167
168static void
169feedback_reset_stipple_counter( struct draw_stage *stage )
170{
171   struct feedback_stage *fs = feedback_stage(stage);
172   fs->reset_stipple_counter = GL_TRUE;
173}
174
175
176static void
177feedback_destroy( struct draw_stage *stage )
178{
179   free(stage);
180}
181
182/**
183 * Create GL feedback drawing stage.
184 */
185static struct draw_stage *
186draw_glfeedback_stage(struct gl_context *ctx, struct draw_context *draw)
187{
188   struct feedback_stage *fs = ST_CALLOC_STRUCT(feedback_stage);
189
190   fs->stage.draw = draw;
191   fs->stage.next = NULL;
192   fs->stage.point = feedback_point;
193   fs->stage.line = feedback_line;
194   fs->stage.tri = feedback_tri;
195   fs->stage.flush = feedback_flush;
196   fs->stage.reset_stipple_counter = feedback_reset_stipple_counter;
197   fs->stage.destroy = feedback_destroy;
198   fs->ctx = ctx;
199
200   return &fs->stage;
201}
202
203
204
205/**********************************************************************
206 * GL Selection functions
207 **********************************************************************/
208
209static void
210select_tri( struct draw_stage *stage, struct prim_header *prim )
211{
212   struct feedback_stage *fs = feedback_stage(stage);
213   _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
214   _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
215   _mesa_update_hitflag( fs->ctx, prim->v[2]->data[0][2] );
216}
217
218static void
219select_line( struct draw_stage *stage, struct prim_header *prim )
220{
221   struct feedback_stage *fs = feedback_stage(stage);
222   _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
223   _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
224}
225
226
227static void
228select_point( struct draw_stage *stage, struct prim_header *prim )
229{
230   struct feedback_stage *fs = feedback_stage(stage);
231   _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
232}
233
234
235static void
236select_flush( struct draw_stage *stage, unsigned flags )
237{
238   /* no-op */
239}
240
241
242static void
243select_reset_stipple_counter( struct draw_stage *stage )
244{
245   /* no-op */
246}
247
248static void
249select_destroy( struct draw_stage *stage )
250{
251   free(stage);
252}
253
254
255/**
256 * Create GL selection mode drawing stage.
257 */
258static struct draw_stage *
259draw_glselect_stage(struct gl_context *ctx, struct draw_context *draw)
260{
261   struct feedback_stage *fs = ST_CALLOC_STRUCT(feedback_stage);
262
263   fs->stage.draw = draw;
264   fs->stage.next = NULL;
265   fs->stage.point = select_point;
266   fs->stage.line = select_line;
267   fs->stage.tri = select_tri;
268   fs->stage.flush = select_flush;
269   fs->stage.reset_stipple_counter = select_reset_stipple_counter;
270   fs->stage.destroy = select_destroy;
271   fs->ctx = ctx;
272
273   return &fs->stage;
274}
275
276
277static void
278st_RenderMode(struct gl_context *ctx, GLenum newMode )
279{
280   struct st_context *st = st_context(ctx);
281   struct draw_context *draw = st_get_draw_context(st);
282
283   if (!st->draw)
284      return;
285
286   if (newMode == GL_RENDER) {
287      /* restore normal VBO draw function */
288      st_init_draw_functions(st->screen, &ctx->Driver);
289   }
290   else if (newMode == GL_SELECT) {
291      if (!st->selection_stage)
292         st->selection_stage = draw_glselect_stage(ctx, draw);
293      draw_set_rasterize_stage(draw, st->selection_stage);
294      /* Plug in new vbo draw function */
295      ctx->Driver.Draw = st_feedback_draw_vbo;
296      ctx->Driver.DrawGallium = _mesa_draw_gallium_fallback;
297      ctx->Driver.DrawGalliumMultiMode = _mesa_draw_gallium_multimode_fallback;
298   }
299   else {
300      struct gl_program *vp = st->ctx->VertexProgram._Current;
301
302      if (!st->feedback_stage)
303         st->feedback_stage = draw_glfeedback_stage(ctx, draw);
304      draw_set_rasterize_stage(draw, st->feedback_stage);
305      /* Plug in new vbo draw function */
306      ctx->Driver.Draw = st_feedback_draw_vbo;
307      ctx->Driver.DrawGallium = _mesa_draw_gallium_fallback;
308      ctx->Driver.DrawGalliumMultiMode = _mesa_draw_gallium_multimode_fallback;
309      /* need to generate/use a vertex program that emits pos/color/tex */
310      if (vp)
311         st->dirty |= ST_NEW_VERTEX_PROGRAM(st, st_program(vp));
312   }
313}
314
315
316
317void st_init_feedback_functions(struct dd_function_table *functions)
318{
319   functions->RenderMode = st_RenderMode;
320}
321