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