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