1848b8605Smrg/*
2848b8605Smrg * Copyright 2013 VMware, Inc.
3848b8605Smrg * All Rights Reserved.
4848b8605Smrg *
5848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6848b8605Smrg * copy of this software and associated documentation files (the
7848b8605Smrg * "Software"), to deal in the Software without restriction, including
8848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
9848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
10848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
11848b8605Smrg * the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice (including the
14848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
15848b8605Smrg * of the Software.
16848b8605Smrg *
17848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20848b8605Smrg * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
21848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24848b8605Smrg */
25848b8605Smrg
26848b8605Smrg
27848b8605Smrg#include "pipe/p_defines.h"
28848b8605Smrg#include "pipe/p_state.h"
29848b8605Smrg#include "util/u_format.h"
30848b8605Smrg#include "util/u_math.h"
31848b8605Smrg#include "util/u_resource.h"
32848b8605Smrg
33848b8605Smrg
34848b8605Smrg/**
35848b8605Smrg * Return the size of the resource in bytes.
36848b8605Smrg */
37848b8605Smrgunsigned
38848b8605Smrgutil_resource_size(const struct pipe_resource *res)
39848b8605Smrg{
40848b8605Smrg   unsigned width = res->width0;
41848b8605Smrg   unsigned height = res->height0;
42848b8605Smrg   unsigned depth = res->depth0;
43848b8605Smrg   unsigned size = 0;
44848b8605Smrg   unsigned level;
45b8e80941Smrg   unsigned samples = MAX2(1, res->nr_samples);
46848b8605Smrg
47848b8605Smrg   for (level = 0; level <= res->last_level; level++) {
48848b8605Smrg      unsigned slices;
49848b8605Smrg
50848b8605Smrg      if (res->target == PIPE_TEXTURE_CUBE)
51848b8605Smrg         slices = 6;
52848b8605Smrg      else if (res->target == PIPE_TEXTURE_3D)
53848b8605Smrg         slices = depth;
54848b8605Smrg      else
55848b8605Smrg         slices = res->array_size;
56848b8605Smrg
57848b8605Smrg      size += (util_format_get_nblocksy(res->format, height) *
58b8e80941Smrg               util_format_get_stride(res->format, width) * slices * samples);
59848b8605Smrg
60848b8605Smrg      width  = u_minify(width, 1);
61848b8605Smrg      height = u_minify(height, 1);
62848b8605Smrg      depth = u_minify(depth, 1);
63848b8605Smrg   }
64848b8605Smrg
65848b8605Smrg   return size;
66848b8605Smrg}
67