José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. |
| 21 | * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR |
| 22 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 23 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 24 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | /* |
| 28 | * Authors: |
| 29 | * Keith Whitwell <keith@tungstengraphics.com> |
| 30 | * Michel Dänzer <michel@tungstengraphics.com> |
| 31 | */ |
| 32 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 35 | #include "pipe/p_context.h" |
| 36 | #include "pipe/p_defines.h" |
Michal Krol | 6df42d8 | 2009-12-03 10:52:47 +0100 | [diff] [blame] | 37 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 38 | #include "util/u_inlines.h" |
Michal Krol | 6df42d8 | 2009-12-03 10:52:47 +0100 | [diff] [blame] | 39 | #include "util/u_format.h" |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 40 | #include "util/u_math.h" |
| 41 | #include "util/u_memory.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 42 | #include "util/u_transfer.h" |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 43 | |
| 44 | #include "lp_context.h" |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 45 | #include "lp_flush.h" |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 46 | #include "lp_screen.h" |
| 47 | #include "lp_tile_image.h" |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 48 | #include "lp_texture.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 49 | #include "lp_setup.h" |
Brian Paul | 0706dae | 2010-01-20 17:44:12 -0700 | [diff] [blame] | 50 | #include "lp_tile_size.h" |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 51 | |
Keith Whitwell | 23e951d | 2010-03-04 16:23:05 +0000 | [diff] [blame] | 52 | #include "state_tracker/sw_winsys.h" |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 53 | |
| 54 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 55 | static INLINE boolean |
| 56 | resource_is_texture(const struct pipe_resource *resource) |
| 57 | { |
| 58 | const unsigned tex_binds = (PIPE_BIND_DISPLAY_TARGET | |
| 59 | PIPE_BIND_SCANOUT | |
| 60 | PIPE_BIND_SHARED | |
| 61 | PIPE_BIND_DEPTH_STENCIL | |
| 62 | PIPE_BIND_SAMPLER_VIEW); |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 63 | const struct llvmpipe_resource *lpr = llvmpipe_resource_const(resource); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 64 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 65 | return (lpr->base.bind & tex_binds) ? TRUE : FALSE; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Allocate storage for llvmpipe_texture::layout array. |
| 72 | * The number of elements is width_in_tiles * height_in_tiles. |
| 73 | */ |
| 74 | static enum lp_texture_layout * |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 75 | alloc_layout_array(unsigned num_slices, unsigned width, unsigned height) |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 76 | { |
| 77 | const unsigned tx = align(width, TILE_SIZE) / TILE_SIZE; |
| 78 | const unsigned ty = align(height, TILE_SIZE) / TILE_SIZE; |
| 79 | |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 80 | assert(num_slices * tx * ty > 0); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 81 | assert(LP_TEX_LAYOUT_NONE == 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */ |
| 82 | |
| 83 | return (enum lp_texture_layout *) |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 84 | calloc(num_slices * tx * ty, sizeof(enum lp_texture_layout)); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | |
Brian Paul | 4414a1a | 2010-01-14 14:19:16 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Conventional allocation path for non-display textures: |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 91 | * Just compute row strides here. Storage is allocated on demand later. |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 92 | */ |
| 93 | static boolean |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 94 | llvmpipe_texture_layout(struct llvmpipe_screen *screen, |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 95 | struct llvmpipe_resource *lpr) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 96 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 97 | struct pipe_resource *pt = &lpr->base; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 98 | unsigned level; |
Roland Scheidegger | d509f84 | 2009-11-26 22:49:58 +0100 | [diff] [blame] | 99 | unsigned width = pt->width0; |
| 100 | unsigned height = pt->height0; |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 101 | unsigned depth = pt->depth0; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 102 | |
| 103 | assert(LP_MAX_TEXTURE_2D_LEVELS <= LP_MAX_TEXTURE_LEVELS); |
| 104 | assert(LP_MAX_TEXTURE_3D_LEVELS <= LP_MAX_TEXTURE_LEVELS); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 105 | |
| 106 | for (level = 0; level <= pt->last_level; level++) { |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 107 | const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE; |
| 108 | const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE; |
| 109 | unsigned nblocksx, num_slices; |
| 110 | |
| 111 | if (lpr->base.target == PIPE_TEXTURE_CUBE) |
| 112 | num_slices = 6; |
| 113 | else if (lpr->base.target == PIPE_TEXTURE_3D) |
| 114 | num_slices = depth; |
| 115 | else |
| 116 | num_slices = 1; |
José Fonseca | 838da1d | 2009-10-18 14:31:58 +0100 | [diff] [blame] | 117 | |
José Fonseca | 838da1d | 2009-10-18 14:31:58 +0100 | [diff] [blame] | 118 | /* Allocate storage for whole quads. This is particularly important |
Brian Paul | d8d80a8 | 2010-01-19 11:58:43 -0700 | [diff] [blame] | 119 | * for depth surfaces, which are currently stored in a swizzled format. |
| 120 | */ |
Brian Paul | 0706dae | 2010-01-20 17:44:12 -0700 | [diff] [blame] | 121 | nblocksx = util_format_get_nblocksx(pt->format, align(width, TILE_SIZE)); |
José Fonseca | 838da1d | 2009-10-18 14:31:58 +0100 | [diff] [blame] | 122 | |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 123 | lpr->row_stride[level] = |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 124 | align(nblocksx * util_format_get_blocksize(pt->format), 16); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 125 | |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 126 | lpr->img_stride[level] = lpr->row_stride[level] * align(height, TILE_SIZE); |
| 127 | |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 128 | lpr->tiles_per_row[level] = width_t; |
| 129 | lpr->tiles_per_image[level] = width_t * height_t; |
| 130 | lpr->num_slices_faces[level] = num_slices; |
| 131 | lpr->layout[level] = alloc_layout_array(num_slices, width, height); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 132 | |
Brian Paul | 4414a1a | 2010-01-14 14:19:16 -0700 | [diff] [blame] | 133 | width = u_minify(width, 1); |
Roland Scheidegger | d509f84 | 2009-11-26 22:49:58 +0100 | [diff] [blame] | 134 | height = u_minify(height, 1); |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 135 | depth = u_minify(depth, 1); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 138 | return TRUE; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Brian Paul | 4414a1a | 2010-01-14 14:19:16 -0700 | [diff] [blame] | 141 | |
| 142 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 143 | static boolean |
José Fonseca | e173a9b | 2009-08-29 20:02:25 +0100 | [diff] [blame] | 144 | llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen, |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 145 | struct llvmpipe_resource *lpr) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 146 | { |
Keith Whitwell | 94ce4eb | 2010-03-04 16:09:33 +0000 | [diff] [blame] | 147 | struct sw_winsys *winsys = screen->winsys; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 148 | |
Brian Paul | 0706dae | 2010-01-20 17:44:12 -0700 | [diff] [blame] | 149 | /* Round up the surface size to a multiple of the tile size to |
| 150 | * avoid tile clipping. |
| 151 | */ |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 152 | const unsigned width = align(lpr->base.width0, TILE_SIZE); |
| 153 | const unsigned height = align(lpr->base.height0, TILE_SIZE); |
| 154 | const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE; |
| 155 | const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE; |
Brian Paul | 0706dae | 2010-01-20 17:44:12 -0700 | [diff] [blame] | 156 | |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 157 | lpr->tiles_per_row[0] = width_t; |
| 158 | lpr->tiles_per_image[0] = width_t * height_t; |
| 159 | lpr->num_slices_faces[0] = 1; |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 160 | lpr->img_stride[0] = 0; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 161 | |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 162 | lpr->layout[0] = alloc_layout_array(1, width, height); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 163 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 164 | lpr->dt = winsys->displaytarget_create(winsys, |
| 165 | lpr->base.bind, |
| 166 | lpr->base.format, |
Brian Paul | 0706dae | 2010-01-20 17:44:12 -0700 | [diff] [blame] | 167 | width, height, |
José Fonseca | e173a9b | 2009-08-29 20:02:25 +0100 | [diff] [blame] | 168 | 16, |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 169 | &lpr->row_stride[0] ); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 170 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 171 | return lpr->dt != NULL; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 175 | static struct pipe_resource * |
| 176 | llvmpipe_resource_create(struct pipe_screen *_screen, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 177 | const struct pipe_resource *templat) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 178 | { |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 179 | static unsigned id_counter = 0; |
José Fonseca | e173a9b | 2009-08-29 20:02:25 +0100 | [diff] [blame] | 180 | struct llvmpipe_screen *screen = llvmpipe_screen(_screen); |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 181 | struct llvmpipe_resource *lpr = CALLOC_STRUCT(llvmpipe_resource); |
| 182 | if (!lpr) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 183 | return NULL; |
| 184 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 185 | lpr->base = *templat; |
| 186 | pipe_reference_init(&lpr->base.reference, 1); |
| 187 | lpr->base.screen = &screen->base; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 188 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 189 | assert(lpr->base.bind); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 190 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 191 | if (lpr->base.bind & (PIPE_BIND_DISPLAY_TARGET | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 192 | PIPE_BIND_SCANOUT | |
| 193 | PIPE_BIND_SHARED)) { |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 194 | /* displayable surface */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 195 | if (!llvmpipe_displaytarget_layout(screen, lpr)) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 196 | goto fail; |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 197 | assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 198 | } |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 199 | else if (lpr->base.bind & (PIPE_BIND_SAMPLER_VIEW | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 200 | PIPE_BIND_DEPTH_STENCIL)) { |
| 201 | /* texture map */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 202 | if (!llvmpipe_texture_layout(screen, lpr)) |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 203 | goto fail; |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 204 | assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 205 | } |
| 206 | else { |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 207 | /* other data (vertex buffer, const buffer, etc) */ |
| 208 | const enum pipe_format format = templat->format; |
| 209 | const uint w = templat->width0 / util_format_get_blockheight(format); |
| 210 | const uint h = templat->height0 / util_format_get_blockwidth(format); |
| 211 | const uint d = templat->depth0; |
| 212 | const uint bpp = util_format_get_blocksize(format); |
| 213 | const uint bytes = w * h * d * bpp; |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 214 | lpr->data = align_malloc(bytes, 16); |
| 215 | if (!lpr->data) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 216 | goto fail; |
| 217 | } |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 218 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 219 | if (resource_is_texture(&lpr->base)) { |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 220 | assert(lpr->layout[0]); |
Brian Paul | 7d5da23 | 2010-04-15 17:41:39 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 223 | lpr->id = id_counter++; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 224 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 225 | return &lpr->base; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 226 | |
| 227 | fail: |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 228 | FREE(lpr); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 233 | static void |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 234 | llvmpipe_resource_destroy(struct pipe_screen *pscreen, |
| 235 | struct pipe_resource *pt) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 236 | { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 237 | struct llvmpipe_screen *screen = llvmpipe_screen(pscreen); |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 238 | struct llvmpipe_resource *lpr = llvmpipe_resource(pt); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 239 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 240 | if (lpr->dt) { |
Brian Paul | 4414a1a | 2010-01-14 14:19:16 -0700 | [diff] [blame] | 241 | /* display target */ |
Keith Whitwell | 94ce4eb | 2010-03-04 16:09:33 +0000 | [diff] [blame] | 242 | struct sw_winsys *winsys = screen->winsys; |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 243 | winsys->displaytarget_destroy(winsys, lpr->dt); |
José Fonseca | e173a9b | 2009-08-29 20:02:25 +0100 | [diff] [blame] | 244 | } |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 245 | else if (resource_is_texture(pt)) { |
Brian Paul | 4414a1a | 2010-01-14 14:19:16 -0700 | [diff] [blame] | 246 | /* regular texture */ |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 247 | uint level; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 248 | |
| 249 | /* free linear image data */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 250 | for (level = 0; level < Elements(lpr->linear); level++) { |
| 251 | if (lpr->linear[level].data) { |
| 252 | align_free(lpr->linear[level].data); |
| 253 | lpr->linear[level].data = NULL; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /* free tiled image data */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 258 | for (level = 0; level < Elements(lpr->tiled); level++) { |
| 259 | if (lpr->tiled[level].data) { |
| 260 | align_free(lpr->tiled[level].data); |
| 261 | lpr->tiled[level].data = NULL; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | /* free layout flag arrays */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 266 | for (level = 0; level < Elements(lpr->tiled); level++) { |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 267 | free(lpr->layout[level]); |
| 268 | lpr->layout[level] = NULL; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 269 | } |
| 270 | } |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 271 | else if (!lpr->userBuffer) { |
| 272 | assert(lpr->data); |
| 273 | align_free(lpr->data); |
Brian Paul | 4414a1a | 2010-01-14 14:19:16 -0700 | [diff] [blame] | 274 | } |
José Fonseca | e173a9b | 2009-08-29 20:02:25 +0100 | [diff] [blame] | 275 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 276 | FREE(lpr); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 280 | /** |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 281 | * Map a resource for read/write. |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 282 | */ |
| 283 | void * |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 284 | llvmpipe_resource_map(struct pipe_resource *resource, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 285 | unsigned face, |
| 286 | unsigned level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 287 | unsigned zslice, |
| 288 | enum lp_texture_usage tex_usage, |
| 289 | enum lp_texture_layout layout) |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 290 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 291 | struct llvmpipe_resource *lpr = llvmpipe_resource(resource); |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 292 | uint8_t *map; |
| 293 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 294 | assert(face < 6); |
| 295 | assert(level < LP_MAX_TEXTURE_LEVELS); |
| 296 | |
| 297 | assert(tex_usage == LP_TEX_USAGE_READ || |
| 298 | tex_usage == LP_TEX_USAGE_READ_WRITE || |
| 299 | tex_usage == LP_TEX_USAGE_WRITE_ALL); |
| 300 | |
| 301 | assert(layout == LP_TEX_LAYOUT_NONE || |
| 302 | layout == LP_TEX_LAYOUT_TILED || |
| 303 | layout == LP_TEX_LAYOUT_LINEAR); |
| 304 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 305 | if (lpr->dt) { |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 306 | /* display target */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 307 | struct llvmpipe_screen *screen = llvmpipe_screen(resource->screen); |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 308 | struct sw_winsys *winsys = screen->winsys; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 309 | unsigned dt_usage; |
| 310 | |
| 311 | if (tex_usage == LP_TEX_USAGE_READ) { |
| 312 | dt_usage = PIPE_TRANSFER_READ; |
| 313 | } |
| 314 | else { |
| 315 | dt_usage = PIPE_TRANSFER_READ_WRITE; |
| 316 | } |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 317 | |
| 318 | assert(face == 0); |
| 319 | assert(level == 0); |
| 320 | assert(zslice == 0); |
| 321 | |
| 322 | /* FIXME: keep map count? */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 323 | map = winsys->displaytarget_map(winsys, lpr->dt, dt_usage); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 324 | |
| 325 | /* install this linear image in texture data structure */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 326 | lpr->linear[level].data = map; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 327 | |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 328 | map = llvmpipe_get_texture_image(lpr, face + zslice, level, |
| 329 | tex_usage, layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 330 | assert(map); |
| 331 | |
| 332 | return map; |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 333 | } |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 334 | else if (resource_is_texture(resource)) { |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 335 | /* regular texture */ |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 336 | if (resource->target != PIPE_TEXTURE_CUBE) { |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 337 | assert(face == 0); |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 338 | } |
| 339 | if (resource->target != PIPE_TEXTURE_3D) { |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 340 | assert(zslice == 0); |
| 341 | } |
| 342 | |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 343 | map = llvmpipe_get_texture_image(lpr, face + zslice, level, |
| 344 | tex_usage, layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 345 | assert(map); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 346 | return map; |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 347 | } |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 348 | else { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 349 | return lpr->data; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 350 | } |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | /** |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 355 | * Unmap a resource. |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 356 | */ |
| 357 | void |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 358 | llvmpipe_resource_unmap(struct pipe_resource *resource, |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 359 | unsigned face, |
| 360 | unsigned level, |
| 361 | unsigned zslice) |
| 362 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 363 | struct llvmpipe_resource *lpr = llvmpipe_resource(resource); |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 364 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 365 | if (lpr->dt) { |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 366 | /* display target */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 367 | struct llvmpipe_screen *lp_screen = llvmpipe_screen(resource->screen); |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 368 | struct sw_winsys *winsys = lp_screen->winsys; |
| 369 | |
| 370 | assert(face == 0); |
| 371 | assert(level == 0); |
| 372 | assert(zslice == 0); |
| 373 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 374 | /* make sure linear image is up to date */ |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 375 | (void) llvmpipe_get_texture_image(lpr, face + zslice, level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 376 | LP_TEX_USAGE_READ, |
| 377 | LP_TEX_LAYOUT_LINEAR); |
| 378 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 379 | winsys->displaytarget_unmap(winsys, lpr->dt); |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 384 | void * |
| 385 | llvmpipe_resource_data(struct pipe_resource *resource) |
| 386 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 387 | struct llvmpipe_resource *lpr = llvmpipe_resource(resource); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 388 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 389 | assert((lpr->base.bind & (PIPE_BIND_DISPLAY_TARGET | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 390 | PIPE_BIND_SCANOUT | |
| 391 | PIPE_BIND_SHARED | |
| 392 | PIPE_BIND_SAMPLER_VIEW)) == 0); |
| 393 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 394 | return lpr->data; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 398 | static struct pipe_resource * |
| 399 | llvmpipe_resource_from_handle(struct pipe_screen *screen, |
| 400 | const struct pipe_resource *template, |
| 401 | struct winsys_handle *whandle) |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 402 | { |
| 403 | struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys; |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 404 | struct llvmpipe_resource *lpr = CALLOC_STRUCT(llvmpipe_resource); |
| 405 | if (!lpr) |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 406 | return NULL; |
| 407 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 408 | lpr->base = *template; |
| 409 | pipe_reference_init(&lpr->base.reference, 1); |
| 410 | lpr->base.screen = screen; |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 411 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 412 | lpr->dt = winsys->displaytarget_from_handle(winsys, |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 413 | template, |
| 414 | whandle, |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 415 | &lpr->row_stride[0]); |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 416 | if (!lpr->dt) |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 417 | goto fail; |
| 418 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 419 | return &lpr->base; |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 420 | |
| 421 | fail: |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 422 | FREE(lpr); |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 423 | return NULL; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | static boolean |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 428 | llvmpipe_resource_get_handle(struct pipe_screen *screen, |
| 429 | struct pipe_resource *pt, |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 430 | struct winsys_handle *whandle) |
| 431 | { |
| 432 | struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys; |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 433 | struct llvmpipe_resource *lpr = llvmpipe_resource(pt); |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 434 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 435 | assert(lpr->dt); |
| 436 | if (!lpr->dt) |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 437 | return FALSE; |
| 438 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 439 | return winsys->displaytarget_get_handle(winsys, lpr->dt, whandle); |
Jakob Bornecrantz | 8b63f9b | 2010-03-11 03:33:03 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 443 | static struct pipe_surface * |
| 444 | llvmpipe_get_tex_surface(struct pipe_screen *screen, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 445 | struct pipe_resource *pt, |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 446 | unsigned face, unsigned level, unsigned zslice, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 447 | enum lp_texture_usage usage) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 448 | { |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 449 | struct pipe_surface *ps; |
| 450 | |
| 451 | assert(level <= pt->last_level); |
| 452 | |
| 453 | ps = CALLOC_STRUCT(pipe_surface); |
| 454 | if (ps) { |
| 455 | pipe_reference_init(&ps->reference, 1); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 456 | pipe_resource_reference(&ps->texture, pt); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 457 | ps->format = pt->format; |
Roland Scheidegger | d509f84 | 2009-11-26 22:49:58 +0100 | [diff] [blame] | 458 | ps->width = u_minify(pt->width0, level); |
| 459 | ps->height = u_minify(pt->height0, level); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 460 | ps->usage = usage; |
| 461 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 462 | ps->face = face; |
| 463 | ps->level = level; |
| 464 | ps->zslice = zslice; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 465 | } |
| 466 | return ps; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | static void |
| 471 | llvmpipe_tex_surface_destroy(struct pipe_surface *surf) |
| 472 | { |
| 473 | /* Effectively do the texture_update work here - if texture images |
| 474 | * needed post-processing to put them into hardware layout, this is |
| 475 | * where it would happen. For llvmpipe, nothing to do. |
| 476 | */ |
| 477 | assert(surf->texture); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 478 | pipe_resource_reference(&surf->texture, NULL); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 479 | FREE(surf); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | static struct pipe_transfer * |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 484 | llvmpipe_get_transfer(struct pipe_context *pipe, |
| 485 | struct pipe_resource *resource, |
| 486 | struct pipe_subresource sr, |
| 487 | unsigned usage, |
| 488 | const struct pipe_box *box) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 489 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 490 | struct llvmpipe_resource *lprex = llvmpipe_resource(resource); |
| 491 | struct llvmpipe_transfer *lpr; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 492 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 493 | assert(resource); |
| 494 | assert(sr.level <= resource->last_level); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 495 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 496 | lpr = CALLOC_STRUCT(llvmpipe_transfer); |
| 497 | if (lpr) { |
| 498 | struct pipe_transfer *pt = &lpr->base; |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 499 | pipe_resource_reference(&pt->resource, resource); |
| 500 | pt->box = *box; |
Jakob Bornecrantz | c81f049 | 2010-04-12 01:47:28 +0200 | [diff] [blame] | 501 | pt->sr = sr; |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 502 | pt->stride = lprex->row_stride[sr.level]; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 503 | pt->usage = usage; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 504 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 505 | return pt; |
| 506 | } |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | static void |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 512 | llvmpipe_transfer_destroy(struct pipe_context *pipe, |
Keith Whitwell | d35ecca | 2010-03-11 16:10:25 +0000 | [diff] [blame] | 513 | struct pipe_transfer *transfer) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 514 | { |
| 515 | /* Effectively do the texture_update work here - if texture images |
| 516 | * needed post-processing to put them into hardware layout, this is |
| 517 | * where it would happen. For llvmpipe, nothing to do. |
| 518 | */ |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 519 | assert (transfer->resource); |
| 520 | pipe_resource_reference(&transfer->resource, NULL); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 521 | FREE(transfer); |
| 522 | } |
| 523 | |
| 524 | |
| 525 | static void * |
Keith Whitwell | b43c182 | 2010-03-11 15:23:16 +0000 | [diff] [blame] | 526 | llvmpipe_transfer_map( struct pipe_context *pipe, |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 527 | struct pipe_transfer *transfer ) |
| 528 | { |
Keith Whitwell | b43c182 | 2010-03-11 15:23:16 +0000 | [diff] [blame] | 529 | struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen); |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 530 | ubyte *map; |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 531 | struct llvmpipe_resource *lpr; |
Roland Scheidegger | c78748a | 2009-12-02 02:08:26 +0100 | [diff] [blame] | 532 | enum pipe_format format; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 533 | enum lp_texture_usage tex_usage; |
| 534 | const char *mode; |
| 535 | |
| 536 | assert(transfer->sr.face < 6); |
| 537 | assert(transfer->sr.level < LP_MAX_TEXTURE_LEVELS); |
| 538 | |
| 539 | /* |
| 540 | printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n", |
| 541 | transfer->x, transfer->y, transfer->width, transfer->height, |
| 542 | transfer->texture->width0, |
| 543 | transfer->texture->height0, |
| 544 | transfer->usage); |
| 545 | */ |
| 546 | |
| 547 | if (transfer->usage == PIPE_TRANSFER_READ) { |
| 548 | tex_usage = LP_TEX_USAGE_READ; |
| 549 | mode = "read"; |
| 550 | } |
| 551 | else { |
| 552 | tex_usage = LP_TEX_USAGE_READ_WRITE; |
| 553 | mode = "read/write"; |
| 554 | } |
| 555 | |
| 556 | if (0) { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 557 | struct llvmpipe_resource *lpr = llvmpipe_resource(transfer->resource); |
| 558 | printf("transfer map tex %u mode %s\n", lpr->id, mode); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 559 | } |
| 560 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 561 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 562 | assert(transfer->resource); |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 563 | lpr = llvmpipe_resource(transfer->resource); |
| 564 | format = lpr->base.format; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 565 | |
José Fonseca | bf40c34 | 2010-03-13 16:13:26 +0000 | [diff] [blame] | 566 | /* |
| 567 | * Transfers, like other pipe operations, must happen in order, so flush the |
| 568 | * context if necessary. |
| 569 | */ |
| 570 | llvmpipe_flush_texture(pipe, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 571 | transfer->resource, |
| 572 | transfer->sr.face, |
| 573 | transfer->sr.level, |
José Fonseca | bf40c34 | 2010-03-13 16:13:26 +0000 | [diff] [blame] | 574 | 0, /* flush_flags */ |
| 575 | !(transfer->usage & PIPE_TRANSFER_WRITE), /* read_only */ |
| 576 | TRUE, /* cpu_access */ |
| 577 | FALSE); /* do_not_flush */ |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 578 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 579 | map = llvmpipe_resource_map(transfer->resource, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 580 | transfer->sr.face, |
| 581 | transfer->sr.level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 582 | transfer->box.z, |
| 583 | tex_usage, LP_TEX_LAYOUT_LINEAR); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 584 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 585 | |
| 586 | /* May want to do different things here depending on read/write nature |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 587 | * of the map: |
| 588 | */ |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 589 | if (transfer->usage & PIPE_TRANSFER_WRITE) { |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 590 | /* Do something to notify sharing contexts of a texture change. |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 591 | */ |
José Fonseca | e173a9b | 2009-08-29 20:02:25 +0100 | [diff] [blame] | 592 | screen->timestamp++; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 593 | } |
| 594 | |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 595 | map += |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 596 | transfer->box.y / util_format_get_blockheight(format) * transfer->stride + |
| 597 | transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format); |
José Fonseca | 3abc7b9 | 2010-03-13 16:04:06 +0000 | [diff] [blame] | 598 | |
| 599 | return map; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | |
| 603 | static void |
Keith Whitwell | b43c182 | 2010-03-11 15:23:16 +0000 | [diff] [blame] | 604 | llvmpipe_transfer_unmap(struct pipe_context *pipe, |
| 605 | struct pipe_transfer *transfer) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 606 | { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 607 | assert(transfer->resource); |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 608 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 609 | llvmpipe_resource_unmap(transfer->resource, |
| 610 | transfer->sr.face, |
| 611 | transfer->sr.level, |
| 612 | transfer->box.z); |
| 613 | } |
| 614 | |
| 615 | static unsigned int |
| 616 | llvmpipe_is_resource_referenced( struct pipe_context *pipe, |
| 617 | struct pipe_resource *presource, |
| 618 | unsigned face, unsigned level) |
| 619 | { |
| 620 | struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe ); |
| 621 | |
| 622 | if (presource->target == PIPE_BUFFER) |
| 623 | return PIPE_UNREFERENCED; |
| 624 | |
| 625 | return lp_setup_is_resource_referenced(llvmpipe->setup, presource); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | |
| 630 | /** |
| 631 | * Create buffer which wraps user-space data. |
| 632 | */ |
| 633 | static struct pipe_resource * |
| 634 | llvmpipe_user_buffer_create(struct pipe_screen *screen, |
| 635 | void *ptr, |
| 636 | unsigned bytes, |
| 637 | unsigned bind_flags) |
| 638 | { |
| 639 | struct llvmpipe_resource *buffer; |
| 640 | |
| 641 | buffer = CALLOC_STRUCT(llvmpipe_resource); |
| 642 | if(!buffer) |
| 643 | return NULL; |
| 644 | |
| 645 | pipe_reference_init(&buffer->base.reference, 1); |
| 646 | buffer->base.screen = screen; |
| 647 | buffer->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */ |
| 648 | buffer->base.bind = bind_flags; |
| 649 | buffer->base._usage = PIPE_USAGE_IMMUTABLE; |
| 650 | buffer->base.flags = 0; |
| 651 | buffer->base.width0 = bytes; |
| 652 | buffer->base.height0 = 1; |
| 653 | buffer->base.depth0 = 1; |
| 654 | buffer->userBuffer = TRUE; |
| 655 | buffer->data = ptr; |
| 656 | |
| 657 | return &buffer->base; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 661 | /** |
| 662 | * Compute size (in bytes) need to store a texture image / mipmap level, |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 663 | * for just one cube face or one 3D texture slice |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 664 | */ |
| 665 | static unsigned |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 666 | tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 667 | enum lp_texture_layout layout) |
| 668 | { |
Brian Paul | 05bf77a | 2010-04-19 11:17:11 -0600 | [diff] [blame] | 669 | const unsigned width = u_minify(lpr->base.width0, level); |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 670 | const unsigned height = u_minify(lpr->base.height0, level); |
Brian Paul | 05bf77a | 2010-04-19 11:17:11 -0600 | [diff] [blame] | 671 | |
| 672 | assert(layout == LP_TEX_LAYOUT_TILED || |
| 673 | layout == LP_TEX_LAYOUT_LINEAR); |
| 674 | |
| 675 | if (layout == LP_TEX_LAYOUT_TILED) { |
| 676 | /* for tiled layout, force a 32bpp format */ |
| 677 | const enum pipe_format format = PIPE_FORMAT_B8G8R8A8_UNORM; |
| 678 | const unsigned block_size = util_format_get_blocksize(format); |
| 679 | const unsigned nblocksy = |
| 680 | util_format_get_nblocksy(format, align(height, TILE_SIZE)); |
| 681 | const unsigned nblocksx = |
| 682 | util_format_get_nblocksx(format, align(width, TILE_SIZE)); |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 683 | const unsigned buffer_size = block_size * nblocksy * nblocksx; |
Brian Paul | 05bf77a | 2010-04-19 11:17:11 -0600 | [diff] [blame] | 684 | return buffer_size; |
| 685 | } |
| 686 | else { |
| 687 | const enum pipe_format format = lpr->base.format; |
| 688 | const unsigned nblocksy = |
| 689 | util_format_get_nblocksy(format, align(height, TILE_SIZE)); |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 690 | const unsigned buffer_size = nblocksy * lpr->row_stride[level]; |
Brian Paul | 05bf77a | 2010-04-19 11:17:11 -0600 | [diff] [blame] | 691 | return buffer_size; |
| 692 | } |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
| 696 | /** |
| 697 | * Compute size (in bytes) need to store a texture image / mipmap level, |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 698 | * including all cube faces or 3D image slices |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 699 | */ |
| 700 | static unsigned |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 701 | tex_image_size(const struct llvmpipe_resource *lpr, unsigned level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 702 | enum lp_texture_layout layout) |
| 703 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 704 | const unsigned buf_size = tex_image_face_size(lpr, level, layout); |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 705 | return buf_size * lpr->num_slices_faces[level]; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | |
| 709 | /** |
| 710 | * This function encapsulates some complicated logic for determining |
| 711 | * how to convert a tile of image data from linear layout to tiled |
| 712 | * layout, or vice versa. |
| 713 | * \param cur_layout the current tile layout |
| 714 | * \param target_layout the desired tile layout |
| 715 | * \param usage how the tile will be accessed (R/W vs. read-only, etc) |
| 716 | * \param new_layout_return returns the new layout mode |
| 717 | * \param convert_return returns TRUE if image conversion is needed |
| 718 | */ |
| 719 | static void |
| 720 | layout_logic(enum lp_texture_layout cur_layout, |
| 721 | enum lp_texture_layout target_layout, |
| 722 | enum lp_texture_usage usage, |
| 723 | enum lp_texture_layout *new_layout_return, |
| 724 | boolean *convert) |
| 725 | { |
| 726 | enum lp_texture_layout other_layout, new_layout; |
| 727 | |
| 728 | *convert = FALSE; |
| 729 | |
| 730 | new_layout = 99; /* debug check */ |
| 731 | |
| 732 | if (target_layout == LP_TEX_LAYOUT_LINEAR) { |
| 733 | other_layout = LP_TEX_LAYOUT_TILED; |
| 734 | } |
| 735 | else { |
| 736 | assert(target_layout == LP_TEX_LAYOUT_TILED); |
| 737 | other_layout = LP_TEX_LAYOUT_LINEAR; |
| 738 | } |
| 739 | |
| 740 | new_layout = target_layout; /* may get changed below */ |
| 741 | |
| 742 | if (cur_layout == LP_TEX_LAYOUT_BOTH) { |
| 743 | if (usage == LP_TEX_USAGE_READ) { |
| 744 | new_layout = LP_TEX_LAYOUT_BOTH; |
| 745 | } |
| 746 | } |
| 747 | else if (cur_layout == other_layout) { |
| 748 | if (usage != LP_TEX_USAGE_WRITE_ALL) { |
| 749 | /* need to convert tiled data to linear or vice versa */ |
| 750 | *convert = TRUE; |
| 751 | |
| 752 | if (usage == LP_TEX_USAGE_READ) |
| 753 | new_layout = LP_TEX_LAYOUT_BOTH; |
| 754 | } |
| 755 | } |
| 756 | else { |
| 757 | assert(cur_layout == LP_TEX_LAYOUT_NONE || |
| 758 | cur_layout == target_layout); |
| 759 | } |
| 760 | |
| 761 | assert(new_layout == LP_TEX_LAYOUT_BOTH || |
| 762 | new_layout == target_layout); |
| 763 | |
| 764 | *new_layout_return = new_layout; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /** |
| 769 | * Return pointer to a texture image. No tiled/linear conversion is done. |
| 770 | */ |
| 771 | void * |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 772 | llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 773 | unsigned face, unsigned level, |
| 774 | enum lp_texture_layout layout) |
| 775 | { |
| 776 | struct llvmpipe_texture_image *img; |
| 777 | unsigned face_offset; |
| 778 | |
| 779 | if (layout == LP_TEX_LAYOUT_LINEAR) { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 780 | img = &lpr->linear[level]; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 781 | } |
| 782 | else { |
| 783 | assert (layout == LP_TEX_LAYOUT_TILED); |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 784 | img = &lpr->tiled[level]; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | if (face > 0) |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 788 | face_offset = face * tex_image_face_size(lpr, level, layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 789 | else |
| 790 | face_offset = 0; |
| 791 | |
| 792 | return (ubyte *) img->data + face_offset; |
| 793 | } |
| 794 | |
| 795 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 796 | static INLINE enum lp_texture_layout |
| 797 | llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource *lpr, |
| 798 | unsigned face_slice, unsigned level, |
| 799 | unsigned x, unsigned y) |
| 800 | { |
| 801 | uint i; |
| 802 | assert(resource_is_texture(&lpr->base)); |
| 803 | assert(x < lpr->tiles_per_row[level]); |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 804 | i = face_slice * lpr->tiles_per_image[level] |
| 805 | + y * lpr->tiles_per_row[level] + x; |
| 806 | return lpr->layout[level][i]; |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | static INLINE void |
| 811 | llvmpipe_set_texture_tile_layout(struct llvmpipe_resource *lpr, |
| 812 | unsigned face_slice, unsigned level, |
| 813 | unsigned x, unsigned y, |
| 814 | enum lp_texture_layout layout) |
| 815 | { |
| 816 | uint i; |
| 817 | assert(resource_is_texture(&lpr->base)); |
| 818 | assert(x < lpr->tiles_per_row[level]); |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 819 | i = face_slice * lpr->tiles_per_image[level] |
| 820 | + y * lpr->tiles_per_row[level] + x; |
| 821 | lpr->layout[level][i] = layout; |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 822 | } |
| 823 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 824 | |
| 825 | /** |
Brian Paul | 2cad624 | 2010-04-19 16:49:37 -0600 | [diff] [blame] | 826 | * Set the layout mode for all tiles in a particular image. |
| 827 | */ |
| 828 | static INLINE void |
| 829 | llvmpipe_set_texture_image_layout(struct llvmpipe_resource *lpr, |
| 830 | unsigned face_slice, unsigned level, |
| 831 | unsigned width_t, unsigned height_t, |
| 832 | enum lp_texture_layout layout) |
| 833 | { |
| 834 | const unsigned start = face_slice * lpr->tiles_per_image[level]; |
| 835 | unsigned i; |
| 836 | |
| 837 | for (i = 0; i < width_t * height_t; i++) { |
| 838 | lpr->layout[level][start + i] = layout; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | |
| 843 | /** |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 844 | * Return pointer to texture image data (either linear or tiled layout) |
| 845 | * for a particular cube face or 3D texture slice. |
| 846 | * |
| 847 | * \param face_slice the cube face or 3D slice of interest |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 848 | * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE |
Brian Paul | 05bf77a | 2010-04-19 11:17:11 -0600 | [diff] [blame] | 849 | * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 850 | */ |
| 851 | void * |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 852 | llvmpipe_get_texture_image(struct llvmpipe_resource *lpr, |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 853 | unsigned face_slice, unsigned level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 854 | enum lp_texture_usage usage, |
| 855 | enum lp_texture_layout layout) |
| 856 | { |
| 857 | /* |
| 858 | * 'target' refers to the image which we're retrieving (either in |
| 859 | * tiled or linear layout). |
| 860 | * 'other' refers to the same image but in the other layout. (it may |
| 861 | * or may not exist. |
| 862 | */ |
| 863 | struct llvmpipe_texture_image *target_img; |
| 864 | struct llvmpipe_texture_image *other_img; |
| 865 | void *target_data; |
| 866 | void *other_data; |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 867 | const unsigned width = u_minify(lpr->base.width0, level); |
| 868 | const unsigned height = u_minify(lpr->base.height0, level); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 869 | const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE; |
| 870 | const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE; |
| 871 | enum lp_texture_layout other_layout; |
Brian Paul | 254dd0c | 2010-04-19 14:07:23 -0600 | [diff] [blame] | 872 | boolean only_allocate; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 873 | |
| 874 | assert(layout == LP_TEX_LAYOUT_NONE || |
| 875 | layout == LP_TEX_LAYOUT_TILED || |
| 876 | layout == LP_TEX_LAYOUT_LINEAR); |
| 877 | |
| 878 | assert(usage == LP_TEX_USAGE_READ || |
| 879 | usage == LP_TEX_USAGE_READ_WRITE || |
| 880 | usage == LP_TEX_USAGE_WRITE_ALL); |
| 881 | |
Brian Paul | 254dd0c | 2010-04-19 14:07:23 -0600 | [diff] [blame] | 882 | /* check for the special case of layout == LP_TEX_LAYOUT_NONE */ |
| 883 | if (layout == LP_TEX_LAYOUT_NONE) { |
| 884 | only_allocate = TRUE; |
| 885 | layout = LP_TEX_LAYOUT_TILED; |
| 886 | } |
| 887 | else { |
| 888 | only_allocate = FALSE; |
| 889 | } |
| 890 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 891 | if (lpr->dt) { |
| 892 | assert(lpr->linear[level].data); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* which is target? which is other? */ |
| 896 | if (layout == LP_TEX_LAYOUT_LINEAR) { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 897 | target_img = &lpr->linear[level]; |
| 898 | other_img = &lpr->tiled[level]; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 899 | other_layout = LP_TEX_LAYOUT_TILED; |
| 900 | } |
| 901 | else { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 902 | target_img = &lpr->tiled[level]; |
| 903 | other_img = &lpr->linear[level]; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 904 | other_layout = LP_TEX_LAYOUT_LINEAR; |
| 905 | } |
| 906 | |
| 907 | target_data = target_img->data; |
| 908 | other_data = other_img->data; |
| 909 | |
| 910 | if (!target_data) { |
| 911 | /* allocate memory for the target image now */ |
Brian Paul | 254dd0c | 2010-04-19 14:07:23 -0600 | [diff] [blame] | 912 | unsigned buffer_size = tex_image_size(lpr, level, layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 913 | target_img->data = align_malloc(buffer_size, 16); |
| 914 | target_data = target_img->data; |
| 915 | } |
| 916 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 917 | if (face_slice > 0) { |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 918 | unsigned target_offset, other_offset; |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 919 | |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 920 | target_offset = face_slice * tex_image_face_size(lpr, level, layout); |
| 921 | other_offset = face_slice * tex_image_face_size(lpr, level, other_layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 922 | if (target_data) { |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 923 | target_data = (uint8_t *) target_data + target_offset; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 924 | } |
| 925 | if (other_data) { |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 926 | other_data = (uint8_t *) other_data + other_offset; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
Brian Paul | 254dd0c | 2010-04-19 14:07:23 -0600 | [diff] [blame] | 930 | if (only_allocate) { |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 931 | /* Just allocating tiled memory. Don't initialize it from the |
Brian Paul | 05bf77a | 2010-04-19 11:17:11 -0600 | [diff] [blame] | 932 | * linear data if it exists. |
| 933 | */ |
Brian Paul | 2cad624 | 2010-04-19 16:49:37 -0600 | [diff] [blame] | 934 | llvmpipe_set_texture_image_layout(lpr, face_slice, level, |
| 935 | width_t, height_t, layout); |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 936 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 937 | return target_data; |
| 938 | } |
| 939 | |
| 940 | if (other_data) { |
| 941 | /* may need to convert other data to the requested layout */ |
| 942 | enum lp_texture_layout new_layout; |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 943 | unsigned x, y; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 944 | |
| 945 | /* loop over all image tiles, doing layout conversion where needed */ |
| 946 | for (y = 0; y < height_t; y++) { |
| 947 | for (x = 0; x < width_t; x++) { |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 948 | enum lp_texture_layout cur_layout = |
| 949 | llvmpipe_get_texture_tile_layout(lpr, face_slice, level, x, y); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 950 | boolean convert; |
| 951 | |
| 952 | layout_logic(cur_layout, layout, usage, &new_layout, &convert); |
| 953 | |
| 954 | if (convert) { |
| 955 | if (layout == LP_TEX_LAYOUT_TILED) { |
| 956 | lp_linear_to_tiled(other_data, target_data, |
| 957 | x * TILE_SIZE, y * TILE_SIZE, |
| 958 | TILE_SIZE, TILE_SIZE, |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 959 | lpr->base.format, |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 960 | lpr->row_stride[level]); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 961 | } |
| 962 | else { |
| 963 | lp_tiled_to_linear(other_data, target_data, |
| 964 | x * TILE_SIZE, y * TILE_SIZE, |
| 965 | TILE_SIZE, TILE_SIZE, |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 966 | lpr->base.format, |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 967 | lpr->row_stride[level]); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 971 | llvmpipe_set_texture_tile_layout(lpr, face_slice, level, x, y, |
| 972 | new_layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | } |
| 976 | else { |
| 977 | /* no other data */ |
Brian Paul | 2cad624 | 2010-04-19 16:49:37 -0600 | [diff] [blame] | 978 | llvmpipe_set_texture_image_layout(lpr, face_slice, level, |
| 979 | width_t, height_t, layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | assert(target_data); |
| 983 | |
| 984 | return target_data; |
| 985 | } |
| 986 | |
| 987 | |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 988 | /** |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 989 | * Return pointer to start of a texture image (1D, 2D, 3D, CUBE). |
| 990 | * All cube faces and 3D slices will be converted to the requested |
| 991 | * layout if needed. |
| 992 | * This is typically used when we're about to sample from a texture. |
| 993 | */ |
| 994 | void * |
| 995 | llvmpipe_get_texture_image_all(struct llvmpipe_resource *lpr, |
| 996 | unsigned level, |
| 997 | enum lp_texture_usage usage, |
| 998 | enum lp_texture_layout layout) |
| 999 | { |
| 1000 | const int slices = lpr->num_slices_faces[level]; |
| 1001 | int slice; |
Alan Hourihane | 17c560d | 2010-04-20 10:33:56 +0100 | [diff] [blame^] | 1002 | void *map = NULL; |
Brian Paul | 202ff7d | 2010-04-19 16:42:47 -0600 | [diff] [blame] | 1003 | |
| 1004 | assert(slices > 0); |
| 1005 | |
| 1006 | for (slice = slices - 1; slice >= 0; slice--) { |
| 1007 | map = llvmpipe_get_texture_image(lpr, slice, level, usage, layout); |
| 1008 | } |
| 1009 | |
| 1010 | return map; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | /** |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1016 | * Get pointer to a linear image where the tile at (x,y) is known to be |
| 1017 | * in linear layout. |
| 1018 | * Conversion from tiled to linear will be done if necessary. |
| 1019 | * \return pointer to start of image/face (not the tile) |
| 1020 | */ |
| 1021 | ubyte * |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1022 | llvmpipe_get_texture_tile_linear(struct llvmpipe_resource *lpr, |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1023 | unsigned face_slice, unsigned level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1024 | enum lp_texture_usage usage, |
| 1025 | unsigned x, unsigned y) |
| 1026 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1027 | struct llvmpipe_texture_image *tiled_img = &lpr->tiled[level]; |
| 1028 | struct llvmpipe_texture_image *linear_img = &lpr->linear[level]; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1029 | enum lp_texture_layout cur_layout, new_layout; |
| 1030 | const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE; |
| 1031 | boolean convert; |
| 1032 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1033 | assert(resource_is_texture(&lpr->base)); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1034 | assert(x % TILE_SIZE == 0); |
| 1035 | assert(y % TILE_SIZE == 0); |
| 1036 | |
| 1037 | if (!linear_img->data) { |
| 1038 | /* allocate memory for the tiled image now */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1039 | unsigned buffer_size = tex_image_size(lpr, level, LP_TEX_LAYOUT_LINEAR); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1040 | linear_img->data = align_malloc(buffer_size, 16); |
| 1041 | } |
| 1042 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1043 | cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1044 | |
| 1045 | layout_logic(cur_layout, LP_TEX_LAYOUT_LINEAR, usage, |
| 1046 | &new_layout, &convert); |
| 1047 | |
| 1048 | if (convert) { |
| 1049 | lp_tiled_to_linear(tiled_img->data, linear_img->data, |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1050 | x, y, TILE_SIZE, TILE_SIZE, lpr->base.format, |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 1051 | lpr->row_stride[level]); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | if (new_layout != cur_layout) |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1055 | llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1056 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1057 | if (face_slice > 0) { |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1058 | unsigned offset |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1059 | = face_slice * tex_image_face_size(lpr, level, LP_TEX_LAYOUT_LINEAR); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1060 | return (ubyte *) linear_img->data + offset; |
| 1061 | } |
| 1062 | else { |
| 1063 | return linear_img->data; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | /** |
| 1069 | * Get pointer to tiled data for rendering. |
| 1070 | * \return pointer to the tiled data at the given tile position |
| 1071 | */ |
| 1072 | ubyte * |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1073 | llvmpipe_get_texture_tile(struct llvmpipe_resource *lpr, |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1074 | unsigned face_slice, unsigned level, |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1075 | enum lp_texture_usage usage, |
| 1076 | unsigned x, unsigned y) |
| 1077 | { |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1078 | const unsigned width = u_minify(lpr->base.width0, level); |
| 1079 | struct llvmpipe_texture_image *tiled_img = &lpr->tiled[level]; |
| 1080 | struct llvmpipe_texture_image *linear_img = &lpr->linear[level]; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1081 | enum lp_texture_layout cur_layout, new_layout; |
| 1082 | const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE; |
| 1083 | boolean convert; |
| 1084 | |
| 1085 | assert(x % TILE_SIZE == 0); |
| 1086 | assert(y % TILE_SIZE == 0); |
| 1087 | |
| 1088 | if (!tiled_img->data) { |
| 1089 | /* allocate memory for the tiled image now */ |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1090 | unsigned buffer_size = tex_image_size(lpr, level, LP_TEX_LAYOUT_TILED); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1091 | tiled_img->data = align_malloc(buffer_size, 16); |
| 1092 | } |
| 1093 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1094 | cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1095 | |
| 1096 | layout_logic(cur_layout, LP_TEX_LAYOUT_TILED, usage, &new_layout, &convert); |
| 1097 | if (convert) { |
| 1098 | lp_linear_to_tiled(linear_img->data, tiled_img->data, |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1099 | x, y, TILE_SIZE, TILE_SIZE, lpr->base.format, |
Brian Paul | f4071e5 | 2010-04-19 17:05:05 -0600 | [diff] [blame] | 1100 | lpr->row_stride[level]); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | if (new_layout != cur_layout) |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1104 | llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1105 | |
| 1106 | /* compute, return address of the 64x64 tile */ |
| 1107 | { |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1108 | unsigned tiles_per_row, tile_offset, face_slice_offset; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1109 | |
| 1110 | tiles_per_row = align(width, TILE_SIZE) / TILE_SIZE; |
| 1111 | |
Brian Paul | d293c43 | 2010-04-16 10:01:32 -0600 | [diff] [blame] | 1112 | assert(tiles_per_row == lpr->tiles_per_row[level]); |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1113 | |
| 1114 | tile_offset = ty * tiles_per_row + tx; |
| 1115 | tile_offset *= TILE_SIZE * TILE_SIZE * 4; |
| 1116 | |
| 1117 | assert(tiled_img->data); |
| 1118 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1119 | face_slice_offset = (face_slice > 0) |
| 1120 | ? (face_slice * tex_image_face_size(lpr, level, LP_TEX_LAYOUT_TILED)) |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1121 | : 0; |
| 1122 | |
Brian Paul | ee7cf9d | 2010-04-19 14:43:22 -0600 | [diff] [blame] | 1123 | return (ubyte *) tiled_img->data + face_slice_offset + tile_offset; |
Brian Paul | 0639765 | 2010-04-16 09:10:54 -0600 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 1128 | void |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 1129 | llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen) |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 1130 | { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 1131 | screen->resource_create = llvmpipe_resource_create; |
| 1132 | screen->resource_destroy = llvmpipe_resource_destroy; |
| 1133 | screen->resource_from_handle = llvmpipe_resource_from_handle; |
| 1134 | screen->resource_get_handle = llvmpipe_resource_get_handle; |
| 1135 | screen->user_buffer_create = llvmpipe_user_buffer_create; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 1136 | |
| 1137 | screen->get_tex_surface = llvmpipe_get_tex_surface; |
| 1138 | screen->tex_surface_destroy = llvmpipe_tex_surface_destroy; |
Keith Whitwell | b43c182 | 2010-03-11 15:23:16 +0000 | [diff] [blame] | 1139 | } |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 1140 | |
Keith Whitwell | b43c182 | 2010-03-11 15:23:16 +0000 | [diff] [blame] | 1141 | |
| 1142 | void |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 1143 | llvmpipe_init_context_resource_funcs(struct pipe_context *pipe) |
Keith Whitwell | b43c182 | 2010-03-11 15:23:16 +0000 | [diff] [blame] | 1144 | { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 1145 | pipe->get_transfer = llvmpipe_get_transfer; |
| 1146 | pipe->transfer_destroy = llvmpipe_transfer_destroy; |
Keith Whitwell | b43c182 | 2010-03-11 15:23:16 +0000 | [diff] [blame] | 1147 | pipe->transfer_map = llvmpipe_transfer_map; |
| 1148 | pipe->transfer_unmap = llvmpipe_transfer_unmap; |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 1149 | pipe->is_resource_referenced = llvmpipe_is_resource_referenced; |
| 1150 | |
| 1151 | pipe->transfer_flush_region = u_default_transfer_flush_region; |
| 1152 | pipe->transfer_inline_write = u_default_transfer_inline_write; |
José Fonseca | 946f432 | 2009-07-26 23:44:38 +0100 | [diff] [blame] | 1153 | } |