dri/nv04: Mipmapping fixes.
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_context.c
1 /*
2 * Copyright (C) 2009-2010 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_bufferobj.h"
30 #include "nouveau_fbo.h"
31
32 #include "main/dd.h"
33 #include "main/framebuffer.h"
34 #include "main/light.h"
35 #include "main/state.h"
36 #include "drivers/common/meta.h"
37 #include "drivers/common/driverfuncs.h"
38 #include "swrast/swrast.h"
39 #include "swrast/s_context.h"
40 #include "vbo/vbo.h"
41 #include "tnl/tnl.h"
42 #include "tnl/t_context.h"
43
44 #define need_GL_EXT_framebuffer_object
45 #define need_GL_EXT_fog_coord
46 #define need_GL_EXT_secondary_color
47
48 #include "main/remap_helper.h"
49
50 static const struct dri_extension nouveau_extensions[] = {
51 { "GL_ARB_multitexture", NULL },
52 { "GL_ARB_texture_env_add", NULL },
53 { "GL_ARB_texture_mirrored_repeat", NULL },
54 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
55 { "GL_EXT_framebuffer_blit", NULL },
56 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
57 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
58 { "GL_EXT_stencil_wrap", NULL },
59 { "GL_EXT_texture_env_combine", NULL },
60 { "GL_EXT_texture_lod_bias", NULL },
61 { "GL_NV_blend_square", NULL },
62 { "GL_NV_texture_env_combine4", NULL },
63 { "GL_SGIS_generate_mipmap", NULL },
64 { NULL, NULL }
65 };
66
67 static void
68 nouveau_channel_flush_notify(struct nouveau_channel *chan)
69 {
70 struct nouveau_context *nctx = chan->user_private;
71 GLcontext *ctx = &nctx->base;
72
73 if (nctx->fallback < SWRAST)
74 nouveau_bo_state_emit(ctx);
75 }
76
77 GLboolean
78 nouveau_context_create(gl_api api,
79 const __GLcontextModes *visual, __DRIcontext *dri_ctx,
80 void *share_ctx)
81 {
82 __DRIscreen *dri_screen = dri_ctx->driScreenPriv;
83 struct nouveau_screen *screen = dri_screen->private;
84 struct nouveau_context *nctx;
85 GLcontext *ctx;
86
87 ctx = screen->driver->context_create(screen, visual, share_ctx);
88 if (!ctx)
89 return GL_FALSE;
90
91 nctx = to_nouveau_context(ctx);
92 nctx->dri_context = dri_ctx;
93 dri_ctx->driverPrivate = ctx;
94
95 return GL_TRUE;
96 }
97
98 GLboolean
99 nouveau_context_init(GLcontext *ctx, struct nouveau_screen *screen,
100 const GLvisual *visual, GLcontext *share_ctx)
101 {
102 struct nouveau_context *nctx = to_nouveau_context(ctx);
103 struct dd_function_table functions;
104 int ret;
105
106 nctx->screen = screen;
107 nctx->fallback = HWTNL;
108
109 /* Initialize the function pointers. */
110 _mesa_init_driver_functions(&functions);
111 nouveau_driver_functions_init(&functions);
112 nouveau_bufferobj_functions_init(&functions);
113 nouveau_texture_functions_init(&functions);
114 nouveau_fbo_functions_init(&functions);
115
116 /* Initialize the mesa context. */
117 _mesa_initialize_context(ctx, visual, share_ctx, &functions, NULL);
118
119 nouveau_state_init(ctx);
120 nouveau_bo_state_init(ctx);
121 _mesa_meta_init(ctx);
122 _swrast_CreateContext(ctx);
123 _vbo_CreateContext(ctx);
124 _tnl_CreateContext(ctx);
125 nouveau_span_functions_init(ctx);
126 _mesa_allow_light_in_model(ctx, GL_FALSE);
127
128 /* Allocate a hardware channel. */
129 ret = nouveau_channel_alloc(context_dev(ctx), 0xbeef0201, 0xbeef0202,
130 &nctx->hw.chan);
131 if (ret) {
132 nouveau_error("Error initializing the FIFO.\n");
133 return GL_FALSE;
134 }
135
136 nctx->hw.chan->flush_notify = nouveau_channel_flush_notify;
137 nctx->hw.chan->user_private = nctx;
138
139 /* Enable any supported extensions. */
140 driInitExtensions(ctx, nouveau_extensions, GL_TRUE);
141
142 return GL_TRUE;
143 }
144
145 void
146 nouveau_context_deinit(GLcontext *ctx)
147 {
148 struct nouveau_context *nctx = to_nouveau_context(ctx);
149
150 if (TNL_CONTEXT(ctx))
151 _tnl_DestroyContext(ctx);
152
153 if (vbo_context(ctx))
154 _vbo_DestroyContext(ctx);
155
156 if (SWRAST_CONTEXT(ctx))
157 _swrast_DestroyContext(ctx);
158
159 if (ctx->Meta)
160 _mesa_meta_free(ctx);
161
162 if (nctx->hw.chan)
163 nouveau_channel_free(&nctx->hw.chan);
164
165 nouveau_bo_state_destroy(ctx);
166 _mesa_free_context_data(ctx);
167 }
168
169 void
170 nouveau_context_destroy(__DRIcontext *dri_ctx)
171 {
172 struct nouveau_context *nctx = dri_ctx->driverPrivate;
173 GLcontext *ctx = &nctx->base;
174
175 context_drv(ctx)->context_destroy(ctx);
176 }
177
178 void
179 nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
180 {
181 GLcontext *ctx = dri_ctx->driverPrivate;
182 __DRIscreen *screen = dri_ctx->driScreenPriv;
183 struct gl_framebuffer *fb = draw->driverPrivate;
184 unsigned int attachments[10];
185 __DRIbuffer *buffers = NULL;
186 int i = 0, count, ret;
187
188 if (draw->lastStamp == *draw->pStamp)
189 return;
190 draw->lastStamp = *draw->pStamp;
191
192 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
193 if (fb->Visual.doubleBufferMode)
194 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
195 if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer)
196 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
197 else if (fb->Visual.haveDepthBuffer)
198 attachments[i++] = __DRI_BUFFER_DEPTH;
199 else if (fb->Visual.haveStencilBuffer)
200 attachments[i++] = __DRI_BUFFER_STENCIL;
201
202 buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
203 attachments, i, &count,
204 draw->loaderPrivate);
205 if (buffers == NULL)
206 return;
207
208 for (i = 0; i < count; i++) {
209 struct gl_renderbuffer *rb;
210 struct nouveau_surface *s;
211 uint32_t old_handle;
212 int index;
213
214 switch (buffers[i].attachment) {
215 case __DRI_BUFFER_FRONT_LEFT:
216 case __DRI_BUFFER_FAKE_FRONT_LEFT:
217 index = BUFFER_FRONT_LEFT;
218 break;
219 case __DRI_BUFFER_BACK_LEFT:
220 index = BUFFER_BACK_LEFT;
221 break;
222 case __DRI_BUFFER_DEPTH:
223 case __DRI_BUFFER_DEPTH_STENCIL:
224 index = BUFFER_DEPTH;
225 break;
226 case __DRI_BUFFER_STENCIL:
227 index = BUFFER_STENCIL;
228 break;
229 default:
230 assert(0);
231 }
232
233 rb = fb->Attachment[index].Renderbuffer;
234 s = &to_nouveau_renderbuffer(rb)->surface;
235
236 s->width = draw->w;
237 s->height = draw->h;
238 s->pitch = buffers[i].pitch;
239 s->cpp = buffers[i].cpp;
240
241 /* Don't bother to reopen the bo if it happens to be
242 * the same. */
243 if (s->bo) {
244 ret = nouveau_bo_handle_get(s->bo, &old_handle);
245 assert(!ret);
246 }
247
248 if (!s->bo || old_handle != buffers[i].name) {
249 nouveau_bo_ref(NULL, &s->bo);
250 ret = nouveau_bo_handle_ref(context_dev(ctx),
251 buffers[i].name, &s->bo);
252 assert(!ret);
253 }
254 }
255
256 _mesa_resize_framebuffer(NULL, fb, draw->w, draw->h);
257 }
258
259 static void
260 update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
261 int *stamp)
262 {
263 GLcontext *ctx = dri_ctx->driverPrivate;
264 struct gl_framebuffer *fb = draw->driverPrivate;
265
266 *stamp = *draw->pStamp;
267
268 nouveau_update_renderbuffers(dri_ctx, draw);
269 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
270
271 context_dirty(ctx, FRAMEBUFFER);
272 }
273
274 GLboolean
275 nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
276 __DRIdrawable *dri_read)
277 {
278 if (dri_ctx) {
279 struct nouveau_context *nctx = dri_ctx->driverPrivate;
280 GLcontext *ctx = &nctx->base;
281
282 /* Ask the X server for new renderbuffers. */
283 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
284 update_framebuffer(dri_ctx, dri_draw,
285 &dri_ctx->dri2.draw_stamp);
286
287 if (dri_draw != dri_read &&
288 dri_read->driverPrivate != ctx->WinSysReadBuffer)
289 update_framebuffer(dri_ctx, dri_read,
290 &dri_ctx->dri2.read_stamp);
291
292 /* Pass it down to mesa. */
293 _mesa_make_current(ctx, dri_draw->driverPrivate,
294 dri_read->driverPrivate);
295 _mesa_update_state(ctx);
296
297 FIRE_RING(context_chan(ctx));
298
299 } else {
300 _mesa_make_current(NULL, NULL, NULL);
301 }
302
303 return GL_TRUE;
304 }
305
306 GLboolean
307 nouveau_context_unbind(__DRIcontext *dri_ctx)
308 {
309 /* Unset current context and dispath table */
310 _mesa_make_current(NULL, NULL, NULL);
311
312 return GL_TRUE;
313 }
314
315 void
316 nouveau_fallback(GLcontext *ctx, enum nouveau_fallback mode)
317 {
318 struct nouveau_context *nctx = to_nouveau_context(ctx);
319
320 nctx->fallback = MAX2(HWTNL, mode);
321
322 if (mode < SWRAST)
323 nouveau_state_emit(ctx);
324 else
325 FIRE_RING(context_chan(ctx));
326 }
327
328 void
329 nouveau_validate_framebuffer(GLcontext *ctx)
330 {
331 __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
332 __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
333 __DRIdrawable *dri_read = dri_ctx->driReadablePriv;
334
335 if (ctx->DrawBuffer->Name == 0 &&
336 dri_ctx->dri2.draw_stamp != *dri_draw->pStamp)
337 update_framebuffer(dri_ctx, dri_draw,
338 &dri_ctx->dri2.draw_stamp);
339
340 if (ctx->ReadBuffer->Name == 0 && dri_draw != dri_read &&
341 dri_ctx->dri2.read_stamp != *dri_read->pStamp)
342 update_framebuffer(dri_ctx, dri_read,
343 &dri_ctx->dri2.read_stamp);
344
345 if (nouveau_next_dirty_state(ctx) >= 0) {
346 nouveau_state_emit(ctx);
347 FIRE_RING(context_chan(ctx));
348 }
349 }