Merge branch 'gallium-vertex-linear' into gallium-tex-surfaces
[mesa.git] / src / mesa / drivers / dri / sis / sis_dd.c
1 /**************************************************************************
2
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Sung-Ching Lin <sclin@sis.com.tw>
31 * Eric Anholt <anholt@FreeBSD.org>
32 *
33 */
34
35 #include "sis_context.h"
36 #include "sis_dd.h"
37 #include "sis_lock.h"
38 #include "sis_alloc.h"
39 #include "sis_span.h"
40 #include "sis_state.h"
41 #include "sis_tris.h"
42
43 #include "swrast/swrast.h"
44 #include "framebuffer.h"
45 #include "renderbuffer.h"
46
47 #include "utils.h"
48
49 #define DRIVER_DATE "20060710"
50
51 /* Return the width and height of the given buffer.
52 */
53 static void
54 sisGetBufferSize( GLframebuffer *buffer,
55 GLuint *width, GLuint *height )
56 {
57 GET_CURRENT_CONTEXT(ctx);
58 sisContextPtr smesa = SIS_CONTEXT(ctx);
59
60 LOCK_HARDWARE();
61 *width = smesa->driDrawable->w;
62 *height = smesa->driDrawable->h;
63 UNLOCK_HARDWARE();
64 }
65
66 /* Return various strings for glGetString().
67 */
68 static const GLubyte *
69 sisGetString( GLcontext *ctx, GLenum name )
70 {
71 sisContextPtr smesa = SIS_CONTEXT(ctx);
72 static char buffer[128];
73 unsigned offset;
74 GLuint agp_mode = (smesa->AGPSize > 0);
75
76 switch ( name )
77 {
78 case GL_VENDOR:
79 return (GLubyte *)"Eric Anholt";
80
81 case GL_RENDERER:
82 offset = driGetRendererString( buffer, "SiS", DRIVER_DATE, agp_mode );
83
84 return (GLubyte *)buffer;
85
86 default:
87 return NULL;
88 }
89 }
90
91 /* Send all commands to the hardware.
92 */
93 static void
94 sisFlush( GLcontext *ctx )
95 {
96 sisContextPtr smesa = SIS_CONTEXT(ctx);
97
98 SIS_FIREVERTICES(smesa);
99 }
100
101 /* Make sure all commands have been sent to the hardware and have
102 * completed processing.
103 */
104 static void
105 sisFinish( GLcontext *ctx )
106 {
107 sisContextPtr smesa = SIS_CONTEXT(ctx);
108
109 SIS_FIREVERTICES(smesa);
110 LOCK_HARDWARE();
111 WaitEngIdle( smesa );
112 UNLOCK_HARDWARE();
113 }
114
115 static void
116 sisDeleteRenderbuffer(struct gl_renderbuffer *rb)
117 {
118 /* Don't free() since we're contained in sis_context struct. */
119 }
120
121 static GLboolean
122 sisRenderbufferStorage(GLcontext *ctx, struct gl_renderbuffer *rb,
123 GLenum internalFormat, GLuint width, GLuint height)
124 {
125 rb->Width = width;
126 rb->Height = height;
127 rb->InternalFormat = internalFormat;
128 return GL_TRUE;
129 }
130
131 static void
132 sisInitRenderbuffer(struct gl_renderbuffer *rb, GLenum format)
133 {
134 const GLuint name = 0;
135
136 _mesa_init_renderbuffer(rb, name);
137
138 /* Make sure we're using a null-valued GetPointer routine */
139 assert(rb->GetPointer(NULL, rb, 0, 0) == NULL);
140
141 rb->InternalFormat = format;
142
143 if (format == GL_RGBA) {
144 /* Color */
145 rb->_BaseFormat = GL_RGBA;
146 rb->DataType = GL_UNSIGNED_BYTE;
147 }
148 else if (format == GL_DEPTH_COMPONENT16) {
149 /* Depth */
150 rb->_BaseFormat = GL_DEPTH_COMPONENT;
151 /* we always Get/Put 32-bit Z values */
152 rb->DataType = GL_UNSIGNED_INT;
153 }
154 else if (format == GL_DEPTH_COMPONENT24) {
155 /* Depth */
156 rb->_BaseFormat = GL_DEPTH_COMPONENT;
157 /* we always Get/Put 32-bit Z values */
158 rb->DataType = GL_UNSIGNED_INT;
159 }
160 else {
161 /* Stencil */
162 ASSERT(format == GL_STENCIL_INDEX8_EXT);
163 rb->_BaseFormat = GL_STENCIL_INDEX;
164 rb->DataType = GL_UNSIGNED_BYTE;
165 }
166
167 rb->Delete = sisDeleteRenderbuffer;
168 rb->AllocStorage = sisRenderbufferStorage;
169 }
170
171 void
172 sisUpdateBufferSize(sisContextPtr smesa)
173 {
174 __GLSiSHardware *current = &smesa->current;
175 __GLSiSHardware *prev = &smesa->prev;
176 struct gl_framebuffer *fb = smesa->glCtx->DrawBuffer;
177
178 if (!smesa->front.Base.InternalFormat) {
179 /* do one-time init for the renderbuffers */
180 sisInitRenderbuffer(&smesa->front.Base, GL_RGBA);
181 sisSetSpanFunctions(&smesa->front, &fb->Visual);
182 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &smesa->front.Base);
183
184 if (fb->Visual.doubleBufferMode) {
185 sisInitRenderbuffer(&smesa->back.Base, GL_RGBA);
186 sisSetSpanFunctions(&smesa->back, &fb->Visual);
187 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &smesa->back.Base);
188 }
189
190 if (smesa->glCtx->Visual.depthBits > 0) {
191 sisInitRenderbuffer(&smesa->depth.Base,
192 (smesa->glCtx->Visual.depthBits == 16
193 ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24));
194 sisSetSpanFunctions(&smesa->depth, &fb->Visual);
195 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &smesa->depth.Base);
196 }
197
198 if (smesa->glCtx->Visual.stencilBits > 0) {
199 sisInitRenderbuffer(&smesa->stencil.Base, GL_STENCIL_INDEX8_EXT);
200 sisSetSpanFunctions(&smesa->stencil, &fb->Visual);
201 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &smesa->stencil.Base);
202 }
203 }
204
205 /* Make sure initialization did what we think it should */
206 assert(smesa->front.Base.InternalFormat);
207 assert(smesa->front.Base.AllocStorage);
208 if (fb->Visual.doubleBufferMode) {
209 assert(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
210 assert(smesa->front.Base.AllocStorage);
211 }
212 if (fb->Visual.depthBits) {
213 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
214 assert(smesa->depth.Base.AllocStorage);
215 }
216
217 /* XXX Should get the base offset of the frontbuffer from the X Server */
218 smesa->front.offset = smesa->driDrawable->x * smesa->bytesPerPixel +
219 smesa->driDrawable->y * smesa->front.pitch;
220 smesa->front.map = (char *) smesa->driScreen->pFB + smesa->front.offset;
221
222 if ( smesa->width == smesa->driDrawable->w &&
223 smesa->height == smesa->driDrawable->h )
224 {
225 return;
226 }
227
228 smesa->front.bpp = smesa->bytesPerPixel * 8;
229 /* Front pitch set on context create */
230 smesa->front.size = smesa->front.pitch * smesa->driDrawable->h;
231
232 smesa->width = smesa->driDrawable->w;
233 smesa->height = smesa->driDrawable->h;
234 smesa->bottom = smesa->height - 1;
235
236 if (smesa->back.offset)
237 sisFreeBackbuffer( smesa );
238 if (smesa->depth.offset)
239 sisFreeZStencilBuffer( smesa );
240
241 if ( smesa->glCtx->Visual.depthBits > 0 )
242 sisAllocZStencilBuffer( smesa );
243 if ( smesa->glCtx->Visual.doubleBufferMode )
244 sisAllocBackbuffer( smesa );
245
246 current->hwZ &= ~MASK_ZBufferPitch;
247 current->hwZ |= smesa->depth.pitch >> 2;
248 current->hwOffsetZ = smesa->depth.offset >> 2;
249
250 if ((current->hwOffsetZ != prev->hwOffsetZ) || (current->hwZ != prev->hwZ)) {
251 prev->hwOffsetZ = current->hwOffsetZ;
252 prev->hwZ = current->hwZ;
253 smesa->GlobalFlag |= GFLAG_ZSETTING;
254 }
255
256 sisUpdateClipping( smesa->glCtx );
257 }
258
259 /* Initialize the driver's misc functions.
260 */
261 void
262 sisInitDriverFuncs( struct dd_function_table *functions )
263 {
264 functions->GetBufferSize = sisGetBufferSize;
265 functions->GetString = sisGetString;
266 functions->Finish = sisFinish;
267 functions->Flush = sisFlush;
268 }