The i915simple driver now runs well enough to lock up hardware.
[mesa.git] / src / mesa / pipe / i915simple / i915_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 "i915_context.h"
35 #include "i915_state.h"
36
37
38 #define EMIT_ATTR( VF_ATTR, FRAG_ATTR, INTERP ) \
39 do { \
40 slot_to_vf_attr[nr_attrs] = VF_ATTR; \
41 nr_attrs++; \
42 attr_mask |= (1 << (VF_ATTR)); \
43 } while (0)
44
45
46 static const GLuint frag_to_vf[FRAG_ATTRIB_MAX] =
47 {
48 VF_ATTRIB_POS,
49 VF_ATTRIB_COLOR0,
50 VF_ATTRIB_COLOR1,
51 VF_ATTRIB_FOG,
52 VF_ATTRIB_TEX0,
53 VF_ATTRIB_TEX1,
54 VF_ATTRIB_TEX2,
55 VF_ATTRIB_TEX3,
56 VF_ATTRIB_TEX4,
57 VF_ATTRIB_TEX5,
58 VF_ATTRIB_TEX6,
59 VF_ATTRIB_TEX7,
60 VF_ATTRIB_VAR0,
61 VF_ATTRIB_VAR1,
62 VF_ATTRIB_VAR2,
63 VF_ATTRIB_VAR3,
64 VF_ATTRIB_VAR4,
65 VF_ATTRIB_VAR5,
66 VF_ATTRIB_VAR6,
67 VF_ATTRIB_VAR7,
68 };
69
70
71 /**
72 * Determine which post-transform / pre-rasterization vertex attributes
73 * we need.
74 * Derived from: fs, setup states.
75 */
76 static void calculate_vertex_layout( struct i915_context *i915 )
77 {
78 // const GLbitfield inputsRead = i915->fs.inputs_read;
79 const GLbitfield inputsRead = (FRAG_BIT_WPOS | FRAG_BIT_COL0);
80 GLuint slot_to_vf_attr[VF_ATTRIB_MAX];
81 GLbitfield attr_mask = 0x0;
82 GLuint nr_attrs = 0;
83 GLuint i;
84
85 memset(slot_to_vf_attr, 0, sizeof(slot_to_vf_attr));
86
87
88 /* TODO - Figure out if we need to do perspective divide, etc.
89 */
90 EMIT_ATTR(VF_ATTRIB_POS, FRAG_ATTRIB_WPOS, INTERP_LINEAR);
91
92 /* Pull in the rest of the attributes. They are all in float4
93 * format. Future optimizations could be to keep some attributes
94 * as fixed point or ubyte format.
95 */
96 for (i = 1; i < FRAG_ATTRIB_TEX0; i++) {
97 if (inputsRead & (1 << i)) {
98 assert(i < sizeof(frag_to_vf) / sizeof(frag_to_vf[0]));
99 if (i915->setup.flatshade
100 && (i == FRAG_ATTRIB_COL0 || i == FRAG_ATTRIB_COL1))
101 EMIT_ATTR(frag_to_vf[i], i, INTERP_CONSTANT);
102 else
103 EMIT_ATTR(frag_to_vf[i], i, INTERP_LINEAR);
104 }
105 }
106
107 for (i = FRAG_ATTRIB_TEX0; i < FRAG_ATTRIB_MAX; i++) {
108 if (inputsRead & (1 << i)) {
109 assert(i < sizeof(frag_to_vf) / sizeof(frag_to_vf[0]));
110 EMIT_ATTR(frag_to_vf[i], i, INTERP_PERSPECTIVE);
111 }
112 }
113
114 /* Additional attributes required for setup: Just twosided
115 * lighting. Edgeflag is dealt with specially by setting bits in
116 * the vertex header.
117 */
118 if (i915->setup.light_twoside) {
119 if (inputsRead & FRAG_BIT_COL0) {
120 EMIT_ATTR(VF_ATTRIB_BFC0, FRAG_ATTRIB_MAX, 0); /* XXX: mark as discarded after setup */
121 }
122
123 if (inputsRead & FRAG_BIT_COL1) {
124 EMIT_ATTR(VF_ATTRIB_BFC1, FRAG_ATTRIB_MAX, 0); /* XXX: discard after setup */
125 }
126 }
127
128 /* If the attributes have changed, tell the draw module (which in turn
129 * tells the vf module) about the new vertex layout.
130 */
131 draw_set_vertex_attributes( i915->draw,
132 slot_to_vf_attr,
133 nr_attrs );
134 }
135
136
137 /**
138 * Recompute cliprect from scissor bounds, scissor enable and surface size.
139 */
140 static void
141 compute_cliprect(struct i915_context *sp)
142 {
143 GLint surfWidth, surfHeight;
144
145 if (sp->framebuffer.num_cbufs > 0) {
146 surfWidth = sp->framebuffer.cbufs[0]->width;
147 surfHeight = sp->framebuffer.cbufs[0]->height;
148 }
149 else {
150 /* no surface? */
151 surfWidth = sp->scissor.maxx;
152 surfHeight = sp->scissor.maxy;
153 }
154
155 if (sp->setup.scissor) {
156 /* clip to scissor rect */
157 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
158 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
159 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
160 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
161 }
162 else {
163 /* clip to surface bounds */
164 sp->cliprect.minx = 0;
165 sp->cliprect.miny = 0;
166 sp->cliprect.maxx = surfWidth;
167 sp->cliprect.maxy = surfHeight;
168 }
169 }
170
171
172 /* Hopefully this will remain quite simple, otherwise need to pull in
173 * something like the state tracker mechanism.
174 */
175 void i915_update_derived( struct i915_context *i915 )
176 {
177 if (i915->dirty & (I915_NEW_SETUP | I915_NEW_FS))
178 calculate_vertex_layout( i915 );
179
180 if (i915->dirty & (I915_NEW_SCISSOR |
181 I915_NEW_STENCIL |
182 I915_NEW_FRAMEBUFFER))
183 compute_cliprect(i915);
184
185 i915->dirty = 0;
186 }