Merge branch 'i915tex_privbuffers' into softpipe_0_1_branch
[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 "main/glheader.h"
29 #include "main/macros.h"
30 #include "main/enums.h"
31
32 #include "vf/vf.h"
33 #include "pipe/draw/draw_context.h"
34 #include "sp_context.h"
35 #include "sp_state.h"
36
37
38 #define EMIT_ATTR( VF_ATTR, FRAG_ATTR, INTERP ) \
39 do { \
40 slot_to_vf_attr[softpipe->nr_attrs] = VF_ATTR; \
41 softpipe->vf_attr_to_slot[VF_ATTR] = softpipe->nr_attrs; \
42 softpipe->fp_attr_to_slot[FRAG_ATTR] = softpipe->nr_attrs; \
43 softpipe->interp[softpipe->nr_attrs] = INTERP; \
44 softpipe->nr_attrs++; \
45 attr_mask |= (1 << (VF_ATTR)); \
46 } while (0)
47
48
49 static const GLuint frag_to_vf[FRAG_ATTRIB_MAX] =
50 {
51 VF_ATTRIB_POS,
52 VF_ATTRIB_COLOR0,
53 VF_ATTRIB_COLOR1,
54 VF_ATTRIB_FOG,
55 VF_ATTRIB_TEX0,
56 VF_ATTRIB_TEX1,
57 VF_ATTRIB_TEX2,
58 VF_ATTRIB_TEX3,
59 VF_ATTRIB_TEX4,
60 VF_ATTRIB_TEX5,
61 VF_ATTRIB_TEX6,
62 VF_ATTRIB_TEX7,
63 VF_ATTRIB_VAR0,
64 VF_ATTRIB_VAR1,
65 VF_ATTRIB_VAR2,
66 VF_ATTRIB_VAR3,
67 VF_ATTRIB_VAR4,
68 VF_ATTRIB_VAR5,
69 VF_ATTRIB_VAR6,
70 VF_ATTRIB_VAR7,
71 };
72
73
74 /**
75 * Determine which post-transform / pre-rasterization vertex attributes
76 * we need.
77 * Derived from: fs, setup states.
78 */
79 static void calculate_vertex_layout( struct softpipe_context *softpipe )
80 {
81 const GLbitfield inputsRead = softpipe->fs.inputs_read;
82 GLuint slot_to_vf_attr[VF_ATTRIB_MAX];
83 GLbitfield attr_mask = 0x0;
84 GLuint i;
85
86 /* Need Z if depth test is enabled or the fragment program uses the
87 * fragment position (XYZW).
88 */
89 if (softpipe->depth_test.enabled ||
90 (inputsRead & FRAG_ATTRIB_WPOS))
91 softpipe->need_z = GL_TRUE;
92 else
93 softpipe->need_z = GL_FALSE;
94
95 /* Need W if we do any perspective-corrected interpolation or the
96 * fragment program uses the fragment position.
97 */
98 if (inputsRead & FRAG_ATTRIB_WPOS)
99 softpipe->need_w = GL_TRUE;
100 else
101 softpipe->need_w = GL_FALSE;
102
103
104 softpipe->nr_attrs = 0;
105 memset(slot_to_vf_attr, 0, sizeof(slot_to_vf_attr));
106
107 memset(softpipe->fp_attr_to_slot, 0, sizeof(softpipe->fp_attr_to_slot));
108 memset(softpipe->vf_attr_to_slot, 0, sizeof(softpipe->vf_attr_to_slot));
109
110 /* TODO - Figure out if we need to do perspective divide, etc.
111 */
112 EMIT_ATTR(VF_ATTRIB_POS, FRAG_ATTRIB_WPOS, INTERP_LINEAR);
113
114 /* Pull in the rest of the attributes. They are all in float4
115 * format. Future optimizations could be to keep some attributes
116 * as fixed point or ubyte format.
117 */
118 for (i = 1; i < FRAG_ATTRIB_TEX0; i++) {
119 if (inputsRead & (1 << i)) {
120 assert(i < sizeof(frag_to_vf) / sizeof(frag_to_vf[0]));
121 if (softpipe->setup.flatshade
122 && (i == FRAG_ATTRIB_COL0 || i == FRAG_ATTRIB_COL1))
123 EMIT_ATTR(frag_to_vf[i], i, INTERP_CONSTANT);
124 else
125 EMIT_ATTR(frag_to_vf[i], i, INTERP_LINEAR);
126 }
127 }
128
129 for (i = FRAG_ATTRIB_TEX0; i < FRAG_ATTRIB_MAX; i++) {
130 if (inputsRead & (1 << i)) {
131 assert(i < sizeof(frag_to_vf) / sizeof(frag_to_vf[0]));
132 EMIT_ATTR(frag_to_vf[i], i, INTERP_PERSPECTIVE);
133 softpipe->need_w = GL_TRUE;
134 }
135 }
136
137 softpipe->nr_frag_attrs = softpipe->nr_attrs;
138
139 /* Additional attributes required for setup: Just twosided
140 * lighting. Edgeflag is dealt with specially by setting bits in
141 * the vertex header.
142 */
143 if (softpipe->setup.light_twoside) {
144 if (inputsRead & FRAG_BIT_COL0) {
145 EMIT_ATTR(VF_ATTRIB_BFC0, FRAG_ATTRIB_MAX, 0); /* XXX: mark as discarded after setup */
146 }
147
148 if (inputsRead & FRAG_BIT_COL1) {
149 EMIT_ATTR(VF_ATTRIB_BFC1, FRAG_ATTRIB_MAX, 0); /* XXX: discard after setup */
150 }
151 }
152
153 /* If the attributes have changed, tell the draw module (which in turn
154 * tells the vf module) about the new vertex layout.
155 */
156 if (attr_mask != softpipe->attr_mask) {
157 softpipe->attr_mask = attr_mask;
158
159 draw_set_vertex_attributes( softpipe->draw,
160 slot_to_vf_attr,
161 softpipe->nr_attrs );
162 }
163 }
164
165
166 /**
167 * Recompute cliprect from scissor bounds, scissor enable and surface size.
168 */
169 static void
170 compute_cliprect(struct softpipe_context *sp)
171 {
172 GLint surfWidth, surfHeight;
173
174 if (sp->framebuffer.num_cbufs > 0) {
175 surfWidth = sp->framebuffer.cbufs[0]->width;
176 surfHeight = sp->framebuffer.cbufs[0]->height;
177 }
178 else {
179 /* no surface? */
180 surfWidth = sp->scissor.maxx;
181 surfHeight = sp->scissor.maxy;
182 }
183
184 if (sp->setup.scissor) {
185 /* clip to scissor rect */
186 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
187 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
188 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
189 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
190 }
191 else {
192 /* clip to surface bounds */
193 sp->cliprect.minx = 0;
194 sp->cliprect.miny = 0;
195 sp->cliprect.maxx = surfWidth;
196 sp->cliprect.maxy = surfHeight;
197 }
198 }
199
200
201 /* Hopefully this will remain quite simple, otherwise need to pull in
202 * something like the state tracker mechanism.
203 */
204 void softpipe_update_derived( struct softpipe_context *softpipe )
205 {
206 if (softpipe->dirty & (SP_NEW_SETUP | SP_NEW_FS))
207 calculate_vertex_layout( softpipe );
208
209 if (softpipe->dirty & (SP_NEW_SCISSOR |
210 SP_NEW_STENCIL |
211 SP_NEW_FRAMEBUFFER))
212 compute_cliprect(softpipe);
213
214 if (softpipe->dirty & (SP_NEW_BLEND |
215 SP_NEW_DEPTH_TEST |
216 SP_NEW_ALPHA_TEST |
217 SP_NEW_FRAMEBUFFER |
218 SP_NEW_STENCIL |
219 SP_NEW_SETUP |
220 SP_NEW_FS))
221 sp_build_quad_pipeline(softpipe);
222
223 softpipe->dirty = 0;
224 }