Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 1 | /********************************************************** |
| 2 | * Copyright 2008-2009 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 | #ifndef SVGA_BUFFER_H |
| 27 | #define SVGA_BUFFER_H |
| 28 | |
| 29 | |
| 30 | #include "pipe/p_compiler.h" |
| 31 | #include "pipe/p_state.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 32 | #include "util/u_transfer.h" |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 33 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 34 | #include "svga_screen_cache.h" |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 35 | #include "svga_screen.h" |
| 36 | #include "svga_cmd.h" |
| 37 | #include "svga_context.h" |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 38 | |
| 39 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 40 | /** |
| 41 | * Maximum number of discontiguous ranges |
| 42 | */ |
| 43 | #define SVGA_BUFFER_MAX_RANGES 32 |
| 44 | |
| 45 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 46 | struct svga_context; |
| 47 | struct svga_winsys_buffer; |
| 48 | struct svga_winsys_surface; |
| 49 | |
| 50 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 51 | extern struct u_resource_vtbl svga_buffer_vtbl; |
| 52 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 53 | struct svga_buffer_range |
| 54 | { |
| 55 | unsigned start; |
| 56 | unsigned end; |
| 57 | }; |
| 58 | |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 59 | struct svga_3d_update_gb_image; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 60 | |
| 61 | /** |
Charmaine Lee | eea6223 | 2016-11-16 15:31:00 -0800 | [diff] [blame] | 62 | * This structure describes the bind flags and cache key associated |
| 63 | * with the host surface. |
| 64 | */ |
| 65 | struct svga_buffer_surface |
| 66 | { |
| 67 | struct list_head list; |
| 68 | unsigned bind_flags; |
| 69 | struct svga_host_surface_cache_key key; |
| 70 | struct svga_winsys_surface *handle; |
| 71 | }; |
| 72 | |
| 73 | /** |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 74 | * SVGA pipe buffer. |
| 75 | */ |
| 76 | struct svga_buffer |
| 77 | { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 78 | struct u_resource b; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 79 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 80 | /** This is a superset of b.b.bind */ |
| 81 | unsigned bind_flags; |
| 82 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 83 | /** |
| 84 | * Regular (non DMA'able) memory. |
| 85 | * |
| 86 | * Used for user buffers or for buffers which we know before hand that can |
| 87 | * never be used by the virtual hardware directly, such as constant buffers. |
| 88 | */ |
| 89 | void *swbuf; |
| 90 | |
| 91 | /** |
| 92 | * Whether swbuf was created by the user or not. |
| 93 | */ |
| 94 | boolean user; |
| 95 | |
| 96 | /** |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 97 | * Creation key for the host surface handle. |
| 98 | * |
| 99 | * This structure describes all the host surface characteristics so that it |
| 100 | * can be looked up in cache, since creating a host surface is often a slow |
| 101 | * operation. |
| 102 | */ |
| 103 | struct svga_host_surface_cache_key key; |
| 104 | |
| 105 | /** |
| 106 | * Host surface handle. |
| 107 | * |
| 108 | * This is a platform independent abstraction for host SID. We create when |
José Fonseca | 74651f5 | 2011-03-16 15:43:00 +0000 | [diff] [blame] | 109 | * trying to bind. |
| 110 | * |
| 111 | * Only set for non-user buffers. |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 112 | */ |
| 113 | struct svga_winsys_surface *handle; |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
Charmaine Lee | eea6223 | 2016-11-16 15:31:00 -0800 | [diff] [blame] | 116 | * List of surfaces created for this buffer resource to support |
| 117 | * incompatible bind flags. |
| 118 | */ |
| 119 | struct list_head surfaces; |
| 120 | |
| 121 | /** |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 122 | * Information about ongoing and past map operations. |
| 123 | */ |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 124 | struct { |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Number of concurrent mappings. |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 127 | */ |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 128 | unsigned count; |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 129 | |
| 130 | /** |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 131 | * Dirty ranges. |
| 132 | * |
| 133 | * Ranges that were touched by the application and need to be uploaded to |
| 134 | * the host. |
| 135 | * |
| 136 | * This information will be copied into dma.boxes, when emiting the |
| 137 | * SVGA3dCmdSurfaceDMA command. |
| 138 | */ |
| 139 | struct svga_buffer_range ranges[SVGA_BUFFER_MAX_RANGES]; |
| 140 | unsigned num_ranges; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 141 | } map; |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 142 | |
| 143 | /** |
José Fonseca | b330928 | 2010-02-22 19:24:18 +0000 | [diff] [blame] | 144 | * Information about uploaded version of user buffers. |
| 145 | */ |
| 146 | struct { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 147 | struct pipe_resource *buffer; |
José Fonseca | b330928 | 2010-02-22 19:24:18 +0000 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * We combine multiple user buffers into the same hardware buffer. This |
| 151 | * is the relative offset within that buffer. |
| 152 | */ |
| 153 | unsigned offset; |
José Fonseca | b330928 | 2010-02-22 19:24:18 +0000 | [diff] [blame] | 154 | |
Thomas Hellstrom | 5421942 | 2011-06-30 08:52:19 +0200 | [diff] [blame] | 155 | /** |
| 156 | * Range of user buffer that is uploaded in @buffer at @offset. |
| 157 | */ |
| 158 | unsigned start; |
| 159 | unsigned end; |
| 160 | } uploaded; |
Thomas Hellstrom | 2b301df | 2011-03-17 21:09:38 +0100 | [diff] [blame] | 161 | |
| 162 | /** |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 163 | * DMA'ble memory. |
| 164 | * |
| 165 | * A piece of GMR memory, with the same size of the buffer. It is created |
| 166 | * when mapping the buffer, and will be used to upload vertex data to the |
| 167 | * host. |
José Fonseca | 74651f5 | 2011-03-16 15:43:00 +0000 | [diff] [blame] | 168 | * |
| 169 | * Only set for non-user buffers. |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 170 | */ |
| 171 | struct svga_winsys_buffer *hwbuf; |
| 172 | |
| 173 | /** |
| 174 | * Information about pending DMA uploads. |
| 175 | * |
| 176 | */ |
| 177 | struct { |
| 178 | /** |
| 179 | * Whether this buffer has an unfinished DMA upload command. |
| 180 | * |
| 181 | * If not set then the rest of the information is null. |
| 182 | */ |
| 183 | boolean pending; |
| 184 | |
| 185 | SVGA3dSurfaceDMAFlags flags; |
| 186 | |
| 187 | /** |
| 188 | * Pointer to the DMA copy box *inside* the command buffer. |
| 189 | */ |
| 190 | SVGA3dCopyBox *boxes; |
| 191 | |
| 192 | /** |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 193 | * Pointer to the sequence of update commands |
| 194 | * *inside* the command buffer. |
| 195 | */ |
| 196 | struct svga_3d_update_gb_image *updates; |
| 197 | |
| 198 | /** |
José Fonseca | cf86133 | 2010-02-18 14:05:57 +0000 | [diff] [blame] | 199 | * Context that has the pending DMA to this buffer. |
| 200 | */ |
| 201 | struct svga_context *svga; |
| 202 | } dma; |
| 203 | |
| 204 | /** |
| 205 | * Linked list head, used to gather all buffers with pending dma uploads on |
| 206 | * a context. It is only valid if the dma.pending is set above. |
| 207 | */ |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 208 | struct list_head head; |
Brian Paul | ac114c6 | 2013-04-03 10:23:57 -0600 | [diff] [blame] | 209 | |
| 210 | unsigned size; /**< Approximate size in bytes */ |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 211 | |
| 212 | boolean dirty; /**< Need to do a readback before mapping? */ |
Brian Paul | 4f74b37 | 2016-08-31 14:49:41 -0600 | [diff] [blame] | 213 | |
| 214 | /** In some cases we try to keep the results of the translate_indices() |
| 215 | * function from svga_draw_elements.c |
| 216 | */ |
| 217 | struct { |
| 218 | enum pipe_prim_type orig_prim, new_prim; |
| 219 | struct pipe_resource *buffer; |
| 220 | unsigned index_size; |
| 221 | unsigned offset; /**< first index */ |
| 222 | unsigned count; /**< num indices */ |
| 223 | } translated_indices; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | |
Ilia Mirkin | a2a1a58 | 2015-07-20 19:58:43 -0400 | [diff] [blame] | 227 | static inline struct svga_buffer * |
Brian Paul | e206f67 | 2016-08-25 15:05:27 -0600 | [diff] [blame] | 228 | svga_buffer(struct pipe_resource *resource) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 229 | { |
Brian Paul | e206f67 | 2016-08-25 15:05:27 -0600 | [diff] [blame] | 230 | struct svga_buffer *buf = (struct svga_buffer *) resource; |
| 231 | assert(buf == NULL || buf->b.vtbl == &svga_buffer_vtbl); |
| 232 | return buf; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
| 236 | /** |
| 237 | * Returns TRUE for user buffers. We may |
| 238 | * decide to use an alternate upload path for these buffers. |
| 239 | */ |
Ilia Mirkin | a2a1a58 | 2015-07-20 19:58:43 -0400 | [diff] [blame] | 240 | static inline boolean |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 241 | svga_buffer_is_user_buffer( struct pipe_resource *buffer ) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 242 | { |
José Fonseca | bd00fb2 | 2011-05-31 13:33:34 +0100 | [diff] [blame] | 243 | if (buffer) { |
| 244 | return svga_buffer(buffer)->user; |
| 245 | } else { |
| 246 | return FALSE; |
| 247 | } |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 250 | /** |
| 251 | * Returns a pointer to a struct svga_winsys_screen given a |
| 252 | * struct svga_buffer. |
| 253 | */ |
Ilia Mirkin | a2a1a58 | 2015-07-20 19:58:43 -0400 | [diff] [blame] | 254 | static inline struct svga_winsys_screen * |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 255 | svga_buffer_winsys_screen(struct svga_buffer *sbuf) |
| 256 | { |
| 257 | return svga_screen(sbuf->b.b.screen)->sws; |
| 258 | } |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 259 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 260 | |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 261 | /** |
| 262 | * Returns whether a buffer has hardware storage that is |
| 263 | * visible to the GPU. |
| 264 | */ |
Ilia Mirkin | a2a1a58 | 2015-07-20 19:58:43 -0400 | [diff] [blame] | 265 | static inline boolean |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 266 | svga_buffer_has_hw_storage(struct svga_buffer *sbuf) |
| 267 | { |
| 268 | if (svga_buffer_winsys_screen(sbuf)->have_gb_objects) |
| 269 | return (sbuf->handle ? TRUE : FALSE); |
| 270 | else |
| 271 | return (sbuf->hwbuf ? TRUE : FALSE); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Map the hardware storage of a buffer. |
Brian Paul | 7348df8 | 2017-04-03 08:42:36 -0600 | [diff] [blame] | 276 | * \param flags bitmask of PIPE_TRANSFER_* flags |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 277 | */ |
Ilia Mirkin | a2a1a58 | 2015-07-20 19:58:43 -0400 | [diff] [blame] | 278 | static inline void * |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 279 | svga_buffer_hw_storage_map(struct svga_context *svga, |
| 280 | struct svga_buffer *sbuf, |
| 281 | unsigned flags, boolean *retry) |
| 282 | { |
| 283 | struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf); |
Brian Paul | efe3751 | 2015-10-12 18:40:27 -0600 | [diff] [blame] | 284 | |
Charmaine Lee | ee39814 | 2016-08-31 14:49:52 -0700 | [diff] [blame] | 285 | svga->hud.num_buffers_mapped++; |
Brian Paul | efe3751 | 2015-10-12 18:40:27 -0600 | [diff] [blame] | 286 | |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 287 | if (sws->have_gb_objects) { |
| 288 | return svga->swc->surface_map(svga->swc, sbuf->handle, flags, retry); |
| 289 | } else { |
| 290 | *retry = FALSE; |
| 291 | return sws->buffer_map(sws, sbuf->hwbuf, flags); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Unmap the hardware storage of a buffer. |
| 297 | */ |
Ilia Mirkin | a2a1a58 | 2015-07-20 19:58:43 -0400 | [diff] [blame] | 298 | static inline void |
Brian Paul | e0a6fb0 | 2014-02-08 09:51:14 -0800 | [diff] [blame] | 299 | svga_buffer_hw_storage_unmap(struct svga_context *svga, |
| 300 | struct svga_buffer *sbuf) |
| 301 | { |
| 302 | struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf); |
| 303 | |
| 304 | if (sws->have_gb_objects) { |
| 305 | struct svga_winsys_context *swc = svga->swc; |
| 306 | boolean rebind; |
| 307 | swc->surface_unmap(swc, sbuf->handle, &rebind); |
| 308 | if (rebind) { |
| 309 | enum pipe_error ret; |
| 310 | ret = SVGA3D_BindGBSurface(swc, sbuf->handle); |
| 311 | if (ret != PIPE_OK) { |
| 312 | /* flush and retry */ |
| 313 | svga_context_flush(svga, NULL); |
| 314 | ret = SVGA3D_BindGBSurface(swc, sbuf->handle); |
| 315 | assert(ret == PIPE_OK); |
| 316 | } |
| 317 | } |
| 318 | } else |
| 319 | sws->buffer_unmap(sws, sbuf->hwbuf); |
| 320 | } |
| 321 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 322 | |
| 323 | struct pipe_resource * |
| 324 | svga_user_buffer_create(struct pipe_screen *screen, |
| 325 | void *ptr, |
| 326 | unsigned bytes, |
| 327 | unsigned usage); |
| 328 | |
| 329 | struct pipe_resource * |
| 330 | svga_buffer_create(struct pipe_screen *screen, |
| 331 | const struct pipe_resource *template); |
| 332 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 333 | |
José Fonseca | 2452921 | 2010-02-18 16:40:14 +0000 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | * Get the host surface handle for this buffer. |
| 337 | * |
| 338 | * This will ensure the host surface is updated, issuing DMAs as needed. |
| 339 | * |
| 340 | * NOTE: This may insert new commands in the context, so it *must* be called |
| 341 | * before reserving command buffer space. And, in order to insert commands |
| 342 | * it may need to call svga_context_flush(). |
| 343 | */ |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 344 | struct svga_winsys_surface * |
| 345 | svga_buffer_handle(struct svga_context *svga, |
Charmaine Lee | b549f5e | 2017-06-26 17:24:15 -0600 | [diff] [blame] | 346 | struct pipe_resource *buf, |
| 347 | unsigned tobind_flags); |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 348 | |
| 349 | void |
| 350 | svga_context_flush_buffers(struct svga_context *svga); |
| 351 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 352 | struct svga_winsys_buffer * |
José Fonseca | b845909 | 2010-05-02 23:54:42 +0100 | [diff] [blame] | 353 | svga_winsys_buffer_create(struct svga_context *svga, |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 354 | unsigned alignment, |
| 355 | unsigned usage, |
| 356 | unsigned size); |
| 357 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 358 | #endif /* SVGA_BUFFER_H */ |