#include "util/u_vbuf.h"
#include "util/u_dump.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
#include "util/u_inlines.h"
#include "util/u_memory.h"
#include "util/u_upload_mgr.h"
struct u_vbuf {
struct u_vbuf_caps caps;
+ bool has_signed_vb_offset;
struct pipe_context *pipe;
struct translate_cache *translate_cache;
uint32_t enabled_vb_mask;
/* Saved vertex buffer. */
- unsigned aux_vertex_buffer_slot;
- struct pipe_vertex_buffer aux_vertex_buffer_saved;
+ struct pipe_vertex_buffer vertex_buffer0_saved;
/* Vertex buffers for the driver.
* There are usually no user buffers. */
uint32_t incompatible_vb_mask; /* each bit describes a corresp. buffer */
/* Which buffer has a non-zero stride. */
uint32_t nonzero_stride_vb_mask; /* each bit describes a corresp. buffer */
+ /* Which buffers are allowed (supported by hardware). */
+ uint32_t allowed_vb_mask;
};
static void *
for (i = 0; i < ARRAY_SIZE(vbuf_format_fallbacks); i++) {
enum pipe_format format = vbuf_format_fallbacks[i].from;
- if (!screen->is_format_supported(screen, format, PIPE_BUFFER, 0,
+ if (!screen->is_format_supported(screen, format, PIPE_BUFFER, 0, 0,
PIPE_BIND_VERTEX_BUFFER)) {
caps->format_translation[format] = vbuf_format_fallbacks[i].to;
fallback = TRUE;
PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY);
caps->user_vertex_buffers =
screen->get_param(screen, PIPE_CAP_USER_VERTEX_BUFFERS);
+ caps->max_vertex_buffers =
+ screen->get_param(screen, PIPE_CAP_MAX_VERTEX_BUFFERS);
if (!caps->buffer_offset_unaligned ||
!caps->buffer_stride_unaligned ||
}
struct u_vbuf *
-u_vbuf_create(struct pipe_context *pipe,
- struct u_vbuf_caps *caps, unsigned aux_vertex_buffer_index)
+u_vbuf_create(struct pipe_context *pipe, struct u_vbuf_caps *caps)
{
struct u_vbuf *mgr = CALLOC_STRUCT(u_vbuf);
mgr->caps = *caps;
- mgr->aux_vertex_buffer_slot = aux_vertex_buffer_index;
mgr->pipe = pipe;
mgr->cso_cache = cso_cache_create();
mgr->translate_cache = translate_cache_create();
memset(mgr->fallback_vbs, ~0, sizeof(mgr->fallback_vbs));
+ mgr->allowed_vb_mask = u_bit_consecutive(0, mgr->caps.max_vertex_buffers);
+
+ mgr->has_signed_vb_offset =
+ pipe->screen->get_param(pipe->screen,
+ PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET);
return mgr;
}
for (i = 0; i < PIPE_MAX_ATTRIBS; i++)
pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[i]);
- pipe_vertex_buffer_unreference(&mgr->aux_vertex_buffer_saved);
+ pipe_vertex_buffer_unreference(&mgr->vertex_buffer0_saved);
translate_cache_destroy(mgr->translate_cache);
cso_cache_delete(mgr->cso_cache);
unsigned size = vb->stride ? num_vertices * vb->stride
: sizeof(double)*4;
- if (offset+size > vb->buffer.resource->width0) {
+ if (!vb->buffer.resource)
+ continue;
+
+ if (offset + size > vb->buffer.resource->width0) {
+ /* Don't try to map past end of buffer. This often happens when
+ * we're translating an attribute that's at offset > 0 from the
+ * start of the vertex. If we'd subtract attrib's offset from
+ * the size, this probably wouldn't happen.
+ */
size = vb->buffer.resource->width0 - offset;
+
+ /* Also adjust num_vertices. A common user error is to call
+ * glDrawRangeElements() with incorrect 'end' argument. The 'end
+ * value should be the max index value, but people often
+ * accidentally add one to this value. This adjustment avoids
+ * crashing (by reading past the end of a hardware buffer mapping)
+ * when people do that.
+ */
+ num_vertices = (size + vb->stride - 1) / vb->stride;
}
map = pipe_buffer_map_range(mgr->pipe, vb->buffer.resource, offset, size,
map -= (ptrdiff_t)vb->stride * min_index;
}
- tr->set_buffer(tr, i, map, vb->stride, ~0);
+ tr->set_buffer(tr, i, map, vb->stride, info->max_index);
}
/* Translate. */
} else {
/* Create and map the output buffer. */
u_upload_alloc(mgr->pipe->stream_uploader,
- key->output_stride * start_vertex,
+ mgr->has_signed_vb_offset ?
+ 0 : key->output_stride * start_vertex,
key->output_stride * num_vertices, 4,
&out_offset, &out_buffer,
(void**)&out_map);
mgr->real_vertex_buffer[out_vb].stride = key->output_stride;
/* Move the buffer reference. */
- pipe_resource_reference(
- &mgr->real_vertex_buffer[out_vb].buffer.resource, NULL);
+ pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[out_vb]);
mgr->real_vertex_buffer[out_vb].buffer.resource = out_buffer;
+ mgr->real_vertex_buffer[out_vb].is_user_buffer = false;
return PIPE_OK;
}
}
}
+ if (used_buffers & ~mgr->allowed_vb_mask) {
+ /* More vertex buffers are used than the hardware supports. In
+ * principle, we only need to make sure that less vertex buffers are
+ * used, and mark some of the latter vertex buffers as incompatible.
+ * For now, mark all vertex buffers as incompatible.
+ */
+ ve->incompatible_vb_mask_any = used_buffers;
+ ve->compatible_vb_mask_any = 0;
+ ve->incompatible_elem_mask = u_bit_consecutive(0, count);
+ }
+
ve->used_vb_mask = used_buffers;
ve->compatible_vb_mask_all = ~ve->incompatible_vb_mask_any & used_buffers;
ve->incompatible_vb_mask_all = ~ve->compatible_vb_mask_any & used_buffers;
unsigned dst_index = start_slot + i;
pipe_vertex_buffer_unreference(&mgr->vertex_buffer[dst_index]);
- pipe_resource_reference(&mgr->real_vertex_buffer[dst_index].buffer.resource,
- NULL);
+ pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[dst_index]);
}
pipe->set_vertex_buffers(pipe, start_slot, count, NULL);
size = mgr->ve->src_format_size[i];
} else if (instance_div) {
/* Per-instance attrib. */
- unsigned count = (num_instances + instance_div - 1) / instance_div;
+
+ /* Figure out how many instances we'll render given instance_div. We
+ * can't use the typical div_round_up() pattern because the CTS uses
+ * instance_div = ~0 for a test, which overflows div_round_up()'s
+ * addition.
+ */
+ unsigned count = num_instances / instance_div;
+ if (count * instance_div != num_instances)
+ count++;
+
first += vb->stride * start_instance;
size = vb->stride * (count - 1) + mgr->ve->src_format_size[i];
} else {
real_vb = &mgr->real_vertex_buffer[i];
ptr = mgr->vertex_buffer[i].buffer.user;
- u_upload_data(mgr->pipe->stream_uploader, start, end - start, 4,
+ u_upload_data(mgr->pipe->stream_uploader,
+ mgr->has_signed_vb_offset ? 0 : start,
+ end - start, 4,
ptr + start, &real_vb->buffer_offset, &real_vb->buffer.resource);
if (!real_vb->buffer.resource)
return PIPE_ERROR_OUT_OF_MEMORY;
mgr->nonzero_stride_vb_mask)) != 0;
}
-static void u_vbuf_get_minmax_index(struct pipe_context *pipe,
- const struct pipe_draw_info *info,
- int *out_min_index, int *out_max_index)
+static void
+u_vbuf_get_minmax_index_mapped(const struct pipe_draw_info *info,
+ const void *indices, unsigned *out_min_index,
+ unsigned *out_max_index)
{
- struct pipe_transfer *transfer = NULL;
- const void *indices;
- unsigned i;
-
- if (info->has_user_indices) {
- indices = (uint8_t*)info->index.user +
- info->start * info->index_size;
- } else {
- indices = pipe_buffer_map_range(pipe, info->index.resource,
- info->start * info->index_size,
- info->count * info->index_size,
- PIPE_TRANSFER_READ, &transfer);
+ if (!info->count) {
+ *out_min_index = 0;
+ *out_max_index = 0;
+ return;
}
switch (info->index_size) {
case 4: {
const unsigned *ui_indices = (const unsigned*)indices;
- unsigned max_ui = 0;
- unsigned min_ui = ~0U;
+ unsigned max = 0;
+ unsigned min = ~0u;
if (info->primitive_restart) {
- for (i = 0; i < info->count; i++) {
+ for (unsigned i = 0; i < info->count; i++) {
if (ui_indices[i] != info->restart_index) {
- if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
- if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
+ if (ui_indices[i] > max) max = ui_indices[i];
+ if (ui_indices[i] < min) min = ui_indices[i];
}
}
}
else {
- for (i = 0; i < info->count; i++) {
- if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
- if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
+ for (unsigned i = 0; i < info->count; i++) {
+ if (ui_indices[i] > max) max = ui_indices[i];
+ if (ui_indices[i] < min) min = ui_indices[i];
}
}
- *out_min_index = min_ui;
- *out_max_index = max_ui;
+ *out_min_index = min;
+ *out_max_index = max;
break;
}
case 2: {
const unsigned short *us_indices = (const unsigned short*)indices;
- unsigned max_us = 0;
- unsigned min_us = ~0U;
+ unsigned short max = 0;
+ unsigned short min = ~((unsigned short)0);
if (info->primitive_restart) {
- for (i = 0; i < info->count; i++) {
+ for (unsigned i = 0; i < info->count; i++) {
if (us_indices[i] != info->restart_index) {
- if (us_indices[i] > max_us) max_us = us_indices[i];
- if (us_indices[i] < min_us) min_us = us_indices[i];
+ if (us_indices[i] > max) max = us_indices[i];
+ if (us_indices[i] < min) min = us_indices[i];
}
}
}
else {
- for (i = 0; i < info->count; i++) {
- if (us_indices[i] > max_us) max_us = us_indices[i];
- if (us_indices[i] < min_us) min_us = us_indices[i];
+ for (unsigned i = 0; i < info->count; i++) {
+ if (us_indices[i] > max) max = us_indices[i];
+ if (us_indices[i] < min) min = us_indices[i];
}
}
- *out_min_index = min_us;
- *out_max_index = max_us;
+ *out_min_index = min;
+ *out_max_index = max;
break;
}
case 1: {
const unsigned char *ub_indices = (const unsigned char*)indices;
- unsigned max_ub = 0;
- unsigned min_ub = ~0U;
+ unsigned char max = 0;
+ unsigned char min = ~((unsigned char)0);
if (info->primitive_restart) {
- for (i = 0; i < info->count; i++) {
+ for (unsigned i = 0; i < info->count; i++) {
if (ub_indices[i] != info->restart_index) {
- if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
- if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
+ if (ub_indices[i] > max) max = ub_indices[i];
+ if (ub_indices[i] < min) min = ub_indices[i];
}
}
}
else {
- for (i = 0; i < info->count; i++) {
- if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
- if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
+ for (unsigned i = 0; i < info->count; i++) {
+ if (ub_indices[i] > max) max = ub_indices[i];
+ if (ub_indices[i] < min) min = ub_indices[i];
}
}
- *out_min_index = min_ub;
- *out_max_index = max_ub;
+ *out_min_index = min;
+ *out_max_index = max;
break;
}
default:
assert(0);
- *out_min_index = 0;
- *out_max_index = 0;
+ }
+}
+
+void u_vbuf_get_minmax_index(struct pipe_context *pipe,
+ const struct pipe_draw_info *info,
+ unsigned *out_min_index, unsigned *out_max_index)
+{
+ struct pipe_transfer *transfer = NULL;
+ const void *indices;
+
+ if (info->has_user_indices) {
+ indices = (uint8_t*)info->index.user +
+ info->start * info->index_size;
+ } else {
+ indices = pipe_buffer_map_range(pipe, info->index.resource,
+ info->start * info->index_size,
+ info->count * info->index_size,
+ PIPE_TRANSFER_READ, &transfer);
}
+ u_vbuf_get_minmax_index_mapped(info, indices, out_min_index, out_max_index);
+
if (transfer) {
pipe_buffer_unmap(pipe, transfer);
}
mgr->dirty_real_vb_mask = 0;
}
+static void
+u_vbuf_split_indexed_multidraw(struct u_vbuf *mgr, struct pipe_draw_info *info,
+ unsigned *indirect_data, unsigned stride,
+ unsigned draw_count)
+{
+ assert(info->index_size);
+ info->indirect = NULL;
+
+ for (unsigned i = 0; i < draw_count; i++) {
+ unsigned offset = i * stride / 4;
+
+ info->count = indirect_data[offset + 0];
+ info->instance_count = indirect_data[offset + 1];
+
+ if (!info->count || !info->instance_count)
+ continue;
+
+ info->start = indirect_data[offset + 2];
+ info->index_bias = indirect_data[offset + 3];
+ info->start_instance = indirect_data[offset + 4];
+
+ u_vbuf_draw_vbo(mgr, info);
+ }
+}
+
void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info)
{
struct pipe_context *pipe = mgr->pipe;
- int start_vertex, min_index;
+ int start_vertex;
+ unsigned min_index;
unsigned num_vertices;
boolean unroll_indices = FALSE;
const uint32_t used_vb_mask = mgr->ve->used_vb_mask;
new_info = *info;
- /* Fallback. We need to know all the parameters. */
+ /* Handle indirect (multi)draws. */
if (new_info.indirect) {
- struct pipe_transfer *transfer = NULL;
- int *data;
-
- if (new_info.index_size) {
- data = pipe_buffer_map_range(pipe, new_info.indirect->buffer,
- new_info.indirect->offset, 20,
- PIPE_TRANSFER_READ, &transfer);
- new_info.index_bias = data[3];
- new_info.start_instance = data[4];
- }
- else {
- data = pipe_buffer_map_range(pipe, new_info.indirect->buffer,
- new_info.indirect->offset, 16,
- PIPE_TRANSFER_READ, &transfer);
- new_info.start_instance = data[3];
+ const struct pipe_draw_indirect_info *indirect = new_info.indirect;
+ unsigned draw_count = 0;
+
+ /* Get the number of draws. */
+ if (indirect->indirect_draw_count) {
+ pipe_buffer_read(pipe, indirect->indirect_draw_count,
+ indirect->indirect_draw_count_offset,
+ 4, &draw_count);
+ } else {
+ draw_count = indirect->draw_count;
}
- new_info.count = data[0];
- new_info.instance_count = data[1];
- new_info.start = data[2];
- pipe_buffer_unmap(pipe, transfer);
- new_info.indirect = NULL;
+ if (!draw_count)
+ return;
+
+ unsigned data_size = (draw_count - 1) * indirect->stride +
+ (new_info.index_size ? 20 : 16);
+ unsigned *data = malloc(data_size);
+ if (!data)
+ return; /* report an error? */
+
+ /* Read the used buffer range only once, because the read can be
+ * uncached.
+ */
+ pipe_buffer_read(pipe, indirect->buffer, indirect->offset, data_size,
+ data);
+
+ if (info->index_size) {
+ /* Indexed multidraw. */
+ unsigned index_bias0 = data[3];
+ bool index_bias_same = true;
+
+ /* If we invoke the translate path, we have to split the multidraw. */
+ if (incompatible_vb_mask ||
+ mgr->ve->incompatible_elem_mask) {
+ u_vbuf_split_indexed_multidraw(mgr, &new_info, data,
+ indirect->stride, draw_count);
+ free(data);
+ return;
+ }
+
+ /* See if index_bias is the same for all draws. */
+ for (unsigned i = 1; i < draw_count; i++) {
+ if (data[i * indirect->stride / 4 + 3] != index_bias0) {
+ index_bias_same = false;
+ break;
+ }
+ }
+
+ /* Split the multidraw if index_bias is different. */
+ if (!index_bias_same) {
+ u_vbuf_split_indexed_multidraw(mgr, &new_info, data,
+ indirect->stride, draw_count);
+ free(data);
+ return;
+ }
+
+ /* If we don't need to use the translate path and index_bias is
+ * the same, we can process the multidraw with the time complexity
+ * equal to 1 draw call (except for the index range computation).
+ * We only need to compute the index range covering all draw calls
+ * of the multidraw.
+ *
+ * The driver will not look at these values because indirect != NULL.
+ * These values determine the user buffer bounds to upload.
+ */
+ new_info.index_bias = index_bias0;
+ new_info.min_index = ~0u;
+ new_info.max_index = 0;
+ new_info.start_instance = ~0u;
+ unsigned end_instance = 0;
+
+ struct pipe_transfer *transfer = NULL;
+ const uint8_t *indices;
+
+ if (info->has_user_indices) {
+ indices = (uint8_t*)info->index.user;
+ } else {
+ indices = (uint8_t*)pipe_buffer_map(pipe, info->index.resource,
+ PIPE_TRANSFER_READ, &transfer);
+ }
+
+ for (unsigned i = 0; i < draw_count; i++) {
+ unsigned offset = i * indirect->stride / 4;
+ unsigned start = data[offset + 2];
+ unsigned count = data[offset + 0];
+ unsigned start_instance = data[offset + 4];
+ unsigned instance_count = data[offset + 1];
+
+ if (!count || !instance_count)
+ continue;
+
+ /* Update the ranges of instances. */
+ new_info.start_instance = MIN2(new_info.start_instance,
+ start_instance);
+ end_instance = MAX2(end_instance, start_instance + instance_count);
+
+ /* Update the index range. */
+ unsigned min, max;
+ new_info.count = count; /* only used by get_minmax_index */
+ u_vbuf_get_minmax_index_mapped(&new_info,
+ indices +
+ new_info.index_size * start,
+ &min, &max);
+
+ new_info.min_index = MIN2(new_info.min_index, min);
+ new_info.max_index = MAX2(new_info.max_index, max);
+ }
+ free(data);
+
+ if (transfer)
+ pipe_buffer_unmap(pipe, transfer);
+
+ /* Set the final instance count. */
+ new_info.instance_count = end_instance - new_info.start_instance;
+
+ if (new_info.start_instance == ~0u || !new_info.instance_count)
+ return;
+ } else {
+ /* Non-indexed multidraw.
+ *
+ * Keep the draw call indirect and compute minimums & maximums,
+ * which will determine the user buffer bounds to upload, but
+ * the driver will not look at these values because indirect != NULL.
+ *
+ * This efficiently processes the multidraw with the time complexity
+ * equal to 1 draw call.
+ */
+ new_info.start = ~0u;
+ new_info.start_instance = ~0u;
+ unsigned end_vertex = 0;
+ unsigned end_instance = 0;
+
+ for (unsigned i = 0; i < draw_count; i++) {
+ unsigned offset = i * indirect->stride / 4;
+ unsigned start = data[offset + 2];
+ unsigned count = data[offset + 0];
+ unsigned start_instance = data[offset + 3];
+ unsigned instance_count = data[offset + 1];
+
+ new_info.start = MIN2(new_info.start, start);
+ new_info.start_instance = MIN2(new_info.start_instance,
+ start_instance);
+
+ end_vertex = MAX2(end_vertex, start + count);
+ end_instance = MAX2(end_instance, start_instance + instance_count);
+ }
+ free(data);
+
+ /* Set the final counts. */
+ new_info.count = end_vertex - new_info.start;
+ new_info.instance_count = end_instance - new_info.start_instance;
+
+ if (new_info.start == ~0u || !new_info.count || !new_info.instance_count)
+ return;
+ }
}
if (new_info.index_size) {
/* See if anything needs to be done for per-vertex attribs. */
if (u_vbuf_need_minmax_index(mgr)) {
- int max_index;
+ unsigned max_index;
if (new_info.max_index != ~0u) {
min_index = new_info.min_index;
* We would have to break this drawing operation into several ones. */
/* Use some heuristic to see if unrolling indices improves
* performance. */
- if (!new_info.primitive_restart &&
+ if (!info->indirect &&
+ !new_info.primitive_restart &&
num_vertices > new_info.count*2 &&
num_vertices - new_info.count > 32 &&
!u_vbuf_mapping_vertex_buffer_blocks(mgr)) {
mgr->ve_saved = NULL;
}
-void u_vbuf_save_aux_vertex_buffer_slot(struct u_vbuf *mgr)
+void u_vbuf_save_vertex_buffer0(struct u_vbuf *mgr)
{
- pipe_vertex_buffer_reference(&mgr->aux_vertex_buffer_saved,
- &mgr->vertex_buffer[mgr->aux_vertex_buffer_slot]);
+ pipe_vertex_buffer_reference(&mgr->vertex_buffer0_saved,
+ &mgr->vertex_buffer[0]);
}
-void u_vbuf_restore_aux_vertex_buffer_slot(struct u_vbuf *mgr)
+void u_vbuf_restore_vertex_buffer0(struct u_vbuf *mgr)
{
- u_vbuf_set_vertex_buffers(mgr, mgr->aux_vertex_buffer_slot, 1,
- &mgr->aux_vertex_buffer_saved);
- pipe_vertex_buffer_unreference(&mgr->aux_vertex_buffer_saved);
+ u_vbuf_set_vertex_buffers(mgr, 0, 1, &mgr->vertex_buffer0_saved);
+ pipe_vertex_buffer_unreference(&mgr->vertex_buffer0_saved);
}