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