st_atom_framebuffer.c revision 2010cd7810da50484fea03259e777ffac0593007
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  * Authors:
30  *   Keith Whitwell <keith@tungstengraphics.com>
31  *   Brian Paul
32  */
33
34#include "st_context.h"
35#include "st_atom.h"
36#include "st_cb_fbo.h"
37#include "st_texture.h"
38#include "pipe/p_context.h"
39#include "pipe/p_inlines.h"
40#include "cso_cache/cso_context.h"
41
42
43
44/**
45 * When doing GL render to texture, we have to be sure that finalize_texture()
46 * didn't yank out the pipe_texture that we earlier created a surface for.
47 * Check for that here and create a new surface if needed.
48 */
49static void
50update_renderbuffer_surface(struct st_context *st,
51                            struct st_renderbuffer *strb)
52{
53   struct pipe_screen *screen = st->pipe->screen;
54   struct pipe_texture *texture = strb->rtt->pt;
55   int rtt_width = strb->Base.Width;
56   int rtt_height = strb->Base.Height;
57
58   if (!strb->surface ||
59       strb->surface->texture != texture ||
60       strb->surface->width != rtt_width ||
61       strb->surface->height != rtt_height) {
62      GLuint level;
63      /* find matching mipmap level size */
64      for (level = 0; level <= texture->last_level; level++) {
65         if (texture->width[level] == rtt_width &&
66             texture->height[level] == rtt_height) {
67
68            pipe_surface_reference(&strb->surface, NULL);
69
70            strb->surface = screen->get_tex_surface(screen,
71                                              texture,
72                                              strb->rtt_face,
73                                              level,
74                                              strb->rtt_slice,
75                                              PIPE_BUFFER_USAGE_GPU_READ |
76                                              PIPE_BUFFER_USAGE_GPU_WRITE);
77#if 0
78            printf("-- alloc new surface %d x %d into tex %p\n",
79                   strb->surface->width, strb->surface->height,
80                   texture);
81#endif
82            break;
83         }
84      }
85   }
86}
87
88
89/**
90 * Update framebuffer state (color, depth, stencil, etc. buffers)
91 */
92static void
93update_framebuffer_state( struct st_context *st )
94{
95   struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
96   struct gl_framebuffer *fb = st->ctx->DrawBuffer;
97   struct st_renderbuffer *strb;
98   GLuint i;
99
100   memset(framebuffer, 0, sizeof(*framebuffer));
101
102   framebuffer->width = fb->Width;
103   framebuffer->height = fb->Height;
104
105   /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
106
107   /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
108    * to determine which surfaces to draw to
109    */
110   framebuffer->num_cbufs = 0;
111   for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
112      strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
113
114      /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
115      if (strb->rtt) {
116         /* rendering to a GL texture, may have to update surface */
117         update_renderbuffer_surface(st, strb);
118      }
119
120      assert(strb->surface);
121      framebuffer->cbufs[framebuffer->num_cbufs] = strb->surface;
122      framebuffer->num_cbufs++;
123   }
124
125   strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
126   if (strb) {
127      strb = st_renderbuffer(strb->Base.Wrapped);
128      if (strb->rtt) {
129         /* rendering to a GL texture, may have to update surface */
130         update_renderbuffer_surface(st, strb);
131      }
132
133      assert(strb->surface);
134      framebuffer->zsbuf = strb->surface;
135   }
136   else {
137      strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
138      if (strb) {
139         strb = st_renderbuffer(strb->Base.Wrapped);
140         assert(strb->surface);
141         framebuffer->zsbuf = strb->surface;
142      }
143   }
144
145   cso_set_framebuffer(st->cso_context, framebuffer);
146
147#if 0
148   if (fb->_ColorDrawBufferMask[0] & BUFFER_BIT_FRONT_LEFT) {
149      if (st->frontbuffer_status == FRONT_STATUS_COPY_OF_BACK) {
150         /* XXX copy back buf to front? */
151      }
152      /* we're assuming we'll really draw to the front buffer */
153      st->frontbuffer_status = FRONT_STATUS_DIRTY;
154   }
155#else
156#if !defined(PIPE_OS_WINDOWS)
157#warning "fix me"
158#endif
159   st->frontbuffer_status = FRONT_STATUS_DIRTY;
160#endif
161}
162
163
164const struct st_tracked_state st_update_framebuffer = {
165   "st_update_framebuffer",				/* name */
166   {							/* dirty */
167      _NEW_BUFFERS,					/* mesa */
168      ST_NEW_FRAMEBUFFER,				/* st */
169   },
170   update_framebuffer_state				/* update */
171};
172
173