don't apply pixelzoom to bitmaps
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Framebuffer/renderbuffer functions.
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/fbobject.h"
39 #include "main/framebuffer.h"
40 #include "main/renderbuffer.h"
41
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "st_context.h"
45 #include "st_cb_fbo.h"
46 #include "st_cb_texture.h"
47 #include "st_format.h"
48 #include "st_public.h"
49
50
51
52 /**
53 * gl_renderbuffer::AllocStorage()
54 */
55 static GLboolean
56 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
57 GLenum internalFormat,
58 GLuint width, GLuint height)
59 {
60 struct pipe_context *pipe = ctx->st->pipe;
61 struct st_renderbuffer *strb = st_renderbuffer(rb);
62 const GLuint pipeFormat
63 = st_choose_pipe_format(pipe, internalFormat, GL_NONE, GL_NONE);
64 const struct pipe_format_info *info = st_get_format_info(pipeFormat);
65 GLuint cpp;
66 GLbitfield flags = PIPE_SURFACE_FLAG_RENDER; /* want to render to surface */
67
68 assert(info);
69 if (!info)
70 return GL_FALSE;
71
72 switch (pipeFormat) {
73 case PIPE_FORMAT_S8_Z24:
74 strb->Base.DataType = GL_UNSIGNED_INT;
75 break;
76 default:
77 strb->Base.DataType = GL_UNSIGNED_BYTE; /* XXX fix */
78 }
79
80 strb->Base._ActualFormat = info->base_format;
81 strb->Base.RedBits = info->red_bits;
82 strb->Base.GreenBits = info->green_bits;
83 strb->Base.BlueBits = info->blue_bits;
84 strb->Base.AlphaBits = info->alpha_bits;
85 strb->Base.DepthBits = info->depth_bits;
86 strb->Base.StencilBits = info->stencil_bits;
87
88 cpp = info->size;
89
90 if (!strb->surface) {
91 strb->surface = pipe->surface_alloc(pipe, pipeFormat);
92 assert(strb->surface);
93 if (!strb->surface)
94 return GL_FALSE;
95 }
96
97 /* free old region */
98 if (strb->surface->region) {
99 pipe->region_release(pipe, &strb->surface->region);
100 }
101
102 /* Softpipe operates on quads, so pad dimensions to multiples of 2 */
103 width += width & 1;
104 height += height & 1;
105
106 strb->surface->region = pipe->region_alloc(pipe, cpp, width, height, flags);
107 if (!strb->surface->region)
108 return GL_FALSE; /* out of memory, try s/w buffer? */
109
110 ASSERT(strb->surface->region->buffer);
111 ASSERT(strb->surface->format);
112
113 strb->Base.Width = strb->surface->width = width;
114 strb->Base.Height = strb->surface->height = height;
115
116 return GL_TRUE;
117 }
118
119
120 /**
121 * gl_renderbuffer::Delete()
122 */
123 static void
124 st_renderbuffer_delete(struct gl_renderbuffer *rb)
125 {
126 struct st_renderbuffer *strb = st_renderbuffer(rb);
127 GET_CURRENT_CONTEXT(ctx);
128 if (ctx) {
129 struct pipe_context *pipe = ctx->st->pipe;
130 ASSERT(strb);
131 if (strb && strb->surface) {
132 if (strb->surface->region) {
133 pipe->region_release(pipe, &strb->surface->region);
134 }
135 free(strb->surface);
136 }
137 }
138 else {
139 _mesa_warning(NULL, "st_renderbuffer_delete() called, but no current context");
140 }
141 free(strb);
142 }
143
144
145 /**
146 * gl_renderbuffer::GetPointer()
147 */
148 static void *
149 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
150 GLint x, GLint y)
151 {
152 /* By returning NULL we force all software rendering to go through
153 * the span routines.
154 */
155 #if 0
156 assert(0); /* Should never get called with softpipe */
157 #endif
158 return NULL;
159 }
160
161
162 /**
163 * Called via ctx->Driver.NewFramebuffer()
164 */
165 static struct gl_framebuffer *
166 st_new_framebuffer(GLcontext *ctx, GLuint name)
167 {
168 /* XXX not sure we need to subclass gl_framebuffer for pipe */
169 return _mesa_new_framebuffer(ctx, name);
170 }
171
172
173 /**
174 * Called via ctx->Driver.NewRenderbuffer()
175 */
176 static struct gl_renderbuffer *
177 st_new_renderbuffer(GLcontext *ctx, GLuint name)
178 {
179 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
180 if (strb) {
181 _mesa_init_renderbuffer(&strb->Base, name);
182 strb->Base.Delete = st_renderbuffer_delete;
183 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
184 strb->Base.GetPointer = null_get_pointer;
185 return &strb->Base;
186 }
187 return NULL;
188 }
189
190
191 #if 000
192 struct gl_renderbuffer *
193 st_new_renderbuffer_fb(struct pipe_region *region, GLuint width, GLuint height)
194 {
195 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
196 if (!strb)
197 return;
198
199 _mesa_init_renderbuffer(&strb->Base, name);
200 strb->Base.Delete = st_renderbuffer_delete;
201 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
202 strb->Base.GetPointer = null_get_pointer;
203 strb->Base.Width = width;
204 strb->Base.Heigth = height;
205
206 strb->region = region;
207
208 return &strb->Base;
209 }
210
211 #else
212
213 struct gl_renderbuffer *
214 st_new_renderbuffer_fb(GLenum intFormat)
215 {
216 struct st_renderbuffer *strb;
217
218 strb = CALLOC_STRUCT(st_renderbuffer);
219 if (!strb) {
220 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
221 return NULL;
222 }
223
224 _mesa_init_renderbuffer(&strb->Base, 0);
225 strb->Base.ClassID = 0x42; /* XXX temp */
226 strb->Base.InternalFormat = intFormat;
227
228 switch (intFormat) {
229 case GL_RGB5:
230 case GL_RGBA8:
231 strb->Base._BaseFormat = GL_RGBA;
232 break;
233 case GL_DEPTH_COMPONENT16:
234 case GL_DEPTH_COMPONENT32:
235 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
236 break;
237 case GL_DEPTH24_STENCIL8_EXT:
238 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
239 break;
240 case GL_STENCIL_INDEX8_EXT:
241 strb->Base._BaseFormat = GL_STENCIL_INDEX;
242 break;
243 default:
244 _mesa_problem(NULL,
245 "Unexpected intFormat in st_new_renderbuffer");
246 return NULL;
247 }
248
249 /* st-specific methods */
250 strb->Base.Delete = st_renderbuffer_delete;
251 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
252 strb->Base.GetPointer = null_get_pointer;
253
254 /* surface is allocate in alloc_renderbuffer_storage() */
255 strb->surface = NULL;
256
257 return &strb->Base;
258 }
259 #endif
260
261
262
263 /**
264 * Called via ctx->Driver.BindFramebufferEXT().
265 */
266 static void
267 st_bind_framebuffer(GLcontext *ctx, GLenum target,
268 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
269 {
270
271 }
272
273 /**
274 * Called by ctx->Driver.FramebufferRenderbuffer
275 */
276 static void
277 st_framebuffer_renderbuffer(GLcontext *ctx,
278 struct gl_framebuffer *fb,
279 GLenum attachment,
280 struct gl_renderbuffer *rb)
281 {
282 /* XXX no need for derivation? */
283 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
284 }
285
286
287 /**
288 * Called by ctx->Driver.RenderTexture
289 */
290 static void
291 st_render_texture(GLcontext *ctx,
292 struct gl_framebuffer *fb,
293 struct gl_renderbuffer_attachment *att)
294 {
295 struct st_context *st = ctx->st;
296 struct st_renderbuffer *strb;
297 struct gl_renderbuffer *rb;
298 struct pipe_context *pipe = st->pipe;
299 struct pipe_mipmap_tree *mt;
300
301 assert(!att->Renderbuffer);
302
303 /* create new renderbuffer which wraps the texture image */
304 rb = st_new_renderbuffer(ctx, 0);
305 if (!rb) {
306 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
307 return;
308 }
309
310 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
311 assert(rb->RefCount == 1);
312 rb->AllocStorage = NULL; /* should not get called */
313 strb = st_renderbuffer(rb);
314
315 /* get the mipmap tree for the texture */
316 mt = st_get_texobj_mipmap_tree(att->Texture);
317 assert(mt);
318 assert(mt->level[0].width);
319
320 rb->Width = mt->level[0].width;
321 rb->Height = mt->level[0].height;
322
323 /* the renderbuffer's surface is inside the mipmap_tree: */
324 strb->surface = pipe->get_tex_surface(pipe, mt,
325 att->CubeMapFace,
326 att->TextureLevel,
327 att->Zoffset);
328 assert(strb->surface);
329
330 /*
331 printf("RENDER TO TEXTURE obj=%p mt=%p surf=%p %d x %d\n",
332 att->Texture, mt, strb->surface, rb->Width, rb->Height);
333 */
334
335 /* Invalidate buffer state so that the pipe's framebuffer state
336 * gets updated.
337 * That's where the new renderbuffer (which we just created) gets
338 * passed to the pipe as a (color/depth) render target.
339 */
340 st_invalidate_state(ctx, _NEW_BUFFERS);
341 }
342
343
344 /**
345 * Called via ctx->Driver.FinishRenderTexture.
346 */
347 static void
348 st_finish_render_texture(GLcontext *ctx,
349 struct gl_renderbuffer_attachment *att)
350 {
351 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
352
353 assert(strb);
354
355 ctx->st->pipe->flush(ctx->st->pipe, 0x0);
356
357 /*
358 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
359 */
360
361 pipe_surface_reference(&strb->surface, NULL);
362
363 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
364
365 /* restore previous framebuffer state */
366 st_invalidate_state(ctx, _NEW_BUFFERS);
367 }
368
369
370
371 void st_init_fbo_functions(struct dd_function_table *functions)
372 {
373 functions->NewFramebuffer = st_new_framebuffer;
374 functions->NewRenderbuffer = st_new_renderbuffer;
375 functions->BindFramebuffer = st_bind_framebuffer;
376 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
377 functions->RenderTexture = st_render_texture;
378 functions->FinishRenderTexture = st_finish_render_texture;
379 /* no longer needed by core Mesa, drivers handle resizes...
380 functions->ResizeBuffers = st_resize_buffers;
381 */
382 }