Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / src / mesa / state_tracker / st_framebuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 #include "main/imports.h"
30 #include "main/buffers.h"
31 #include "main/context.h"
32 #include "main/framebuffer.h"
33 #include "main/matrix.h"
34 #include "main/renderbuffer.h"
35 #include "main/scissor.h"
36 #include "main/viewport.h"
37 #include "st_context.h"
38 #include "st_cb_fbo.h"
39 #include "st_public.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_context.h"
42
43
44 struct st_framebuffer *
45 st_create_framebuffer( const __GLcontextModes *visual,
46 enum pipe_format colorFormat,
47 enum pipe_format depthFormat,
48 enum pipe_format stencilFormat,
49 uint width, uint height,
50 void *private)
51 {
52 struct st_framebuffer *stfb = ST_CALLOC_STRUCT(st_framebuffer);
53 if (stfb) {
54 int samples = st_get_msaa();
55
56 if (visual->sampleBuffers)
57 samples = visual->samples;
58
59 _mesa_initialize_framebuffer(&stfb->Base, visual);
60
61 {
62 /* fake frontbuffer */
63 /* XXX allocation should only happen in the unusual case
64 it's actually needed */
65 struct gl_renderbuffer *rb
66 = st_new_renderbuffer_fb(colorFormat, samples);
67 _mesa_add_renderbuffer(&stfb->Base, BUFFER_FRONT_LEFT, rb);
68 }
69
70 if (visual->doubleBufferMode) {
71 struct gl_renderbuffer *rb
72 = st_new_renderbuffer_fb(colorFormat, samples);
73 _mesa_add_renderbuffer(&stfb->Base, BUFFER_BACK_LEFT, rb);
74 }
75
76 if (depthFormat == stencilFormat && depthFormat != PIPE_FORMAT_NONE) {
77 /* combined depth/stencil buffer */
78 struct gl_renderbuffer *depthStencilRb
79 = st_new_renderbuffer_fb(depthFormat, samples);
80 /* note: bind RB to two attachment points */
81 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthStencilRb);
82 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, depthStencilRb);
83 }
84 else {
85 /* separate depth and/or stencil */
86
87 if (visual->depthBits == 32) {
88 /* 32-bit depth buffer */
89 struct gl_renderbuffer *depthRb
90 = st_new_renderbuffer_fb(depthFormat, samples);
91 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
92 }
93 else if (visual->depthBits == 24) {
94 /* 24-bit depth buffer, ignore stencil bits */
95 struct gl_renderbuffer *depthRb
96 = st_new_renderbuffer_fb(depthFormat, samples);
97 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
98 }
99 else if (visual->depthBits > 0) {
100 /* 16-bit depth buffer */
101 struct gl_renderbuffer *depthRb
102 = st_new_renderbuffer_fb(depthFormat, samples);
103 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
104 }
105
106 if (visual->stencilBits > 0) {
107 /* 8-bit stencil */
108 struct gl_renderbuffer *stencilRb
109 = st_new_renderbuffer_fb(stencilFormat, samples);
110 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, stencilRb);
111 }
112 }
113
114 if (visual->accumRedBits > 0) {
115 /* 16-bit/channel accum */
116 struct gl_renderbuffer *accumRb
117 = st_new_renderbuffer_fb(DEFAULT_ACCUM_PIPE_FORMAT, 0); /* XXX accum isn't multisampled right? */
118 _mesa_add_renderbuffer(&stfb->Base, BUFFER_ACCUM, accumRb);
119 }
120
121 stfb->Base.Initialized = GL_TRUE;
122 stfb->InitWidth = width;
123 stfb->InitHeight = height;
124 stfb->Private = private;
125 }
126 return stfb;
127 }
128
129
130 void st_resize_framebuffer( struct st_framebuffer *stfb,
131 uint width, uint height )
132 {
133 if (stfb->Base.Width != width || stfb->Base.Height != height) {
134 GET_CURRENT_CONTEXT(ctx);
135 if (ctx) {
136 if (stfb->InitWidth == 0 && stfb->InitHeight == 0) {
137 /* didn't have a valid size until now */
138 stfb->InitWidth = width;
139 stfb->InitHeight = height;
140 if (ctx->Viewport.Width <= 1) {
141 /* set context's initial viewport/scissor size */
142 _mesa_set_viewport(ctx, 0, 0, width, height);
143 _mesa_set_scissor(ctx, 0, 0, width, height);
144 }
145 }
146
147 _mesa_resize_framebuffer(ctx, &stfb->Base, width, height);
148
149 assert(stfb->Base.Width == width);
150 assert(stfb->Base.Height == height);
151 }
152 }
153 }
154
155
156 void st_unreference_framebuffer( struct st_framebuffer *stfb )
157 {
158 _mesa_reference_framebuffer((struct gl_framebuffer **) &stfb, NULL);
159 }
160
161
162
163 /**
164 * Set/replace a framebuffer surface.
165 * The user of the state tracker can use this instead of
166 * st_resize_framebuffer() to provide new surfaces when a window is resized.
167 */
168 void
169 st_set_framebuffer_surface(struct st_framebuffer *stfb,
170 uint surfIndex, struct pipe_surface *surf)
171 {
172 GET_CURRENT_CONTEXT(ctx);
173 static const GLuint invalid_size = 9999999;
174 struct st_renderbuffer *strb;
175 GLuint width, height, i;
176
177 assert(surfIndex < BUFFER_COUNT);
178
179 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
180
181 /* fail */
182 if (!strb) return;
183
184 /* replace the renderbuffer's surface/texture pointers */
185 pipe_surface_reference( &strb->surface, surf );
186 pipe_texture_reference( &strb->texture, surf->texture );
187
188 if (ctx) {
189 /* If ctx isn't set, we've likely not made current yet.
190 * But when we do, we need to start setting this dirty bit
191 * to ensure the renderbuffer attachements are up-to-date
192 * via update_framebuffer.
193 */
194 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
195 }
196
197 /* update renderbuffer's width/height */
198 strb->Base.Width = surf->width;
199 strb->Base.Height = surf->height;
200
201 /* Try to update the framebuffer's width/height from the renderbuffer
202 * sizes. Before we start drawing, all the rbs _should_ be the same size.
203 */
204 width = height = invalid_size;
205 for (i = 0; i < BUFFER_COUNT; i++) {
206 if (stfb->Base.Attachment[i].Renderbuffer) {
207 if (width == invalid_size) {
208 width = stfb->Base.Attachment[i].Renderbuffer->Width;
209 height = stfb->Base.Attachment[i].Renderbuffer->Height;
210 }
211 else if (width != stfb->Base.Attachment[i].Renderbuffer->Width ||
212 height != stfb->Base.Attachment[i].Renderbuffer->Height) {
213 /* inconsistant renderbuffer sizes, bail out */
214 return;
215 }
216 }
217 }
218
219 if (width != invalid_size) {
220 /* OK, the renderbuffers are of a consistant size, so update the
221 * parent framebuffer's size.
222 */
223 stfb->Base.Width = width;
224 stfb->Base.Height = height;
225 }
226 }
227
228
229
230 /**
231 * Return the pipe_surface for the given renderbuffer.
232 */
233 int
234 st_get_framebuffer_surface(struct st_framebuffer *stfb, uint surfIndex, struct pipe_surface **surface)
235 {
236 struct st_renderbuffer *strb;
237
238 assert(surfIndex <= ST_SURFACE_DEPTH);
239
240 /* sanity checks, ST tokens should match Mesa tokens */
241 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
242 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
243
244 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
245 if (strb) {
246 *surface = strb->surface;
247 return GL_TRUE;
248 }
249
250 *surface = NULL;
251 return GL_FALSE;
252 }
253
254 int
255 st_get_framebuffer_texture(struct st_framebuffer *stfb, uint surfIndex, struct pipe_texture **texture)
256 {
257 struct st_renderbuffer *strb;
258
259 assert(surfIndex <= ST_SURFACE_DEPTH);
260
261 /* sanity checks, ST tokens should match Mesa tokens */
262 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
263 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
264
265 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
266 if (strb) {
267 *texture = strb->texture;
268 return GL_TRUE;
269 }
270
271 *texture = NULL;
272 return GL_FALSE;
273 }
274
275 /**
276 * This function is to be called prior to SwapBuffers on the given
277 * framebuffer. It checks if the current context is bound to the framebuffer
278 * and flushes rendering if needed.
279 */
280 void
281 st_notify_swapbuffers(struct st_framebuffer *stfb)
282 {
283 GET_CURRENT_CONTEXT(ctx);
284
285 if (ctx && ctx->DrawBuffer == &stfb->Base) {
286 st_flush( ctx->st,
287 PIPE_FLUSH_RENDER_CACHE |
288 PIPE_FLUSH_SWAPBUFFERS |
289 PIPE_FLUSH_FRAME,
290 NULL );
291 ctx->st->frontbuffer_status = FRONT_STATUS_COPY_OF_BACK;
292 }
293 }
294
295
296 /**
297 * Quick hack - allows the winsys to inform the driver that surface
298 * states are now undefined after a glXSwapBuffers or similar.
299 */
300 void
301 st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
302 {
303 GET_CURRENT_CONTEXT(ctx);
304
305 if (ctx && ctx->DrawBuffer == &stfb->Base) {
306 struct st_renderbuffer *strb;
307
308 /* Mark back color buffers as undefined */
309 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_BACK_LEFT].
310 Renderbuffer);
311 if (strb && strb->surface)
312 strb->surface->status = PIPE_SURFACE_STATUS_UNDEFINED;
313
314 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_BACK_RIGHT].
315 Renderbuffer);
316 if (strb && strb->surface)
317 strb->surface->status = PIPE_SURFACE_STATUS_UNDEFINED;
318 }
319 }
320
321
322 void *st_framebuffer_private( struct st_framebuffer *stfb )
323 {
324 return stfb->Private;
325 }
326
327 void st_get_framebuffer_dimensions( struct st_framebuffer *stfb,
328 uint *width,
329 uint *height)
330 {
331 *width = stfb->Base.Width;
332 *height = stfb->Base.Height;
333 }