st_cb_feedback.c revision de69fc1703f79e5c97e66b654de7a93b7abce8f0
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/tgsi/exec/tgsi_attribs.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 */
67struct 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
79static INLINE struct feedback_stage *
80feedback_stage( struct draw_stage *stage )
81{
82   return (struct feedback_stage *)stage;
83}
84
85
86static void
87feedback_vertex(GLcontext *ctx, const struct draw_context *draw,
88                const struct vertex_header *v)
89{
90   GLfloat win[4];
91   const GLfloat *color, *texcoord;
92   const GLfloat ci = 0;
93   GLuint slot;
94
95   win[0] = v->data[0][0];
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   slot = draw->vertex_info.attrib_to_slot[TGSI_ATTRIB_COLOR0];
101   if (slot)
102      color = v->data[slot];
103   else
104      color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
105
106   slot = draw->vertex_info.attrib_to_slot[TGSI_ATTRIB_TEX0];
107   if (slot)
108      texcoord = v->data[slot];
109   else
110      texcoord = ctx->Current.Attrib[VERT_ATTRIB_TEX0];
111
112   _mesa_feedback_vertex(ctx, win, color, ci, texcoord);
113}
114
115
116static void
117feedback_tri( struct draw_stage *stage, struct prim_header *prim )
118{
119   struct feedback_stage *fs = feedback_stage(stage);
120   struct draw_context *draw = stage->draw;
121   FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_POLYGON_TOKEN);
122   FEEDBACK_TOKEN(fs->ctx, (GLfloat) 3); /* three vertices */
123   feedback_vertex(fs->ctx, draw, prim->v[0]);
124   feedback_vertex(fs->ctx, draw, prim->v[1]);
125   feedback_vertex(fs->ctx, draw, prim->v[2]);
126}
127
128
129static void
130feedback_line( struct draw_stage *stage, struct prim_header *prim )
131{
132   struct feedback_stage *fs = feedback_stage(stage);
133   struct draw_context *draw = stage->draw;
134   if (fs->reset_stipple_counter) {
135      FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_LINE_RESET_TOKEN);
136      fs->reset_stipple_counter = GL_FALSE;
137   }
138   else {
139      FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_LINE_TOKEN);
140   }
141   feedback_vertex(fs->ctx, draw, prim->v[0]);
142   feedback_vertex(fs->ctx, draw, prim->v[1]);
143}
144
145
146static void
147feedback_point( struct draw_stage *stage, struct prim_header *prim )
148{
149   struct feedback_stage *fs = feedback_stage(stage);
150   struct draw_context *draw = stage->draw;
151   FEEDBACK_TOKEN(fs->ctx, (GLfloat) GL_POINT_TOKEN);
152   feedback_vertex(fs->ctx, draw, prim->v[0]);
153}
154
155
156static void
157feedback_end( struct draw_stage *stage )
158{
159   /* no-op */
160}
161
162
163static void
164feedback_begin( struct draw_stage *stage )
165{
166   /* no-op */
167}
168
169
170static void
171feedback_reset_stipple_counter( struct draw_stage *stage )
172{
173   struct feedback_stage *fs = feedback_stage(stage);
174   fs->reset_stipple_counter = GL_TRUE;
175}
176
177
178/**
179 * Create GL feedback drawing stage.
180 */
181static struct draw_stage *
182draw_glfeedback_stage(GLcontext *ctx, struct draw_context *draw)
183{
184   struct feedback_stage *fs = CALLOC_STRUCT(feedback_stage);
185
186   fs->stage.draw = draw;
187   fs->stage.next = NULL;
188   fs->stage.begin = feedback_begin;
189   fs->stage.point = feedback_point;
190   fs->stage.line = feedback_line;
191   fs->stage.tri = feedback_tri;
192   fs->stage.end = feedback_end;
193   fs->stage.reset_stipple_counter = feedback_reset_stipple_counter;
194   fs->ctx = ctx;
195
196   return &fs->stage;
197}
198
199
200
201/**********************************************************************
202 * GL Selection functions
203 **********************************************************************/
204
205static void
206select_tri( struct draw_stage *stage, struct prim_header *prim )
207{
208   struct feedback_stage *fs = feedback_stage(stage);
209   _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
210   _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
211   _mesa_update_hitflag( fs->ctx, prim->v[2]->data[0][2] );
212}
213
214static void
215select_line( struct draw_stage *stage, struct prim_header *prim )
216{
217   struct feedback_stage *fs = feedback_stage(stage);
218   _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
219   _mesa_update_hitflag( fs->ctx, prim->v[1]->data[0][2] );
220}
221
222
223static void
224select_point( struct draw_stage *stage, struct prim_header *prim )
225{
226   struct feedback_stage *fs = feedback_stage(stage);
227   _mesa_update_hitflag( fs->ctx, prim->v[0]->data[0][2] );
228}
229
230
231static void
232select_begin( struct draw_stage *stage )
233{
234   /* no-op */
235}
236
237
238static void
239select_end( struct draw_stage *stage )
240{
241   /* no-op */
242}
243
244
245static void
246select_reset_stipple_counter( struct draw_stage *stage )
247{
248   /* no-op */
249}
250
251
252/**
253 * Create GL selection mode drawing stage.
254 */
255static struct draw_stage *
256draw_glselect_stage(GLcontext *ctx, struct draw_context *draw)
257{
258   struct feedback_stage *fs = CALLOC_STRUCT(feedback_stage);
259
260   fs->stage.draw = draw;
261   fs->stage.next = NULL;
262   fs->stage.begin = select_begin;
263   fs->stage.point = select_point;
264   fs->stage.line = select_line;
265   fs->stage.tri = select_tri;
266   fs->stage.end = select_end;
267   fs->stage.reset_stipple_counter = select_reset_stipple_counter;
268   fs->ctx = ctx;
269
270   return &fs->stage;
271}
272
273
274static void
275st_RenderMode(GLcontext *ctx, GLenum newMode )
276{
277   struct st_context *st = ctx->st;
278   struct vbo_context *vbo = (struct vbo_context *) ctx->swtnl_im;
279   struct draw_context *draw = st->draw;
280
281   if (newMode == GL_RENDER) {
282      /* restore normal VBO draw function */
283      vbo->draw_prims = st_draw_vbo;
284   }
285   else if (newMode == GL_SELECT) {
286      if (!st->selection_stage)
287         st->selection_stage = draw_glselect_stage(ctx, draw);
288      draw_set_rasterize_stage(draw, st->selection_stage);
289      /* Plug in new vbo draw function */
290      vbo->draw_prims = st_feedback_draw_vbo;
291   }
292   else {
293      if (!st->feedback_stage)
294         st->feedback_stage = draw_glfeedback_stage(ctx, draw);
295      draw_set_rasterize_stage(draw, st->feedback_stage);
296      /* Plug in new vbo draw function */
297      vbo->draw_prims = st_feedback_draw_vbo;
298   }
299}
300
301
302
303void st_init_feedback_functions(struct dd_function_table *functions)
304{
305   functions->RenderMode = st_RenderMode;
306}
307