1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2011 Marek Olšák <maraeo@gmail.com> 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci/** 29bf215546Sopenharmony_ci * This module uploads user buffers and translates the vertex buffers which 30bf215546Sopenharmony_ci * contain incompatible vertices (i.e. not supported by the driver/hardware) 31bf215546Sopenharmony_ci * into compatible ones, based on the Gallium CAPs. 32bf215546Sopenharmony_ci * 33bf215546Sopenharmony_ci * It does not upload index buffers. 34bf215546Sopenharmony_ci * 35bf215546Sopenharmony_ci * The module heavily uses bitmasks to represent per-buffer and 36bf215546Sopenharmony_ci * per-vertex-element flags to avoid looping over the list of buffers just 37bf215546Sopenharmony_ci * to see if there's a non-zero stride, or user buffer, or unsupported format, 38bf215546Sopenharmony_ci * etc. 39bf215546Sopenharmony_ci * 40bf215546Sopenharmony_ci * There are 3 categories of vertex elements, which are processed separately: 41bf215546Sopenharmony_ci * - per-vertex attribs (stride != 0, instance_divisor == 0) 42bf215546Sopenharmony_ci * - instanced attribs (stride != 0, instance_divisor > 0) 43bf215546Sopenharmony_ci * - constant attribs (stride == 0) 44bf215546Sopenharmony_ci * 45bf215546Sopenharmony_ci * All needed uploads and translations are performed every draw command, but 46bf215546Sopenharmony_ci * only the subset of vertices needed for that draw command is uploaded or 47bf215546Sopenharmony_ci * translated. (the module never translates whole buffers) 48bf215546Sopenharmony_ci * 49bf215546Sopenharmony_ci * 50bf215546Sopenharmony_ci * The module consists of two main parts: 51bf215546Sopenharmony_ci * 52bf215546Sopenharmony_ci * 53bf215546Sopenharmony_ci * 1) Translate (u_vbuf_translate_begin/end) 54bf215546Sopenharmony_ci * 55bf215546Sopenharmony_ci * This is pretty much a vertex fetch fallback. It translates vertices from 56bf215546Sopenharmony_ci * one vertex buffer to another in an unused vertex buffer slot. It does 57bf215546Sopenharmony_ci * whatever is needed to make the vertices readable by the hardware (changes 58bf215546Sopenharmony_ci * vertex formats and aligns offsets and strides). The translate module is 59bf215546Sopenharmony_ci * used here. 60bf215546Sopenharmony_ci * 61bf215546Sopenharmony_ci * Each of the 3 categories is translated to a separate buffer. 62bf215546Sopenharmony_ci * Only the [min_index, max_index] range is translated. For instanced attribs, 63bf215546Sopenharmony_ci * the range is [start_instance, start_instance+instance_count]. For constant 64bf215546Sopenharmony_ci * attribs, the range is [0, 1]. 65bf215546Sopenharmony_ci * 66bf215546Sopenharmony_ci * 67bf215546Sopenharmony_ci * 2) User buffer uploading (u_vbuf_upload_buffers) 68bf215546Sopenharmony_ci * 69bf215546Sopenharmony_ci * Only the [min_index, max_index] range is uploaded (just like Translate) 70bf215546Sopenharmony_ci * with a single memcpy. 71bf215546Sopenharmony_ci * 72bf215546Sopenharmony_ci * This method works best for non-indexed draw operations or indexed draw 73bf215546Sopenharmony_ci * operations where the [min_index, max_index] range is not being way bigger 74bf215546Sopenharmony_ci * than the vertex count. 75bf215546Sopenharmony_ci * 76bf215546Sopenharmony_ci * If the range is too big (e.g. one triangle with indices {0, 1, 10000}), 77bf215546Sopenharmony_ci * the per-vertex attribs are uploaded via the translate module, all packed 78bf215546Sopenharmony_ci * into one vertex buffer, and the indexed draw call is turned into 79bf215546Sopenharmony_ci * a non-indexed one in the process. This adds additional complexity 80bf215546Sopenharmony_ci * to the translate part, but it prevents bad apps from bringing your frame 81bf215546Sopenharmony_ci * rate down. 82bf215546Sopenharmony_ci * 83bf215546Sopenharmony_ci * 84bf215546Sopenharmony_ci * If there is nothing to do, it forwards every command to the driver. 85bf215546Sopenharmony_ci * The module also has its own CSO cache of vertex element states. 86bf215546Sopenharmony_ci */ 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci#include "util/u_vbuf.h" 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci#include "util/u_dump.h" 91bf215546Sopenharmony_ci#include "util/format/u_format.h" 92bf215546Sopenharmony_ci#include "util/u_helpers.h" 93bf215546Sopenharmony_ci#include "util/u_inlines.h" 94bf215546Sopenharmony_ci#include "util/u_memory.h" 95bf215546Sopenharmony_ci#include "indices/u_primconvert.h" 96bf215546Sopenharmony_ci#include "util/u_prim_restart.h" 97bf215546Sopenharmony_ci#include "util/u_screen.h" 98bf215546Sopenharmony_ci#include "util/u_upload_mgr.h" 99bf215546Sopenharmony_ci#include "translate/translate.h" 100bf215546Sopenharmony_ci#include "translate/translate_cache.h" 101bf215546Sopenharmony_ci#include "cso_cache/cso_cache.h" 102bf215546Sopenharmony_ci#include "cso_cache/cso_hash.h" 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_cistruct u_vbuf_elements { 105bf215546Sopenharmony_ci unsigned count; 106bf215546Sopenharmony_ci struct pipe_vertex_element ve[PIPE_MAX_ATTRIBS]; 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci unsigned src_format_size[PIPE_MAX_ATTRIBS]; 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci /* If (velem[i].src_format != native_format[i]), the vertex buffer 111bf215546Sopenharmony_ci * referenced by the vertex element cannot be used for rendering and 112bf215546Sopenharmony_ci * its vertex data must be translated to native_format[i]. */ 113bf215546Sopenharmony_ci enum pipe_format native_format[PIPE_MAX_ATTRIBS]; 114bf215546Sopenharmony_ci unsigned native_format_size[PIPE_MAX_ATTRIBS]; 115bf215546Sopenharmony_ci unsigned component_size[PIPE_MAX_ATTRIBS]; 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci /* Which buffers are used by the vertex element state. */ 118bf215546Sopenharmony_ci uint32_t used_vb_mask; 119bf215546Sopenharmony_ci /* This might mean two things: 120bf215546Sopenharmony_ci * - src_format != native_format, as discussed above. 121bf215546Sopenharmony_ci * - src_offset % 4 != 0 (if the caps don't allow such an offset). */ 122bf215546Sopenharmony_ci uint32_t incompatible_elem_mask; /* each bit describes a corresp. attrib */ 123bf215546Sopenharmony_ci /* Which buffer has at least one vertex element referencing it 124bf215546Sopenharmony_ci * incompatible. */ 125bf215546Sopenharmony_ci uint32_t incompatible_vb_mask_any; 126bf215546Sopenharmony_ci /* Which buffer has all vertex elements referencing it incompatible. */ 127bf215546Sopenharmony_ci uint32_t incompatible_vb_mask_all; 128bf215546Sopenharmony_ci /* Which buffer has at least one vertex element referencing it 129bf215546Sopenharmony_ci * compatible. */ 130bf215546Sopenharmony_ci uint32_t compatible_vb_mask_any; 131bf215546Sopenharmony_ci uint32_t vb_align_mask[2]; //which buffers require 2/4 byte alignments 132bf215546Sopenharmony_ci /* Which buffer has all vertex elements referencing it compatible. */ 133bf215546Sopenharmony_ci uint32_t compatible_vb_mask_all; 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci /* Which buffer has at least one vertex element referencing it 136bf215546Sopenharmony_ci * non-instanced. */ 137bf215546Sopenharmony_ci uint32_t noninstance_vb_mask_any; 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci /* Which buffers are used by multiple vertex attribs. */ 140bf215546Sopenharmony_ci uint32_t interleaved_vb_mask; 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci void *driver_cso; 143bf215546Sopenharmony_ci}; 144bf215546Sopenharmony_ci 145bf215546Sopenharmony_cienum { 146bf215546Sopenharmony_ci VB_VERTEX = 0, 147bf215546Sopenharmony_ci VB_INSTANCE = 1, 148bf215546Sopenharmony_ci VB_CONST = 2, 149bf215546Sopenharmony_ci VB_NUM = 3 150bf215546Sopenharmony_ci}; 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_cistruct u_vbuf { 153bf215546Sopenharmony_ci struct u_vbuf_caps caps; 154bf215546Sopenharmony_ci bool has_signed_vb_offset; 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci struct pipe_context *pipe; 157bf215546Sopenharmony_ci struct translate_cache *translate_cache; 158bf215546Sopenharmony_ci struct cso_cache cso_cache; 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci struct primconvert_context *pc; 161bf215546Sopenharmony_ci bool flatshade_first; 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci /* This is what was set in set_vertex_buffers. 164bf215546Sopenharmony_ci * May contain user buffers. */ 165bf215546Sopenharmony_ci struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS]; 166bf215546Sopenharmony_ci uint32_t enabled_vb_mask; 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ci uint32_t unaligned_vb_mask[2]; //16/32bit 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci /* Vertex buffers for the driver. 171bf215546Sopenharmony_ci * There are usually no user buffers. */ 172bf215546Sopenharmony_ci struct pipe_vertex_buffer real_vertex_buffer[PIPE_MAX_ATTRIBS]; 173bf215546Sopenharmony_ci uint32_t dirty_real_vb_mask; /* which buffers are dirty since the last 174bf215546Sopenharmony_ci call of set_vertex_buffers */ 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_ci /* Vertex elements. */ 177bf215546Sopenharmony_ci struct u_vbuf_elements *ve, *ve_saved; 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci /* Vertex elements used for the translate fallback. */ 180bf215546Sopenharmony_ci struct cso_velems_state fallback_velems; 181bf215546Sopenharmony_ci /* If non-NULL, this is a vertex element state used for the translate 182bf215546Sopenharmony_ci * fallback and therefore used for rendering too. */ 183bf215546Sopenharmony_ci boolean using_translate; 184bf215546Sopenharmony_ci /* The vertex buffer slot index where translated vertices have been 185bf215546Sopenharmony_ci * stored in. */ 186bf215546Sopenharmony_ci unsigned fallback_vbs[VB_NUM]; 187bf215546Sopenharmony_ci unsigned fallback_vbs_mask; 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ci /* Which buffer is a user buffer. */ 190bf215546Sopenharmony_ci uint32_t user_vb_mask; /* each bit describes a corresp. buffer */ 191bf215546Sopenharmony_ci /* Which buffer is incompatible (unaligned). */ 192bf215546Sopenharmony_ci uint32_t incompatible_vb_mask; /* each bit describes a corresp. buffer */ 193bf215546Sopenharmony_ci /* Which buffer has a non-zero stride. */ 194bf215546Sopenharmony_ci uint32_t nonzero_stride_vb_mask; /* each bit describes a corresp. buffer */ 195bf215546Sopenharmony_ci /* Which buffers are allowed (supported by hardware). */ 196bf215546Sopenharmony_ci uint32_t allowed_vb_mask; 197bf215546Sopenharmony_ci}; 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_cistatic void * 200bf215546Sopenharmony_ciu_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count, 201bf215546Sopenharmony_ci const struct pipe_vertex_element *attribs); 202bf215546Sopenharmony_cistatic void u_vbuf_delete_vertex_elements(void *ctx, void *state, 203bf215546Sopenharmony_ci enum cso_cache_type type); 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_cistatic const struct { 206bf215546Sopenharmony_ci enum pipe_format from, to; 207bf215546Sopenharmony_ci} vbuf_format_fallbacks[] = { 208bf215546Sopenharmony_ci { PIPE_FORMAT_R32_FIXED, PIPE_FORMAT_R32_FLOAT }, 209bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32_FIXED, PIPE_FORMAT_R32G32_FLOAT }, 210bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32_FIXED, PIPE_FORMAT_R32G32B32_FLOAT }, 211bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32A32_FIXED, PIPE_FORMAT_R32G32B32A32_FLOAT }, 212bf215546Sopenharmony_ci { PIPE_FORMAT_R16_FLOAT, PIPE_FORMAT_R32_FLOAT }, 213bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16_FLOAT, PIPE_FORMAT_R32G32_FLOAT }, 214bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16_FLOAT, PIPE_FORMAT_R32G32B32_FLOAT }, 215bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16A16_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT }, 216bf215546Sopenharmony_ci { PIPE_FORMAT_R64_FLOAT, PIPE_FORMAT_R32_FLOAT }, 217bf215546Sopenharmony_ci { PIPE_FORMAT_R64G64_FLOAT, PIPE_FORMAT_R32G32_FLOAT }, 218bf215546Sopenharmony_ci { PIPE_FORMAT_R64G64B64_FLOAT, PIPE_FORMAT_R32G32B32_FLOAT }, 219bf215546Sopenharmony_ci { PIPE_FORMAT_R64G64B64A64_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT }, 220bf215546Sopenharmony_ci { PIPE_FORMAT_R32_UNORM, PIPE_FORMAT_R32_FLOAT }, 221bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32_UNORM, PIPE_FORMAT_R32G32_FLOAT }, 222bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32_UNORM, PIPE_FORMAT_R32G32B32_FLOAT }, 223bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32A32_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT }, 224bf215546Sopenharmony_ci { PIPE_FORMAT_R32_SNORM, PIPE_FORMAT_R32_FLOAT }, 225bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32_SNORM, PIPE_FORMAT_R32G32_FLOAT }, 226bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32_SNORM, PIPE_FORMAT_R32G32B32_FLOAT }, 227bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32A32_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT }, 228bf215546Sopenharmony_ci { PIPE_FORMAT_R32_USCALED, PIPE_FORMAT_R32_FLOAT }, 229bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32_USCALED, PIPE_FORMAT_R32G32_FLOAT }, 230bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32_USCALED, PIPE_FORMAT_R32G32B32_FLOAT }, 231bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32A32_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT }, 232bf215546Sopenharmony_ci { PIPE_FORMAT_R32_SSCALED, PIPE_FORMAT_R32_FLOAT }, 233bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32_SSCALED, PIPE_FORMAT_R32G32_FLOAT }, 234bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT }, 235bf215546Sopenharmony_ci { PIPE_FORMAT_R32G32B32A32_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT }, 236bf215546Sopenharmony_ci { PIPE_FORMAT_R16_UNORM, PIPE_FORMAT_R32_FLOAT }, 237bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16_UNORM, PIPE_FORMAT_R32G32_FLOAT }, 238bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16_UNORM, PIPE_FORMAT_R32G32B32_FLOAT }, 239bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16A16_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT }, 240bf215546Sopenharmony_ci { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R32_FLOAT }, 241bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16_SNORM, PIPE_FORMAT_R32G32_FLOAT }, 242bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16_SNORM, PIPE_FORMAT_R32G32B32_FLOAT }, 243bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16_SINT, PIPE_FORMAT_R32G32B32_SINT }, 244bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16_UINT, PIPE_FORMAT_R32G32B32_UINT }, 245bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT }, 246bf215546Sopenharmony_ci { PIPE_FORMAT_R16_USCALED, PIPE_FORMAT_R32_FLOAT }, 247bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16_USCALED, PIPE_FORMAT_R32G32_FLOAT }, 248bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16_USCALED, PIPE_FORMAT_R32G32B32_FLOAT }, 249bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16A16_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT }, 250bf215546Sopenharmony_ci { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R32_FLOAT }, 251bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16_SSCALED, PIPE_FORMAT_R32G32_FLOAT }, 252bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT }, 253bf215546Sopenharmony_ci { PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT }, 254bf215546Sopenharmony_ci { PIPE_FORMAT_R8_UNORM, PIPE_FORMAT_R32_FLOAT }, 255bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8_UNORM, PIPE_FORMAT_R32G32_FLOAT }, 256bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8_UNORM, PIPE_FORMAT_R32G32B32_FLOAT }, 257bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8A8_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT }, 258bf215546Sopenharmony_ci { PIPE_FORMAT_R8_SNORM, PIPE_FORMAT_R32_FLOAT }, 259bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8_SNORM, PIPE_FORMAT_R32G32_FLOAT }, 260bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8_SNORM, PIPE_FORMAT_R32G32B32_FLOAT }, 261bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8A8_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT }, 262bf215546Sopenharmony_ci { PIPE_FORMAT_R8_USCALED, PIPE_FORMAT_R32_FLOAT }, 263bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8_USCALED, PIPE_FORMAT_R32G32_FLOAT }, 264bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8_USCALED, PIPE_FORMAT_R32G32B32_FLOAT }, 265bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8A8_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT }, 266bf215546Sopenharmony_ci { PIPE_FORMAT_R8_SSCALED, PIPE_FORMAT_R32_FLOAT }, 267bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8_SSCALED, PIPE_FORMAT_R32G32_FLOAT }, 268bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT }, 269bf215546Sopenharmony_ci { PIPE_FORMAT_R8G8B8A8_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT }, 270bf215546Sopenharmony_ci}; 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_civoid u_vbuf_get_caps(struct pipe_screen *screen, struct u_vbuf_caps *caps, 273bf215546Sopenharmony_ci bool needs64b) 274bf215546Sopenharmony_ci{ 275bf215546Sopenharmony_ci unsigned i; 276bf215546Sopenharmony_ci 277bf215546Sopenharmony_ci memset(caps, 0, sizeof(*caps)); 278bf215546Sopenharmony_ci 279bf215546Sopenharmony_ci /* I'd rather have a bitfield of which formats are supported and a static 280bf215546Sopenharmony_ci * table of the translations indexed by format, but since we don't have C99 281bf215546Sopenharmony_ci * we can't easily make a sparsely-populated table indexed by format. So, 282bf215546Sopenharmony_ci * we construct the sparse table here. 283bf215546Sopenharmony_ci */ 284bf215546Sopenharmony_ci for (i = 0; i < PIPE_FORMAT_COUNT; i++) 285bf215546Sopenharmony_ci caps->format_translation[i] = i; 286bf215546Sopenharmony_ci 287bf215546Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(vbuf_format_fallbacks); i++) { 288bf215546Sopenharmony_ci enum pipe_format format = vbuf_format_fallbacks[i].from; 289bf215546Sopenharmony_ci unsigned comp_bits = util_format_get_component_bits(format, 0, 0); 290bf215546Sopenharmony_ci 291bf215546Sopenharmony_ci if ((comp_bits > 32) && !needs64b) 292bf215546Sopenharmony_ci continue; 293bf215546Sopenharmony_ci 294bf215546Sopenharmony_ci if (!screen->is_format_supported(screen, format, PIPE_BUFFER, 0, 0, 295bf215546Sopenharmony_ci PIPE_BIND_VERTEX_BUFFER)) { 296bf215546Sopenharmony_ci caps->format_translation[format] = vbuf_format_fallbacks[i].to; 297bf215546Sopenharmony_ci caps->fallback_always = true; 298bf215546Sopenharmony_ci } 299bf215546Sopenharmony_ci } 300bf215546Sopenharmony_ci 301bf215546Sopenharmony_ci caps->buffer_offset_unaligned = 302bf215546Sopenharmony_ci !screen->get_param(screen, 303bf215546Sopenharmony_ci PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY); 304bf215546Sopenharmony_ci caps->buffer_stride_unaligned = 305bf215546Sopenharmony_ci !screen->get_param(screen, 306bf215546Sopenharmony_ci PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY); 307bf215546Sopenharmony_ci caps->velem_src_offset_unaligned = 308bf215546Sopenharmony_ci !screen->get_param(screen, 309bf215546Sopenharmony_ci PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY); 310bf215546Sopenharmony_ci caps->attrib_component_unaligned = 311bf215546Sopenharmony_ci !screen->get_param(screen, 312bf215546Sopenharmony_ci PIPE_CAP_VERTEX_ATTRIB_ELEMENT_ALIGNED_ONLY); 313bf215546Sopenharmony_ci assert(caps->attrib_component_unaligned || 314bf215546Sopenharmony_ci (caps->velem_src_offset_unaligned && caps->buffer_stride_unaligned && caps->buffer_offset_unaligned)); 315bf215546Sopenharmony_ci caps->user_vertex_buffers = 316bf215546Sopenharmony_ci screen->get_param(screen, PIPE_CAP_USER_VERTEX_BUFFERS); 317bf215546Sopenharmony_ci caps->max_vertex_buffers = 318bf215546Sopenharmony_ci screen->get_param(screen, PIPE_CAP_MAX_VERTEX_BUFFERS); 319bf215546Sopenharmony_ci 320bf215546Sopenharmony_ci if (screen->get_param(screen, PIPE_CAP_PRIMITIVE_RESTART) || 321bf215546Sopenharmony_ci screen->get_param(screen, PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX)) { 322bf215546Sopenharmony_ci caps->rewrite_restart_index = screen->get_param(screen, PIPE_CAP_EMULATE_NONFIXED_PRIMITIVE_RESTART); 323bf215546Sopenharmony_ci caps->supported_restart_modes = screen->get_param(screen, PIPE_CAP_SUPPORTED_PRIM_MODES_WITH_RESTART); 324bf215546Sopenharmony_ci caps->supported_restart_modes |= BITFIELD_BIT(PIPE_PRIM_PATCHES); 325bf215546Sopenharmony_ci if (caps->supported_restart_modes != BITFIELD_MASK(PIPE_PRIM_MAX)) 326bf215546Sopenharmony_ci caps->fallback_always = true; 327bf215546Sopenharmony_ci caps->fallback_always |= caps->rewrite_restart_index; 328bf215546Sopenharmony_ci } 329bf215546Sopenharmony_ci caps->supported_prim_modes = screen->get_param(screen, PIPE_CAP_SUPPORTED_PRIM_MODES); 330bf215546Sopenharmony_ci if (caps->supported_prim_modes != BITFIELD_MASK(PIPE_PRIM_MAX)) 331bf215546Sopenharmony_ci caps->fallback_always = true; 332bf215546Sopenharmony_ci 333bf215546Sopenharmony_ci if (!screen->is_format_supported(screen, PIPE_FORMAT_R8_UINT, PIPE_BUFFER, 0, 0, PIPE_BIND_INDEX_BUFFER)) 334bf215546Sopenharmony_ci caps->fallback_always = caps->rewrite_ubyte_ibs = true; 335bf215546Sopenharmony_ci 336bf215546Sopenharmony_ci /* OpenGL 2.0 requires a minimum of 16 vertex buffers */ 337bf215546Sopenharmony_ci if (caps->max_vertex_buffers < 16) 338bf215546Sopenharmony_ci caps->fallback_always = true; 339bf215546Sopenharmony_ci 340bf215546Sopenharmony_ci if (!caps->buffer_offset_unaligned || 341bf215546Sopenharmony_ci !caps->buffer_stride_unaligned || 342bf215546Sopenharmony_ci !caps->attrib_component_unaligned || 343bf215546Sopenharmony_ci !caps->velem_src_offset_unaligned) 344bf215546Sopenharmony_ci caps->fallback_always = true; 345bf215546Sopenharmony_ci 346bf215546Sopenharmony_ci if (!caps->fallback_always && !caps->user_vertex_buffers) 347bf215546Sopenharmony_ci caps->fallback_only_for_user_vbuffers = true; 348bf215546Sopenharmony_ci} 349bf215546Sopenharmony_ci 350bf215546Sopenharmony_cistruct u_vbuf * 351bf215546Sopenharmony_ciu_vbuf_create(struct pipe_context *pipe, struct u_vbuf_caps *caps) 352bf215546Sopenharmony_ci{ 353bf215546Sopenharmony_ci struct u_vbuf *mgr = CALLOC_STRUCT(u_vbuf); 354bf215546Sopenharmony_ci 355bf215546Sopenharmony_ci mgr->caps = *caps; 356bf215546Sopenharmony_ci mgr->pipe = pipe; 357bf215546Sopenharmony_ci if (caps->rewrite_ubyte_ibs || caps->rewrite_restart_index || 358bf215546Sopenharmony_ci /* require all but patches */ 359bf215546Sopenharmony_ci ((caps->supported_prim_modes & caps->supported_restart_modes & BITFIELD_MASK(PIPE_PRIM_MAX))) != 360bf215546Sopenharmony_ci BITFIELD_MASK(PIPE_PRIM_MAX)) { 361bf215546Sopenharmony_ci struct primconvert_config cfg; 362bf215546Sopenharmony_ci cfg.fixed_prim_restart = caps->rewrite_restart_index; 363bf215546Sopenharmony_ci cfg.primtypes_mask = caps->supported_prim_modes; 364bf215546Sopenharmony_ci cfg.restart_primtypes_mask = caps->supported_restart_modes; 365bf215546Sopenharmony_ci mgr->pc = util_primconvert_create_config(pipe, &cfg); 366bf215546Sopenharmony_ci } 367bf215546Sopenharmony_ci mgr->translate_cache = translate_cache_create(); 368bf215546Sopenharmony_ci memset(mgr->fallback_vbs, ~0, sizeof(mgr->fallback_vbs)); 369bf215546Sopenharmony_ci mgr->allowed_vb_mask = u_bit_consecutive(0, mgr->caps.max_vertex_buffers); 370bf215546Sopenharmony_ci 371bf215546Sopenharmony_ci mgr->has_signed_vb_offset = 372bf215546Sopenharmony_ci pipe->screen->get_param(pipe->screen, 373bf215546Sopenharmony_ci PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET); 374bf215546Sopenharmony_ci 375bf215546Sopenharmony_ci cso_cache_init(&mgr->cso_cache, pipe); 376bf215546Sopenharmony_ci cso_cache_set_delete_cso_callback(&mgr->cso_cache, 377bf215546Sopenharmony_ci u_vbuf_delete_vertex_elements, pipe); 378bf215546Sopenharmony_ci 379bf215546Sopenharmony_ci return mgr; 380bf215546Sopenharmony_ci} 381bf215546Sopenharmony_ci 382bf215546Sopenharmony_ci/* u_vbuf uses its own caching for vertex elements, because it needs to keep 383bf215546Sopenharmony_ci * its own preprocessed state per vertex element CSO. */ 384bf215546Sopenharmony_cistatic struct u_vbuf_elements * 385bf215546Sopenharmony_ciu_vbuf_set_vertex_elements_internal(struct u_vbuf *mgr, 386bf215546Sopenharmony_ci const struct cso_velems_state *velems) 387bf215546Sopenharmony_ci{ 388bf215546Sopenharmony_ci struct pipe_context *pipe = mgr->pipe; 389bf215546Sopenharmony_ci unsigned key_size, hash_key; 390bf215546Sopenharmony_ci struct cso_hash_iter iter; 391bf215546Sopenharmony_ci struct u_vbuf_elements *ve; 392bf215546Sopenharmony_ci 393bf215546Sopenharmony_ci /* need to include the count into the stored state data too. */ 394bf215546Sopenharmony_ci key_size = sizeof(struct pipe_vertex_element) * velems->count + 395bf215546Sopenharmony_ci sizeof(unsigned); 396bf215546Sopenharmony_ci hash_key = cso_construct_key((void*)velems, key_size); 397bf215546Sopenharmony_ci iter = cso_find_state_template(&mgr->cso_cache, hash_key, CSO_VELEMENTS, 398bf215546Sopenharmony_ci (void*)velems, key_size); 399bf215546Sopenharmony_ci 400bf215546Sopenharmony_ci if (cso_hash_iter_is_null(iter)) { 401bf215546Sopenharmony_ci struct cso_velements *cso = MALLOC_STRUCT(cso_velements); 402bf215546Sopenharmony_ci memcpy(&cso->state, velems, key_size); 403bf215546Sopenharmony_ci cso->data = u_vbuf_create_vertex_elements(mgr, velems->count, 404bf215546Sopenharmony_ci velems->velems); 405bf215546Sopenharmony_ci 406bf215546Sopenharmony_ci iter = cso_insert_state(&mgr->cso_cache, hash_key, CSO_VELEMENTS, cso); 407bf215546Sopenharmony_ci ve = cso->data; 408bf215546Sopenharmony_ci } else { 409bf215546Sopenharmony_ci ve = ((struct cso_velements *)cso_hash_iter_data(iter))->data; 410bf215546Sopenharmony_ci } 411bf215546Sopenharmony_ci 412bf215546Sopenharmony_ci assert(ve); 413bf215546Sopenharmony_ci 414bf215546Sopenharmony_ci if (ve != mgr->ve) 415bf215546Sopenharmony_ci pipe->bind_vertex_elements_state(pipe, ve->driver_cso); 416bf215546Sopenharmony_ci 417bf215546Sopenharmony_ci return ve; 418bf215546Sopenharmony_ci} 419bf215546Sopenharmony_ci 420bf215546Sopenharmony_civoid u_vbuf_set_vertex_elements(struct u_vbuf *mgr, 421bf215546Sopenharmony_ci const struct cso_velems_state *velems) 422bf215546Sopenharmony_ci{ 423bf215546Sopenharmony_ci mgr->ve = u_vbuf_set_vertex_elements_internal(mgr, velems); 424bf215546Sopenharmony_ci} 425bf215546Sopenharmony_ci 426bf215546Sopenharmony_civoid u_vbuf_set_flatshade_first(struct u_vbuf *mgr, bool flatshade_first) 427bf215546Sopenharmony_ci{ 428bf215546Sopenharmony_ci mgr->flatshade_first = flatshade_first; 429bf215546Sopenharmony_ci} 430bf215546Sopenharmony_ci 431bf215546Sopenharmony_civoid u_vbuf_unset_vertex_elements(struct u_vbuf *mgr) 432bf215546Sopenharmony_ci{ 433bf215546Sopenharmony_ci mgr->ve = NULL; 434bf215546Sopenharmony_ci} 435bf215546Sopenharmony_ci 436bf215546Sopenharmony_civoid u_vbuf_destroy(struct u_vbuf *mgr) 437bf215546Sopenharmony_ci{ 438bf215546Sopenharmony_ci struct pipe_screen *screen = mgr->pipe->screen; 439bf215546Sopenharmony_ci unsigned i; 440bf215546Sopenharmony_ci const unsigned num_vb = screen->get_shader_param(screen, PIPE_SHADER_VERTEX, 441bf215546Sopenharmony_ci PIPE_SHADER_CAP_MAX_INPUTS); 442bf215546Sopenharmony_ci 443bf215546Sopenharmony_ci mgr->pipe->set_vertex_buffers(mgr->pipe, 0, 0, num_vb, false, NULL); 444bf215546Sopenharmony_ci 445bf215546Sopenharmony_ci for (i = 0; i < PIPE_MAX_ATTRIBS; i++) 446bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(&mgr->vertex_buffer[i]); 447bf215546Sopenharmony_ci for (i = 0; i < PIPE_MAX_ATTRIBS; i++) 448bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[i]); 449bf215546Sopenharmony_ci 450bf215546Sopenharmony_ci if (mgr->pc) 451bf215546Sopenharmony_ci util_primconvert_destroy(mgr->pc); 452bf215546Sopenharmony_ci 453bf215546Sopenharmony_ci translate_cache_destroy(mgr->translate_cache); 454bf215546Sopenharmony_ci cso_cache_delete(&mgr->cso_cache); 455bf215546Sopenharmony_ci FREE(mgr); 456bf215546Sopenharmony_ci} 457bf215546Sopenharmony_ci 458bf215546Sopenharmony_cistatic enum pipe_error 459bf215546Sopenharmony_ciu_vbuf_translate_buffers(struct u_vbuf *mgr, struct translate_key *key, 460bf215546Sopenharmony_ci const struct pipe_draw_info *info, 461bf215546Sopenharmony_ci const struct pipe_draw_start_count_bias *draw, 462bf215546Sopenharmony_ci unsigned vb_mask, unsigned out_vb, 463bf215546Sopenharmony_ci int start_vertex, unsigned num_vertices, 464bf215546Sopenharmony_ci int min_index, boolean unroll_indices) 465bf215546Sopenharmony_ci{ 466bf215546Sopenharmony_ci struct translate *tr; 467bf215546Sopenharmony_ci struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {0}; 468bf215546Sopenharmony_ci struct pipe_resource *out_buffer = NULL; 469bf215546Sopenharmony_ci uint8_t *out_map; 470bf215546Sopenharmony_ci unsigned out_offset, mask; 471bf215546Sopenharmony_ci 472bf215546Sopenharmony_ci /* Get a translate object. */ 473bf215546Sopenharmony_ci tr = translate_cache_find(mgr->translate_cache, key); 474bf215546Sopenharmony_ci 475bf215546Sopenharmony_ci /* Map buffers we want to translate. */ 476bf215546Sopenharmony_ci mask = vb_mask; 477bf215546Sopenharmony_ci while (mask) { 478bf215546Sopenharmony_ci struct pipe_vertex_buffer *vb; 479bf215546Sopenharmony_ci unsigned offset; 480bf215546Sopenharmony_ci uint8_t *map; 481bf215546Sopenharmony_ci unsigned i = u_bit_scan(&mask); 482bf215546Sopenharmony_ci 483bf215546Sopenharmony_ci vb = &mgr->vertex_buffer[i]; 484bf215546Sopenharmony_ci offset = vb->buffer_offset + vb->stride * start_vertex; 485bf215546Sopenharmony_ci 486bf215546Sopenharmony_ci if (vb->is_user_buffer) { 487bf215546Sopenharmony_ci map = (uint8_t*)vb->buffer.user + offset; 488bf215546Sopenharmony_ci } else { 489bf215546Sopenharmony_ci unsigned size = vb->stride ? num_vertices * vb->stride 490bf215546Sopenharmony_ci : sizeof(double)*4; 491bf215546Sopenharmony_ci 492bf215546Sopenharmony_ci if (!vb->buffer.resource) { 493bf215546Sopenharmony_ci static uint64_t dummy_buf[4] = { 0 }; 494bf215546Sopenharmony_ci tr->set_buffer(tr, i, dummy_buf, 0, 0); 495bf215546Sopenharmony_ci continue; 496bf215546Sopenharmony_ci } 497bf215546Sopenharmony_ci 498bf215546Sopenharmony_ci if (vb->stride) { 499bf215546Sopenharmony_ci /* the stride cannot be used to calculate the map size of the buffer, 500bf215546Sopenharmony_ci * as it only determines the bytes between elements, not the size of elements 501bf215546Sopenharmony_ci * themselves, meaning that if stride < element_size, the mapped size will 502bf215546Sopenharmony_ci * be too small and conversion will overrun the map buffer 503bf215546Sopenharmony_ci * 504bf215546Sopenharmony_ci * instead, add the size of the largest possible attribute to the final attribute's offset 505bf215546Sopenharmony_ci * in order to ensure the map is large enough 506bf215546Sopenharmony_ci */ 507bf215546Sopenharmony_ci unsigned last_offset = size - vb->stride; 508bf215546Sopenharmony_ci size = MAX2(size, last_offset + sizeof(double)*4); 509bf215546Sopenharmony_ci } 510bf215546Sopenharmony_ci 511bf215546Sopenharmony_ci if (offset + size > vb->buffer.resource->width0) { 512bf215546Sopenharmony_ci /* Don't try to map past end of buffer. This often happens when 513bf215546Sopenharmony_ci * we're translating an attribute that's at offset > 0 from the 514bf215546Sopenharmony_ci * start of the vertex. If we'd subtract attrib's offset from 515bf215546Sopenharmony_ci * the size, this probably wouldn't happen. 516bf215546Sopenharmony_ci */ 517bf215546Sopenharmony_ci size = vb->buffer.resource->width0 - offset; 518bf215546Sopenharmony_ci 519bf215546Sopenharmony_ci /* Also adjust num_vertices. A common user error is to call 520bf215546Sopenharmony_ci * glDrawRangeElements() with incorrect 'end' argument. The 'end 521bf215546Sopenharmony_ci * value should be the max index value, but people often 522bf215546Sopenharmony_ci * accidentally add one to this value. This adjustment avoids 523bf215546Sopenharmony_ci * crashing (by reading past the end of a hardware buffer mapping) 524bf215546Sopenharmony_ci * when people do that. 525bf215546Sopenharmony_ci */ 526bf215546Sopenharmony_ci num_vertices = (size + vb->stride - 1) / vb->stride; 527bf215546Sopenharmony_ci } 528bf215546Sopenharmony_ci 529bf215546Sopenharmony_ci map = pipe_buffer_map_range(mgr->pipe, vb->buffer.resource, offset, size, 530bf215546Sopenharmony_ci PIPE_MAP_READ, &vb_transfer[i]); 531bf215546Sopenharmony_ci } 532bf215546Sopenharmony_ci 533bf215546Sopenharmony_ci /* Subtract min_index so that indexing with the index buffer works. */ 534bf215546Sopenharmony_ci if (unroll_indices) { 535bf215546Sopenharmony_ci map -= (ptrdiff_t)vb->stride * min_index; 536bf215546Sopenharmony_ci } 537bf215546Sopenharmony_ci 538bf215546Sopenharmony_ci tr->set_buffer(tr, i, map, vb->stride, info->max_index); 539bf215546Sopenharmony_ci } 540bf215546Sopenharmony_ci 541bf215546Sopenharmony_ci /* Translate. */ 542bf215546Sopenharmony_ci if (unroll_indices) { 543bf215546Sopenharmony_ci struct pipe_transfer *transfer = NULL; 544bf215546Sopenharmony_ci const unsigned offset = draw->start * info->index_size; 545bf215546Sopenharmony_ci uint8_t *map; 546bf215546Sopenharmony_ci 547bf215546Sopenharmony_ci /* Create and map the output buffer. */ 548bf215546Sopenharmony_ci u_upload_alloc(mgr->pipe->stream_uploader, 0, 549bf215546Sopenharmony_ci key->output_stride * draw->count, 4, 550bf215546Sopenharmony_ci &out_offset, &out_buffer, 551bf215546Sopenharmony_ci (void**)&out_map); 552bf215546Sopenharmony_ci if (!out_buffer) 553bf215546Sopenharmony_ci return PIPE_ERROR_OUT_OF_MEMORY; 554bf215546Sopenharmony_ci 555bf215546Sopenharmony_ci if (info->has_user_indices) { 556bf215546Sopenharmony_ci map = (uint8_t*)info->index.user + offset; 557bf215546Sopenharmony_ci } else { 558bf215546Sopenharmony_ci map = pipe_buffer_map_range(mgr->pipe, info->index.resource, offset, 559bf215546Sopenharmony_ci draw->count * info->index_size, 560bf215546Sopenharmony_ci PIPE_MAP_READ, &transfer); 561bf215546Sopenharmony_ci } 562bf215546Sopenharmony_ci 563bf215546Sopenharmony_ci switch (info->index_size) { 564bf215546Sopenharmony_ci case 4: 565bf215546Sopenharmony_ci tr->run_elts(tr, (unsigned*)map, draw->count, 0, 0, out_map); 566bf215546Sopenharmony_ci break; 567bf215546Sopenharmony_ci case 2: 568bf215546Sopenharmony_ci tr->run_elts16(tr, (uint16_t*)map, draw->count, 0, 0, out_map); 569bf215546Sopenharmony_ci break; 570bf215546Sopenharmony_ci case 1: 571bf215546Sopenharmony_ci tr->run_elts8(tr, map, draw->count, 0, 0, out_map); 572bf215546Sopenharmony_ci break; 573bf215546Sopenharmony_ci } 574bf215546Sopenharmony_ci 575bf215546Sopenharmony_ci if (transfer) { 576bf215546Sopenharmony_ci pipe_buffer_unmap(mgr->pipe, transfer); 577bf215546Sopenharmony_ci } 578bf215546Sopenharmony_ci } else { 579bf215546Sopenharmony_ci /* Create and map the output buffer. */ 580bf215546Sopenharmony_ci u_upload_alloc(mgr->pipe->stream_uploader, 581bf215546Sopenharmony_ci mgr->has_signed_vb_offset ? 582bf215546Sopenharmony_ci 0 : key->output_stride * start_vertex, 583bf215546Sopenharmony_ci key->output_stride * num_vertices, 4, 584bf215546Sopenharmony_ci &out_offset, &out_buffer, 585bf215546Sopenharmony_ci (void**)&out_map); 586bf215546Sopenharmony_ci if (!out_buffer) 587bf215546Sopenharmony_ci return PIPE_ERROR_OUT_OF_MEMORY; 588bf215546Sopenharmony_ci 589bf215546Sopenharmony_ci out_offset -= key->output_stride * start_vertex; 590bf215546Sopenharmony_ci 591bf215546Sopenharmony_ci tr->run(tr, 0, num_vertices, 0, 0, out_map); 592bf215546Sopenharmony_ci } 593bf215546Sopenharmony_ci 594bf215546Sopenharmony_ci /* Unmap all buffers. */ 595bf215546Sopenharmony_ci mask = vb_mask; 596bf215546Sopenharmony_ci while (mask) { 597bf215546Sopenharmony_ci unsigned i = u_bit_scan(&mask); 598bf215546Sopenharmony_ci 599bf215546Sopenharmony_ci if (vb_transfer[i]) { 600bf215546Sopenharmony_ci pipe_buffer_unmap(mgr->pipe, vb_transfer[i]); 601bf215546Sopenharmony_ci } 602bf215546Sopenharmony_ci } 603bf215546Sopenharmony_ci 604bf215546Sopenharmony_ci /* Setup the new vertex buffer. */ 605bf215546Sopenharmony_ci mgr->real_vertex_buffer[out_vb].buffer_offset = out_offset; 606bf215546Sopenharmony_ci mgr->real_vertex_buffer[out_vb].stride = key->output_stride; 607bf215546Sopenharmony_ci 608bf215546Sopenharmony_ci /* Move the buffer reference. */ 609bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[out_vb]); 610bf215546Sopenharmony_ci mgr->real_vertex_buffer[out_vb].buffer.resource = out_buffer; 611bf215546Sopenharmony_ci mgr->real_vertex_buffer[out_vb].is_user_buffer = false; 612bf215546Sopenharmony_ci 613bf215546Sopenharmony_ci return PIPE_OK; 614bf215546Sopenharmony_ci} 615bf215546Sopenharmony_ci 616bf215546Sopenharmony_cistatic boolean 617bf215546Sopenharmony_ciu_vbuf_translate_find_free_vb_slots(struct u_vbuf *mgr, 618bf215546Sopenharmony_ci unsigned mask[VB_NUM]) 619bf215546Sopenharmony_ci{ 620bf215546Sopenharmony_ci unsigned type; 621bf215546Sopenharmony_ci unsigned fallback_vbs[VB_NUM]; 622bf215546Sopenharmony_ci /* Set the bit for each buffer which is incompatible, or isn't set. */ 623bf215546Sopenharmony_ci uint32_t unused_vb_mask = 624bf215546Sopenharmony_ci mgr->ve->incompatible_vb_mask_all | mgr->incompatible_vb_mask | 625bf215546Sopenharmony_ci ~mgr->enabled_vb_mask; 626bf215546Sopenharmony_ci uint32_t unused_vb_mask_orig; 627bf215546Sopenharmony_ci boolean insufficient_buffers = false; 628bf215546Sopenharmony_ci 629bf215546Sopenharmony_ci /* No vertex buffers available at all */ 630bf215546Sopenharmony_ci if (!unused_vb_mask) 631bf215546Sopenharmony_ci return FALSE; 632bf215546Sopenharmony_ci 633bf215546Sopenharmony_ci memset(fallback_vbs, ~0, sizeof(fallback_vbs)); 634bf215546Sopenharmony_ci mgr->fallback_vbs_mask = 0; 635bf215546Sopenharmony_ci 636bf215546Sopenharmony_ci /* Find free slots for each type if needed. */ 637bf215546Sopenharmony_ci unused_vb_mask_orig = unused_vb_mask; 638bf215546Sopenharmony_ci for (type = 0; type < VB_NUM; type++) { 639bf215546Sopenharmony_ci if (mask[type]) { 640bf215546Sopenharmony_ci uint32_t index; 641bf215546Sopenharmony_ci 642bf215546Sopenharmony_ci if (!unused_vb_mask) { 643bf215546Sopenharmony_ci insufficient_buffers = true; 644bf215546Sopenharmony_ci break; 645bf215546Sopenharmony_ci } 646bf215546Sopenharmony_ci 647bf215546Sopenharmony_ci index = ffs(unused_vb_mask) - 1; 648bf215546Sopenharmony_ci fallback_vbs[type] = index; 649bf215546Sopenharmony_ci mgr->fallback_vbs_mask |= 1 << index; 650bf215546Sopenharmony_ci unused_vb_mask &= ~(1 << index); 651bf215546Sopenharmony_ci /*printf("found slot=%i for type=%i\n", index, type);*/ 652bf215546Sopenharmony_ci } 653bf215546Sopenharmony_ci } 654bf215546Sopenharmony_ci 655bf215546Sopenharmony_ci if (insufficient_buffers) { 656bf215546Sopenharmony_ci /* not enough vbs for all types supported by the hardware, they will have to share one 657bf215546Sopenharmony_ci * buffer */ 658bf215546Sopenharmony_ci uint32_t index = ffs(unused_vb_mask_orig) - 1; 659bf215546Sopenharmony_ci /* When sharing one vertex buffer use per-vertex frequency for everything. */ 660bf215546Sopenharmony_ci fallback_vbs[VB_VERTEX] = index; 661bf215546Sopenharmony_ci mgr->fallback_vbs_mask = 1 << index; 662bf215546Sopenharmony_ci mask[VB_VERTEX] = mask[VB_VERTEX] | mask[VB_CONST] | mask[VB_INSTANCE]; 663bf215546Sopenharmony_ci mask[VB_CONST] = 0; 664bf215546Sopenharmony_ci mask[VB_INSTANCE] = 0; 665bf215546Sopenharmony_ci } 666bf215546Sopenharmony_ci 667bf215546Sopenharmony_ci for (type = 0; type < VB_NUM; type++) { 668bf215546Sopenharmony_ci if (mask[type]) { 669bf215546Sopenharmony_ci mgr->dirty_real_vb_mask |= 1 << fallback_vbs[type]; 670bf215546Sopenharmony_ci } 671bf215546Sopenharmony_ci } 672bf215546Sopenharmony_ci 673bf215546Sopenharmony_ci memcpy(mgr->fallback_vbs, fallback_vbs, sizeof(fallback_vbs)); 674bf215546Sopenharmony_ci return TRUE; 675bf215546Sopenharmony_ci} 676bf215546Sopenharmony_ci 677bf215546Sopenharmony_cistatic boolean 678bf215546Sopenharmony_ciu_vbuf_translate_begin(struct u_vbuf *mgr, 679bf215546Sopenharmony_ci const struct pipe_draw_info *info, 680bf215546Sopenharmony_ci const struct pipe_draw_start_count_bias *draw, 681bf215546Sopenharmony_ci int start_vertex, unsigned num_vertices, 682bf215546Sopenharmony_ci int min_index, boolean unroll_indices, 683bf215546Sopenharmony_ci uint32_t misaligned) 684bf215546Sopenharmony_ci{ 685bf215546Sopenharmony_ci unsigned mask[VB_NUM] = {0}; 686bf215546Sopenharmony_ci struct translate_key key[VB_NUM]; 687bf215546Sopenharmony_ci unsigned elem_index[VB_NUM][PIPE_MAX_ATTRIBS]; /* ... into key.elements */ 688bf215546Sopenharmony_ci unsigned i, type; 689bf215546Sopenharmony_ci const unsigned incompatible_vb_mask = (misaligned | mgr->incompatible_vb_mask) & 690bf215546Sopenharmony_ci mgr->ve->used_vb_mask; 691bf215546Sopenharmony_ci 692bf215546Sopenharmony_ci const int start[VB_NUM] = { 693bf215546Sopenharmony_ci start_vertex, /* VERTEX */ 694bf215546Sopenharmony_ci info->start_instance, /* INSTANCE */ 695bf215546Sopenharmony_ci 0 /* CONST */ 696bf215546Sopenharmony_ci }; 697bf215546Sopenharmony_ci 698bf215546Sopenharmony_ci const unsigned num[VB_NUM] = { 699bf215546Sopenharmony_ci num_vertices, /* VERTEX */ 700bf215546Sopenharmony_ci info->instance_count, /* INSTANCE */ 701bf215546Sopenharmony_ci 1 /* CONST */ 702bf215546Sopenharmony_ci }; 703bf215546Sopenharmony_ci 704bf215546Sopenharmony_ci memset(key, 0, sizeof(key)); 705bf215546Sopenharmony_ci memset(elem_index, ~0, sizeof(elem_index)); 706bf215546Sopenharmony_ci 707bf215546Sopenharmony_ci /* See if there are vertex attribs of each type to translate and 708bf215546Sopenharmony_ci * which ones. */ 709bf215546Sopenharmony_ci for (i = 0; i < mgr->ve->count; i++) { 710bf215546Sopenharmony_ci unsigned vb_index = mgr->ve->ve[i].vertex_buffer_index; 711bf215546Sopenharmony_ci 712bf215546Sopenharmony_ci if (!mgr->vertex_buffer[vb_index].stride) { 713bf215546Sopenharmony_ci if (!(mgr->ve->incompatible_elem_mask & (1 << i)) && 714bf215546Sopenharmony_ci !(incompatible_vb_mask & (1 << vb_index))) { 715bf215546Sopenharmony_ci continue; 716bf215546Sopenharmony_ci } 717bf215546Sopenharmony_ci mask[VB_CONST] |= 1 << vb_index; 718bf215546Sopenharmony_ci } else if (mgr->ve->ve[i].instance_divisor) { 719bf215546Sopenharmony_ci if (!(mgr->ve->incompatible_elem_mask & (1 << i)) && 720bf215546Sopenharmony_ci !(incompatible_vb_mask & (1 << vb_index))) { 721bf215546Sopenharmony_ci continue; 722bf215546Sopenharmony_ci } 723bf215546Sopenharmony_ci mask[VB_INSTANCE] |= 1 << vb_index; 724bf215546Sopenharmony_ci } else { 725bf215546Sopenharmony_ci if (!unroll_indices && 726bf215546Sopenharmony_ci !(mgr->ve->incompatible_elem_mask & (1 << i)) && 727bf215546Sopenharmony_ci !(incompatible_vb_mask & (1 << vb_index))) { 728bf215546Sopenharmony_ci continue; 729bf215546Sopenharmony_ci } 730bf215546Sopenharmony_ci mask[VB_VERTEX] |= 1 << vb_index; 731bf215546Sopenharmony_ci } 732bf215546Sopenharmony_ci } 733bf215546Sopenharmony_ci 734bf215546Sopenharmony_ci assert(mask[VB_VERTEX] || mask[VB_INSTANCE] || mask[VB_CONST]); 735bf215546Sopenharmony_ci 736bf215546Sopenharmony_ci /* Find free vertex buffer slots. */ 737bf215546Sopenharmony_ci if (!u_vbuf_translate_find_free_vb_slots(mgr, mask)) { 738bf215546Sopenharmony_ci return FALSE; 739bf215546Sopenharmony_ci } 740bf215546Sopenharmony_ci 741bf215546Sopenharmony_ci unsigned min_alignment[VB_NUM] = {0}; 742bf215546Sopenharmony_ci /* Initialize the translate keys. */ 743bf215546Sopenharmony_ci for (i = 0; i < mgr->ve->count; i++) { 744bf215546Sopenharmony_ci struct translate_key *k; 745bf215546Sopenharmony_ci struct translate_element *te; 746bf215546Sopenharmony_ci enum pipe_format output_format = mgr->ve->native_format[i]; 747bf215546Sopenharmony_ci unsigned bit, vb_index = mgr->ve->ve[i].vertex_buffer_index; 748bf215546Sopenharmony_ci bit = 1 << vb_index; 749bf215546Sopenharmony_ci 750bf215546Sopenharmony_ci if (!(mgr->ve->incompatible_elem_mask & (1 << i)) && 751bf215546Sopenharmony_ci !(incompatible_vb_mask & (1 << vb_index)) && 752bf215546Sopenharmony_ci (!unroll_indices || !(mask[VB_VERTEX] & bit))) { 753bf215546Sopenharmony_ci continue; 754bf215546Sopenharmony_ci } 755bf215546Sopenharmony_ci 756bf215546Sopenharmony_ci /* Set type to what we will translate. 757bf215546Sopenharmony_ci * Whether vertex, instance, or constant attribs. */ 758bf215546Sopenharmony_ci for (type = 0; type < VB_NUM; type++) { 759bf215546Sopenharmony_ci if (mask[type] & bit) { 760bf215546Sopenharmony_ci break; 761bf215546Sopenharmony_ci } 762bf215546Sopenharmony_ci } 763bf215546Sopenharmony_ci assert(type < VB_NUM); 764bf215546Sopenharmony_ci if (mgr->ve->ve[i].src_format != output_format) 765bf215546Sopenharmony_ci assert(translate_is_output_format_supported(output_format)); 766bf215546Sopenharmony_ci /*printf("velem=%i type=%i\n", i, type);*/ 767bf215546Sopenharmony_ci 768bf215546Sopenharmony_ci /* Add the vertex element. */ 769bf215546Sopenharmony_ci k = &key[type]; 770bf215546Sopenharmony_ci elem_index[type][i] = k->nr_elements; 771bf215546Sopenharmony_ci 772bf215546Sopenharmony_ci te = &k->element[k->nr_elements]; 773bf215546Sopenharmony_ci te->type = TRANSLATE_ELEMENT_NORMAL; 774bf215546Sopenharmony_ci te->instance_divisor = 0; 775bf215546Sopenharmony_ci te->input_buffer = vb_index; 776bf215546Sopenharmony_ci te->input_format = mgr->ve->ve[i].src_format; 777bf215546Sopenharmony_ci te->input_offset = mgr->ve->ve[i].src_offset; 778bf215546Sopenharmony_ci te->output_format = output_format; 779bf215546Sopenharmony_ci te->output_offset = k->output_stride; 780bf215546Sopenharmony_ci unsigned adjustment = 0; 781bf215546Sopenharmony_ci if (!mgr->caps.attrib_component_unaligned && 782bf215546Sopenharmony_ci te->output_offset % mgr->ve->component_size[i] != 0) { 783bf215546Sopenharmony_ci unsigned aligned = align(te->output_offset, mgr->ve->component_size[i]); 784bf215546Sopenharmony_ci adjustment = aligned - te->output_offset; 785bf215546Sopenharmony_ci te->output_offset = aligned; 786bf215546Sopenharmony_ci } 787bf215546Sopenharmony_ci 788bf215546Sopenharmony_ci k->output_stride += mgr->ve->native_format_size[i] + adjustment; 789bf215546Sopenharmony_ci k->nr_elements++; 790bf215546Sopenharmony_ci min_alignment[type] = MAX2(min_alignment[type], mgr->ve->component_size[i]); 791bf215546Sopenharmony_ci } 792bf215546Sopenharmony_ci 793bf215546Sopenharmony_ci /* Translate buffers. */ 794bf215546Sopenharmony_ci for (type = 0; type < VB_NUM; type++) { 795bf215546Sopenharmony_ci if (key[type].nr_elements) { 796bf215546Sopenharmony_ci enum pipe_error err; 797bf215546Sopenharmony_ci if (!mgr->caps.attrib_component_unaligned) 798bf215546Sopenharmony_ci key[type].output_stride = align(key[type].output_stride, min_alignment[type]); 799bf215546Sopenharmony_ci err = u_vbuf_translate_buffers(mgr, &key[type], info, draw, 800bf215546Sopenharmony_ci mask[type], mgr->fallback_vbs[type], 801bf215546Sopenharmony_ci start[type], num[type], min_index, 802bf215546Sopenharmony_ci unroll_indices && type == VB_VERTEX); 803bf215546Sopenharmony_ci if (err != PIPE_OK) 804bf215546Sopenharmony_ci return FALSE; 805bf215546Sopenharmony_ci 806bf215546Sopenharmony_ci /* Fixup the stride for constant attribs. */ 807bf215546Sopenharmony_ci if (type == VB_CONST) { 808bf215546Sopenharmony_ci mgr->real_vertex_buffer[mgr->fallback_vbs[VB_CONST]].stride = 0; 809bf215546Sopenharmony_ci } 810bf215546Sopenharmony_ci } 811bf215546Sopenharmony_ci } 812bf215546Sopenharmony_ci 813bf215546Sopenharmony_ci /* Setup new vertex elements. */ 814bf215546Sopenharmony_ci for (i = 0; i < mgr->ve->count; i++) { 815bf215546Sopenharmony_ci for (type = 0; type < VB_NUM; type++) { 816bf215546Sopenharmony_ci if (elem_index[type][i] < key[type].nr_elements) { 817bf215546Sopenharmony_ci struct translate_element *te = &key[type].element[elem_index[type][i]]; 818bf215546Sopenharmony_ci mgr->fallback_velems.velems[i].instance_divisor = mgr->ve->ve[i].instance_divisor; 819bf215546Sopenharmony_ci mgr->fallback_velems.velems[i].src_format = te->output_format; 820bf215546Sopenharmony_ci mgr->fallback_velems.velems[i].src_offset = te->output_offset; 821bf215546Sopenharmony_ci mgr->fallback_velems.velems[i].vertex_buffer_index = mgr->fallback_vbs[type]; 822bf215546Sopenharmony_ci 823bf215546Sopenharmony_ci /* elem_index[type][i] can only be set for one type. */ 824bf215546Sopenharmony_ci assert(type > VB_INSTANCE || elem_index[type+1][i] == ~0u); 825bf215546Sopenharmony_ci assert(type > VB_VERTEX || elem_index[type+2][i] == ~0u); 826bf215546Sopenharmony_ci break; 827bf215546Sopenharmony_ci } 828bf215546Sopenharmony_ci } 829bf215546Sopenharmony_ci /* No translating, just copy the original vertex element over. */ 830bf215546Sopenharmony_ci if (type == VB_NUM) { 831bf215546Sopenharmony_ci memcpy(&mgr->fallback_velems.velems[i], &mgr->ve->ve[i], 832bf215546Sopenharmony_ci sizeof(struct pipe_vertex_element)); 833bf215546Sopenharmony_ci } 834bf215546Sopenharmony_ci } 835bf215546Sopenharmony_ci 836bf215546Sopenharmony_ci mgr->fallback_velems.count = mgr->ve->count; 837bf215546Sopenharmony_ci 838bf215546Sopenharmony_ci u_vbuf_set_vertex_elements_internal(mgr, &mgr->fallback_velems); 839bf215546Sopenharmony_ci mgr->using_translate = TRUE; 840bf215546Sopenharmony_ci return TRUE; 841bf215546Sopenharmony_ci} 842bf215546Sopenharmony_ci 843bf215546Sopenharmony_cistatic void u_vbuf_translate_end(struct u_vbuf *mgr) 844bf215546Sopenharmony_ci{ 845bf215546Sopenharmony_ci unsigned i; 846bf215546Sopenharmony_ci 847bf215546Sopenharmony_ci /* Restore vertex elements. */ 848bf215546Sopenharmony_ci mgr->pipe->bind_vertex_elements_state(mgr->pipe, mgr->ve->driver_cso); 849bf215546Sopenharmony_ci mgr->using_translate = FALSE; 850bf215546Sopenharmony_ci 851bf215546Sopenharmony_ci /* Unreference the now-unused VBOs. */ 852bf215546Sopenharmony_ci for (i = 0; i < VB_NUM; i++) { 853bf215546Sopenharmony_ci unsigned vb = mgr->fallback_vbs[i]; 854bf215546Sopenharmony_ci if (vb != ~0u) { 855bf215546Sopenharmony_ci pipe_resource_reference(&mgr->real_vertex_buffer[vb].buffer.resource, NULL); 856bf215546Sopenharmony_ci mgr->fallback_vbs[i] = ~0; 857bf215546Sopenharmony_ci } 858bf215546Sopenharmony_ci } 859bf215546Sopenharmony_ci /* This will cause the buffer to be unbound in the driver later. */ 860bf215546Sopenharmony_ci mgr->dirty_real_vb_mask |= mgr->fallback_vbs_mask; 861bf215546Sopenharmony_ci mgr->fallback_vbs_mask = 0; 862bf215546Sopenharmony_ci} 863bf215546Sopenharmony_ci 864bf215546Sopenharmony_cistatic void * 865bf215546Sopenharmony_ciu_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count, 866bf215546Sopenharmony_ci const struct pipe_vertex_element *attribs) 867bf215546Sopenharmony_ci{ 868bf215546Sopenharmony_ci struct pipe_vertex_element tmp[PIPE_MAX_ATTRIBS]; 869bf215546Sopenharmony_ci util_lower_uint64_vertex_elements(&attribs, &count, tmp); 870bf215546Sopenharmony_ci 871bf215546Sopenharmony_ci struct pipe_context *pipe = mgr->pipe; 872bf215546Sopenharmony_ci unsigned i; 873bf215546Sopenharmony_ci struct pipe_vertex_element driver_attribs[PIPE_MAX_ATTRIBS]; 874bf215546Sopenharmony_ci struct u_vbuf_elements *ve = CALLOC_STRUCT(u_vbuf_elements); 875bf215546Sopenharmony_ci uint32_t used_buffers = 0; 876bf215546Sopenharmony_ci 877bf215546Sopenharmony_ci ve->count = count; 878bf215546Sopenharmony_ci 879bf215546Sopenharmony_ci memcpy(ve->ve, attribs, sizeof(struct pipe_vertex_element) * count); 880bf215546Sopenharmony_ci memcpy(driver_attribs, attribs, sizeof(struct pipe_vertex_element) * count); 881bf215546Sopenharmony_ci 882bf215546Sopenharmony_ci /* Set the best native format in case the original format is not 883bf215546Sopenharmony_ci * supported. */ 884bf215546Sopenharmony_ci for (i = 0; i < count; i++) { 885bf215546Sopenharmony_ci enum pipe_format format = ve->ve[i].src_format; 886bf215546Sopenharmony_ci unsigned vb_index_bit = 1 << ve->ve[i].vertex_buffer_index; 887bf215546Sopenharmony_ci 888bf215546Sopenharmony_ci ve->src_format_size[i] = util_format_get_blocksize(format); 889bf215546Sopenharmony_ci 890bf215546Sopenharmony_ci if (used_buffers & vb_index_bit) 891bf215546Sopenharmony_ci ve->interleaved_vb_mask |= vb_index_bit; 892bf215546Sopenharmony_ci 893bf215546Sopenharmony_ci used_buffers |= vb_index_bit; 894bf215546Sopenharmony_ci 895bf215546Sopenharmony_ci if (!ve->ve[i].instance_divisor) { 896bf215546Sopenharmony_ci ve->noninstance_vb_mask_any |= vb_index_bit; 897bf215546Sopenharmony_ci } 898bf215546Sopenharmony_ci 899bf215546Sopenharmony_ci format = mgr->caps.format_translation[format]; 900bf215546Sopenharmony_ci 901bf215546Sopenharmony_ci driver_attribs[i].src_format = format; 902bf215546Sopenharmony_ci ve->native_format[i] = format; 903bf215546Sopenharmony_ci ve->native_format_size[i] = 904bf215546Sopenharmony_ci util_format_get_blocksize(ve->native_format[i]); 905bf215546Sopenharmony_ci 906bf215546Sopenharmony_ci const struct util_format_description *desc = util_format_description(format); 907bf215546Sopenharmony_ci bool is_packed = false; 908bf215546Sopenharmony_ci for (unsigned c = 0; c < desc->nr_channels; c++) 909bf215546Sopenharmony_ci is_packed |= desc->channel[c].size != desc->channel[0].size || desc->channel[c].size % 8 != 0; 910bf215546Sopenharmony_ci unsigned component_size = is_packed ? 911bf215546Sopenharmony_ci ve->native_format_size[i] : (ve->native_format_size[i] / desc->nr_channels); 912bf215546Sopenharmony_ci ve->component_size[i] = component_size; 913bf215546Sopenharmony_ci 914bf215546Sopenharmony_ci if (ve->ve[i].src_format != format || 915bf215546Sopenharmony_ci (!mgr->caps.velem_src_offset_unaligned && 916bf215546Sopenharmony_ci ve->ve[i].src_offset % 4 != 0) || 917bf215546Sopenharmony_ci (!mgr->caps.attrib_component_unaligned && 918bf215546Sopenharmony_ci ve->ve[i].src_offset % component_size != 0)) { 919bf215546Sopenharmony_ci ve->incompatible_elem_mask |= 1 << i; 920bf215546Sopenharmony_ci ve->incompatible_vb_mask_any |= vb_index_bit; 921bf215546Sopenharmony_ci } else { 922bf215546Sopenharmony_ci ve->compatible_vb_mask_any |= vb_index_bit; 923bf215546Sopenharmony_ci if (component_size == 2) 924bf215546Sopenharmony_ci ve->vb_align_mask[0] |= vb_index_bit; 925bf215546Sopenharmony_ci else if (component_size == 4) 926bf215546Sopenharmony_ci ve->vb_align_mask[1] |= vb_index_bit; 927bf215546Sopenharmony_ci } 928bf215546Sopenharmony_ci } 929bf215546Sopenharmony_ci 930bf215546Sopenharmony_ci if (used_buffers & ~mgr->allowed_vb_mask) { 931bf215546Sopenharmony_ci /* More vertex buffers are used than the hardware supports. In 932bf215546Sopenharmony_ci * principle, we only need to make sure that less vertex buffers are 933bf215546Sopenharmony_ci * used, and mark some of the latter vertex buffers as incompatible. 934bf215546Sopenharmony_ci * For now, mark all vertex buffers as incompatible. 935bf215546Sopenharmony_ci */ 936bf215546Sopenharmony_ci ve->incompatible_vb_mask_any = used_buffers; 937bf215546Sopenharmony_ci ve->compatible_vb_mask_any = 0; 938bf215546Sopenharmony_ci ve->incompatible_elem_mask = u_bit_consecutive(0, count); 939bf215546Sopenharmony_ci } 940bf215546Sopenharmony_ci 941bf215546Sopenharmony_ci ve->used_vb_mask = used_buffers; 942bf215546Sopenharmony_ci ve->compatible_vb_mask_all = ~ve->incompatible_vb_mask_any & used_buffers; 943bf215546Sopenharmony_ci ve->incompatible_vb_mask_all = ~ve->compatible_vb_mask_any & used_buffers; 944bf215546Sopenharmony_ci 945bf215546Sopenharmony_ci /* Align the formats and offsets to the size of DWORD if needed. */ 946bf215546Sopenharmony_ci if (!mgr->caps.velem_src_offset_unaligned) { 947bf215546Sopenharmony_ci for (i = 0; i < count; i++) { 948bf215546Sopenharmony_ci ve->native_format_size[i] = align(ve->native_format_size[i], 4); 949bf215546Sopenharmony_ci driver_attribs[i].src_offset = align(ve->ve[i].src_offset, 4); 950bf215546Sopenharmony_ci } 951bf215546Sopenharmony_ci } 952bf215546Sopenharmony_ci 953bf215546Sopenharmony_ci /* Only create driver CSO if no incompatible elements */ 954bf215546Sopenharmony_ci if (!ve->incompatible_elem_mask) { 955bf215546Sopenharmony_ci ve->driver_cso = 956bf215546Sopenharmony_ci pipe->create_vertex_elements_state(pipe, count, driver_attribs); 957bf215546Sopenharmony_ci } 958bf215546Sopenharmony_ci 959bf215546Sopenharmony_ci return ve; 960bf215546Sopenharmony_ci} 961bf215546Sopenharmony_ci 962bf215546Sopenharmony_cistatic void u_vbuf_delete_vertex_elements(void *ctx, void *state, 963bf215546Sopenharmony_ci enum cso_cache_type type) 964bf215546Sopenharmony_ci{ 965bf215546Sopenharmony_ci struct pipe_context *pipe = (struct pipe_context*)ctx; 966bf215546Sopenharmony_ci struct cso_velements *cso = (struct cso_velements*)state; 967bf215546Sopenharmony_ci struct u_vbuf_elements *ve = (struct u_vbuf_elements*)cso->data; 968bf215546Sopenharmony_ci 969bf215546Sopenharmony_ci if (ve->driver_cso) 970bf215546Sopenharmony_ci pipe->delete_vertex_elements_state(pipe, ve->driver_cso); 971bf215546Sopenharmony_ci FREE(ve); 972bf215546Sopenharmony_ci FREE(cso); 973bf215546Sopenharmony_ci} 974bf215546Sopenharmony_ci 975bf215546Sopenharmony_civoid u_vbuf_set_vertex_buffers(struct u_vbuf *mgr, 976bf215546Sopenharmony_ci unsigned start_slot, unsigned count, 977bf215546Sopenharmony_ci unsigned unbind_num_trailing_slots, 978bf215546Sopenharmony_ci bool take_ownership, 979bf215546Sopenharmony_ci const struct pipe_vertex_buffer *bufs) 980bf215546Sopenharmony_ci{ 981bf215546Sopenharmony_ci unsigned i; 982bf215546Sopenharmony_ci /* which buffers are enabled */ 983bf215546Sopenharmony_ci uint32_t enabled_vb_mask = 0; 984bf215546Sopenharmony_ci /* which buffers are in user memory */ 985bf215546Sopenharmony_ci uint32_t user_vb_mask = 0; 986bf215546Sopenharmony_ci /* which buffers are incompatible with the driver */ 987bf215546Sopenharmony_ci uint32_t incompatible_vb_mask = 0; 988bf215546Sopenharmony_ci /* which buffers have a non-zero stride */ 989bf215546Sopenharmony_ci uint32_t nonzero_stride_vb_mask = 0; 990bf215546Sopenharmony_ci /* which buffers are unaligned to 2/4 bytes */ 991bf215546Sopenharmony_ci uint32_t unaligned_vb_mask[2] = {0}; 992bf215546Sopenharmony_ci uint32_t mask = 993bf215546Sopenharmony_ci ~(((1ull << (count + unbind_num_trailing_slots)) - 1) << start_slot); 994bf215546Sopenharmony_ci 995bf215546Sopenharmony_ci if (!bufs) { 996bf215546Sopenharmony_ci struct pipe_context *pipe = mgr->pipe; 997bf215546Sopenharmony_ci /* Unbind. */ 998bf215546Sopenharmony_ci unsigned total_count = count + unbind_num_trailing_slots; 999bf215546Sopenharmony_ci mgr->dirty_real_vb_mask &= mask; 1000bf215546Sopenharmony_ci 1001bf215546Sopenharmony_ci /* Zero out the bits we are going to rewrite completely. */ 1002bf215546Sopenharmony_ci mgr->user_vb_mask &= mask; 1003bf215546Sopenharmony_ci mgr->incompatible_vb_mask &= mask; 1004bf215546Sopenharmony_ci mgr->nonzero_stride_vb_mask &= mask; 1005bf215546Sopenharmony_ci mgr->enabled_vb_mask &= mask; 1006bf215546Sopenharmony_ci mgr->unaligned_vb_mask[0] &= mask; 1007bf215546Sopenharmony_ci mgr->unaligned_vb_mask[1] &= mask; 1008bf215546Sopenharmony_ci 1009bf215546Sopenharmony_ci for (i = 0; i < total_count; i++) { 1010bf215546Sopenharmony_ci unsigned dst_index = start_slot + i; 1011bf215546Sopenharmony_ci 1012bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(&mgr->vertex_buffer[dst_index]); 1013bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[dst_index]); 1014bf215546Sopenharmony_ci } 1015bf215546Sopenharmony_ci 1016bf215546Sopenharmony_ci pipe->set_vertex_buffers(pipe, start_slot, count, 1017bf215546Sopenharmony_ci unbind_num_trailing_slots, false, NULL); 1018bf215546Sopenharmony_ci return; 1019bf215546Sopenharmony_ci } 1020bf215546Sopenharmony_ci 1021bf215546Sopenharmony_ci for (i = 0; i < count; i++) { 1022bf215546Sopenharmony_ci unsigned dst_index = start_slot + i; 1023bf215546Sopenharmony_ci const struct pipe_vertex_buffer *vb = &bufs[i]; 1024bf215546Sopenharmony_ci struct pipe_vertex_buffer *orig_vb = &mgr->vertex_buffer[dst_index]; 1025bf215546Sopenharmony_ci struct pipe_vertex_buffer *real_vb = &mgr->real_vertex_buffer[dst_index]; 1026bf215546Sopenharmony_ci 1027bf215546Sopenharmony_ci if (!vb->buffer.resource) { 1028bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(orig_vb); 1029bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(real_vb); 1030bf215546Sopenharmony_ci continue; 1031bf215546Sopenharmony_ci } 1032bf215546Sopenharmony_ci 1033bf215546Sopenharmony_ci bool not_user = !vb->is_user_buffer && vb->is_user_buffer == orig_vb->is_user_buffer; 1034bf215546Sopenharmony_ci /* struct isn't tightly packed: do not use memcmp */ 1035bf215546Sopenharmony_ci if (not_user && orig_vb->stride == vb->stride && 1036bf215546Sopenharmony_ci orig_vb->buffer_offset == vb->buffer_offset && orig_vb->buffer.resource == vb->buffer.resource) { 1037bf215546Sopenharmony_ci mask |= BITFIELD_BIT(dst_index); 1038bf215546Sopenharmony_ci if (take_ownership) { 1039bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(orig_vb); 1040bf215546Sopenharmony_ci /* the pointer was unset in the line above, so copy it back */ 1041bf215546Sopenharmony_ci orig_vb->buffer.resource = vb->buffer.resource; 1042bf215546Sopenharmony_ci } 1043bf215546Sopenharmony_ci if (mask == UINT32_MAX) 1044bf215546Sopenharmony_ci return; 1045bf215546Sopenharmony_ci continue; 1046bf215546Sopenharmony_ci } 1047bf215546Sopenharmony_ci 1048bf215546Sopenharmony_ci if (take_ownership) { 1049bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(orig_vb); 1050bf215546Sopenharmony_ci memcpy(orig_vb, vb, sizeof(*vb)); 1051bf215546Sopenharmony_ci } else { 1052bf215546Sopenharmony_ci pipe_vertex_buffer_reference(orig_vb, vb); 1053bf215546Sopenharmony_ci } 1054bf215546Sopenharmony_ci 1055bf215546Sopenharmony_ci if (vb->stride) { 1056bf215546Sopenharmony_ci nonzero_stride_vb_mask |= 1 << dst_index; 1057bf215546Sopenharmony_ci } 1058bf215546Sopenharmony_ci enabled_vb_mask |= 1 << dst_index; 1059bf215546Sopenharmony_ci 1060bf215546Sopenharmony_ci if ((!mgr->caps.buffer_offset_unaligned && vb->buffer_offset % 4 != 0) || 1061bf215546Sopenharmony_ci (!mgr->caps.buffer_stride_unaligned && vb->stride % 4 != 0)) { 1062bf215546Sopenharmony_ci incompatible_vb_mask |= 1 << dst_index; 1063bf215546Sopenharmony_ci real_vb->buffer_offset = vb->buffer_offset; 1064bf215546Sopenharmony_ci real_vb->stride = vb->stride; 1065bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(real_vb); 1066bf215546Sopenharmony_ci real_vb->is_user_buffer = false; 1067bf215546Sopenharmony_ci continue; 1068bf215546Sopenharmony_ci } 1069bf215546Sopenharmony_ci 1070bf215546Sopenharmony_ci if (!mgr->caps.attrib_component_unaligned) { 1071bf215546Sopenharmony_ci if (vb->buffer_offset % 2 != 0 || vb->stride % 2 != 0) 1072bf215546Sopenharmony_ci unaligned_vb_mask[0] |= BITFIELD_BIT(dst_index); 1073bf215546Sopenharmony_ci if (vb->buffer_offset % 4 != 0 || vb->stride % 4 != 0) 1074bf215546Sopenharmony_ci unaligned_vb_mask[1] |= BITFIELD_BIT(dst_index); 1075bf215546Sopenharmony_ci } 1076bf215546Sopenharmony_ci 1077bf215546Sopenharmony_ci if (!mgr->caps.user_vertex_buffers && vb->is_user_buffer) { 1078bf215546Sopenharmony_ci user_vb_mask |= 1 << dst_index; 1079bf215546Sopenharmony_ci real_vb->buffer_offset = vb->buffer_offset; 1080bf215546Sopenharmony_ci real_vb->stride = vb->stride; 1081bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(real_vb); 1082bf215546Sopenharmony_ci real_vb->is_user_buffer = false; 1083bf215546Sopenharmony_ci continue; 1084bf215546Sopenharmony_ci } 1085bf215546Sopenharmony_ci 1086bf215546Sopenharmony_ci pipe_vertex_buffer_reference(real_vb, vb); 1087bf215546Sopenharmony_ci } 1088bf215546Sopenharmony_ci 1089bf215546Sopenharmony_ci for (i = 0; i < unbind_num_trailing_slots; i++) { 1090bf215546Sopenharmony_ci unsigned dst_index = start_slot + count + i; 1091bf215546Sopenharmony_ci 1092bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(&mgr->vertex_buffer[dst_index]); 1093bf215546Sopenharmony_ci pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[dst_index]); 1094bf215546Sopenharmony_ci } 1095bf215546Sopenharmony_ci 1096bf215546Sopenharmony_ci 1097bf215546Sopenharmony_ci /* Zero out the bits we are going to rewrite completely. */ 1098bf215546Sopenharmony_ci mgr->user_vb_mask &= mask; 1099bf215546Sopenharmony_ci mgr->incompatible_vb_mask &= mask; 1100bf215546Sopenharmony_ci mgr->nonzero_stride_vb_mask &= mask; 1101bf215546Sopenharmony_ci mgr->enabled_vb_mask &= mask; 1102bf215546Sopenharmony_ci mgr->unaligned_vb_mask[0] &= mask; 1103bf215546Sopenharmony_ci mgr->unaligned_vb_mask[1] &= mask; 1104bf215546Sopenharmony_ci 1105bf215546Sopenharmony_ci mgr->user_vb_mask |= user_vb_mask; 1106bf215546Sopenharmony_ci mgr->incompatible_vb_mask |= incompatible_vb_mask; 1107bf215546Sopenharmony_ci mgr->nonzero_stride_vb_mask |= nonzero_stride_vb_mask; 1108bf215546Sopenharmony_ci mgr->enabled_vb_mask |= enabled_vb_mask; 1109bf215546Sopenharmony_ci mgr->unaligned_vb_mask[0] |= unaligned_vb_mask[0]; 1110bf215546Sopenharmony_ci mgr->unaligned_vb_mask[1] |= unaligned_vb_mask[1]; 1111bf215546Sopenharmony_ci 1112bf215546Sopenharmony_ci /* All changed buffers are marked as dirty, even the NULL ones, 1113bf215546Sopenharmony_ci * which will cause the NULL buffers to be unbound in the driver later. */ 1114bf215546Sopenharmony_ci mgr->dirty_real_vb_mask |= ~mask; 1115bf215546Sopenharmony_ci} 1116bf215546Sopenharmony_ci 1117bf215546Sopenharmony_cistatic ALWAYS_INLINE bool 1118bf215546Sopenharmony_ciget_upload_offset_size(struct u_vbuf *mgr, 1119bf215546Sopenharmony_ci const struct pipe_vertex_buffer *vb, 1120bf215546Sopenharmony_ci struct u_vbuf_elements *ve, 1121bf215546Sopenharmony_ci const struct pipe_vertex_element *velem, 1122bf215546Sopenharmony_ci unsigned vb_index, unsigned velem_index, 1123bf215546Sopenharmony_ci int start_vertex, unsigned num_vertices, 1124bf215546Sopenharmony_ci int start_instance, unsigned num_instances, 1125bf215546Sopenharmony_ci unsigned *offset, unsigned *size) 1126bf215546Sopenharmony_ci{ 1127bf215546Sopenharmony_ci /* Skip the buffers generated by translate. */ 1128bf215546Sopenharmony_ci if ((1 << vb_index) & mgr->fallback_vbs_mask || !vb->is_user_buffer) 1129bf215546Sopenharmony_ci return false; 1130bf215546Sopenharmony_ci 1131bf215546Sopenharmony_ci unsigned instance_div = velem->instance_divisor; 1132bf215546Sopenharmony_ci *offset = vb->buffer_offset + velem->src_offset; 1133bf215546Sopenharmony_ci 1134bf215546Sopenharmony_ci if (!vb->stride) { 1135bf215546Sopenharmony_ci /* Constant attrib. */ 1136bf215546Sopenharmony_ci *size = ve->src_format_size[velem_index]; 1137bf215546Sopenharmony_ci } else if (instance_div) { 1138bf215546Sopenharmony_ci /* Per-instance attrib. */ 1139bf215546Sopenharmony_ci 1140bf215546Sopenharmony_ci /* Figure out how many instances we'll render given instance_div. We 1141bf215546Sopenharmony_ci * can't use the typical div_round_up() pattern because the CTS uses 1142bf215546Sopenharmony_ci * instance_div = ~0 for a test, which overflows div_round_up()'s 1143bf215546Sopenharmony_ci * addition. 1144bf215546Sopenharmony_ci */ 1145bf215546Sopenharmony_ci unsigned count = num_instances / instance_div; 1146bf215546Sopenharmony_ci if (count * instance_div != num_instances) 1147bf215546Sopenharmony_ci count++; 1148bf215546Sopenharmony_ci 1149bf215546Sopenharmony_ci *offset += vb->stride * start_instance; 1150bf215546Sopenharmony_ci *size = vb->stride * (count - 1) + ve->src_format_size[velem_index]; 1151bf215546Sopenharmony_ci } else { 1152bf215546Sopenharmony_ci /* Per-vertex attrib. */ 1153bf215546Sopenharmony_ci *offset += vb->stride * start_vertex; 1154bf215546Sopenharmony_ci *size = vb->stride * (num_vertices - 1) + ve->src_format_size[velem_index]; 1155bf215546Sopenharmony_ci } 1156bf215546Sopenharmony_ci return true; 1157bf215546Sopenharmony_ci} 1158bf215546Sopenharmony_ci 1159bf215546Sopenharmony_ci 1160bf215546Sopenharmony_cistatic enum pipe_error 1161bf215546Sopenharmony_ciu_vbuf_upload_buffers(struct u_vbuf *mgr, 1162bf215546Sopenharmony_ci int start_vertex, unsigned num_vertices, 1163bf215546Sopenharmony_ci int start_instance, unsigned num_instances) 1164bf215546Sopenharmony_ci{ 1165bf215546Sopenharmony_ci unsigned i; 1166bf215546Sopenharmony_ci struct u_vbuf_elements *ve = mgr->ve; 1167bf215546Sopenharmony_ci unsigned nr_velems = ve->count; 1168bf215546Sopenharmony_ci const struct pipe_vertex_element *velems = 1169bf215546Sopenharmony_ci mgr->using_translate ? mgr->fallback_velems.velems : ve->ve; 1170bf215546Sopenharmony_ci 1171bf215546Sopenharmony_ci /* Faster path when no vertex attribs are interleaved. */ 1172bf215546Sopenharmony_ci if ((ve->interleaved_vb_mask & mgr->user_vb_mask) == 0) { 1173bf215546Sopenharmony_ci for (i = 0; i < nr_velems; i++) { 1174bf215546Sopenharmony_ci const struct pipe_vertex_element *velem = &velems[i]; 1175bf215546Sopenharmony_ci unsigned index = velem->vertex_buffer_index; 1176bf215546Sopenharmony_ci struct pipe_vertex_buffer *vb = &mgr->vertex_buffer[index]; 1177bf215546Sopenharmony_ci unsigned offset, size; 1178bf215546Sopenharmony_ci 1179bf215546Sopenharmony_ci if (!get_upload_offset_size(mgr, vb, ve, velem, index, i, start_vertex, 1180bf215546Sopenharmony_ci num_vertices, start_instance, num_instances, 1181bf215546Sopenharmony_ci &offset, &size)) 1182bf215546Sopenharmony_ci continue; 1183bf215546Sopenharmony_ci 1184bf215546Sopenharmony_ci struct pipe_vertex_buffer *real_vb = &mgr->real_vertex_buffer[index]; 1185bf215546Sopenharmony_ci const uint8_t *ptr = mgr->vertex_buffer[index].buffer.user; 1186bf215546Sopenharmony_ci 1187bf215546Sopenharmony_ci u_upload_data(mgr->pipe->stream_uploader, 1188bf215546Sopenharmony_ci mgr->has_signed_vb_offset ? 0 : offset, 1189bf215546Sopenharmony_ci size, 4, ptr + offset, &real_vb->buffer_offset, 1190bf215546Sopenharmony_ci &real_vb->buffer.resource); 1191bf215546Sopenharmony_ci if (!real_vb->buffer.resource) 1192bf215546Sopenharmony_ci return PIPE_ERROR_OUT_OF_MEMORY; 1193bf215546Sopenharmony_ci 1194bf215546Sopenharmony_ci real_vb->buffer_offset -= offset; 1195bf215546Sopenharmony_ci } 1196bf215546Sopenharmony_ci return PIPE_OK; 1197bf215546Sopenharmony_ci } 1198bf215546Sopenharmony_ci 1199bf215546Sopenharmony_ci unsigned start_offset[PIPE_MAX_ATTRIBS]; 1200bf215546Sopenharmony_ci unsigned end_offset[PIPE_MAX_ATTRIBS]; 1201bf215546Sopenharmony_ci uint32_t buffer_mask = 0; 1202bf215546Sopenharmony_ci 1203bf215546Sopenharmony_ci /* Slower path supporting interleaved vertex attribs using 2 loops. */ 1204bf215546Sopenharmony_ci /* Determine how much data needs to be uploaded. */ 1205bf215546Sopenharmony_ci for (i = 0; i < nr_velems; i++) { 1206bf215546Sopenharmony_ci const struct pipe_vertex_element *velem = &velems[i]; 1207bf215546Sopenharmony_ci unsigned index = velem->vertex_buffer_index; 1208bf215546Sopenharmony_ci struct pipe_vertex_buffer *vb = &mgr->vertex_buffer[index]; 1209bf215546Sopenharmony_ci unsigned first, size, index_bit; 1210bf215546Sopenharmony_ci 1211bf215546Sopenharmony_ci if (!get_upload_offset_size(mgr, vb, ve, velem, index, i, start_vertex, 1212bf215546Sopenharmony_ci num_vertices, start_instance, num_instances, 1213bf215546Sopenharmony_ci &first, &size)) 1214bf215546Sopenharmony_ci continue; 1215bf215546Sopenharmony_ci 1216bf215546Sopenharmony_ci index_bit = 1 << index; 1217bf215546Sopenharmony_ci 1218bf215546Sopenharmony_ci /* Update offsets. */ 1219bf215546Sopenharmony_ci if (!(buffer_mask & index_bit)) { 1220bf215546Sopenharmony_ci start_offset[index] = first; 1221bf215546Sopenharmony_ci end_offset[index] = first + size; 1222bf215546Sopenharmony_ci } else { 1223bf215546Sopenharmony_ci if (first < start_offset[index]) 1224bf215546Sopenharmony_ci start_offset[index] = first; 1225bf215546Sopenharmony_ci if (first + size > end_offset[index]) 1226bf215546Sopenharmony_ci end_offset[index] = first + size; 1227bf215546Sopenharmony_ci } 1228bf215546Sopenharmony_ci 1229bf215546Sopenharmony_ci buffer_mask |= index_bit; 1230bf215546Sopenharmony_ci } 1231bf215546Sopenharmony_ci 1232bf215546Sopenharmony_ci /* Upload buffers. */ 1233bf215546Sopenharmony_ci while (buffer_mask) { 1234bf215546Sopenharmony_ci unsigned start, end; 1235bf215546Sopenharmony_ci struct pipe_vertex_buffer *real_vb; 1236bf215546Sopenharmony_ci const uint8_t *ptr; 1237bf215546Sopenharmony_ci 1238bf215546Sopenharmony_ci i = u_bit_scan(&buffer_mask); 1239bf215546Sopenharmony_ci 1240bf215546Sopenharmony_ci start = start_offset[i]; 1241bf215546Sopenharmony_ci end = end_offset[i]; 1242bf215546Sopenharmony_ci assert(start < end); 1243bf215546Sopenharmony_ci 1244bf215546Sopenharmony_ci real_vb = &mgr->real_vertex_buffer[i]; 1245bf215546Sopenharmony_ci ptr = mgr->vertex_buffer[i].buffer.user; 1246bf215546Sopenharmony_ci 1247bf215546Sopenharmony_ci u_upload_data(mgr->pipe->stream_uploader, 1248bf215546Sopenharmony_ci mgr->has_signed_vb_offset ? 0 : start, 1249bf215546Sopenharmony_ci end - start, 4, 1250bf215546Sopenharmony_ci ptr + start, &real_vb->buffer_offset, &real_vb->buffer.resource); 1251bf215546Sopenharmony_ci if (!real_vb->buffer.resource) 1252bf215546Sopenharmony_ci return PIPE_ERROR_OUT_OF_MEMORY; 1253bf215546Sopenharmony_ci 1254bf215546Sopenharmony_ci real_vb->buffer_offset -= start; 1255bf215546Sopenharmony_ci } 1256bf215546Sopenharmony_ci 1257bf215546Sopenharmony_ci return PIPE_OK; 1258bf215546Sopenharmony_ci} 1259bf215546Sopenharmony_ci 1260bf215546Sopenharmony_cistatic boolean u_vbuf_need_minmax_index(const struct u_vbuf *mgr, uint32_t misaligned) 1261bf215546Sopenharmony_ci{ 1262bf215546Sopenharmony_ci /* See if there are any per-vertex attribs which will be uploaded or 1263bf215546Sopenharmony_ci * translated. Use bitmasks to get the info instead of looping over vertex 1264bf215546Sopenharmony_ci * elements. */ 1265bf215546Sopenharmony_ci return (mgr->ve->used_vb_mask & 1266bf215546Sopenharmony_ci ((mgr->user_vb_mask | 1267bf215546Sopenharmony_ci mgr->incompatible_vb_mask | 1268bf215546Sopenharmony_ci misaligned | 1269bf215546Sopenharmony_ci mgr->ve->incompatible_vb_mask_any) & 1270bf215546Sopenharmony_ci mgr->ve->noninstance_vb_mask_any & 1271bf215546Sopenharmony_ci mgr->nonzero_stride_vb_mask)) != 0; 1272bf215546Sopenharmony_ci} 1273bf215546Sopenharmony_ci 1274bf215546Sopenharmony_cistatic boolean u_vbuf_mapping_vertex_buffer_blocks(const struct u_vbuf *mgr, uint32_t misaligned) 1275bf215546Sopenharmony_ci{ 1276bf215546Sopenharmony_ci /* Return true if there are hw buffers which don't need to be translated. 1277bf215546Sopenharmony_ci * 1278bf215546Sopenharmony_ci * We could query whether each buffer is busy, but that would 1279bf215546Sopenharmony_ci * be way more costly than this. */ 1280bf215546Sopenharmony_ci return (mgr->ve->used_vb_mask & 1281bf215546Sopenharmony_ci (~mgr->user_vb_mask & 1282bf215546Sopenharmony_ci ~mgr->incompatible_vb_mask & 1283bf215546Sopenharmony_ci ~misaligned & 1284bf215546Sopenharmony_ci mgr->ve->compatible_vb_mask_all & 1285bf215546Sopenharmony_ci mgr->ve->noninstance_vb_mask_any & 1286bf215546Sopenharmony_ci mgr->nonzero_stride_vb_mask)) != 0; 1287bf215546Sopenharmony_ci} 1288bf215546Sopenharmony_ci 1289bf215546Sopenharmony_cistatic void 1290bf215546Sopenharmony_ciu_vbuf_get_minmax_index_mapped(const struct pipe_draw_info *info, 1291bf215546Sopenharmony_ci unsigned count, 1292bf215546Sopenharmony_ci const void *indices, unsigned *out_min_index, 1293bf215546Sopenharmony_ci unsigned *out_max_index) 1294bf215546Sopenharmony_ci{ 1295bf215546Sopenharmony_ci if (!count) { 1296bf215546Sopenharmony_ci *out_min_index = 0; 1297bf215546Sopenharmony_ci *out_max_index = 0; 1298bf215546Sopenharmony_ci return; 1299bf215546Sopenharmony_ci } 1300bf215546Sopenharmony_ci 1301bf215546Sopenharmony_ci switch (info->index_size) { 1302bf215546Sopenharmony_ci case 4: { 1303bf215546Sopenharmony_ci const unsigned *ui_indices = (const unsigned*)indices; 1304bf215546Sopenharmony_ci unsigned max = 0; 1305bf215546Sopenharmony_ci unsigned min = ~0u; 1306bf215546Sopenharmony_ci if (info->primitive_restart) { 1307bf215546Sopenharmony_ci for (unsigned i = 0; i < count; i++) { 1308bf215546Sopenharmony_ci if (ui_indices[i] != info->restart_index) { 1309bf215546Sopenharmony_ci if (ui_indices[i] > max) max = ui_indices[i]; 1310bf215546Sopenharmony_ci if (ui_indices[i] < min) min = ui_indices[i]; 1311bf215546Sopenharmony_ci } 1312bf215546Sopenharmony_ci } 1313bf215546Sopenharmony_ci } 1314bf215546Sopenharmony_ci else { 1315bf215546Sopenharmony_ci for (unsigned i = 0; i < count; i++) { 1316bf215546Sopenharmony_ci if (ui_indices[i] > max) max = ui_indices[i]; 1317bf215546Sopenharmony_ci if (ui_indices[i] < min) min = ui_indices[i]; 1318bf215546Sopenharmony_ci } 1319bf215546Sopenharmony_ci } 1320bf215546Sopenharmony_ci *out_min_index = min; 1321bf215546Sopenharmony_ci *out_max_index = max; 1322bf215546Sopenharmony_ci break; 1323bf215546Sopenharmony_ci } 1324bf215546Sopenharmony_ci case 2: { 1325bf215546Sopenharmony_ci const unsigned short *us_indices = (const unsigned short*)indices; 1326bf215546Sopenharmony_ci unsigned short max = 0; 1327bf215546Sopenharmony_ci unsigned short min = ~((unsigned short)0); 1328bf215546Sopenharmony_ci if (info->primitive_restart) { 1329bf215546Sopenharmony_ci for (unsigned i = 0; i < count; i++) { 1330bf215546Sopenharmony_ci if (us_indices[i] != info->restart_index) { 1331bf215546Sopenharmony_ci if (us_indices[i] > max) max = us_indices[i]; 1332bf215546Sopenharmony_ci if (us_indices[i] < min) min = us_indices[i]; 1333bf215546Sopenharmony_ci } 1334bf215546Sopenharmony_ci } 1335bf215546Sopenharmony_ci } 1336bf215546Sopenharmony_ci else { 1337bf215546Sopenharmony_ci for (unsigned i = 0; i < count; i++) { 1338bf215546Sopenharmony_ci if (us_indices[i] > max) max = us_indices[i]; 1339bf215546Sopenharmony_ci if (us_indices[i] < min) min = us_indices[i]; 1340bf215546Sopenharmony_ci } 1341bf215546Sopenharmony_ci } 1342bf215546Sopenharmony_ci *out_min_index = min; 1343bf215546Sopenharmony_ci *out_max_index = max; 1344bf215546Sopenharmony_ci break; 1345bf215546Sopenharmony_ci } 1346bf215546Sopenharmony_ci case 1: { 1347bf215546Sopenharmony_ci const unsigned char *ub_indices = (const unsigned char*)indices; 1348bf215546Sopenharmony_ci unsigned char max = 0; 1349bf215546Sopenharmony_ci unsigned char min = ~((unsigned char)0); 1350bf215546Sopenharmony_ci if (info->primitive_restart) { 1351bf215546Sopenharmony_ci for (unsigned i = 0; i < count; i++) { 1352bf215546Sopenharmony_ci if (ub_indices[i] != info->restart_index) { 1353bf215546Sopenharmony_ci if (ub_indices[i] > max) max = ub_indices[i]; 1354bf215546Sopenharmony_ci if (ub_indices[i] < min) min = ub_indices[i]; 1355bf215546Sopenharmony_ci } 1356bf215546Sopenharmony_ci } 1357bf215546Sopenharmony_ci } 1358bf215546Sopenharmony_ci else { 1359bf215546Sopenharmony_ci for (unsigned i = 0; i < count; i++) { 1360bf215546Sopenharmony_ci if (ub_indices[i] > max) max = ub_indices[i]; 1361bf215546Sopenharmony_ci if (ub_indices[i] < min) min = ub_indices[i]; 1362bf215546Sopenharmony_ci } 1363bf215546Sopenharmony_ci } 1364bf215546Sopenharmony_ci *out_min_index = min; 1365bf215546Sopenharmony_ci *out_max_index = max; 1366bf215546Sopenharmony_ci break; 1367bf215546Sopenharmony_ci } 1368bf215546Sopenharmony_ci default: 1369bf215546Sopenharmony_ci unreachable("bad index size"); 1370bf215546Sopenharmony_ci } 1371bf215546Sopenharmony_ci} 1372bf215546Sopenharmony_ci 1373bf215546Sopenharmony_civoid u_vbuf_get_minmax_index(struct pipe_context *pipe, 1374bf215546Sopenharmony_ci const struct pipe_draw_info *info, 1375bf215546Sopenharmony_ci const struct pipe_draw_start_count_bias *draw, 1376bf215546Sopenharmony_ci unsigned *out_min_index, unsigned *out_max_index) 1377bf215546Sopenharmony_ci{ 1378bf215546Sopenharmony_ci struct pipe_transfer *transfer = NULL; 1379bf215546Sopenharmony_ci const void *indices; 1380bf215546Sopenharmony_ci 1381bf215546Sopenharmony_ci if (info->has_user_indices) { 1382bf215546Sopenharmony_ci indices = (uint8_t*)info->index.user + 1383bf215546Sopenharmony_ci draw->start * info->index_size; 1384bf215546Sopenharmony_ci } else { 1385bf215546Sopenharmony_ci indices = pipe_buffer_map_range(pipe, info->index.resource, 1386bf215546Sopenharmony_ci draw->start * info->index_size, 1387bf215546Sopenharmony_ci draw->count * info->index_size, 1388bf215546Sopenharmony_ci PIPE_MAP_READ, &transfer); 1389bf215546Sopenharmony_ci } 1390bf215546Sopenharmony_ci 1391bf215546Sopenharmony_ci u_vbuf_get_minmax_index_mapped(info, draw->count, indices, 1392bf215546Sopenharmony_ci out_min_index, out_max_index); 1393bf215546Sopenharmony_ci 1394bf215546Sopenharmony_ci if (transfer) { 1395bf215546Sopenharmony_ci pipe_buffer_unmap(pipe, transfer); 1396bf215546Sopenharmony_ci } 1397bf215546Sopenharmony_ci} 1398bf215546Sopenharmony_ci 1399bf215546Sopenharmony_cistatic void u_vbuf_set_driver_vertex_buffers(struct u_vbuf *mgr) 1400bf215546Sopenharmony_ci{ 1401bf215546Sopenharmony_ci struct pipe_context *pipe = mgr->pipe; 1402bf215546Sopenharmony_ci unsigned start_slot, count; 1403bf215546Sopenharmony_ci 1404bf215546Sopenharmony_ci start_slot = ffs(mgr->dirty_real_vb_mask) - 1; 1405bf215546Sopenharmony_ci count = util_last_bit(mgr->dirty_real_vb_mask >> start_slot); 1406bf215546Sopenharmony_ci 1407bf215546Sopenharmony_ci if (mgr->dirty_real_vb_mask == mgr->enabled_vb_mask && 1408bf215546Sopenharmony_ci mgr->dirty_real_vb_mask == mgr->user_vb_mask) { 1409bf215546Sopenharmony_ci /* Fast path that allows us to transfer the VBO references to the driver 1410bf215546Sopenharmony_ci * to skip atomic reference counting there. These are freshly uploaded 1411bf215546Sopenharmony_ci * user buffers that can be discarded after this call. 1412bf215546Sopenharmony_ci */ 1413bf215546Sopenharmony_ci pipe->set_vertex_buffers(pipe, start_slot, count, 0, true, 1414bf215546Sopenharmony_ci mgr->real_vertex_buffer + start_slot); 1415bf215546Sopenharmony_ci 1416bf215546Sopenharmony_ci /* We don't own the VBO references now. Set them to NULL. */ 1417bf215546Sopenharmony_ci for (unsigned i = 0; i < count; i++) { 1418bf215546Sopenharmony_ci assert(!mgr->real_vertex_buffer[start_slot + i].is_user_buffer); 1419bf215546Sopenharmony_ci mgr->real_vertex_buffer[start_slot + i].buffer.resource = NULL; 1420bf215546Sopenharmony_ci } 1421bf215546Sopenharmony_ci } else { 1422bf215546Sopenharmony_ci /* Slow path where we have to keep VBO references. */ 1423bf215546Sopenharmony_ci pipe->set_vertex_buffers(pipe, start_slot, count, 0, false, 1424bf215546Sopenharmony_ci mgr->real_vertex_buffer + start_slot); 1425bf215546Sopenharmony_ci } 1426bf215546Sopenharmony_ci mgr->dirty_real_vb_mask = 0; 1427bf215546Sopenharmony_ci} 1428bf215546Sopenharmony_ci 1429bf215546Sopenharmony_cistatic void 1430bf215546Sopenharmony_ciu_vbuf_split_indexed_multidraw(struct u_vbuf *mgr, struct pipe_draw_info *info, 1431bf215546Sopenharmony_ci unsigned drawid_offset, 1432bf215546Sopenharmony_ci unsigned *indirect_data, unsigned stride, 1433bf215546Sopenharmony_ci unsigned draw_count) 1434bf215546Sopenharmony_ci{ 1435bf215546Sopenharmony_ci /* Increase refcount to be able to use take_index_buffer_ownership with 1436bf215546Sopenharmony_ci * all draws. 1437bf215546Sopenharmony_ci */ 1438bf215546Sopenharmony_ci if (draw_count > 1 && info->take_index_buffer_ownership) 1439bf215546Sopenharmony_ci p_atomic_add(&info->index.resource->reference.count, draw_count - 1); 1440bf215546Sopenharmony_ci 1441bf215546Sopenharmony_ci assert(info->index_size); 1442bf215546Sopenharmony_ci 1443bf215546Sopenharmony_ci for (unsigned i = 0; i < draw_count; i++) { 1444bf215546Sopenharmony_ci struct pipe_draw_start_count_bias draw; 1445bf215546Sopenharmony_ci unsigned offset = i * stride / 4; 1446bf215546Sopenharmony_ci 1447bf215546Sopenharmony_ci draw.count = indirect_data[offset + 0]; 1448bf215546Sopenharmony_ci info->instance_count = indirect_data[offset + 1]; 1449bf215546Sopenharmony_ci draw.start = indirect_data[offset + 2]; 1450bf215546Sopenharmony_ci draw.index_bias = indirect_data[offset + 3]; 1451bf215546Sopenharmony_ci info->start_instance = indirect_data[offset + 4]; 1452bf215546Sopenharmony_ci 1453bf215546Sopenharmony_ci u_vbuf_draw_vbo(mgr, info, drawid_offset, NULL, &draw, 1); 1454bf215546Sopenharmony_ci } 1455bf215546Sopenharmony_ci} 1456bf215546Sopenharmony_ci 1457bf215546Sopenharmony_civoid u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info, 1458bf215546Sopenharmony_ci unsigned drawid_offset, 1459bf215546Sopenharmony_ci const struct pipe_draw_indirect_info *indirect, 1460bf215546Sopenharmony_ci const struct pipe_draw_start_count_bias *draws, 1461bf215546Sopenharmony_ci unsigned num_draws) 1462bf215546Sopenharmony_ci{ 1463bf215546Sopenharmony_ci struct pipe_context *pipe = mgr->pipe; 1464bf215546Sopenharmony_ci int start_vertex; 1465bf215546Sopenharmony_ci unsigned min_index; 1466bf215546Sopenharmony_ci unsigned num_vertices; 1467bf215546Sopenharmony_ci boolean unroll_indices = FALSE; 1468bf215546Sopenharmony_ci const uint32_t used_vb_mask = mgr->ve->used_vb_mask; 1469bf215546Sopenharmony_ci uint32_t user_vb_mask = mgr->user_vb_mask & used_vb_mask; 1470bf215546Sopenharmony_ci struct pipe_draw_info new_info; 1471bf215546Sopenharmony_ci struct pipe_draw_start_count_bias new_draw; 1472bf215546Sopenharmony_ci unsigned fixed_restart_index = info->index_size ? util_prim_restart_index_from_size(info->index_size) : 0; 1473bf215546Sopenharmony_ci 1474bf215546Sopenharmony_ci uint32_t misaligned = 0; 1475bf215546Sopenharmony_ci if (!mgr->caps.attrib_component_unaligned) { 1476bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(mgr->unaligned_vb_mask); i++) { 1477bf215546Sopenharmony_ci misaligned |= mgr->ve->vb_align_mask[i] & mgr->unaligned_vb_mask[i]; 1478bf215546Sopenharmony_ci } 1479bf215546Sopenharmony_ci } 1480bf215546Sopenharmony_ci const uint32_t incompatible_vb_mask = 1481bf215546Sopenharmony_ci (mgr->incompatible_vb_mask | misaligned) & used_vb_mask; 1482bf215546Sopenharmony_ci 1483bf215546Sopenharmony_ci /* Normal draw. No fallback and no user buffers. */ 1484bf215546Sopenharmony_ci if (!incompatible_vb_mask && 1485bf215546Sopenharmony_ci !mgr->ve->incompatible_elem_mask && 1486bf215546Sopenharmony_ci !user_vb_mask && 1487bf215546Sopenharmony_ci (info->index_size != 1 || !mgr->caps.rewrite_ubyte_ibs) && 1488bf215546Sopenharmony_ci (!info->primitive_restart || 1489bf215546Sopenharmony_ci info->restart_index == fixed_restart_index || 1490bf215546Sopenharmony_ci !mgr->caps.rewrite_restart_index) && 1491bf215546Sopenharmony_ci (!info->primitive_restart || mgr->caps.supported_restart_modes & BITFIELD_BIT(info->mode)) && 1492bf215546Sopenharmony_ci mgr->caps.supported_prim_modes & BITFIELD_BIT(info->mode)) { 1493bf215546Sopenharmony_ci 1494bf215546Sopenharmony_ci /* Set vertex buffers if needed. */ 1495bf215546Sopenharmony_ci if (mgr->dirty_real_vb_mask & used_vb_mask) { 1496bf215546Sopenharmony_ci u_vbuf_set_driver_vertex_buffers(mgr); 1497bf215546Sopenharmony_ci } 1498bf215546Sopenharmony_ci 1499bf215546Sopenharmony_ci pipe->draw_vbo(pipe, info, drawid_offset, indirect, draws, num_draws); 1500bf215546Sopenharmony_ci return; 1501bf215546Sopenharmony_ci } 1502bf215546Sopenharmony_ci 1503bf215546Sopenharmony_ci /* Increase refcount to be able to use take_index_buffer_ownership with 1504bf215546Sopenharmony_ci * all draws. 1505bf215546Sopenharmony_ci */ 1506bf215546Sopenharmony_ci if (num_draws > 1 && info->take_index_buffer_ownership) 1507bf215546Sopenharmony_ci p_atomic_add(&info->index.resource->reference.count, num_draws - 1); 1508bf215546Sopenharmony_ci new_info = *info; 1509bf215546Sopenharmony_ci 1510bf215546Sopenharmony_ci for (unsigned d = 0; d < num_draws; d++) { 1511bf215546Sopenharmony_ci new_draw = draws[d]; 1512bf215546Sopenharmony_ci if (info->increment_draw_id) 1513bf215546Sopenharmony_ci drawid_offset++; 1514bf215546Sopenharmony_ci 1515bf215546Sopenharmony_ci /* Handle indirect (multi)draws. */ 1516bf215546Sopenharmony_ci if (indirect && indirect->buffer) { 1517bf215546Sopenharmony_ci unsigned draw_count = 0; 1518bf215546Sopenharmony_ci 1519bf215546Sopenharmony_ci /* Get the number of draws. */ 1520bf215546Sopenharmony_ci if (indirect->indirect_draw_count) { 1521bf215546Sopenharmony_ci pipe_buffer_read(pipe, indirect->indirect_draw_count, 1522bf215546Sopenharmony_ci indirect->indirect_draw_count_offset, 1523bf215546Sopenharmony_ci 4, &draw_count); 1524bf215546Sopenharmony_ci } else { 1525bf215546Sopenharmony_ci draw_count = indirect->draw_count; 1526bf215546Sopenharmony_ci } 1527bf215546Sopenharmony_ci 1528bf215546Sopenharmony_ci if (!draw_count) 1529bf215546Sopenharmony_ci goto cleanup; 1530bf215546Sopenharmony_ci 1531bf215546Sopenharmony_ci unsigned data_size = (draw_count - 1) * indirect->stride + 1532bf215546Sopenharmony_ci (new_info.index_size ? 20 : 16); 1533bf215546Sopenharmony_ci unsigned *data = malloc(data_size); 1534bf215546Sopenharmony_ci if (!data) 1535bf215546Sopenharmony_ci goto cleanup; /* report an error? */ 1536bf215546Sopenharmony_ci 1537bf215546Sopenharmony_ci /* Read the used buffer range only once, because the read can be 1538bf215546Sopenharmony_ci * uncached. 1539bf215546Sopenharmony_ci */ 1540bf215546Sopenharmony_ci pipe_buffer_read(pipe, indirect->buffer, indirect->offset, data_size, 1541bf215546Sopenharmony_ci data); 1542bf215546Sopenharmony_ci 1543bf215546Sopenharmony_ci if (info->index_size) { 1544bf215546Sopenharmony_ci /* Indexed multidraw. */ 1545bf215546Sopenharmony_ci unsigned index_bias0 = data[3]; 1546bf215546Sopenharmony_ci bool index_bias_same = true; 1547bf215546Sopenharmony_ci 1548bf215546Sopenharmony_ci /* If we invoke the translate path, we have to split the multidraw. */ 1549bf215546Sopenharmony_ci if (incompatible_vb_mask || 1550bf215546Sopenharmony_ci mgr->ve->incompatible_elem_mask) { 1551bf215546Sopenharmony_ci u_vbuf_split_indexed_multidraw(mgr, &new_info, drawid_offset, data, 1552bf215546Sopenharmony_ci indirect->stride, draw_count); 1553bf215546Sopenharmony_ci free(data); 1554bf215546Sopenharmony_ci return; 1555bf215546Sopenharmony_ci } 1556bf215546Sopenharmony_ci 1557bf215546Sopenharmony_ci /* See if index_bias is the same for all draws. */ 1558bf215546Sopenharmony_ci for (unsigned i = 1; i < draw_count; i++) { 1559bf215546Sopenharmony_ci if (data[i * indirect->stride / 4 + 3] != index_bias0) { 1560bf215546Sopenharmony_ci index_bias_same = false; 1561bf215546Sopenharmony_ci break; 1562bf215546Sopenharmony_ci } 1563bf215546Sopenharmony_ci } 1564bf215546Sopenharmony_ci 1565bf215546Sopenharmony_ci /* Split the multidraw if index_bias is different. */ 1566bf215546Sopenharmony_ci if (!index_bias_same) { 1567bf215546Sopenharmony_ci u_vbuf_split_indexed_multidraw(mgr, &new_info, drawid_offset, data, 1568bf215546Sopenharmony_ci indirect->stride, draw_count); 1569bf215546Sopenharmony_ci free(data); 1570bf215546Sopenharmony_ci return; 1571bf215546Sopenharmony_ci } 1572bf215546Sopenharmony_ci 1573bf215546Sopenharmony_ci /* If we don't need to use the translate path and index_bias is 1574bf215546Sopenharmony_ci * the same, we can process the multidraw with the time complexity 1575bf215546Sopenharmony_ci * equal to 1 draw call (except for the index range computation). 1576bf215546Sopenharmony_ci * We only need to compute the index range covering all draw calls 1577bf215546Sopenharmony_ci * of the multidraw. 1578bf215546Sopenharmony_ci * 1579bf215546Sopenharmony_ci * The driver will not look at these values because indirect != NULL. 1580bf215546Sopenharmony_ci * These values determine the user buffer bounds to upload. 1581bf215546Sopenharmony_ci */ 1582bf215546Sopenharmony_ci new_draw.index_bias = index_bias0; 1583bf215546Sopenharmony_ci new_info.index_bounds_valid = true; 1584bf215546Sopenharmony_ci new_info.min_index = ~0u; 1585bf215546Sopenharmony_ci new_info.max_index = 0; 1586bf215546Sopenharmony_ci new_info.start_instance = ~0u; 1587bf215546Sopenharmony_ci unsigned end_instance = 0; 1588bf215546Sopenharmony_ci 1589bf215546Sopenharmony_ci struct pipe_transfer *transfer = NULL; 1590bf215546Sopenharmony_ci const uint8_t *indices; 1591bf215546Sopenharmony_ci 1592bf215546Sopenharmony_ci if (info->has_user_indices) { 1593bf215546Sopenharmony_ci indices = (uint8_t*)info->index.user; 1594bf215546Sopenharmony_ci } else { 1595bf215546Sopenharmony_ci indices = (uint8_t*)pipe_buffer_map(pipe, info->index.resource, 1596bf215546Sopenharmony_ci PIPE_MAP_READ, &transfer); 1597bf215546Sopenharmony_ci } 1598bf215546Sopenharmony_ci 1599bf215546Sopenharmony_ci for (unsigned i = 0; i < draw_count; i++) { 1600bf215546Sopenharmony_ci unsigned offset = i * indirect->stride / 4; 1601bf215546Sopenharmony_ci unsigned start = data[offset + 2]; 1602bf215546Sopenharmony_ci unsigned count = data[offset + 0]; 1603bf215546Sopenharmony_ci unsigned start_instance = data[offset + 4]; 1604bf215546Sopenharmony_ci unsigned instance_count = data[offset + 1]; 1605bf215546Sopenharmony_ci 1606bf215546Sopenharmony_ci if (!count || !instance_count) 1607bf215546Sopenharmony_ci continue; 1608bf215546Sopenharmony_ci 1609bf215546Sopenharmony_ci /* Update the ranges of instances. */ 1610bf215546Sopenharmony_ci new_info.start_instance = MIN2(new_info.start_instance, 1611bf215546Sopenharmony_ci start_instance); 1612bf215546Sopenharmony_ci end_instance = MAX2(end_instance, start_instance + instance_count); 1613bf215546Sopenharmony_ci 1614bf215546Sopenharmony_ci /* Update the index range. */ 1615bf215546Sopenharmony_ci unsigned min, max; 1616bf215546Sopenharmony_ci u_vbuf_get_minmax_index_mapped(&new_info, count, 1617bf215546Sopenharmony_ci indices + 1618bf215546Sopenharmony_ci new_info.index_size * start, 1619bf215546Sopenharmony_ci &min, &max); 1620bf215546Sopenharmony_ci 1621bf215546Sopenharmony_ci new_info.min_index = MIN2(new_info.min_index, min); 1622bf215546Sopenharmony_ci new_info.max_index = MAX2(new_info.max_index, max); 1623bf215546Sopenharmony_ci } 1624bf215546Sopenharmony_ci free(data); 1625bf215546Sopenharmony_ci 1626bf215546Sopenharmony_ci if (transfer) 1627bf215546Sopenharmony_ci pipe_buffer_unmap(pipe, transfer); 1628bf215546Sopenharmony_ci 1629bf215546Sopenharmony_ci /* Set the final instance count. */ 1630bf215546Sopenharmony_ci new_info.instance_count = end_instance - new_info.start_instance; 1631bf215546Sopenharmony_ci 1632bf215546Sopenharmony_ci if (new_info.start_instance == ~0u || !new_info.instance_count) 1633bf215546Sopenharmony_ci goto cleanup; 1634bf215546Sopenharmony_ci } else { 1635bf215546Sopenharmony_ci /* Non-indexed multidraw. 1636bf215546Sopenharmony_ci * 1637bf215546Sopenharmony_ci * Keep the draw call indirect and compute minimums & maximums, 1638bf215546Sopenharmony_ci * which will determine the user buffer bounds to upload, but 1639bf215546Sopenharmony_ci * the driver will not look at these values because indirect != NULL. 1640bf215546Sopenharmony_ci * 1641bf215546Sopenharmony_ci * This efficiently processes the multidraw with the time complexity 1642bf215546Sopenharmony_ci * equal to 1 draw call. 1643bf215546Sopenharmony_ci */ 1644bf215546Sopenharmony_ci new_draw.start = ~0u; 1645bf215546Sopenharmony_ci new_info.start_instance = ~0u; 1646bf215546Sopenharmony_ci unsigned end_vertex = 0; 1647bf215546Sopenharmony_ci unsigned end_instance = 0; 1648bf215546Sopenharmony_ci 1649bf215546Sopenharmony_ci for (unsigned i = 0; i < draw_count; i++) { 1650bf215546Sopenharmony_ci unsigned offset = i * indirect->stride / 4; 1651bf215546Sopenharmony_ci unsigned start = data[offset + 2]; 1652bf215546Sopenharmony_ci unsigned count = data[offset + 0]; 1653bf215546Sopenharmony_ci unsigned start_instance = data[offset + 3]; 1654bf215546Sopenharmony_ci unsigned instance_count = data[offset + 1]; 1655bf215546Sopenharmony_ci 1656bf215546Sopenharmony_ci new_draw.start = MIN2(new_draw.start, start); 1657bf215546Sopenharmony_ci new_info.start_instance = MIN2(new_info.start_instance, 1658bf215546Sopenharmony_ci start_instance); 1659bf215546Sopenharmony_ci 1660bf215546Sopenharmony_ci end_vertex = MAX2(end_vertex, start + count); 1661bf215546Sopenharmony_ci end_instance = MAX2(end_instance, start_instance + instance_count); 1662bf215546Sopenharmony_ci } 1663bf215546Sopenharmony_ci free(data); 1664bf215546Sopenharmony_ci 1665bf215546Sopenharmony_ci /* Set the final counts. */ 1666bf215546Sopenharmony_ci new_draw.count = end_vertex - new_draw.start; 1667bf215546Sopenharmony_ci new_info.instance_count = end_instance - new_info.start_instance; 1668bf215546Sopenharmony_ci 1669bf215546Sopenharmony_ci if (new_draw.start == ~0u || !new_draw.count || !new_info.instance_count) 1670bf215546Sopenharmony_ci goto cleanup; 1671bf215546Sopenharmony_ci } 1672bf215546Sopenharmony_ci } else { 1673bf215546Sopenharmony_ci if ((!indirect && !new_draw.count) || !new_info.instance_count) 1674bf215546Sopenharmony_ci goto cleanup; 1675bf215546Sopenharmony_ci } 1676bf215546Sopenharmony_ci 1677bf215546Sopenharmony_ci if (new_info.index_size) { 1678bf215546Sopenharmony_ci /* See if anything needs to be done for per-vertex attribs. */ 1679bf215546Sopenharmony_ci if (u_vbuf_need_minmax_index(mgr, misaligned)) { 1680bf215546Sopenharmony_ci unsigned max_index; 1681bf215546Sopenharmony_ci 1682bf215546Sopenharmony_ci if (new_info.index_bounds_valid) { 1683bf215546Sopenharmony_ci min_index = new_info.min_index; 1684bf215546Sopenharmony_ci max_index = new_info.max_index; 1685bf215546Sopenharmony_ci } else { 1686bf215546Sopenharmony_ci u_vbuf_get_minmax_index(mgr->pipe, &new_info, &new_draw, 1687bf215546Sopenharmony_ci &min_index, &max_index); 1688bf215546Sopenharmony_ci } 1689bf215546Sopenharmony_ci 1690bf215546Sopenharmony_ci assert(min_index <= max_index); 1691bf215546Sopenharmony_ci 1692bf215546Sopenharmony_ci start_vertex = min_index + new_draw.index_bias; 1693bf215546Sopenharmony_ci num_vertices = max_index + 1 - min_index; 1694bf215546Sopenharmony_ci 1695bf215546Sopenharmony_ci /* Primitive restart doesn't work when unrolling indices. 1696bf215546Sopenharmony_ci * We would have to break this drawing operation into several ones. */ 1697bf215546Sopenharmony_ci /* Use some heuristic to see if unrolling indices improves 1698bf215546Sopenharmony_ci * performance. */ 1699bf215546Sopenharmony_ci if (!indirect && 1700bf215546Sopenharmony_ci !new_info.primitive_restart && 1701bf215546Sopenharmony_ci util_is_vbo_upload_ratio_too_large(new_draw.count, num_vertices) && 1702bf215546Sopenharmony_ci !u_vbuf_mapping_vertex_buffer_blocks(mgr, misaligned)) { 1703bf215546Sopenharmony_ci unroll_indices = TRUE; 1704bf215546Sopenharmony_ci user_vb_mask &= ~(mgr->nonzero_stride_vb_mask & 1705bf215546Sopenharmony_ci mgr->ve->noninstance_vb_mask_any); 1706bf215546Sopenharmony_ci } 1707bf215546Sopenharmony_ci } else { 1708bf215546Sopenharmony_ci /* Nothing to do for per-vertex attribs. */ 1709bf215546Sopenharmony_ci start_vertex = 0; 1710bf215546Sopenharmony_ci num_vertices = 0; 1711bf215546Sopenharmony_ci min_index = 0; 1712bf215546Sopenharmony_ci } 1713bf215546Sopenharmony_ci } else { 1714bf215546Sopenharmony_ci start_vertex = new_draw.start; 1715bf215546Sopenharmony_ci num_vertices = new_draw.count; 1716bf215546Sopenharmony_ci min_index = 0; 1717bf215546Sopenharmony_ci } 1718bf215546Sopenharmony_ci 1719bf215546Sopenharmony_ci /* Translate vertices with non-native layouts or formats. */ 1720bf215546Sopenharmony_ci if (unroll_indices || 1721bf215546Sopenharmony_ci incompatible_vb_mask || 1722bf215546Sopenharmony_ci mgr->ve->incompatible_elem_mask) { 1723bf215546Sopenharmony_ci if (!u_vbuf_translate_begin(mgr, &new_info, &new_draw, 1724bf215546Sopenharmony_ci start_vertex, num_vertices, 1725bf215546Sopenharmony_ci min_index, unroll_indices, misaligned)) { 1726bf215546Sopenharmony_ci debug_warn_once("u_vbuf_translate_begin() failed"); 1727bf215546Sopenharmony_ci goto cleanup; 1728bf215546Sopenharmony_ci } 1729bf215546Sopenharmony_ci 1730bf215546Sopenharmony_ci if (unroll_indices) { 1731bf215546Sopenharmony_ci new_info.index_size = 0; 1732bf215546Sopenharmony_ci new_draw.index_bias = 0; 1733bf215546Sopenharmony_ci new_info.index_bounds_valid = true; 1734bf215546Sopenharmony_ci new_info.min_index = 0; 1735bf215546Sopenharmony_ci new_info.max_index = new_draw.count - 1; 1736bf215546Sopenharmony_ci new_draw.start = 0; 1737bf215546Sopenharmony_ci } 1738bf215546Sopenharmony_ci 1739bf215546Sopenharmony_ci user_vb_mask &= ~(incompatible_vb_mask | 1740bf215546Sopenharmony_ci mgr->ve->incompatible_vb_mask_all); 1741bf215546Sopenharmony_ci } 1742bf215546Sopenharmony_ci 1743bf215546Sopenharmony_ci /* Upload user buffers. */ 1744bf215546Sopenharmony_ci if (user_vb_mask) { 1745bf215546Sopenharmony_ci if (u_vbuf_upload_buffers(mgr, start_vertex, num_vertices, 1746bf215546Sopenharmony_ci new_info.start_instance, 1747bf215546Sopenharmony_ci new_info.instance_count) != PIPE_OK) { 1748bf215546Sopenharmony_ci debug_warn_once("u_vbuf_upload_buffers() failed"); 1749bf215546Sopenharmony_ci goto cleanup; 1750bf215546Sopenharmony_ci } 1751bf215546Sopenharmony_ci 1752bf215546Sopenharmony_ci mgr->dirty_real_vb_mask |= user_vb_mask; 1753bf215546Sopenharmony_ci } 1754bf215546Sopenharmony_ci 1755bf215546Sopenharmony_ci /* 1756bf215546Sopenharmony_ci if (unroll_indices) { 1757bf215546Sopenharmony_ci printf("unrolling indices: start_vertex = %i, num_vertices = %i\n", 1758bf215546Sopenharmony_ci start_vertex, num_vertices); 1759bf215546Sopenharmony_ci util_dump_draw_info(stdout, info); 1760bf215546Sopenharmony_ci printf("\n"); 1761bf215546Sopenharmony_ci } 1762bf215546Sopenharmony_ci 1763bf215546Sopenharmony_ci unsigned i; 1764bf215546Sopenharmony_ci for (i = 0; i < mgr->nr_vertex_buffers; i++) { 1765bf215546Sopenharmony_ci printf("input %i: ", i); 1766bf215546Sopenharmony_ci util_dump_vertex_buffer(stdout, mgr->vertex_buffer+i); 1767bf215546Sopenharmony_ci printf("\n"); 1768bf215546Sopenharmony_ci } 1769bf215546Sopenharmony_ci for (i = 0; i < mgr->nr_real_vertex_buffers; i++) { 1770bf215546Sopenharmony_ci printf("real %i: ", i); 1771bf215546Sopenharmony_ci util_dump_vertex_buffer(stdout, mgr->real_vertex_buffer+i); 1772bf215546Sopenharmony_ci printf("\n"); 1773bf215546Sopenharmony_ci } 1774bf215546Sopenharmony_ci */ 1775bf215546Sopenharmony_ci 1776bf215546Sopenharmony_ci u_upload_unmap(pipe->stream_uploader); 1777bf215546Sopenharmony_ci if (mgr->dirty_real_vb_mask) 1778bf215546Sopenharmony_ci u_vbuf_set_driver_vertex_buffers(mgr); 1779bf215546Sopenharmony_ci 1780bf215546Sopenharmony_ci if ((new_info.index_size == 1 && mgr->caps.rewrite_ubyte_ibs) || 1781bf215546Sopenharmony_ci (new_info.primitive_restart && 1782bf215546Sopenharmony_ci ((new_info.restart_index != fixed_restart_index && mgr->caps.rewrite_restart_index) || 1783bf215546Sopenharmony_ci !(mgr->caps.supported_restart_modes & BITFIELD_BIT(new_info.mode)))) || 1784bf215546Sopenharmony_ci !(mgr->caps.supported_prim_modes & BITFIELD_BIT(new_info.mode))) { 1785bf215546Sopenharmony_ci util_primconvert_save_flatshade_first(mgr->pc, mgr->flatshade_first); 1786bf215546Sopenharmony_ci util_primconvert_draw_vbo(mgr->pc, &new_info, drawid_offset, indirect, &new_draw, 1); 1787bf215546Sopenharmony_ci } else 1788bf215546Sopenharmony_ci pipe->draw_vbo(pipe, &new_info, drawid_offset, indirect, &new_draw, 1); 1789bf215546Sopenharmony_ci } 1790bf215546Sopenharmony_ci 1791bf215546Sopenharmony_ci if (mgr->using_translate) { 1792bf215546Sopenharmony_ci u_vbuf_translate_end(mgr); 1793bf215546Sopenharmony_ci } 1794bf215546Sopenharmony_ci return; 1795bf215546Sopenharmony_ci 1796bf215546Sopenharmony_cicleanup: 1797bf215546Sopenharmony_ci if (info->take_index_buffer_ownership) { 1798bf215546Sopenharmony_ci struct pipe_resource *indexbuf = info->index.resource; 1799bf215546Sopenharmony_ci pipe_resource_reference(&indexbuf, NULL); 1800bf215546Sopenharmony_ci } 1801bf215546Sopenharmony_ci} 1802bf215546Sopenharmony_ci 1803bf215546Sopenharmony_civoid u_vbuf_save_vertex_elements(struct u_vbuf *mgr) 1804bf215546Sopenharmony_ci{ 1805bf215546Sopenharmony_ci assert(!mgr->ve_saved); 1806bf215546Sopenharmony_ci mgr->ve_saved = mgr->ve; 1807bf215546Sopenharmony_ci} 1808bf215546Sopenharmony_ci 1809bf215546Sopenharmony_civoid u_vbuf_restore_vertex_elements(struct u_vbuf *mgr) 1810bf215546Sopenharmony_ci{ 1811bf215546Sopenharmony_ci if (mgr->ve != mgr->ve_saved) { 1812bf215546Sopenharmony_ci struct pipe_context *pipe = mgr->pipe; 1813bf215546Sopenharmony_ci 1814bf215546Sopenharmony_ci mgr->ve = mgr->ve_saved; 1815bf215546Sopenharmony_ci pipe->bind_vertex_elements_state(pipe, 1816bf215546Sopenharmony_ci mgr->ve ? mgr->ve->driver_cso : NULL); 1817bf215546Sopenharmony_ci } 1818bf215546Sopenharmony_ci mgr->ve_saved = NULL; 1819bf215546Sopenharmony_ci} 1820