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