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_fbo.h"
29 #include "nouveau_context.h"
30 #include "nouveau_texture.h"
32 #include "main/framebuffer.h"
33 #include "main/renderbuffer.h"
34 #include "main/fbobject.h"
35 #include "util/u_memory.h"
38 set_renderbuffer_format(struct gl_renderbuffer
*rb
, GLenum internalFormat
)
40 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
42 rb
->InternalFormat
= internalFormat
;
44 switch (internalFormat
) {
47 rb
->_BaseFormat
= GL_RGB
;
48 rb
->Format
= MESA_FORMAT_B8G8R8X8_UNORM
;
53 rb
->_BaseFormat
= GL_RGBA
;
54 rb
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
58 rb
->_BaseFormat
= GL_RGB
;
59 rb
->Format
= MESA_FORMAT_B5G6R5_UNORM
;
62 case GL_DEPTH_COMPONENT16
:
63 rb
->_BaseFormat
= GL_DEPTH_COMPONENT
;
64 rb
->Format
= MESA_FORMAT_Z_UNORM16
;
67 case GL_DEPTH_COMPONENT
:
68 case GL_DEPTH_COMPONENT24
:
69 case GL_STENCIL_INDEX8_EXT
:
70 case GL_DEPTH24_STENCIL8_EXT
:
71 rb
->_BaseFormat
= GL_DEPTH_STENCIL
;
72 rb
->Format
= MESA_FORMAT_S8_UINT_Z24_UNORM
;
79 s
->format
= rb
->Format
;
85 nouveau_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
86 GLenum internalFormat
,
87 GLuint width
, GLuint height
)
89 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
91 if (!set_renderbuffer_format(rb
, internalFormat
))
97 nouveau_surface_alloc(ctx
, s
, TILED
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_MAP
,
98 rb
->Format
, width
, height
);
100 context_dirty(ctx
, FRAMEBUFFER
);
105 nouveau_renderbuffer_del(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
107 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
109 nouveau_surface_ref(NULL
, s
);
110 _mesa_delete_renderbuffer(ctx
, rb
);
113 static struct gl_renderbuffer
*
114 nouveau_renderbuffer_new(struct gl_context
*ctx
, GLuint name
)
116 struct gl_renderbuffer
*rb
;
118 rb
= (struct gl_renderbuffer
*)
119 CALLOC_STRUCT(nouveau_renderbuffer
);
123 _mesa_init_renderbuffer(rb
, name
);
125 rb
->AllocStorage
= nouveau_renderbuffer_storage
;
126 rb
->Delete
= nouveau_renderbuffer_del
;
132 nouveau_renderbuffer_map(struct gl_context
*ctx
,
133 struct gl_renderbuffer
*rb
,
134 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
140 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
145 /* driver does not support GL_FRAMEBUFFER_FLIP_Y_MESA */
146 assert((rb
->Name
== 0) == flip_y
);
148 if (mode
& GL_MAP_READ_BIT
)
149 flags
|= NOUVEAU_BO_RD
;
150 if (mode
& GL_MAP_WRITE_BIT
)
151 flags
|= NOUVEAU_BO_WR
;
153 nouveau_bo_map(s
->bo
, flags
, context_client(ctx
));
159 map
+= stride
* (rb
->Height
- 1);
164 map
+= (int)y
* stride
;
167 *out_stride
= stride
;
171 nouveau_renderbuffer_unmap(struct gl_context
*ctx
,
172 struct gl_renderbuffer
*rb
)
177 nouveau_renderbuffer_dri_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
178 GLenum internalFormat
,
179 GLuint width
, GLuint height
)
181 if (!set_renderbuffer_format(rb
, internalFormat
))
190 struct gl_renderbuffer
*
191 nouveau_renderbuffer_dri_new(GLenum format
, __DRIdrawable
*drawable
)
193 struct gl_renderbuffer
*rb
;
195 rb
= nouveau_renderbuffer_new(NULL
, 0);
199 rb
->AllocStorage
= nouveau_renderbuffer_dri_storage
;
201 if (!set_renderbuffer_format(rb
, format
)) {
202 nouveau_renderbuffer_del(NULL
, rb
);
209 static struct gl_framebuffer
*
210 nouveau_framebuffer_new(struct gl_context
*ctx
, GLuint name
)
212 struct nouveau_framebuffer
*nfb
;
214 nfb
= CALLOC_STRUCT(nouveau_framebuffer
);
218 _mesa_initialize_user_framebuffer(&nfb
->base
, name
);
223 struct gl_framebuffer
*
224 nouveau_framebuffer_dri_new(const struct gl_config
*visual
)
226 struct nouveau_framebuffer
*nfb
;
228 nfb
= CALLOC_STRUCT(nouveau_framebuffer
);
232 _mesa_initialize_window_framebuffer(&nfb
->base
, visual
);
233 nfb
->need_front
= !visual
->doubleBufferMode
;
239 nouveau_bind_framebuffer(struct gl_context
*ctx
, GLenum target
,
240 struct gl_framebuffer
*dfb
,
241 struct gl_framebuffer
*rfb
)
243 context_dirty(ctx
, FRAMEBUFFER
);
247 nouveau_framebuffer_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
248 GLenum attachment
, struct gl_renderbuffer
*rb
)
250 _mesa_FramebufferRenderbuffer_sw(ctx
, fb
, attachment
, rb
);
252 context_dirty(ctx
, FRAMEBUFFER
);
256 nouveau_render_texture(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
257 struct gl_renderbuffer_attachment
*att
)
259 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
260 struct gl_texture_image
*ti
= rb
->TexImage
;
262 /* Update the renderbuffer fields from the texture. */
263 nouveau_surface_ref(&to_nouveau_teximage(ti
)->surface
,
264 &to_nouveau_renderbuffer(rb
)->surface
);
266 context_dirty(ctx
, FRAMEBUFFER
);
270 nouveau_finish_render_texture(struct gl_context
*ctx
,
271 struct gl_renderbuffer
*rb
)
273 if (rb
&& rb
->TexImage
)
274 texture_dirty(rb
->TexImage
->TexObject
);
278 validate_format_bpp(mesa_format format
)
281 case MESA_FORMAT_B8G8R8X8_UNORM
:
282 case MESA_FORMAT_B8G8R8A8_UNORM
:
283 case MESA_FORMAT_S8_UINT_Z24_UNORM
:
285 case MESA_FORMAT_B5G6R5_UNORM
:
286 case MESA_FORMAT_Z_UNORM16
:
294 nouveau_check_framebuffer_complete(struct gl_context
*ctx
,
295 struct gl_framebuffer
*fb
)
297 struct gl_renderbuffer_attachment
*color
=
298 &fb
->Attachment
[BUFFER_COLOR0
];
299 struct gl_renderbuffer_attachment
*depth
=
300 &fb
->Attachment
[BUFFER_DEPTH
];
301 int color_bpp
= 0, zeta_bpp
;
303 if (color
->Type
== GL_TEXTURE
) {
304 color_bpp
= validate_format_bpp(
305 color
->Renderbuffer
->TexImage
->TexFormat
);
310 if (depth
->Type
== GL_TEXTURE
) {
311 zeta_bpp
= validate_format_bpp(
312 depth
->Renderbuffer
->TexImage
->TexFormat
);
315 /* NV04/NV05 requires same bpp-ness for color/zeta */
316 if (context_chipset(ctx
) < 0x10 &&
317 color_bpp
&& color_bpp
!= zeta_bpp
)
323 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
328 nouveau_fbo_functions_init(struct dd_function_table
*functions
)
330 functions
->NewFramebuffer
= nouveau_framebuffer_new
;
331 functions
->NewRenderbuffer
= nouveau_renderbuffer_new
;
332 functions
->MapRenderbuffer
= nouveau_renderbuffer_map
;
333 functions
->UnmapRenderbuffer
= nouveau_renderbuffer_unmap
;
334 functions
->BindFramebuffer
= nouveau_bind_framebuffer
;
335 functions
->FramebufferRenderbuffer
= nouveau_framebuffer_renderbuffer
;
336 functions
->RenderTexture
= nouveau_render_texture
;
337 functions
->FinishRenderTexture
= nouveau_finish_render_texture
;
338 functions
->ValidateFramebuffer
= nouveau_check_framebuffer_complete
;