Hide texture layout details from the state tracker.
[mesa.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
42 #include "st_context.h"
43 #include "st_atom.h"
44 #include "st_draw.h"
45 #include "st_cb_rasterpos.h"
46 #include "st_draw.h"
47 #include "pipe/draw/draw_context.h"
48 #include "pipe/draw/draw_private.h"
49 #include "shader/prog_instruction.h"
50 #include "vbo/vbo.h"
51
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 GLcontext *ctx; /**< Rendering context */
61
62 /* vertex attrib info we can setup once and re-use */
63 struct gl_client_array array[VERT_ATTRIB_MAX];
64 const struct gl_client_array *arrays[VERT_ATTRIB_MAX];
65 struct _mesa_prim prim;
66 };
67
68
69 static INLINE struct rastpos_stage *
70 rastpos_stage( struct draw_stage *stage )
71 {
72 return (struct rastpos_stage *) stage;
73 }
74
75 static void
76 rastpos_begin( struct draw_stage *stage )
77 {
78 /* no-op */
79 }
80
81 static void
82 rastpos_end( struct draw_stage *stage )
83 {
84 /* no-op */
85 }
86
87 static void
88 rastpos_reset_stipple_counter( struct draw_stage *stage )
89 {
90 /* no-op */
91 }
92
93 static void
94 rastpos_tri( struct draw_stage *stage, struct prim_header *prim )
95 {
96 /* should never get here */
97 assert(0);
98 }
99
100 static void
101 rastpos_line( struct draw_stage *stage, struct prim_header *prim )
102 {
103 /* should never get here */
104 assert(0);
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
113 update_attrib(GLcontext *ctx, const GLuint *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 != ~0)
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
132 rastpos_point(struct draw_stage *stage, struct prim_header *prim)
133 {
134 struct rastpos_stage *rs = rastpos_stage(stage);
135 GLcontext *ctx = rs->ctx;
136 struct st_context *st = ctx->st;
137 const GLfloat height = ctx->DrawBuffer->Height;
138 const GLuint *outputMapping = st->vertex_result_to_slot;
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 ctx->Current.RasterPos[1] = height - 1 - pos[1];
149 ctx->Current.RasterPos[2] = pos[2];
150 ctx->Current.RasterPos[3] = pos[3];
151
152 /* update other raster attribs */
153 update_attrib(ctx, outputMapping, prim->v[0],
154 ctx->Current.RasterColor,
155 VERT_RESULT_COL0, VERT_ATTRIB_COLOR0);
156
157 update_attrib(ctx, outputMapping, prim->v[0],
158 ctx->Current.RasterSecondaryColor,
159 VERT_RESULT_COL1, VERT_ATTRIB_COLOR1);
160
161 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
162 update_attrib(ctx, outputMapping, prim->v[0],
163 ctx->Current.RasterTexCoords[i],
164 VERT_RESULT_TEX0 + i, VERT_ATTRIB_TEX0 + i);
165 }
166 }
167
168
169 /**
170 * Create rasterpos "drawing" stage.
171 */
172 static struct rastpos_stage *
173 new_draw_rastpos_stage(GLcontext *ctx, struct draw_context *draw)
174 {
175 struct rastpos_stage *rs = CALLOC_STRUCT(rastpos_stage);
176
177 rs->stage.draw = draw;
178 rs->stage.next = NULL;
179 rs->stage.begin = rastpos_begin;
180 rs->stage.point = rastpos_point;
181 rs->stage.line = rastpos_line;
182 rs->stage.tri = rastpos_tri;
183 rs->stage.end = rastpos_end;
184 rs->stage.reset_stipple_counter = rastpos_reset_stipple_counter;
185 rs->ctx = ctx;
186
187 return rs;
188 }
189
190
191 static void
192 st_RasterPos(GLcontext *ctx, const GLfloat v[4])
193 {
194 struct st_context *st = ctx->st;
195 struct draw_context *draw = st->draw;
196 struct rastpos_stage *rs;
197
198 if (st->rastpos_stage) {
199 /* get rastpos stage info */
200 rs = rastpos_stage(st->rastpos_stage);
201 }
202 else {
203 /* create rastpos draw stage */
204 GLuint i;
205
206 rs = new_draw_rastpos_stage(ctx, draw);
207 st->rastpos_stage = &rs->stage;
208
209 /* one-time init */
210 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
211 rs->array[i].Size = 4;
212 rs->array[i].Type = GL_FLOAT;
213 rs->array[i].Stride = 0;
214 rs->array[i].StrideB = 0;
215 rs->array[i].Ptr = (GLubyte *) ctx->Current.Attrib[i];
216 rs->array[i].Enabled = GL_TRUE;
217 rs->array[i].Normalized = GL_TRUE;
218 rs->array[i].BufferObj = NULL;
219 rs->arrays[i] = &rs->array[i];
220 }
221
222 rs->prim.mode = GL_POINTS;
223 rs->prim.indexed = 0;
224 rs->prim.begin = 1;
225 rs->prim.end = 1;
226 rs->prim.weak = 0;
227 rs->prim.start = 0;
228 rs->prim.count = 1;
229 }
230
231 /* plug our rastpos stage into the draw module */
232 draw_set_rasterize_stage(st->draw, st->rastpos_stage);
233
234 /* make sure everything's up to date */
235 st_validate_state(ctx->st);
236
237 /* This will get set only if rastpos_point(), above, gets called */
238 ctx->Current.RasterPosValid = GL_FALSE;
239
240 /* All vertex attribs but position were previously initialized above.
241 * Just plug in position pointer now.
242 */
243 rs->array[0].Ptr = (GLubyte *) v;
244
245 /* draw the point */
246 st_feedback_draw_vbo(ctx, rs->arrays, &rs->prim, 1, NULL, 0, 1);
247 }
248
249
250
251 void st_init_rasterpos_functions(struct dd_function_table *functions)
252 {
253 functions->RasterPos = st_RasterPos;
254 }