Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / i915 / intel_state.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
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/macros.h"
32 #include "main/enums.h"
33 #include "main/colormac.h"
34 #include "main/dd.h"
35
36 #include "intel_screen.h"
37 #include "intel_context.h"
38 #include "intel_fbo.h"
39 #include "intel_regions.h"
40 #include "swrast/swrast.h"
41
42 int
43 intel_translate_shadow_compare_func( GLenum func )
44 {
45 switch(func) {
46 case GL_NEVER:
47 return COMPAREFUNC_ALWAYS;
48 case GL_LESS:
49 return COMPAREFUNC_LEQUAL;
50 case GL_LEQUAL:
51 return COMPAREFUNC_LESS;
52 case GL_GREATER:
53 return COMPAREFUNC_GEQUAL;
54 case GL_GEQUAL:
55 return COMPAREFUNC_GREATER;
56 case GL_NOTEQUAL:
57 return COMPAREFUNC_EQUAL;
58 case GL_EQUAL:
59 return COMPAREFUNC_NOTEQUAL;
60 case GL_ALWAYS:
61 return COMPAREFUNC_NEVER;
62 }
63
64 fprintf(stderr, "Unknown value in %s: %x\n", __FUNCTION__, func);
65 return COMPAREFUNC_NEVER;
66 }
67
68 int
69 intel_translate_compare_func(GLenum func)
70 {
71 switch (func) {
72 case GL_NEVER:
73 return COMPAREFUNC_NEVER;
74 case GL_LESS:
75 return COMPAREFUNC_LESS;
76 case GL_LEQUAL:
77 return COMPAREFUNC_LEQUAL;
78 case GL_GREATER:
79 return COMPAREFUNC_GREATER;
80 case GL_GEQUAL:
81 return COMPAREFUNC_GEQUAL;
82 case GL_NOTEQUAL:
83 return COMPAREFUNC_NOTEQUAL;
84 case GL_EQUAL:
85 return COMPAREFUNC_EQUAL;
86 case GL_ALWAYS:
87 return COMPAREFUNC_ALWAYS;
88 }
89
90 fprintf(stderr, "Unknown value in %s: %x\n", __FUNCTION__, func);
91 return COMPAREFUNC_ALWAYS;
92 }
93
94 int
95 intel_translate_stencil_op(GLenum op)
96 {
97 switch (op) {
98 case GL_KEEP:
99 return STENCILOP_KEEP;
100 case GL_ZERO:
101 return STENCILOP_ZERO;
102 case GL_REPLACE:
103 return STENCILOP_REPLACE;
104 case GL_INCR:
105 return STENCILOP_INCRSAT;
106 case GL_DECR:
107 return STENCILOP_DECRSAT;
108 case GL_INCR_WRAP:
109 return STENCILOP_INCR;
110 case GL_DECR_WRAP:
111 return STENCILOP_DECR;
112 case GL_INVERT:
113 return STENCILOP_INVERT;
114 default:
115 return STENCILOP_ZERO;
116 }
117 }
118
119 int
120 intel_translate_blend_factor(GLenum factor)
121 {
122 switch (factor) {
123 case GL_ZERO:
124 return BLENDFACT_ZERO;
125 case GL_SRC_ALPHA:
126 return BLENDFACT_SRC_ALPHA;
127 case GL_ONE:
128 return BLENDFACT_ONE;
129 case GL_SRC_COLOR:
130 return BLENDFACT_SRC_COLR;
131 case GL_ONE_MINUS_SRC_COLOR:
132 return BLENDFACT_INV_SRC_COLR;
133 case GL_DST_COLOR:
134 return BLENDFACT_DST_COLR;
135 case GL_ONE_MINUS_DST_COLOR:
136 return BLENDFACT_INV_DST_COLR;
137 case GL_ONE_MINUS_SRC_ALPHA:
138 return BLENDFACT_INV_SRC_ALPHA;
139 case GL_DST_ALPHA:
140 return BLENDFACT_DST_ALPHA;
141 case GL_ONE_MINUS_DST_ALPHA:
142 return BLENDFACT_INV_DST_ALPHA;
143 case GL_SRC_ALPHA_SATURATE:
144 return BLENDFACT_SRC_ALPHA_SATURATE;
145 case GL_CONSTANT_COLOR:
146 return BLENDFACT_CONST_COLOR;
147 case GL_ONE_MINUS_CONSTANT_COLOR:
148 return BLENDFACT_INV_CONST_COLOR;
149 case GL_CONSTANT_ALPHA:
150 return BLENDFACT_CONST_ALPHA;
151 case GL_ONE_MINUS_CONSTANT_ALPHA:
152 return BLENDFACT_INV_CONST_ALPHA;
153 }
154
155 fprintf(stderr, "Unknown value in %s: %x\n", __FUNCTION__, factor);
156 return BLENDFACT_ZERO;
157 }
158
159 int
160 intel_translate_logic_op(GLenum opcode)
161 {
162 switch (opcode) {
163 case GL_CLEAR:
164 return LOGICOP_CLEAR;
165 case GL_AND:
166 return LOGICOP_AND;
167 case GL_AND_REVERSE:
168 return LOGICOP_AND_RVRSE;
169 case GL_COPY:
170 return LOGICOP_COPY;
171 case GL_COPY_INVERTED:
172 return LOGICOP_COPY_INV;
173 case GL_AND_INVERTED:
174 return LOGICOP_AND_INV;
175 case GL_NOOP:
176 return LOGICOP_NOOP;
177 case GL_XOR:
178 return LOGICOP_XOR;
179 case GL_OR:
180 return LOGICOP_OR;
181 case GL_OR_INVERTED:
182 return LOGICOP_OR_INV;
183 case GL_NOR:
184 return LOGICOP_NOR;
185 case GL_EQUIV:
186 return LOGICOP_EQUIV;
187 case GL_INVERT:
188 return LOGICOP_INV;
189 case GL_OR_REVERSE:
190 return LOGICOP_OR_RVRSE;
191 case GL_NAND:
192 return LOGICOP_NAND;
193 case GL_SET:
194 return LOGICOP_SET;
195 default:
196 return LOGICOP_SET;
197 }
198 }
199
200
201 static void
202 intelClearColor(GLcontext * ctx, const GLfloat color[4])
203 {
204 struct intel_context *intel = intel_context(ctx);
205 GLubyte clear[4];
206
207 CLAMPED_FLOAT_TO_UBYTE(clear[0], color[0]);
208 CLAMPED_FLOAT_TO_UBYTE(clear[1], color[1]);
209 CLAMPED_FLOAT_TO_UBYTE(clear[2], color[2]);
210 CLAMPED_FLOAT_TO_UBYTE(clear[3], color[3]);
211
212 /* compute both 32 and 16-bit clear values */
213 intel->ClearColor8888 = INTEL_PACKCOLOR8888(clear[0], clear[1],
214 clear[2], clear[3]);
215 intel->ClearColor565 = INTEL_PACKCOLOR565(clear[0], clear[1], clear[2]);
216 }
217
218
219 /**
220 * Update the viewport transformation matrix. Depends on:
221 * - viewport pos/size
222 * - depthrange
223 * - window pos/size or FBO size
224 */
225 static void
226 intelCalcViewport(GLcontext * ctx)
227 {
228 struct intel_context *intel = intel_context(ctx);
229 const GLfloat *v = ctx->Viewport._WindowMap.m;
230 const GLfloat depthScale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
231 GLfloat *m = intel->ViewportMatrix.m;
232 GLfloat yScale, yBias;
233
234 if (ctx->DrawBuffer->Name) {
235 /* User created FBO */
236 struct intel_renderbuffer *irb
237 = intel_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
238 if (irb && !irb->RenderToTexture) {
239 /* y=0=top */
240 yScale = -1.0;
241 yBias = irb->Base.Height;
242 }
243 else {
244 /* y=0=bottom */
245 yScale = 1.0;
246 yBias = 0.0;
247 }
248 }
249 else {
250 /* window buffer, y=0=top */
251 yScale = -1.0;
252 yBias = (intel->driDrawable) ? intel->driDrawable->h : 0.0F;
253 }
254
255 m[MAT_SX] = v[MAT_SX];
256 m[MAT_TX] = v[MAT_TX];
257
258 m[MAT_SY] = v[MAT_SY] * yScale;
259 m[MAT_TY] = v[MAT_TY] * yScale + yBias;
260
261 m[MAT_SZ] = v[MAT_SZ] * depthScale;
262 m[MAT_TZ] = v[MAT_TZ] * depthScale;
263 }
264
265 static void
266 intelViewport(GLcontext * ctx,
267 GLint x, GLint y, GLsizei width, GLsizei height)
268 {
269 intelCalcViewport(ctx);
270
271 intel_viewport(ctx, x, y, width, height);
272 }
273
274 static void
275 intelDepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
276 {
277 intelCalcViewport(ctx);
278 }
279
280 /* Fallback to swrast for select and feedback.
281 */
282 static void
283 intelRenderMode(GLcontext * ctx, GLenum mode)
284 {
285 struct intel_context *intel = intel_context(ctx);
286 FALLBACK(intel, INTEL_FALLBACK_RENDERMODE, (mode != GL_RENDER));
287 }
288
289
290 void
291 intelInitStateFuncs(struct dd_function_table *functions)
292 {
293 functions->RenderMode = intelRenderMode;
294 functions->Viewport = intelViewport;
295 functions->DepthRange = intelDepthRange;
296 functions->ClearColor = intelClearColor;
297 }