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"
37 set_renderbuffer_format(struct gl_renderbuffer
*rb
, GLenum internalFormat
)
39 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
41 rb
->InternalFormat
= internalFormat
;
43 switch (internalFormat
) {
46 rb
->_BaseFormat
= GL_RGB
;
47 rb
->Format
= MESA_FORMAT_B8G8R8X8_UNORM
;
52 rb
->_BaseFormat
= GL_RGBA
;
53 rb
->Format
= MESA_FORMAT_B8G8R8A8_UNORM
;
57 rb
->_BaseFormat
= GL_RGB
;
58 rb
->Format
= MESA_FORMAT_B5G6R5_UNORM
;
61 case GL_DEPTH_COMPONENT16
:
62 rb
->_BaseFormat
= GL_DEPTH_COMPONENT
;
63 rb
->Format
= MESA_FORMAT_Z_UNORM16
;
66 case GL_DEPTH_COMPONENT
:
67 case GL_DEPTH_COMPONENT24
:
68 case GL_STENCIL_INDEX8_EXT
:
69 case GL_DEPTH24_STENCIL8_EXT
:
70 rb
->_BaseFormat
= GL_DEPTH_STENCIL
;
71 rb
->Format
= MESA_FORMAT_S8_UINT_Z24_UNORM
;
78 s
->format
= rb
->Format
;
84 nouveau_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
85 GLenum internalFormat
,
86 GLuint width
, GLuint height
)
88 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
90 if (!set_renderbuffer_format(rb
, internalFormat
))
96 nouveau_surface_alloc(ctx
, s
, TILED
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_MAP
,
97 rb
->Format
, width
, height
);
99 context_dirty(ctx
, FRAMEBUFFER
);
104 nouveau_renderbuffer_del(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
)
106 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
108 nouveau_surface_ref(NULL
, s
);
109 _mesa_delete_renderbuffer(ctx
, rb
);
112 static struct gl_renderbuffer
*
113 nouveau_renderbuffer_new(struct gl_context
*ctx
, GLuint name
)
115 struct gl_renderbuffer
*rb
;
117 rb
= (struct gl_renderbuffer
*)
118 CALLOC_STRUCT(nouveau_renderbuffer
);
122 _mesa_init_renderbuffer(rb
, name
);
124 rb
->AllocStorage
= nouveau_renderbuffer_storage
;
125 rb
->Delete
= nouveau_renderbuffer_del
;
131 nouveau_renderbuffer_map(struct gl_context
*ctx
,
132 struct gl_renderbuffer
*rb
,
133 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
138 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
143 if (mode
& GL_MAP_READ_BIT
)
144 flags
|= NOUVEAU_BO_RD
;
145 if (mode
& GL_MAP_WRITE_BIT
)
146 flags
|= NOUVEAU_BO_WR
;
148 nouveau_bo_map(s
->bo
, flags
, context_client(ctx
));
154 map
+= stride
* (rb
->Height
- 1);
159 map
+= (int)y
* stride
;
162 *out_stride
= stride
;
166 nouveau_renderbuffer_unmap(struct gl_context
*ctx
,
167 struct gl_renderbuffer
*rb
)
172 nouveau_renderbuffer_dri_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
173 GLenum internalFormat
,
174 GLuint width
, GLuint height
)
176 if (!set_renderbuffer_format(rb
, internalFormat
))
185 struct gl_renderbuffer
*
186 nouveau_renderbuffer_dri_new(GLenum format
, __DRIdrawable
*drawable
)
188 struct gl_renderbuffer
*rb
;
190 rb
= nouveau_renderbuffer_new(NULL
, 0);
194 rb
->AllocStorage
= nouveau_renderbuffer_dri_storage
;
196 if (!set_renderbuffer_format(rb
, format
)) {
197 nouveau_renderbuffer_del(NULL
, rb
);
204 static struct gl_framebuffer
*
205 nouveau_framebuffer_new(struct gl_context
*ctx
, GLuint name
)
207 struct nouveau_framebuffer
*nfb
;
209 nfb
= CALLOC_STRUCT(nouveau_framebuffer
);
213 _mesa_initialize_user_framebuffer(&nfb
->base
, name
);
218 struct gl_framebuffer
*
219 nouveau_framebuffer_dri_new(const struct gl_config
*visual
)
221 struct nouveau_framebuffer
*nfb
;
223 nfb
= CALLOC_STRUCT(nouveau_framebuffer
);
227 _mesa_initialize_window_framebuffer(&nfb
->base
, visual
);
228 nfb
->need_front
= !visual
->doubleBufferMode
;
234 nouveau_bind_framebuffer(struct gl_context
*ctx
, GLenum target
,
235 struct gl_framebuffer
*dfb
,
236 struct gl_framebuffer
*rfb
)
238 context_dirty(ctx
, FRAMEBUFFER
);
242 nouveau_framebuffer_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
243 GLenum attachment
, struct gl_renderbuffer
*rb
)
245 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
247 context_dirty(ctx
, FRAMEBUFFER
);
251 nouveau_render_texture(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
252 struct gl_renderbuffer_attachment
*att
)
254 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
255 struct gl_texture_image
*ti
= rb
->TexImage
;
257 /* Update the renderbuffer fields from the texture. */
258 nouveau_surface_ref(&to_nouveau_teximage(ti
)->surface
,
259 &to_nouveau_renderbuffer(rb
)->surface
);
261 context_dirty(ctx
, FRAMEBUFFER
);
265 nouveau_finish_render_texture(struct gl_context
*ctx
,
266 struct gl_renderbuffer
*rb
)
268 texture_dirty(rb
->TexImage
->TexObject
);
272 validate_format_bpp(mesa_format format
)
275 case MESA_FORMAT_B8G8R8X8_UNORM
:
276 case MESA_FORMAT_B8G8R8A8_UNORM
:
277 case MESA_FORMAT_S8_UINT_Z24_UNORM
:
279 case MESA_FORMAT_B5G6R5_UNORM
:
280 case MESA_FORMAT_Z_UNORM16
:
288 nouveau_check_framebuffer_complete(struct gl_context
*ctx
,
289 struct gl_framebuffer
*fb
)
291 struct gl_renderbuffer_attachment
*color
=
292 &fb
->Attachment
[BUFFER_COLOR0
];
293 struct gl_renderbuffer_attachment
*depth
=
294 &fb
->Attachment
[BUFFER_DEPTH
];
295 int color_bpp
= 0, zeta_bpp
;
297 if (color
->Type
== GL_TEXTURE
) {
298 color_bpp
= validate_format_bpp(
299 color
->Renderbuffer
->TexImage
->TexFormat
);
304 if (depth
->Type
== GL_TEXTURE
) {
305 zeta_bpp
= validate_format_bpp(
306 depth
->Renderbuffer
->TexImage
->TexFormat
);
309 /* NV04/NV05 requires same bpp-ness for color/zeta */
310 if (context_chipset(ctx
) < 0x10 &&
311 color_bpp
&& color_bpp
!= zeta_bpp
)
317 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
322 nouveau_fbo_functions_init(struct dd_function_table
*functions
)
324 functions
->NewFramebuffer
= nouveau_framebuffer_new
;
325 functions
->NewRenderbuffer
= nouveau_renderbuffer_new
;
326 functions
->MapRenderbuffer
= nouveau_renderbuffer_map
;
327 functions
->UnmapRenderbuffer
= nouveau_renderbuffer_unmap
;
328 functions
->BindFramebuffer
= nouveau_bind_framebuffer
;
329 functions
->FramebufferRenderbuffer
= nouveau_framebuffer_renderbuffer
;
330 functions
->RenderTexture
= nouveau_render_texture
;
331 functions
->FinishRenderTexture
= nouveau_finish_render_texture
;
332 functions
->ValidateFramebuffer
= nouveau_check_framebuffer_complete
;