fix some semantic info mix-ups in calculate_vertex_layout()
[mesa.git] / src / mesa / pipe / 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 "pipe/draw/draw_context.h"
31 #include "pipe/draw/draw_vertex.h"
32 #include "sp_context.h"
33 #include "sp_state.h"
34
35
36 /**
37 * Determine which post-transform / pre-rasterization vertex attributes
38 * we need.
39 * Derived from: fs, setup states.
40 */
41 static void calculate_vertex_layout( struct softpipe_context *softpipe )
42 {
43 const struct pipe_shader_state *vs = softpipe->vs->state;
44 const struct pipe_shader_state *fs = &softpipe->fs->shader;
45 const enum interp_mode colorInterp
46 = softpipe->rasterizer->flatshade ? INTERP_CONSTANT : INTERP_LINEAR;
47 struct vertex_info *vinfo = &softpipe->vertex_info;
48 boolean emitBack0 = FALSE, emitBack1 = FALSE, emitPsize = FALSE;
49 uint front0 = 0, back0 = 0, front1 = 0, back1 = 0;
50 uint i;
51
52 memset(vinfo, 0, sizeof(*vinfo));
53
54
55 if (fs->input_semantic_name[0] == TGSI_SEMANTIC_POSITION) {
56 /* Need Z if depth test is enabled or the fragment program uses the
57 * fragment position (XYZW).
58 */
59 }
60
61 softpipe->psize_slot = -1;
62
63 /* always emit vertex pos */
64 draw_emit_vertex_attr(vinfo, FORMAT_4F, INTERP_LINEAR);
65
66 /*
67 * XXX I think we need to reconcile the vertex shader outputs with
68 * the fragment shader inputs here to make sure the slots line up.
69 * Might just be getting lucky so far.
70 * Or maybe do that in the state tracker?
71 */
72
73 for (i = 0; i < vs->num_outputs; i++) {
74 switch (vs->output_semantic_name[i]) {
75
76 case TGSI_SEMANTIC_POSITION:
77 /* vertex programs always emit position, but might not be
78 * needed for fragment progs.
79 */
80 /* no-op */
81 break;
82
83 case TGSI_SEMANTIC_COLOR:
84 if (vs->output_semantic_index[i] == 0) {
85 front0 = draw_emit_vertex_attr(vinfo, FORMAT_4F, colorInterp);
86 }
87 else {
88 assert(vs->output_semantic_index[i] == 1);
89 front1 = draw_emit_vertex_attr(vinfo, FORMAT_4F, colorInterp);
90 }
91 break;
92
93 case TGSI_SEMANTIC_BCOLOR:
94 if (vs->output_semantic_index[i] == 0) {
95 emitBack0 = TRUE;
96 }
97 else {
98 assert(vs->output_semantic_index[i] == 1);
99 emitBack1 = TRUE;
100 }
101 break;
102
103 case TGSI_SEMANTIC_FOG:
104 draw_emit_vertex_attr(vinfo, FORMAT_1F, INTERP_PERSPECTIVE);
105 break;
106
107 case TGSI_SEMANTIC_PSIZE:
108 /* XXX only emit if drawing points or front/back polygon mode
109 * is point mode
110 */
111 emitPsize = TRUE;
112 break;
113
114 case TGSI_SEMANTIC_GENERIC:
115 /* this includes texcoords and varying vars */
116 draw_emit_vertex_attr(vinfo, FORMAT_4F, INTERP_PERSPECTIVE);
117 break;
118
119 default:
120 assert(0);
121 }
122 }
123
124 softpipe->nr_frag_attrs = fs->num_inputs;
125
126 /* We want these after all other attribs since they won't get passed
127 * to the fragment shader. All prior vertex output attribs should match
128 * up 1:1 with the fragment shader inputs.
129 */
130 if (emitBack0) {
131 back0 = draw_emit_vertex_attr(vinfo, FORMAT_4F, colorInterp);
132 }
133 if (emitBack1) {
134 back1 = draw_emit_vertex_attr(vinfo, FORMAT_4F, colorInterp);
135 }
136 if (emitPsize) {
137 softpipe->psize_slot
138 = draw_emit_vertex_attr(vinfo, FORMAT_1F, INTERP_CONSTANT);
139 }
140
141 /* If the attributes have changed, tell the draw module about
142 * the new vertex layout.
143 */
144 /* XXX we also need to do this when the shading mode (interp modes) change: */
145 if (1/*vinfo->attr_mask != softpipe->attr_mask*/) {
146 /*softpipe->attr_mask = vinfo->attr_mask;*/
147
148 draw_set_vertex_info( softpipe->draw, vinfo);
149
150 draw_set_twoside_attributes(softpipe->draw,
151 front0, back0, front1, back1);
152 }
153 }
154
155
156 /**
157 * Recompute cliprect from scissor bounds, scissor enable and surface size.
158 */
159 static void
160 compute_cliprect(struct softpipe_context *sp)
161 {
162 unsigned surfWidth, surfHeight;
163
164 if (sp->framebuffer.num_cbufs > 0) {
165 surfWidth = sp->framebuffer.cbufs[0]->width;
166 surfHeight = sp->framebuffer.cbufs[0]->height;
167 }
168 else {
169 /* no surface? */
170 surfWidth = sp->scissor.maxx;
171 surfHeight = sp->scissor.maxy;
172 }
173
174 if (sp->rasterizer->scissor) {
175 /* clip to scissor rect */
176 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
177 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
178 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
179 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
180 }
181 else {
182 /* clip to surface bounds */
183 sp->cliprect.minx = 0;
184 sp->cliprect.miny = 0;
185 sp->cliprect.maxx = surfWidth;
186 sp->cliprect.maxy = surfHeight;
187 }
188 }
189
190
191 /* Hopefully this will remain quite simple, otherwise need to pull in
192 * something like the state tracker mechanism.
193 */
194 void softpipe_update_derived( struct softpipe_context *softpipe )
195 {
196 if (softpipe->dirty & (SP_NEW_RASTERIZER | SP_NEW_FS))
197 calculate_vertex_layout( softpipe );
198
199 if (softpipe->dirty & (SP_NEW_SCISSOR |
200 SP_NEW_DEPTH_STENCIL_ALPHA |
201 SP_NEW_FRAMEBUFFER))
202 compute_cliprect(softpipe);
203
204 if (softpipe->dirty & (SP_NEW_BLEND |
205 SP_NEW_DEPTH_STENCIL_ALPHA |
206 SP_NEW_FRAMEBUFFER |
207 SP_NEW_RASTERIZER |
208 SP_NEW_FS |
209 SP_NEW_QUERY))
210 sp_build_quad_pipeline(softpipe);
211
212 softpipe->dirty = 0;
213 }