Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_gen_mipmap.c
4561 views
/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4* Copyright 2008 VMware, Inc. All rights reserved.5* Copyright 2014 Advanced Micro Devices, Inc.6*7* Permission is hereby granted, free of charge, to any person obtaining a8* copy of this software and associated documentation files (the9* "Software"), to deal in the Software without restriction, including10* without limitation the rights to use, copy, modify, merge, publish,11* distribute, sub license, and/or sell copies of the Software, and to12* permit persons to whom the Software is furnished to do so, subject to13* the following conditions:14*15* The above copyright notice and this permission notice (including the16* next paragraph) shall be included in all copies or substantial portions17* of the Software.18*19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS20* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.22* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR23* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,24* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE25* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.26*27**************************************************************************/2829/**30* @file31* Mipmap generation utility32*33* @author Brian Paul, Marek Olšák34*/353637#include "util/u_gen_mipmap.h"38#include "util/format/u_format.h"39#include "util/u_inlines.h"404142/**43* Generate mipmap images. It's assumed all needed texture memory is44* already allocated.45*46* \param pt the texture to generate mipmap levels for47* \param format format of texture48* \param first_layer the first layer to generate mipmap levels for49* (ignored for 3D textures)50* \param last_layer the last layer to generate mipmap levels for51* (ignored for 3D textures)52* \param base_level the first mipmap level to use as a src53* \param last_level the last mipmap level to generate54* \param filter the minification filter used to generate mipmap levels with55* one of PIPE_TEX_FILTER_LINEAR, PIPE_TEX_FILTER_NEAREST56*/57boolean58util_gen_mipmap(struct pipe_context *pipe, struct pipe_resource *pt,59enum pipe_format format, uint base_level, uint last_level,60uint first_layer, uint last_layer, uint filter)61{62struct pipe_screen *screen = pipe->screen;63struct pipe_blit_info blit;64uint dstLevel;65boolean is_zs = util_format_is_depth_or_stencil(format);66boolean has_depth =67util_format_has_depth(util_format_description(format));6869/* nothing to do for stencil-only formats */70if (is_zs && !has_depth)71return TRUE;7273/* nothing to do for integer formats */74if (!is_zs && util_format_is_pure_integer(format))75return TRUE;7677if (!screen->is_format_supported(screen, format, pt->target,78pt->nr_samples, pt->nr_storage_samples,79PIPE_BIND_SAMPLER_VIEW |80(is_zs ? PIPE_BIND_DEPTH_STENCIL :81PIPE_BIND_RENDER_TARGET))) {82return FALSE;83}8485/* The texture object should have room for the levels which we're86* about to generate.87*/88assert(last_level <= pt->last_level);8990/* If this fails, why are we here? */91assert(last_level > base_level);92assert(filter == PIPE_TEX_FILTER_LINEAR ||93filter == PIPE_TEX_FILTER_NEAREST);9495memset(&blit, 0, sizeof(blit));96blit.src.resource = blit.dst.resource = pt;97blit.src.format = blit.dst.format = format;98/* don't set the stencil mask, stencil shouldn't be changed */99blit.mask = is_zs ? PIPE_MASK_Z : PIPE_MASK_RGBA;100blit.filter = filter;101102for (dstLevel = base_level + 1; dstLevel <= last_level; dstLevel++) {103blit.src.level = dstLevel - 1;104blit.dst.level = dstLevel;105106blit.src.box.width = u_minify(pt->width0, blit.src.level);107blit.src.box.height = u_minify(pt->height0, blit.src.level);108109blit.dst.box.width = u_minify(pt->width0, blit.dst.level);110blit.dst.box.height = u_minify(pt->height0, blit.dst.level);111112if (pt->target == PIPE_TEXTURE_3D) {113/* generate all layers/slices at once */114blit.src.box.z = blit.dst.box.z = 0;115blit.src.box.depth = util_num_layers(pt, blit.src.level);116blit.dst.box.depth = util_num_layers(pt, blit.dst.level);117}118else {119blit.src.box.z = blit.dst.box.z = first_layer;120blit.src.box.depth = blit.dst.box.depth =121(last_layer + 1 - first_layer);122}123124pipe->blit(pipe, &blit);125}126return TRUE;127}128129130