Merge branch 'lp-offset-twoside'
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_fbo.c
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
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:
12 *
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.
16 *
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.
24 *
25 */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_fbo.h"
29 #include "nouveau_context.h"
30 #include "nouveau_texture.h"
31
32 #include "main/framebuffer.h"
33 #include "main/renderbuffer.h"
34 #include "main/fbobject.h"
35
36 static GLboolean
37 set_renderbuffer_format(struct gl_renderbuffer *rb, GLenum internalFormat)
38 {
39 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
40
41 rb->InternalFormat = internalFormat;
42
43 switch (internalFormat) {
44 case GL_RGB:
45 case GL_RGB8:
46 rb->_BaseFormat = GL_RGB;
47 rb->Format = MESA_FORMAT_XRGB8888;
48 rb->DataType = GL_UNSIGNED_BYTE;
49 s->cpp = 4;
50 break;
51 case GL_RGBA:
52 case GL_RGBA8:
53 rb->_BaseFormat = GL_RGBA;
54 rb->Format = MESA_FORMAT_ARGB8888;
55 rb->DataType = GL_UNSIGNED_BYTE;
56 s->cpp = 4;
57 break;
58 case GL_RGB5:
59 rb->_BaseFormat = GL_RGB;
60 rb->Format = MESA_FORMAT_RGB565;
61 rb->DataType = GL_UNSIGNED_BYTE;
62 s->cpp = 2;
63 break;
64 case GL_DEPTH_COMPONENT16:
65 rb->_BaseFormat = GL_DEPTH_COMPONENT;
66 rb->Format = MESA_FORMAT_Z16;
67 rb->DataType = GL_UNSIGNED_SHORT;
68 s->cpp = 2;
69 break;
70 case GL_DEPTH_COMPONENT:
71 case GL_DEPTH_COMPONENT24:
72 case GL_STENCIL_INDEX8_EXT:
73 case GL_DEPTH24_STENCIL8_EXT:
74 rb->_BaseFormat = GL_DEPTH_STENCIL;
75 rb->Format = MESA_FORMAT_Z24_S8;
76 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
77 s->cpp = 4;
78 break;
79 default:
80 return GL_FALSE;
81 }
82
83 s->format = rb->Format;
84
85 return GL_TRUE;
86 }
87
88 static GLboolean
89 nouveau_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
90 GLenum internalFormat,
91 GLuint width, GLuint height)
92 {
93 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
94
95 if (!set_renderbuffer_format(rb, internalFormat))
96 return GL_FALSE;
97
98 rb->Width = width;
99 rb->Height = height;
100
101 nouveau_surface_alloc(ctx, s, TILED, NOUVEAU_BO_VRAM | NOUVEAU_BO_MAP,
102 rb->Format, width, height);
103
104 context_dirty(ctx, FRAMEBUFFER);
105 return GL_TRUE;
106 }
107
108 static void
109 nouveau_renderbuffer_del(struct gl_renderbuffer *rb)
110 {
111 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
112
113 nouveau_surface_ref(NULL, s);
114 FREE(rb);
115 }
116
117 static struct gl_renderbuffer *
118 nouveau_renderbuffer_new(struct gl_context *ctx, GLuint name)
119 {
120 struct gl_renderbuffer *rb;
121
122 rb = (struct gl_renderbuffer *)
123 CALLOC_STRUCT(nouveau_renderbuffer);
124 if (!rb)
125 return NULL;
126
127 _mesa_init_renderbuffer(rb, name);
128
129 rb->AllocStorage = nouveau_renderbuffer_storage;
130 rb->Delete = nouveau_renderbuffer_del;
131
132 return rb;
133 }
134
135 static GLboolean
136 nouveau_renderbuffer_dri_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
137 GLenum internalFormat,
138 GLuint width, GLuint height)
139 {
140 if (!set_renderbuffer_format(rb, internalFormat))
141 return GL_FALSE;
142
143 rb->Width = width;
144 rb->Height = height;
145
146 return GL_TRUE;
147 }
148
149 struct gl_renderbuffer *
150 nouveau_renderbuffer_dri_new(GLenum format, __DRIdrawable *drawable)
151 {
152 struct gl_renderbuffer *rb;
153
154 rb = nouveau_renderbuffer_new(NULL, 0);
155 if (!rb)
156 return NULL;
157
158 rb->AllocStorage = nouveau_renderbuffer_dri_storage;
159
160 if (!set_renderbuffer_format(rb, format)) {
161 nouveau_renderbuffer_del(rb);
162 return NULL;
163 }
164
165 return rb;
166 }
167
168 static struct gl_framebuffer *
169 nouveau_framebuffer_new(struct gl_context *ctx, GLuint name)
170 {
171 struct nouveau_framebuffer *nfb;
172
173 nfb = CALLOC_STRUCT(nouveau_framebuffer);
174 if (!nfb)
175 return NULL;
176
177 _mesa_initialize_user_framebuffer(&nfb->base, name);
178
179 return &nfb->base;
180 }
181
182 struct gl_framebuffer *
183 nouveau_framebuffer_dri_new(const struct gl_config *visual)
184 {
185 struct nouveau_framebuffer *nfb;
186
187 nfb = CALLOC_STRUCT(nouveau_framebuffer);
188 if (!nfb)
189 return NULL;
190
191 _mesa_initialize_window_framebuffer(&nfb->base, visual);
192 nfb->need_front = !visual->doubleBufferMode;
193
194 return &nfb->base;
195 }
196
197 static void
198 nouveau_bind_framebuffer(struct gl_context *ctx, GLenum target,
199 struct gl_framebuffer *dfb,
200 struct gl_framebuffer *rfb)
201 {
202 context_dirty(ctx, FRAMEBUFFER);
203 }
204
205 static void
206 nouveau_framebuffer_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
207 GLenum attachment, struct gl_renderbuffer *rb)
208 {
209 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
210
211 context_dirty(ctx, FRAMEBUFFER);
212 }
213
214 static GLenum
215 get_tex_format(struct gl_texture_image *ti)
216 {
217 switch (ti->TexFormat) {
218 case MESA_FORMAT_ARGB8888:
219 return GL_RGBA8;
220 case MESA_FORMAT_XRGB8888:
221 return GL_RGB8;
222 case MESA_FORMAT_RGB565:
223 return GL_RGB5;
224 default:
225 return GL_NONE;
226 }
227 }
228
229 static void
230 nouveau_render_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
231 struct gl_renderbuffer_attachment *att)
232 {
233 struct gl_renderbuffer *rb = att->Renderbuffer;
234 struct gl_texture_image *ti =
235 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
236
237 /* Allocate a renderbuffer object for the texture if we
238 * haven't already done so. */
239 if (!rb) {
240 rb = nouveau_renderbuffer_new(ctx, ~0);
241 assert(rb);
242
243 rb->AllocStorage = NULL;
244 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
245 }
246
247 /* Update the renderbuffer fields from the texture. */
248 set_renderbuffer_format(rb, get_tex_format(ti));
249 rb->Width = ti->Width;
250 rb->Height = ti->Height;
251 nouveau_surface_ref(&to_nouveau_teximage(ti)->surface,
252 &to_nouveau_renderbuffer(rb)->surface);
253
254 context_dirty(ctx, FRAMEBUFFER);
255 }
256
257 static void
258 nouveau_finish_render_texture(struct gl_context *ctx,
259 struct gl_renderbuffer_attachment *att)
260 {
261 texture_dirty(att->Texture);
262 }
263
264 void
265 nouveau_fbo_functions_init(struct dd_function_table *functions)
266 {
267 #if FEATURE_EXT_framebuffer_object
268 functions->NewFramebuffer = nouveau_framebuffer_new;
269 functions->NewRenderbuffer = nouveau_renderbuffer_new;
270 functions->BindFramebuffer = nouveau_bind_framebuffer;
271 functions->FramebufferRenderbuffer = nouveau_framebuffer_renderbuffer;
272 functions->RenderTexture = nouveau_render_texture;
273 functions->FinishRenderTexture = nouveau_finish_render_texture;
274 #endif
275 }