Merge branch 'mesa_7_6_branch' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / gallium / drivers / softpipe / sp_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "draw/draw_context.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_private.h"
34 #include "sp_context.h"
35 #include "sp_screen.h"
36 #include "sp_state.h"
37 #include "sp_texture.h"
38 #include "sp_tex_tile_cache.h"
39
40
41 /**
42 * Mark the current vertex layout as "invalid".
43 * We'll validate the vertex layout later, when we start to actually
44 * render a point or line or tri.
45 */
46 static void
47 invalidate_vertex_layout(struct softpipe_context *softpipe)
48 {
49 softpipe->vertex_info.num_attribs = 0;
50 }
51
52
53 /**
54 * The vertex info describes how to convert the post-transformed vertices
55 * (simple float[][4]) used by the 'draw' module into vertices for
56 * rasterization.
57 *
58 * This function validates the vertex layout and returns a pointer to a
59 * vertex_info object.
60 */
61 struct vertex_info *
62 softpipe_get_vertex_info(struct softpipe_context *softpipe)
63 {
64 struct vertex_info *vinfo = &softpipe->vertex_info;
65
66 if (vinfo->num_attribs == 0) {
67 /* compute vertex layout now */
68 const struct sp_fragment_shader *spfs = softpipe->fs;
69 const enum interp_mode colorInterp
70 = softpipe->rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
71 struct vertex_info *vinfo_vbuf = &softpipe->vertex_info_vbuf;
72 const uint num = draw_num_vs_outputs(softpipe->draw);
73 uint i;
74
75 /* Tell draw_vbuf to simply emit the whole post-xform vertex
76 * as-is. No longer any need to try and emit draw vertex_header
77 * info.
78 */
79 vinfo_vbuf->num_attribs = 0;
80 for (i = 0; i < num; i++) {
81 draw_emit_vertex_attr(vinfo_vbuf, EMIT_4F, INTERP_PERSPECTIVE, i);
82 }
83 draw_compute_vertex_size(vinfo_vbuf);
84
85 /*
86 * Loop over fragment shader inputs, searching for the matching output
87 * from the vertex shader.
88 */
89 vinfo->num_attribs = 0;
90 for (i = 0; i < spfs->info.num_inputs; i++) {
91 int src;
92 enum interp_mode interp;
93
94 switch (spfs->info.input_interpolate[i]) {
95 case TGSI_INTERPOLATE_CONSTANT:
96 interp = INTERP_CONSTANT;
97 break;
98 case TGSI_INTERPOLATE_LINEAR:
99 interp = INTERP_LINEAR;
100 break;
101 case TGSI_INTERPOLATE_PERSPECTIVE:
102 interp = INTERP_PERSPECTIVE;
103 break;
104 default:
105 assert(0);
106 interp = INTERP_LINEAR;
107 }
108
109 switch (spfs->info.input_semantic_name[i]) {
110 case TGSI_SEMANTIC_POSITION:
111 src = draw_find_vs_output(softpipe->draw,
112 TGSI_SEMANTIC_POSITION, 0);
113 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_POS, src);
114 break;
115
116 case TGSI_SEMANTIC_COLOR:
117 src = draw_find_vs_output(softpipe->draw, TGSI_SEMANTIC_COLOR,
118 spfs->info.input_semantic_index[i]);
119 draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
120 break;
121
122 case TGSI_SEMANTIC_FOG:
123 src = draw_find_vs_output(softpipe->draw, TGSI_SEMANTIC_FOG, 0);
124 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
125 break;
126
127 case TGSI_SEMANTIC_GENERIC:
128 case TGSI_SEMANTIC_FACE:
129 /* this includes texcoords and varying vars */
130 src = draw_find_vs_output(softpipe->draw, TGSI_SEMANTIC_GENERIC,
131 spfs->info.input_semantic_index[i]);
132 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
133 break;
134
135 default:
136 assert(0);
137 }
138 }
139
140 softpipe->psize_slot = draw_find_vs_output(softpipe->draw,
141 TGSI_SEMANTIC_PSIZE, 0);
142 if (softpipe->psize_slot > 0) {
143 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT,
144 softpipe->psize_slot);
145 }
146
147 draw_compute_vertex_size(vinfo);
148 }
149
150 return vinfo;
151 }
152
153
154 /**
155 * Called from vbuf module.
156 *
157 * Note that there's actually two different vertex layouts in softpipe.
158 *
159 * The normal one is computed in softpipe_get_vertex_info() above and is
160 * used by the point/line/tri "setup" code.
161 *
162 * The other one (this one) is only used by the vbuf module (which is
163 * not normally used by default but used in testing). For the vbuf module,
164 * we basically want to pass-through the draw module's vertex layout as-is.
165 * When the softpipe vbuf code begins drawing, the normal vertex layout
166 * will come into play again.
167 */
168 struct vertex_info *
169 softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
170 {
171 (void) softpipe_get_vertex_info(softpipe);
172 return &softpipe->vertex_info_vbuf;
173 }
174
175
176 /**
177 * Recompute cliprect from scissor bounds, scissor enable and surface size.
178 */
179 static void
180 compute_cliprect(struct softpipe_context *sp)
181 {
182 /* SP_NEW_FRAMEBUFFER
183 */
184 uint surfWidth = sp->framebuffer.width;
185 uint surfHeight = sp->framebuffer.height;
186
187 /* SP_NEW_RASTERIZER
188 */
189 if (sp->rasterizer->scissor) {
190
191 /* SP_NEW_SCISSOR
192 *
193 * clip to scissor rect:
194 */
195 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
196 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
197 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
198 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
199 }
200 else {
201 /* clip to surface bounds */
202 sp->cliprect.minx = 0;
203 sp->cliprect.miny = 0;
204 sp->cliprect.maxx = surfWidth;
205 sp->cliprect.maxy = surfHeight;
206 }
207 }
208
209
210 static void
211 update_tgsi_samplers( struct softpipe_context *softpipe )
212 {
213 unsigned i;
214
215 softpipe_reset_sampler_varients( softpipe );
216
217 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
218 struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[i];
219 if (tc->texture) {
220 struct softpipe_texture *spt = softpipe_texture(tc->texture);
221 if (spt->timestamp != tc->timestamp) {
222 sp_tex_tile_cache_validate_texture( tc );
223 /*
224 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
225 */
226 tc->timestamp = spt->timestamp;
227 }
228 }
229 }
230 }
231
232
233 /* Hopefully this will remain quite simple, otherwise need to pull in
234 * something like the state tracker mechanism.
235 */
236 void softpipe_update_derived( struct softpipe_context *softpipe )
237 {
238 struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
239
240 /* Check for updated textures.
241 */
242 if (softpipe->tex_timestamp != sp_screen->timestamp) {
243 softpipe->tex_timestamp = sp_screen->timestamp;
244 softpipe->dirty |= SP_NEW_TEXTURE;
245 }
246
247 if (softpipe->dirty & (SP_NEW_SAMPLER |
248 SP_NEW_TEXTURE |
249 SP_NEW_FS |
250 SP_NEW_VS))
251 update_tgsi_samplers( softpipe );
252
253 if (softpipe->dirty & (SP_NEW_RASTERIZER |
254 SP_NEW_FS |
255 SP_NEW_VS))
256 invalidate_vertex_layout( softpipe );
257
258 if (softpipe->dirty & (SP_NEW_SCISSOR |
259 SP_NEW_RASTERIZER |
260 SP_NEW_FRAMEBUFFER))
261 compute_cliprect(softpipe);
262
263 if (softpipe->dirty & (SP_NEW_BLEND |
264 SP_NEW_DEPTH_STENCIL_ALPHA |
265 SP_NEW_FRAMEBUFFER |
266 SP_NEW_FS))
267 sp_build_quad_pipeline(softpipe);
268
269 softpipe->dirty = 0;
270 }