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 * glRasterPos implementation. Basically render a GL_POINT with our
30 * private draw module. Plug in a special "rasterpos" stage at the end
31 * of the 'draw' pipeline to capture the results and update the current
32 * raster pos attributes.
33 *
34 * Authors:
35 * Brian Paul
36 */
37
38
39
40 #include "main/macros.h"
41 #include "main/arrayobj.h"
42 #include "main/feedback.h"
43 #include "main/rastpos.h"
44 #include "main/state.h"
45 #include "main/varray.h"
46
47 #include "st_context.h"
48 #include "st_atom.h"
49 #include "st_draw.h"
50 #include "st_program.h"
51 #include "st_cb_rasterpos.h"
52 #include "st_util.h"
53 #include "draw/draw_context.h"
54 #include "draw/draw_pipe.h"
55 #include "vbo/vbo.h"
56
57
58 /**
59 * Our special drawing pipeline stage (replaces rasterization).
60 */
61 struct rastpos_stage
62 {
63 struct draw_stage stage; /**< Base class */
64 struct gl_context *ctx; /**< Rendering context */
65
66 /* vertex attrib info we can setup once and re-use */
67 struct gl_vertex_array_object *VAO;
68 struct _mesa_prim prim;
69 };
70
71
72 static inline struct rastpos_stage *
rastpos_stage(struct draw_stage * stage)73 rastpos_stage( struct draw_stage *stage )
74 {
75 return (struct rastpos_stage *) stage;
76 }
77
78 static void
rastpos_flush(struct draw_stage * stage,unsigned flags)79 rastpos_flush( struct draw_stage *stage, unsigned flags )
80 {
81 /* no-op */
82 }
83
84 static void
rastpos_reset_stipple_counter(struct draw_stage * stage)85 rastpos_reset_stipple_counter( struct draw_stage *stage )
86 {
87 /* no-op */
88 }
89
90 static void
rastpos_tri(struct draw_stage * stage,struct prim_header * prim)91 rastpos_tri( struct draw_stage *stage, struct prim_header *prim )
92 {
93 /* should never get here */
94 assert(0);
95 }
96
97 static void
rastpos_line(struct draw_stage * stage,struct prim_header * prim)98 rastpos_line( struct draw_stage *stage, struct prim_header *prim )
99 {
100 /* should never get here */
101 assert(0);
102 }
103
104 static void
rastpos_destroy(struct draw_stage * stage)105 rastpos_destroy(struct draw_stage *stage)
106 {
107 struct rastpos_stage *rstage = (struct rastpos_stage*)stage;
108 _mesa_reference_vao(rstage->ctx, &rstage->VAO, NULL);
109 free(stage);
110 }
111
112
113 /**
114 * Update a raster pos attribute from the vertex result if it's present,
115 * else copy the current attrib.
116 */
117 static void
update_attrib(struct gl_context * ctx,const ubyte * outputMapping,const struct vertex_header * vert,GLfloat * dest,GLuint result,GLuint defaultAttrib)118 update_attrib(struct gl_context *ctx, const ubyte *outputMapping,
119 const struct vertex_header *vert,
120 GLfloat *dest,
121 GLuint result, GLuint defaultAttrib)
122 {
123 const GLfloat *src;
124 const ubyte k = outputMapping[result];
125 if (k != 0xff)
126 src = vert->data[k];
127 else
128 src = ctx->Current.Attrib[defaultAttrib];
129 COPY_4V(dest, src);
130 }
131
132
133 /**
134 * Normally, this function would render a GL_POINT.
135 */
136 static void
rastpos_point(struct draw_stage * stage,struct prim_header * prim)137 rastpos_point(struct draw_stage *stage, struct prim_header *prim)
138 {
139 struct rastpos_stage *rs = rastpos_stage(stage);
140 struct gl_context *ctx = rs->ctx;
141 struct st_context *st = st_context(ctx);
142 const GLfloat height = (GLfloat) ctx->DrawBuffer->Height;
143 struct st_vertex_program *stvp = (struct st_vertex_program *)st->vp;
144 const ubyte *outputMapping = stvp->result_to_output;
145 const GLfloat *pos;
146 GLuint i;
147
148 ctx->PopAttribState |= GL_CURRENT_BIT;
149
150 /* if we get here, we didn't get clipped */
151 ctx->Current.RasterPosValid = GL_TRUE;
152
153 /* update raster pos */
154 pos = prim->v[0]->data[0];
155 ctx->Current.RasterPos[0] = pos[0];
156 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
157 ctx->Current.RasterPos[1] = height - pos[1]; /* invert Y */
158 else
159 ctx->Current.RasterPos[1] = pos[1];
160 ctx->Current.RasterPos[2] = pos[2];
161 ctx->Current.RasterPos[3] = pos[3];
162
163 /* update other raster attribs */
164 update_attrib(ctx, outputMapping, prim->v[0],
165 ctx->Current.RasterColor,
166 VARYING_SLOT_COL0, VERT_ATTRIB_COLOR0);
167
168 update_attrib(ctx, outputMapping, prim->v[0],
169 ctx->Current.RasterSecondaryColor,
170 VARYING_SLOT_COL1, VERT_ATTRIB_COLOR1);
171
172 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
173 update_attrib(ctx, outputMapping, prim->v[0],
174 ctx->Current.RasterTexCoords[i],
175 VARYING_SLOT_TEX0 + i, VERT_ATTRIB_TEX0 + i);
176 }
177
178 if (ctx->RenderMode == GL_SELECT) {
179 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
180 }
181 }
182
183
184 /**
185 * Create rasterpos "drawing" stage.
186 */
187 static struct rastpos_stage *
new_draw_rastpos_stage(struct gl_context * ctx,struct draw_context * draw)188 new_draw_rastpos_stage(struct gl_context *ctx, struct draw_context *draw)
189 {
190 struct rastpos_stage *rs = ST_CALLOC_STRUCT(rastpos_stage);
191
192 rs->stage.draw = draw;
193 rs->stage.next = NULL;
194 rs->stage.point = rastpos_point;
195 rs->stage.line = rastpos_line;
196 rs->stage.tri = rastpos_tri;
197 rs->stage.flush = rastpos_flush;
198 rs->stage.destroy = rastpos_destroy;
199 rs->stage.reset_stipple_counter = rastpos_reset_stipple_counter;
200 rs->stage.destroy = rastpos_destroy;
201 rs->ctx = ctx;
202
203 rs->VAO = _mesa_new_vao(ctx, ~((GLuint)0));
204 _mesa_vertex_attrib_binding(ctx, rs->VAO, VERT_ATTRIB_POS, 0);
205 _mesa_update_array_format(ctx, rs->VAO, VERT_ATTRIB_POS, 4, GL_FLOAT,
206 GL_RGBA, GL_FALSE, GL_FALSE, GL_FALSE, 0);
207 _mesa_enable_vertex_array_attrib(ctx, rs->VAO, 0);
208
209 rs->prim.mode = GL_POINTS;
210 rs->prim.begin = 1;
211 rs->prim.end = 1;
212 rs->prim.start = 0;
213 rs->prim.count = 1;
214
215 return rs;
216 }
217
218
219 static void
st_RasterPos(struct gl_context * ctx,const GLfloat v[4])220 st_RasterPos(struct gl_context *ctx, const GLfloat v[4])
221 {
222 struct st_context *st = st_context(ctx);
223 struct draw_context *draw = st_get_draw_context(st);
224 struct rastpos_stage *rs;
225
226 if (!st->draw)
227 return;
228
229 if (ctx->VertexProgram._Current == NULL ||
230 ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram) {
231 /* No vertex shader/program is enabled, used the simple/fast fixed-
232 * function implementation of RasterPos.
233 */
234 _mesa_RasterPos(ctx, v);
235 return;
236 }
237
238 if (st->rastpos_stage) {
239 /* get rastpos stage info */
240 rs = rastpos_stage(st->rastpos_stage);
241 }
242 else {
243 /* create rastpos draw stage */
244 rs = new_draw_rastpos_stage(ctx, draw);
245 st->rastpos_stage = &rs->stage;
246 }
247
248 /* plug our rastpos stage into the draw module */
249 draw_set_rasterize_stage(st->draw, st->rastpos_stage);
250
251 /* make sure everything's up to date */
252 st_validate_state(st, ST_PIPELINE_RENDER);
253
254 /* This will get set only if rastpos_point(), above, gets called */
255 ctx->PopAttribState |= GL_CURRENT_BIT;
256 ctx->Current.RasterPosValid = GL_FALSE;
257
258 /* All vertex attribs but position were previously initialized above.
259 * Just plug in position pointer now.
260 */
261 rs->VAO->VertexAttrib[VERT_ATTRIB_POS].Ptr = (GLubyte *) v;
262 rs->VAO->NewArrays |= VERT_BIT_POS;
263 _mesa_set_draw_vao(ctx, rs->VAO, VERT_BIT_POS);
264
265 /* Draw the point. */
266 st_feedback_draw_vbo(ctx, &rs->prim, 1, NULL, true, false, 0, 0, 1, 1, 0);
267
268 /* restore draw's rasterization stage depending on rendermode */
269 if (ctx->RenderMode == GL_FEEDBACK) {
270 draw_set_rasterize_stage(draw, st->feedback_stage);
271 }
272 else if (ctx->RenderMode == GL_SELECT) {
273 draw_set_rasterize_stage(draw, st->selection_stage);
274 }
275 }
276
277
278
st_init_rasterpos_functions(struct dd_function_table * functions)279 void st_init_rasterpos_functions(struct dd_function_table *functions)
280 {
281 functions->RasterPos = st_RasterPos;
282 }
283