Merge branch '7.8'
[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_COMPONENT24:
71 case GL_STENCIL_INDEX8_EXT:
72 case GL_DEPTH24_STENCIL8_EXT:
73 rb->_BaseFormat = GL_DEPTH_STENCIL;
74 rb->Format = MESA_FORMAT_Z24_S8;
75 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
76 s->cpp = 4;
77 break;
78 default:
79 return GL_FALSE;
80 }
81
82 s->format = rb->Format;
83
84 return GL_TRUE;
85 }
86
87 static GLboolean
88 nouveau_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
89 GLenum internalFormat,
90 GLuint width, GLuint height)
91 {
92 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
93
94 if (!set_renderbuffer_format(rb, internalFormat))
95 return GL_FALSE;
96
97 rb->Width = width;
98 rb->Height = height;
99
100 nouveau_surface_alloc(ctx, s, TILED, NOUVEAU_BO_VRAM | NOUVEAU_BO_MAP,
101 rb->Format, width, height);
102
103 context_dirty(ctx, FRAMEBUFFER);
104 return GL_TRUE;
105 }
106
107 static void
108 nouveau_renderbuffer_del(struct gl_renderbuffer *rb)
109 {
110 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
111
112 nouveau_surface_ref(NULL, s);
113 FREE(rb);
114 }
115
116 static struct gl_renderbuffer *
117 nouveau_renderbuffer_new(GLcontext *ctx, GLuint name)
118 {
119 struct gl_renderbuffer *rb;
120
121 rb = (struct gl_renderbuffer *)
122 CALLOC_STRUCT(nouveau_renderbuffer);
123 if (!rb)
124 return NULL;
125
126 _mesa_init_renderbuffer(rb, name);
127
128 rb->AllocStorage = nouveau_renderbuffer_storage;
129 rb->Delete = nouveau_renderbuffer_del;
130
131 return rb;
132 }
133
134 static GLboolean
135 nouveau_renderbuffer_dri_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
136 GLenum internalFormat,
137 GLuint width, GLuint height)
138 {
139 if (!set_renderbuffer_format(rb, internalFormat))
140 return GL_FALSE;
141
142 rb->Width = width;
143 rb->Height = height;
144
145 return GL_TRUE;
146 }
147
148 struct gl_renderbuffer *
149 nouveau_renderbuffer_dri_new(GLenum format, __DRIdrawable *drawable)
150 {
151 struct gl_renderbuffer *rb;
152
153 rb = nouveau_renderbuffer_new(NULL, 0);
154 if (!rb)
155 return NULL;
156
157 rb->AllocStorage = nouveau_renderbuffer_dri_storage;
158
159 if (!set_renderbuffer_format(rb, format)) {
160 nouveau_renderbuffer_del(rb);
161 return NULL;
162 }
163
164 return rb;
165 }
166
167 static struct gl_framebuffer *
168 nouveau_framebuffer_new(GLcontext *ctx, GLuint name)
169 {
170 struct nouveau_framebuffer *nfb;
171
172 nfb = CALLOC_STRUCT(nouveau_framebuffer);
173 if (!nfb)
174 return NULL;
175
176 _mesa_initialize_user_framebuffer(&nfb->base, name);
177
178 return &nfb->base;
179 }
180
181 struct gl_framebuffer *
182 nouveau_framebuffer_dri_new(const GLvisual *visual)
183 {
184 struct nouveau_framebuffer *nfb;
185
186 nfb = CALLOC_STRUCT(nouveau_framebuffer);
187 if (!nfb)
188 return NULL;
189
190 _mesa_initialize_window_framebuffer(&nfb->base, visual);
191
192 return &nfb->base;
193 }
194
195 static void
196 nouveau_bind_framebuffer(GLcontext *ctx, GLenum target,
197 struct gl_framebuffer *dfb,
198 struct gl_framebuffer *rfb)
199 {
200 context_dirty(ctx, FRAMEBUFFER);
201 }
202
203 static void
204 nouveau_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
205 GLenum attachment, struct gl_renderbuffer *rb)
206 {
207 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
208
209 context_dirty(ctx, FRAMEBUFFER);
210 }
211
212 static GLenum
213 get_tex_format(struct gl_texture_image *ti)
214 {
215 switch (ti->TexFormat) {
216 case MESA_FORMAT_ARGB8888:
217 return GL_RGBA8;
218 case MESA_FORMAT_XRGB8888:
219 return GL_RGB8;
220 case MESA_FORMAT_RGB565:
221 return GL_RGB5;
222 default:
223 assert(0);
224 }
225 }
226
227 static void
228 nouveau_render_texture(GLcontext *ctx, struct gl_framebuffer *fb,
229 struct gl_renderbuffer_attachment *att)
230 {
231 struct gl_renderbuffer *rb = att->Renderbuffer;
232 struct gl_texture_image *ti =
233 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
234 int ret;
235
236 /* Allocate a renderbuffer object for the texture if we
237 * haven't already done so. */
238 if (!rb) {
239 rb = nouveau_renderbuffer_new(ctx, ~0);
240 assert(rb);
241
242 rb->AllocStorage = NULL;
243 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
244 }
245
246 /* Update the renderbuffer fields from the texture. */
247 ret = set_renderbuffer_format(rb, get_tex_format(ti));
248 assert(ret);
249
250 rb->Width = ti->Width;
251 rb->Height = ti->Height;
252 nouveau_surface_ref(&to_nouveau_teximage(ti)->surface,
253 &to_nouveau_renderbuffer(rb)->surface);
254
255 context_dirty(ctx, FRAMEBUFFER);
256 }
257
258 static void
259 nouveau_finish_render_texture(GLcontext *ctx,
260 struct gl_renderbuffer_attachment *att)
261 {
262 texture_dirty(att->Texture);
263 }
264
265 void
266 nouveau_fbo_functions_init(struct dd_function_table *functions)
267 {
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 }