• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "util/u_framebuffer.h"
47 #include "main/framebuffer.h"
48 
49 
50 /**
51  * Update framebuffer size.
52  *
53  * We need to derive pipe_framebuffer size from the bound pipe_surfaces here
54  * instead of copying gl_framebuffer size because for certain target types
55  * (like PIPE_TEXTURE_1D_ARRAY) gl_framebuffer::Height has the number of layers
56  * instead of 1.
57  */
58 static void
update_framebuffer_size(struct pipe_framebuffer_state * framebuffer,struct pipe_surface * surface)59 update_framebuffer_size(struct pipe_framebuffer_state *framebuffer,
60                         struct pipe_surface *surface)
61 {
62    assert(surface);
63    assert(surface->width  < USHRT_MAX);
64    assert(surface->height < USHRT_MAX);
65    framebuffer->width  = MIN2(framebuffer->width,  surface->width);
66    framebuffer->height = MIN2(framebuffer->height, surface->height);
67 }
68 
69 /**
70  * Round up the requested multisample count to the next supported sample size.
71  */
72 static unsigned
framebuffer_quantize_num_samples(struct st_context * st,unsigned num_samples)73 framebuffer_quantize_num_samples(struct st_context *st, unsigned num_samples)
74 {
75    struct pipe_screen *screen = st->pipe->screen;
76    int quantized_samples = 0;
77    unsigned msaa_mode;
78 
79    if (!num_samples)
80       return 0;
81 
82    /* Assumes the highest supported MSAA is a power of 2 */
83    msaa_mode = util_next_power_of_two(st->ctx->Const.MaxFramebufferSamples);
84    assert(!(num_samples > msaa_mode)); /* be safe from infinite loops */
85 
86    /**
87     * Check if the MSAA mode that is higher than the requested
88     * num_samples is supported, and if so returning it.
89     */
90    for (; msaa_mode >= num_samples; msaa_mode = msaa_mode / 2) {
91       /**
92        * For ARB_framebuffer_no_attachment, A format of
93        * PIPE_FORMAT_NONE implies what number of samples is
94        * supported for a framebuffer with no attachment. Thus the
95        * drivers callback must be adjusted for this.
96        */
97       if (screen->is_format_supported(screen, PIPE_FORMAT_NONE,
98                                       PIPE_TEXTURE_2D, msaa_mode,
99                                       PIPE_BIND_RENDER_TARGET))
100          quantized_samples = msaa_mode;
101    }
102    return quantized_samples;
103 }
104 
105 /**
106  * Update framebuffer state (color, depth, stencil, etc. buffers)
107  */
108 void
st_update_framebuffer_state(struct st_context * st)109 st_update_framebuffer_state( struct st_context *st )
110 {
111    struct pipe_framebuffer_state framebuffer;
112    struct gl_framebuffer *fb = st->ctx->DrawBuffer;
113    struct st_renderbuffer *strb;
114    GLuint i;
115 
116    st_flush_bitmap_cache(st);
117    st_invalidate_readpix_cache(st);
118 
119    st->state.fb_orientation = st_fb_orientation(fb);
120 
121    /**
122     * Quantize the derived default number of samples:
123     *
124     * A query to the driver of supported MSAA values the
125     * hardware supports is done as to legalize the number
126     * of application requested samples, NumSamples.
127     * See commit eb9cf3c for more information.
128     */
129    fb->DefaultGeometry._NumSamples =
130       framebuffer_quantize_num_samples(st, fb->DefaultGeometry.NumSamples);
131 
132    framebuffer.width  = _mesa_geometric_width(fb);
133    framebuffer.height = _mesa_geometric_height(fb);
134    framebuffer.samples = _mesa_geometric_samples(fb);
135    framebuffer.layers = _mesa_geometric_layers(fb);
136 
137    /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
138     * to determine which surfaces to draw to
139     */
140    framebuffer.nr_cbufs = fb->_NumColorDrawBuffers;
141 
142    for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
143       framebuffer.cbufs[i] = NULL;
144       strb = st_renderbuffer(fb->_ColorDrawBuffers[i]);
145 
146       if (strb) {
147          if (strb->is_rtt || (strb->texture &&
148              _mesa_get_format_color_encoding(strb->Base.Format) == GL_SRGB)) {
149             /* rendering to a GL texture, may have to update surface */
150             st_update_renderbuffer_surface(st, strb);
151          }
152 
153          if (strb->surface) {
154             framebuffer.cbufs[i] = strb->surface;
155             update_framebuffer_size(&framebuffer, strb->surface);
156          }
157          strb->defined = GL_TRUE; /* we'll be drawing something */
158       }
159    }
160 
161    for (i = framebuffer.nr_cbufs; i < PIPE_MAX_COLOR_BUFS; i++) {
162       framebuffer.cbufs[i] = NULL;
163    }
164 
165    /* Remove trailing GL_NONE draw buffers. */
166    while (framebuffer.nr_cbufs &&
167           !framebuffer.cbufs[framebuffer.nr_cbufs-1]) {
168       framebuffer.nr_cbufs--;
169    }
170 
171    /*
172     * Depth/Stencil renderbuffer/surface.
173     */
174    strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
175    if (strb) {
176       if (strb->is_rtt) {
177          /* rendering to a GL texture, may have to update surface */
178          st_update_renderbuffer_surface(st, strb);
179       }
180       framebuffer.zsbuf = strb->surface;
181       update_framebuffer_size(&framebuffer, strb->surface);
182    }
183    else {
184       strb = st_renderbuffer(fb->Attachment[BUFFER_STENCIL].Renderbuffer);
185       if (strb) {
186          if (strb->is_rtt) {
187             /* rendering to a GL texture, may have to update surface */
188             st_update_renderbuffer_surface(st, strb);
189          }
190          framebuffer.zsbuf = strb->surface;
191          update_framebuffer_size(&framebuffer, strb->surface);
192       }
193       else
194          framebuffer.zsbuf = NULL;
195    }
196 
197 #ifdef DEBUG
198    /* Make sure the resource binding flags were set properly */
199    for (i = 0; i < framebuffer.nr_cbufs; i++) {
200       assert(!framebuffer.cbufs[i] ||
201              framebuffer.cbufs[i]->texture->bind & PIPE_BIND_RENDER_TARGET);
202    }
203    if (framebuffer.zsbuf) {
204       assert(framebuffer.zsbuf->texture->bind & PIPE_BIND_DEPTH_STENCIL);
205    }
206 #endif
207 
208    if (framebuffer.width == USHRT_MAX)
209       framebuffer.width = 0;
210    if (framebuffer.height == USHRT_MAX)
211       framebuffer.height = 0;
212 
213    cso_set_framebuffer(st->cso_context, &framebuffer);
214 
215    st->state.fb_width = framebuffer.width;
216    st->state.fb_height = framebuffer.height;
217    st->state.fb_num_samples = util_framebuffer_get_num_samples(&framebuffer);
218    st->state.fb_num_layers = util_framebuffer_get_num_layers(&framebuffer);
219 }
220