Brian Paul | 59d2c4f | 2012-01-07 14:16:27 -0700 | [diff] [blame] | 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 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 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" |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 33 | #include "svga_format.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 34 | |
| 35 | |
Brian Paul | e3f5b8a | 2017-06-16 16:36:43 -0600 | [diff] [blame] | 36 | /** |
| 37 | * This is the primary driver entrypoint for allocating graphics memory |
| 38 | * (vertex/index/constant buffers, textures, etc) |
| 39 | */ |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 40 | static struct pipe_resource * |
| 41 | svga_resource_create(struct pipe_screen *screen, |
Brian Paul | 59d2c4f | 2012-01-07 14:16:27 -0700 | [diff] [blame] | 42 | const struct pipe_resource *template) |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 43 | { |
Brian Paul | e3f5b8a | 2017-06-16 16:36:43 -0600 | [diff] [blame] | 44 | struct pipe_resource *r; |
| 45 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 46 | if (template->target == PIPE_BUFFER) |
Brian Paul | e3f5b8a | 2017-06-16 16:36:43 -0600 | [diff] [blame] | 47 | r = svga_buffer_create(screen, template); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 48 | else |
Brian Paul | e3f5b8a | 2017-06-16 16:36:43 -0600 | [diff] [blame] | 49 | r = svga_texture_create(screen, template); |
| 50 | |
| 51 | if (!r) { |
| 52 | struct svga_screen *svgascreen = svga_screen(screen); |
| 53 | svgascreen->hud.num_failed_allocations++; |
| 54 | } |
| 55 | |
| 56 | return r; |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Brian Paul | 59d2c4f | 2012-01-07 14:16:27 -0700 | [diff] [blame] | 59 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 60 | static struct pipe_resource * |
| 61 | svga_resource_from_handle(struct pipe_screen * screen, |
Brian Paul | 59d2c4f | 2012-01-07 14:16:27 -0700 | [diff] [blame] | 62 | const struct pipe_resource *template, |
Marek Olšák | 82db518 | 2016-02-24 18:51:15 +0100 | [diff] [blame] | 63 | struct winsys_handle *whandle, |
| 64 | unsigned usage) |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 65 | { |
| 66 | if (template->target == PIPE_BUFFER) |
| 67 | return NULL; |
| 68 | else |
Jakob Bornecrantz | 35d960c | 2010-04-19 20:10:57 +0100 | [diff] [blame] | 69 | return svga_texture_from_handle(screen, template, whandle); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 73 | /** |
| 74 | * Check if a resource (texture, buffer) of the given size |
| 75 | * and format can be created. |
| 76 | * \Return TRUE if OK, FALSE if too large. |
| 77 | */ |
| 78 | static boolean |
| 79 | svga_can_create_resource(struct pipe_screen *screen, |
| 80 | const struct pipe_resource *res) |
| 81 | { |
| 82 | struct svga_screen *svgascreen = svga_screen(screen); |
| 83 | struct svga_winsys_screen *sws = svgascreen->sws; |
| 84 | SVGA3dSurfaceFormat format; |
| 85 | SVGA3dSize base_level_size; |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 86 | uint32 numMipLevels; |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 87 | uint32 arraySize; |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 88 | |
| 89 | if (res->target == PIPE_BUFFER) { |
| 90 | format = SVGA3D_BUFFER; |
| 91 | base_level_size.width = res->width0; |
| 92 | base_level_size.height = 1; |
| 93 | base_level_size.depth = 1; |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 94 | numMipLevels = 1; |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 95 | arraySize = 1; |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 96 | |
| 97 | } else { |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 98 | if (res->target == PIPE_TEXTURE_CUBE) |
| 99 | assert(res->array_size == 6); |
| 100 | |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 101 | format = svga_translate_format(svgascreen, res->format, res->bind); |
| 102 | if (format == SVGA3D_FORMAT_INVALID) |
| 103 | return FALSE; |
| 104 | |
| 105 | base_level_size.width = res->width0; |
| 106 | base_level_size.height = res->height0; |
| 107 | base_level_size.depth = res->depth0; |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 108 | numMipLevels = res->last_level + 1; |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 109 | arraySize = res->array_size; |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return sws->surface_can_create(sws, format, base_level_size, |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 113 | arraySize, numMipLevels); |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 117 | void |
| 118 | svga_init_resource_functions(struct svga_context *svga) |
| 119 | { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 120 | svga->pipe.transfer_map = u_transfer_map_vtbl; |
| 121 | svga->pipe.transfer_flush_region = u_transfer_flush_region_vtbl; |
| 122 | svga->pipe.transfer_unmap = u_transfer_unmap_vtbl; |
Marek Olšák | 1ffe77e | 2016-07-16 21:19:48 +0200 | [diff] [blame] | 123 | svga->pipe.buffer_subdata = u_default_buffer_subdata; |
| 124 | svga->pipe.texture_subdata = u_default_texture_subdata; |
Charmaine Lee | 6303231 | 2015-12-22 11:20:41 -0800 | [diff] [blame] | 125 | |
| 126 | if (svga_have_vgpu10(svga)) { |
| 127 | svga->pipe.generate_mipmap = svga_texture_generate_mipmap; |
| 128 | } else { |
| 129 | svga->pipe.generate_mipmap = NULL; |
| 130 | } |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | svga_init_screen_resource_functions(struct svga_screen *is) |
| 135 | { |
| 136 | is->screen.resource_create = svga_resource_create; |
| 137 | is->screen.resource_from_handle = svga_resource_from_handle; |
| 138 | is->screen.resource_get_handle = u_resource_get_handle_vtbl; |
| 139 | is->screen.resource_destroy = u_resource_destroy_vtbl; |
Charmaine Lee | 0c06527 | 2014-08-12 07:37:12 -0600 | [diff] [blame] | 140 | is->screen.can_create_resource = svga_can_create_resource; |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 141 | } |