mesa: Remove EXT_blend_subtract extension enable flag
[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
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
45 static void
46 nouveau_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
55 GLboolean
56 nouveau_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
76 GLboolean
77 nouveau_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_texture_mirrored_repeat = true;
121 ctx->Extensions.EXT_blend_color = true;
122 ctx->Extensions.EXT_blend_minmax = true;
123 ctx->Extensions.EXT_fog_coord = true;
124 ctx->Extensions.EXT_framebuffer_blit = true;
125 ctx->Extensions.EXT_framebuffer_object = true;
126 ctx->Extensions.EXT_packed_depth_stencil = true;
127 ctx->Extensions.EXT_secondary_color = true;
128 ctx->Extensions.EXT_texture_filter_anisotropic = true;
129 ctx->Extensions.NV_blend_square = true;
130 ctx->Extensions.NV_texture_env_combine4 = true;
131
132 return GL_TRUE;
133 }
134
135 void
136 nouveau_context_deinit(struct gl_context *ctx)
137 {
138 struct nouveau_context *nctx = to_nouveau_context(ctx);
139
140 if (TNL_CONTEXT(ctx))
141 _tnl_DestroyContext(ctx);
142
143 if (vbo_context(ctx))
144 _vbo_DestroyContext(ctx);
145
146 if (SWRAST_CONTEXT(ctx))
147 _swrast_DestroyContext(ctx);
148
149 if (ctx->Meta)
150 _mesa_meta_free(ctx);
151
152 if (nctx->hw.chan)
153 nouveau_channel_free(&nctx->hw.chan);
154
155 nouveau_scratch_destroy(ctx);
156 nouveau_bo_state_destroy(ctx);
157 _mesa_free_context_data(ctx);
158 }
159
160 void
161 nouveau_context_destroy(__DRIcontext *dri_ctx)
162 {
163 struct nouveau_context *nctx = dri_ctx->driverPrivate;
164 struct gl_context *ctx = &nctx->base;
165
166 context_drv(ctx)->context_destroy(ctx);
167 }
168
169 void
170 nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
171 {
172 struct gl_context *ctx = dri_ctx->driverPrivate;
173 struct nouveau_context *nctx = to_nouveau_context(ctx);
174 __DRIscreen *screen = dri_ctx->driScreenPriv;
175 struct gl_framebuffer *fb = draw->driverPrivate;
176 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
177 unsigned int attachments[10];
178 __DRIbuffer *buffers = NULL;
179 int i = 0, count, ret;
180
181 if (draw->lastStamp == *draw->pStamp)
182 return;
183 draw->lastStamp = *draw->pStamp;
184
185 if (nfb->need_front)
186 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
187 if (fb->Visual.doubleBufferMode)
188 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
189 if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer)
190 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
191 else if (fb->Visual.haveDepthBuffer)
192 attachments[i++] = __DRI_BUFFER_DEPTH;
193 else if (fb->Visual.haveStencilBuffer)
194 attachments[i++] = __DRI_BUFFER_STENCIL;
195
196 buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
197 attachments, i, &count,
198 draw->loaderPrivate);
199 if (buffers == NULL)
200 return;
201
202 for (i = 0; i < count; i++) {
203 struct gl_renderbuffer *rb;
204 struct nouveau_surface *s;
205 uint32_t old_name;
206 int index;
207
208 switch (buffers[i].attachment) {
209 case __DRI_BUFFER_FRONT_LEFT:
210 case __DRI_BUFFER_FAKE_FRONT_LEFT:
211 index = BUFFER_FRONT_LEFT;
212 break;
213 case __DRI_BUFFER_BACK_LEFT:
214 index = BUFFER_BACK_LEFT;
215 break;
216 case __DRI_BUFFER_DEPTH:
217 case __DRI_BUFFER_DEPTH_STENCIL:
218 index = BUFFER_DEPTH;
219 break;
220 case __DRI_BUFFER_STENCIL:
221 index = BUFFER_STENCIL;
222 break;
223 default:
224 assert(0);
225 }
226
227 rb = fb->Attachment[index].Renderbuffer;
228 s = &to_nouveau_renderbuffer(rb)->surface;
229
230 s->width = draw->w;
231 s->height = draw->h;
232 s->pitch = buffers[i].pitch;
233 s->cpp = buffers[i].cpp;
234
235 if (index == BUFFER_DEPTH && s->bo) {
236 ret = nouveau_bo_handle_get(s->bo, &old_name);
237 /*
238 * Disable fast Z clears in the next frame, the
239 * depth buffer contents are undefined.
240 */
241 if (!ret && old_name != buffers[i].name)
242 nctx->hierz.clear_seq = 0;
243 }
244
245 nouveau_bo_ref(NULL, &s->bo);
246 ret = nouveau_bo_handle_ref(context_dev(ctx),
247 buffers[i].name, &s->bo);
248 assert(!ret);
249 }
250
251 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
252 }
253
254 static void
255 update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
256 int *stamp)
257 {
258 struct gl_context *ctx = dri_ctx->driverPrivate;
259 struct gl_framebuffer *fb = draw->driverPrivate;
260
261 *stamp = *draw->pStamp;
262
263 nouveau_update_renderbuffers(dri_ctx, draw);
264 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
265
266 /* Clean up references to the old framebuffer objects. */
267 context_dirty(ctx, FRAMEBUFFER);
268 context_bctx(ctx, FRAMEBUFFER);
269 FIRE_RING(context_chan(ctx));
270 }
271
272 GLboolean
273 nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
274 __DRIdrawable *dri_read)
275 {
276 if (dri_ctx) {
277 struct nouveau_context *nctx = dri_ctx->driverPrivate;
278 struct gl_context *ctx = &nctx->base;
279
280 /* Ask the X server for new renderbuffers. */
281 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
282 update_framebuffer(dri_ctx, dri_draw,
283 &dri_ctx->dri2.draw_stamp);
284
285 if (dri_draw != dri_read &&
286 dri_read->driverPrivate != ctx->WinSysReadBuffer)
287 update_framebuffer(dri_ctx, dri_read,
288 &dri_ctx->dri2.read_stamp);
289
290 /* Pass it down to mesa. */
291 _mesa_make_current(ctx, dri_draw->driverPrivate,
292 dri_read->driverPrivate);
293 _mesa_update_state(ctx);
294
295 } else {
296 _mesa_make_current(NULL, NULL, NULL);
297 }
298
299 return GL_TRUE;
300 }
301
302 GLboolean
303 nouveau_context_unbind(__DRIcontext *dri_ctx)
304 {
305 /* Unset current context and dispatch table */
306 _mesa_make_current(NULL, NULL, NULL);
307
308 return GL_TRUE;
309 }
310
311 void
312 nouveau_fallback(struct gl_context *ctx, enum nouveau_fallback mode)
313 {
314 struct nouveau_context *nctx = to_nouveau_context(ctx);
315
316 nctx->fallback = MAX2(HWTNL, mode);
317
318 if (mode < SWRAST) {
319 nouveau_state_emit(ctx);
320 nouveau_bo_state_emit(ctx);
321 } else {
322 FIRE_RING(context_chan(ctx));
323 }
324 }
325
326 static void
327 validate_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
328 int *stamp)
329 {
330 struct gl_framebuffer *fb = draw->driverPrivate;
331 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
332 GLboolean need_front =
333 (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT ||
334 fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT);
335
336 if (nfb->need_front != need_front) {
337 nfb->need_front = need_front;
338 dri2InvalidateDrawable(draw);
339 }
340
341 if (*draw->pStamp != *stamp)
342 update_framebuffer(dri_ctx, draw, stamp);
343 }
344
345 void
346 nouveau_validate_framebuffer(struct gl_context *ctx)
347 {
348 __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
349 __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
350 __DRIdrawable *dri_read = dri_ctx->driReadablePriv;
351
352 if (ctx->DrawBuffer->Name == 0)
353 validate_framebuffer(dri_ctx, dri_draw,
354 &dri_ctx->dri2.draw_stamp);
355
356 if (ctx->ReadBuffer->Name == 0)
357 validate_framebuffer(dri_ctx, dri_read,
358 &dri_ctx->dri2.read_stamp);
359
360 if (ctx->NewState & _NEW_BUFFERS)
361 _mesa_update_state(ctx);
362 }