Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / state_tracker / st_cb_rasterpos.c
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  * 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/mfeatures.h"
42 #include "main/feedback.h"
43
44 #include "st_context.h"
45 #include "st_atom.h"
46 #include "st_draw.h"
47 #include "st_cb_rasterpos.h"
48 #include "draw/draw_context.h"
49 #include "draw/draw_pipe.h"
50 #include "vbo/vbo.h"
51
52
53 #if FEATURE_rastpos
54
55 /**
56  * Our special drawing pipeline stage (replaces rasterization).
57  */
58 struct rastpos_stage
59 {
60    struct draw_stage stage;   /**< Base class */
61    struct gl_context *ctx;            /**< Rendering context */
62
63    /* vertex attrib info we can setup once and re-use */
64    struct gl_client_array array[VERT_ATTRIB_MAX];
65    const struct gl_client_array *arrays[VERT_ATTRIB_MAX];
66    struct _mesa_prim prim;
67 };
68
69
70 static INLINE struct rastpos_stage *
71 rastpos_stage( struct draw_stage *stage )
72 {
73    return (struct rastpos_stage *) stage;
74 }
75
76 static void
77 rastpos_flush( struct draw_stage *stage, unsigned flags )
78 {
79    /* no-op */
80 }
81
82 static void
83 rastpos_reset_stipple_counter( struct draw_stage *stage )
84 {
85    /* no-op */
86 }
87
88 static void
89 rastpos_tri( struct draw_stage *stage, struct prim_header *prim )
90 {
91    /* should never get here */
92    assert(0);
93 }
94
95 static void
96 rastpos_line( struct draw_stage *stage, struct prim_header *prim )
97 {
98    /* should never get here */
99    assert(0);
100 }
101
102 static void
103 rastpos_destroy(struct draw_stage *stage)
104 {
105    free(stage);
106 }
107
108
109 /**
110  * Update a raster pos attribute from the vertex result if it's present,
111  * else copy the current attrib.
112  */
113 static void
114 update_attrib(struct gl_context *ctx, const GLuint *outputMapping,
115               const struct vertex_header *vert,
116               GLfloat *dest,
117               GLuint result, GLuint defaultAttrib)
118 {
119    const GLfloat *src;
120    const GLuint k = outputMapping[result];
121    if (k != ~0U)
122       src = vert->data[k];
123    else
124       src = ctx->Current.Attrib[defaultAttrib];
125    COPY_4V(dest, src);
126 }
127
128
129 /**
130  * Normally, this function would render a GL_POINT.
131  */
132 static void
133 rastpos_point(struct draw_stage *stage, struct prim_header *prim)
134 {
135    struct rastpos_stage *rs = rastpos_stage(stage);
136    struct gl_context *ctx = rs->ctx;
137    struct st_context *st = st_context(ctx);
138    const GLfloat height = (GLfloat) ctx->DrawBuffer->Height;
139    const GLuint *outputMapping = st->vertex_result_to_slot;
140    const GLfloat *pos;
141    GLuint i;
142
143    /* if we get here, we didn't get clipped */
144    ctx->Current.RasterPosValid = GL_TRUE;
145
146    /* update raster pos */
147    pos = prim->v[0]->data[0];
148    ctx->Current.RasterPos[0] = pos[0];
149    if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
150       ctx->Current.RasterPos[1] = height - pos[1]; /* invert Y */
151    else
152       ctx->Current.RasterPos[1] = pos[1];
153    ctx->Current.RasterPos[2] = pos[2];
154    ctx->Current.RasterPos[3] = pos[3];
155
156    /* update other raster attribs */
157    update_attrib(ctx, outputMapping, prim->v[0],
158                  ctx->Current.RasterColor,
159                  VERT_RESULT_COL0, VERT_ATTRIB_COLOR0);
160
161    update_attrib(ctx, outputMapping, prim->v[0],
162                  ctx->Current.RasterSecondaryColor,
163                  VERT_RESULT_COL1, VERT_ATTRIB_COLOR1);
164
165    for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
166       update_attrib(ctx, outputMapping, prim->v[0],
167                     ctx->Current.RasterTexCoords[i],
168                     VERT_RESULT_TEX0 + i, VERT_ATTRIB_TEX0 + i);
169    }
170
171    if (ctx->RenderMode == GL_SELECT) {
172       _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
173    }
174 }
175
176
177 /**
178  * Create rasterpos "drawing" stage.
179  */
180 static struct rastpos_stage *
181 new_draw_rastpos_stage(struct gl_context *ctx, struct draw_context *draw)
182 {
183    struct rastpos_stage *rs = ST_CALLOC_STRUCT(rastpos_stage);
184    GLuint i;
185
186    rs->stage.draw = draw;
187    rs->stage.next = NULL;
188    rs->stage.point = rastpos_point;
189    rs->stage.line = rastpos_line;
190    rs->stage.tri = rastpos_tri;
191    rs->stage.flush = rastpos_flush;
192    rs->stage.destroy = rastpos_destroy;
193    rs->stage.reset_stipple_counter = rastpos_reset_stipple_counter;
194    rs->stage.destroy = rastpos_destroy;
195    rs->ctx = ctx;
196
197    for (i = 0; i < Elements(rs->array); i++) {
198       rs->array[i].Size = 4;
199       rs->array[i].Type = GL_FLOAT;
200       rs->array[i].Format = GL_RGBA;
201       rs->array[i].Stride = 0;
202       rs->array[i].StrideB = 0;
203       rs->array[i].Ptr = (GLubyte *) ctx->Current.Attrib[i];
204       rs->array[i].Enabled = GL_TRUE;
205       rs->array[i].Normalized = GL_TRUE;
206       rs->array[i].BufferObj = NULL;
207       rs->arrays[i] = &rs->array[i];
208    }
209
210    rs->prim.mode = GL_POINTS;
211    rs->prim.indexed = 0;
212    rs->prim.begin = 1;
213    rs->prim.end = 1;
214    rs->prim.weak = 0;
215    rs->prim.start = 0;
216    rs->prim.count = 1;
217
218    return rs;
219 }
220
221
222 static void
223 st_RasterPos(struct gl_context *ctx, const GLfloat v[4])
224 {
225    struct st_context *st = st_context(ctx);
226    struct draw_context *draw = st->draw;
227    struct rastpos_stage *rs;
228
229    if (st->rastpos_stage) {
230       /* get rastpos stage info */
231       rs = rastpos_stage(st->rastpos_stage);
232    }
233    else {
234       /* create rastpos draw stage */
235       rs = new_draw_rastpos_stage(ctx, draw);
236       st->rastpos_stage = &rs->stage;
237    }
238
239    /* plug our rastpos stage into the draw module */
240    draw_set_rasterize_stage(st->draw, st->rastpos_stage);
241
242    /* make sure everything's up to date */
243    st_validate_state(st);
244
245    /* This will get set only if rastpos_point(), above, gets called */
246    ctx->Current.RasterPosValid = GL_FALSE;
247
248    /* All vertex attribs but position were previously initialized above.
249     * Just plug in position pointer now.
250     */
251    rs->array[0].Ptr = (GLubyte *) v;
252
253    /* draw the point */
254    st_feedback_draw_vbo(ctx, rs->arrays, &rs->prim, 1, NULL, GL_TRUE, 0, 1);
255
256    /* restore draw's rasterization stage depending on rendermode */
257    if (ctx->RenderMode == GL_FEEDBACK) {
258       draw_set_rasterize_stage(draw, st->feedback_stage);
259    }
260    else if (ctx->RenderMode == GL_SELECT) {
261       draw_set_rasterize_stage(draw, st->selection_stage);
262    }
263 }
264
265
266
267 void st_init_rasterpos_functions(struct dd_function_table *functions)
268 {
269    functions->RasterPos = st_RasterPos;
270 }
271
272 #endif /* FEATURE_rastpos */