a59b1efc9ea4216697537fe38476bd9d822eb2b5
[mesa.git] / src / mesa / drivers / dri / r300 / r300_ioctl.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 **************************************************************************/
29
30 /*
31 * Authors:
32 * Keith Whitwell <keith@tungstengraphics.com>
33 * Nicolai Haehnle <prefect_@gmx.net>
34 */
35
36 #include <sched.h>
37 #include <errno.h>
38
39 #include "glheader.h"
40 #include "imports.h"
41 #include "macros.h"
42 #include "context.h"
43 #include "swrast/swrast.h"
44
45 #include "r300_context.h"
46 #include "radeon_ioctl.h"
47 #include "r300_ioctl.h"
48 #include "r300_cmdbuf.h"
49 #include "r300_state.h"
50 #include "r300_program.h"
51 #include "radeon_reg.h"
52
53 #include "vblank.h"
54
55
56 static void r300ClearColorBuffer(r300ContextPtr r300, int buffer)
57 {
58 GLcontext* ctx = r300->radeon.glCtx;
59 __DRIdrawablePrivate *dPriv = r300->radeon.dri.drawable;
60 int i;
61 GLuint cboffset, cbpitch;
62 drm_r300_cmd_header_t* cmd;
63
64 if (RADEON_DEBUG & DEBUG_IOCTL)
65 fprintf(stderr, "%s: %s buffer\n", __FUNCTION__,
66 buffer ? "back" : "front");
67
68 if (buffer) {
69 cboffset = r300->radeon.radeonScreen->backOffset;
70 cbpitch = r300->radeon.radeonScreen->backPitch;
71 } else {
72 cboffset = r300->radeon.radeonScreen->frontOffset;
73 cbpitch = r300->radeon.radeonScreen->frontPitch;
74 }
75 cboffset = r300->radeon.radeonScreen->fbLocation;
76
77 R300_STATECHANGE(r300, vpt);
78 r300->hw.vpt.cmd[R300_VPT_XSCALE] = r300PackFloat32(1.0);
79 r300->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(dPriv->x);
80 r300->hw.vpt.cmd[R300_VPT_YSCALE] = r300PackFloat32(1.0);
81 r300->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(dPriv->y);
82 r300->hw.vpt.cmd[R300_VPT_ZSCALE] = r300PackFloat32(1.0);
83 r300->hw.vpt.cmd[R300_VPT_ZOFFSET] = r300PackFloat32(0.0);
84
85 R300_STATECHANGE(r300, cb);
86 r300->hw.cb.cmd[R300_CB_OFFSET] = cboffset;
87 r300->hw.cb.cmd[R300_CB_PITCH] = cbpitch | R300_COLOR_UNKNOWN_22_23;
88
89 R300_STATECHANGE(r300, unk221C);
90 r300->hw.unk221C.cmd[1] = R300_221C_CLEAR;
91
92 R300_STATECHANGE(r300, ri);
93 for(i = 1; i <= 8; ++i)
94 r300->hw.ri.cmd[i] = R300_RS_INTERP_USED;
95
96 R300_STATECHANGE(r300, rr);
97 ((drm_r300_cmd_header_t*)r300->hw.rr.cmd)->unchecked_state.count = 1;
98 r300->hw.rr.cmd[1] = 0x00004000;
99
100 R300_STATECHANGE(r300, fp);
101 r300->hw.fp.cmd[R300_FP_CNTL0] = 0; /* 1 pass, no textures */
102 r300->hw.fp.cmd[R300_FP_CNTL1] = 0; /* no temporaries */
103 r300->hw.fp.cmd[R300_FP_CNTL2] = 0; /* no offset, one ALU instr */
104 r300->hw.fp.cmd[R300_FP_NODE0] = 0;
105 r300->hw.fp.cmd[R300_FP_NODE1] = 0;
106 r300->hw.fp.cmd[R300_FP_NODE2] = 0;
107 r300->hw.fp.cmd[R300_FP_NODE3] = R300_PFS_NODE_LAST_NODE;
108
109 R300_STATECHANGE(r300, fpi[0]);
110 R300_STATECHANGE(r300, fpi[1]);
111 R300_STATECHANGE(r300, fpi[2]);
112 R300_STATECHANGE(r300, fpi[3]);
113 ((drm_r300_cmd_header_t*)r300->hw.fpi[0].cmd)->unchecked_state.count = 1;
114 ((drm_r300_cmd_header_t*)r300->hw.fpi[1].cmd)->unchecked_state.count = 1;
115 ((drm_r300_cmd_header_t*)r300->hw.fpi[2].cmd)->unchecked_state.count = 1;
116 ((drm_r300_cmd_header_t*)r300->hw.fpi[3].cmd)->unchecked_state.count = 1;
117
118 /* MOV o0, t0 */
119 r300->hw.fpi[0].cmd[1] = FP_INSTRC(MAD, FP_ARGC(SRC0C_XYZ), FP_ARGC(ONE), FP_ARGC(ZERO));
120 r300->hw.fpi[1].cmd[1] = FP_SELC(0,NO,XYZ,FP_TMP(0),0,0);
121 r300->hw.fpi[2].cmd[1] = FP_INSTRA(MAD, FP_ARGA(SRC0A), FP_ARGA(ONE), FP_ARGA(ZERO));
122 r300->hw.fpi[3].cmd[1] = FP_SELA(0,NO,W,FP_TMP(0),0,0);
123
124 R300_STATECHANGE(r300, pvs);
125 r300->hw.pvs.cmd[R300_PVS_CNTL_1] =
126 (0 << R300_PVS_CNTL_1_PROGRAM_START_SHIFT) |
127 (0 << R300_PVS_CNTL_1_UNKNOWN_SHIFT) |
128 (1 << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
129 r300->hw.pvs.cmd[R300_PVS_CNTL_2] = 0; /* no parameters */
130 r300->hw.pvs.cmd[R300_PVS_CNTL_3] =
131 (1 << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT);
132
133 R300_STATECHANGE(r300, vpi);
134 ((drm_r300_cmd_header_t*)r300->hw.vpi.cmd)->unchecked_state.count = 8;
135
136 /* MOV o0, i0; */
137 r300->hw.vpi.cmd[1] = VP_OUT(ADD,OUT,0,XYZW);
138 r300->hw.vpi.cmd[2] = VP_IN(IN,0);
139 r300->hw.vpi.cmd[3] = VP_ZERO();
140 r300->hw.vpi.cmd[4] = 0;
141
142 /* MOV o1, i1; */
143 r300->hw.vpi.cmd[5] = VP_OUT(ADD,OUT,1,XYZW);
144 r300->hw.vpi.cmd[6] = VP_IN(IN,1);
145 r300->hw.vpi.cmd[7] = VP_ZERO();
146 r300->hw.vpi.cmd[8] = 0;
147
148 /* Make sure we have enough space */
149 r300EnsureCmdBufSpace(r300, r300->hw.max_state_size + 9, __FUNCTION__);
150
151 r300EmitState(r300);
152
153 cmd = (drm_r300_cmd_header_t*)r300AllocCmdBuf(r300, 9, __FUNCTION__);
154 cmd[0].packet3.cmd_type = R300_CMD_PACKET3;
155 cmd[0].packet3.packet = R300_CMD_PACKET3_CLEAR;
156 cmd[1].u = r300PackFloat32(dPriv->w / 2.0); /* my guess is as good as yours */
157 cmd[2].u = r300PackFloat32(dPriv->h / 2.0);
158 cmd[3].u = r300PackFloat32(0.0);
159 cmd[4].u = r300PackFloat32(1.0);
160 cmd[5].u = r300PackFloat32(ctx->Color.ClearColor[0]);
161 cmd[6].u = r300PackFloat32(ctx->Color.ClearColor[1]);
162 cmd[7].u = r300PackFloat32(ctx->Color.ClearColor[2]);
163 cmd[8].u = r300PackFloat32(ctx->Color.ClearColor[3]);
164 }
165
166
167 /**
168 * Buffer clear
169 */
170 static void r300Clear(GLcontext * ctx, GLbitfield mask, GLboolean all,
171 GLint cx, GLint cy, GLint cw, GLint ch)
172 {
173 r300ContextPtr r300 = R300_CONTEXT(ctx);
174 __DRIdrawablePrivate *dPriv = r300->radeon.dri.drawable;
175 int flags = 0;
176 int swapped;
177
178 if (RADEON_DEBUG & DEBUG_IOCTL)
179 fprintf(stderr, "%s: all=%d cx=%d cy=%d cw=%d ch=%d\n",
180 __FUNCTION__, all, cx, cy, cw, ch);
181
182 {
183 LOCK_HARDWARE(&r300->radeon);
184 UNLOCK_HARDWARE(&r300->radeon);
185 if (dPriv->numClipRects == 0)
186 return;
187 }
188
189 if (mask & DD_FRONT_LEFT_BIT) {
190 flags |= DD_FRONT_LEFT_BIT;
191 mask &= ~DD_FRONT_LEFT_BIT;
192 }
193
194 if (mask & DD_BACK_LEFT_BIT) {
195 flags |= DD_BACK_LEFT_BIT;
196 mask &= ~DD_BACK_LEFT_BIT;
197 }
198
199 if (mask) {
200 if (RADEON_DEBUG & DEBUG_FALLBACKS)
201 fprintf(stderr, "%s: swrast clear, mask: %x\n",
202 __FUNCTION__, mask);
203 _swrast_Clear(ctx, mask, all, cx, cy, cw, ch);
204 }
205
206 swapped = r300->radeon.doPageFlip && (r300->radeon.sarea->pfCurrentPage == 1);
207
208 if (flags & DD_FRONT_LEFT_BIT)
209 r300ClearColorBuffer(r300, swapped);
210
211 if (flags & DD_BACK_LEFT_BIT)
212 r300ClearColorBuffer(r300, swapped ^ 1);
213
214 /* Recalculate the hardware set. This could be done more efficiently,
215 * but do keep it like this for now.
216 */
217 r300ResetHwState(r300);
218 }
219
220 void r300Flush(GLcontext * ctx)
221 {
222 r300ContextPtr r300 = R300_CONTEXT(ctx);
223
224 if (RADEON_DEBUG & DEBUG_IOCTL)
225 fprintf(stderr, "%s\n", __FUNCTION__);
226
227 if (r300->cmdbuf.count_used > r300->cmdbuf.count_reemit)
228 r300FlushCmdBuf(r300, __FUNCTION__);
229 }
230
231 void r300InitIoctlFuncs(struct dd_function_table *functions)
232 {
233 functions->Clear = r300Clear;
234 functions->Finish = radeonFinish;
235 functions->Flush = r300Flush;
236 }