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