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 #include "nv_object.xml.h"
33 
34 #include "main/dd.h"
35 #include "main/framebuffer.h"
36 #include "main/fbobject.h"
37 #include "main/light.h"
38 #include "main/state.h"
39 #include "main/version.h"
40 #include "drivers/common/meta.h"
41 #include "drivers/common/driverfuncs.h"
42 #include "swrast/swrast.h"
43 #include "swrast/s_context.h"
44 #include "vbo/vbo.h"
45 #include "tnl/tnl.h"
46 #include "tnl/t_context.h"
47 
48 GLboolean
nouveau_context_create(gl_api api,const struct gl_config * visual,__DRIcontext * dri_ctx,unsigned major_version,unsigned minor_version,uint32_t flags,unsigned * error,void * share_ctx)49 nouveau_context_create(gl_api api,
50 		       const struct gl_config *visual, __DRIcontext *dri_ctx,
51 		       unsigned major_version,
52 		       unsigned minor_version,
53 		       uint32_t flags,
54 		       unsigned *error,
55 		       void *share_ctx)
56 {
57 	__DRIscreen *dri_screen = dri_ctx->driScreenPriv;
58 	struct nouveau_screen *screen = dri_screen->driverPrivate;
59 	struct nouveau_context *nctx;
60 	struct gl_context *ctx;
61 
62 	switch (api) {
63 	case API_OPENGL:
64 		/* Do after-the-fact version checking (below).
65 		 */
66 		break;
67 	case API_OPENGLES:
68 		/* NV10 and NV20 can support OpenGL ES 1.0 only.  Older chips
69 		 * cannot do even that.
70 		 */
71 		if ((screen->device->chipset & 0xf0) == 0x00) {
72 			*error = __DRI_CTX_ERROR_BAD_API;
73 			return GL_FALSE;
74 		} else if (minor_version != 0) {
75 			*error = __DRI_CTX_ERROR_BAD_VERSION;
76 			return GL_FALSE;
77 		}
78 		break;
79 	case API_OPENGLES2:
80 	case API_OPENGL_CORE:
81 		*error = __DRI_CTX_ERROR_BAD_API;
82 		return GL_FALSE;
83 	}
84 
85 	/* API and flag filtering is handled in dri2CreateContextAttribs.
86 	 */
87 	(void) flags;
88 
89 	ctx = screen->driver->context_create(screen, visual, share_ctx);
90 	if (!ctx) {
91 		*error = __DRI_CTX_ERROR_NO_MEMORY;
92 		return GL_FALSE;
93 	}
94 
95 	nctx = to_nouveau_context(ctx);
96 	nctx->dri_context = dri_ctx;
97 	dri_ctx->driverPrivate = ctx;
98 
99 	_mesa_compute_version(ctx);
100 	if (ctx->Version < major_version * 10 + minor_version) {
101 	   nouveau_context_destroy(dri_ctx);
102 	   *error = __DRI_CTX_ERROR_BAD_VERSION;
103 	   return GL_FALSE;
104 	}
105 
106 	if (nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_VRAM, 0, 4096,
107 			   NULL, &nctx->fence)) {
108 		nouveau_context_destroy(dri_ctx);
109 		*error = __DRI_CTX_ERROR_NO_MEMORY;
110 		return GL_FALSE;
111 	}
112 
113 	*error = __DRI_CTX_ERROR_SUCCESS;
114 	return GL_TRUE;
115 }
116 
117 GLboolean
nouveau_context_init(struct gl_context * ctx,struct nouveau_screen * screen,const struct gl_config * visual,struct gl_context * share_ctx)118 nouveau_context_init(struct gl_context *ctx, struct nouveau_screen *screen,
119 		     const struct gl_config *visual, struct gl_context *share_ctx)
120 {
121 	struct nouveau_context *nctx = to_nouveau_context(ctx);
122 	struct dd_function_table functions;
123 	int ret;
124 
125 	nctx->screen = screen;
126 	nctx->fallback = HWTNL;
127 
128 	/* Initialize the function pointers. */
129 	_mesa_init_driver_functions(&functions);
130 	nouveau_driver_functions_init(&functions);
131 	nouveau_bufferobj_functions_init(&functions);
132 	nouveau_texture_functions_init(&functions);
133 	nouveau_fbo_functions_init(&functions);
134 
135 	/* Initialize the mesa context. */
136 	_mesa_initialize_context(ctx, API_OPENGL, visual,
137                                  share_ctx, &functions, NULL);
138 
139 	nouveau_state_init(ctx);
140 	nouveau_scratch_init(ctx);
141 	_mesa_meta_init(ctx);
142 	_swrast_CreateContext(ctx);
143 	_vbo_CreateContext(ctx);
144 	_tnl_CreateContext(ctx);
145 	nouveau_span_functions_init(ctx);
146 	_mesa_allow_light_in_model(ctx, GL_FALSE);
147 
148 	/* Allocate a hardware channel. */
149 	ret = nouveau_object_new(&context_dev(ctx)->object, 0xbeef0000,
150 				 NOUVEAU_FIFO_CHANNEL_CLASS,
151 				 &(struct nv04_fifo){
152 					.vram = 0xbeef0201,
153 					.gart = 0xbeef0202
154 				 }, sizeof(struct nv04_fifo), &nctx->hw.chan);
155 	if (ret) {
156 		nouveau_error("Error initializing the FIFO.\n");
157 		return GL_FALSE;
158 	}
159 
160 	/* Allocate a client (thread data) */
161 	ret = nouveau_client_new(context_dev(ctx), &nctx->hw.client);
162 	if (ret) {
163 		nouveau_error("Error creating thread data\n");
164 		return GL_FALSE;
165 	}
166 
167 	/* Allocate a push buffer */
168 	ret = nouveau_pushbuf_new(nctx->hw.client, nctx->hw.chan, 4,
169 				  512 * 1024, true, &nctx->hw.pushbuf);
170 	if (ret) {
171 		nouveau_error("Error allocating DMA push buffer\n");
172 		return GL_FALSE;
173 	}
174 
175 	/* Allocate buffer context */
176 	ret = nouveau_bufctx_new(nctx->hw.client, 16, &nctx->hw.bufctx);
177 	if (ret) {
178 		nouveau_error("Error allocating buffer context\n");
179 		return GL_FALSE;
180 	}
181 
182 	nctx->hw.pushbuf->user_priv = nctx->hw.bufctx;
183 
184 	/* Allocate NULL object */
185 	ret = nouveau_object_new(nctx->hw.chan, 0x00000000, NV01_NULL_CLASS,
186 				 NULL, 0, &nctx->hw.null);
187 	if (ret) {
188 		nouveau_error("Error allocating NULL object\n");
189 		return GL_FALSE;
190 	}
191 
192 	/* Enable any supported extensions. */
193 	ctx->Extensions.EXT_blend_color = true;
194 	ctx->Extensions.EXT_blend_minmax = true;
195 	ctx->Extensions.EXT_fog_coord = true;
196 	ctx->Extensions.EXT_framebuffer_blit = true;
197 	ctx->Extensions.EXT_framebuffer_object = true;
198 	ctx->Extensions.EXT_packed_depth_stencil = true;
199 	ctx->Extensions.EXT_secondary_color = true;
200 	ctx->Extensions.EXT_texture_filter_anisotropic = true;
201 	ctx->Extensions.NV_blend_square = true;
202 	ctx->Extensions.NV_texture_env_combine4 = true;
203 
204 	return GL_TRUE;
205 }
206 
207 void
nouveau_context_deinit(struct gl_context * ctx)208 nouveau_context_deinit(struct gl_context *ctx)
209 {
210 	struct nouveau_context *nctx = to_nouveau_context(ctx);
211 
212 	if (TNL_CONTEXT(ctx))
213 		_tnl_DestroyContext(ctx);
214 
215 	if (vbo_context(ctx))
216 		_vbo_DestroyContext(ctx);
217 
218 	if (SWRAST_CONTEXT(ctx))
219 		_swrast_DestroyContext(ctx);
220 
221 	if (ctx->Meta)
222 		_mesa_meta_free(ctx);
223 
224 	nouveau_bufctx_del(&nctx->hw.bufctx);
225 	nouveau_pushbuf_del(&nctx->hw.pushbuf);
226 	nouveau_client_del(&nctx->hw.client);
227 	nouveau_object_del(&nctx->hw.chan);
228 
229 	nouveau_scratch_destroy(ctx);
230 	_mesa_free_context_data(ctx);
231 }
232 
233 void
nouveau_context_destroy(__DRIcontext * dri_ctx)234 nouveau_context_destroy(__DRIcontext *dri_ctx)
235 {
236 	struct nouveau_context *nctx = dri_ctx->driverPrivate;
237 	struct gl_context *ctx = &nctx->base;
238 
239 	nouveau_bo_ref(NULL, &nctx->fence);
240 	context_drv(ctx)->context_destroy(ctx);
241 }
242 
243 void
nouveau_update_renderbuffers(__DRIcontext * dri_ctx,__DRIdrawable * draw)244 nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
245 {
246 	struct gl_context *ctx = dri_ctx->driverPrivate;
247 	struct nouveau_context *nctx = to_nouveau_context(ctx);
248 	__DRIscreen *screen = dri_ctx->driScreenPriv;
249 	struct gl_framebuffer *fb = draw->driverPrivate;
250 	struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
251 	unsigned int attachments[10];
252 	__DRIbuffer *buffers = NULL;
253 	int i = 0, count, ret;
254 
255 	if (draw->lastStamp == draw->dri2.stamp)
256 		return;
257 	draw->lastStamp = draw->dri2.stamp;
258 
259 	if (nfb->need_front)
260 		attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
261 	if (fb->Visual.doubleBufferMode)
262 		attachments[i++] = __DRI_BUFFER_BACK_LEFT;
263 	if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer)
264 		attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
265 	else if (fb->Visual.haveDepthBuffer)
266 		attachments[i++] = __DRI_BUFFER_DEPTH;
267 	else if (fb->Visual.haveStencilBuffer)
268 		attachments[i++] = __DRI_BUFFER_STENCIL;
269 
270 	buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
271 						     attachments, i, &count,
272 						     draw->loaderPrivate);
273 	if (buffers == NULL)
274 		return;
275 
276 	for (i = 0; i < count; i++) {
277 		struct gl_renderbuffer *rb;
278 		struct nouveau_surface *s;
279 		uint32_t old_name;
280 		int index;
281 
282 		switch (buffers[i].attachment) {
283 		case __DRI_BUFFER_FRONT_LEFT:
284 		case __DRI_BUFFER_FAKE_FRONT_LEFT:
285 			index = BUFFER_FRONT_LEFT;
286 			break;
287 		case __DRI_BUFFER_BACK_LEFT:
288 			index = BUFFER_BACK_LEFT;
289 			break;
290 		case __DRI_BUFFER_DEPTH:
291 		case __DRI_BUFFER_DEPTH_STENCIL:
292 			index = BUFFER_DEPTH;
293 			break;
294 		case __DRI_BUFFER_STENCIL:
295 			index = BUFFER_STENCIL;
296 			break;
297 		default:
298 			assert(0);
299 		}
300 
301 		rb = fb->Attachment[index].Renderbuffer;
302 		s = &to_nouveau_renderbuffer(rb)->surface;
303 
304 		s->width = draw->w;
305 		s->height = draw->h;
306 		s->pitch = buffers[i].pitch;
307 		s->cpp = buffers[i].cpp;
308 
309 		if (index == BUFFER_DEPTH && s->bo) {
310 			ret = nouveau_bo_name_get(s->bo, &old_name);
311 			/*
312 			 * Disable fast Z clears in the next frame, the
313 			 * depth buffer contents are undefined.
314 			 */
315 			if (!ret && old_name != buffers[i].name)
316 				nctx->hierz.clear_seq = 0;
317 		}
318 
319 		nouveau_bo_ref(NULL, &s->bo);
320 		ret = nouveau_bo_name_ref(context_dev(ctx),
321 					  buffers[i].name, &s->bo);
322 		assert(!ret);
323 	}
324 
325 	_mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
326 }
327 
328 static void
update_framebuffer(__DRIcontext * dri_ctx,__DRIdrawable * draw,int * stamp)329 update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
330 		   int *stamp)
331 {
332 	struct gl_context *ctx = dri_ctx->driverPrivate;
333 	struct gl_framebuffer *fb = draw->driverPrivate;
334 
335 	*stamp = draw->dri2.stamp;
336 
337 	nouveau_update_renderbuffers(dri_ctx, draw);
338 	_mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
339 
340 	/* Clean up references to the old framebuffer objects. */
341 	context_dirty(ctx, FRAMEBUFFER);
342 	nouveau_bufctx_reset(to_nouveau_context(ctx)->hw.bufctx, BUFCTX_FB);
343 	PUSH_KICK(context_push(ctx));
344 }
345 
346 GLboolean
nouveau_context_make_current(__DRIcontext * dri_ctx,__DRIdrawable * dri_draw,__DRIdrawable * dri_read)347 nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
348 			     __DRIdrawable *dri_read)
349 {
350 	if (dri_ctx) {
351 		struct nouveau_context *nctx = dri_ctx->driverPrivate;
352 		struct gl_context *ctx = &nctx->base;
353 
354 		/* Ask the X server for new renderbuffers. */
355 		if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
356 			update_framebuffer(dri_ctx, dri_draw,
357 					   &dri_ctx->dri2.draw_stamp);
358 
359 		if (dri_draw != dri_read &&
360 		    dri_read->driverPrivate != ctx->WinSysReadBuffer)
361 			update_framebuffer(dri_ctx, dri_read,
362 					   &dri_ctx->dri2.read_stamp);
363 
364 		/* Pass it down to mesa. */
365 		_mesa_make_current(ctx, dri_draw->driverPrivate,
366 				   dri_read->driverPrivate);
367 		_mesa_update_state(ctx);
368 
369 	} else {
370 		_mesa_make_current(NULL, NULL, NULL);
371 	}
372 
373 	return GL_TRUE;
374 }
375 
376 GLboolean
nouveau_context_unbind(__DRIcontext * dri_ctx)377 nouveau_context_unbind(__DRIcontext *dri_ctx)
378 {
379 	/* Unset current context and dispatch table */
380 	_mesa_make_current(NULL, NULL, NULL);
381 
382 	return GL_TRUE;
383 }
384 
385 void
nouveau_fallback(struct gl_context * ctx,enum nouveau_fallback mode)386 nouveau_fallback(struct gl_context *ctx, enum nouveau_fallback mode)
387 {
388 	struct nouveau_context *nctx = to_nouveau_context(ctx);
389 
390 	nctx->fallback = MAX2(HWTNL, mode);
391 
392 	if (mode < SWRAST) {
393 		nouveau_state_emit(ctx);
394 #if 0
395 		nouveau_bo_state_emit(ctx);
396 #endif
397 	} else {
398 		PUSH_KICK(context_push(ctx));
399 	}
400 }
401 
402 static void
validate_framebuffer(__DRIcontext * dri_ctx,__DRIdrawable * draw,int * stamp)403 validate_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
404 		     int *stamp)
405 {
406 	struct gl_framebuffer *fb = draw->driverPrivate;
407 	struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
408 	GLboolean need_front =
409 		(fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT ||
410 		 fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT);
411 
412 	if (nfb->need_front != need_front) {
413 		nfb->need_front = need_front;
414 		dri2InvalidateDrawable(draw);
415 	}
416 
417 	if (draw->dri2.stamp != *stamp)
418 		update_framebuffer(dri_ctx, draw, stamp);
419 }
420 
421 void
nouveau_validate_framebuffer(struct gl_context * ctx)422 nouveau_validate_framebuffer(struct gl_context *ctx)
423 {
424 	__DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
425 	__DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
426 	__DRIdrawable *dri_read = dri_ctx->driReadablePriv;
427 
428 	if (_mesa_is_winsys_fbo(ctx->DrawBuffer))
429 		validate_framebuffer(dri_ctx, dri_draw,
430 				     &dri_ctx->dri2.draw_stamp);
431 
432 	if (_mesa_is_winsys_fbo(ctx->ReadBuffer))
433 		validate_framebuffer(dri_ctx, dri_read,
434 				     &dri_ctx->dri2.read_stamp);
435 
436 	if (ctx->NewState & _NEW_BUFFERS)
437 		_mesa_update_state(ctx);
438 }
439