2011-03-31 14:40:25 +01:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright 2011 VMware, Inc.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
|
|
|
|
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "util/u_debug.h"
|
2013-04-05 13:29:36 +01:00
|
|
|
#include "util/u_inlines.h"
|
2011-03-31 14:40:25 +01:00
|
|
|
#include "util/u_math.h"
|
2019-06-27 23:05:31 +01:00
|
|
|
#include "util/format/u_format.h"
|
2011-03-31 14:40:25 +01:00
|
|
|
#include "util/u_draw.h"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-11-12 18:50:32 +00:00
|
|
|
* Returns the largest legal index value plus one for the current set
|
|
|
|
* of bound vertex buffers. Regardless of any other consideration,
|
|
|
|
* all vertex lookups need to be clamped to 0..max_index-1 to prevent
|
|
|
|
* an out-of-bound access.
|
|
|
|
*
|
|
|
|
* Note that if zero is returned it means that one or more buffers is
|
|
|
|
* too small to contain any valid vertex data.
|
2011-03-31 14:40:25 +01:00
|
|
|
*/
|
|
|
|
unsigned
|
|
|
|
util_draw_max_index(
|
|
|
|
const struct pipe_vertex_buffer *vertex_buffers,
|
|
|
|
const struct pipe_vertex_element *vertex_elements,
|
|
|
|
unsigned nr_vertex_elements,
|
|
|
|
const struct pipe_draw_info *info)
|
|
|
|
{
|
|
|
|
unsigned max_index;
|
|
|
|
unsigned i;
|
|
|
|
|
2011-11-12 18:50:32 +00:00
|
|
|
max_index = ~0U - 1;
|
2011-03-31 14:40:25 +01:00
|
|
|
for (i = 0; i < nr_vertex_elements; i++) {
|
|
|
|
const struct pipe_vertex_element *element =
|
|
|
|
&vertex_elements[i];
|
|
|
|
const struct pipe_vertex_buffer *buffer =
|
|
|
|
&vertex_buffers[element->vertex_buffer_index];
|
|
|
|
unsigned buffer_size;
|
|
|
|
const struct util_format_description *format_desc;
|
|
|
|
unsigned format_size;
|
|
|
|
|
2017-04-02 13:30:16 +01:00
|
|
|
if (buffer->is_user_buffer || !buffer->buffer.resource) {
|
2012-04-24 19:58:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-02 13:30:16 +01:00
|
|
|
assert(buffer->buffer.resource->height0 == 1);
|
|
|
|
assert(buffer->buffer.resource->depth0 == 1);
|
|
|
|
buffer_size = buffer->buffer.resource->width0;
|
2011-03-31 14:40:25 +01:00
|
|
|
|
|
|
|
format_desc = util_format_description(element->src_format);
|
|
|
|
assert(format_desc->block.width == 1);
|
|
|
|
assert(format_desc->block.height == 1);
|
|
|
|
assert(format_desc->block.bits % 8 == 0);
|
|
|
|
format_size = format_desc->block.bits/8;
|
|
|
|
|
2011-11-12 18:50:32 +00:00
|
|
|
if (buffer->buffer_offset >= buffer_size) {
|
|
|
|
/* buffer is too small */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-31 14:40:25 +01:00
|
|
|
buffer_size -= buffer->buffer_offset;
|
|
|
|
|
2011-11-12 18:50:32 +00:00
|
|
|
if (element->src_offset >= buffer_size) {
|
|
|
|
/* buffer is too small */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-31 14:40:25 +01:00
|
|
|
buffer_size -= element->src_offset;
|
|
|
|
|
2011-11-12 18:50:32 +00:00
|
|
|
if (format_size > buffer_size) {
|
|
|
|
/* buffer is too small */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-31 14:40:25 +01:00
|
|
|
buffer_size -= format_size;
|
|
|
|
|
|
|
|
if (buffer->stride != 0) {
|
|
|
|
unsigned buffer_max_index;
|
|
|
|
|
|
|
|
buffer_max_index = buffer_size / buffer->stride;
|
|
|
|
|
|
|
|
if (element->instance_divisor == 0) {
|
|
|
|
/* Per-vertex data */
|
|
|
|
max_index = MIN2(max_index, buffer_max_index);
|
|
|
|
}
|
|
|
|
else {
|
2019-12-04 01:38:14 +00:00
|
|
|
/* Per-instance data. Simply make sure gallium frontends didn't
|
2011-03-31 14:40:25 +01:00
|
|
|
* request more instances than those that fit in the buffer */
|
2012-11-16 17:57:38 +00:00
|
|
|
if ((info->start_instance + info->instance_count)/element->instance_divisor
|
|
|
|
> (buffer_max_index + 1)) {
|
|
|
|
/* FIXME: We really should stop thinking in terms of maximum
|
|
|
|
* indices/instances and simply start clamping against buffer
|
|
|
|
* size. */
|
|
|
|
debug_printf("%s: too many instances for vertex buffer\n",
|
|
|
|
__FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-31 14:40:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-12 18:50:32 +00:00
|
|
|
return max_index + 1;
|
2011-03-31 14:40:25 +01:00
|
|
|
}
|
2013-04-05 13:29:36 +01:00
|
|
|
|
2021-05-21 10:34:28 +01:00
|
|
|
struct u_indirect_params *
|
|
|
|
util_draw_indirect_read(struct pipe_context *pipe,
|
|
|
|
const struct pipe_draw_info *info_in,
|
|
|
|
const struct pipe_draw_indirect_info *indirect,
|
|
|
|
unsigned *num_draws)
|
|
|
|
{
|
|
|
|
struct pipe_transfer *transfer;
|
|
|
|
uint32_t *params;
|
|
|
|
struct u_indirect_params *draws;
|
|
|
|
unsigned num_params = info_in->index_size ? 5 : 4;
|
|
|
|
|
|
|
|
assert(indirect);
|
|
|
|
assert(!indirect->count_from_stream_output);
|
|
|
|
|
|
|
|
uint32_t draw_count = indirect->draw_count;
|
|
|
|
if (indirect->indirect_draw_count) {
|
|
|
|
struct pipe_transfer *dc_transfer;
|
|
|
|
uint32_t *dc_param = pipe_buffer_map_range(pipe,
|
|
|
|
indirect->indirect_draw_count,
|
|
|
|
indirect->indirect_draw_count_offset,
|
|
|
|
4, PIPE_MAP_READ, &dc_transfer);
|
|
|
|
if (!dc_transfer) {
|
|
|
|
debug_printf("%s: failed to map indirect draw count buffer\n", __FUNCTION__);
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-04-19 21:06:44 +01:00
|
|
|
draw_count = dc_param[0];
|
2021-05-21 10:34:28 +01:00
|
|
|
pipe_buffer_unmap(pipe, dc_transfer);
|
|
|
|
}
|
2022-04-19 21:06:44 +01:00
|
|
|
if (!draw_count) {
|
|
|
|
*num_draws = draw_count;
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-05-21 10:34:28 +01:00
|
|
|
draws = malloc(sizeof(struct u_indirect_params) * draw_count);
|
|
|
|
if (!draws)
|
|
|
|
return NULL;
|
|
|
|
|
2022-04-15 15:34:03 +01:00
|
|
|
unsigned map_size = (draw_count - 1) * indirect->stride + (num_params * sizeof(uint32_t));
|
2021-05-21 10:34:28 +01:00
|
|
|
params = pipe_buffer_map_range(pipe,
|
|
|
|
indirect->buffer,
|
|
|
|
indirect->offset,
|
2022-04-15 15:34:03 +01:00
|
|
|
map_size,
|
2021-05-21 10:34:28 +01:00
|
|
|
PIPE_MAP_READ,
|
|
|
|
&transfer);
|
|
|
|
if (!transfer) {
|
|
|
|
debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__);
|
|
|
|
free(draws);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < draw_count; i++) {
|
|
|
|
memcpy(&draws[i].info, info_in, sizeof(struct pipe_draw_info));
|
|
|
|
draws[i].draw.count = params[0];
|
|
|
|
draws[i].info.instance_count = params[1];
|
|
|
|
draws[i].draw.start = params[2];
|
|
|
|
draws[i].draw.index_bias = info_in->index_size ? params[3] : 0;
|
|
|
|
draws[i].info.start_instance = info_in->index_size ? params[4] : params[3];
|
|
|
|
params += indirect->stride / 4;
|
|
|
|
}
|
|
|
|
pipe_buffer_unmap(pipe, transfer);
|
|
|
|
*num_draws = draw_count;
|
|
|
|
return draws;
|
|
|
|
}
|
2013-04-05 13:29:36 +01:00
|
|
|
|
2020-11-01 11:38:32 +00:00
|
|
|
/* This extracts the draw arguments from the indirect resource,
|
2013-04-05 13:29:36 +01:00
|
|
|
* puts them into a new instance of pipe_draw_info, and calls draw_vbo on it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
util_draw_indirect(struct pipe_context *pipe,
|
2020-11-01 11:38:32 +00:00
|
|
|
const struct pipe_draw_info *info_in,
|
|
|
|
const struct pipe_draw_indirect_info *indirect)
|
2013-04-05 13:29:36 +01:00
|
|
|
{
|
|
|
|
struct pipe_draw_info info;
|
|
|
|
struct pipe_transfer *transfer;
|
|
|
|
uint32_t *params;
|
2020-04-20 06:39:50 +01:00
|
|
|
unsigned num_params = info_in->index_size ? 5 : 4;
|
2013-04-05 13:29:36 +01:00
|
|
|
|
2020-11-01 11:38:32 +00:00
|
|
|
assert(indirect);
|
|
|
|
assert(!indirect->count_from_stream_output);
|
2013-04-05 13:29:36 +01:00
|
|
|
|
|
|
|
memcpy(&info, info_in, sizeof(info));
|
|
|
|
|
2020-11-01 11:38:32 +00:00
|
|
|
uint32_t draw_count = indirect->draw_count;
|
2019-12-23 05:13:10 +00:00
|
|
|
|
2020-11-01 11:38:32 +00:00
|
|
|
if (indirect->indirect_draw_count) {
|
2019-12-23 05:13:10 +00:00
|
|
|
struct pipe_transfer *dc_transfer;
|
|
|
|
uint32_t *dc_param = pipe_buffer_map_range(pipe,
|
2020-11-01 11:38:32 +00:00
|
|
|
indirect->indirect_draw_count,
|
|
|
|
indirect->indirect_draw_count_offset,
|
2020-07-01 13:16:12 +01:00
|
|
|
4, PIPE_MAP_READ, &dc_transfer);
|
2019-12-23 05:13:10 +00:00
|
|
|
if (!dc_transfer) {
|
|
|
|
debug_printf("%s: failed to map indirect draw count buffer\n", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dc_param[0] < draw_count)
|
|
|
|
draw_count = dc_param[0];
|
|
|
|
pipe_buffer_unmap(pipe, dc_transfer);
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:38:32 +00:00
|
|
|
if (indirect->stride)
|
|
|
|
num_params = MIN2(indirect->stride / 4, num_params);
|
2013-04-05 13:29:36 +01:00
|
|
|
params = (uint32_t *)
|
|
|
|
pipe_buffer_map_range(pipe,
|
2020-11-01 11:38:32 +00:00
|
|
|
indirect->buffer,
|
|
|
|
indirect->offset,
|
|
|
|
(num_params * indirect->draw_count) * sizeof(uint32_t),
|
2020-07-01 13:16:12 +01:00
|
|
|
PIPE_MAP_READ,
|
2013-04-05 13:29:36 +01:00
|
|
|
&transfer);
|
|
|
|
if (!transfer) {
|
|
|
|
debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-23 05:13:10 +00:00
|
|
|
for (unsigned i = 0; i < draw_count; i++) {
|
2021-04-11 14:49:49 +01:00
|
|
|
struct pipe_draw_start_count_bias draw;
|
2020-11-01 14:04:40 +00:00
|
|
|
|
|
|
|
draw.count = params[0];
|
2019-12-23 04:56:01 +00:00
|
|
|
info.instance_count = params[1];
|
2020-11-01 14:04:40 +00:00
|
|
|
draw.start = params[2];
|
2021-04-11 15:26:29 +01:00
|
|
|
draw.index_bias = info_in->index_size ? params[3] : 0;
|
2019-12-23 04:56:01 +00:00
|
|
|
info.start_instance = info_in->index_size ? params[4] : params[3];
|
2013-04-05 13:29:36 +01:00
|
|
|
|
2021-04-11 18:35:38 +01:00
|
|
|
pipe->draw_vbo(pipe, &info, i, NULL, &draw, 1);
|
2013-04-05 13:29:36 +01:00
|
|
|
|
2020-11-01 11:38:32 +00:00
|
|
|
params += indirect->stride / 4;
|
2019-12-23 04:56:01 +00:00
|
|
|
}
|
|
|
|
pipe_buffer_unmap(pipe, transfer);
|
2013-04-05 13:29:36 +01:00
|
|
|
}
|
2021-03-18 17:48:41 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
util_draw_multi(struct pipe_context *pctx, const struct pipe_draw_info *info,
|
2021-04-11 18:35:38 +01:00
|
|
|
unsigned drawid_offset,
|
2021-03-18 17:48:41 +00:00
|
|
|
const struct pipe_draw_indirect_info *indirect,
|
2021-04-11 14:49:49 +01:00
|
|
|
const struct pipe_draw_start_count_bias *draws,
|
2021-03-18 17:48:41 +00:00
|
|
|
unsigned num_draws)
|
|
|
|
{
|
|
|
|
struct pipe_draw_info tmp_info = *info;
|
2021-04-11 18:35:38 +01:00
|
|
|
unsigned drawid = drawid_offset;
|
2021-03-18 17:48:41 +00:00
|
|
|
|
|
|
|
/* If you call this with num_draws==1, that is probably going to be
|
|
|
|
* an infinite loop
|
|
|
|
*/
|
|
|
|
assert(num_draws > 1);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_draws; i++) {
|
|
|
|
if (indirect || (draws[i].count && info->instance_count))
|
2021-04-11 18:35:38 +01:00
|
|
|
pctx->draw_vbo(pctx, &tmp_info, drawid, indirect, &draws[i], 1);
|
2021-03-18 17:48:41 +00:00
|
|
|
if (tmp_info.increment_draw_id)
|
2021-04-11 18:35:38 +01:00
|
|
|
drawid++;
|
2021-03-18 17:48:41 +00:00
|
|
|
}
|
|
|
|
}
|