1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2013 VMware, Inc.
3bf215546Sopenharmony_ci * All Rights Reserved.
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
7bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
8bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
9bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
10bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
11bf215546Sopenharmony_ci * the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
14bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
15bf215546Sopenharmony_ci * of the Software.
16bf215546Sopenharmony_ci *
17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20bf215546Sopenharmony_ci * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
21bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_defines.h"
28bf215546Sopenharmony_ci#include "pipe/p_state.h"
29bf215546Sopenharmony_ci#include "util/format/u_format.h"
30bf215546Sopenharmony_ci#include "util/u_math.h"
31bf215546Sopenharmony_ci#include "util/u_resource.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/**
35bf215546Sopenharmony_ci * Return the size of the resource in bytes.
36bf215546Sopenharmony_ci */
37bf215546Sopenharmony_ciunsigned
38bf215546Sopenharmony_ciutil_resource_size(const struct pipe_resource *res)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   unsigned width = res->width0;
41bf215546Sopenharmony_ci   unsigned height = res->height0;
42bf215546Sopenharmony_ci   unsigned depth = res->depth0;
43bf215546Sopenharmony_ci   unsigned size = 0;
44bf215546Sopenharmony_ci   unsigned level;
45bf215546Sopenharmony_ci   unsigned samples = MAX2(1, res->nr_samples);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   for (level = 0; level <= res->last_level; level++) {
48bf215546Sopenharmony_ci      unsigned slices;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci      if (res->target == PIPE_TEXTURE_CUBE)
51bf215546Sopenharmony_ci         slices = 6;
52bf215546Sopenharmony_ci      else if (res->target == PIPE_TEXTURE_3D)
53bf215546Sopenharmony_ci         slices = depth;
54bf215546Sopenharmony_ci      else
55bf215546Sopenharmony_ci         slices = res->array_size;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci      size += (util_format_get_nblocksy(res->format, height) *
58bf215546Sopenharmony_ci               util_format_get_stride(res->format, width) * slices * samples);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci      width  = u_minify(width, 1);
61bf215546Sopenharmony_ci      height = u_minify(height, 1);
62bf215546Sopenharmony_ci      depth = u_minify(depth, 1);
63bf215546Sopenharmony_ci   }
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   return size;
66bf215546Sopenharmony_ci}
67