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