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