Fixed off by one errors in clipping.
[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 "glheader.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "enums.h"
33 #include "dd.h"
34
35 #include "intel_screen.h"
36 #include "intel_context.h"
37 #include "swrast/swrast.h"
38
39
40 static void intelDrawBuffer(GLcontext *ctx, GLenum mode )
41 {
42 intelContextPtr intel = INTEL_CONTEXT(ctx);
43 intelScreenPrivate *screen = intel->intelScreen;
44 int front = 0;
45
46 switch ( ctx->Color._DrawDestMask[0] ) {
47 case DD_FRONT_LEFT_BIT:
48 front = 1;
49 FALLBACK( intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE );
50 break;
51 case DD_BACK_LEFT_BIT:
52 front = 0;
53 FALLBACK( intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE );
54 break;
55 default:
56 FALLBACK( intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE );
57 return;
58 }
59
60 /* We want to update the s/w rast state too so that r200SetBuffer()
61 * gets called.
62 */
63 _swrast_DrawBuffer(ctx, mode);
64
65 if ( intel->sarea->pf_current_page == 1 )
66 front ^= 1;
67
68 intelSetFrontClipRects( intel );
69
70 if (front) {
71 intel->drawOffset = screen->frontOffset;
72 intel->drawMap = (char *)intel->driScreen->pFB;
73 intel->readMap = (char *)intel->driScreen->pFB;
74 } else {
75 intel->drawOffset = screen->backOffset;
76 intel->drawMap = screen->back.map;
77 intel->readMap = screen->back.map;
78 }
79
80 intel->vtbl.set_draw_offset( intel, intel->drawOffset );
81 }
82
83 static void intelReadBuffer( GLcontext *ctx, GLenum mode )
84 {
85 /* nothing, until we implement h/w glRead/CopyPixels or CopyTexImage */
86 }
87
88
89 static void intelClearColor(GLcontext *ctx, const GLfloat color[4])
90 {
91 intelContextPtr intel = INTEL_CONTEXT(ctx);
92 intelScreenPrivate *screen = intel->intelScreen;
93
94 CLAMPED_FLOAT_TO_UBYTE(intel->clear_red, color[0]);
95 CLAMPED_FLOAT_TO_UBYTE(intel->clear_green, color[1]);
96 CLAMPED_FLOAT_TO_UBYTE(intel->clear_blue, color[2]);
97 CLAMPED_FLOAT_TO_UBYTE(intel->clear_alpha, color[3]);
98
99 intel->ClearColor = INTEL_PACKCOLOR(screen->fbFormat,
100 intel->clear_red,
101 intel->clear_green,
102 intel->clear_blue,
103 intel->clear_alpha);
104 }
105
106
107 static void intelCalcViewport( GLcontext *ctx )
108 {
109 intelContextPtr intel = INTEL_CONTEXT(ctx);
110 const GLfloat *v = ctx->Viewport._WindowMap.m;
111 GLfloat *m = intel->ViewportMatrix.m;
112
113 /* See also intel_translate_vertex. SUBPIXEL adjustments can be done
114 * via state vars, too.
115 */
116 m[MAT_SX] = v[MAT_SX];
117 m[MAT_TX] = v[MAT_TX] + SUBPIXEL_X;
118 m[MAT_SY] = - v[MAT_SY];
119 m[MAT_TY] = - v[MAT_TY] + intel->driDrawable->h + SUBPIXEL_Y;
120 m[MAT_SZ] = v[MAT_SZ] * intel->depth_scale;
121 m[MAT_TZ] = v[MAT_TZ] * intel->depth_scale;
122 }
123
124 static void intelViewport( GLcontext *ctx,
125 GLint x, GLint y,
126 GLsizei width, GLsizei height )
127 {
128 intelCalcViewport( ctx );
129 }
130
131 static void intelDepthRange( GLcontext *ctx,
132 GLclampd nearval, GLclampd farval )
133 {
134 intelCalcViewport( ctx );
135 }
136
137 /* Fallback to swrast for select and feedback.
138 */
139 static void intelRenderMode( GLcontext *ctx, GLenum mode )
140 {
141 intelContextPtr intel = INTEL_CONTEXT(ctx);
142 FALLBACK( intel, INTEL_FALLBACK_RENDERMODE, (mode != GL_RENDER) );
143 }
144
145
146 void intelInitStateFuncs( struct dd_function_table *functions )
147 {
148 functions->DrawBuffer = intelDrawBuffer;
149 functions->ReadBuffer = intelReadBuffer;
150 functions->RenderMode = intelRenderMode;
151 functions->Viewport = intelViewport;
152 functions->DepthRange = intelDepthRange;
153 functions->ClearColor = intelClearColor;
154 }
155