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