diff options
author | Marek Olšák <[email protected]> | 2012-03-29 17:51:50 +0200 |
---|---|---|
committer | Marek Olšák <[email protected]> | 2012-10-31 00:55:13 +0100 |
commit | e73bf3b805de78299f1a652668ba4e6eab9bac94 (patch) | |
tree | 11839d343c6aad3a8fb5c594cadf961288adbea0 /src/gallium/auxiliary/util | |
parent | a7c5be098aee3a8228cbd95558bac29cb7ff6a3d (diff) |
gallium: add start_slot parameter to set_vertex_buffers
This allows updating only a subrange of buffer bindings.
set_vertex_buffers(pipe, start_slot, count, NULL) unbinds buffers in that
range. Binding NULL resources unbinds buffers too (both buffer and user_buffer
must be NULL).
The meta ops are adapted to only save, change, and restore the single slot
they use. The cso_context can save and restore only one vertex buffer slot.
The clients can query which one it is using cso_get_aux_vertex_buffer_slot.
It's currently set to 0. (the Draw module breaks if it's set to non-zero)
It should decrease the CPU overhead when using a lot of meta ops, but
the drivers must be able to treat each vertex buffer slot as a separate
state (only r600g does so at the moment).
I can imagine this also being useful for optimizing some OpenGL use cases.
Reviewed-by: Brian Paul <[email protected]>
Diffstat (limited to 'src/gallium/auxiliary/util')
-rw-r--r-- | src/gallium/auxiliary/util/u_blit.c | 19 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_blitter.c | 32 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_blitter.h | 20 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_draw_quad.c | 11 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_draw_quad.h | 6 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_gen_mipmap.c | 7 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_helpers.c | 90 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_helpers.h | 51 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_inlines.h | 28 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_math.h | 4 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_vbuf.c | 223 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_vbuf.h | 9 |
12 files changed, 336 insertions, 164 deletions
diff --git a/src/gallium/auxiliary/util/u_blit.c b/src/gallium/auxiliary/util/u_blit.c index bf1c392cd41..ab1549e2de6 100644 --- a/src/gallium/auxiliary/util/u_blit.c +++ b/src/gallium/auxiliary/util/u_blit.c @@ -133,7 +133,7 @@ util_create_blit(struct pipe_context *pipe, struct cso_context *cso) for (i = 0; i < 2; i++) { ctx->velem[i].src_offset = i * 4 * sizeof(float); ctx->velem[i].instance_divisor = 0; - ctx->velem[i].vertex_buffer_index = 0; + ctx->velem[i].vertex_buffer_index = cso_get_aux_vertex_buffer_slot(cso); ctx->velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; } @@ -665,7 +665,7 @@ util_blit_pixels(struct blit_state *ctx, cso_save_vertex_shader(ctx->cso); cso_save_geometry_shader(ctx->cso); cso_save_vertex_elements(ctx->cso); - cso_save_vertex_buffers(ctx->cso); + cso_save_aux_vertex_buffer_slot(ctx->cso); /* set misc state we care about */ if (writemask) @@ -776,7 +776,9 @@ util_blit_pixels(struct blit_state *ctx, z); if (ctx->vbuf) { - util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, offset, + util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, + cso_get_aux_vertex_buffer_slot(ctx->cso), + offset, PIPE_PRIM_TRIANGLE_FAN, 4, /* verts */ 2); /* attribs/vert */ @@ -795,7 +797,7 @@ util_blit_pixels(struct blit_state *ctx, cso_restore_vertex_shader(ctx->cso); cso_restore_geometry_shader(ctx->cso); cso_restore_vertex_elements(ctx->cso); - cso_restore_vertex_buffers(ctx->cso); + cso_restore_aux_vertex_buffer_slot(ctx->cso); cso_restore_stream_outputs(ctx->cso); pipe_sampler_view_reference(&sampler_view, NULL); @@ -869,7 +871,7 @@ util_blit_pixels_tex(struct blit_state *ctx, cso_save_vertex_shader(ctx->cso); cso_save_geometry_shader(ctx->cso); cso_save_vertex_elements(ctx->cso); - cso_save_vertex_buffers(ctx->cso); + cso_save_aux_vertex_buffer_slot(ctx->cso); /* set misc state we care about */ cso_set_blend(ctx->cso, &ctx->blend_write_color); @@ -923,8 +925,9 @@ util_blit_pixels_tex(struct blit_state *ctx, s0, t0, s1, t1, z); - util_draw_vertex_buffer(ctx->pipe, ctx->cso, - ctx->vbuf, offset, + util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, + cso_get_aux_vertex_buffer_slot(ctx->cso), + offset, PIPE_PRIM_TRIANGLE_FAN, 4, /* verts */ 2); /* attribs/vert */ @@ -942,6 +945,6 @@ util_blit_pixels_tex(struct blit_state *ctx, cso_restore_vertex_shader(ctx->cso); cso_restore_geometry_shader(ctx->cso); cso_restore_vertex_elements(ctx->cso); - cso_restore_vertex_buffers(ctx->cso); + cso_restore_aux_vertex_buffer_slot(ctx->cso); cso_restore_stream_outputs(ctx->cso); } diff --git a/src/gallium/auxiliary/util/u_blitter.c b/src/gallium/auxiliary/util/u_blitter.c index f4ac4aa8685..bb784d6b370 100644 --- a/src/gallium/auxiliary/util/u_blitter.c +++ b/src/gallium/auxiliary/util/u_blitter.c @@ -158,7 +158,6 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) ctx->base.saved_fb_state.nr_cbufs = ~0; ctx->base.saved_num_sampler_views = ~0; ctx->base.saved_num_sampler_states = ~0; - ctx->base.saved_num_vertex_buffers = ~0; ctx->base.saved_num_so_targets = ~0; ctx->has_geometry_shader = @@ -241,11 +240,14 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state); } + ctx->base.vb_slot = 0; /* 0 for now */ + /* vertex elements states */ memset(&velem[0], 0, sizeof(velem[0]) * 2); for (i = 0; i < 2; i++) { velem[i].src_offset = i * 4 * sizeof(float); velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; + velem[i].vertex_buffer_index = ctx->base.vb_slot; } ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); @@ -253,20 +255,25 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe) memset(&velem[0], 0, sizeof(velem[0]) * 2); velem[0].src_offset = 0; velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; + velem[0].vertex_buffer_index = ctx->base.vb_slot; velem[1].src_offset = 4 * sizeof(float); velem[1].src_format = PIPE_FORMAT_R32G32B32A32_SINT; + velem[1].vertex_buffer_index = ctx->base.vb_slot; ctx->velem_sint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); memset(&velem[0], 0, sizeof(velem[0]) * 2); velem[0].src_offset = 0; velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; + velem[0].vertex_buffer_index = ctx->base.vb_slot; velem[1].src_offset = 4 * sizeof(float); velem[1].src_format = PIPE_FORMAT_R32G32B32A32_UINT; + velem[1].vertex_buffer_index = ctx->base.vb_slot; ctx->velem_uint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); } if (ctx->has_stream_out) { velem[0].src_format = PIPE_FORMAT_R32_UINT; + velem[0].vertex_buffer_index = ctx->base.vb_slot; ctx->velem_state_readbuf = pipe->create_vertex_elements_state(pipe, 1, &velem[0]); } @@ -387,7 +394,6 @@ static void blitter_unset_running_flag(struct blitter_context_priv *ctx) static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx) { - assert(ctx->base.saved_num_vertex_buffers != ~0); assert(ctx->base.saved_velem_state != INVALID_PTR); assert(ctx->base.saved_vs != INVALID_PTR); assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR); @@ -400,18 +406,10 @@ static void blitter_restore_vertex_states(struct blitter_context_priv *ctx) struct pipe_context *pipe = ctx->base.pipe; unsigned i; - /* Vertex buffers. */ - pipe->set_vertex_buffers(pipe, - ctx->base.saved_num_vertex_buffers, - ctx->base.saved_vertex_buffers); - - for (i = 0; i < ctx->base.saved_num_vertex_buffers; i++) { - if (ctx->base.saved_vertex_buffers[i].buffer) { - pipe_resource_reference(&ctx->base.saved_vertex_buffers[i].buffer, - NULL); - } - } - ctx->base.saved_num_vertex_buffers = ~0; + /* Vertex buffer. */ + pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, + &ctx->base.saved_vertex_buffer); + pipe_resource_reference(&ctx->base.saved_vertex_buffer.buffer, NULL); /* Vertex elements. */ pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state); @@ -935,8 +933,8 @@ static void blitter_draw(struct blitter_context_priv *ctx, u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices, &offset, &buf); u_upload_unmap(ctx->upload); - util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, offset, - PIPE_PRIM_TRIANGLE_FAN, 4, 2); + util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, ctx->base.vb_slot, + offset, PIPE_PRIM_TRIANGLE_FAN, 4, 2); pipe_resource_reference(&buf, NULL); } @@ -1687,7 +1685,7 @@ void util_blitter_copy_buffer(struct blitter_context *blitter, vb.buffer_offset = srcx; vb.stride = 4; - pipe->set_vertex_buffers(pipe, 1, &vb); + pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb); pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf); pipe->bind_vs_state(pipe, ctx->vs_pos_only); if (ctx->has_geometry_shader) diff --git a/src/gallium/auxiliary/util/u_blitter.h b/src/gallium/auxiliary/util/u_blitter.h index c49faaad717..b96e68e9310 100644 --- a/src/gallium/auxiliary/util/u_blitter.h +++ b/src/gallium/auxiliary/util/u_blitter.h @@ -104,8 +104,8 @@ struct blitter_context unsigned saved_num_sampler_views; struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS]; - unsigned saved_num_vertex_buffers; - struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS]; + unsigned vb_slot; + struct pipe_vertex_buffer saved_vertex_buffer; unsigned saved_num_so_targets; struct pipe_stream_output_target *saved_so_targets[PIPE_MAX_SO_BUFFERS]; @@ -466,17 +466,13 @@ util_blitter_save_fragment_sampler_views(struct blitter_context *blitter, } static INLINE void -util_blitter_save_vertex_buffers(struct blitter_context *blitter, - unsigned num_vertex_buffers, - struct pipe_vertex_buffer *vertex_buffers) +util_blitter_save_vertex_buffer_slot(struct blitter_context *blitter, + struct pipe_vertex_buffer *vertex_buffers) { - assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers)); - - blitter->saved_num_vertex_buffers = 0; - util_copy_vertex_buffers(blitter->saved_vertex_buffers, - (unsigned*)&blitter->saved_num_vertex_buffers, - vertex_buffers, - num_vertex_buffers); + pipe_resource_reference(&blitter->saved_vertex_buffer.buffer, + vertex_buffers[blitter->vb_slot].buffer); + memcpy(&blitter->saved_vertex_buffer, &vertex_buffers[blitter->vb_slot], + sizeof(struct pipe_vertex_buffer)); } static INLINE void diff --git a/src/gallium/auxiliary/util/u_draw_quad.c b/src/gallium/auxiliary/util/u_draw_quad.c index 81c4f107ea9..3fe324afa80 100644 --- a/src/gallium/auxiliary/util/u_draw_quad.c +++ b/src/gallium/auxiliary/util/u_draw_quad.c @@ -42,6 +42,7 @@ void util_draw_vertex_buffer(struct pipe_context *pipe, struct cso_context *cso, struct pipe_resource *vbuf, + uint vbuf_slot, uint offset, uint prim_type, uint num_verts, @@ -60,10 +61,10 @@ util_draw_vertex_buffer(struct pipe_context *pipe, /* note: vertex elements already set by caller */ if (cso) { - cso_set_vertex_buffers(cso, 1, &vbuffer); + cso_set_vertex_buffers(cso, vbuf_slot, 1, &vbuffer); cso_draw_arrays(cso, prim_type, 0, num_verts); } else { - pipe->set_vertex_buffers(pipe, 1, &vbuffer); + pipe->set_vertex_buffers(pipe, vbuf_slot, 1, &vbuffer); util_draw_arrays(pipe, prim_type, 0, num_verts); } } @@ -86,7 +87,7 @@ util_draw_user_vertex_buffer(struct cso_context *cso, void *buffer, /* note: vertex elements already set by caller */ - cso_set_vertex_buffers(cso, 1, &vbuffer); + cso_set_vertex_buffers(cso, 0, 1, &vbuffer); cso_draw_arrays(cso, prim_type, 0, num_verts); } @@ -97,6 +98,7 @@ util_draw_user_vertex_buffer(struct cso_context *cso, void *buffer, */ void util_draw_texquad(struct pipe_context *pipe, struct cso_context *cso, + uint vbuf_slot, float x0, float y0, float x1, float y1, float z) { uint numAttribs = 2, i, j; @@ -145,7 +147,8 @@ util_draw_texquad(struct pipe_context *pipe, struct cso_context *cso, goto out; pipe_buffer_write(pipe, vbuf, 0, vertexBytes, v); - util_draw_vertex_buffer(pipe, cso, vbuf, 0, PIPE_PRIM_TRIANGLE_FAN, 4, 2); + util_draw_vertex_buffer(pipe, cso, vbuf, vbuf_slot, 0, + PIPE_PRIM_TRIANGLE_FAN, 4, 2); out: if (vbuf) diff --git a/src/gallium/auxiliary/util/u_draw_quad.h b/src/gallium/auxiliary/util/u_draw_quad.h index 2834a4a8115..cb17f4e4cb2 100644 --- a/src/gallium/auxiliary/util/u_draw_quad.h +++ b/src/gallium/auxiliary/util/u_draw_quad.h @@ -44,8 +44,9 @@ struct cso_context; extern void util_draw_vertex_buffer(struct pipe_context *pipe, struct cso_context *cso, - struct pipe_resource *vbuf, uint offset, - uint num_attribs, uint num_verts, uint prim_type); + struct pipe_resource *vbuf, uint vbuf_slot, + uint offset, uint prim_type, uint num_attribs, + uint num_verts); void util_draw_user_vertex_buffer(struct cso_context *cso, void *buffer, @@ -53,6 +54,7 @@ util_draw_user_vertex_buffer(struct cso_context *cso, void *buffer, extern void util_draw_texquad(struct pipe_context *pipe, struct cso_context *cso, + uint vbuf_slot, float x0, float y0, float x1, float y1, float z); diff --git a/src/gallium/auxiliary/util/u_gen_mipmap.c b/src/gallium/auxiliary/util/u_gen_mipmap.c index 4d8adb9f355..48ebdb9fa3d 100644 --- a/src/gallium/auxiliary/util/u_gen_mipmap.c +++ b/src/gallium/auxiliary/util/u_gen_mipmap.c @@ -1287,7 +1287,7 @@ util_create_gen_mipmap(struct pipe_context *pipe, for (i = 0; i < 2; i++) { ctx->velem[i].src_offset = i * 4 * sizeof(float); ctx->velem[i].instance_divisor = 0; - ctx->velem[i].vertex_buffer_index = 0; + ctx->velem[i].vertex_buffer_index = cso_get_aux_vertex_buffer_slot(cso); ctx->velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; } @@ -1565,7 +1565,7 @@ util_gen_mipmap(struct gen_mipmap_state *ctx, cso_save_geometry_shader(ctx->cso); cso_save_viewport(ctx->cso); cso_save_vertex_elements(ctx->cso); - cso_save_vertex_buffers(ctx->cso); + cso_save_aux_vertex_buffer_slot(ctx->cso); /* bind our state */ cso_set_blend(ctx->cso, is_depth ? &ctx->blend_keep_color : @@ -1673,6 +1673,7 @@ util_gen_mipmap(struct gen_mipmap_state *ctx, util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, + cso_get_aux_vertex_buffer_slot(ctx->cso), offset, PIPE_PRIM_TRIANGLE_FAN, 4, /* verts */ @@ -1697,5 +1698,5 @@ util_gen_mipmap(struct gen_mipmap_state *ctx, cso_restore_viewport(ctx->cso); cso_restore_vertex_elements(ctx->cso); cso_restore_stream_outputs(ctx->cso); - cso_restore_vertex_buffers(ctx->cso); + cso_restore_aux_vertex_buffer_slot(ctx->cso); } diff --git a/src/gallium/auxiliary/util/u_helpers.c b/src/gallium/auxiliary/util/u_helpers.c new file mode 100644 index 00000000000..ac1edcdbb82 --- /dev/null +++ b/src/gallium/auxiliary/util/u_helpers.c @@ -0,0 +1,90 @@ +/************************************************************************** + * + * Copyright 2012 Marek Olšák <[email protected]> + * 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 THE AUTHORS AND/OR THEIR 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_helpers.h" +#include "util/u_inlines.h" + +/** + * This function is used to copy an array of pipe_vertex_buffer structures, + * while properly referencing the pipe_vertex_buffer::buffer member. + * + * enabled_buffers is updated such that the bits corresponding to the indices + * of disabled buffers are set to 0 and the enabled ones are set to 1. + * + * \sa util_copy_framebuffer_state + */ +void util_set_vertex_buffers_mask(struct pipe_vertex_buffer *dst, + uint32_t *enabled_buffers, + const struct pipe_vertex_buffer *src, + unsigned start_slot, unsigned count) +{ + unsigned i; + uint32_t bitmask = 0; + + dst += start_slot; + + if (src) { + for (i = 0; i < count; i++) { + if (src[i].buffer || src[i].user_buffer) { + bitmask |= 1 << i; + } + pipe_resource_reference(&dst[i].buffer, src[i].buffer); + } + + /* Copy over the other members of pipe_vertex_buffer. */ + memcpy(dst, src, count * sizeof(struct pipe_vertex_buffer)); + + *enabled_buffers &= ~(((1ull << count) - 1) << start_slot); + *enabled_buffers |= bitmask << start_slot; + } + else { + /* Unreference the buffers. */ + for (i = 0; i < count; i++) { + pipe_resource_reference(&dst[i].buffer, NULL); + dst[i].user_buffer = NULL; + } + + *enabled_buffers &= ~(((1ull << count) - 1) << start_slot); + } +} + +/** + * Same as util_set_vertex_buffers_mask, but it only returns the number + * of bound buffers. + */ +void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst, + unsigned *dst_count, + const struct pipe_vertex_buffer *src, + unsigned start_slot, unsigned count) +{ + uint32_t enabled_buffers = (1ull << *dst_count) - 1; + + util_set_vertex_buffers_mask(dst, &enabled_buffers, src, start_slot, + count); + + *dst_count = util_last_bit(enabled_buffers); +} diff --git a/src/gallium/auxiliary/util/u_helpers.h b/src/gallium/auxiliary/util/u_helpers.h new file mode 100644 index 00000000000..09c7116fa6d --- /dev/null +++ b/src/gallium/auxiliary/util/u_helpers.h @@ -0,0 +1,51 @@ +/************************************************************************** + * + * Copyright 2012 Marek Olšák <[email protected]> + * 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 THE AUTHORS AND/OR THEIR 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. + * + **************************************************************************/ + +#ifndef U_HELPERS_H +#define U_HELPERS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "pipe/p_state.h" + +void util_set_vertex_buffers_mask(struct pipe_vertex_buffer *dst, + uint32_t *enabled_buffers, + const struct pipe_vertex_buffer *src, + unsigned start_slot, unsigned count); + +void util_set_vertex_buffers_count(struct pipe_vertex_buffer *dst, + unsigned *dst_count, + const struct pipe_vertex_buffer *src, + unsigned start_slot, unsigned count); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/gallium/auxiliary/util/u_inlines.h b/src/gallium/auxiliary/util/u_inlines.h index 5c6e7eb3234..3d68cf5400e 100644 --- a/src/gallium/auxiliary/util/u_inlines.h +++ b/src/gallium/auxiliary/util/u_inlines.h @@ -449,34 +449,6 @@ static INLINE boolean util_get_offset( } } -/** - * This function is used to copy an array of pipe_vertex_buffer structures, - * while properly referencing the pipe_vertex_buffer::buffer member. - * - * \sa util_copy_framebuffer_state - */ -static INLINE void util_copy_vertex_buffers(struct pipe_vertex_buffer *dst, - unsigned *dst_count, - const struct pipe_vertex_buffer *src, - unsigned src_count) -{ - unsigned i; - - /* Reference the buffers of 'src' in 'dst'. */ - for (i = 0; i < src_count; i++) { - pipe_resource_reference(&dst[i].buffer, src[i].buffer); - } - /* Unreference the rest of the buffers in 'dst'. */ - for (; i < *dst_count; i++) { - pipe_resource_reference(&dst[i].buffer, NULL); - } - - /* Update the size of 'dst' and copy over the other members - * of pipe_vertex_buffer. */ - *dst_count = src_count; - memcpy(dst, src, src_count * sizeof(struct pipe_vertex_buffer)); -} - static INLINE float util_get_min_point_size(const struct pipe_rasterizer_state *state) { diff --git a/src/gallium/auxiliary/util/u_math.h b/src/gallium/auxiliary/util/u_math.h index 4047bd9b016..607fbecb02f 100644 --- a/src/gallium/auxiliary/util/u_math.h +++ b/src/gallium/auxiliary/util/u_math.h @@ -482,12 +482,16 @@ unsigned ffs( unsigned u ) */ static INLINE unsigned util_last_bit(unsigned u) { +#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) + return u == 0 ? 0 : 32 - __builtin_clz(u); +#else unsigned r = 0; while (u) { r++; u >>= 1; } return r; +#endif } diff --git a/src/gallium/auxiliary/util/u_vbuf.c b/src/gallium/auxiliary/util/u_vbuf.c index 1cc83c3dd39..7fb492882f5 100644 --- a/src/gallium/auxiliary/util/u_vbuf.c +++ b/src/gallium/auxiliary/util/u_vbuf.c @@ -89,17 +89,17 @@ struct u_vbuf { /* This is what was set in set_vertex_buffers. * May contain user buffers. */ struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS]; - unsigned nr_vertex_buffers; + uint32_t enabled_vb_mask; - /* Saved vertex buffers. */ - struct pipe_vertex_buffer vertex_buffer_saved[PIPE_MAX_ATTRIBS]; - unsigned nr_vertex_buffers_saved; + /* Saved vertex buffer. */ + unsigned aux_vertex_buffer_slot; + struct pipe_vertex_buffer aux_vertex_buffer_saved; /* Vertex buffers for the driver. - * There are no user buffers. */ + * There are usually no user buffers. */ struct pipe_vertex_buffer real_vertex_buffer[PIPE_MAX_ATTRIBS]; - unsigned nr_real_vertex_buffers; - boolean vertex_buffers_dirty; + uint32_t dirty_real_vb_mask; /* which buffers are dirty since the last + call of set_vertex_buffers */ /* The index buffer. */ struct pipe_index_buffer index_buffer; @@ -174,11 +174,12 @@ void u_vbuf_get_caps(struct pipe_screen *screen, struct u_vbuf_caps *caps) struct u_vbuf * u_vbuf_create(struct pipe_context *pipe, - struct u_vbuf_caps *caps) + struct u_vbuf_caps *caps, unsigned aux_vertex_buffer_index) { 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(); @@ -239,16 +240,20 @@ void u_vbuf_set_vertex_elements(struct u_vbuf *mgr, unsigned count, void u_vbuf_destroy(struct u_vbuf *mgr) { + struct pipe_screen *screen = mgr->pipe->screen; unsigned i; + unsigned num_vb = screen->get_shader_param(screen, PIPE_SHADER_VERTEX, + PIPE_SHADER_CAP_MAX_INPUTS); - mgr->pipe->set_vertex_buffers(mgr->pipe, 0, NULL); + mgr->pipe->set_vertex_buffers(mgr->pipe, 0, num_vb, NULL); - for (i = 0; i < mgr->nr_vertex_buffers; i++) { + for (i = 0; i < PIPE_MAX_ATTRIBS; i++) { pipe_resource_reference(&mgr->vertex_buffer[i].buffer, NULL); } - for (i = 0; i < mgr->nr_real_vertex_buffers; i++) { + for (i = 0; i < PIPE_MAX_ATTRIBS; i++) { pipe_resource_reference(&mgr->real_vertex_buffer[i].buffer, NULL); } + pipe_resource_reference(&mgr->aux_vertex_buffer_saved.buffer, NULL); translate_cache_destroy(mgr->translate_cache); u_upload_destroy(mgr->uploader); @@ -267,39 +272,42 @@ u_vbuf_translate_buffers(struct u_vbuf *mgr, struct translate_key *key, struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {0}; struct pipe_resource *out_buffer = NULL; uint8_t *out_map; - unsigned i, out_offset; + unsigned out_offset, mask; /* Get a translate object. */ tr = translate_cache_find(mgr->translate_cache, key); /* Map buffers we want to translate. */ - for (i = 0; i < mgr->nr_vertex_buffers; i++) { - if (vb_mask & (1 << i)) { - struct pipe_vertex_buffer *vb = &mgr->vertex_buffer[i]; - unsigned offset = vb->buffer_offset + vb->stride * start_vertex; - uint8_t *map; + mask = vb_mask; + while (mask) { + struct pipe_vertex_buffer *vb; + unsigned offset; + uint8_t *map; + unsigned i = u_bit_scan(&mask); - if (vb->user_buffer) { - map = (uint8_t*)vb->user_buffer + offset; - } else { - unsigned size = vb->stride ? num_vertices * vb->stride - : sizeof(double)*4; + vb = &mgr->vertex_buffer[i]; + offset = vb->buffer_offset + vb->stride * start_vertex; - if (offset+size > vb->buffer->width0) { - size = vb->buffer->width0 - offset; - } + if (vb->user_buffer) { + map = (uint8_t*)vb->user_buffer + offset; + } else { + unsigned size = vb->stride ? num_vertices * vb->stride + : sizeof(double)*4; - map = pipe_buffer_map_range(mgr->pipe, vb->buffer, offset, size, - PIPE_TRANSFER_READ, &vb_transfer[i]); + if (offset+size > vb->buffer->width0) { + size = vb->buffer->width0 - offset; } - /* Subtract min_index so that indexing with the index buffer works. */ - if (unroll_indices) { - map -= vb->stride * min_index; - } + map = pipe_buffer_map_range(mgr->pipe, vb->buffer, offset, size, + PIPE_TRANSFER_READ, &vb_transfer[i]); + } - tr->set_buffer(tr, i, map, vb->stride, ~0); + /* Subtract min_index so that indexing with the index buffer works. */ + if (unroll_indices) { + map -= vb->stride * min_index; } + + tr->set_buffer(tr, i, map, vb->stride, ~0); } /* Translate. */ @@ -354,7 +362,10 @@ u_vbuf_translate_buffers(struct u_vbuf *mgr, struct translate_key *key, } /* Unmap all buffers. */ - for (i = 0; i < mgr->nr_vertex_buffers; i++) { + mask = vb_mask; + while (mask) { + unsigned i = u_bit_scan(&mask); + if (vb_transfer[i]) { pipe_buffer_unmap(mgr->pipe, vb_transfer[i]); } @@ -379,7 +390,7 @@ u_vbuf_translate_find_free_vb_slots(struct u_vbuf *mgr, /* Set the bit for each buffer which is incompatible, or isn't set. */ uint32_t unused_vb_mask = mgr->ve->incompatible_vb_mask_all | mgr->incompatible_vb_mask | - ~((1 << mgr->nr_vertex_buffers) - 1); + ~mgr->enabled_vb_mask; memset(fallback_vbs, ~0, sizeof(fallback_vbs)); @@ -389,20 +400,21 @@ u_vbuf_translate_find_free_vb_slots(struct u_vbuf *mgr, uint32_t index; if (!unused_vb_mask) { - /* fail, reset the number to its original value */ - mgr->nr_real_vertex_buffers = mgr->nr_vertex_buffers; return FALSE; } index = ffs(unused_vb_mask) - 1; fallback_vbs[type] = index; - if (index >= mgr->nr_real_vertex_buffers) { - mgr->nr_real_vertex_buffers = index + 1; - } /*printf("found slot=%i for type=%i\n", index, type);*/ } } + for (type = 0; type < VB_NUM; type++) { + if (mask[type]) { + mgr->dirty_real_vb_mask |= 1 << fallback_vbs[type]; + } + } + memcpy(mgr->fallback_vbs, fallback_vbs, sizeof(fallback_vbs)); return TRUE; } @@ -568,9 +580,11 @@ static void u_vbuf_translate_end(struct u_vbuf *mgr) if (vb != ~0) { pipe_resource_reference(&mgr->real_vertex_buffer[vb].buffer, NULL); mgr->fallback_vbs[i] = ~0; + + /* This will cause the buffer to be unbound in the driver later. */ + mgr->dirty_real_vb_mask |= 1 << vb; } } - mgr->nr_real_vertex_buffers = mgr->nr_vertex_buffers; } #define FORMAT_REPLACE(what, withwhat) \ @@ -700,45 +714,77 @@ static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso) FREE(ve); } -void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr, unsigned count, +void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr, + unsigned start_slot, unsigned count, const struct pipe_vertex_buffer *bufs) { unsigned i; + /* which buffers are enabled */ + uint32_t enabled_vb_mask = 0; + /* which buffers are in user memory */ + uint32_t user_vb_mask = 0; + /* which buffers are incompatible with the driver */ + uint32_t incompatible_vb_mask = 0; + /* which buffers have a non-zero stride */ + uint32_t nonzero_stride_vb_mask = 0; + uint32_t mask = ~(((1ull << count) - 1) << start_slot); + + /* Zero out the bits we are going to rewrite completely. */ + mgr->user_vb_mask &= mask; + mgr->incompatible_vb_mask &= mask; + mgr->nonzero_stride_vb_mask &= mask; + mgr->enabled_vb_mask &= mask; + + if (!bufs) { + struct pipe_context *pipe = mgr->pipe; + /* Unbind. */ + mgr->dirty_real_vb_mask &= mask; - mgr->user_vb_mask = 0; - mgr->incompatible_vb_mask = 0; - mgr->nonzero_stride_vb_mask = 0; + for (i = 0; i < count; i++) { + unsigned dst_index = start_slot + i; + + pipe_resource_reference(&mgr->vertex_buffer[dst_index].buffer, NULL); + pipe_resource_reference(&mgr->real_vertex_buffer[dst_index].buffer, + NULL); + } + + pipe->set_vertex_buffers(pipe, start_slot, count, NULL); + return; + } for (i = 0; i < count; i++) { + unsigned dst_index = start_slot + i; const struct pipe_vertex_buffer *vb = &bufs[i]; - struct pipe_vertex_buffer *orig_vb = &mgr->vertex_buffer[i]; - struct pipe_vertex_buffer *real_vb = &mgr->real_vertex_buffer[i]; + struct pipe_vertex_buffer *orig_vb = &mgr->vertex_buffer[dst_index]; + struct pipe_vertex_buffer *real_vb = &mgr->real_vertex_buffer[dst_index]; + + if (!vb->buffer && !vb->user_buffer) { + pipe_resource_reference(&orig_vb->buffer, NULL); + pipe_resource_reference(&real_vb->buffer, NULL); + real_vb->user_buffer = NULL; + continue; + } pipe_resource_reference(&orig_vb->buffer, vb->buffer); orig_vb->user_buffer = vb->user_buffer; real_vb->buffer_offset = orig_vb->buffer_offset = vb->buffer_offset; real_vb->stride = orig_vb->stride = vb->stride; - real_vb->user_buffer = NULL; if (vb->stride) { - mgr->nonzero_stride_vb_mask |= 1 << i; - } - - if (!vb->buffer && !vb->user_buffer) { - pipe_resource_reference(&real_vb->buffer, NULL); - continue; + nonzero_stride_vb_mask |= 1 << dst_index; } + enabled_vb_mask |= 1 << dst_index; if ((!mgr->caps.buffer_offset_unaligned && vb->buffer_offset % 4 != 0) || (!mgr->caps.buffer_stride_unaligned && vb->stride % 4 != 0)) { - mgr->incompatible_vb_mask |= 1 << i; + incompatible_vb_mask |= 1 << dst_index; pipe_resource_reference(&real_vb->buffer, NULL); continue; } if (!mgr->caps.user_vertex_buffers && vb->user_buffer) { - mgr->user_vb_mask |= 1 << i; + user_vb_mask |= 1 << dst_index; pipe_resource_reference(&real_vb->buffer, NULL); continue; } @@ -747,16 +793,14 @@ void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr, unsigned count, real_vb->user_buffer = vb->user_buffer; } - for (i = count; i < mgr->nr_vertex_buffers; i++) { - pipe_resource_reference(&mgr->vertex_buffer[i].buffer, NULL); - } - for (i = count; i < mgr->nr_real_vertex_buffers; i++) { - pipe_resource_reference(&mgr->real_vertex_buffer[i].buffer, NULL); - } + mgr->user_vb_mask |= user_vb_mask; + mgr->incompatible_vb_mask |= incompatible_vb_mask; + mgr->nonzero_stride_vb_mask |= nonzero_stride_vb_mask; + mgr->enabled_vb_mask |= enabled_vb_mask; - mgr->nr_vertex_buffers = count; - mgr->nr_real_vertex_buffers = count; - mgr->vertex_buffers_dirty = TRUE; + /* All changed buffers are marked as dirty, even the NULL ones, + * which will cause the NULL buffers to be unbound in the driver later. */ + mgr->dirty_real_vb_mask |= ~mask; } void u_vbuf_set_index_buffer(struct u_vbuf *mgr, @@ -782,7 +826,7 @@ u_vbuf_upload_buffers(struct u_vbuf *mgr, { unsigned i; unsigned nr_velems = mgr->ve->count; - unsigned nr_vbufs = mgr->nr_vertex_buffers; + unsigned nr_vbufs = util_last_bit(mgr->enabled_vb_mask); struct pipe_vertex_element *velems = mgr->using_translate ? mgr->fallback_velems : mgr->ve->ve; unsigned start_offset[PIPE_MAX_ATTRIBS]; @@ -978,6 +1022,19 @@ static void u_vbuf_get_minmax_index(struct pipe_context *pipe, } } +static void u_vbuf_set_driver_vertex_buffers(struct u_vbuf *mgr) +{ + struct pipe_context *pipe = mgr->pipe; + unsigned start_slot, count; + + start_slot = ffs(mgr->dirty_real_vb_mask) - 1; + count = util_last_bit(mgr->dirty_real_vb_mask >> start_slot); + + pipe->set_vertex_buffers(pipe, start_slot, count, + mgr->real_vertex_buffer + start_slot); + mgr->dirty_real_vb_mask = 0; +} + void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info) { struct pipe_context *pipe = mgr->pipe; @@ -990,11 +1047,10 @@ void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info) if (!mgr->incompatible_vb_mask && !mgr->ve->incompatible_elem_mask && !user_vb_mask) { + /* Set vertex buffers if needed. */ - if (mgr->vertex_buffers_dirty) { - pipe->set_vertex_buffers(pipe, mgr->nr_real_vertex_buffers, - mgr->real_vertex_buffer); - mgr->vertex_buffers_dirty = FALSE; + if (mgr->dirty_real_vb_mask) { + u_vbuf_set_driver_vertex_buffers(mgr); } pipe->draw_vbo(pipe, info); @@ -1062,6 +1118,7 @@ void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info) if (user_vb_mask) { u_vbuf_upload_buffers(mgr, start_vertex, num_vertices, info->start_instance, info->instance_count); + mgr->dirty_real_vb_mask |= user_vb_mask; } /* @@ -1086,8 +1143,7 @@ void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info) */ u_upload_unmap(mgr->uploader); - pipe->set_vertex_buffers(pipe, mgr->nr_real_vertex_buffers, - mgr->real_vertex_buffer); + u_vbuf_set_driver_vertex_buffers(mgr); if (unlikely(unroll_indices)) { struct pipe_draw_info new_info = *info; @@ -1105,7 +1161,6 @@ void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info) if (mgr->using_translate) { u_vbuf_translate_end(mgr); } - mgr->vertex_buffers_dirty = TRUE; } void u_vbuf_save_vertex_elements(struct u_vbuf *mgr) @@ -1126,22 +1181,18 @@ void u_vbuf_restore_vertex_elements(struct u_vbuf *mgr) mgr->ve_saved = NULL; } -void u_vbuf_save_vertex_buffers(struct u_vbuf *mgr) +void u_vbuf_save_aux_vertex_buffer_slot(struct u_vbuf *mgr) { - util_copy_vertex_buffers(mgr->vertex_buffer_saved, - &mgr->nr_vertex_buffers_saved, - mgr->vertex_buffer, - mgr->nr_vertex_buffers); + struct pipe_vertex_buffer *vb = + &mgr->vertex_buffer[mgr->aux_vertex_buffer_slot]; + + pipe_resource_reference(&mgr->aux_vertex_buffer_saved.buffer, vb->buffer); + memcpy(&mgr->aux_vertex_buffer_saved, vb, sizeof(*vb)); } -void u_vbuf_restore_vertex_buffers(struct u_vbuf *mgr) +void u_vbuf_restore_aux_vertex_buffer_slot(struct u_vbuf *mgr) { - unsigned i; - - u_vbuf_set_vertex_buffers(mgr, mgr->nr_vertex_buffers_saved, - mgr->vertex_buffer_saved); - for (i = 0; i < mgr->nr_vertex_buffers_saved; i++) { - pipe_resource_reference(&mgr->vertex_buffer_saved[i].buffer, NULL); - } - mgr->nr_vertex_buffers_saved = 0; + u_vbuf_set_vertex_buffers(mgr, mgr->aux_vertex_buffer_slot, 1, + &mgr->aux_vertex_buffer_saved); + pipe_resource_reference(&mgr->aux_vertex_buffer_saved.buffer, NULL); } diff --git a/src/gallium/auxiliary/util/u_vbuf.h b/src/gallium/auxiliary/util/u_vbuf.h index 59eb59a1190..0f82271649d 100644 --- a/src/gallium/auxiliary/util/u_vbuf.h +++ b/src/gallium/auxiliary/util/u_vbuf.h @@ -64,14 +64,15 @@ void u_vbuf_get_caps(struct pipe_screen *screen, struct u_vbuf_caps *caps); struct u_vbuf * u_vbuf_create(struct pipe_context *pipe, - struct u_vbuf_caps *caps); + struct u_vbuf_caps *caps, unsigned aux_vertex_buffer_index); void u_vbuf_destroy(struct u_vbuf *mgr); /* State and draw functions. */ void u_vbuf_set_vertex_elements(struct u_vbuf *mgr, unsigned count, const struct pipe_vertex_element *states); -void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr, unsigned count, +void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr, + unsigned start_slot, unsigned count, const struct pipe_vertex_buffer *bufs); void u_vbuf_set_index_buffer(struct u_vbuf *mgr, const struct pipe_index_buffer *ib); @@ -80,7 +81,7 @@ void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info); /* Save/restore functionality. */ void u_vbuf_save_vertex_elements(struct u_vbuf *mgr); void u_vbuf_restore_vertex_elements(struct u_vbuf *mgr); -void u_vbuf_save_vertex_buffers(struct u_vbuf *mgr); -void u_vbuf_restore_vertex_buffers(struct u_vbuf *mgr); +void u_vbuf_save_aux_vertex_buffer_slot(struct u_vbuf *mgr); +void u_vbuf_restore_aux_vertex_buffer_slot(struct u_vbuf *mgr); #endif |