i915: rename some functions
[mesa.git] / src / mesa / drivers / dri / i915 / i915_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/dd.h"
34 #include "tnl/tnl.h"
35 #include "tnl/t_context.h"
36
37 #include "texmem.h"
38
39 #include "drivers/common/driverfuncs.h"
40
41 #include "intel_fbo.h"
42 #include "intel_screen.h"
43 #include "intel_batchbuffer.h"
44
45 #include "i915_context.h"
46 #include "i915_reg.h"
47
48 #define FILE_DEBUG_FLAG DEBUG_STATE
49
50 static void
51 i915StencilFuncSeparate(GLcontext * ctx, GLenum face, GLenum func, GLint ref,
52 GLuint mask)
53 {
54 struct i915_context *i915 = I915_CONTEXT(ctx);
55 int test = intel_translate_compare_func(func);
56
57 mask = mask & 0xff;
58
59 DBG("%s : func: %s, ref : 0x%x, mask: 0x%x\n", __FUNCTION__,
60 _mesa_lookup_enum_by_nr(func), ref, mask);
61
62
63 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
64 i915->state.Ctx[I915_CTXREG_STATE4] &= ~MODE4_ENABLE_STENCIL_TEST_MASK;
65 i915->state.Ctx[I915_CTXREG_STATE4] |= (ENABLE_STENCIL_TEST_MASK |
66 STENCIL_TEST_MASK(mask));
67
68 i915->state.Ctx[I915_CTXREG_LIS5] &= ~(S5_STENCIL_REF_MASK |
69 S5_STENCIL_TEST_FUNC_MASK);
70
71 i915->state.Ctx[I915_CTXREG_LIS5] |= ((ref << S5_STENCIL_REF_SHIFT) |
72 (test <<
73 S5_STENCIL_TEST_FUNC_SHIFT));
74 }
75
76 static void
77 i915StencilMaskSeparate(GLcontext * ctx, GLenum face, GLuint mask)
78 {
79 struct i915_context *i915 = I915_CONTEXT(ctx);
80
81 DBG("%s : mask 0x%x\n", __FUNCTION__, mask);
82
83 mask = mask & 0xff;
84
85 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
86 i915->state.Ctx[I915_CTXREG_STATE4] &= ~MODE4_ENABLE_STENCIL_WRITE_MASK;
87 i915->state.Ctx[I915_CTXREG_STATE4] |= (ENABLE_STENCIL_WRITE_MASK |
88 STENCIL_WRITE_MASK(mask));
89 }
90
91
92 static void
93 i915StencilOpSeparate(GLcontext * ctx, GLenum face, GLenum fail, GLenum zfail,
94 GLenum zpass)
95 {
96 struct i915_context *i915 = I915_CONTEXT(ctx);
97 int fop = intel_translate_stencil_op(fail);
98 int dfop = intel_translate_stencil_op(zfail);
99 int dpop = intel_translate_stencil_op(zpass);
100
101
102 DBG("%s: fail : %s, zfail: %s, zpass : %s\n", __FUNCTION__,
103 _mesa_lookup_enum_by_nr(fail),
104 _mesa_lookup_enum_by_nr(zfail), _mesa_lookup_enum_by_nr(zpass));
105
106 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
107
108 i915->state.Ctx[I915_CTXREG_LIS5] &= ~(S5_STENCIL_FAIL_MASK |
109 S5_STENCIL_PASS_Z_FAIL_MASK |
110 S5_STENCIL_PASS_Z_PASS_MASK);
111
112 i915->state.Ctx[I915_CTXREG_LIS5] |= ((fop << S5_STENCIL_FAIL_SHIFT) |
113 (dfop <<
114 S5_STENCIL_PASS_Z_FAIL_SHIFT) |
115 (dpop <<
116 S5_STENCIL_PASS_Z_PASS_SHIFT));
117 }
118
119 static void
120 i915AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
121 {
122 struct i915_context *i915 = I915_CONTEXT(ctx);
123 int test = intel_translate_compare_func(func);
124 GLubyte refByte;
125
126 UNCLAMPED_FLOAT_TO_UBYTE(refByte, ref);
127
128 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
129 i915->state.Ctx[I915_CTXREG_LIS6] &= ~(S6_ALPHA_TEST_FUNC_MASK |
130 S6_ALPHA_REF_MASK);
131 i915->state.Ctx[I915_CTXREG_LIS6] |= ((test << S6_ALPHA_TEST_FUNC_SHIFT) |
132 (((GLuint) refByte) <<
133 S6_ALPHA_REF_SHIFT));
134 }
135
136 /* This function makes sure that the proper enables are
137 * set for LogicOp, Independant Alpha Blend, and Blending.
138 * It needs to be called from numerous places where we
139 * could change the LogicOp or Independant Alpha Blend without subsequent
140 * calls to glEnable.
141 */
142 static void
143 i915EvalLogicOpBlendState(GLcontext * ctx)
144 {
145 struct i915_context *i915 = I915_CONTEXT(ctx);
146
147 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
148
149 if (RGBA_LOGICOP_ENABLED(ctx)) {
150 i915->state.Ctx[I915_CTXREG_LIS5] |= S5_LOGICOP_ENABLE;
151 i915->state.Ctx[I915_CTXREG_LIS6] &= ~S6_CBUF_BLEND_ENABLE;
152 }
153 else {
154 i915->state.Ctx[I915_CTXREG_LIS5] &= ~S5_LOGICOP_ENABLE;
155
156 if (ctx->Color.BlendEnabled) {
157 i915->state.Ctx[I915_CTXREG_LIS6] |= S6_CBUF_BLEND_ENABLE;
158 }
159 else {
160 i915->state.Ctx[I915_CTXREG_LIS6] &= ~S6_CBUF_BLEND_ENABLE;
161 }
162 }
163 }
164
165 static void
166 i915BlendColor(GLcontext * ctx, const GLfloat color[4])
167 {
168 struct i915_context *i915 = I915_CONTEXT(ctx);
169 GLubyte r, g, b, a;
170
171 DBG("%s\n", __FUNCTION__);
172
173 UNCLAMPED_FLOAT_TO_UBYTE(r, color[RCOMP]);
174 UNCLAMPED_FLOAT_TO_UBYTE(g, color[GCOMP]);
175 UNCLAMPED_FLOAT_TO_UBYTE(b, color[BCOMP]);
176 UNCLAMPED_FLOAT_TO_UBYTE(a, color[ACOMP]);
177
178 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
179 i915->state.Ctx[I915_CTXREG_BLENDCOLOR1] =
180 (a << 24) | (r << 16) | (g << 8) | b;
181 }
182
183
184 #define DST_BLND_FACT(f) ((f)<<S6_CBUF_DST_BLEND_FACT_SHIFT)
185 #define SRC_BLND_FACT(f) ((f)<<S6_CBUF_SRC_BLEND_FACT_SHIFT)
186 #define DST_ABLND_FACT(f) ((f)<<IAB_DST_FACTOR_SHIFT)
187 #define SRC_ABLND_FACT(f) ((f)<<IAB_SRC_FACTOR_SHIFT)
188
189
190
191 static GLuint
192 translate_blend_equation(GLenum mode)
193 {
194 switch (mode) {
195 case GL_FUNC_ADD:
196 return BLENDFUNC_ADD;
197 case GL_MIN:
198 return BLENDFUNC_MIN;
199 case GL_MAX:
200 return BLENDFUNC_MAX;
201 case GL_FUNC_SUBTRACT:
202 return BLENDFUNC_SUBTRACT;
203 case GL_FUNC_REVERSE_SUBTRACT:
204 return BLENDFUNC_REVERSE_SUBTRACT;
205 default:
206 return 0;
207 }
208 }
209
210 static void
211 i915UpdateBlendState(GLcontext * ctx)
212 {
213 struct i915_context *i915 = I915_CONTEXT(ctx);
214 GLuint iab = (i915->state.Ctx[I915_CTXREG_IAB] &
215 ~(IAB_SRC_FACTOR_MASK |
216 IAB_DST_FACTOR_MASK |
217 (BLENDFUNC_MASK << IAB_FUNC_SHIFT) | IAB_ENABLE));
218
219 GLuint lis6 = (i915->state.Ctx[I915_CTXREG_LIS6] &
220 ~(S6_CBUF_SRC_BLEND_FACT_MASK |
221 S6_CBUF_DST_BLEND_FACT_MASK | S6_CBUF_BLEND_FUNC_MASK));
222
223 GLuint eqRGB = ctx->Color.BlendEquationRGB;
224 GLuint eqA = ctx->Color.BlendEquationA;
225 GLuint srcRGB = ctx->Color.BlendSrcRGB;
226 GLuint dstRGB = ctx->Color.BlendDstRGB;
227 GLuint srcA = ctx->Color.BlendSrcA;
228 GLuint dstA = ctx->Color.BlendDstA;
229
230 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
231 srcRGB = dstRGB = GL_ONE;
232 }
233
234 if (eqA == GL_MIN || eqA == GL_MAX) {
235 srcA = dstA = GL_ONE;
236 }
237
238 lis6 |= SRC_BLND_FACT(intel_translate_blend_factor(srcRGB));
239 lis6 |= DST_BLND_FACT(intel_translate_blend_factor(dstRGB));
240 lis6 |= translate_blend_equation(eqRGB) << S6_CBUF_BLEND_FUNC_SHIFT;
241
242 iab |= SRC_ABLND_FACT(intel_translate_blend_factor(srcA));
243 iab |= DST_ABLND_FACT(intel_translate_blend_factor(dstA));
244 iab |= translate_blend_equation(eqA) << IAB_FUNC_SHIFT;
245
246 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB)
247 iab |= IAB_ENABLE;
248
249 if (iab != i915->state.Ctx[I915_CTXREG_IAB] ||
250 lis6 != i915->state.Ctx[I915_CTXREG_LIS6]) {
251 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
252 i915->state.Ctx[I915_CTXREG_IAB] = iab;
253 i915->state.Ctx[I915_CTXREG_LIS6] = lis6;
254 }
255
256 /* This will catch a logicop blend equation */
257 i915EvalLogicOpBlendState(ctx);
258 }
259
260
261 static void
262 i915BlendFuncSeparate(GLcontext * ctx, GLenum srcRGB,
263 GLenum dstRGB, GLenum srcA, GLenum dstA)
264 {
265 i915UpdateBlendState(ctx);
266 }
267
268
269 static void
270 i915BlendEquationSeparate(GLcontext * ctx, GLenum eqRGB, GLenum eqA)
271 {
272 i915UpdateBlendState(ctx);
273 }
274
275
276 static void
277 i915DepthFunc(GLcontext * ctx, GLenum func)
278 {
279 struct i915_context *i915 = I915_CONTEXT(ctx);
280 int test = intel_translate_compare_func(func);
281
282 DBG("%s\n", __FUNCTION__);
283
284 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
285 i915->state.Ctx[I915_CTXREG_LIS6] &= ~S6_DEPTH_TEST_FUNC_MASK;
286 i915->state.Ctx[I915_CTXREG_LIS6] |= test << S6_DEPTH_TEST_FUNC_SHIFT;
287 }
288
289 static void
290 i915DepthMask(GLcontext * ctx, GLboolean flag)
291 {
292 struct i915_context *i915 = I915_CONTEXT(ctx);
293
294 DBG("%s flag (%d)\n", __FUNCTION__, flag);
295
296 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
297
298 if (flag && ctx->Depth.Test)
299 i915->state.Ctx[I915_CTXREG_LIS6] |= S6_DEPTH_WRITE_ENABLE;
300 else
301 i915->state.Ctx[I915_CTXREG_LIS6] &= ~S6_DEPTH_WRITE_ENABLE;
302 }
303
304
305
306 /**
307 * Update the viewport transformation matrix. Depends on:
308 * - viewport pos/size
309 * - depthrange
310 * - window pos/size or FBO size
311 */
312 static void
313 intelCalcViewport(GLcontext * ctx)
314 {
315 struct intel_context *intel = intel_context(ctx);
316 const GLfloat *v = ctx->Viewport._WindowMap.m;
317 const GLfloat depthScale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
318 GLfloat *m = intel->ViewportMatrix.m;
319 GLfloat yScale, yBias;
320
321 if (ctx->DrawBuffer->Name) {
322 /* User created FBO */
323 struct intel_renderbuffer *irb
324 = intel_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
325 if (irb && !irb->RenderToTexture) {
326 /* y=0=top */
327 yScale = -1.0;
328 yBias = irb->Base.Height;
329 }
330 else {
331 /* y=0=bottom */
332 yScale = 1.0;
333 yBias = 0.0;
334 }
335 }
336 else {
337 /* window buffer, y=0=top */
338 yScale = -1.0;
339 yBias = (intel->driDrawable) ? intel->driDrawable->h : 0.0F;
340 }
341
342 m[MAT_SX] = v[MAT_SX];
343 m[MAT_TX] = v[MAT_TX];
344
345 m[MAT_SY] = v[MAT_SY] * yScale;
346 m[MAT_TY] = v[MAT_TY] * yScale + yBias;
347
348 m[MAT_SZ] = v[MAT_SZ] * depthScale;
349 m[MAT_TZ] = v[MAT_TZ] * depthScale;
350 }
351
352
353 /** Called from ctx->Driver.Viewport() */
354 static void
355 i915Viewport(GLcontext * ctx,
356 GLint x, GLint y, GLsizei width, GLsizei height)
357 {
358 intelCalcViewport(ctx);
359
360 intel_viewport(ctx, x, y, width, height);
361 }
362
363
364 /** Called from ctx->Driver.DepthRange() */
365 static void
366 i915DepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
367 {
368 intelCalcViewport(ctx);
369 }
370
371
372 /* =============================================================
373 * Polygon stipple
374 *
375 * The i915 supports a 4x4 stipple natively, GL wants 32x32.
376 * Fortunately stipple is usually a repeating pattern.
377 */
378 static void
379 i915PolygonStipple(GLcontext * ctx, const GLubyte * mask)
380 {
381 struct i915_context *i915 = I915_CONTEXT(ctx);
382 const GLubyte *m;
383 GLubyte p[4];
384 int i, j, k;
385 int active = (ctx->Polygon.StippleFlag &&
386 i915->intel.reduced_primitive == GL_TRIANGLES);
387 GLuint newMask;
388
389 if (active) {
390 I915_STATECHANGE(i915, I915_UPLOAD_STIPPLE);
391 i915->state.Stipple[I915_STPREG_ST1] &= ~ST1_ENABLE;
392 }
393
394 /* Use the already unpacked stipple data from the context rather than the
395 * uninterpreted mask passed in.
396 */
397 mask = (const GLubyte *)ctx->PolygonStipple;
398 m = mask;
399
400 p[0] = mask[12] & 0xf;
401 p[0] |= p[0] << 4;
402 p[1] = mask[8] & 0xf;
403 p[1] |= p[1] << 4;
404 p[2] = mask[4] & 0xf;
405 p[2] |= p[2] << 4;
406 p[3] = mask[0] & 0xf;
407 p[3] |= p[3] << 4;
408
409 for (k = 0; k < 8; k++)
410 for (j = 3; j >= 0; j--)
411 for (i = 0; i < 4; i++, m++)
412 if (*m != p[j]) {
413 i915->intel.hw_stipple = 0;
414 return;
415 }
416
417 newMask = (((p[0] & 0xf) << 0) |
418 ((p[1] & 0xf) << 4) |
419 ((p[2] & 0xf) << 8) | ((p[3] & 0xf) << 12));
420
421
422 if (newMask == 0xffff || newMask == 0x0) {
423 /* this is needed to make conform pass */
424 i915->intel.hw_stipple = 0;
425 return;
426 }
427
428 i915->state.Stipple[I915_STPREG_ST1] &= ~0xffff;
429 i915->state.Stipple[I915_STPREG_ST1] |= newMask;
430 i915->intel.hw_stipple = 1;
431
432 if (active)
433 i915->state.Stipple[I915_STPREG_ST1] |= ST1_ENABLE;
434 }
435
436
437 /* =============================================================
438 * Hardware clipping
439 */
440 static void
441 i915Scissor(GLcontext * ctx, GLint x, GLint y, GLsizei w, GLsizei h)
442 {
443 struct i915_context *i915 = I915_CONTEXT(ctx);
444 int x1, y1, x2, y2;
445
446 if (!ctx->DrawBuffer)
447 return;
448
449 DBG("%s %d,%d %dx%d\n", __FUNCTION__, x, y, w, h);
450
451 if (ctx->DrawBuffer->Name == 0) {
452 x1 = x;
453 y1 = ctx->DrawBuffer->Height - (y + h);
454 x2 = x + w - 1;
455 y2 = y1 + h - 1;
456 DBG("%s %d..%d,%d..%d (inverted)\n", __FUNCTION__, x1, x2, y1, y2);
457 }
458 else {
459 /* FBO - not inverted
460 */
461 x1 = x;
462 y1 = y;
463 x2 = x + w - 1;
464 y2 = y + h - 1;
465 DBG("%s %d..%d,%d..%d (not inverted)\n", __FUNCTION__, x1, x2, y1, y2);
466 }
467
468 x1 = CLAMP(x1, 0, ctx->DrawBuffer->Width - 1);
469 y1 = CLAMP(y1, 0, ctx->DrawBuffer->Height - 1);
470 x2 = CLAMP(x2, 0, ctx->DrawBuffer->Width - 1);
471 y2 = CLAMP(y2, 0, ctx->DrawBuffer->Height - 1);
472
473 DBG("%s %d..%d,%d..%d (clamped)\n", __FUNCTION__, x1, x2, y1, y2);
474
475 I915_STATECHANGE(i915, I915_UPLOAD_BUFFERS);
476 i915->state.Buffer[I915_DESTREG_SR1] = (y1 << 16) | (x1 & 0xffff);
477 i915->state.Buffer[I915_DESTREG_SR2] = (y2 << 16) | (x2 & 0xffff);
478 }
479
480 static void
481 i915LogicOp(GLcontext * ctx, GLenum opcode)
482 {
483 struct i915_context *i915 = I915_CONTEXT(ctx);
484 int tmp = intel_translate_logic_op(opcode);
485
486 DBG("%s\n", __FUNCTION__);
487
488 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
489 i915->state.Ctx[I915_CTXREG_STATE4] &= ~LOGICOP_MASK;
490 i915->state.Ctx[I915_CTXREG_STATE4] |= LOGIC_OP_FUNC(tmp);
491 }
492
493
494
495 static void
496 i915CullFaceFrontFace(GLcontext * ctx, GLenum unused)
497 {
498 struct i915_context *i915 = I915_CONTEXT(ctx);
499 GLuint mode;
500
501 DBG("%s %d\n", __FUNCTION__,
502 ctx->DrawBuffer ? ctx->DrawBuffer->Name : 0);
503
504 if (!ctx->Polygon.CullFlag) {
505 mode = S4_CULLMODE_NONE;
506 }
507 else if (ctx->Polygon.CullFaceMode != GL_FRONT_AND_BACK) {
508 mode = S4_CULLMODE_CW;
509
510 if (ctx->DrawBuffer && ctx->DrawBuffer->Name != 0)
511 mode ^= (S4_CULLMODE_CW ^ S4_CULLMODE_CCW);
512 if (ctx->Polygon.CullFaceMode == GL_FRONT)
513 mode ^= (S4_CULLMODE_CW ^ S4_CULLMODE_CCW);
514 if (ctx->Polygon.FrontFace != GL_CCW)
515 mode ^= (S4_CULLMODE_CW ^ S4_CULLMODE_CCW);
516 }
517 else {
518 mode = S4_CULLMODE_BOTH;
519 }
520
521 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
522 i915->state.Ctx[I915_CTXREG_LIS4] &= ~S4_CULLMODE_MASK;
523 i915->state.Ctx[I915_CTXREG_LIS4] |= mode;
524 }
525
526 static void
527 i915LineWidth(GLcontext * ctx, GLfloat widthf)
528 {
529 struct i915_context *i915 = I915_CONTEXT(ctx);
530 int lis4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_LINE_WIDTH_MASK;
531 int width;
532
533 DBG("%s\n", __FUNCTION__);
534
535 width = (int) (widthf * 2);
536 CLAMP_SELF(width, 1, 0xf);
537 lis4 |= width << S4_LINE_WIDTH_SHIFT;
538
539 if (lis4 != i915->state.Ctx[I915_CTXREG_LIS4]) {
540 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
541 i915->state.Ctx[I915_CTXREG_LIS4] = lis4;
542 }
543 }
544
545 static void
546 i915PointSize(GLcontext * ctx, GLfloat size)
547 {
548 struct i915_context *i915 = I915_CONTEXT(ctx);
549 int lis4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_POINT_WIDTH_MASK;
550 GLint point_size = (int) size;
551
552 DBG("%s\n", __FUNCTION__);
553
554 CLAMP_SELF(point_size, 1, 255);
555 lis4 |= point_size << S4_POINT_WIDTH_SHIFT;
556
557 if (lis4 != i915->state.Ctx[I915_CTXREG_LIS4]) {
558 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
559 i915->state.Ctx[I915_CTXREG_LIS4] = lis4;
560 }
561 }
562
563
564 /* =============================================================
565 * Color masks
566 */
567
568 static void
569 i915ColorMask(GLcontext * ctx,
570 GLboolean r, GLboolean g, GLboolean b, GLboolean a)
571 {
572 struct i915_context *i915 = I915_CONTEXT(ctx);
573 GLuint tmp = i915->state.Ctx[I915_CTXREG_LIS5] & ~S5_WRITEDISABLE_MASK;
574
575 DBG("%s r(%d) g(%d) b(%d) a(%d)\n", __FUNCTION__, r, g, b,
576 a);
577
578 if (!r)
579 tmp |= S5_WRITEDISABLE_RED;
580 if (!g)
581 tmp |= S5_WRITEDISABLE_GREEN;
582 if (!b)
583 tmp |= S5_WRITEDISABLE_BLUE;
584 if (!a)
585 tmp |= S5_WRITEDISABLE_ALPHA;
586
587 if (tmp != i915->state.Ctx[I915_CTXREG_LIS5]) {
588 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
589 i915->state.Ctx[I915_CTXREG_LIS5] = tmp;
590 }
591 }
592
593 static void
594 update_specular(GLcontext * ctx)
595 {
596 /* A hack to trigger the rebuild of the fragment program.
597 */
598 intel_context(ctx)->NewGLState |= _NEW_TEXTURE;
599 }
600
601 static void
602 i915LightModelfv(GLcontext * ctx, GLenum pname, const GLfloat * param)
603 {
604 DBG("%s\n", __FUNCTION__);
605
606 if (pname == GL_LIGHT_MODEL_COLOR_CONTROL) {
607 update_specular(ctx);
608 }
609 }
610
611 static void
612 i915ShadeModel(GLcontext * ctx, GLenum mode)
613 {
614 struct i915_context *i915 = I915_CONTEXT(ctx);
615 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
616
617 if (mode == GL_SMOOTH) {
618 i915->state.Ctx[I915_CTXREG_LIS4] &= ~(S4_FLATSHADE_ALPHA |
619 S4_FLATSHADE_COLOR |
620 S4_FLATSHADE_SPECULAR);
621 }
622 else {
623 i915->state.Ctx[I915_CTXREG_LIS4] |= (S4_FLATSHADE_ALPHA |
624 S4_FLATSHADE_COLOR |
625 S4_FLATSHADE_SPECULAR);
626 }
627 }
628
629 /* =============================================================
630 * Fog
631 */
632 void
633 i915_update_fog(GLcontext * ctx)
634 {
635 struct i915_context *i915 = I915_CONTEXT(ctx);
636 GLenum mode;
637 GLboolean enabled;
638 GLboolean try_pixel_fog;
639
640 if (ctx->FragmentProgram._Current) {
641 /* Pull in static fog state from program */
642 mode = ctx->FragmentProgram._Current->FogOption;
643 enabled = (mode != GL_NONE);
644 try_pixel_fog = 0;
645 }
646 else {
647 enabled = ctx->Fog.Enabled;
648 mode = ctx->Fog.Mode;
649 #if 0
650 /* XXX - DISABLED -- Need ortho fallback */
651 try_pixel_fog = (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT
652 && ctx->Hint.Fog == GL_NICEST);
653 #else
654 try_pixel_fog = 0;
655 #endif
656 }
657
658 if (!enabled) {
659 i915->vertex_fog = I915_FOG_NONE;
660 }
661 else if (try_pixel_fog) {
662 I915_STATECHANGE(i915, I915_UPLOAD_FOG);
663 i915->state.Fog[I915_FOGREG_MODE1] &= ~FMC1_FOGFUNC_MASK;
664 i915->vertex_fog = I915_FOG_PIXEL;
665
666 switch (mode) {
667 case GL_LINEAR:
668 if (ctx->Fog.End <= ctx->Fog.Start) {
669 /* XXX - this won't work with fragment programs. Need to
670 * either fallback or append fog instructions to end of
671 * program in the case of linear fog.
672 */
673 printf("vertex fog!\n");
674 i915->state.Fog[I915_FOGREG_MODE1] |= FMC1_FOGFUNC_VERTEX;
675 i915->vertex_fog = I915_FOG_VERTEX;
676 }
677 else {
678 GLfloat c2 = 1.0 / (ctx->Fog.End - ctx->Fog.Start);
679 GLfloat c1 = ctx->Fog.End * c2;
680
681 i915->state.Fog[I915_FOGREG_MODE1] &= ~FMC1_C1_MASK;
682 i915->state.Fog[I915_FOGREG_MODE1] |= FMC1_FOGFUNC_PIXEL_LINEAR;
683 i915->state.Fog[I915_FOGREG_MODE1] |=
684 ((GLuint) (c1 * FMC1_C1_ONE)) & FMC1_C1_MASK;
685
686 if (i915->state.Fog[I915_FOGREG_MODE1] & FMC1_FOGINDEX_Z) {
687 i915->state.Fog[I915_FOGREG_MODE2]
688 = (GLuint) (c2 * FMC2_C2_ONE);
689 }
690 else {
691 fi_type fi;
692 fi.f = c2;
693 i915->state.Fog[I915_FOGREG_MODE2] = fi.i;
694 }
695 }
696 break;
697 case GL_EXP:
698 i915->state.Fog[I915_FOGREG_MODE1] |= FMC1_FOGFUNC_PIXEL_EXP;
699 break;
700 case GL_EXP2:
701 i915->state.Fog[I915_FOGREG_MODE1] |= FMC1_FOGFUNC_PIXEL_EXP2;
702 break;
703 default:
704 break;
705 }
706 }
707 else { /* if (i915->vertex_fog != I915_FOG_VERTEX) */
708 I915_STATECHANGE(i915, I915_UPLOAD_FOG);
709 i915->state.Fog[I915_FOGREG_MODE1] &= ~FMC1_FOGFUNC_MASK;
710 i915->state.Fog[I915_FOGREG_MODE1] |= FMC1_FOGFUNC_VERTEX;
711 i915->vertex_fog = I915_FOG_VERTEX;
712 }
713
714 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
715 I915_ACTIVESTATE(i915, I915_UPLOAD_FOG, enabled);
716 if (enabled)
717 i915->state.Ctx[I915_CTXREG_LIS5] |= S5_FOG_ENABLE;
718 else
719 i915->state.Ctx[I915_CTXREG_LIS5] &= ~S5_FOG_ENABLE;
720
721 /* Always enable pixel fog. Vertex fog using fog coord will conflict
722 * with fog code appended onto fragment program.
723 */
724 _tnl_allow_vertex_fog( ctx, 0 );
725 _tnl_allow_pixel_fog( ctx, 1 );
726 }
727
728 static void
729 i915Fogfv(GLcontext * ctx, GLenum pname, const GLfloat * param)
730 {
731 struct i915_context *i915 = I915_CONTEXT(ctx);
732
733 switch (pname) {
734 case GL_FOG_COORDINATE_SOURCE_EXT:
735 case GL_FOG_MODE:
736 case GL_FOG_START:
737 case GL_FOG_END:
738 break;
739
740 case GL_FOG_DENSITY:
741 I915_STATECHANGE(i915, I915_UPLOAD_FOG);
742
743 if (i915->state.Fog[I915_FOGREG_MODE1] & FMC1_FOGINDEX_Z) {
744 i915->state.Fog[I915_FOGREG_MODE3] =
745 (GLuint) (ctx->Fog.Density * FMC3_D_ONE);
746 }
747 else {
748 fi_type fi;
749 fi.f = ctx->Fog.Density;
750 i915->state.Fog[I915_FOGREG_MODE3] = fi.i;
751 }
752 break;
753
754 case GL_FOG_COLOR:
755 I915_STATECHANGE(i915, I915_UPLOAD_FOG);
756 i915->state.Fog[I915_FOGREG_COLOR] =
757 (_3DSTATE_FOG_COLOR_CMD |
758 ((GLubyte) (ctx->Fog.Color[0] * 255.0F) << 16) |
759 ((GLubyte) (ctx->Fog.Color[1] * 255.0F) << 8) |
760 ((GLubyte) (ctx->Fog.Color[2] * 255.0F) << 0));
761 break;
762
763 default:
764 break;
765 }
766 }
767
768 static void
769 i915Hint(GLcontext * ctx, GLenum target, GLenum state)
770 {
771 switch (target) {
772 case GL_FOG_HINT:
773 break;
774 default:
775 break;
776 }
777 }
778
779 /* =============================================================
780 */
781
782 static void
783 i915Enable(GLcontext * ctx, GLenum cap, GLboolean state)
784 {
785 struct i915_context *i915 = I915_CONTEXT(ctx);
786
787 switch (cap) {
788 case GL_TEXTURE_2D:
789 break;
790
791 case GL_LIGHTING:
792 case GL_COLOR_SUM:
793 update_specular(ctx);
794 break;
795
796 case GL_ALPHA_TEST:
797 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
798 if (state)
799 i915->state.Ctx[I915_CTXREG_LIS6] |= S6_ALPHA_TEST_ENABLE;
800 else
801 i915->state.Ctx[I915_CTXREG_LIS6] &= ~S6_ALPHA_TEST_ENABLE;
802 break;
803
804 case GL_BLEND:
805 i915EvalLogicOpBlendState(ctx);
806 break;
807
808 case GL_COLOR_LOGIC_OP:
809 i915EvalLogicOpBlendState(ctx);
810
811 /* Logicop doesn't seem to work at 16bpp:
812 */
813 if (ctx->Visual.rgbBits == 16)
814 FALLBACK(&i915->intel, I915_FALLBACK_LOGICOP, state);
815 break;
816
817 case GL_FRAGMENT_PROGRAM_ARB:
818 break;
819
820 case GL_DITHER:
821 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
822 if (state)
823 i915->state.Ctx[I915_CTXREG_LIS5] |= S5_COLOR_DITHER_ENABLE;
824 else
825 i915->state.Ctx[I915_CTXREG_LIS5] &= ~S5_COLOR_DITHER_ENABLE;
826 break;
827
828 case GL_DEPTH_TEST:
829 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
830 if (state)
831 i915->state.Ctx[I915_CTXREG_LIS6] |= S6_DEPTH_TEST_ENABLE;
832 else
833 i915->state.Ctx[I915_CTXREG_LIS6] &= ~S6_DEPTH_TEST_ENABLE;
834
835 i915DepthMask(ctx, ctx->Depth.Mask);
836 break;
837
838 case GL_SCISSOR_TEST:
839 I915_STATECHANGE(i915, I915_UPLOAD_BUFFERS);
840 if (state)
841 i915->state.Buffer[I915_DESTREG_SENABLE] =
842 (_3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT);
843 else
844 i915->state.Buffer[I915_DESTREG_SENABLE] =
845 (_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
846 break;
847
848 case GL_LINE_SMOOTH:
849 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
850 if (state)
851 i915->state.Ctx[I915_CTXREG_LIS4] |= S4_LINE_ANTIALIAS_ENABLE;
852 else
853 i915->state.Ctx[I915_CTXREG_LIS4] &= ~S4_LINE_ANTIALIAS_ENABLE;
854 break;
855
856 case GL_FOG:
857 break;
858
859 case GL_CULL_FACE:
860 i915CullFaceFrontFace(ctx, 0);
861 break;
862
863 case GL_STENCIL_TEST:
864 {
865 GLboolean hw_stencil = GL_FALSE;
866 if (ctx->DrawBuffer) {
867 struct intel_renderbuffer *irbStencil
868 = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_STENCIL);
869 hw_stencil = (irbStencil && irbStencil->region);
870 }
871 if (hw_stencil) {
872 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
873 if (state)
874 i915->state.Ctx[I915_CTXREG_LIS5] |= (S5_STENCIL_TEST_ENABLE |
875 S5_STENCIL_WRITE_ENABLE);
876 else
877 i915->state.Ctx[I915_CTXREG_LIS5] &= ~(S5_STENCIL_TEST_ENABLE |
878 S5_STENCIL_WRITE_ENABLE);
879 }
880 else {
881 FALLBACK(&i915->intel, I915_FALLBACK_STENCIL, state);
882 }
883 }
884 break;
885
886 case GL_POLYGON_STIPPLE:
887 /* The stipple command worked on my 855GM box, but not my 845G.
888 * I'll do more testing later to find out exactly which hardware
889 * supports it. Disabled for now.
890 */
891 if (i915->intel.hw_stipple &&
892 i915->intel.reduced_primitive == GL_TRIANGLES) {
893 I915_STATECHANGE(i915, I915_UPLOAD_STIPPLE);
894 if (state)
895 i915->state.Stipple[I915_STPREG_ST1] |= ST1_ENABLE;
896 else
897 i915->state.Stipple[I915_STPREG_ST1] &= ~ST1_ENABLE;
898 }
899 break;
900
901 case GL_POLYGON_SMOOTH:
902 break;
903
904 case GL_POINT_SMOOTH:
905 break;
906
907 default:
908 ;
909 }
910 }
911
912
913 static void
914 i915_init_packets(struct i915_context *i915)
915 {
916 /* Zero all state */
917 memset(&i915->state, 0, sizeof(i915->state));
918
919
920 {
921 I915_STATECHANGE(i915, I915_UPLOAD_CTX);
922 /* Probably don't want to upload all this stuff every time one
923 * piece changes.
924 */
925 i915->state.Ctx[I915_CTXREG_LI] = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 |
926 I1_LOAD_S(2) |
927 I1_LOAD_S(4) |
928 I1_LOAD_S(5) | I1_LOAD_S(6) | (3));
929 i915->state.Ctx[I915_CTXREG_LIS2] = 0;
930 i915->state.Ctx[I915_CTXREG_LIS4] = 0;
931 i915->state.Ctx[I915_CTXREG_LIS5] = 0;
932
933 if (i915->intel.ctx.Visual.rgbBits == 16)
934 i915->state.Ctx[I915_CTXREG_LIS5] |= S5_COLOR_DITHER_ENABLE;
935
936
937 i915->state.Ctx[I915_CTXREG_LIS6] = (S6_COLOR_WRITE_ENABLE |
938 (2 << S6_TRISTRIP_PV_SHIFT));
939
940 i915->state.Ctx[I915_CTXREG_STATE4] = (_3DSTATE_MODES_4_CMD |
941 ENABLE_LOGIC_OP_FUNC |
942 LOGIC_OP_FUNC(LOGICOP_COPY) |
943 ENABLE_STENCIL_TEST_MASK |
944 STENCIL_TEST_MASK(0xff) |
945 ENABLE_STENCIL_WRITE_MASK |
946 STENCIL_WRITE_MASK(0xff));
947
948 i915->state.Ctx[I915_CTXREG_IAB] =
949 (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD | IAB_MODIFY_ENABLE |
950 IAB_MODIFY_FUNC | IAB_MODIFY_SRC_FACTOR | IAB_MODIFY_DST_FACTOR);
951
952 i915->state.Ctx[I915_CTXREG_BLENDCOLOR0] =
953 _3DSTATE_CONST_BLEND_COLOR_CMD;
954 i915->state.Ctx[I915_CTXREG_BLENDCOLOR1] = 0;
955
956 }
957
958 {
959 I915_STATECHANGE(i915, I915_UPLOAD_STIPPLE);
960 i915->state.Stipple[I915_STPREG_ST0] = _3DSTATE_STIPPLE;
961 }
962
963
964 {
965 I915_STATECHANGE(i915, I915_UPLOAD_FOG);
966 i915->state.Fog[I915_FOGREG_MODE0] = _3DSTATE_FOG_MODE_CMD;
967 i915->state.Fog[I915_FOGREG_MODE1] = (FMC1_FOGFUNC_MODIFY_ENABLE |
968 FMC1_FOGFUNC_VERTEX |
969 FMC1_FOGINDEX_MODIFY_ENABLE |
970 FMC1_FOGINDEX_W |
971 FMC1_C1_C2_MODIFY_ENABLE |
972 FMC1_DENSITY_MODIFY_ENABLE);
973 i915->state.Fog[I915_FOGREG_COLOR] = _3DSTATE_FOG_COLOR_CMD;
974 }
975
976 {
977 i915->state.Buffer[I915_DESTREG_DV0] = _3DSTATE_DST_BUF_VARS_CMD;
978
979 /* scissor */
980 i915->state.Buffer[I915_DESTREG_SENABLE] =
981 (_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
982 i915->state.Buffer[I915_DESTREG_SR0] = _3DSTATE_SCISSOR_RECT_0_CMD;
983 i915->state.Buffer[I915_DESTREG_SR1] = 0;
984 i915->state.Buffer[I915_DESTREG_SR2] = 0;
985 }
986
987
988 #if 0
989 {
990 I915_STATECHANGE(i915, I915_UPLOAD_DEFAULTS);
991 i915->state.Default[I915_DEFREG_C0] = _3DSTATE_DEFAULT_DIFFUSE;
992 i915->state.Default[I915_DEFREG_C1] = 0;
993 i915->state.Default[I915_DEFREG_S0] = _3DSTATE_DEFAULT_SPECULAR;
994 i915->state.Default[I915_DEFREG_S1] = 0;
995 i915->state.Default[I915_DEFREG_Z0] = _3DSTATE_DEFAULT_Z;
996 i915->state.Default[I915_DEFREG_Z1] = 0;
997 }
998 #endif
999
1000
1001 /* These will be emitted every at the head of every buffer, unless
1002 * we get hardware contexts working.
1003 */
1004 i915->state.active = (I915_UPLOAD_PROGRAM |
1005 I915_UPLOAD_STIPPLE |
1006 I915_UPLOAD_CTX |
1007 I915_UPLOAD_BUFFERS | I915_UPLOAD_INVARIENT);
1008 }
1009
1010 void
1011 i915InitStateFunctions(struct dd_function_table *functions)
1012 {
1013 functions->AlphaFunc = i915AlphaFunc;
1014 functions->BlendColor = i915BlendColor;
1015 functions->BlendEquationSeparate = i915BlendEquationSeparate;
1016 functions->BlendFuncSeparate = i915BlendFuncSeparate;
1017 functions->ColorMask = i915ColorMask;
1018 functions->CullFace = i915CullFaceFrontFace;
1019 functions->DepthFunc = i915DepthFunc;
1020 functions->DepthMask = i915DepthMask;
1021 functions->Enable = i915Enable;
1022 functions->Fogfv = i915Fogfv;
1023 functions->FrontFace = i915CullFaceFrontFace;
1024 functions->Hint = i915Hint;
1025 functions->LightModelfv = i915LightModelfv;
1026 functions->LineWidth = i915LineWidth;
1027 functions->LogicOpcode = i915LogicOp;
1028 functions->PointSize = i915PointSize;
1029 functions->PolygonStipple = i915PolygonStipple;
1030 functions->Scissor = i915Scissor;
1031 functions->ShadeModel = i915ShadeModel;
1032 functions->StencilFuncSeparate = i915StencilFuncSeparate;
1033 functions->StencilMaskSeparate = i915StencilMaskSeparate;
1034 functions->StencilOpSeparate = i915StencilOpSeparate;
1035 functions->DepthRange = i915DepthRange;
1036 functions->Viewport = i915Viewport;
1037 }
1038
1039
1040 void
1041 i915InitState(struct i915_context *i915)
1042 {
1043 GLcontext *ctx = &i915->intel.ctx;
1044
1045 i915_init_packets(i915);
1046
1047 _mesa_init_driver_state(ctx);
1048
1049 memcpy(&i915->initial, &i915->state, sizeof(i915->state));
1050 i915->current = &i915->state;
1051 }