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