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