st_atom_framebuffer.c revision 8a1dfba73e5b675635108e7d8309444704b8fef4
1/**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
31  *   Brian Paul
32  */
33
34#include <limits.h>
35
36#include "st_context.h"
37#include "st_atom.h"
38#include "st_cb_bitmap.h"
39#include "st_cb_fbo.h"
40#include "st_texture.h"
41#include "pipe/p_context.h"
42#include "cso_cache/cso_context.h"
43#include "util/u_math.h"
44#include "util/u_inlines.h"
45#include "util/u_format.h"
46
47
48/**
49 * Update framebuffer size.
50 *
51 * We need to derive pipe_framebuffer size from the bound pipe_surfaces here
52 * instead of copying gl_framebuffer size because for certain target types
53 * (like PIPE_TEXTURE_1D_ARRAY) gl_framebuffer::Height has the number of layers
54 * instead of 1.
55 */
56static void
57update_framebuffer_size(struct pipe_framebuffer_state *framebuffer,
58                        struct pipe_surface *surface)
59{
60   assert(surface);
61   assert(surface->width  < UINT_MAX);
62   assert(surface->height < UINT_MAX);
63   framebuffer->width  = MIN2(framebuffer->width,  surface->width);
64   framebuffer->height = MIN2(framebuffer->height, surface->height);
65}
66
67
68/**
69 * Update framebuffer state (color, depth, stencil, etc. buffers)
70 */
71static void
72update_framebuffer_state( struct st_context *st )
73{
74   struct pipe_framebuffer_state *framebuffer = &st->state.framebuffer;
75   struct gl_framebuffer *fb = st->ctx->DrawBuffer;
76   struct st_renderbuffer *strb;
77   GLuint i;
78
79   st_flush_bitmap_cache(st);
80
81   st->state.fb_orientation = st_fb_orientation(fb);
82   framebuffer->width  = UINT_MAX;
83   framebuffer->height = UINT_MAX;
84
85   /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
86
87   /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
88    * to determine which surfaces to draw to
89    */
90   framebuffer->nr_cbufs = fb->_NumColorDrawBuffers;
91
92   for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
93      pipe_surface_reference(&framebuffer->cbufs[i], NULL);
94
95      strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
96
97      if (strb) {
98         if (strb->is_rtt || (strb->texture &&
99             _mesa_get_format_color_encoding(strb->Base.Format) == GL_SRGB)) {
100            /* rendering to a GL texture, may have to update surface */
101            st_update_renderbuffer_surface(st, strb);
102         }
103
104         if (strb->surface) {
105            pipe_surface_reference(&framebuffer->cbufs[i], strb->surface);
106            update_framebuffer_size(framebuffer, strb->surface);
107         }
108         strb->defined = GL_TRUE; /* we'll be drawing something */
109      }
110   }
111
112   for (i = framebuffer->nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
113      pipe_surface_reference(&framebuffer->cbufs[i], NULL);
114   }
115
116   /* Remove trailing GL_NONE draw buffers. */
117   while (framebuffer->nr_cbufs &&
118          !framebuffer->cbufs[framebuffer->nr_cbufs-1]) {
119      framebuffer->nr_cbufs--;
120   }
121
122   /*
123    * Depth/Stencil renderbuffer/surface.
124    */
125   strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
126   if (strb) {
127      if (strb->is_rtt) {
128         /* rendering to a GL texture, may have to update surface */
129         st_update_renderbuffer_surface(st, strb);
130      }
131      pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
132      update_framebuffer_size(framebuffer, strb->surface);
133   }
134   else {
135      strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
136      if (strb) {
137         assert(strb->surface);
138         pipe_surface_reference(&framebuffer->zsbuf, strb->surface);
139         update_framebuffer_size(framebuffer, strb->surface);
140      }
141      else
142         pipe_surface_reference(&framebuffer->zsbuf, NULL);
143   }
144
145#ifdef DEBUG
146   /* Make sure the resource binding flags were set properly */
147   for (i = 0; i < framebuffer->nr_cbufs; i++) {
148      assert(!framebuffer->cbufs[i] ||
149             framebuffer->cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
150   }
151   if (framebuffer->zsbuf) {
152      assert(framebuffer->zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
153   }
154#endif
155
156   if (framebuffer->width == UINT_MAX)
157      framebuffer->width = 0;
158   if (framebuffer->height == UINT_MAX)
159      framebuffer->height = 0;
160
161   cso_set_framebuffer(st->cso_context, framebuffer);
162}
163
164
165const struct st_tracked_state st_update_framebuffer = {
166   "st_update_framebuffer",				/* name */
167   {							/* dirty */
168      _NEW_BUFFERS,					/* mesa */
169      ST_NEW_FRAMEBUFFER,				/* st */
170   },
171   update_framebuffer_state				/* update */
172};
173
174