u_draw.c revision 438d7ac146dc89d1c2943610c662c57e11a47382
1/************************************************************************** 2 * 3 * Copyright 2011 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 29#include "util/u_debug.h" 30#include "util/u_math.h" 31#include "util/u_format.h" 32#include "util/u_draw.h" 33 34 35/** 36 * Returns the largest legal index value plus one for the current set 37 * of bound vertex buffers. Regardless of any other consideration, 38 * all vertex lookups need to be clamped to 0..max_index-1 to prevent 39 * an out-of-bound access. 40 * 41 * Note that if zero is returned it means that one or more buffers is 42 * too small to contain any valid vertex data. 43 */ 44unsigned 45util_draw_max_index( 46 const struct pipe_vertex_buffer *vertex_buffers, 47 unsigned nr_vertex_buffers, 48 const struct pipe_vertex_element *vertex_elements, 49 unsigned nr_vertex_elements, 50 const struct pipe_draw_info *info) 51{ 52 unsigned max_index; 53 unsigned i; 54 55 max_index = ~0U - 1; 56 for (i = 0; i < nr_vertex_elements; i++) { 57 const struct pipe_vertex_element *element = 58 &vertex_elements[i]; 59 const struct pipe_vertex_buffer *buffer = 60 &vertex_buffers[element->vertex_buffer_index]; 61 unsigned buffer_size; 62 const struct util_format_description *format_desc; 63 unsigned format_size; 64 65 assert(buffer->buffer->height0 == 1); 66 assert(buffer->buffer->depth0 == 1); 67 buffer_size = buffer->buffer->width0; 68 69 format_desc = util_format_description(element->src_format); 70 assert(format_desc->block.width == 1); 71 assert(format_desc->block.height == 1); 72 assert(format_desc->block.bits % 8 == 0); 73 format_size = format_desc->block.bits/8; 74 75 if (buffer->buffer_offset >= buffer_size) { 76 /* buffer is too small */ 77 return 0; 78 } 79 80 buffer_size -= buffer->buffer_offset; 81 82 if (element->src_offset >= buffer_size) { 83 /* buffer is too small */ 84 return 0; 85 } 86 87 buffer_size -= element->src_offset; 88 89 if (format_size > buffer_size) { 90 /* buffer is too small */ 91 return 0; 92 } 93 94 buffer_size -= format_size; 95 96 if (buffer->stride != 0) { 97 unsigned buffer_max_index; 98 99 buffer_max_index = buffer_size / buffer->stride; 100 101 if (element->instance_divisor == 0) { 102 /* Per-vertex data */ 103 max_index = MIN2(max_index, buffer_max_index); 104 } 105 else { 106 /* Per-instance data. Simply make sure the state tracker didn't 107 * request more instances than those that fit in the buffer */ 108 assert((info->start_instance + info->instance_count)/element->instance_divisor 109 <= (buffer_max_index + 1)); 110 } 111 } 112 } 113 114 return max_index + 1; 115} 116