2 * Copyright (C) 2009-2010 Francisco Jerez.
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:
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.
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.
28 #include "nouveau_driver.h"
29 #include "nouveau_context.h"
30 #include "nouveau_bufferobj.h"
31 #include "nouveau_fbo.h"
34 #include "main/framebuffer.h"
35 #include "main/light.h"
36 #include "main/state.h"
37 #include "main/version.h"
38 #include "drivers/common/meta.h"
39 #include "drivers/common/driverfuncs.h"
40 #include "swrast/swrast.h"
41 #include "swrast/s_context.h"
44 #include "tnl/t_context.h"
47 nouveau_channel_flush_notify(struct nouveau_channel *chan)
49 struct nouveau_context *nctx = chan->user_private;
50 struct gl_context *ctx = &nctx->base;
52 if (nctx->fallback < SWRAST)
53 nouveau_bo_state_emit(ctx);
57 nouveau_context_create(gl_api api,
58 const struct gl_config *visual, __DRIcontext *dri_ctx,
59 unsigned major_version,
60 unsigned minor_version,
65 __DRIscreen *dri_screen = dri_ctx->driScreenPriv;
66 struct nouveau_screen *screen = dri_screen->driverPrivate;
67 struct nouveau_context *nctx;
68 struct gl_context *ctx;
70 /* API and flag filtering is handled in dri2CreateContextAttribs.
75 ctx = screen->driver->context_create(screen, visual, share_ctx);
77 *error = __DRI_CTX_ERROR_NO_MEMORY;
81 nctx = to_nouveau_context(ctx);
82 nctx->dri_context = dri_ctx;
83 dri_ctx->driverPrivate = ctx;
85 _mesa_compute_version(ctx);
86 if (ctx->VersionMajor < major_version
87 || (ctx->VersionMajor == major_version
88 && ctx->VersionMinor < minor_version)) {
89 nouveau_context_destroy(dri_ctx);
90 *error = __DRI_CTX_ERROR_BAD_VERSION;
94 *error = __DRI_CTX_ERROR_SUCCESS;
99 nouveau_context_init(struct gl_context *ctx, struct nouveau_screen *screen,
100 const struct gl_config *visual, struct gl_context *share_ctx)
102 struct nouveau_context *nctx = to_nouveau_context(ctx);
103 struct dd_function_table functions;
106 nctx->screen = screen;
107 nctx->fallback = HWTNL;
109 /* Initialize the function pointers. */
110 _mesa_init_driver_functions(&functions);
111 nouveau_driver_functions_init(&functions);
112 nouveau_bufferobj_functions_init(&functions);
113 nouveau_texture_functions_init(&functions);
114 nouveau_fbo_functions_init(&functions);
116 /* Initialize the mesa context. */
117 _mesa_initialize_context(ctx, API_OPENGL, visual,
118 share_ctx, &functions, NULL);
120 nouveau_state_init(ctx);
121 nouveau_bo_state_init(ctx);
122 nouveau_scratch_init(ctx);
123 _mesa_meta_init(ctx);
124 _swrast_CreateContext(ctx);
125 _vbo_CreateContext(ctx);
126 _tnl_CreateContext(ctx);
127 nouveau_span_functions_init(ctx);
128 _mesa_allow_light_in_model(ctx, GL_FALSE);
130 /* Allocate a hardware channel. */
131 ret = nouveau_channel_alloc(context_dev(ctx), 0xbeef0201, 0xbeef0202,
132 512*1024, &nctx->hw.chan);
134 nouveau_error("Error initializing the FIFO.\n");
138 nctx->hw.chan->flush_notify = nouveau_channel_flush_notify;
139 nctx->hw.chan->user_private = nctx;
141 /* Enable any supported extensions. */
142 ctx->Extensions.EXT_blend_color = true;
143 ctx->Extensions.EXT_blend_minmax = true;
144 ctx->Extensions.EXT_fog_coord = true;
145 ctx->Extensions.EXT_framebuffer_blit = true;
146 ctx->Extensions.EXT_framebuffer_object = true;
147 ctx->Extensions.EXT_packed_depth_stencil = true;
148 ctx->Extensions.EXT_secondary_color = true;
149 ctx->Extensions.EXT_texture_filter_anisotropic = true;
150 ctx->Extensions.NV_blend_square = true;
151 ctx->Extensions.NV_texture_env_combine4 = true;
157 nouveau_context_deinit(struct gl_context *ctx)
159 struct nouveau_context *nctx = to_nouveau_context(ctx);
161 if (TNL_CONTEXT(ctx))
162 _tnl_DestroyContext(ctx);
164 if (vbo_context(ctx))
165 _vbo_DestroyContext(ctx);
167 if (SWRAST_CONTEXT(ctx))
168 _swrast_DestroyContext(ctx);
171 _mesa_meta_free(ctx);
174 nouveau_channel_free(&nctx->hw.chan);
176 nouveau_scratch_destroy(ctx);
177 nouveau_bo_state_destroy(ctx);
178 _mesa_free_context_data(ctx);
182 nouveau_context_destroy(__DRIcontext *dri_ctx)
184 struct nouveau_context *nctx = dri_ctx->driverPrivate;
185 struct gl_context *ctx = &nctx->base;
187 context_drv(ctx)->context_destroy(ctx);
191 nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
193 struct gl_context *ctx = dri_ctx->driverPrivate;
194 struct nouveau_context *nctx = to_nouveau_context(ctx);
195 __DRIscreen *screen = dri_ctx->driScreenPriv;
196 struct gl_framebuffer *fb = draw->driverPrivate;
197 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
198 unsigned int attachments[10];
199 __DRIbuffer *buffers = NULL;
200 int i = 0, count, ret;
202 if (draw->lastStamp == draw->dri2.stamp)
204 draw->lastStamp = draw->dri2.stamp;
207 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
208 if (fb->Visual.doubleBufferMode)
209 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
210 if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer)
211 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
212 else if (fb->Visual.haveDepthBuffer)
213 attachments[i++] = __DRI_BUFFER_DEPTH;
214 else if (fb->Visual.haveStencilBuffer)
215 attachments[i++] = __DRI_BUFFER_STENCIL;
217 buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
218 attachments, i, &count,
219 draw->loaderPrivate);
223 for (i = 0; i < count; i++) {
224 struct gl_renderbuffer *rb;
225 struct nouveau_surface *s;
229 switch (buffers[i].attachment) {
230 case __DRI_BUFFER_FRONT_LEFT:
231 case __DRI_BUFFER_FAKE_FRONT_LEFT:
232 index = BUFFER_FRONT_LEFT;
234 case __DRI_BUFFER_BACK_LEFT:
235 index = BUFFER_BACK_LEFT;
237 case __DRI_BUFFER_DEPTH:
238 case __DRI_BUFFER_DEPTH_STENCIL:
239 index = BUFFER_DEPTH;
241 case __DRI_BUFFER_STENCIL:
242 index = BUFFER_STENCIL;
248 rb = fb->Attachment[index].Renderbuffer;
249 s = &to_nouveau_renderbuffer(rb)->surface;
253 s->pitch = buffers[i].pitch;
254 s->cpp = buffers[i].cpp;
256 if (index == BUFFER_DEPTH && s->bo) {
257 ret = nouveau_bo_handle_get(s->bo, &old_name);
259 * Disable fast Z clears in the next frame, the
260 * depth buffer contents are undefined.
262 if (!ret && old_name != buffers[i].name)
263 nctx->hierz.clear_seq = 0;
266 nouveau_bo_ref(NULL, &s->bo);
267 ret = nouveau_bo_handle_ref(context_dev(ctx),
268 buffers[i].name, &s->bo);
272 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
276 update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
279 struct gl_context *ctx = dri_ctx->driverPrivate;
280 struct gl_framebuffer *fb = draw->driverPrivate;
282 *stamp = draw->dri2.stamp;
284 nouveau_update_renderbuffers(dri_ctx, draw);
285 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
287 /* Clean up references to the old framebuffer objects. */
288 context_dirty(ctx, FRAMEBUFFER);
289 context_bctx(ctx, FRAMEBUFFER);
290 FIRE_RING(context_chan(ctx));
294 nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
295 __DRIdrawable *dri_read)
298 struct nouveau_context *nctx = dri_ctx->driverPrivate;
299 struct gl_context *ctx = &nctx->base;
301 /* Ask the X server for new renderbuffers. */
302 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
303 update_framebuffer(dri_ctx, dri_draw,
304 &dri_ctx->dri2.draw_stamp);
306 if (dri_draw != dri_read &&
307 dri_read->driverPrivate != ctx->WinSysReadBuffer)
308 update_framebuffer(dri_ctx, dri_read,
309 &dri_ctx->dri2.read_stamp);
311 /* Pass it down to mesa. */
312 _mesa_make_current(ctx, dri_draw->driverPrivate,
313 dri_read->driverPrivate);
314 _mesa_update_state(ctx);
317 _mesa_make_current(NULL, NULL, NULL);
324 nouveau_context_unbind(__DRIcontext *dri_ctx)
326 /* Unset current context and dispatch table */
327 _mesa_make_current(NULL, NULL, NULL);
333 nouveau_fallback(struct gl_context *ctx, enum nouveau_fallback mode)
335 struct nouveau_context *nctx = to_nouveau_context(ctx);
337 nctx->fallback = MAX2(HWTNL, mode);
340 nouveau_state_emit(ctx);
341 nouveau_bo_state_emit(ctx);
343 FIRE_RING(context_chan(ctx));
348 validate_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
351 struct gl_framebuffer *fb = draw->driverPrivate;
352 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
353 GLboolean need_front =
354 (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT ||
355 fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT);
357 if (nfb->need_front != need_front) {
358 nfb->need_front = need_front;
359 dri2InvalidateDrawable(draw);
362 if (draw->dri2.stamp != *stamp)
363 update_framebuffer(dri_ctx, draw, stamp);
367 nouveau_validate_framebuffer(struct gl_context *ctx)
369 __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
370 __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
371 __DRIdrawable *dri_read = dri_ctx->driReadablePriv;
373 if (ctx->DrawBuffer->Name == 0)
374 validate_framebuffer(dri_ctx, dri_draw,
375 &dri_ctx->dri2.draw_stamp);
377 if (ctx->ReadBuffer->Name == 0)
378 validate_framebuffer(dri_ctx, dri_read,
379 &dri_ctx->dri2.read_stamp);
381 if (ctx->NewState & _NEW_BUFFERS)
382 _mesa_update_state(ctx);