Merge branch 'upstream-gallium-0.1' into nouveau-gallium-0.1
[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 "pipe/p_util.h"
29 #include "pipe/p_shader_tokens.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_vertex.h"
32 #include "draw/draw_private.h"
33 #include "sp_context.h"
34 #include "sp_state.h"
35
36
37 /**
38 * Search vertex program's outputs to find a match for the given
39 * semantic name/index. Return the index of the output slot.
40 *
41 * Return 0 if not found. This will cause the fragment program to use
42 * vertex attrib 0 (position) in the cases where the fragment program
43 * attempts to use a missing vertex program output. This is an undefined
44 * condition that users shouldn't hit anyway.
45 */
46 static int
47 find_vs_output(const struct pipe_shader_state *vs,
48 uint semantic_name,
49 uint semantic_index)
50 {
51 uint i;
52 for (i = 0; i < vs->num_outputs; i++) {
53 if (vs->output_semantic_name[i] == semantic_name &&
54 vs->output_semantic_index[i] == semantic_index)
55 return i;
56 }
57 return 0;
58 }
59
60
61 /**
62 * Mark the current vertex layout as "invalid".
63 * We'll validate the vertex layout later, when we start to actually
64 * render a point or line or tri.
65 */
66 static void
67 invalidate_vertex_layout(struct softpipe_context *softpipe)
68 {
69 softpipe->vertex_info.num_attribs = 0;
70 }
71
72
73 /**
74 * The vertex info describes how to convert the post-transformed vertices
75 * (simple float[][4]) used by the 'draw' module into vertices for
76 * rasterization.
77 *
78 * This function validates the vertex layout and returns a pointer to a
79 * vertex_info object.
80 */
81 struct vertex_info *
82 softpipe_get_vertex_info(struct softpipe_context *softpipe)
83 {
84 struct vertex_info *vinfo = &softpipe->vertex_info;
85
86 if (vinfo->num_attribs == 0) {
87 /* compute vertex layout now */
88 const struct pipe_shader_state *vs = &softpipe->vs->shader;
89 const struct pipe_shader_state *fs = &softpipe->fs->shader;
90 const enum interp_mode colorInterp
91 = softpipe->rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
92 uint i;
93
94 if (softpipe->vbuf) {
95 /* if using the post-transform vertex buffer, tell draw_vbuf to
96 * simply emit the whole post-xform vertex as-is:
97 */
98 struct vertex_info *vinfo_vbuf = &softpipe->vertex_info_vbuf;
99 vinfo_vbuf->num_attribs = 0;
100 draw_emit_vertex_attr(vinfo_vbuf, EMIT_ALL, INTERP_NONE, 0);
101 vinfo_vbuf->size = 4 * vs->num_outputs
102 + sizeof(struct vertex_header) / 4;
103 }
104
105 /*
106 * Loop over fragment shader inputs, searching for the matching output
107 * from the vertex shader.
108 */
109 vinfo->num_attribs = 0;
110 for (i = 0; i < fs->num_inputs; i++) {
111 int src;
112 switch (fs->input_semantic_name[i]) {
113 case TGSI_SEMANTIC_POSITION:
114 src = find_vs_output(vs, 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 = find_vs_output(vs, TGSI_SEMANTIC_COLOR,
120 fs->input_semantic_index[i]);
121 draw_emit_vertex_attr(vinfo, EMIT_4F, colorInterp, src);
122 break;
123
124 case TGSI_SEMANTIC_FOG:
125 src = find_vs_output(vs, TGSI_SEMANTIC_FOG, 0);
126 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
127 break;
128
129 case TGSI_SEMANTIC_GENERIC:
130 /* this includes texcoords and varying vars */
131 src = find_vs_output(vs, TGSI_SEMANTIC_GENERIC,
132 fs->input_semantic_index[i]);
133 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, src);
134 break;
135
136 default:
137 assert(0);
138 }
139 }
140
141 softpipe->psize_slot = find_vs_output(vs, 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 unsigned surfWidth, surfHeight;
183
184 if (sp->framebuffer.num_cbufs > 0) {
185 surfWidth = sp->framebuffer.cbufs[0]->width;
186 surfHeight = sp->framebuffer.cbufs[0]->height;
187 }
188 else {
189 /* no surface? */
190 surfWidth = sp->scissor.maxx;
191 surfHeight = sp->scissor.maxy;
192 }
193
194 if (sp->rasterizer->scissor) {
195 /* clip to scissor rect */
196 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
197 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
198 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
199 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
200 }
201 else {
202 /* clip to surface bounds */
203 sp->cliprect.minx = 0;
204 sp->cliprect.miny = 0;
205 sp->cliprect.maxx = surfWidth;
206 sp->cliprect.maxy = surfHeight;
207 }
208 }
209
210
211 /* Hopefully this will remain quite simple, otherwise need to pull in
212 * something like the state tracker mechanism.
213 */
214 void softpipe_update_derived( struct softpipe_context *softpipe )
215 {
216 if (softpipe->dirty & (SP_NEW_RASTERIZER |
217 SP_NEW_FS |
218 SP_NEW_VS))
219 invalidate_vertex_layout( softpipe );
220
221 if (softpipe->dirty & (SP_NEW_SCISSOR |
222 SP_NEW_DEPTH_STENCIL_ALPHA |
223 SP_NEW_FRAMEBUFFER))
224 compute_cliprect(softpipe);
225
226 if (softpipe->dirty & (SP_NEW_BLEND |
227 SP_NEW_DEPTH_STENCIL_ALPHA |
228 SP_NEW_FRAMEBUFFER |
229 SP_NEW_RASTERIZER |
230 SP_NEW_FS |
231 SP_NEW_QUERY))
232 sp_build_quad_pipeline(softpipe);
233
234 softpipe->dirty = 0;
235 }