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