1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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.
40 #include "main/imports.h"
41 #include "main/context.h"
42 #include "main/feedback.h"
43 #include "main/macros.h"
47 #include "st_context.h"
50 #include "st_cb_feedback.h"
51 #include "st_cb_bufferobjects.h"
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"
58 #include "pipe/draw/draw_context.h"
59 #include "pipe/draw/draw_private.h"
63 * This is actually used for both feedback and selection.
67 struct draw_stage stage
; /**< Base class */
68 GLcontext
*ctx
; /**< Rendering context */
69 GLboolean reset_stipple_counter
;
73 /**********************************************************************
74 * GL Feedback functions
75 **********************************************************************/
77 static INLINE
struct feedback_stage
*
78 feedback_stage( struct draw_stage
*stage
)
80 return (struct feedback_stage
*)stage
;
85 feedback_vertex(GLcontext
*ctx
, const struct draw_context
*draw
,
86 const struct vertex_header
*v
)
88 const struct st_context
*st
= ctx
->st
;
90 const GLfloat
*color
, *texcoord
;
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];
101 * When we compute vertex layout, save info about position of the
102 * color and texcoord attribs to use here.
105 slot
= st
->vertex_result_to_slot
[VERT_RESULT_COL0
];
107 color
= v
->data
[slot
];
109 color
= ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
];
111 slot
= st
->vertex_result_to_slot
[VERT_RESULT_TEX0
];
113 texcoord
= v
->data
[slot
];
115 texcoord
= ctx
->Current
.Attrib
[VERT_ATTRIB_TEX0
];
117 _mesa_feedback_vertex(ctx
, win
, color
, ci
, texcoord
);
122 feedback_tri( struct draw_stage
*stage
, struct prim_header
*prim
)
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]);
135 feedback_line( struct draw_stage
*stage
, struct prim_header
*prim
)
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
;
144 FEEDBACK_TOKEN(fs
->ctx
, (GLfloat
) GL_LINE_TOKEN
);
146 feedback_vertex(fs
->ctx
, draw
, prim
->v
[0]);
147 feedback_vertex(fs
->ctx
, draw
, prim
->v
[1]);
152 feedback_point( struct draw_stage
*stage
, struct prim_header
*prim
)
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]);
162 feedback_flush( struct draw_stage
*stage
, unsigned flags
)
169 feedback_reset_stipple_counter( struct draw_stage
*stage
)
171 struct feedback_stage
*fs
= feedback_stage(stage
);
172 fs
->reset_stipple_counter
= GL_TRUE
;
177 * Create GL feedback drawing stage.
179 static struct draw_stage
*
180 draw_glfeedback_stage(GLcontext
*ctx
, struct draw_context
*draw
)
182 struct feedback_stage
*fs
= CALLOC_STRUCT(feedback_stage
);
184 fs
->stage
.draw
= draw
;
185 fs
->stage
.next
= NULL
;
186 fs
->stage
.point
= feedback_point
;
187 fs
->stage
.line
= feedback_line
;
188 fs
->stage
.tri
= feedback_tri
;
189 fs
->stage
.flush
= feedback_flush
;
190 fs
->stage
.reset_stipple_counter
= feedback_reset_stipple_counter
;
198 /**********************************************************************
199 * GL Selection functions
200 **********************************************************************/
203 select_tri( struct draw_stage
*stage
, struct prim_header
*prim
)
205 struct feedback_stage
*fs
= feedback_stage(stage
);
206 _mesa_update_hitflag( fs
->ctx
, prim
->v
[0]->data
[0][2] );
207 _mesa_update_hitflag( fs
->ctx
, prim
->v
[1]->data
[0][2] );
208 _mesa_update_hitflag( fs
->ctx
, prim
->v
[2]->data
[0][2] );
212 select_line( struct draw_stage
*stage
, struct prim_header
*prim
)
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] );
221 select_point( struct draw_stage
*stage
, struct prim_header
*prim
)
223 struct feedback_stage
*fs
= feedback_stage(stage
);
224 _mesa_update_hitflag( fs
->ctx
, prim
->v
[0]->data
[0][2] );
229 select_flush( struct draw_stage
*stage
, unsigned flags
)
236 select_reset_stipple_counter( struct draw_stage
*stage
)
243 * Create GL selection mode drawing stage.
245 static struct draw_stage
*
246 draw_glselect_stage(GLcontext
*ctx
, struct draw_context
*draw
)
248 struct feedback_stage
*fs
= CALLOC_STRUCT(feedback_stage
);
250 fs
->stage
.draw
= draw
;
251 fs
->stage
.next
= NULL
;
252 fs
->stage
.point
= select_point
;
253 fs
->stage
.line
= select_line
;
254 fs
->stage
.tri
= select_tri
;
255 fs
->stage
.flush
= select_flush
;
256 fs
->stage
.reset_stipple_counter
= select_reset_stipple_counter
;
264 st_RenderMode(GLcontext
*ctx
, GLenum newMode
)
266 struct st_context
*st
= ctx
->st
;
267 struct draw_context
*draw
= st
->draw
;
269 if (newMode
== GL_RENDER
) {
270 /* restore normal VBO draw function */
271 vbo_set_draw_func(ctx
, st_draw_vbo
);
273 else if (newMode
== GL_SELECT
) {
274 if (!st
->selection_stage
)
275 st
->selection_stage
= draw_glselect_stage(ctx
, draw
);
276 draw_set_rasterize_stage(draw
, st
->selection_stage
);
277 /* Plug in new vbo draw function */
278 vbo_set_draw_func(ctx
, st_feedback_draw_vbo
);
281 if (!st
->feedback_stage
)
282 st
->feedback_stage
= draw_glfeedback_stage(ctx
, draw
);
283 draw_set_rasterize_stage(draw
, st
->feedback_stage
);
284 /* Plug in new vbo draw function */
285 vbo_set_draw_func(ctx
, st_feedback_draw_vbo
);
286 /* need to generate/use a vertex program that emits pos/color/tex */
287 st
->dirty
.st
|= ST_NEW_VERTEX_PROGRAM
;
293 void st_init_feedback_functions(struct dd_function_table
*functions
)
295 functions
->RenderMode
= st_RenderMode
;