summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/r300/radeon_state.c
blob: 0cacbb5ebf5e4d16a0c0016b93b0bf46f36b1005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**************************************************************************

Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.

The Weather Channel (TM) funded Tungsten Graphics to develop the
initial release of the Radeon 8500 driver under the XFree86 license.
This notice must be preserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice (including the
next paragraph) shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*
 * Authors:
 *   Keith Whitwell <keith@tungstengraphics.com>
 */

#include "glheader.h"
#include "imports.h"
#include "api_arrayelt.h"
#include "enums.h"
#include "colormac.h"
#include "light.h"

#include "swrast/swrast.h"
#include "array_cache/acache.h"
#include "tnl/tnl.h"
#include "tnl/t_pipeline.h"
#include "swrast_setup/swrast_setup.h"

#include "r200_context.h"
#include "radeon_ioctl.h"
#include "radeon_state.h"
#include "r200_state.h"
#include "r300_ioctl.h"


/* =============================================================
 * Scissoring
 */

static GLboolean intersect_rect(drm_clip_rect_t * out,
				drm_clip_rect_t * a, drm_clip_rect_t * b)
{
	*out = *a;
	if (b->x1 > out->x1)
		out->x1 = b->x1;
	if (b->y1 > out->y1)
		out->y1 = b->y1;
	if (b->x2 < out->x2)
		out->x2 = b->x2;
	if (b->y2 < out->y2)
		out->y2 = b->y2;
	if (out->x1 >= out->x2)
		return GL_FALSE;
	if (out->y1 >= out->y2)
		return GL_FALSE;
	return GL_TRUE;
}

void radeonRecalcScissorRects(radeonContextPtr radeon)
{
	drm_clip_rect_t *out;
	int i;

	/* Grow cliprect store?
	 */
	if (radeon->state.scissor.numAllocedClipRects < radeon->numClipRects) {
		while (radeon->state.scissor.numAllocedClipRects <
		       radeon->numClipRects) {
			radeon->state.scissor.numAllocedClipRects += 1;	/* zero case */
			radeon->state.scissor.numAllocedClipRects *= 2;
		}

		if (radeon->state.scissor.pClipRects)
			FREE(radeon->state.scissor.pClipRects);

		radeon->state.scissor.pClipRects =
		    MALLOC(radeon->state.scissor.numAllocedClipRects *
			   sizeof(drm_clip_rect_t));

		if (radeon->state.scissor.pClipRects == NULL) {
			radeon->state.scissor.numAllocedClipRects = 0;
			return;
		}
	}

	out = radeon->state.scissor.pClipRects;
	radeon->state.scissor.numClipRects = 0;

	for (i = 0; i < radeon->numClipRects; i++) {
		if (intersect_rect(out,
				   &radeon->pClipRects[i],
				   &radeon->state.scissor.rect)) {
			radeon->state.scissor.numClipRects++;
			out++;
		}
	}
}

void radeonUpdateScissor(GLcontext* ctx)
{
	radeonContextPtr radeon = RADEON_CONTEXT(ctx);

	assert(radeon->state.scissor.enabled == ctx->Scissor.Enabled);

	if (radeon->dri.drawable) {
		__DRIdrawablePrivate *dPriv = radeon->dri.drawable;
		int x1 = dPriv->x + ctx->Scissor.X;
		int y1 = dPriv->y + dPriv->h - (ctx->Scissor.Y + ctx->Scissor.Height);

		radeon->state.scissor.rect.x1 = x1;
		radeon->state.scissor.rect.y1 = y1;
		radeon->state.scissor.rect.x2 = x1 + ctx->Scissor.Width - 1;
		radeon->state.scissor.rect.y2 = y1 + ctx->Scissor.Height - 1;

		radeonRecalcScissorRects(radeon);
	}
}

static void radeonScissor(GLcontext* ctx, GLint x, GLint y, GLsizei w, GLsizei h)
{
	radeonContextPtr radeon = RADEON_CONTEXT(ctx);

	if (ctx->Scissor.Enabled) {
		/* We don't pipeline cliprect changes */
		if (IS_FAMILY_R200(radeon))
			R200_FIREVERTICES((r200ContextPtr)radeon);
		else
			r300Flush(ctx);

		radeonUpdateScissor(ctx);
	}
}


/**
 * Update cliprects and scissors.
 */
void radeonSetCliprects(radeonContextPtr radeon, GLenum mode)
{
	__DRIdrawablePrivate *dPriv = radeon->dri.drawable;

	switch (mode) {
	case GL_FRONT_LEFT:
		radeon->numClipRects = dPriv->numClipRects;
		radeon->pClipRects = dPriv->pClipRects;
		break;
	case GL_BACK_LEFT:
		/* Can't ignore 2d windows if we are page flipping.
		 */
		if (dPriv->numBackClipRects == 0 || radeon->doPageFlip) {
			radeon->numClipRects = dPriv->numClipRects;
			radeon->pClipRects = dPriv->pClipRects;
		} else {
			radeon->numClipRects = dPriv->numBackClipRects;
			radeon->pClipRects = dPriv->pBackClipRects;
		}
		break;
	default:
		fprintf(stderr, "bad mode in radeonSetCliprects\n");
		radeon->numClipRects = 0;
		radeon->pClipRects = 0;
		return;
	}

	if (radeon->state.scissor.enabled)
		radeonRecalcScissorRects(radeon);
}


/**
 * Handle common enable bits.
 * Called as a fallback by r200Enable/r300Enable.
 */
void radeonEnable(GLcontext* ctx, GLenum cap, GLboolean state)
{
	radeonContextPtr radeon = RADEON_CONTEXT(ctx);

	switch(cap) {
	case GL_SCISSOR_TEST:
		/* We don't pipeline cliprect & scissor changes */
		if (IS_FAMILY_R200(radeon))
			R200_FIREVERTICES((r200ContextPtr)radeon);
		else
			r300Flush(ctx);

		radeon->state.scissor.enabled = state;
		radeonUpdateScissor(ctx);
		break;

	default:
		return;
	}
}


/**
 * Initialize default state.
 * This function is called once at context init time from
 * r200InitState/r300InitState
 */
void radeonInitState(radeonContextPtr radeon)
{
	radeon->Fallback = 0;

	if (radeon->glCtx->Visual.doubleBufferMode && radeon->sarea->pfCurrentPage == 0) {
		radeon->state.color.drawOffset = radeon->radeonScreen->backOffset;
		radeon->state.color.drawPitch = radeon->radeonScreen->backPitch;
	} else {
		radeon->state.color.drawOffset = radeon->radeonScreen->frontOffset;
		radeon->state.color.drawPitch = radeon->radeonScreen->frontPitch;
	}

	radeon->state.pixel.readOffset = radeon->state.color.drawOffset;
	radeon->state.pixel.readPitch = radeon->state.color.drawPitch;
}


/**
 * Initialize common state functions.
 * Called by r200InitStateFuncs/r300InitStateFuncs
 */
void radeonInitStateFuncs(struct dd_function_table *functions)
{
	functions->Scissor = radeonScissor;
}