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