Merge branch '7.8'
[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_env_combine", NULL },
54 { "GL_ARB_texture_env_dot3", NULL },
55 { "GL_ARB_texture_mirrored_repeat", NULL },
56 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
57 { "GL_EXT_framebuffer_blit", NULL },
58 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
59 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
60 { "GL_EXT_stencil_wrap", NULL },
61 { "GL_EXT_texture_lod_bias", NULL },
62 { "GL_NV_blend_square", 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(const __GLcontextModes *visual, __DRIcontext *dri_ctx,
79 void *share_ctx)
80 {
81 __DRIscreen *dri_screen = dri_ctx->driScreenPriv;
82 struct nouveau_screen *screen = dri_screen->private;
83 struct nouveau_context *nctx;
84 GLcontext *ctx;
85
86 ctx = screen->driver->context_create(screen, visual, share_ctx);
87 if (!ctx)
88 return GL_FALSE;
89
90 nctx = to_nouveau_context(ctx);
91 nctx->dri_context = dri_ctx;
92 dri_ctx->driverPrivate = ctx;
93
94 return GL_TRUE;
95 }
96
97 GLboolean
98 nouveau_context_init(GLcontext *ctx, struct nouveau_screen *screen,
99 const GLvisual *visual, GLcontext *share_ctx)
100 {
101 struct nouveau_context *nctx = to_nouveau_context(ctx);
102 struct dd_function_table functions;
103 int ret;
104
105 nctx->screen = screen;
106 nctx->fallback = HWTNL;
107
108 /* Initialize the function pointers. */
109 _mesa_init_driver_functions(&functions);
110 nouveau_driver_functions_init(&functions);
111 nouveau_bufferobj_functions_init(&functions);
112 nouveau_texture_functions_init(&functions);
113 nouveau_fbo_functions_init(&functions);
114
115 /* Initialize the mesa context. */
116 _mesa_initialize_context(ctx, visual, share_ctx, &functions, NULL);
117
118 nouveau_state_init(ctx);
119 nouveau_bo_state_init(ctx);
120 _mesa_meta_init(ctx);
121 _swrast_CreateContext(ctx);
122 _vbo_CreateContext(ctx);
123 _tnl_CreateContext(ctx);
124 nouveau_span_functions_init(ctx);
125 _mesa_allow_light_in_model(ctx, GL_FALSE);
126
127 /* Allocate a hardware channel. */
128 ret = nouveau_channel_alloc(context_dev(ctx), 0xbeef0201, 0xbeef0202,
129 &nctx->hw.chan);
130 if (ret) {
131 nouveau_error("Error initializing the FIFO.\n");
132 return GL_FALSE;
133 }
134
135 nctx->hw.chan->flush_notify = nouveau_channel_flush_notify;
136 nctx->hw.chan->user_private = nctx;
137
138 /* Enable any supported extensions. */
139 driInitExtensions(ctx, nouveau_extensions, GL_TRUE);
140
141 return GL_TRUE;
142 }
143
144 void
145 nouveau_context_deinit(GLcontext *ctx)
146 {
147 struct nouveau_context *nctx = to_nouveau_context(ctx);
148
149 if (TNL_CONTEXT(ctx))
150 _tnl_DestroyContext(ctx);
151
152 if (vbo_context(ctx))
153 _vbo_DestroyContext(ctx);
154
155 if (SWRAST_CONTEXT(ctx))
156 _swrast_DestroyContext(ctx);
157
158 if (ctx->Meta)
159 _mesa_meta_free(ctx);
160
161 if (nctx->hw.chan)
162 nouveau_channel_free(&nctx->hw.chan);
163
164 nouveau_bo_state_destroy(ctx);
165 _mesa_free_context_data(ctx);
166 }
167
168 void
169 nouveau_context_destroy(__DRIcontext *dri_ctx)
170 {
171 struct nouveau_context *nctx = dri_ctx->driverPrivate;
172 GLcontext *ctx = &nctx->base;
173
174 context_drv(ctx)->context_destroy(ctx);
175 }
176
177 void
178 nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
179 {
180 GLcontext *ctx = dri_ctx->driverPrivate;
181 __DRIscreen *screen = dri_ctx->driScreenPriv;
182 struct gl_framebuffer *fb = draw->driverPrivate;
183 unsigned int attachments[10];
184 __DRIbuffer *buffers = NULL;
185 int i = 0, count, ret;
186
187 if (draw->lastStamp == *draw->pStamp)
188 return;
189 draw->lastStamp = *draw->pStamp;
190
191 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
192 if (fb->Visual.doubleBufferMode)
193 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
194 if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer)
195 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
196 else if (fb->Visual.haveDepthBuffer)
197 attachments[i++] = __DRI_BUFFER_DEPTH;
198 else if (fb->Visual.haveStencilBuffer)
199 attachments[i++] = __DRI_BUFFER_STENCIL;
200
201 buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
202 attachments, i, &count,
203 draw->loaderPrivate);
204 if (buffers == NULL)
205 return;
206
207 for (i = 0; i < count; i++) {
208 struct gl_renderbuffer *rb;
209 struct nouveau_surface *s;
210 uint32_t old_handle;
211 int index;
212
213 switch (buffers[i].attachment) {
214 case __DRI_BUFFER_FRONT_LEFT:
215 case __DRI_BUFFER_FAKE_FRONT_LEFT:
216 index = BUFFER_FRONT_LEFT;
217 break;
218 case __DRI_BUFFER_BACK_LEFT:
219 index = BUFFER_BACK_LEFT;
220 break;
221 case __DRI_BUFFER_DEPTH:
222 case __DRI_BUFFER_DEPTH_STENCIL:
223 index = BUFFER_DEPTH;
224 break;
225 case __DRI_BUFFER_STENCIL:
226 index = BUFFER_STENCIL;
227 break;
228 default:
229 assert(0);
230 }
231
232 rb = fb->Attachment[index].Renderbuffer;
233 s = &to_nouveau_renderbuffer(rb)->surface;
234
235 s->width = draw->w;
236 s->height = draw->h;
237 s->pitch = buffers[i].pitch;
238 s->cpp = buffers[i].cpp;
239
240 /* Don't bother to reopen the bo if it happens to be
241 * the same. */
242 if (s->bo) {
243 ret = nouveau_bo_handle_get(s->bo, &old_handle);
244 assert(!ret);
245 }
246
247 if (!s->bo || old_handle != buffers[i].name) {
248 nouveau_bo_ref(NULL, &s->bo);
249 ret = nouveau_bo_handle_ref(context_dev(ctx),
250 buffers[i].name, &s->bo);
251 assert(!ret);
252 }
253 }
254
255 _mesa_resize_framebuffer(NULL, fb, draw->w, draw->h);
256 }
257
258 static void
259 update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
260 int *stamp)
261 {
262 GLcontext *ctx = dri_ctx->driverPrivate;
263 struct gl_framebuffer *fb = draw->driverPrivate;
264
265 *stamp = *draw->pStamp;
266
267 nouveau_update_renderbuffers(dri_ctx, draw);
268 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
269
270 context_dirty(ctx, FRAMEBUFFER);
271 }
272
273 GLboolean
274 nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
275 __DRIdrawable *dri_read)
276 {
277 if (dri_ctx) {
278 struct nouveau_context *nctx = dri_ctx->driverPrivate;
279 GLcontext *ctx = &nctx->base;
280
281 /* Ask the X server for new renderbuffers. */
282 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
283 update_framebuffer(dri_ctx, dri_draw,
284 &dri_ctx->dri2.draw_stamp);
285
286 if (dri_draw != dri_read &&
287 dri_read->driverPrivate != ctx->WinSysReadBuffer)
288 update_framebuffer(dri_ctx, dri_read,
289 &dri_ctx->dri2.read_stamp);
290
291 /* Pass it down to mesa. */
292 _mesa_make_current(ctx, dri_draw->driverPrivate,
293 dri_read->driverPrivate);
294 _mesa_update_state(ctx);
295
296 FIRE_RING(context_chan(ctx));
297
298 } else {
299 _mesa_make_current(NULL, NULL, NULL);
300 }
301
302 return GL_TRUE;
303 }
304
305 GLboolean
306 nouveau_context_unbind(__DRIcontext *dri_ctx)
307 {
308 return GL_TRUE;
309 }
310
311 void
312 nouveau_fallback(GLcontext *ctx, enum nouveau_fallback mode)
313 {
314 struct nouveau_context *nctx = to_nouveau_context(ctx);
315
316 nctx->fallback = MAX2(HWTNL, mode);
317
318 if (mode < SWRAST)
319 nouveau_state_emit(ctx);
320 else
321 FIRE_RING(context_chan(ctx));
322 }
323
324 void
325 nouveau_validate_framebuffer(GLcontext *ctx)
326 {
327 __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
328 __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
329 __DRIdrawable *dri_read = dri_ctx->driReadablePriv;
330
331 if (ctx->DrawBuffer->Name == 0 &&
332 dri_ctx->dri2.draw_stamp != *dri_draw->pStamp)
333 update_framebuffer(dri_ctx, dri_draw,
334 &dri_ctx->dri2.draw_stamp);
335
336 if (ctx->ReadBuffer->Name == 0 && dri_draw != dri_read &&
337 dri_ctx->dri2.read_stamp != *dri_read->pStamp)
338 update_framebuffer(dri_ctx, dri_read,
339 &dri_ctx->dri2.read_stamp);
340
341 if (nouveau_next_dirty_state(ctx) >= 0) {
342 nouveau_state_emit(ctx);
343 FIRE_RING(context_chan(ctx));
344 }
345 }