X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Fstate_tracker%2Fst_draw.c;h=5040c6fa5ab9ebfb084a9a5979824c7455af12df;hb=636d01bd61cac83e13c3c64874e7e34e828ca93a;hp=34f75a379693f50ddd016a48c2faf5bf124c4c09;hpb=2a904fd6a0cb80eec6dec2bae07fd8778b04caf3;p=mesa.git diff --git a/src/mesa/state_tracker/st_draw.c b/src/mesa/state_tracker/st_draw.c index 34f75a37969..5040c6fa5ab 100644 --- a/src/mesa/state_tracker/st_draw.c +++ b/src/mesa/state_tracker/st_draw.c @@ -33,7 +33,7 @@ * * We basically convert the VBO's vertex attribute/array information into * Gallium vertex state, bind the vertex buffer objects and call - * pipe->draw_elements(), pipe->draw_range_elements() or pipe->draw_arrays(). + * pipe->draw_vbo(). * * Authors: * Keith Whitwell @@ -42,6 +42,7 @@ #include "main/imports.h" #include "main/image.h" +#include "main/bufferobj.h" #include "main/macros.h" #include "main/mfeatures.h" #include "program/prog_uniform.h" @@ -181,7 +182,7 @@ static GLuint fixed_types[4] = { /** * Return a PIPE_FORMAT_x for the given GL datatype and size. */ -GLuint +enum pipe_format st_pipe_vertex_format(GLenum type, GLuint size, GLenum format, GLboolean normalized) { @@ -211,7 +212,7 @@ st_pipe_vertex_format(GLenum type, GLuint size, GLenum format, case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1]; case GL_FIXED: return fixed_types[size-1]; default: assert(0); return 0; - } + } } else { switch (type) { @@ -226,19 +227,32 @@ st_pipe_vertex_format(GLenum type, GLuint size, GLenum format, case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1]; case GL_FIXED: return fixed_types[size-1]; default: assert(0); return 0; - } + } } - return 0; /* silence compiler warning */ + return PIPE_FORMAT_NONE; /* silence compiler warning */ } +/** + * This is very similar to vbo_all_varyings_in_vbos() but we test + * the stride. See bug 38626. + */ +static GLboolean +all_varyings_in_vbos(const struct gl_client_array *arrays[]) +{ + GLuint i; + + for (i = 0; i < VERT_ATTRIB_MAX; i++) + if (arrays[i]->StrideB && !_mesa_is_bufferobj(arrays[i]->BufferObj)) + return GL_FALSE; + return GL_TRUE; +} /** * Examine the active arrays to determine if we have interleaved * vertex arrays all living in one VBO, or all living in user space. - * \param userSpace returns whether the arrays are in user space. */ static GLboolean is_interleaved_arrays(const struct st_vertex_program *vp, @@ -248,37 +262,35 @@ is_interleaved_arrays(const struct st_vertex_program *vp, GLuint attr; const struct gl_buffer_object *firstBufObj = NULL; GLint firstStride = -1; - const GLubyte *client_addr = NULL; + const GLubyte *firstPtr = NULL; + GLboolean userSpaceBuffer = GL_FALSE; for (attr = 0; attr < vpv->num_inputs; attr++) { const GLuint mesaAttr = vp->index_to_input[attr]; - const struct gl_buffer_object *bufObj = arrays[mesaAttr]->BufferObj; - const GLsizei stride = arrays[mesaAttr]->StrideB; /* in bytes */ + const struct gl_client_array *array = arrays[mesaAttr]; + const struct gl_buffer_object *bufObj = array->BufferObj; + const GLsizei stride = array->StrideB; /* in bytes */ - if (firstStride < 0) { + if (attr == 0) { + /* save info about the first array */ firstStride = stride; - } - else if (firstStride != stride) { - return GL_FALSE; - } - - if (!bufObj || !bufObj->Name) { - /* Try to detect if the client-space arrays are - * "close" to each other. - */ - if (!client_addr) { - client_addr = arrays[mesaAttr]->Ptr; - } - else if (abs(arrays[mesaAttr]->Ptr - client_addr) > firstStride) { - /* arrays start too far apart */ - return GL_FALSE; - } - } - else if (!firstBufObj) { + firstPtr = array->Ptr; firstBufObj = bufObj; + userSpaceBuffer = !bufObj || !bufObj->Name; } - else if (bufObj != firstBufObj) { - return GL_FALSE; + else { + /* check if other arrays interleave with the first, in same buffer */ + if (stride != firstStride) + return GL_FALSE; /* strides don't match */ + + if (bufObj != firstBufObj) + return GL_FALSE; /* arrays in different VBOs */ + + if (abs(array->Ptr - firstPtr) > firstStride) + return GL_FALSE; /* arrays start too far apart */ + + if ((!bufObj || !_mesa_is_bufferobj(bufObj)) != userSpaceBuffer) + return GL_FALSE; /* mix of VBO and user-space arrays */ } } @@ -299,50 +311,68 @@ setup_interleaved_attribs(struct gl_context *ctx, const struct gl_client_array **arrays, struct pipe_vertex_buffer *vbuffer, struct pipe_vertex_element velements[], - unsigned max_index) + unsigned max_index, + unsigned num_instances) { struct st_context *st = st_context(ctx); struct pipe_context *pipe = st->pipe; GLuint attr; const GLubyte *low_addr = NULL; - /* Find the lowest address. */ - for (attr = 0; attr < vpv->num_inputs; attr++) { - const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr; + /* Find the lowest address of the arrays we're drawing */ + if (vpv->num_inputs) { + low_addr = arrays[vp->index_to_input[0]]->Ptr; - low_addr = !low_addr ? start : MIN2(low_addr, start); + for (attr = 1; attr < vpv->num_inputs; attr++) { + const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr; + low_addr = MIN2(low_addr, start); + } } for (attr = 0; attr < vpv->num_inputs; attr++) { const GLuint mesaAttr = vp->index_to_input[attr]; - struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj; + const struct gl_client_array *array = arrays[mesaAttr]; + struct gl_buffer_object *bufobj = array->BufferObj; struct st_buffer_object *stobj = st_buffer_object(bufobj); - GLsizei stride = arrays[mesaAttr]->StrideB; + unsigned src_offset = (unsigned) (array->Ptr - low_addr); + GLuint element_size = array->_ElementSize; + GLsizei stride = array->StrideB; + + assert(element_size == array->Size * _mesa_sizeof_type(array->Type)); if (attr == 0) { - if (bufobj && bufobj->Name) { + if (bufobj && _mesa_is_bufferobj(bufobj)) { vbuffer->buffer = NULL; pipe_resource_reference(&vbuffer->buffer, stobj->buffer); vbuffer->buffer_offset = pointer_to_offset(low_addr); - } else { - vbuffer->buffer = - pipe_user_buffer_create(pipe->screen, (void*)low_addr, - stride * (max_index + 1), - PIPE_BIND_VERTEX_BUFFER); + } + else { + uint divisor = array->InstanceDivisor; + uint last_index = divisor ? num_instances / divisor : max_index; + uint bytes = src_offset + stride * last_index + element_size; + + vbuffer->buffer = pipe_user_buffer_create(pipe->screen, + (void*) low_addr, + bytes, + PIPE_BIND_VERTEX_BUFFER); vbuffer->buffer_offset = 0; + + /* Track user vertex buffers. */ + pipe_resource_reference(&st->user_attrib[0].buffer, vbuffer->buffer); + st->user_attrib[0].element_size = element_size; + st->user_attrib[0].stride = stride; + st->num_user_attribs = 1; } vbuffer->stride = stride; /* in bytes */ } - velements[attr].src_offset = - (unsigned) (arrays[mesaAttr]->Ptr - low_addr); - velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor; + velements[attr].src_offset = src_offset; + velements[attr].instance_divisor = array->InstanceDivisor; velements[attr].vertex_buffer_index = 0; - velements[attr].src_format = - st_pipe_vertex_format(arrays[mesaAttr]->Type, - arrays[mesaAttr]->Size, - arrays[mesaAttr]->Format, - arrays[mesaAttr]->Normalized); + velements[attr].src_format = st_pipe_vertex_format(array->Type, + array->Size, + array->Format, + array->Normalized); assert(velements[attr].src_format); } } @@ -361,7 +391,8 @@ setup_non_interleaved_attribs(struct gl_context *ctx, const struct gl_client_array **arrays, struct pipe_vertex_buffer vbuffer[], struct pipe_vertex_element velements[], - unsigned max_index) + unsigned max_index, + unsigned num_instances) { struct st_context *st = st_context(ctx); struct pipe_context *pipe = st->pipe; @@ -369,10 +400,14 @@ setup_non_interleaved_attribs(struct gl_context *ctx, for (attr = 0; attr < vpv->num_inputs; attr++) { const GLuint mesaAttr = vp->index_to_input[attr]; - struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj; - GLsizei stride = arrays[mesaAttr]->StrideB; + const struct gl_client_array *array = arrays[mesaAttr]; + struct gl_buffer_object *bufobj = array->BufferObj; + GLuint element_size = array->_ElementSize; + GLsizei stride = array->StrideB; + + assert(element_size == array->Size * _mesa_sizeof_type(array->Type)); - if (bufobj && bufobj->Name) { + if (bufobj && _mesa_is_bufferobj(bufobj)) { /* Attribute data is in a VBO. * Recall that for VBOs, the gl_client_array->Ptr field is * really an offset from the start of the VBO, not a pointer. @@ -382,42 +417,54 @@ setup_non_interleaved_attribs(struct gl_context *ctx, vbuffer[attr].buffer = NULL; pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer); - vbuffer[attr].buffer_offset = pointer_to_offset(arrays[mesaAttr]->Ptr); + vbuffer[attr].buffer_offset = pointer_to_offset(array->Ptr); } else { /* wrap user data */ - if (arrays[mesaAttr]->Ptr) { - vbuffer[attr].buffer = - pipe_user_buffer_create(pipe->screen, - (void *) arrays[mesaAttr]->Ptr, - stride * (max_index + 1), - PIPE_BIND_VERTEX_BUFFER); + uint bytes; + void *ptr; + + if (array->Ptr) { + uint divisor = array->InstanceDivisor; + uint last_index = divisor ? num_instances / divisor : max_index; + + bytes = stride * last_index + element_size; + + ptr = (void *) array->Ptr; } else { /* no array, use ctx->Current.Attrib[] value */ - uint bytes = sizeof(ctx->Current.Attrib[0]); - vbuffer[attr].buffer = - pipe_user_buffer_create(pipe->screen, - (void *) ctx->Current.Attrib[mesaAttr], - bytes, - PIPE_BIND_VERTEX_BUFFER); + bytes = element_size = sizeof(ctx->Current.Attrib[0]); + ptr = (void *) ctx->Current.Attrib[mesaAttr]; stride = 0; } + assert(ptr); + assert(bytes); + + vbuffer[attr].buffer = + pipe_user_buffer_create(pipe->screen, ptr, bytes, + PIPE_BIND_VERTEX_BUFFER); + vbuffer[attr].buffer_offset = 0; + + /* Track user vertex buffers. */ + pipe_resource_reference(&st->user_attrib[attr].buffer, vbuffer[attr].buffer); + st->user_attrib[attr].element_size = element_size; + st->user_attrib[attr].stride = stride; + st->num_user_attribs = MAX2(st->num_user_attribs, attr + 1); } /* common-case setup */ vbuffer[attr].stride = stride; /* in bytes */ velements[attr].src_offset = 0; - velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor; + velements[attr].instance_divisor = array->InstanceDivisor; velements[attr].vertex_buffer_index = attr; - velements[attr].src_format - = st_pipe_vertex_format(arrays[mesaAttr]->Type, - arrays[mesaAttr]->Size, - arrays[mesaAttr]->Format, - arrays[mesaAttr]->Normalized); + velements[attr].src_format = st_pipe_vertex_format(array->Type, + array->Size, + array->Format, + array->Normalized); assert(velements[attr].src_format); } } @@ -451,7 +498,7 @@ setup_index_buffer(struct gl_context *ctx, } /* get/create the index buffer object */ - if (bufobj && bufobj->Name) { + if (bufobj && _mesa_is_bufferobj(bufobj)) { /* elements/indexes are in a real VBO */ struct st_buffer_object *stobj = st_buffer_object(bufobj); pipe_resource_reference(&ibuffer->buffer, stobj->buffer); @@ -467,6 +514,7 @@ setup_index_buffer(struct gl_context *ctx, } } + /** * Prior to drawing, check that any uniforms referenced by the * current shader have been set. If a uniform has not been set, @@ -513,8 +561,8 @@ translate_prim(const struct gl_context *ctx, unsigned prim) assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY); /* Avoid quadstrips if it's easy to do so: - * Note: it's imporant to do the correct trimming if we change the prim type! - * We do that wherever this function is called. + * Note: it's important to do the correct trimming if we change the + * prim type! We do that wherever this function is called. */ if (prim == GL_QUAD_STRIP && ctx->Light.ShadeModel != GL_FLAT && @@ -529,7 +577,8 @@ translate_prim(const struct gl_context *ctx, unsigned prim) static void st_validate_varrays(struct gl_context *ctx, const struct gl_client_array **arrays, - unsigned max_index) + unsigned max_index, + unsigned num_instances) { struct st_context *st = st_context(ctx); const struct st_vertex_program *vp; @@ -538,18 +587,27 @@ st_validate_varrays(struct gl_context *ctx, struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS]; unsigned num_vbuffers, num_velements; GLuint attr; + unsigned i; /* must get these after state validation! */ vp = st->vp; vpv = st->vp_variant; memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs); + + /* Unreference any user vertex buffers. */ + for (i = 0; i < st->num_user_attribs; i++) { + pipe_resource_reference(&st->user_attrib[i].buffer, NULL); + } + st->num_user_attribs = 0; + /* * Setup the vbuffer[] and velements[] arrays. */ if (is_interleaved_arrays(vp, vpv, arrays)) { setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements, - max_index); + max_index, num_instances); + num_vbuffers = 1; num_velements = vpv->num_inputs; if (num_velements == 0) @@ -557,7 +615,8 @@ st_validate_varrays(struct gl_context *ctx, } else { setup_non_interleaved_attribs(ctx, vp, vpv, arrays, - vbuffer, velements, max_index); + vbuffer, velements, max_index, + num_instances); num_vbuffers = vpv->num_inputs; num_velements = vpv->num_inputs; } @@ -594,9 +653,10 @@ st_draw_vbo(struct gl_context *ctx, struct pipe_context *pipe = st->pipe; struct pipe_index_buffer ibuffer; struct pipe_draw_info info; - unsigned i; + unsigned i, num_instances = 1; GLboolean new_array = - st->dirty.st && (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM)) != 0; + st->dirty.st && + (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT)) != 0; /* Mesa core state should have been validated already */ assert(ctx->NewState == 0x0); @@ -604,9 +664,14 @@ st_draw_vbo(struct gl_context *ctx, if (ib) { /* Gallium probably doesn't want this in some cases. */ if (!index_bounds_valid) - if (!vbo_all_varyings_in_vbos(arrays)) + if (!all_varyings_in_vbos(arrays)) vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index); - } else { + + for (i = 0; i < nr_prims; i++) { + num_instances = MAX2(num_instances, prims[i].num_instances); + } + } + else { /* Get min/max index for non-indexed drawing. */ min_index = ~0; max_index = 0; @@ -614,6 +679,7 @@ st_draw_vbo(struct gl_context *ctx, for (i = 0; i < nr_prims; i++) { min_index = MIN2(min_index, prims[i].start); max_index = MAX2(max_index, prims[i].start + prims[i].count - 1); + num_instances = MAX2(num_instances, prims[i].num_instances); } } @@ -634,7 +700,7 @@ st_draw_vbo(struct gl_context *ctx, st_validate_state(st); if (new_array) { - st_validate_varrays(ctx, arrays, max_index); + st_validate_varrays(ctx, arrays, max_index, num_instances); } #if 0 @@ -646,6 +712,26 @@ st_draw_vbo(struct gl_context *ctx, #endif } + /* Notify the driver that the content of user buffers may have been + * changed. */ + assert(max_index >= min_index); + if (!new_array && st->num_user_attribs) { + for (i = 0; i < st->num_user_attribs; i++) { + if (st->user_attrib[i].buffer) { + unsigned element_size = st->user_attrib[i].element_size; + unsigned stride = st->user_attrib[i].stride; + unsigned min_offset = min_index * stride; + unsigned max_offset = max_index * stride + element_size; + + assert(max_offset > min_offset); + + pipe->redefine_user_buffer(pipe, st->user_attrib[i].buffer, + min_offset, + max_offset - min_offset); + } + } + } + setup_index_buffer(ctx, ib, &ibuffer); pipe->set_index_buffer(pipe, &ibuffer); @@ -658,8 +744,8 @@ st_draw_vbo(struct gl_context *ctx, } } - info.primitive_restart = st->ctx->Array.PrimitiveRestart; - info.restart_index = st->ctx->Array.RestartIndex; + info.primitive_restart = ctx->Array.PrimitiveRestart; + info.restart_index = ctx->Array.RestartIndex; /* do actual drawing */ for (i = 0; i < nr_prims; i++) { @@ -681,7 +767,8 @@ st_draw_vbo(struct gl_context *ctx, } -void st_init_draw( struct st_context *st ) +void +st_init_draw(struct st_context *st) { struct gl_context *ctx = st->ctx; @@ -701,11 +788,10 @@ void st_init_draw( struct st_context *st ) } -void st_destroy_draw( struct st_context *st ) +void +st_destroy_draw(struct st_context *st) { #if FEATURE_feedback || FEATURE_rastpos draw_destroy(st->draw); #endif } - -