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