u_framebuffer.c revision af69d88d
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 */
48boolean
49util_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   for (i = 0; i < Elements(src->cbufs); i++) {
59      if (dst->cbufs[i] != src->cbufs[i]) {
60         return FALSE;
61      }
62   }
63
64   if (dst->nr_cbufs != src->nr_cbufs) {
65      return FALSE;
66   }
67
68   if (dst->zsbuf != src->zsbuf) {
69      return FALSE;
70   }
71
72   return TRUE;
73}
74
75
76/**
77 * Copy framebuffer state from src to dst, updating refcounts.
78 */
79void
80util_copy_framebuffer_state(struct pipe_framebuffer_state *dst,
81                            const struct pipe_framebuffer_state *src)
82{
83   unsigned i;
84
85   dst->width = src->width;
86   dst->height = src->height;
87
88   for (i = 0; i < src->nr_cbufs; i++)
89      pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
90
91   /* Set remaining dest cbuf pointers to NULL */
92   for ( ; i < Elements(dst->cbufs); i++)
93      pipe_surface_reference(&dst->cbufs[i], NULL);
94
95   dst->nr_cbufs = src->nr_cbufs;
96
97   pipe_surface_reference(&dst->zsbuf, src->zsbuf);
98}
99
100
101void
102util_unreference_framebuffer_state(struct pipe_framebuffer_state *fb)
103{
104   unsigned i;
105
106   for (i = 0; i < fb->nr_cbufs; i++) {
107      pipe_surface_reference(&fb->cbufs[i], NULL);
108   }
109
110   pipe_surface_reference(&fb->zsbuf, NULL);
111
112   fb->width = fb->height = 0;
113   fb->nr_cbufs = 0;
114}
115
116
117/* Where multiple sizes are allowed for framebuffer surfaces, find the
118 * minimum width and height of all bound surfaces.
119 */
120boolean
121util_framebuffer_min_size(const struct pipe_framebuffer_state *fb,
122                          unsigned *width,
123                          unsigned *height)
124{
125   unsigned w = ~0;
126   unsigned h = ~0;
127   unsigned i;
128
129   for (i = 0; i < fb->nr_cbufs; i++) {
130      if (!fb->cbufs[i])
131         continue;
132
133      w = MIN2(w, fb->cbufs[i]->width);
134      h = MIN2(h, fb->cbufs[i]->height);
135   }
136
137   if (fb->zsbuf) {
138      w = MIN2(w, fb->zsbuf->width);
139      h = MIN2(h, fb->zsbuf->height);
140   }
141
142   if (w == ~0) {
143      *width = 0;
144      *height = 0;
145      return FALSE;
146   }
147   else {
148      *width = w;
149      *height = h;
150      return TRUE;
151   }
152}
153
154
155/**
156 * Return the number of layers set in the framebuffer state.
157 */
158unsigned
159util_framebuffer_get_num_layers(const struct pipe_framebuffer_state *fb)
160{
161	unsigned i, num_layers = 0;
162
163	for (i = 0; i < fb->nr_cbufs; i++) {
164		if (fb->cbufs[i]) {
165			unsigned num = fb->cbufs[i]->u.tex.last_layer -
166				       fb->cbufs[i]->u.tex.first_layer + 1;
167			num_layers = MAX2(num_layers, num);
168		}
169	}
170	if (fb->zsbuf) {
171		unsigned num = fb->zsbuf->u.tex.last_layer -
172			       fb->zsbuf->u.tex.first_layer + 1;
173		num_layers = MAX2(num_layers, num);
174	}
175	return num_layers;
176}
177
178
179/**
180 * Return the number of MSAA samples.
181 */
182unsigned
183util_framebuffer_get_num_samples(const struct pipe_framebuffer_state *fb)
184{
185   unsigned i;
186
187   for (i = 0; i < fb->nr_cbufs; i++) {
188      if (fb->cbufs[i]) {
189         return MAX2(1, fb->cbufs[i]->texture->nr_samples);
190      }
191   }
192   if (fb->zsbuf) {
193      return MAX2(1, fb->zsbuf->texture->nr_samples);
194   }
195
196   return 1;
197}
198