Merge branch 'origin' into glsl-compiler-1
[mesa.git] / src / mesa / drivers / dri / savage / savageioctl.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29
30 #include "mtypes.h"
31 #include "macros.h"
32 #include "dd.h"
33 #include "context.h"
34 #include "swrast/swrast.h"
35 #include "colormac.h"
36
37 #include "mm.h"
38 #include "savagecontext.h"
39 #include "savageioctl.h"
40 #include "savage_bci.h"
41 #include "savagestate.h"
42 #include "savagespan.h"
43
44 #include "drm.h"
45 #include <sys/ioctl.h>
46 #include <sys/timeb.h>
47
48 #define DEPTH_SCALE_16 ((1<<16)-1)
49 #define DEPTH_SCALE_24 ((1<<24)-1)
50
51
52 void savageGetDMABuffer( savageContextPtr imesa )
53 {
54 int idx = 0;
55 int size = 0;
56 drmDMAReq dma;
57 int retcode;
58 drmBufPtr buf;
59
60 assert (imesa->savageScreen->bufs);
61
62 if (SAVAGE_DEBUG & DEBUG_DMA)
63 fprintf(stderr, "Getting dma buffer\n");
64
65 dma.context = imesa->hHWContext;
66 dma.send_count = 0;
67 dma.send_list = NULL;
68 dma.send_sizes = NULL;
69 dma.flags = 0;
70 dma.request_count = 1;
71 dma.request_size = imesa->bufferSize;
72 dma.request_list = &idx;
73 dma.request_sizes = &size;
74 dma.granted_count = 0;
75
76
77 if (SAVAGE_DEBUG & DEBUG_DMA)
78 fprintf(stderr, "drmDMA (get) ctx %d count %d size 0x%x\n",
79 dma.context, dma.request_count,
80 dma.request_size);
81
82 while (1) {
83 retcode = drmDMA(imesa->driFd, &dma);
84
85 if (SAVAGE_DEBUG & DEBUG_DMA)
86 fprintf(stderr, "retcode %d sz %d idx %d count %d\n",
87 retcode,
88 dma.request_sizes[0],
89 dma.request_list[0],
90 dma.granted_count);
91
92 if (retcode == 0 &&
93 dma.request_sizes[0] &&
94 dma.granted_count)
95 break;
96
97 if (SAVAGE_DEBUG & DEBUG_DMA)
98 fprintf(stderr, "\n\nflush");
99 }
100
101 buf = &(imesa->savageScreen->bufs->list[idx]);
102
103 if (SAVAGE_DEBUG & DEBUG_DMA)
104 fprintf(stderr,
105 "drmDMA (get) returns size[0] 0x%x idx[0] %d\n"
106 "dma_buffer now: buf idx: %d size: %d used: %d addr %p\n",
107 dma.request_sizes[0], dma.request_list[0],
108 buf->idx, buf->total,
109 buf->used, buf->address);
110
111 imesa->dmaVtxBuf.total = buf->total / 4;
112 imesa->dmaVtxBuf.used = 0;
113 imesa->dmaVtxBuf.flushed = 0;
114 imesa->dmaVtxBuf.idx = buf->idx;
115 imesa->dmaVtxBuf.buf = (u_int32_t *)buf->address;
116
117 if (SAVAGE_DEBUG & DEBUG_DMA)
118 fprintf(stderr, "finished getbuffer\n");
119 }
120
121 #if 0
122 /* Still keeping this around because it demonstrates page flipping and
123 * automatic z-clear. */
124 static void savage_BCI_clear(GLcontext *ctx, drm_savage_clear_t *pclear)
125 {
126 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
127 int nbox = imesa->sarea->nbox;
128 drm_clip_rect_t *pbox = imesa->sarea->boxes;
129 int i;
130
131
132 if (nbox > SAVAGE_NR_SAREA_CLIPRECTS)
133 nbox = SAVAGE_NR_SAREA_CLIPRECTS;
134
135 for (i = 0 ; i < nbox ; i++, pbox++) {
136 unsigned int x = pbox->x1;
137 unsigned int y = pbox->y1;
138 unsigned int width = pbox->x2 - x;
139 unsigned int height = pbox->y2 - y;
140 u_int32_t *bciptr;
141
142 if (pbox->x1 > pbox->x2 ||
143 pbox->y1 > pbox->y2 ||
144 pbox->x2 > imesa->savageScreen->width ||
145 pbox->y2 > imesa->savageScreen->height)
146 continue;
147
148 if ( pclear->flags & SAVAGE_FRONT ) {
149 bciptr = savageDMAAlloc (imesa, 8);
150 WRITE_CMD((bciptr) , 0x4BCC8C00,u_int32_t);
151 WRITE_CMD((bciptr) , imesa->savageScreen->frontOffset,u_int32_t);
152 WRITE_CMD((bciptr) , imesa->savageScreen->frontBitmapDesc,u_int32_t);
153 WRITE_CMD((bciptr) , pclear->clear_color,u_int32_t);
154 WRITE_CMD((bciptr) , (y <<16) | x,u_int32_t);
155 WRITE_CMD((bciptr) , (height << 16) | width,u_int32_t);
156 savageDMACommit (imesa, bciptr);
157 }
158 if ( pclear->flags & SAVAGE_BACK ) {
159 bciptr = savageDMAAlloc (imesa, 8);
160 WRITE_CMD((bciptr) , 0x4BCC8C00,u_int32_t);
161 WRITE_CMD((bciptr) , imesa->savageScreen->backOffset,u_int32_t);
162 WRITE_CMD((bciptr) , imesa->savageScreen->backBitmapDesc,u_int32_t);
163 WRITE_CMD((bciptr) , pclear->clear_color,u_int32_t);
164 WRITE_CMD((bciptr) , (y <<16) | x,u_int32_t);
165 WRITE_CMD((bciptr) , (height << 16) | width,u_int32_t);
166 savageDMACommit (imesa, bciptr);
167 }
168
169 if ( pclear->flags & (SAVAGE_DEPTH |SAVAGE_STENCIL) ) {
170 u_int32_t writeMask = 0x0;
171 if(imesa->hw_stencil)
172 {
173 if(pclear->flags & SAVAGE_STENCIL)
174 {
175
176 writeMask |= 0xFF000000;
177 }
178 if(pclear->flags & SAVAGE_DEPTH)
179 {
180 writeMask |= 0x00FFFFFF;
181 }
182 }
183 if(imesa->IsFullScreen && imesa->NotFirstFrame &&
184 imesa->savageScreen->chipset >= S3_SAVAGE4)
185 {
186 imesa->regs.s4.zBufCtrl.ni.autoZEnable = GL_TRUE;
187 imesa->regs.s4.zBufCtrl.ni.frameID =
188 ~imesa->regs.s4.zBufCtrl.ni.frameID;
189
190 imesa->dirty |= SAVAGE_UPLOAD_GLOBAL;
191 }
192 else
193 {
194 if(imesa->IsFullScreen)
195 imesa->NotFirstFrame = GL_TRUE;
196
197 if(imesa->hw_stencil)
198 {
199 bciptr = savageDMAAlloc (imesa, 10);
200 if(writeMask != 0xFFFFFFFF)
201 {
202 WRITE_CMD((bciptr) , 0x960100D7,u_int32_t);
203 WRITE_CMD((bciptr) , writeMask,u_int32_t);
204 }
205 }
206 else
207 {
208 bciptr = savageDMAAlloc (imesa, 6);
209 }
210
211 WRITE_CMD((bciptr) , 0x4BCC8C00,u_int32_t);
212 WRITE_CMD((bciptr) , imesa->savageScreen->depthOffset,u_int32_t);
213 WRITE_CMD((bciptr) , imesa->savageScreen->depthBitmapDesc,u_int32_t);
214 WRITE_CMD((bciptr) , pclear->clear_depth,u_int32_t);
215 WRITE_CMD((bciptr) , (y <<16) | x,u_int32_t);
216 WRITE_CMD((bciptr) , (height << 16) | width,u_int32_t);
217 if(imesa->hw_stencil)
218 {
219 if(writeMask != 0xFFFFFFFF)
220 {
221 WRITE_CMD((bciptr) , 0x960100D7,u_int32_t);
222 WRITE_CMD((bciptr) , 0xFFFFFFFF,u_int32_t);
223 }
224 }
225 savageDMACommit (imesa, bciptr);
226 }
227 }
228 }
229 /* FK: Make sure that the clear stuff is emitted. Otherwise a
230 software fallback may get overwritten by a delayed clear. */
231 savageDMAFlush (imesa);
232 }
233
234 static void savage_BCI_swap(savageContextPtr imesa)
235 {
236 int nbox = imesa->sarea->nbox;
237 drm_clip_rect_t *pbox = imesa->sarea->boxes;
238 int i;
239 volatile u_int32_t *bciptr;
240
241 if (nbox > SAVAGE_NR_SAREA_CLIPRECTS)
242 nbox = SAVAGE_NR_SAREA_CLIPRECTS;
243 savageDMAFlush (imesa);
244
245 if(imesa->IsFullScreen)
246 { /* full screen*/
247 unsigned int tmp0;
248 tmp0 = imesa->savageScreen->frontOffset;
249 imesa->savageScreen->frontOffset = imesa->savageScreen->backOffset;
250 imesa->savageScreen->backOffset = tmp0;
251
252 if(imesa->toggle == TARGET_BACK)
253 imesa->toggle = TARGET_FRONT;
254 else
255 imesa->toggle = TARGET_BACK;
256
257 driFlipRenderbuffers(imesa->glCtx->DrawBuffer,
258 imesa->toggle != TARGET_FRONT);
259
260 imesa->regs.s4.destCtrl.ni.offset = imesa->savageScreen->backOffset>>11;
261 imesa->dirty |= SAVAGE_UPLOAD_GLOBAL;
262 bciptr = SAVAGE_GET_BCI_POINTER(imesa,3);
263 *(bciptr) = 0x960100B0;
264 *(bciptr) = (imesa->savageScreen->frontOffset);
265 *(bciptr) = 0xA0000000;
266 }
267
268 else
269 { /* Use bitblt copy from back to front buffer*/
270
271 for (i = 0 ; i < nbox; i++, pbox++)
272 {
273 unsigned int w = pbox->x2 - pbox->x1;
274 unsigned int h = pbox->y2 - pbox->y1;
275
276 if (pbox->x1 > pbox->x2 ||
277 pbox->y1 > pbox->y2 ||
278 pbox->x2 > imesa->savageScreen->width ||
279 pbox->y2 > imesa->savageScreen->height)
280 continue;
281
282 bciptr = SAVAGE_GET_BCI_POINTER(imesa,6);
283
284 *(bciptr) = 0x4BCC00C0;
285
286 *(bciptr) = imesa->savageScreen->backOffset;
287 *(bciptr) = imesa->savageScreen->backBitmapDesc;
288 *(bciptr) = (pbox->y1 <<16) | pbox->x1; /*x0, y0*/
289 *(bciptr) = (pbox->y1 <<16) | pbox->x1;
290 *(bciptr) = (h << 16) | w;
291 }
292
293 }
294 }
295 #endif
296
297
298 static GLboolean intersect_rect( drm_clip_rect_t *out,
299 const drm_clip_rect_t *a,
300 const drm_clip_rect_t *b )
301 {
302 *out = *a;
303 if (b->x1 > out->x1) out->x1 = b->x1;
304 if (b->y1 > out->y1) out->y1 = b->y1;
305 if (b->x2 < out->x2) out->x2 = b->x2;
306 if (b->y2 < out->y2) out->y2 = b->y2;
307
308 return ((out->x1 < out->x2) && (out->y1 < out->y2));
309 }
310
311
312 static GLuint savageIntersectClipRects(drm_clip_rect_t *dest,
313 const drm_clip_rect_t *src,
314 GLuint nsrc,
315 const drm_clip_rect_t *clip)
316 {
317 GLuint i, ndest;
318
319 for (i = 0, ndest = 0; i < nsrc; ++i, ++src) {
320 if (intersect_rect(dest, src, clip)) {
321 dest++;
322 ndest++;
323 }
324 }
325
326 return ndest;
327 }
328
329
330 static void savageDDClear( GLcontext *ctx, GLbitfield mask )
331 {
332 savageContextPtr imesa = SAVAGE_CONTEXT( ctx );
333 GLuint colorMask, depthMask, clearColor, clearDepth, flags;
334 GLint cx = ctx->DrawBuffer->_Xmin;
335 GLint cy = ctx->DrawBuffer->_Ymin;
336 GLint cw = ctx->DrawBuffer->_Xmax - cx;
337 GLint ch = ctx->DrawBuffer->_Ymax - cy;
338
339 /* XXX FIX ME: the cx,cy,cw,ch vars are currently ignored! */
340
341 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
342 fprintf (stderr, "%s\n", __FUNCTION__);
343
344 clearColor = imesa->ClearColor;
345 if (imesa->float_depth) {
346 if (imesa->savageScreen->zpp == 2)
347 clearDepth = savageEncodeFloat16(1.0 - ctx->Depth.Clear);
348 else
349 clearDepth = savageEncodeFloat24(1.0 - ctx->Depth.Clear);
350 } else {
351 if (imesa->savageScreen->zpp == 2)
352 clearDepth = (GLuint) ((1.0 - ctx->Depth.Clear) * DEPTH_SCALE_16);
353 else
354 clearDepth = (GLuint) ((1.0 - ctx->Depth.Clear) * DEPTH_SCALE_24);
355 }
356
357 colorMask = 0;
358 depthMask = 0;
359 switch (imesa->savageScreen->cpp) {
360 case 2:
361 colorMask = PACK_COLOR_565(ctx->Color.ColorMask[0],
362 ctx->Color.ColorMask[1],
363 ctx->Color.ColorMask[2]);
364 break;
365 case 4:
366 colorMask = PACK_COLOR_8888(ctx->Color.ColorMask[3],
367 ctx->Color.ColorMask[2],
368 ctx->Color.ColorMask[1],
369 ctx->Color.ColorMask[0]);
370 break;
371 }
372
373 flags = 0;
374
375 if (mask & BUFFER_BIT_FRONT_LEFT) {
376 flags |= SAVAGE_FRONT;
377 mask &= ~BUFFER_BIT_FRONT_LEFT;
378 }
379
380 if (mask & BUFFER_BIT_BACK_LEFT) {
381 flags |= SAVAGE_BACK;
382 mask &= ~BUFFER_BIT_BACK_LEFT;
383 }
384
385 if ((mask & BUFFER_BIT_DEPTH) && ctx->Depth.Mask) {
386 flags |= SAVAGE_DEPTH;
387 depthMask |=
388 (imesa->savageScreen->zpp == 2) ? 0xffffffff : 0x00ffffff;
389 mask &= ~BUFFER_BIT_DEPTH;
390 }
391
392 if((mask & BUFFER_BIT_STENCIL) && imesa->hw_stencil)
393 {
394 flags |= SAVAGE_DEPTH;
395 depthMask |= 0xff000000;
396 mask &= ~BUFFER_BIT_STENCIL;
397 }
398
399 savageFlushVertices(imesa);
400
401 if (flags) {
402 GLboolean depthCleared = GL_FALSE;
403 if (flags & (SAVAGE_FRONT|SAVAGE_BACK)) {
404 drm_savage_cmd_header_t *cmd;
405 cmd = savageAllocCmdBuf(imesa, sizeof(drm_savage_cmd_header_t));
406 cmd[0].clear0.cmd = SAVAGE_CMD_CLEAR;
407 if ((flags & SAVAGE_DEPTH) &&
408 clearDepth == clearColor && depthMask == colorMask) {
409 cmd[0].clear0.flags = flags;
410 depthCleared = GL_TRUE;
411 } else
412 cmd[0].clear0.flags = flags & (SAVAGE_FRONT|SAVAGE_BACK);
413 cmd[1].clear1.mask = colorMask;
414 cmd[1].clear1.value = clearColor;
415 }
416
417 if ((flags & SAVAGE_DEPTH) && !depthCleared) {
418 drm_savage_cmd_header_t *cmd;
419 cmd = savageAllocCmdBuf(imesa, sizeof(drm_savage_cmd_header_t));
420 cmd[0].clear0.cmd = SAVAGE_CMD_CLEAR;
421 cmd[0].clear0.flags = SAVAGE_DEPTH;
422 cmd[1].clear1.mask = depthMask;
423 cmd[1].clear1.value = clearDepth;
424 }
425 }
426
427 if (mask)
428 _swrast_Clear( ctx, mask );
429 }
430
431 /*
432 * Copy the back buffer to the front buffer.
433 */
434 void savageSwapBuffers( __DRIdrawablePrivate *dPriv )
435 {
436 savageContextPtr imesa;
437
438 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
439 fprintf (stderr, "%s\n================================\n", __FUNCTION__);
440
441 assert(dPriv);
442 assert(dPriv->driContextPriv);
443 assert(dPriv->driContextPriv->driverPrivate);
444
445 imesa = (savageContextPtr) dPriv->driContextPriv->driverPrivate;
446 if (imesa->IsDouble)
447 _mesa_notifySwapBuffers( imesa->glCtx );
448
449 FLUSH_BATCH(imesa);
450
451 if (imesa->sync_frames)
452 imesa->lastSwap = savageEmitEvent( imesa, 0 );
453
454 if (imesa->lastSwap != 0)
455 savageWaitEvent( imesa, imesa->lastSwap );
456
457 {
458 drm_savage_cmd_header_t *cmd = savageAllocCmdBuf(imesa, 0);
459 cmd->cmd.cmd = SAVAGE_CMD_SWAP;
460 imesa->inSwap = GL_TRUE; /* ignore scissors in savageFlushCmdBuf */
461 savageFlushCmdBuf(imesa, GL_FALSE);
462 imesa->inSwap = GL_FALSE;
463 }
464
465 if (!imesa->sync_frames)
466 /* don't sync, but limit the lag to one frame. */
467 imesa->lastSwap = savageEmitEvent( imesa, 0 );
468 }
469
470 unsigned int savageEmitEventLocked( savageContextPtr imesa, unsigned int flags )
471 {
472 drm_savage_event_emit_t event;
473 int ret;
474 event.count = 0;
475 event.flags = flags;
476 ret = drmCommandWriteRead( imesa->driFd, DRM_SAVAGE_BCI_EVENT_EMIT,
477 &event, sizeof(event) );
478 if (ret) {
479 fprintf (stderr, "emit event returned %d\n", ret);
480 exit (1);
481 }
482 return event.count;
483 }
484 unsigned int savageEmitEvent( savageContextPtr imesa, unsigned int flags )
485 {
486 unsigned int ret;
487 LOCK_HARDWARE( imesa );
488 ret = savageEmitEventLocked( imesa, flags );
489 UNLOCK_HARDWARE( imesa );
490 return ret;
491 }
492
493
494 void savageWaitEvent( savageContextPtr imesa, unsigned int count )
495 {
496 drm_savage_event_wait_t event;
497 int ret;
498 event.count = count;
499 event.flags = 0;
500 ret = drmCommandWriteRead( imesa->driFd, DRM_SAVAGE_BCI_EVENT_WAIT,
501 &event, sizeof(event) );
502 if (ret) {
503 fprintf (stderr, "wait event returned %d\n", ret);
504 exit (1);
505 }
506 }
507
508
509 void savageFlushVertices( savageContextPtr imesa )
510 {
511 struct savage_vtxbuf_t *buffer = imesa->vtxBuf;
512
513 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
514 fprintf (stderr, "%s\n", __FUNCTION__);
515
516 if (!buffer->total)
517 return;
518
519 if (buffer->used > buffer->flushed) {
520 drm_savage_cmd_header_t *cmd;
521 /* State must be updated "per primitive" because hardware
522 * culling must be disabled for unfilled primitives, points
523 * and lines. */
524 savageEmitChangedState (imesa);
525 cmd = savageAllocCmdBuf(imesa, 0);
526 cmd->prim.cmd = buffer == &imesa->dmaVtxBuf ?
527 SAVAGE_CMD_DMA_PRIM : SAVAGE_CMD_VB_PRIM;
528 cmd->prim.prim = imesa->HwPrim;
529 cmd->prim.skip = imesa->skip;
530 cmd->prim.start = buffer->flushed / imesa->HwVertexSize;
531 cmd->prim.count = buffer->used / imesa->HwVertexSize - cmd->prim.start;
532 buffer->flushed = buffer->used;
533 }
534 }
535
536 void savageFlushCmdBufLocked( savageContextPtr imesa, GLboolean discard )
537 {
538 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
539
540 if (!imesa->dmaVtxBuf.total)
541 discard = GL_FALSE;
542
543 /* complete indexed drawing commands */
544 savageFlushElts(imesa);
545
546 if (imesa->cmdBuf.write != imesa->cmdBuf.start || discard) {
547 drm_savage_cmdbuf_t cmdbuf;
548 drm_savage_cmd_header_t *start;
549 int ret;
550
551 /* If we lost the context we must restore the initial state (at
552 * the start of the command buffer). */
553 if (imesa->lostContext) {
554 start = imesa->cmdBuf.base;
555 imesa->lostContext = GL_FALSE;
556 } else
557 start = imesa->cmdBuf.start;
558
559 if ((SAVAGE_DEBUG & DEBUG_DMA) && discard)
560 fprintf (stderr, "Discarding DMA buffer, used=%u\n",
561 imesa->dmaVtxBuf.used);
562
563 cmdbuf.dma_idx = imesa->dmaVtxBuf.idx;
564 cmdbuf.discard = discard;
565 cmdbuf.vb_addr = imesa->clientVtxBuf.buf;
566 cmdbuf.vb_size = imesa->clientVtxBuf.total*4;
567 cmdbuf.vb_stride = imesa->HwVertexSize;
568 cmdbuf.cmd_addr = start;
569 cmdbuf.size = (imesa->cmdBuf.write - start);
570 if (!imesa->inSwap && imesa->scissor.enabled) {
571 drm_clip_rect_t *box = dPriv->pClipRects, *ibox;
572 drm_clip_rect_t scissor;
573 GLuint nbox = dPriv->numClipRects, nibox;
574 /* transform and clip scissor to viewport */
575 scissor.x1 = MAX2(imesa->scissor.x, 0) + dPriv->x;
576 scissor.y1 = MAX2(dPriv->h - imesa->scissor.y - imesa->scissor.h,
577 0) + dPriv->y;
578 scissor.x2 = MIN2(imesa->scissor.x + imesa->scissor.w,
579 dPriv->w) + dPriv->x;
580 scissor.y2 = MIN2(dPriv->h - imesa->scissor.y,
581 dPriv->h) + dPriv->y;
582 /* intersect cliprects with scissor */
583 ibox = malloc(dPriv->numClipRects*sizeof(drm_clip_rect_t));
584 if (!ibox) {
585 fprintf(stderr, "Out of memory.\n");
586 exit(1);
587 }
588 nibox = savageIntersectClipRects(ibox, box, nbox, &scissor);
589 cmdbuf.nbox = nibox;
590 cmdbuf.box_addr = ibox;
591 } else {
592 cmdbuf.nbox = dPriv->numClipRects;
593 cmdbuf.box_addr = dPriv->pClipRects;
594 }
595
596 ret = drmCommandWrite( imesa->driFd, DRM_SAVAGE_BCI_CMDBUF,
597 &cmdbuf, sizeof(cmdbuf) );
598 if (ret) {
599 fprintf (stderr, "cmdbuf ioctl returned %d\n", ret);
600 exit(1);
601 }
602
603 if (cmdbuf.box_addr != dPriv->pClipRects) {
604 free(cmdbuf.box_addr);
605 }
606
607 /* Save the current state at the start of the command buffer. That
608 * state will only be emitted, if the context was lost since the
609 * last command buffer. */
610 imesa->cmdBuf.write = imesa->cmdBuf.base;
611 savageEmitOldState(imesa);
612 imesa->cmdBuf.start = imesa->cmdBuf.write;
613 }
614
615 if (discard) {
616 assert (!savageHaveIndexedVerts(imesa));
617 imesa->dmaVtxBuf.total = 0;
618 imesa->dmaVtxBuf.used = 0;
619 imesa->dmaVtxBuf.flushed = 0;
620 }
621 if (!savageHaveIndexedVerts(imesa)) {
622 imesa->clientVtxBuf.used = 0;
623 imesa->clientVtxBuf.flushed = 0;
624 }
625 }
626
627
628 void savageFlushCmdBuf( savageContextPtr imesa, GLboolean discard )
629 {
630 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
631 fprintf (stderr, "%s\n", __FUNCTION__);
632 LOCK_HARDWARE(imesa);
633 savageFlushCmdBufLocked (imesa, discard);
634 UNLOCK_HARDWARE(imesa);
635 }
636
637
638 static void savageDDFlush( GLcontext *ctx )
639 {
640 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
641 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
642 fprintf (stderr, "%s\n", __FUNCTION__);
643 savageFlushVertices (imesa);
644 savageFlushCmdBuf(imesa, GL_FALSE);
645 }
646
647 static void savageDDFinish( GLcontext *ctx )
648 {
649 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
650 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
651 fprintf (stderr, "%s\n", __FUNCTION__);
652 savageFlushVertices (imesa);
653 savageFlushCmdBuf(imesa, GL_FALSE);
654 WAIT_IDLE_EMPTY(imesa);
655 }
656
657 void savageDDInitIoctlFuncs( GLcontext *ctx )
658 {
659 ctx->Driver.Clear = savageDDClear;
660 ctx->Driver.Flush = savageDDFlush;
661 ctx->Driver.Finish = savageDDFinish;
662 }