st_atom_framebuffer.c revision 1a82d9648b3db780e58e4966924157542d148c58
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      int 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, j;
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 (j = 0; j < MAX_DRAW_BUFFERS; j++) {
112      for (i = 0; i < fb->_NumColorDrawBuffers[j]; i++) {
113         strb = st_renderbuffer(fb->_ColorDrawBuffers[j][i]);
114
115         /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
116         if (strb->rtt) {
117            /* rendering to a GL texture, may have to update surface */
118            update_renderbuffer_surface(st, strb);
119         }
120
121         assert(strb->surface);
122         framebuffer->cbufs[framebuffer->num_cbufs] = strb->surface;
123         framebuffer->num_cbufs++;
124      }
125   }
126
127   strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
128   if (strb) {
129      strb = st_renderbuffer(strb->Base.Wrapped);
130      assert(strb->surface);
131      framebuffer->zsbuf = strb->surface;
132   }
133   else {
134      strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
135      if (strb) {
136         strb = st_renderbuffer(strb->Base.Wrapped);
137         assert(strb->surface);
138         framebuffer->zsbuf = strb->surface;
139      }
140   }
141
142   cso_set_framebuffer(st->cso_context, framebuffer);
143
144   if (fb->_ColorDrawBufferMask[0] & BUFFER_BIT_FRONT_LEFT) {
145      if (st->frontbuffer_status == FRONT_STATUS_COPY_OF_BACK) {
146         /* XXX copy back buf to front? */
147      }
148      /* we're assuming we'll really draw to the front buffer */
149      st->frontbuffer_status = FRONT_STATUS_DIRTY;
150   }
151}
152
153
154const struct st_tracked_state st_update_framebuffer = {
155   "st_update_framebuffer",				/* name */
156   {							/* dirty */
157      _NEW_BUFFERS,					/* mesa */
158      ST_NEW_FRAMEBUFFER,				/* st */
159   },
160   update_framebuffer_state				/* update */
161};
162
163