Merge branch 'gallium-userbuf'
[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 <stdbool.h>
28 #include "nouveau_driver.h"
29 #include "nouveau_context.h"
30 #include "nouveau_bufferobj.h"
31 #include "nouveau_fbo.h"
32 #include "nv_object.xml.h"
33
34 #include "main/dd.h"
35 #include "main/framebuffer.h"
36 #include "main/fbobject.h"
37 #include "main/light.h"
38 #include "main/state.h"
39 #include "main/version.h"
40 #include "drivers/common/meta.h"
41 #include "drivers/common/driverfuncs.h"
42 #include "swrast/swrast.h"
43 #include "swrast/s_context.h"
44 #include "vbo/vbo.h"
45 #include "tnl/tnl.h"
46 #include "tnl/t_context.h"
47
48 GLboolean
49 nouveau_context_create(gl_api api,
50 const struct gl_config *visual, __DRIcontext *dri_ctx,
51 unsigned major_version,
52 unsigned minor_version,
53 uint32_t flags,
54 unsigned *error,
55 void *share_ctx)
56 {
57 __DRIscreen *dri_screen = dri_ctx->driScreenPriv;
58 struct nouveau_screen *screen = dri_screen->driverPrivate;
59 struct nouveau_context *nctx;
60 struct gl_context *ctx;
61
62 /* API and flag filtering is handled in dri2CreateContextAttribs.
63 */
64 (void) api;
65 (void) flags;
66
67 ctx = screen->driver->context_create(screen, visual, share_ctx);
68 if (!ctx) {
69 *error = __DRI_CTX_ERROR_NO_MEMORY;
70 return GL_FALSE;
71 }
72
73 nctx = to_nouveau_context(ctx);
74 nctx->dri_context = dri_ctx;
75 dri_ctx->driverPrivate = ctx;
76
77 _mesa_compute_version(ctx);
78 if (ctx->VersionMajor < major_version
79 || (ctx->VersionMajor == major_version
80 && ctx->VersionMinor < minor_version)) {
81 nouveau_context_destroy(dri_ctx);
82 *error = __DRI_CTX_ERROR_BAD_VERSION;
83 return GL_FALSE;
84 }
85
86 *error = __DRI_CTX_ERROR_SUCCESS;
87 return GL_TRUE;
88 }
89
90 GLboolean
91 nouveau_context_init(struct gl_context *ctx, struct nouveau_screen *screen,
92 const struct gl_config *visual, struct gl_context *share_ctx)
93 {
94 struct nouveau_context *nctx = to_nouveau_context(ctx);
95 struct dd_function_table functions;
96 int ret;
97
98 nctx->screen = screen;
99 nctx->fallback = HWTNL;
100
101 /* Initialize the function pointers. */
102 _mesa_init_driver_functions(&functions);
103 nouveau_driver_functions_init(&functions);
104 nouveau_bufferobj_functions_init(&functions);
105 nouveau_texture_functions_init(&functions);
106 nouveau_fbo_functions_init(&functions);
107
108 /* Initialize the mesa context. */
109 _mesa_initialize_context(ctx, API_OPENGL, visual,
110 share_ctx, &functions, NULL);
111
112 nouveau_state_init(ctx);
113 nouveau_scratch_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_object_new(&context_dev(ctx)->object, 0xbeef0000,
123 NOUVEAU_FIFO_CHANNEL_CLASS,
124 &(struct nv04_fifo){
125 .vram = 0xbeef0201,
126 .gart = 0xbeef0202
127 }, sizeof(struct nv04_fifo), &nctx->hw.chan);
128 if (ret) {
129 nouveau_error("Error initializing the FIFO.\n");
130 return GL_FALSE;
131 }
132
133 /* Allocate a client (thread data) */
134 ret = nouveau_client_new(context_dev(ctx), &nctx->hw.client);
135 if (ret) {
136 nouveau_error("Error creating thread data\n");
137 return GL_FALSE;
138 }
139
140 /* Allocate a push buffer */
141 ret = nouveau_pushbuf_new(nctx->hw.client, nctx->hw.chan, 4,
142 512 * 1024, true, &nctx->hw.pushbuf);
143 if (ret) {
144 nouveau_error("Error allocating DMA push buffer\n");
145 return GL_FALSE;
146 }
147
148 /* Allocate buffer context */
149 ret = nouveau_bufctx_new(nctx->hw.client, 16, &nctx->hw.bufctx);
150 if (ret) {
151 nouveau_error("Error allocating buffer context\n");
152 return GL_FALSE;
153 }
154
155 nctx->hw.pushbuf->user_priv = nctx->hw.bufctx;
156
157 /* Allocate NULL object */
158 ret = nouveau_object_new(nctx->hw.chan, 0x00000000, NV01_NULL_CLASS,
159 NULL, 0, &nctx->hw.null);
160 if (ret) {
161 nouveau_error("Error allocating NULL object\n");
162 return GL_FALSE;
163 }
164
165 /* Enable any supported extensions. */
166 ctx->Extensions.EXT_blend_color = true;
167 ctx->Extensions.EXT_blend_minmax = true;
168 ctx->Extensions.EXT_fog_coord = true;
169 ctx->Extensions.EXT_framebuffer_blit = true;
170 ctx->Extensions.EXT_framebuffer_object = true;
171 ctx->Extensions.EXT_packed_depth_stencil = true;
172 ctx->Extensions.EXT_secondary_color = true;
173 ctx->Extensions.EXT_texture_filter_anisotropic = true;
174 ctx->Extensions.NV_blend_square = true;
175 ctx->Extensions.NV_texture_env_combine4 = true;
176
177 return GL_TRUE;
178 }
179
180 void
181 nouveau_context_deinit(struct gl_context *ctx)
182 {
183 struct nouveau_context *nctx = to_nouveau_context(ctx);
184
185 if (TNL_CONTEXT(ctx))
186 _tnl_DestroyContext(ctx);
187
188 if (vbo_context(ctx))
189 _vbo_DestroyContext(ctx);
190
191 if (SWRAST_CONTEXT(ctx))
192 _swrast_DestroyContext(ctx);
193
194 if (ctx->Meta)
195 _mesa_meta_free(ctx);
196
197 nouveau_bufctx_del(&nctx->hw.bufctx);
198 nouveau_pushbuf_del(&nctx->hw.pushbuf);
199 nouveau_client_del(&nctx->hw.client);
200 nouveau_object_del(&nctx->hw.chan);
201
202 nouveau_scratch_destroy(ctx);
203 _mesa_free_context_data(ctx);
204 }
205
206 void
207 nouveau_context_destroy(__DRIcontext *dri_ctx)
208 {
209 struct nouveau_context *nctx = dri_ctx->driverPrivate;
210 struct gl_context *ctx = &nctx->base;
211
212 context_drv(ctx)->context_destroy(ctx);
213 }
214
215 void
216 nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
217 {
218 struct gl_context *ctx = dri_ctx->driverPrivate;
219 struct nouveau_context *nctx = to_nouveau_context(ctx);
220 __DRIscreen *screen = dri_ctx->driScreenPriv;
221 struct gl_framebuffer *fb = draw->driverPrivate;
222 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
223 unsigned int attachments[10];
224 __DRIbuffer *buffers = NULL;
225 int i = 0, count, ret;
226
227 if (draw->lastStamp == draw->dri2.stamp)
228 return;
229 draw->lastStamp = draw->dri2.stamp;
230
231 if (nfb->need_front)
232 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
233 if (fb->Visual.doubleBufferMode)
234 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
235 if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer)
236 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
237 else if (fb->Visual.haveDepthBuffer)
238 attachments[i++] = __DRI_BUFFER_DEPTH;
239 else if (fb->Visual.haveStencilBuffer)
240 attachments[i++] = __DRI_BUFFER_STENCIL;
241
242 buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
243 attachments, i, &count,
244 draw->loaderPrivate);
245 if (buffers == NULL)
246 return;
247
248 for (i = 0; i < count; i++) {
249 struct gl_renderbuffer *rb;
250 struct nouveau_surface *s;
251 uint32_t old_name;
252 int index;
253
254 switch (buffers[i].attachment) {
255 case __DRI_BUFFER_FRONT_LEFT:
256 case __DRI_BUFFER_FAKE_FRONT_LEFT:
257 index = BUFFER_FRONT_LEFT;
258 break;
259 case __DRI_BUFFER_BACK_LEFT:
260 index = BUFFER_BACK_LEFT;
261 break;
262 case __DRI_BUFFER_DEPTH:
263 case __DRI_BUFFER_DEPTH_STENCIL:
264 index = BUFFER_DEPTH;
265 break;
266 case __DRI_BUFFER_STENCIL:
267 index = BUFFER_STENCIL;
268 break;
269 default:
270 assert(0);
271 }
272
273 rb = fb->Attachment[index].Renderbuffer;
274 s = &to_nouveau_renderbuffer(rb)->surface;
275
276 s->width = draw->w;
277 s->height = draw->h;
278 s->pitch = buffers[i].pitch;
279 s->cpp = buffers[i].cpp;
280
281 if (index == BUFFER_DEPTH && s->bo) {
282 ret = nouveau_bo_name_get(s->bo, &old_name);
283 /*
284 * Disable fast Z clears in the next frame, the
285 * depth buffer contents are undefined.
286 */
287 if (!ret && old_name != buffers[i].name)
288 nctx->hierz.clear_seq = 0;
289 }
290
291 nouveau_bo_ref(NULL, &s->bo);
292 ret = nouveau_bo_name_ref(context_dev(ctx),
293 buffers[i].name, &s->bo);
294 assert(!ret);
295 }
296
297 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
298 }
299
300 static void
301 update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
302 int *stamp)
303 {
304 struct gl_context *ctx = dri_ctx->driverPrivate;
305 struct gl_framebuffer *fb = draw->driverPrivate;
306
307 *stamp = draw->dri2.stamp;
308
309 nouveau_update_renderbuffers(dri_ctx, draw);
310 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
311
312 /* Clean up references to the old framebuffer objects. */
313 context_dirty(ctx, FRAMEBUFFER);
314 nouveau_bufctx_reset(to_nouveau_context(ctx)->hw.bufctx, BUFCTX_FB);
315 PUSH_KICK(context_push(ctx));
316 }
317
318 GLboolean
319 nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
320 __DRIdrawable *dri_read)
321 {
322 if (dri_ctx) {
323 struct nouveau_context *nctx = dri_ctx->driverPrivate;
324 struct gl_context *ctx = &nctx->base;
325
326 /* Ask the X server for new renderbuffers. */
327 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
328 update_framebuffer(dri_ctx, dri_draw,
329 &dri_ctx->dri2.draw_stamp);
330
331 if (dri_draw != dri_read &&
332 dri_read->driverPrivate != ctx->WinSysReadBuffer)
333 update_framebuffer(dri_ctx, dri_read,
334 &dri_ctx->dri2.read_stamp);
335
336 /* Pass it down to mesa. */
337 _mesa_make_current(ctx, dri_draw->driverPrivate,
338 dri_read->driverPrivate);
339 _mesa_update_state(ctx);
340
341 } else {
342 _mesa_make_current(NULL, NULL, NULL);
343 }
344
345 return GL_TRUE;
346 }
347
348 GLboolean
349 nouveau_context_unbind(__DRIcontext *dri_ctx)
350 {
351 /* Unset current context and dispatch table */
352 _mesa_make_current(NULL, NULL, NULL);
353
354 return GL_TRUE;
355 }
356
357 void
358 nouveau_fallback(struct gl_context *ctx, enum nouveau_fallback mode)
359 {
360 struct nouveau_context *nctx = to_nouveau_context(ctx);
361
362 nctx->fallback = MAX2(HWTNL, mode);
363
364 if (mode < SWRAST) {
365 nouveau_state_emit(ctx);
366 #if 0
367 nouveau_bo_state_emit(ctx);
368 #endif
369 } else {
370 PUSH_KICK(context_push(ctx));
371 }
372 }
373
374 static void
375 validate_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
376 int *stamp)
377 {
378 struct gl_framebuffer *fb = draw->driverPrivate;
379 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
380 GLboolean need_front =
381 (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT ||
382 fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT);
383
384 if (nfb->need_front != need_front) {
385 nfb->need_front = need_front;
386 dri2InvalidateDrawable(draw);
387 }
388
389 if (draw->dri2.stamp != *stamp)
390 update_framebuffer(dri_ctx, draw, stamp);
391 }
392
393 void
394 nouveau_validate_framebuffer(struct gl_context *ctx)
395 {
396 __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
397 __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
398 __DRIdrawable *dri_read = dri_ctx->driReadablePriv;
399
400 if (_mesa_is_winsys_fbo(ctx->DrawBuffer))
401 validate_framebuffer(dri_ctx, dri_draw,
402 &dri_ctx->dri2.draw_stamp);
403
404 if (_mesa_is_winsys_fbo(ctx->ReadBuffer))
405 validate_framebuffer(dri_ctx, dri_read,
406 &dri_ctx->dri2.read_stamp);
407
408 if (ctx->NewState & _NEW_BUFFERS)
409 _mesa_update_state(ctx);
410 }