Cut a bunch of code by not trying to precompute the blit commands and instead
[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 /* $XFree86: xc/lib/GL/mesa/src/drv/sis/sis_ctx.c,v 1.3 2000/09/26 15:56:48 tsi Exp $ */
28
29 /*
30 * Authors:
31 * Sung-Ching Lin <sclin@sis.com.tw>
32 * Eric Anholt <anholt@FreeBSD.org>
33 *
34 */
35
36 #include "sis_context.h"
37 #include "sis_dd.h"
38 #include "sis_lock.h"
39 #include "sis_alloc.h"
40 #include "sis_span.h"
41 #include "sis_state.h"
42 #include "sis_tris.h"
43
44 #include "swrast/swrast.h"
45 #include "framebuffer.h"
46 #include "renderbuffer.h"
47
48 #include "utils.h"
49
50 #define DRIVER_DATE "20051023"
51
52 /* Return the width and height of the given buffer.
53 */
54 static void
55 sisGetBufferSize( GLframebuffer *buffer,
56 GLuint *width, GLuint *height )
57 {
58 GET_CURRENT_CONTEXT(ctx);
59 sisContextPtr smesa = SIS_CONTEXT(ctx);
60
61 LOCK_HARDWARE();
62 *width = smesa->driDrawable->w;
63 *height = smesa->driDrawable->h;
64 UNLOCK_HARDWARE();
65 }
66
67 /* Return various strings for glGetString().
68 */
69 static const GLubyte *
70 sisGetString( GLcontext *ctx, GLenum name )
71 {
72 sisContextPtr smesa = SIS_CONTEXT(ctx);
73 static char buffer[128];
74 unsigned offset;
75 GLuint agp_mode = (smesa->AGPSize > 0);
76
77 switch ( name )
78 {
79 case GL_VENDOR:
80 return (GLubyte *)"Eric Anholt";
81
82 case GL_RENDERER:
83 offset = driGetRendererString( buffer, "SiS", DRIVER_DATE, agp_mode );
84
85 return (GLubyte *)buffer;
86
87 default:
88 return NULL;
89 }
90 }
91
92 /* Send all commands to the hardware.
93 */
94 static void
95 sisFlush( GLcontext *ctx )
96 {
97 sisContextPtr smesa = SIS_CONTEXT(ctx);
98
99 SIS_FIREVERTICES(smesa);
100 }
101
102 /* Make sure all commands have been sent to the hardware and have
103 * completed processing.
104 */
105 static void
106 sisFinish( GLcontext *ctx )
107 {
108 sisContextPtr smesa = SIS_CONTEXT(ctx);
109
110 SIS_FIREVERTICES(smesa);
111 LOCK_HARDWARE();
112 WaitEngIdle( smesa );
113 UNLOCK_HARDWARE();
114 }
115
116 static void
117 sisDeleteRenderbuffer(struct gl_renderbuffer *rb)
118 {
119 /* Don't free() since we're contained in sis_context struct. */
120 }
121
122 static GLboolean
123 sisRenderbufferStorage(GLcontext *ctx, struct gl_renderbuffer *rb,
124 GLenum internalFormat, GLuint width, GLuint height)
125 {
126 rb->Width = width;
127 rb->Height = height;
128 rb->InternalFormat = internalFormat;
129 return GL_TRUE;
130 }
131
132 static void
133 sisInitRenderbuffer(struct gl_renderbuffer *rb, GLenum format)
134 {
135 const GLuint name = 0;
136
137 _mesa_init_renderbuffer(rb, name);
138
139 /* Make sure we're using a null-valued GetPointer routine */
140 assert(rb->GetPointer(NULL, rb, 0, 0) == NULL);
141
142 rb->InternalFormat = format;
143
144 if (format == GL_RGBA) {
145 /* Color */
146 rb->_BaseFormat = GL_RGBA;
147 rb->DataType = GL_UNSIGNED_BYTE;
148 }
149 else if (format == GL_DEPTH_COMPONENT16) {
150 /* Depth */
151 rb->_BaseFormat = GL_DEPTH_COMPONENT;
152 /* we always Get/Put 32-bit Z values */
153 rb->DataType = GL_UNSIGNED_INT;
154 }
155 else if (format == GL_DEPTH_COMPONENT24) {
156 /* Depth */
157 rb->_BaseFormat = GL_DEPTH_COMPONENT;
158 /* we always Get/Put 32-bit Z values */
159 rb->DataType = GL_UNSIGNED_INT;
160 }
161 else {
162 /* Stencil */
163 ASSERT(format == GL_STENCIL_INDEX8);
164 rb->_BaseFormat = GL_STENCIL_INDEX;
165 rb->DataType = GL_UNSIGNED_BYTE;
166 }
167
168 rb->Delete = sisDeleteRenderbuffer;
169 rb->AllocStorage = sisRenderbufferStorage;
170 }
171
172 void
173 sisUpdateBufferSize(sisContextPtr smesa)
174 {
175 __GLSiSHardware *current = &smesa->current;
176 __GLSiSHardware *prev = &smesa->prev;
177 struct gl_framebuffer *fb = smesa->glCtx->DrawBuffer;
178
179 if (!smesa->front.Base.InternalFormat) {
180 /* do one-time init for the renderbuffers */
181 sisInitRenderbuffer(&smesa->front.Base, GL_RGBA);
182 sisSetSpanFunctions(&smesa->front, &fb->Visual);
183 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &smesa->front.Base);
184
185 if (fb->Visual.doubleBufferMode) {
186 sisInitRenderbuffer(&smesa->back.Base, GL_RGBA);
187 sisSetSpanFunctions(&smesa->back, &fb->Visual);
188 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &smesa->back.Base);
189 }
190
191 if (smesa->glCtx->Visual.depthBits > 0) {
192 sisInitRenderbuffer(&smesa->depth.Base,
193 (smesa->glCtx->Visual.depthBits == 16
194 ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24));
195 sisSetSpanFunctions(&smesa->depth, &fb->Visual);
196 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &smesa->depth.Base);
197 }
198
199 if (smesa->glCtx->Visual.stencilBits > 0) {
200 sisInitRenderbuffer(&smesa->stencil.Base, GL_STENCIL_INDEX8_EXT);
201 sisSetSpanFunctions(&smesa->stencil, &fb->Visual);
202 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &smesa->stencil.Base);
203 }
204 }
205
206 /* Make sure initialization did what we think it should */
207 assert(smesa->front.Base.InternalFormat);
208 assert(smesa->front.Base.AllocStorage);
209 if (fb->Visual.doubleBufferMode) {
210 assert(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
211 assert(smesa->front.Base.AllocStorage);
212 }
213 if (fb->Visual.depthBits) {
214 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
215 assert(smesa->depth.Base.AllocStorage);
216 }
217
218 if ( smesa->width == smesa->driDrawable->w &&
219 smesa->height == smesa->driDrawable->h )
220 {
221 return;
222 }
223
224 smesa->front.bpp = smesa->bytesPerPixel * 8;
225 /* Front pitch set on context create */
226 smesa->front.size = smesa->front.pitch * smesa->driDrawable->h;
227 /* XXX Should get the base offset of the frontbuffer from the X Server */
228 smesa->front.offset = smesa->driDrawable->x * smesa->bytesPerPixel +
229 smesa->driDrawable->y * smesa->front.pitch;
230 smesa->front.map = (char *) smesa->driScreen->pFB + smesa->front.offset;
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 }