1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
36 #include "st_context.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"
49 * Update framebuffer size.
51 * We need to derive pipe_framebuffer size from the bound pipe_surfaces here
52 * instead of copying gl_framebuffer size because for certain target types
53 * (like PIPE_TEXTURE_1D_ARRAY) gl_framebuffer::Height has the number of layers
57 update_framebuffer_size(struct pipe_framebuffer_state
*framebuffer
,
58 struct pipe_surface
*surface
)
61 assert(surface
->width
< UINT_MAX
);
62 assert(surface
->height
< UINT_MAX
);
63 framebuffer
->width
= MIN2(framebuffer
->width
, surface
->width
);
64 framebuffer
->height
= MIN2(framebuffer
->height
, surface
->height
);
69 * Update framebuffer state (color, depth, stencil, etc. buffers)
72 update_framebuffer_state( struct st_context
*st
)
74 struct pipe_framebuffer_state
*framebuffer
= &st
->state
.framebuffer
;
75 struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
76 struct st_renderbuffer
*strb
;
79 st_flush_bitmap_cache(st
);
81 st
->state
.fb_orientation
= st_fb_orientation(fb
);
82 framebuffer
->width
= UINT_MAX
;
83 framebuffer
->height
= UINT_MAX
;
85 /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
87 /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
88 * to determine which surfaces to draw to
90 framebuffer
->nr_cbufs
= fb
->_NumColorDrawBuffers
;
92 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
93 pipe_surface_reference(&framebuffer
->cbufs
[i
], NULL
);
95 strb
= st_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
98 if (strb
->is_rtt
|| (strb
->texture
&&
99 _mesa_get_format_color_encoding(strb
->Base
.Format
) == GL_SRGB
)) {
100 /* rendering to a GL texture, may have to update surface */
101 st_update_renderbuffer_surface(st
, strb
);
105 pipe_surface_reference(&framebuffer
->cbufs
[i
], strb
->surface
);
106 update_framebuffer_size(framebuffer
, strb
->surface
);
108 strb
->defined
= GL_TRUE
; /* we'll be drawing something */
112 for (i
= framebuffer
->nr_cbufs
; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
113 pipe_surface_reference(&framebuffer
->cbufs
[i
], NULL
);
116 /* Remove trailing GL_NONE draw buffers. */
117 while (framebuffer
->nr_cbufs
&&
118 !framebuffer
->cbufs
[framebuffer
->nr_cbufs
-1]) {
119 framebuffer
->nr_cbufs
--;
123 * Depth/Stencil renderbuffer/surface.
125 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
128 /* rendering to a GL texture, may have to update surface */
129 st_update_renderbuffer_surface(st
, strb
);
131 pipe_surface_reference(&framebuffer
->zsbuf
, strb
->surface
);
132 update_framebuffer_size(framebuffer
, strb
->surface
);
135 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
138 /* rendering to a GL texture, may have to update surface */
139 st_update_renderbuffer_surface(st
, strb
);
141 pipe_surface_reference(&framebuffer
->zsbuf
, strb
->surface
);
142 update_framebuffer_size(framebuffer
, strb
->surface
);
145 pipe_surface_reference(&framebuffer
->zsbuf
, NULL
);
149 /* Make sure the resource binding flags were set properly */
150 for (i
= 0; i
< framebuffer
->nr_cbufs
; i
++) {
151 assert(!framebuffer
->cbufs
[i
] ||
152 framebuffer
->cbufs
[i
]->texture
->bind
& PIPE_BIND_RENDER_TARGET
);
154 if (framebuffer
->zsbuf
) {
155 assert(framebuffer
->zsbuf
->texture
->bind
& PIPE_BIND_DEPTH_STENCIL
);
159 if (framebuffer
->width
== UINT_MAX
)
160 framebuffer
->width
= 0;
161 if (framebuffer
->height
== UINT_MAX
)
162 framebuffer
->height
= 0;
164 cso_set_framebuffer(st
->cso_context
, framebuffer
);
168 const struct st_tracked_state st_update_framebuffer
= {
169 "st_update_framebuffer", /* name */
171 _NEW_BUFFERS
, /* mesa */
172 ST_NEW_FRAMEBUFFER
, /* st */
174 update_framebuffer_state
/* update */