Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_sampler_view.c
4570 views
/**********************************************************1* Copyright 2008-2009 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/2425#include "svga_cmd.h"2627#include "pipe/p_state.h"28#include "pipe/p_defines.h"29#include "util/u_inlines.h"30#include "os/os_thread.h"31#include "util/format/u_format.h"32#include "util/u_math.h"33#include "util/u_memory.h"34#include "util/u_string.h"3536#include "svga_format.h"37#include "svga_screen.h"38#include "svga_context.h"39#include "svga_resource_texture.h"40#include "svga_sampler_view.h"41#include "svga_debug.h"42#include "svga_surface.h"434445void46svga_debug_describe_sampler_view(char *buf, const struct svga_sampler_view *sv)47{48char res[128];49debug_describe_resource(res, sv->texture);50sprintf(buf, "svga_sampler_view<%s,[%u,%u]>",51res, sv->min_lod, sv->max_lod);52}535455struct svga_sampler_view *56svga_get_tex_sampler_view(struct pipe_context *pipe,57struct pipe_resource *pt,58unsigned min_lod, unsigned max_lod)59{60struct svga_context *svga = svga_context(pipe);61struct svga_screen *ss = svga_screen(pipe->screen);62struct svga_texture *tex = svga_texture(pt);63struct svga_sampler_view *sv = NULL;64SVGA3dSurface1Flags flags = SVGA3D_SURFACE_HINT_TEXTURE;65SVGA3dSurfaceFormat format = svga_translate_format(ss, pt->format,66PIPE_BIND_SAMPLER_VIEW);67boolean view = TRUE;6869assert(pt);70assert(min_lod <= max_lod);71assert(max_lod <= pt->last_level);72assert(!svga_have_vgpu10(svga));7374/* Is a view needed */75{76/*77* Can't control max lod. For first level views and when we only78* look at one level we disable mip filtering to achive the same79* results as a view.80*/81if (min_lod == 0 && max_lod >= pt->last_level)82view = FALSE;8384if (ss->debug.no_sampler_view)85view = FALSE;8687if (ss->debug.force_sampler_view)88view = TRUE;89}9091/* First try the cache */92if (view) {93mtx_lock(&ss->tex_mutex);94if (tex->cached_view &&95tex->cached_view->min_lod == min_lod &&96tex->cached_view->max_lod == max_lod) {97svga_sampler_view_reference(&sv, tex->cached_view);98mtx_unlock(&ss->tex_mutex);99SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",100pt, min_lod, max_lod, pt->last_level);101svga_validate_sampler_view(svga_context(pipe), sv);102return sv;103}104mtx_unlock(&ss->tex_mutex);105}106107sv = CALLOC_STRUCT(svga_sampler_view);108if (!sv)109return NULL;110111pipe_reference_init(&sv->reference, 1);112113/* Note: we're not refcounting the texture resource here to avoid114* a circular dependency.115*/116sv->texture = pt;117118sv->min_lod = min_lod;119sv->max_lod = max_lod;120121/* No view needed just use the whole texture */122if (!view) {123SVGA_DBG(DEBUG_VIEWS,124"svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",125pt, min_lod, max_lod,126max_lod - min_lod + 1,127pt->width0,128pt->height0,129pt->depth0,130pt->last_level);131sv->key.cachable = 0;132sv->handle = tex->handle;133debug_reference(&sv->reference,134(debug_reference_descriptor)svga_debug_describe_sampler_view, 0);135return sv;136}137138SVGA_DBG(DEBUG_VIEWS,139"svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",140pt, min_lod, max_lod,141max_lod - min_lod + 1,142pt->width0,143pt->height0,144pt->depth0,145pt->last_level);146147sv->age = tex->age;148sv->handle = svga_texture_view_surface(svga, tex,149PIPE_BIND_SAMPLER_VIEW,150flags, format,151min_lod,152max_lod - min_lod + 1,153-1, 1, -1, FALSE,154&sv->key);155156if (!sv->handle) {157sv->key.cachable = 0;158sv->handle = tex->handle;159debug_reference(&sv->reference,160(debug_reference_descriptor)161svga_debug_describe_sampler_view, 0);162return sv;163}164165mtx_lock(&ss->tex_mutex);166svga_sampler_view_reference(&tex->cached_view, sv);167mtx_unlock(&ss->tex_mutex);168169debug_reference(&sv->reference,170(debug_reference_descriptor)171svga_debug_describe_sampler_view, 0);172173return sv;174}175176177void178svga_validate_sampler_view(struct svga_context *svga,179struct svga_sampler_view *v)180{181struct svga_texture *tex = svga_texture(v->texture);182unsigned numFaces;183unsigned age = 0;184int i;185unsigned k;186187assert(svga);188assert(!svga_have_vgpu10(svga));189190if (v->handle == tex->handle)191return;192193age = tex->age;194195if (tex->b.target == PIPE_TEXTURE_CUBE)196numFaces = 6;197else198numFaces = 1;199200for (i = v->min_lod; i <= v->max_lod; i++) {201for (k = 0; k < numFaces; k++) {202assert(i < ARRAY_SIZE(tex->view_age));203if (v->age < tex->view_age[i])204svga_texture_copy_handle(svga,205tex->handle, 0, 0, 0, i, k,206v->handle, 0, 0, 0, i - v->min_lod, k,207u_minify(tex->b.width0, i),208u_minify(tex->b.height0, i),209u_minify(tex->b.depth0, i));210}211}212213v->age = age;214}215216217void218svga_destroy_sampler_view_priv(struct svga_sampler_view *v)219{220struct svga_texture *tex = svga_texture(v->texture);221222if (v->handle != tex->handle) {223struct svga_screen *ss = svga_screen(v->texture->screen);224SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);225svga_screen_surface_destroy(ss, &v->key, &v->handle);226}227228/* Note: we're not refcounting the texture resource here to avoid229* a circular dependency.230*/231v->texture = NULL;232233FREE(v);234}235236237