Merge branch 'mesa_7_6_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 enum interp_mode interp;
95
96 switch (spfs->info.input_interpolate[i]) {
97 case TGSI_INTERPOLATE_CONSTANT:
98 interp = INTERP_CONSTANT;
99 break;
100 case TGSI_INTERPOLATE_LINEAR:
101 interp = INTERP_LINEAR;
102 break;
103 case TGSI_INTERPOLATE_PERSPECTIVE:
104 interp = INTERP_PERSPECTIVE;
105 break;
106 default:
107 assert(0);
108 interp = INTERP_LINEAR;
109 }
110
111 switch (spfs->info.input_semantic_name[i]) {
112 case TGSI_SEMANTIC_POSITION:
113 src = draw_find_vs_output(softpipe->draw,
114 TGSI_SEMANTIC_POSITION, 0);
115 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_POS, src);
116 break;
117
118 case TGSI_SEMANTIC_COLOR:
119 src = draw_find_vs_output(softpipe->draw, TGSI_SEMANTIC_COLOR,
120 spfs->info.input_semantic_index[i]);
121 draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
122 break;
123
124 case TGSI_SEMANTIC_FOG:
125 src = draw_find_vs_output(softpipe->draw, TGSI_SEMANTIC_FOG, 0);
126 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
127 break;
128
129 case TGSI_SEMANTIC_GENERIC:
130 case TGSI_SEMANTIC_FACE:
131 /* this includes texcoords and varying vars */
132 src = draw_find_vs_output(softpipe->draw, TGSI_SEMANTIC_GENERIC,
133 spfs->info.input_semantic_index[i]);
134 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
135 break;
136
137 default:
138 assert(0);
139 }
140 }
141
142 softpipe->psize_slot = draw_find_vs_output(softpipe->draw,
143 TGSI_SEMANTIC_PSIZE, 0);
144 if (softpipe->psize_slot > 0) {
145 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT,
146 softpipe->psize_slot);
147 }
148
149 draw_compute_vertex_size(vinfo);
150 }
151
152 return vinfo;
153 }
154
155
156 /**
157 * Called from vbuf module.
158 *
159 * Note that there's actually two different vertex layouts in softpipe.
160 *
161 * The normal one is computed in softpipe_get_vertex_info() above and is
162 * used by the point/line/tri "setup" code.
163 *
164 * The other one (this one) is only used by the vbuf module (which is
165 * not normally used by default but used in testing). For the vbuf module,
166 * we basically want to pass-through the draw module's vertex layout as-is.
167 * When the softpipe vbuf code begins drawing, the normal vertex layout
168 * will come into play again.
169 */
170 struct vertex_info *
171 softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
172 {
173 (void) softpipe_get_vertex_info(softpipe);
174 return &softpipe->vertex_info_vbuf;
175 }
176
177
178 /**
179 * Recompute cliprect from scissor bounds, scissor enable and surface size.
180 */
181 static void
182 compute_cliprect(struct softpipe_context *sp)
183 {
184 uint surfWidth = sp->framebuffer.width;
185 uint surfHeight = sp->framebuffer.height;
186
187 if (sp->rasterizer->scissor) {
188 /* clip to scissor rect */
189 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
190 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
191 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
192 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
193 }
194 else {
195 /* clip to surface bounds */
196 sp->cliprect.minx = 0;
197 sp->cliprect.miny = 0;
198 sp->cliprect.maxx = surfWidth;
199 sp->cliprect.maxy = surfHeight;
200 }
201 }
202
203
204 /* Hopefully this will remain quite simple, otherwise need to pull in
205 * something like the state tracker mechanism.
206 */
207 void softpipe_update_derived( struct softpipe_context *softpipe )
208 {
209 if (softpipe->dirty & (SP_NEW_RASTERIZER |
210 SP_NEW_FS |
211 SP_NEW_VS))
212 invalidate_vertex_layout( softpipe );
213
214 if (softpipe->dirty & (SP_NEW_SCISSOR |
215 SP_NEW_DEPTH_STENCIL_ALPHA |
216 SP_NEW_FRAMEBUFFER))
217 compute_cliprect(softpipe);
218
219 if (softpipe->dirty & (SP_NEW_BLEND |
220 SP_NEW_DEPTH_STENCIL_ALPHA |
221 SP_NEW_FRAMEBUFFER |
222 SP_NEW_RASTERIZER |
223 SP_NEW_FS |
224 SP_NEW_QUERY))
225 sp_build_quad_pipeline(softpipe);
226
227 softpipe->dirty = 0;
228 }