2 * Copyright (C) 2009 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.
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_bufferobj.h"
30 #include "nouveau_fbo.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"
42 #define need_GL_EXT_framebuffer_object
43 #define need_GL_EXT_fog_coord
45 #include "main/remap_helper.h"
47 static const struct dri_extension nouveau_extensions
[] = {
48 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions
},
49 { "GL_ARB_multitexture", NULL
},
50 { "GL_EXT_texture_lod_bias", NULL
},
51 { "GL_SGIS_generate_mipmap", NULL
},
52 { "GL_ARB_texture_env_combine", NULL
},
53 { "GL_ARB_texture_env_dot3", NULL
},
54 { "GL_ARB_texture_env_add", NULL
},
55 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions
},
60 nouveau_context_create(const __GLcontextModes
*visual
, __DRIcontext
*dri_ctx
,
63 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
64 struct nouveau_screen
*screen
= dri_screen
->private;
65 struct nouveau_context
*nctx
;
68 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
72 nctx
= to_nouveau_context(ctx
);
73 nctx
->dri_context
= dri_ctx
;
74 dri_ctx
->driverPrivate
= ctx
;
80 nouveau_context_init(GLcontext
*ctx
, struct nouveau_screen
*screen
,
81 const GLvisual
*visual
, GLcontext
*share_ctx
)
83 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
84 struct dd_function_table functions
;
86 nctx
->screen
= screen
;
87 nctx
->fallback
= HWTNL
;
89 /* Initialize the function pointers */
90 _mesa_init_driver_functions(&functions
);
91 nouveau_driver_functions_init(&functions
);
92 nouveau_bufferobj_functions_init(&functions
);
93 nouveau_texture_functions_init(&functions
);
94 nouveau_fbo_functions_init(&functions
);
96 /* Initialize the mesa context */
97 _mesa_initialize_context(ctx
, visual
, share_ctx
, &functions
, NULL
);
99 nouveau_state_init(ctx
);
100 nouveau_bo_state_init(ctx
);
101 _mesa_meta_init(ctx
);
102 _swrast_CreateContext(ctx
);
103 _vbo_CreateContext(ctx
);
104 _tnl_CreateContext(ctx
);
105 nouveau_span_functions_init(ctx
);
106 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
108 /* Enable any supported extensions */
109 driInitExtensions(ctx
, nouveau_extensions
, GL_TRUE
);
115 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
117 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
118 GLcontext
*ctx
= &nctx
->base
;
120 if (nctx
->screen
->context
== nctx
)
121 nctx
->screen
->context
= NULL
;
123 _tnl_DestroyContext(ctx
);
124 _vbo_DestroyContext(ctx
);
125 _swrast_DestroyContext(ctx
);
126 _mesa_meta_free(ctx
);
127 nouveau_bo_state_destroy(ctx
);
128 context_drv(ctx
)->context_destroy(ctx
);
132 nouveau_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
,
135 struct nouveau_context
*nctx
= context
->driverPrivate
;
136 GLcontext
*ctx
= &nctx
->base
;
137 __DRIscreen
*screen
= context
->driScreenPriv
;
138 struct gl_framebuffer
*fb
= drawable
->driverPrivate
;
139 unsigned int attachments
[10];
140 __DRIbuffer
*buffers
= NULL
;
141 int i
= 0, count
, ret
;
143 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
144 if (fb
->Visual
.doubleBufferMode
)
145 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
146 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
147 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
148 else if (fb
->Visual
.haveDepthBuffer
)
149 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
150 else if (fb
->Visual
.haveStencilBuffer
)
151 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
153 buffers
= (*screen
->dri2
.loader
->getBuffers
)(drawable
,
154 &drawable
->w
, &drawable
->h
,
155 attachments
, i
, &count
,
156 drawable
->loaderPrivate
);
160 for (i
= 0; i
< count
; i
++) {
161 struct gl_renderbuffer
*rb
;
162 struct nouveau_surface
*s
;
166 switch (buffers
[i
].attachment
) {
167 case __DRI_BUFFER_FRONT_LEFT
:
168 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
169 index
= BUFFER_FRONT_LEFT
;
171 case __DRI_BUFFER_BACK_LEFT
:
172 index
= BUFFER_BACK_LEFT
;
174 case __DRI_BUFFER_DEPTH
:
175 case __DRI_BUFFER_DEPTH_STENCIL
:
176 index
= BUFFER_DEPTH
;
178 case __DRI_BUFFER_STENCIL
:
179 index
= BUFFER_STENCIL
;
185 rb
= fb
->Attachment
[index
].Renderbuffer
;
186 s
= &to_nouveau_renderbuffer(rb
)->surface
;
188 s
->width
= drawable
->w
;
189 s
->height
= drawable
->h
;
190 s
->pitch
= buffers
[i
].pitch
;
191 s
->cpp
= buffers
[i
].cpp
;
193 /* Don't bother to reopen the bo if it happens to be
196 ret
= nouveau_bo_handle_get(s
->bo
, &old_handle
);
200 if (!s
->bo
|| old_handle
!= buffers
[i
].name
) {
201 nouveau_bo_ref(NULL
, &s
->bo
);
202 ret
= nouveau_bo_handle_ref(context_dev(ctx
),
203 buffers
[i
].name
, &s
->bo
);
206 context_dirty(ctx
, FRAMEBUFFER
);
210 _mesa_resize_framebuffer(ctx
, fb
, drawable
->w
, drawable
->h
);
214 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
215 __DRIdrawable
*dri_read
)
218 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
219 GLcontext
*ctx
= &nctx
->base
;
221 if (nctx
->screen
->context
!= nctx
) {
222 nctx
->screen
->context
= nctx
;
223 BITSET_ONES(nctx
->dirty
);
226 /* Ask the X server for new renderbuffers. */
227 nouveau_update_renderbuffers(dri_ctx
, dri_draw
,
228 &nctx
->drawable
.d_stamp
);
229 if (dri_draw
!= dri_read
)
230 nouveau_update_renderbuffers(dri_ctx
, dri_read
,
231 &nctx
->drawable
.r_stamp
);
233 /* Pass it down to mesa. */
234 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
235 dri_read
->driverPrivate
);
236 _mesa_update_state(ctx
);
238 FIRE_RING(context_chan(ctx
));
241 _mesa_make_current(NULL
, NULL
, NULL
);
248 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
254 nouveau_fallback(GLcontext
*ctx
, enum nouveau_fallback mode
)
256 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
258 nctx
->fallback
= MAX2(HWTNL
, mode
);
261 nouveau_state_emit(ctx
);
263 FIRE_RING(context_chan(ctx
));
267 nouveau_validate_framebuffer(GLcontext
*ctx
)
269 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
271 /* Someone's planning to draw something really soon. */
272 nctx
->drawable
.dirty
= GL_TRUE
;