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