xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/util/u_framebuffer.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2009-2010 VMware, Inc.  All Rights Reserved.
4  *
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:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
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.
24  *
25  **************************************************************************/
26 
27 /**
28  * @file
29  * Framebuffer utility functions.
30  *
31  * @author Brian Paul
32  */
33 
34 
35 #include "pipe/p_screen.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
39 
40 #include "util/u_memory.h"
41 #include "util/u_framebuffer.h"
42 
43 
44 /**
45  * Compare pipe_framebuffer_state objects.
46  * \return TRUE if same, FALSE if different
47  */
48 bool
util_framebuffer_state_equal(const struct pipe_framebuffer_state * dst,const struct pipe_framebuffer_state * src)49 util_framebuffer_state_equal(const struct pipe_framebuffer_state *dst,
50                              const struct pipe_framebuffer_state *src)
51 {
52    unsigned i;
53 
54    if (dst->width != src->width ||
55        dst->height != src->height)
56       return false;
57 
58    if (dst->samples != src->samples ||
59        dst->layers  != src->layers)
60       return false;
61 
62    if (dst->nr_cbufs != src->nr_cbufs) {
63       return false;
64    }
65 
66    for (i = 0; i < src->nr_cbufs; i++) {
67       if (dst->cbufs[i] != src->cbufs[i]) {
68          return false;
69       }
70    }
71 
72    if (dst->zsbuf != src->zsbuf) {
73       return false;
74    }
75 
76    if (dst->resolve != src->resolve) {
77       return false;
78    }
79 
80    return true;
81 }
82 
83 
84 /**
85  * Copy framebuffer state from src to dst, updating refcounts.
86  */
87 void
util_copy_framebuffer_state(struct pipe_framebuffer_state * dst,const struct pipe_framebuffer_state * src)88 util_copy_framebuffer_state(struct pipe_framebuffer_state *dst,
89                             const struct pipe_framebuffer_state *src)
90 {
91    unsigned i;
92 
93    if (src) {
94       dst->width = src->width;
95       dst->height = src->height;
96 
97       dst->samples = src->samples;
98       dst->layers  = src->layers;
99 
100       for (i = 0; i < src->nr_cbufs; i++)
101          pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
102 
103       /* Set remaining dest cbuf pointers to NULL */
104       for ( ; i < ARRAY_SIZE(dst->cbufs); i++)
105          pipe_surface_reference(&dst->cbufs[i], NULL);
106 
107       dst->nr_cbufs = src->nr_cbufs;
108 
109       dst->viewmask = src->viewmask;
110       pipe_surface_reference(&dst->zsbuf, src->zsbuf);
111       pipe_resource_reference(&dst->resolve, src->resolve);
112    } else {
113       dst->width = 0;
114       dst->height = 0;
115 
116       dst->samples = 0;
117       dst->layers  = 0;
118 
119       for (i = 0 ; i < ARRAY_SIZE(dst->cbufs); i++)
120          pipe_surface_reference(&dst->cbufs[i], NULL);
121 
122       dst->nr_cbufs = 0;
123 
124       dst->viewmask = 0;
125 
126       pipe_surface_reference(&dst->zsbuf, NULL);
127       pipe_resource_reference(&dst->resolve, NULL);
128    }
129 }
130 
131 
132 void
util_unreference_framebuffer_state(struct pipe_framebuffer_state * fb)133 util_unreference_framebuffer_state(struct pipe_framebuffer_state *fb)
134 {
135    unsigned i;
136 
137    for (i = 0; i < fb->nr_cbufs; i++) {
138       pipe_surface_reference(&fb->cbufs[i], NULL);
139    }
140 
141    pipe_surface_reference(&fb->zsbuf, NULL);
142    pipe_resource_reference(&fb->resolve, NULL);
143 
144    fb->samples = fb->layers = 0;
145    fb->width = fb->height = 0;
146    fb->nr_cbufs = 0;
147 
148    fb->viewmask = 0;
149 }
150 
151 
152 /* Where multiple sizes are allowed for framebuffer surfaces, find the
153  * minimum width and height of all bound surfaces.
154  */
155 bool
util_framebuffer_min_size(const struct pipe_framebuffer_state * fb,unsigned * width,unsigned * height)156 util_framebuffer_min_size(const struct pipe_framebuffer_state *fb,
157                           unsigned *width,
158                           unsigned *height)
159 {
160    unsigned w = ~0;
161    unsigned h = ~0;
162    unsigned i;
163 
164    for (i = 0; i < fb->nr_cbufs; i++) {
165       if (!fb->cbufs[i])
166          continue;
167 
168       w = MIN2(w, fb->cbufs[i]->width);
169       h = MIN2(h, fb->cbufs[i]->height);
170    }
171 
172    if (fb->zsbuf) {
173       w = MIN2(w, fb->zsbuf->width);
174       h = MIN2(h, fb->zsbuf->height);
175    }
176 
177    if (w == ~0u) {
178       *width = 0;
179       *height = 0;
180       return false;
181    }
182    else {
183       *width = w;
184       *height = h;
185       return true;
186    }
187 }
188 
189 
190 /**
191  * Return the number of layers set in the framebuffer state.
192  */
193 unsigned
util_framebuffer_get_num_layers(const struct pipe_framebuffer_state * fb)194 util_framebuffer_get_num_layers(const struct pipe_framebuffer_state *fb)
195 {
196 	unsigned i, num_layers = 0;
197 
198 	/**
199 	 * In the case of ARB_framebuffer_no_attachment
200 	 * we obtain the number of layers directly from
201 	 * the framebuffer state.
202 	 */
203 	if (!(fb->nr_cbufs || fb->zsbuf))
204 		return fb->layers;
205 
206 	for (i = 0; i < fb->nr_cbufs; i++) {
207 		if (fb->cbufs[i]) {
208 			unsigned num = fb->cbufs[i]->u.tex.last_layer -
209 				       fb->cbufs[i]->u.tex.first_layer + 1;
210 			num_layers = MAX2(num_layers, num);
211 		}
212 	}
213 	if (fb->zsbuf) {
214 		unsigned num = fb->zsbuf->u.tex.last_layer -
215 			       fb->zsbuf->u.tex.first_layer + 1;
216 		num_layers = MAX2(num_layers, num);
217 	}
218 	return num_layers;
219 }
220 
221 
222 /**
223  * Return the number of MSAA samples.
224  */
225 unsigned
util_framebuffer_get_num_samples(const struct pipe_framebuffer_state * fb)226 util_framebuffer_get_num_samples(const struct pipe_framebuffer_state *fb)
227 {
228    unsigned i;
229 
230    /**
231     * In the case of ARB_framebuffer_no_attachment
232     * we obtain the number of samples directly from
233     * the framebuffer state.
234     *
235     * NOTE: fb->samples may wind up as zero due to memset()'s on internal
236     *       driver structures on their initialization and so we take the
237     *       MAX here to ensure we have a valid number of samples. However,
238     *       if samples is legitimately not getting set somewhere
239     *       multi-sampling will evidently break.
240     */
241    if (!(fb->nr_cbufs || fb->zsbuf))
242       return MAX2(fb->samples, 1);
243 
244    /**
245     * If a driver doesn't advertise PIPE_CAP_SURFACE_SAMPLE_COUNT,
246     * pipe_surface::nr_samples will always be 0.
247     */
248    for (i = 0; i < fb->nr_cbufs; i++) {
249       if (fb->cbufs[i]) {
250          return MAX3(1, fb->cbufs[i]->texture->nr_samples,
251                      fb->cbufs[i]->nr_samples);
252       }
253    }
254    if (fb->zsbuf) {
255       return MAX3(1, fb->zsbuf->texture->nr_samples,
256                   fb->zsbuf->nr_samples);
257    }
258 
259    return MAX2(fb->samples, 1);
260 }
261 
262 
263 /**
264  * Flip the sample location state along the Y axis.
265  */
266 void
util_sample_locations_flip_y(struct pipe_screen * screen,unsigned fb_height,unsigned samples,uint8_t * locations)267 util_sample_locations_flip_y(struct pipe_screen *screen, unsigned fb_height,
268                              unsigned samples, uint8_t *locations)
269 {
270    unsigned row, i, shift, grid_width, grid_height;
271    uint8_t new_locations[
272       PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
273       PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
274 
275    screen->get_sample_pixel_grid(screen, samples, &grid_width, &grid_height);
276 
277    shift = fb_height % grid_height;
278 
279    for (row = 0; row < grid_height; row++) {
280       unsigned row_size = grid_width * samples;
281       for (i = 0; i < row_size; i++) {
282          unsigned dest_row = grid_height - row - 1;
283          /* this relies on unsigned integer wraparound behaviour */
284          dest_row = (dest_row - shift) % grid_height;
285          new_locations[dest_row * row_size + i] = locations[row * row_size + i];
286       }
287    }
288 
289    memcpy(locations, new_locations, grid_width * grid_height * samples);
290 }
291