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