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