2 * Copyright (C) 2009-2010 Francisco Jerez.
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:
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.
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.
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"
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"
46 #include "tnl/t_context.h"
49 nouveau_context_create(gl_api api
,
50 const struct gl_config
*visual
, __DRIcontext
*dri_ctx
,
51 unsigned major_version
,
52 unsigned minor_version
,
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
;
64 /* Do after-the-fact version checking (below).
68 /* NV10 and NV20 can support OpenGL ES 1.0 only. Older chips
69 * cannot do even that.
71 if ((screen
->device
->chipset
& 0xf0) == 0x00) {
72 *error
= __DRI_CTX_ERROR_BAD_API
;
74 } else if (minor_version
!= 0) {
75 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
81 *error
= __DRI_CTX_ERROR_BAD_API
;
85 /* API and flag filtering is handled in dri2CreateContextAttribs.
89 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
91 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
95 nctx
= to_nouveau_context(ctx
);
96 nctx
->dri_context
= dri_ctx
;
97 dri_ctx
->driverPrivate
= ctx
;
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
;
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
;
113 *error
= __DRI_CTX_ERROR_SUCCESS
;
118 nouveau_context_init(struct gl_context
*ctx
, struct nouveau_screen
*screen
,
119 const struct gl_config
*visual
, struct gl_context
*share_ctx
)
121 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
122 struct dd_function_table functions
;
125 nctx
->screen
= screen
;
126 nctx
->fallback
= HWTNL
;
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
);
135 /* Initialize the mesa context. */
136 _mesa_initialize_context(ctx
, API_OPENGL
, visual
,
137 share_ctx
, &functions
, NULL
);
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
);
148 /* Allocate a hardware channel. */
149 ret
= nouveau_object_new(&context_dev(ctx
)->object
, 0xbeef0000,
150 NOUVEAU_FIFO_CHANNEL_CLASS
,
154 }, sizeof(struct nv04_fifo
), &nctx
->hw
.chan
);
156 nouveau_error("Error initializing the FIFO.\n");
160 /* Allocate a client (thread data) */
161 ret
= nouveau_client_new(context_dev(ctx
), &nctx
->hw
.client
);
163 nouveau_error("Error creating thread data\n");
167 /* Allocate a push buffer */
168 ret
= nouveau_pushbuf_new(nctx
->hw
.client
, nctx
->hw
.chan
, 4,
169 512 * 1024, true, &nctx
->hw
.pushbuf
);
171 nouveau_error("Error allocating DMA push buffer\n");
175 /* Allocate buffer context */
176 ret
= nouveau_bufctx_new(nctx
->hw
.client
, 16, &nctx
->hw
.bufctx
);
178 nouveau_error("Error allocating buffer context\n");
182 nctx
->hw
.pushbuf
->user_priv
= nctx
->hw
.bufctx
;
184 /* Allocate NULL object */
185 ret
= nouveau_object_new(nctx
->hw
.chan
, 0x00000000, NV01_NULL_CLASS
,
186 NULL
, 0, &nctx
->hw
.null
);
188 nouveau_error("Error allocating NULL object\n");
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;
208 nouveau_context_deinit(struct gl_context
*ctx
)
210 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
212 if (TNL_CONTEXT(ctx
))
213 _tnl_DestroyContext(ctx
);
215 if (vbo_context(ctx
))
216 _vbo_DestroyContext(ctx
);
218 if (SWRAST_CONTEXT(ctx
))
219 _swrast_DestroyContext(ctx
);
222 _mesa_meta_free(ctx
);
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
);
229 nouveau_scratch_destroy(ctx
);
230 _mesa_free_context_data(ctx
);
234 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
236 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
237 struct gl_context
*ctx
= &nctx
->base
;
239 nouveau_bo_ref(NULL
, &nctx
->fence
);
240 context_drv(ctx
)->context_destroy(ctx
);
244 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
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
;
255 if (draw
->lastStamp
== draw
->dri2
.stamp
)
257 draw
->lastStamp
= draw
->dri2
.stamp
;
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
;
270 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
271 attachments
, i
, &count
,
272 draw
->loaderPrivate
);
276 for (i
= 0; i
< count
; i
++) {
277 struct gl_renderbuffer
*rb
;
278 struct nouveau_surface
*s
;
282 switch (buffers
[i
].attachment
) {
283 case __DRI_BUFFER_FRONT_LEFT
:
284 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
285 index
= BUFFER_FRONT_LEFT
;
287 case __DRI_BUFFER_BACK_LEFT
:
288 index
= BUFFER_BACK_LEFT
;
290 case __DRI_BUFFER_DEPTH
:
291 case __DRI_BUFFER_DEPTH_STENCIL
:
292 index
= BUFFER_DEPTH
;
294 case __DRI_BUFFER_STENCIL
:
295 index
= BUFFER_STENCIL
;
301 rb
= fb
->Attachment
[index
].Renderbuffer
;
302 s
= &to_nouveau_renderbuffer(rb
)->surface
;
306 s
->pitch
= buffers
[i
].pitch
;
307 s
->cpp
= buffers
[i
].cpp
;
309 if (index
== BUFFER_DEPTH
&& s
->bo
) {
310 ret
= nouveau_bo_name_get(s
->bo
, &old_name
);
312 * Disable fast Z clears in the next frame, the
313 * depth buffer contents are undefined.
315 if (!ret
&& old_name
!= buffers
[i
].name
)
316 nctx
->hierz
.clear_seq
= 0;
319 nouveau_bo_ref(NULL
, &s
->bo
);
320 ret
= nouveau_bo_name_ref(context_dev(ctx
),
321 buffers
[i
].name
, &s
->bo
);
325 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
329 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
332 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
333 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
335 *stamp
= draw
->dri2
.stamp
;
337 nouveau_update_renderbuffers(dri_ctx
, draw
);
338 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
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
));
347 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
348 __DRIdrawable
*dri_read
)
351 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
352 struct gl_context
*ctx
= &nctx
->base
;
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
);
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
);
364 /* Pass it down to mesa. */
365 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
366 dri_read
->driverPrivate
);
367 _mesa_update_state(ctx
);
370 _mesa_make_current(NULL
, NULL
, NULL
);
377 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
379 /* Unset current context and dispatch table */
380 _mesa_make_current(NULL
, NULL
, NULL
);
386 nouveau_fallback(struct gl_context
*ctx
, enum nouveau_fallback mode
)
388 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
390 nctx
->fallback
= MAX2(HWTNL
, mode
);
393 nouveau_state_emit(ctx
);
395 nouveau_bo_state_emit(ctx
);
398 PUSH_KICK(context_push(ctx
));
403 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
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
);
412 if (nfb
->need_front
!= need_front
) {
413 nfb
->need_front
= need_front
;
414 dri2InvalidateDrawable(draw
);
417 if (draw
->dri2
.stamp
!= *stamp
)
418 update_framebuffer(dri_ctx
, draw
, stamp
);
422 nouveau_validate_framebuffer(struct gl_context
*ctx
)
424 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
425 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
426 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
428 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
))
429 validate_framebuffer(dri_ctx
, dri_draw
,
430 &dri_ctx
->dri2
.draw_stamp
);
432 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
))
433 validate_framebuffer(dri_ctx
, dri_read
,
434 &dri_ctx
->dri2
.read_stamp
);
436 if (ctx
->NewState
& _NEW_BUFFERS
)
437 _mesa_update_state(ctx
);