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