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