Merge branch 'gallium-nopointsizeminmax'
[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_COMPONENT;
74 rb->Format = MESA_FORMAT_Z24_S8;
75 rb->DataType = GL_UNSIGNED_INT;
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 context_dirty(ctx, FRAMEBUFFER);
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(GLcontext *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 GLvisual *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
193 return &nfb->base;
194 }
195
196 static void
197 nouveau_bind_framebuffer(GLcontext *ctx, GLenum target,
198 struct gl_framebuffer *dfb,
199 struct gl_framebuffer *rfb)
200 {
201 context_dirty(ctx, FRAMEBUFFER);
202 }
203
204 static void
205 nouveau_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
206 GLenum attachment, struct gl_renderbuffer *rb)
207 {
208 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
209
210 context_dirty(ctx, FRAMEBUFFER);
211 }
212
213 static GLenum
214 get_tex_format(struct gl_texture_image *ti)
215 {
216 switch (ti->TexFormat) {
217 case MESA_FORMAT_ARGB8888:
218 return GL_RGBA8;
219 case MESA_FORMAT_RGB565:
220 return GL_RGB5;
221 default:
222 assert(0);
223 }
224 }
225
226 static void
227 nouveau_render_texture(GLcontext *ctx, struct gl_framebuffer *fb,
228 struct gl_renderbuffer_attachment *att)
229 {
230 struct gl_renderbuffer *rb = att->Renderbuffer;
231 struct gl_texture_image *ti =
232 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
233 int ret;
234
235 /* Allocate a renderbuffer object for the texture if we
236 * haven't already done so. */
237 if (!rb) {
238 rb = nouveau_renderbuffer_new(ctx, 0);
239 assert(rb);
240
241 rb->AllocStorage = NULL;
242 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
243 }
244
245 /* Update the renderbuffer fields from the texture. */
246 ret = set_renderbuffer_format(rb, get_tex_format(ti));
247 assert(ret);
248
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(GLcontext *ctx,
259 struct gl_renderbuffer_attachment *att)
260 {
261 struct nouveau_renderbuffer *nrb
262 = to_nouveau_renderbuffer(att->Renderbuffer);
263
264 texture_dirty(att->Texture);
265 nouveau_surface_ref(NULL, &nrb->surface);
266 }
267
268 void
269 nouveau_fbo_functions_init(struct dd_function_table *functions)
270 {
271 functions->NewFramebuffer = nouveau_framebuffer_new;
272 functions->NewRenderbuffer = nouveau_renderbuffer_new;
273 functions->BindFramebuffer = nouveau_bind_framebuffer;
274 functions->FramebufferRenderbuffer = nouveau_framebuffer_renderbuffer;
275 functions->RenderTexture = nouveau_render_texture;
276 functions->FinishRenderTexture = nouveau_finish_render_texture;
277 }