Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_resource.c
4570 views
/**********************************************************1* Copyright 2008-2012 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 "util/u_debug.h"2627#include "svga_resource.h"28#include "svga_resource_buffer.h"29#include "svga_resource_texture.h"30#include "svga_context.h"31#include "svga_screen.h"32#include "svga_format.h"333435/**36* This is the primary driver entrypoint for allocating graphics memory37* (vertex/index/constant buffers, textures, etc)38*/39static struct pipe_resource *40svga_resource_create(struct pipe_screen *screen,41const struct pipe_resource *template)42{43struct pipe_resource *r;4445if (template->target == PIPE_BUFFER)46r = svga_buffer_create(screen, template);47else48r = svga_texture_create(screen, template);4950if (!r) {51struct svga_screen *svgascreen = svga_screen(screen);52svgascreen->hud.num_failed_allocations++;53}5455return r;56}575859static struct pipe_resource *60svga_resource_from_handle(struct pipe_screen * screen,61const struct pipe_resource *template,62struct winsys_handle *whandle,63unsigned usage)64{65if (template->target == PIPE_BUFFER)66return NULL;67else68return svga_texture_from_handle(screen, template, whandle);69}707172/**73* Check if a resource (texture, buffer) of the given size74* and format can be created.75* \Return TRUE if OK, FALSE if too large.76*/77static bool78svga_can_create_resource(struct pipe_screen *screen,79const struct pipe_resource *res)80{81struct svga_screen *svgascreen = svga_screen(screen);82struct svga_winsys_screen *sws = svgascreen->sws;83SVGA3dSurfaceFormat format;84SVGA3dSize base_level_size;85uint32 numMipLevels;86uint32 arraySize;87uint32 numSamples;8889if (res->target == PIPE_BUFFER) {90format = SVGA3D_BUFFER;91base_level_size.width = res->width0;92base_level_size.height = 1;93base_level_size.depth = 1;94numMipLevels = 1;95arraySize = 1;96numSamples = 0;9798} else {99if (res->target == PIPE_TEXTURE_CUBE)100assert(res->array_size == 6);101102format = svga_translate_format(svgascreen, res->format, res->bind);103if (format == SVGA3D_FORMAT_INVALID)104return false;105106base_level_size.width = res->width0;107base_level_size.height = res->height0;108base_level_size.depth = res->depth0;109numMipLevels = res->last_level + 1;110arraySize = res->array_size;111numSamples = res->nr_samples;112}113114return sws->surface_can_create(sws, format, base_level_size,115arraySize, numMipLevels, numSamples);116}117118119void120svga_init_resource_functions(struct svga_context *svga)121{122svga->pipe.buffer_map = svga_buffer_transfer_map;123svga->pipe.texture_map = svga_texture_transfer_map;124svga->pipe.transfer_flush_region = svga_buffer_transfer_flush_region;125svga->pipe.buffer_unmap = svga_buffer_transfer_unmap;126svga->pipe.texture_unmap = svga_texture_transfer_unmap;127svga->pipe.buffer_subdata = u_default_buffer_subdata;128svga->pipe.texture_subdata = u_default_texture_subdata;129130if (svga_have_vgpu10(svga)) {131svga->pipe.generate_mipmap = svga_texture_generate_mipmap;132} else {133svga->pipe.generate_mipmap = NULL;134}135}136137void138svga_init_screen_resource_functions(struct svga_screen *is)139{140is->screen.resource_create = svga_resource_create;141is->screen.resource_from_handle = svga_resource_from_handle;142is->screen.resource_get_handle = svga_resource_get_handle;143is->screen.resource_destroy = svga_resource_destroy;144is->screen.can_create_resource = svga_can_create_resource;145}146147148