1 /**************************************************************************
3 * Copyright 2009-2010 VMware, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 * Framebuffer utility functions.
35 #include "pipe/p_screen.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
40 #include "util/u_memory.h"
41 #include "util/u_framebuffer.h"
45 * Compare pipe_framebuffer_state objects.
46 * \return TRUE if same, FALSE if different
49 util_framebuffer_state_equal(const struct pipe_framebuffer_state
*dst
,
50 const struct pipe_framebuffer_state
*src
)
54 if (dst
->width
!= src
->width
||
55 dst
->height
!= src
->height
)
58 if (dst
->samples
!= src
->samples
||
59 dst
->layers
!= src
->layers
)
62 if (dst
->nr_cbufs
!= src
->nr_cbufs
) {
66 for (i
= 0; i
< src
->nr_cbufs
; i
++) {
67 if (dst
->cbufs
[i
] != src
->cbufs
[i
]) {
72 if (dst
->zsbuf
!= src
->zsbuf
) {
81 * Copy framebuffer state from src to dst, updating refcounts.
84 util_copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
85 const struct pipe_framebuffer_state
*src
)
89 dst
->width
= src
->width
;
90 dst
->height
= src
->height
;
92 dst
->samples
= src
->samples
;
93 dst
->layers
= src
->layers
;
95 for (i
= 0; i
< src
->nr_cbufs
; i
++)
96 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
98 /* Set remaining dest cbuf pointers to NULL */
99 for ( ; i
< Elements(dst
->cbufs
); i
++)
100 pipe_surface_reference(&dst
->cbufs
[i
], NULL
);
102 dst
->nr_cbufs
= src
->nr_cbufs
;
104 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
109 util_unreference_framebuffer_state(struct pipe_framebuffer_state
*fb
)
113 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
114 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
117 pipe_surface_reference(&fb
->zsbuf
, NULL
);
119 fb
->samples
= fb
->layers
= 0;
120 fb
->width
= fb
->height
= 0;
125 /* Where multiple sizes are allowed for framebuffer surfaces, find the
126 * minimum width and height of all bound surfaces.
129 util_framebuffer_min_size(const struct pipe_framebuffer_state
*fb
,
137 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
141 w
= MIN2(w
, fb
->cbufs
[i
]->width
);
142 h
= MIN2(h
, fb
->cbufs
[i
]->height
);
146 w
= MIN2(w
, fb
->zsbuf
->width
);
147 h
= MIN2(h
, fb
->zsbuf
->height
);
164 * Return the number of layers set in the framebuffer state.
167 util_framebuffer_get_num_layers(const struct pipe_framebuffer_state
*fb
)
169 unsigned i
, num_layers
= 0;
172 * In the case of ARB_framebuffer_no_attachment
173 * we obtain the number of layers directly from
174 * the framebuffer state.
176 if (!(fb
->nr_cbufs
|| fb
->zsbuf
))
179 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
181 unsigned num
= fb
->cbufs
[i
]->u
.tex
.last_layer
-
182 fb
->cbufs
[i
]->u
.tex
.first_layer
+ 1;
183 num_layers
= MAX2(num_layers
, num
);
187 unsigned num
= fb
->zsbuf
->u
.tex
.last_layer
-
188 fb
->zsbuf
->u
.tex
.first_layer
+ 1;
189 num_layers
= MAX2(num_layers
, num
);
196 * Return the number of MSAA samples.
199 util_framebuffer_get_num_samples(const struct pipe_framebuffer_state
*fb
)
204 * In the case of ARB_framebuffer_no_attachment
205 * we obtain the number of samples directly from
206 * the framebuffer state.
208 * NOTE: fb->samples may wind up as zero due to memset()'s on internal
209 * driver structures on their initialization and so we take the
210 * MAX here to ensure we have a valid number of samples. However,
211 * if samples is legitimately not getting set somewhere
212 * multi-sampling will evidently break.
214 if (!(fb
->nr_cbufs
|| fb
->zsbuf
))
215 return MAX2(fb
->samples
, 1);
217 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
219 return MAX2(1, fb
->cbufs
[i
]->texture
->nr_samples
);
223 return MAX2(1, fb
->zsbuf
->texture
->nr_samples
);