1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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.
39 #include "main/imports.h"
40 #include "main/macros.h"
41 #include "main/feedback.h"
42 #include "main/rastpos.h"
44 #include "st_context.h"
47 #include "st_program.h"
48 #include "st_cb_rasterpos.h"
49 #include "draw/draw_context.h"
50 #include "draw/draw_pipe.h"
55 * Our special drawing pipeline stage (replaces rasterization).
59 struct draw_stage stage
; /**< Base class */
60 struct gl_context
*ctx
; /**< Rendering context */
62 /* vertex attrib info we can setup once and re-use */
63 struct gl_vertex_buffer_binding binding
;
64 struct gl_array_attributes attrib
[VERT_ATTRIB_MAX
];
65 struct gl_vertex_array array
[VERT_ATTRIB_MAX
];
66 struct _mesa_prim prim
;
70 static inline struct rastpos_stage
*
71 rastpos_stage( struct draw_stage
*stage
)
73 return (struct rastpos_stage
*) stage
;
77 rastpos_flush( struct draw_stage
*stage
, unsigned flags
)
83 rastpos_reset_stipple_counter( struct draw_stage
*stage
)
89 rastpos_tri( struct draw_stage
*stage
, struct prim_header
*prim
)
91 /* should never get here */
96 rastpos_line( struct draw_stage
*stage
, struct prim_header
*prim
)
98 /* should never get here */
103 rastpos_destroy(struct draw_stage
*stage
)
110 * Update a raster pos attribute from the vertex result if it's present,
111 * else copy the current attrib.
114 update_attrib(struct gl_context
*ctx
, const ubyte
*outputMapping
,
115 const struct vertex_header
*vert
,
117 GLuint result
, GLuint defaultAttrib
)
120 const GLuint k
= outputMapping
[result
];
124 src
= ctx
->Current
.Attrib
[defaultAttrib
];
130 * Normally, this function would render a GL_POINT.
133 rastpos_point(struct draw_stage
*stage
, struct prim_header
*prim
)
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 ubyte
*outputMapping
= st
->vp
->result_to_output
;
143 /* if we get here, we didn't get clipped */
144 ctx
->Current
.RasterPosValid
= GL_TRUE
;
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 */
152 ctx
->Current
.RasterPos
[1] = pos
[1];
153 ctx
->Current
.RasterPos
[2] = pos
[2];
154 ctx
->Current
.RasterPos
[3] = pos
[3];
156 /* update other raster attribs */
157 update_attrib(ctx
, outputMapping
, prim
->v
[0],
158 ctx
->Current
.RasterColor
,
159 VARYING_SLOT_COL0
, VERT_ATTRIB_COLOR0
);
161 update_attrib(ctx
, outputMapping
, prim
->v
[0],
162 ctx
->Current
.RasterSecondaryColor
,
163 VARYING_SLOT_COL1
, VERT_ATTRIB_COLOR1
);
165 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
166 update_attrib(ctx
, outputMapping
, prim
->v
[0],
167 ctx
->Current
.RasterTexCoords
[i
],
168 VARYING_SLOT_TEX0
+ i
, VERT_ATTRIB_TEX0
+ i
);
171 if (ctx
->RenderMode
== GL_SELECT
) {
172 _mesa_update_hitflag( ctx
, ctx
->Current
.RasterPos
[2] );
178 * Create rasterpos "drawing" stage.
180 static struct rastpos_stage
*
181 new_draw_rastpos_stage(struct gl_context
*ctx
, struct draw_context
*draw
)
183 struct rastpos_stage
*rs
= ST_CALLOC_STRUCT(rastpos_stage
);
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
;
197 rs
->binding
.Stride
= 0;
198 rs
->binding
.BufferObj
= NULL
;
199 for (i
= 0; i
< ARRAY_SIZE(rs
->array
); i
++) {
200 rs
->attrib
[i
].Size
= 4;
201 rs
->attrib
[i
].Type
= GL_FLOAT
;
202 rs
->attrib
[i
].Format
= GL_RGBA
;
203 rs
->attrib
[i
].Ptr
= (GLubyte
*) ctx
->Current
.Attrib
[i
];
204 rs
->attrib
[i
].Normalized
= GL_TRUE
;
205 rs
->array
[i
].BufferBinding
= &rs
->binding
;
206 rs
->array
[i
].VertexAttrib
= &rs
->attrib
[i
];
209 rs
->prim
.mode
= GL_POINTS
;
210 rs
->prim
.indexed
= 0;
222 st_RasterPos(struct gl_context
*ctx
, const GLfloat v
[4])
224 struct st_context
*st
= st_context(ctx
);
225 struct draw_context
*draw
= st_get_draw_context(st
);
226 struct rastpos_stage
*rs
;
227 const struct gl_vertex_array
*saved_arrays
= ctx
->Array
._DrawArrays
;
232 if (ctx
->VertexProgram
._Current
== NULL
||
233 ctx
->VertexProgram
._Current
== ctx
->VertexProgram
._TnlProgram
) {
234 /* No vertex shader/program is enabled, used the simple/fast fixed-
235 * function implementation of RasterPos.
237 _mesa_RasterPos(ctx
, v
);
241 if (st
->rastpos_stage
) {
242 /* get rastpos stage info */
243 rs
= rastpos_stage(st
->rastpos_stage
);
246 /* create rastpos draw stage */
247 rs
= new_draw_rastpos_stage(ctx
, draw
);
248 st
->rastpos_stage
= &rs
->stage
;
251 /* plug our rastpos stage into the draw module */
252 draw_set_rasterize_stage(st
->draw
, st
->rastpos_stage
);
254 /* make sure everything's up to date */
255 st_validate_state(st
, ST_PIPELINE_RENDER
);
257 /* This will get set only if rastpos_point(), above, gets called */
258 ctx
->Current
.RasterPosValid
= GL_FALSE
;
260 /* All vertex attribs but position were previously initialized above.
261 * Just plug in position pointer now.
263 rs
->attrib
[0].Ptr
= (GLubyte
*) v
;
267 * Don't set DriverFlags.NewArray.
268 * st_feedback_draw_vbo doesn't check for that flag. */
269 ctx
->Array
._DrawArrays
= rs
->array
;
270 st_feedback_draw_vbo(ctx
, &rs
->prim
, 1, NULL
, GL_TRUE
, 0, 1,
272 ctx
->Array
._DrawArrays
= saved_arrays
;
274 /* restore draw's rasterization stage depending on rendermode */
275 if (ctx
->RenderMode
== GL_FEEDBACK
) {
276 draw_set_rasterize_stage(draw
, st
->feedback_stage
);
278 else if (ctx
->RenderMode
== GL_SELECT
) {
279 draw_set_rasterize_stage(draw
, st
->selection_stage
);
285 void st_init_rasterpos_functions(struct dd_function_table
*functions
)
287 functions
->RasterPos
= st_RasterPos
;