slang: initialize the context
[mesa.git] / src / mesa / drivers / dri / r300 / radeon_state.c
1 /**************************************************************************
2
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Keith Whitwell <keith@tungstengraphics.com>
34 */
35
36 #include "main/glheader.h"
37 #include "main/imports.h"
38 #include "main/api_arrayelt.h"
39 #include "main/enums.h"
40 #include "main/framebuffer.h"
41 #include "main/colormac.h"
42 #include "main/light.h"
43
44 #include "swrast/swrast.h"
45 #include "vbo/vbo.h"
46 #include "tnl/tnl.h"
47 #include "tnl/t_pipeline.h"
48 #include "swrast_setup/swrast_setup.h"
49
50 #include "radeon_ioctl.h"
51 #include "radeon_state.h"
52 #include "r300_ioctl.h"
53
54
55 /* =============================================================
56 * Scissoring
57 */
58
59 static GLboolean intersect_rect(drm_clip_rect_t * out,
60 drm_clip_rect_t * a, drm_clip_rect_t * b)
61 {
62 *out = *a;
63 if (b->x1 > out->x1)
64 out->x1 = b->x1;
65 if (b->y1 > out->y1)
66 out->y1 = b->y1;
67 if (b->x2 < out->x2)
68 out->x2 = b->x2;
69 if (b->y2 < out->y2)
70 out->y2 = b->y2;
71 if (out->x1 >= out->x2)
72 return GL_FALSE;
73 if (out->y1 >= out->y2)
74 return GL_FALSE;
75 return GL_TRUE;
76 }
77
78 void radeonRecalcScissorRects(radeonContextPtr radeon)
79 {
80 drm_clip_rect_t *out;
81 int i;
82
83 /* Grow cliprect store?
84 */
85 if (radeon->state.scissor.numAllocedClipRects < radeon->numClipRects) {
86 while (radeon->state.scissor.numAllocedClipRects <
87 radeon->numClipRects) {
88 radeon->state.scissor.numAllocedClipRects += 1; /* zero case */
89 radeon->state.scissor.numAllocedClipRects *= 2;
90 }
91
92 if (radeon->state.scissor.pClipRects)
93 FREE(radeon->state.scissor.pClipRects);
94
95 radeon->state.scissor.pClipRects =
96 MALLOC(radeon->state.scissor.numAllocedClipRects *
97 sizeof(drm_clip_rect_t));
98
99 if (radeon->state.scissor.pClipRects == NULL) {
100 radeon->state.scissor.numAllocedClipRects = 0;
101 return;
102 }
103 }
104
105 out = radeon->state.scissor.pClipRects;
106 radeon->state.scissor.numClipRects = 0;
107
108 for (i = 0; i < radeon->numClipRects; i++) {
109 if (intersect_rect(out,
110 &radeon->pClipRects[i],
111 &radeon->state.scissor.rect)) {
112 radeon->state.scissor.numClipRects++;
113 out++;
114 }
115 }
116 }
117
118 void radeonUpdateScissor(GLcontext* ctx)
119 {
120 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
121
122 if (radeon->dri.drawable) {
123 __DRIdrawablePrivate *dPriv = radeon->dri.drawable;
124 int x1 = dPriv->x + ctx->Scissor.X;
125 int y1 = dPriv->y + dPriv->h - (ctx->Scissor.Y + ctx->Scissor.Height);
126
127 radeon->state.scissor.rect.x1 = x1;
128 radeon->state.scissor.rect.y1 = y1;
129 radeon->state.scissor.rect.x2 = x1 + ctx->Scissor.Width;
130 radeon->state.scissor.rect.y2 = y1 + ctx->Scissor.Height;
131
132 radeonRecalcScissorRects(radeon);
133 }
134 }
135
136 static void radeonScissor(GLcontext* ctx, GLint x, GLint y, GLsizei w, GLsizei h)
137 {
138 if (ctx->Scissor.Enabled) {
139 /* We don't pipeline cliprect changes */
140 r300Flush(ctx);
141 radeonUpdateScissor(ctx);
142 }
143 }
144
145
146 /**
147 * Update cliprects and scissors.
148 */
149 void radeonSetCliprects(radeonContextPtr radeon)
150 {
151 __DRIdrawablePrivate *const drawable = radeon->dri.drawable;
152 __DRIdrawablePrivate *const readable = radeon->dri.readable;
153 GLframebuffer *const draw_fb = (GLframebuffer*)drawable->driverPrivate;
154 GLframebuffer *const read_fb = (GLframebuffer*)readable->driverPrivate;
155
156 if (draw_fb->_ColorDrawBufferIndexes[0] == BUFFER_BACK_LEFT) {
157 /* Can't ignore 2d windows if we are page flipping. */
158 if (drawable->numBackClipRects == 0 || radeon->doPageFlip ||
159 radeon->sarea->pfCurrentPage == 1) {
160 radeon->numClipRects = drawable->numClipRects;
161 radeon->pClipRects = drawable->pClipRects;
162 } else {
163 radeon->numClipRects = drawable->numBackClipRects;
164 radeon->pClipRects = drawable->pBackClipRects;
165 }
166 } else {
167 /* front buffer (or none, or multiple buffers */
168 radeon->numClipRects = drawable->numClipRects;
169 radeon->pClipRects = drawable->pClipRects;
170 }
171
172 if ((draw_fb->Width != drawable->w) ||
173 (draw_fb->Height != drawable->h)) {
174 _mesa_resize_framebuffer(radeon->glCtx, draw_fb,
175 drawable->w, drawable->h);
176 draw_fb->Initialized = GL_TRUE;
177 }
178
179 if (drawable != readable) {
180 if ((read_fb->Width != readable->w) ||
181 (read_fb->Height != readable->h)) {
182 _mesa_resize_framebuffer(radeon->glCtx, read_fb,
183 readable->w, readable->h);
184 read_fb->Initialized = GL_TRUE;
185 }
186 }
187
188 if (radeon->state.scissor.enabled)
189 radeonRecalcScissorRects(radeon);
190
191 radeon->lastStamp = drawable->lastStamp;
192 }
193
194
195 /**
196 * Handle common enable bits.
197 * Called as a fallback by r200Enable/r300Enable.
198 */
199 void radeonEnable(GLcontext* ctx, GLenum cap, GLboolean state)
200 {
201 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
202
203 switch(cap) {
204 case GL_SCISSOR_TEST:
205 /* We don't pipeline cliprect & scissor changes */
206 r300Flush(ctx);
207
208 radeon->state.scissor.enabled = state;
209 radeonUpdateScissor(ctx);
210 break;
211
212 default:
213 return;
214 }
215 }
216
217
218 /**
219 * Initialize default state.
220 * This function is called once at context init time from
221 * r200InitState/r300InitState
222 */
223 void radeonInitState(radeonContextPtr radeon)
224 {
225 radeon->Fallback = 0;
226
227 if (radeon->glCtx->Visual.doubleBufferMode && radeon->sarea->pfCurrentPage == 0) {
228 radeon->state.color.drawOffset = radeon->radeonScreen->backOffset;
229 radeon->state.color.drawPitch = radeon->radeonScreen->backPitch;
230 } else {
231 radeon->state.color.drawOffset = radeon->radeonScreen->frontOffset;
232 radeon->state.color.drawPitch = radeon->radeonScreen->frontPitch;
233 }
234 }
235
236
237 /**
238 * Initialize common state functions.
239 * Called by r200InitStateFuncs/r300InitStateFuncs
240 */
241 void radeonInitStateFuncs(struct dd_function_table *functions)
242 {
243 functions->Scissor = radeonScissor;
244 }