1 /********************************************************** 2 * Copyright 2008-2012 VMware, Inc. All rights reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without 7 * restriction, including without limitation the rights to use, copy, 8 * modify, merge, publish, distribute, sublicense, and/or sell copies 9 * of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 * 24 **********************************************************/ 25 26 #include "util/u_debug.h" 27 28 #include "svga_resource.h" 29 #include "svga_resource_buffer.h" 30 #include "svga_resource_texture.h" 31 #include "svga_context.h" 32 #include "svga_screen.h" 33 #include "svga_format.h" 34 35 36 static struct pipe_resource * 37 svga_resource_create(struct pipe_screen *screen, 38 const struct pipe_resource *template) 39 { 40 if (template->target == PIPE_BUFFER) 41 return svga_buffer_create(screen, template); 42 else 43 return svga_texture_create(screen, template); 44 } 45 46 47 static struct pipe_resource * 48 svga_resource_from_handle(struct pipe_screen * screen, 49 const struct pipe_resource *template, 50 struct winsys_handle *whandle, 51 unsigned usage) 52 { 53 if (template->target == PIPE_BUFFER) 54 return NULL; 55 else 56 return svga_texture_from_handle(screen, template, whandle); 57 } 58 59 60 /** 61 * Check if a resource (texture, buffer) of the given size 62 * and format can be created. 63 * \Return TRUE if OK, FALSE if too large. 64 */ 65 static boolean 66 svga_can_create_resource(struct pipe_screen *screen, 67 const struct pipe_resource *res) 68 { 69 struct svga_screen *svgascreen = svga_screen(screen); 70 struct svga_winsys_screen *sws = svgascreen->sws; 71 SVGA3dSurfaceFormat format; 72 SVGA3dSize base_level_size; 73 uint32 numMipLevels; 74 uint32 arraySize; 75 76 if (res->target == PIPE_BUFFER) { 77 format = SVGA3D_BUFFER; 78 base_level_size.width = res->width0; 79 base_level_size.height = 1; 80 base_level_size.depth = 1; 81 numMipLevels = 1; 82 arraySize = 1; 83 84 } else { 85 if (res->target == PIPE_TEXTURE_CUBE) 86 assert(res->array_size == 6); 87 88 format = svga_translate_format(svgascreen, res->format, res->bind); 89 if (format == SVGA3D_FORMAT_INVALID) 90 return FALSE; 91 92 base_level_size.width = res->width0; 93 base_level_size.height = res->height0; 94 base_level_size.depth = res->depth0; 95 numMipLevels = res->last_level + 1; 96 arraySize = res->array_size; 97 } 98 99 return sws->surface_can_create(sws, format, base_level_size, 100 arraySize, numMipLevels); 101 } 102 103 104 void 105 svga_init_resource_functions(struct svga_context *svga) 106 { 107 svga->pipe.transfer_map = u_transfer_map_vtbl; 108 svga->pipe.transfer_flush_region = u_transfer_flush_region_vtbl; 109 svga->pipe.transfer_unmap = u_transfer_unmap_vtbl; 110 svga->pipe.buffer_subdata = u_default_buffer_subdata; 111 svga->pipe.texture_subdata = u_default_texture_subdata; 112 113 if (svga_have_vgpu10(svga)) { 114 svga->pipe.generate_mipmap = svga_texture_generate_mipmap; 115 } else { 116 svga->pipe.generate_mipmap = NULL; 117 } 118 } 119 120 void 121 svga_init_screen_resource_functions(struct svga_screen *is) 122 { 123 is->screen.resource_create = svga_resource_create; 124 is->screen.resource_from_handle = svga_resource_from_handle; 125 is->screen.resource_get_handle = u_resource_get_handle_vtbl; 126 is->screen.resource_destroy = u_resource_destroy_vtbl; 127 is->screen.can_create_resource = svga_can_create_resource; 128 } 129