mesa: remove gl_renderbuffer::DataType
[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 #include "main/mfeatures.h"
36
37 static GLboolean
38 set_renderbuffer_format(struct gl_renderbuffer *rb, GLenum internalFormat)
39 {
40 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
41
42 rb->InternalFormat = internalFormat;
43
44 switch (internalFormat) {
45 case GL_RGB:
46 case GL_RGB8:
47 rb->_BaseFormat = GL_RGB;
48 rb->Format = MESA_FORMAT_XRGB8888;
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 s->cpp = 4;
56 break;
57 case GL_RGB5:
58 rb->_BaseFormat = GL_RGB;
59 rb->Format = MESA_FORMAT_RGB565;
60 s->cpp = 2;
61 break;
62 case GL_DEPTH_COMPONENT16:
63 rb->_BaseFormat = GL_DEPTH_COMPONENT;
64 rb->Format = MESA_FORMAT_Z16;
65 s->cpp = 2;
66 break;
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;
73 s->cpp = 4;
74 break;
75 default:
76 return GL_FALSE;
77 }
78
79 s->format = rb->Format;
80
81 return GL_TRUE;
82 }
83
84 static GLboolean
85 nouveau_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
86 GLenum internalFormat,
87 GLuint width, GLuint height)
88 {
89 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
90
91 if (!set_renderbuffer_format(rb, internalFormat))
92 return GL_FALSE;
93
94 rb->Width = width;
95 rb->Height = height;
96
97 nouveau_surface_alloc(ctx, s, TILED, NOUVEAU_BO_VRAM | NOUVEAU_BO_MAP,
98 rb->Format, width, height);
99
100 context_dirty(ctx, FRAMEBUFFER);
101 return GL_TRUE;
102 }
103
104 static void
105 nouveau_renderbuffer_del(struct gl_renderbuffer *rb)
106 {
107 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
108
109 nouveau_surface_ref(NULL, s);
110 FREE(rb);
111 }
112
113 static struct gl_renderbuffer *
114 nouveau_renderbuffer_new(struct gl_context *ctx, GLuint name)
115 {
116 struct gl_renderbuffer *rb;
117
118 rb = (struct gl_renderbuffer *)
119 CALLOC_STRUCT(nouveau_renderbuffer);
120 if (!rb)
121 return NULL;
122
123 _mesa_init_renderbuffer(rb, name);
124
125 rb->AllocStorage = nouveau_renderbuffer_storage;
126 rb->Delete = nouveau_renderbuffer_del;
127
128 return rb;
129 }
130
131 static void
132 nouveau_renderbuffer_map(struct gl_context *ctx,
133 struct gl_renderbuffer *rb,
134 GLuint x, GLuint y, GLuint w, GLuint h,
135 GLbitfield mode,
136 GLubyte **out_map,
137 GLint *out_stride)
138 {
139 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
140 GLubyte *map;
141 int stride;
142 int flags = 0;
143
144 if (mode & GL_MAP_READ_BIT)
145 flags |= NOUVEAU_BO_RD;
146 if (mode & GL_MAP_WRITE_BIT)
147 flags |= NOUVEAU_BO_WR;
148
149 nouveau_bo_map(s->bo, flags);
150
151 map = s->bo->map;
152 stride = s->pitch;
153
154 if (rb->Name == 0) {
155 map += stride * (rb->Height - 1);
156 stride = -stride;
157 }
158
159 map += x * s->cpp;
160 map += (int)y * stride;
161
162 *out_map = map;
163 *out_stride = stride;
164 }
165
166 static void
167 nouveau_renderbuffer_unmap(struct gl_context *ctx,
168 struct gl_renderbuffer *rb)
169 {
170 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
171
172 nouveau_bo_unmap(s->bo);
173 }
174
175 static GLboolean
176 nouveau_renderbuffer_dri_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
177 GLenum internalFormat,
178 GLuint width, GLuint height)
179 {
180 if (!set_renderbuffer_format(rb, internalFormat))
181 return GL_FALSE;
182
183 rb->Width = width;
184 rb->Height = height;
185
186 return GL_TRUE;
187 }
188
189 struct gl_renderbuffer *
190 nouveau_renderbuffer_dri_new(GLenum format, __DRIdrawable *drawable)
191 {
192 struct gl_renderbuffer *rb;
193
194 rb = nouveau_renderbuffer_new(NULL, 0);
195 if (!rb)
196 return NULL;
197
198 rb->AllocStorage = nouveau_renderbuffer_dri_storage;
199
200 if (!set_renderbuffer_format(rb, format)) {
201 nouveau_renderbuffer_del(rb);
202 return NULL;
203 }
204
205 return rb;
206 }
207
208 static struct gl_framebuffer *
209 nouveau_framebuffer_new(struct gl_context *ctx, GLuint name)
210 {
211 struct nouveau_framebuffer *nfb;
212
213 nfb = CALLOC_STRUCT(nouveau_framebuffer);
214 if (!nfb)
215 return NULL;
216
217 _mesa_initialize_user_framebuffer(&nfb->base, name);
218
219 return &nfb->base;
220 }
221
222 struct gl_framebuffer *
223 nouveau_framebuffer_dri_new(const struct gl_config *visual)
224 {
225 struct nouveau_framebuffer *nfb;
226
227 nfb = CALLOC_STRUCT(nouveau_framebuffer);
228 if (!nfb)
229 return NULL;
230
231 _mesa_initialize_window_framebuffer(&nfb->base, visual);
232 nfb->need_front = !visual->doubleBufferMode;
233
234 return &nfb->base;
235 }
236
237 static void
238 nouveau_bind_framebuffer(struct gl_context *ctx, GLenum target,
239 struct gl_framebuffer *dfb,
240 struct gl_framebuffer *rfb)
241 {
242 context_dirty(ctx, FRAMEBUFFER);
243 }
244
245 static void
246 nouveau_framebuffer_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
247 GLenum attachment, struct gl_renderbuffer *rb)
248 {
249 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
250
251 context_dirty(ctx, FRAMEBUFFER);
252 }
253
254 static GLenum
255 get_tex_format(struct gl_texture_image *ti)
256 {
257 switch (ti->TexFormat) {
258 case MESA_FORMAT_ARGB8888:
259 return GL_RGBA8;
260 case MESA_FORMAT_XRGB8888:
261 return GL_RGB8;
262 case MESA_FORMAT_RGB565:
263 return GL_RGB5;
264 default:
265 return GL_NONE;
266 }
267 }
268
269 static void
270 nouveau_render_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
271 struct gl_renderbuffer_attachment *att)
272 {
273 struct gl_renderbuffer *rb = att->Renderbuffer;
274 struct gl_texture_image *ti =
275 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
276
277 /* Allocate a renderbuffer object for the texture if we
278 * haven't already done so. */
279 if (!rb) {
280 rb = nouveau_renderbuffer_new(ctx, ~0);
281 assert(rb);
282
283 rb->AllocStorage = NULL;
284 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
285 }
286
287 /* Update the renderbuffer fields from the texture. */
288 set_renderbuffer_format(rb, get_tex_format(ti));
289 rb->Width = ti->Width;
290 rb->Height = ti->Height;
291 nouveau_surface_ref(&to_nouveau_teximage(ti)->surface,
292 &to_nouveau_renderbuffer(rb)->surface);
293
294 context_dirty(ctx, FRAMEBUFFER);
295 }
296
297 static void
298 nouveau_finish_render_texture(struct gl_context *ctx,
299 struct gl_renderbuffer_attachment *att)
300 {
301 texture_dirty(att->Texture);
302 }
303
304 void
305 nouveau_fbo_functions_init(struct dd_function_table *functions)
306 {
307 #if FEATURE_EXT_framebuffer_object
308 functions->NewFramebuffer = nouveau_framebuffer_new;
309 functions->NewRenderbuffer = nouveau_renderbuffer_new;
310 functions->MapRenderbuffer = nouveau_renderbuffer_map;
311 functions->UnmapRenderbuffer = nouveau_renderbuffer_unmap;
312 functions->BindFramebuffer = nouveau_bind_framebuffer;
313 functions->FramebufferRenderbuffer = nouveau_framebuffer_renderbuffer;
314 functions->RenderTexture = nouveau_render_texture;
315 functions->FinishRenderTexture = nouveau_finish_render_texture;
316 #endif
317 }