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