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 "main/mfeatures.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_XRGB8888;
53 rb->_BaseFormat = GL_RGBA;
54 rb->Format = MESA_FORMAT_ARGB8888;
58 rb->_BaseFormat = GL_RGB;
59 rb->Format = MESA_FORMAT_RGB565;
62 case GL_DEPTH_COMPONENT16:
63 rb->_BaseFormat = GL_DEPTH_COMPONENT;
64 rb->Format = MESA_FORMAT_Z16;
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_Z24_S8;
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,
139 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
144 if (mode & GL_MAP_READ_BIT)
145 flags |= NOUVEAU_BO_RD;
146 if (mode & GL_MAP_WRITE_BIT)
147 flags |= NOUVEAU_BO_WR;
149 nouveau_bo_map(s->bo, flags, context_client(ctx));
155 map += stride * (rb->Height - 1);
160 map += (int)y * stride;
163 *out_stride = stride;
167 nouveau_renderbuffer_unmap(struct gl_context *ctx,
168 struct gl_renderbuffer *rb)
173 nouveau_renderbuffer_dri_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
174 GLenum internalFormat,
175 GLuint width, GLuint height)
177 if (!set_renderbuffer_format(rb, internalFormat))
186 struct gl_renderbuffer *
187 nouveau_renderbuffer_dri_new(GLenum format, __DRIdrawable *drawable)
189 struct gl_renderbuffer *rb;
191 rb = nouveau_renderbuffer_new(NULL, 0);
195 rb->AllocStorage = nouveau_renderbuffer_dri_storage;
197 if (!set_renderbuffer_format(rb, format)) {
198 nouveau_renderbuffer_del(rb);
205 static struct gl_framebuffer *
206 nouveau_framebuffer_new(struct gl_context *ctx, GLuint name)
208 struct nouveau_framebuffer *nfb;
210 nfb = CALLOC_STRUCT(nouveau_framebuffer);
214 _mesa_initialize_user_framebuffer(&nfb->base, name);
219 struct gl_framebuffer *
220 nouveau_framebuffer_dri_new(const struct gl_config *visual)
222 struct nouveau_framebuffer *nfb;
224 nfb = CALLOC_STRUCT(nouveau_framebuffer);
228 _mesa_initialize_window_framebuffer(&nfb->base, visual);
229 nfb->need_front = !visual->doubleBufferMode;
235 nouveau_bind_framebuffer(struct gl_context *ctx, GLenum target,
236 struct gl_framebuffer *dfb,
237 struct gl_framebuffer *rfb)
239 context_dirty(ctx, FRAMEBUFFER);
243 nouveau_framebuffer_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
244 GLenum attachment, struct gl_renderbuffer *rb)
246 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
248 context_dirty(ctx, FRAMEBUFFER);
252 get_tex_format(struct gl_texture_image *ti)
254 switch (ti->TexFormat) {
255 case MESA_FORMAT_ARGB8888:
257 case MESA_FORMAT_XRGB8888:
259 case MESA_FORMAT_RGB565:
267 nouveau_render_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
268 struct gl_renderbuffer_attachment *att)
270 struct gl_renderbuffer *rb = att->Renderbuffer;
271 struct gl_texture_image *ti =
272 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
274 /* Allocate a renderbuffer object for the texture if we
275 * haven't already done so. */
277 rb = nouveau_renderbuffer_new(ctx, ~0);
280 rb->AllocStorage = NULL;
281 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
284 /* Update the renderbuffer fields from the texture. */
285 set_renderbuffer_format(rb, get_tex_format(ti));
286 rb->Width = ti->Width;
287 rb->Height = ti->Height;
288 nouveau_surface_ref(&to_nouveau_teximage(ti)->surface,
289 &to_nouveau_renderbuffer(rb)->surface);
291 context_dirty(ctx, FRAMEBUFFER);
295 nouveau_finish_render_texture(struct gl_context *ctx,
296 struct gl_renderbuffer_attachment *att)
298 texture_dirty(att->Texture);
302 nouveau_fbo_functions_init(struct dd_function_table *functions)
304 #if FEATURE_EXT_framebuffer_object
305 functions->NewFramebuffer = nouveau_framebuffer_new;
306 functions->NewRenderbuffer = nouveau_renderbuffer_new;
307 functions->MapRenderbuffer = nouveau_renderbuffer_map;
308 functions->UnmapRenderbuffer = nouveau_renderbuffer_unmap;
309 functions->BindFramebuffer = nouveau_bind_framebuffer;
310 functions->FramebufferRenderbuffer = nouveau_framebuffer_renderbuffer;
311 functions->RenderTexture = nouveau_render_texture;
312 functions->FinishRenderTexture = nouveau_finish_render_texture;