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/arrayobj.h"
42 #include "main/feedback.h"
43 #include "main/rastpos.h"
44 #include "main/state.h"
45 #include "main/varray.h"
47 #include "st_context.h"
50 #include "st_program.h"
51 #include "st_cb_rasterpos.h"
52 #include "draw/draw_context.h"
53 #include "draw/draw_pipe.h"
58 * Our special drawing pipeline stage (replaces rasterization).
62 struct draw_stage stage
; /**< Base class */
63 struct gl_context
*ctx
; /**< Rendering context */
65 /* vertex attrib info we can setup once and re-use */
66 struct gl_vertex_array_object
*VAO
;
67 struct _mesa_prim prim
;
71 static inline struct rastpos_stage
*
72 rastpos_stage( struct draw_stage
*stage
)
74 return (struct rastpos_stage
*) stage
;
78 rastpos_flush( struct draw_stage
*stage
, unsigned flags
)
84 rastpos_reset_stipple_counter( struct draw_stage
*stage
)
90 rastpos_tri( struct draw_stage
*stage
, struct prim_header
*prim
)
92 /* should never get here */
97 rastpos_line( struct draw_stage
*stage
, struct prim_header
*prim
)
99 /* should never get here */
104 rastpos_destroy(struct draw_stage
*stage
)
106 struct rastpos_stage
*rstage
= (struct rastpos_stage
*)stage
;
107 _mesa_reference_vao(rstage
->ctx
, &rstage
->VAO
, NULL
);
113 * Update a raster pos attribute from the vertex result if it's present,
114 * else copy the current attrib.
117 update_attrib(struct gl_context
*ctx
, const ubyte
*outputMapping
,
118 const struct vertex_header
*vert
,
120 GLuint result
, GLuint defaultAttrib
)
123 const GLuint k
= outputMapping
[result
];
127 src
= ctx
->Current
.Attrib
[defaultAttrib
];
133 * Normally, this function would render a GL_POINT.
136 rastpos_point(struct draw_stage
*stage
, struct prim_header
*prim
)
138 struct rastpos_stage
*rs
= rastpos_stage(stage
);
139 struct gl_context
*ctx
= rs
->ctx
;
140 struct st_context
*st
= st_context(ctx
);
141 const GLfloat height
= (GLfloat
) ctx
->DrawBuffer
->Height
;
142 const ubyte
*outputMapping
= st
->vp
->result_to_output
;
146 /* if we get here, we didn't get clipped */
147 ctx
->Current
.RasterPosValid
= GL_TRUE
;
149 /* update raster pos */
150 pos
= prim
->v
[0]->data
[0];
151 ctx
->Current
.RasterPos
[0] = pos
[0];
152 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
)
153 ctx
->Current
.RasterPos
[1] = height
- pos
[1]; /* invert Y */
155 ctx
->Current
.RasterPos
[1] = pos
[1];
156 ctx
->Current
.RasterPos
[2] = pos
[2];
157 ctx
->Current
.RasterPos
[3] = pos
[3];
159 /* update other raster attribs */
160 update_attrib(ctx
, outputMapping
, prim
->v
[0],
161 ctx
->Current
.RasterColor
,
162 VARYING_SLOT_COL0
, VERT_ATTRIB_COLOR0
);
164 update_attrib(ctx
, outputMapping
, prim
->v
[0],
165 ctx
->Current
.RasterSecondaryColor
,
166 VARYING_SLOT_COL1
, VERT_ATTRIB_COLOR1
);
168 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
169 update_attrib(ctx
, outputMapping
, prim
->v
[0],
170 ctx
->Current
.RasterTexCoords
[i
],
171 VARYING_SLOT_TEX0
+ i
, VERT_ATTRIB_TEX0
+ i
);
174 if (ctx
->RenderMode
== GL_SELECT
) {
175 _mesa_update_hitflag( ctx
, ctx
->Current
.RasterPos
[2] );
181 * Create rasterpos "drawing" stage.
183 static struct rastpos_stage
*
184 new_draw_rastpos_stage(struct gl_context
*ctx
, struct draw_context
*draw
)
186 struct rastpos_stage
*rs
= ST_CALLOC_STRUCT(rastpos_stage
);
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
;
199 rs
->VAO
= _mesa_new_vao(ctx
, ~((GLuint
)0));
200 _mesa_vertex_attrib_binding(ctx
, rs
->VAO
, VERT_ATTRIB_POS
, 0);
201 _mesa_update_array_format(ctx
, rs
->VAO
, VERT_ATTRIB_POS
, 4, GL_FLOAT
,
202 GL_RGBA
, GL_FALSE
, GL_FALSE
, GL_FALSE
, 0);
203 _mesa_enable_vertex_array_attrib(ctx
, rs
->VAO
, 0);
205 rs
->prim
.mode
= GL_POINTS
;
206 rs
->prim
.indexed
= 0;
218 st_RasterPos(struct gl_context
*ctx
, const GLfloat v
[4])
220 struct st_context
*st
= st_context(ctx
);
221 struct draw_context
*draw
= st_get_draw_context(st
);
222 struct rastpos_stage
*rs
;
227 if (ctx
->VertexProgram
._Current
== NULL
||
228 ctx
->VertexProgram
._Current
== ctx
->VertexProgram
._TnlProgram
) {
229 /* No vertex shader/program is enabled, used the simple/fast fixed-
230 * function implementation of RasterPos.
232 _mesa_RasterPos(ctx
, v
);
236 if (st
->rastpos_stage
) {
237 /* get rastpos stage info */
238 rs
= rastpos_stage(st
->rastpos_stage
);
241 /* create rastpos draw stage */
242 rs
= new_draw_rastpos_stage(ctx
, draw
);
243 st
->rastpos_stage
= &rs
->stage
;
246 /* plug our rastpos stage into the draw module */
247 draw_set_rasterize_stage(st
->draw
, st
->rastpos_stage
);
249 /* make sure everything's up to date */
250 st_validate_state(st
, ST_PIPELINE_RENDER
);
252 /* This will get set only if rastpos_point(), above, gets called */
253 ctx
->Current
.RasterPosValid
= GL_FALSE
;
255 /* All vertex attribs but position were previously initialized above.
256 * Just plug in position pointer now.
258 rs
->VAO
->VertexAttrib
[VERT_ATTRIB_POS
].Ptr
= (GLubyte
*) v
;
259 rs
->VAO
->NewArrays
|= VERT_BIT_POS
;
260 _mesa_set_draw_vao(ctx
, rs
->VAO
, VERT_BIT_POS
);
262 /* Draw the point. */
263 st_feedback_draw_vbo(ctx
, &rs
->prim
, 1, NULL
, GL_TRUE
, 0, 1,
266 /* restore draw's rasterization stage depending on rendermode */
267 if (ctx
->RenderMode
== GL_FEEDBACK
) {
268 draw_set_rasterize_stage(draw
, st
->feedback_stage
);
270 else if (ctx
->RenderMode
== GL_SELECT
) {
271 draw_set_rasterize_stage(draw
, st
->selection_stage
);
277 void st_init_rasterpos_functions(struct dd_function_table
*functions
)
279 functions
->RasterPos
= st_RasterPos
;