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