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