Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / mesa / drivers / dri / savage / savageioctl.h
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 #ifndef SAVAGE_IOCTL_H
27 #define SAVAGE_IOCTL_H
28
29 #include "savagecontext.h"
30
31 void savageFlushVertices( savageContextPtr mmesa );
32
33 unsigned int savageEmitEventLocked( savageContextPtr imesa, unsigned int flags );
34 unsigned int savageEmitEvent( savageContextPtr imesa, unsigned int flags );
35 void savageWaitEvent( savageContextPtr imesa, unsigned int event);
36
37 void savageFlushCmdBufLocked( savageContextPtr imesa, GLboolean discard );
38 void savageFlushCmdBuf( savageContextPtr imesa, GLboolean discard );
39
40 void savageDDInitIoctlFuncs( struct gl_context *ctx );
41
42 void savageSwapBuffers( __DRIdrawable *dPriv );
43
44 #define WAIT_IDLE_EMPTY(imesa) do { \
45 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG) \
46 fprintf (stderr, "WAIT_IDLE_EMPTY in %s\n", __FUNCTION__); \
47 savageWaitEvent(imesa, \
48 savageEmitEvent(imesa, SAVAGE_WAIT_2D|SAVAGE_WAIT_3D)); \
49 } while (0)
50
51 #define WAIT_IDLE_EMPTY_LOCKED(imesa) do { \
52 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG) \
53 fprintf (stderr, "WAIT_IDLE_EMPTY_LOCKED in %s\n", __FUNCTION__); \
54 savageWaitEvent(imesa, savageEmitEventLocked( \
55 imesa, SAVAGE_WAIT_2D|SAVAGE_WAIT_3D)); \
56 } while (0)
57
58 #define FLUSH_BATCH(imesa) do { \
59 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG) \
60 fprintf (stderr, "FLUSH_BATCH in %s\n", __FUNCTION__); \
61 savageFlushVertices(imesa); \
62 savageFlushCmdBuf(imesa, GL_FALSE); \
63 } while (0)
64
65 extern void savageGetDMABuffer( savageContextPtr imesa );
66
67 static INLINE
68 void savageReleaseIndexedVerts( savageContextPtr imesa )
69 {
70 imesa->firstElt = -1;
71 }
72
73 static INLINE
74 GLboolean savageHaveIndexedVerts( savageContextPtr imesa )
75 {
76 return (imesa->firstElt != -1);
77 }
78
79 static INLINE
80 uint32_t *savageAllocVtxBuf( savageContextPtr imesa, GLuint words )
81 {
82 struct savage_vtxbuf_t *buffer = imesa->vtxBuf;
83 uint32_t *head;
84
85 if (buffer == &imesa->dmaVtxBuf) {
86 if (!buffer->total) {
87 LOCK_HARDWARE(imesa);
88 savageGetDMABuffer(imesa);
89 UNLOCK_HARDWARE(imesa);
90 } else if (buffer->used + words > buffer->total) {
91 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
92 fprintf (stderr, "... flushing DMA buffer in %s\n",
93 __FUNCTION__);
94 savageReleaseIndexedVerts(imesa);
95 savageFlushVertices(imesa);
96 LOCK_HARDWARE(imesa);
97 savageFlushCmdBufLocked(imesa, GL_TRUE); /* discard DMA buffer */
98 savageGetDMABuffer(imesa);
99 UNLOCK_HARDWARE(imesa);
100 }
101 } else if (buffer->used + words > buffer->total) {
102 if (SAVAGE_DEBUG & DEBUG_VERBOSE_MSG)
103 fprintf (stderr, "... flushing client vertex buffer in %s\n",
104 __FUNCTION__);
105 savageReleaseIndexedVerts(imesa);
106 savageFlushVertices(imesa);
107 LOCK_HARDWARE(imesa);
108 savageFlushCmdBufLocked(imesa, GL_FALSE); /* free clientVtxBuf */
109 UNLOCK_HARDWARE(imesa);
110 }
111
112 head = &buffer->buf[buffer->used];
113
114 buffer->used += words;
115 return head;
116 }
117
118 static INLINE
119 uint32_t *savageAllocIndexedVerts( savageContextPtr imesa, GLuint n )
120 {
121 uint32_t *ret;
122 savageFlushVertices(imesa);
123 ret = savageAllocVtxBuf(imesa, n*imesa->HwVertexSize);
124 imesa->firstElt = imesa->vtxBuf->flushed / imesa->HwVertexSize;
125 imesa->vtxBuf->flushed = imesa->vtxBuf->used;
126 return ret;
127 }
128
129 /* Flush Elts:
130 * - Complete the drawing command with the correct number of indices.
131 * - Actually allocate entries for the indices in the command buffer.
132 * (This allocation must succeed without wrapping the cmd buffer!)
133 */
134 static INLINE
135 void savageFlushElts( savageContextPtr imesa )
136 {
137 if (imesa->elts.cmd) {
138 GLuint qwords = (imesa->elts.n + 3) >> 2;
139 assert(imesa->cmdBuf.write - imesa->cmdBuf.base + qwords
140 <= imesa->cmdBuf.size);
141 imesa->cmdBuf.write += qwords;
142
143 imesa->elts.cmd->idx.count = imesa->elts.n;
144 imesa->elts.cmd = NULL;
145 }
146 }
147
148 /* Allocate a command buffer entry with <bytes> bytes of arguments:
149 * - implies savageFlushElts
150 */
151 static INLINE
152 drm_savage_cmd_header_t *savageAllocCmdBuf( savageContextPtr imesa, GLuint bytes )
153 {
154 drm_savage_cmd_header_t *ret;
155 GLuint qwords = ((bytes + 7) >> 3) + 1; /* round up */
156 assert (qwords < imesa->cmdBuf.size);
157
158 savageFlushElts(imesa);
159
160 if (imesa->cmdBuf.write - imesa->cmdBuf.base + qwords > imesa->cmdBuf.size)
161 savageFlushCmdBuf(imesa, GL_FALSE);
162
163 ret = (drm_savage_cmd_header_t *)imesa->cmdBuf.write;
164 imesa->cmdBuf.write += qwords;
165 return ret;
166 }
167
168 /* Allocate Elts:
169 * - if it doesn't fit, flush the cmd buffer first
170 * - allocates the drawing command on the cmd buffer if there is no
171 * incomplete indexed drawing command yet
172 * - increments the number of elts. Final allocation is done in savageFlushElts
173 */
174 static INLINE
175 uint16_t *savageAllocElts( savageContextPtr imesa, GLuint n )
176 {
177 uint16_t *ret;
178 GLuint qwords;
179 assert (savageHaveIndexedVerts(imesa));
180
181 if (imesa->elts.cmd)
182 qwords = (imesa->elts.n + n + 3) >> 2;
183 else
184 qwords = ((n + 3) >> 2) + 1;
185 if (imesa->cmdBuf.write - imesa->cmdBuf.base + qwords > imesa->cmdBuf.size)
186 savageFlushCmdBuf(imesa, GL_FALSE); /* implies savageFlushElts */
187
188 if (!imesa->elts.cmd) {
189 savageFlushVertices(imesa);
190 imesa->elts.cmd = savageAllocCmdBuf(imesa, 0);
191 imesa->elts.cmd->idx.cmd = (imesa->vtxBuf == &imesa->dmaVtxBuf) ?
192 SAVAGE_CMD_DMA_IDX : SAVAGE_CMD_VB_IDX;
193 imesa->elts.cmd->idx.prim = imesa->HwPrim;
194 imesa->elts.cmd->idx.skip = imesa->skip;
195 imesa->elts.n = 0;
196 }
197
198 ret = (uint16_t *)(imesa->elts.cmd+1) + imesa->elts.n;
199 imesa->elts.n += n;
200 return ret;
201 }
202
203 #endif