/** * Handle primitive restart in software. * * This function breaks up calls into the driver so primitive restart * support is not required in the driver. */ void vbo_sw_primitive_restart(struct gl_context *ctx, const struct _mesa_prim *prims, GLuint nr_prims, const struct _mesa_index_buffer *ib, struct gl_buffer_object *indirect) { GLuint prim_num; struct _mesa_prim new_prim; struct _mesa_index_buffer new_ib; struct sub_primitive *sub_prims; struct sub_primitive *sub_prim; GLuint num_sub_prims; GLuint sub_prim_num; GLuint end_index; GLuint sub_end_index; GLuint restart_index = _mesa_primitive_restart_index(ctx, ib->index_size); struct _mesa_prim temp_prim; struct vbo_context *vbo = vbo_context(ctx); vbo_draw_func draw_prims_func = vbo->draw_prims; GLboolean map_ib = ib->obj->Name && !ib->obj->Mappings[MAP_INTERNAL].Pointer; void *ptr; /* If there is an indirect buffer, map it and extract the draw params */ if (indirect && prims[0].is_indirect) { const uint32_t *indirect_params; if (!ctx->Driver.MapBufferRange(ctx, 0, indirect->Size, GL_MAP_READ_BIT, indirect, MAP_INTERNAL)) { /* something went wrong with mapping, give up */ _mesa_error(ctx, GL_OUT_OF_MEMORY, "failed to map indirect buffer for sw primitive restart"); return; } assert(nr_prims == 1); new_prim = prims[0]; indirect_params = (const uint32_t *) ADD_POINTERS(indirect->Mappings[MAP_INTERNAL].Pointer, new_prim.indirect_offset); new_prim.is_indirect = 0; new_prim.count = indirect_params[0]; new_prim.num_instances = indirect_params[1]; new_prim.start = indirect_params[2]; new_prim.basevertex = indirect_params[3]; new_prim.base_instance = indirect_params[4]; new_ib = *ib; new_ib.count = new_prim.count; prims = &new_prim; ib = &new_ib; ctx->Driver.UnmapBuffer(ctx, indirect, MAP_INTERNAL); } /* Find the sub-primitives. These are regions in the index buffer which * are split based on the primitive restart index value. */ if (map_ib) { ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT, ib->obj, MAP_INTERNAL); } ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr); sub_prims = find_sub_primitives(ptr, ib->index_size, 0, ib->count, restart_index, &num_sub_prims); if (map_ib) { ctx->Driver.UnmapBuffer(ctx, ib->obj, MAP_INTERNAL); } /* Loop over the primitives, and use the located sub-primitives to draw * each primitive with a break to implement each primitive restart. */ for (prim_num = 0; prim_num < nr_prims; prim_num++) { end_index = prims[prim_num].start + prims[prim_num].count; memcpy(&temp_prim, &prims[prim_num], sizeof (temp_prim)); /* Loop over the sub-primitives drawing sub-ranges of the primitive. */ for (sub_prim_num = 0; sub_prim_num < num_sub_prims; sub_prim_num++) { sub_prim = &sub_prims[sub_prim_num]; sub_end_index = sub_prim->start + sub_prim->count; if (prims[prim_num].start <= sub_prim->start) { temp_prim.start = MAX2(prims[prim_num].start, sub_prim->start); temp_prim.count = MIN2(sub_end_index, end_index) - temp_prim.start; if ((temp_prim.start == sub_prim->start) && (temp_prim.count == sub_prim->count)) { draw_prims_func(ctx, &temp_prim, 1, ib, GL_TRUE, sub_prim->min_index, sub_prim->max_index, NULL, 0, NULL); } else { draw_prims_func(ctx, &temp_prim, 1, ib, GL_FALSE, -1, -1, NULL, 0, NULL); } } if (sub_end_index >= end_index) { break; } } } free(sub_prims); }
/** * Handle primitive restart in software. * * This function breaks up calls into the driver so primitive restart * support is not required in the driver. */ void vbo_sw_primitive_restart(struct gl_context *ctx, const struct _mesa_prim *prims, GLuint nr_prims, const struct _mesa_index_buffer *ib) { GLuint prim_num; struct sub_primitive *sub_prims; struct sub_primitive *sub_prim; GLuint num_sub_prims; GLuint sub_prim_num; GLuint end_index; GLuint sub_end_index; GLuint restart_index = ctx->Array.RestartIndex; struct _mesa_prim temp_prim; struct vbo_context *vbo = vbo_context(ctx); vbo_draw_func draw_prims_func = vbo->draw_prims; GLboolean map_ib = ib->obj->Name && !ib->obj->Pointer; void *ptr; /* Find the sub-primitives. These are regions in the index buffer which * are split based on the primitive restart index value. */ if (map_ib) { ctx->Driver.MapBufferRange(ctx, 0, ib->obj->Size, GL_MAP_READ_BIT, ib->obj); } ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr); sub_prims = find_sub_primitives(ptr, vbo_sizeof_ib_type(ib->type), 0, ib->count, restart_index, &num_sub_prims); if (map_ib) { ctx->Driver.UnmapBuffer(ctx, ib->obj); } /* Loop over the primitives, and use the located sub-primitives to draw * each primitive with a break to implement each primitive restart. */ for (prim_num = 0; prim_num < nr_prims; prim_num++) { end_index = prims[prim_num].start + prims[prim_num].count; memcpy(&temp_prim, &prims[prim_num], sizeof (temp_prim)); /* Loop over the sub-primitives drawing sub-ranges of the primitive. */ for (sub_prim_num = 0; sub_prim_num < num_sub_prims; sub_prim_num++) { sub_prim = &sub_prims[sub_prim_num]; sub_end_index = sub_prim->start + sub_prim->count; if (prims[prim_num].start <= sub_prim->start) { temp_prim.start = MAX2(prims[prim_num].start, sub_prim->start); temp_prim.count = MIN2(sub_end_index, end_index) - temp_prim.start; if ((temp_prim.start == sub_prim->start) && (temp_prim.count == sub_prim->count)) { draw_prims_func(ctx, &temp_prim, 1, ib, GL_TRUE, sub_prim->min_index, sub_prim->max_index, NULL); } else { draw_prims_func(ctx, &temp_prim, 1, ib, GL_FALSE, -1, -1, NULL); } } if (sub_end_index >= end_index) { break; } } } free(sub_prims); }