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