static void
generate_fetch(struct gallivm_state *gallivm,
+ struct draw_context *draw,
LLVMValueRef vbuffers_ptr,
LLVMValueRef *res,
struct pipe_vertex_element *velem,
struct lp_build_if_state if_ctx;
if (velem->instance_divisor) {
- /* array index = instance_id / instance_divisor */
- index = LLVMBuildUDiv(builder, instance_id,
- lp_build_const_int32(gallivm, velem->instance_divisor),
- "instance_divisor");
+ /* Index is equal to the start instance plus the number of current
+ * instance divided by the divisor. In this case we compute it as:
+ * index = start_instance + ((instance_id - start_instance) / divisor)
+ */
+ LLVMValueRef current_instance;
+ index = lp_build_const_int32(gallivm, draw->start_instance);
+ current_instance = LLVMBuildSub(builder, instance_id, index, "");
+ current_instance = LLVMBuildUDiv(builder, current_instance,
+ lp_build_const_int32(gallivm, velem->instance_divisor),
+ "instance_divisor");
+ index = LLVMBuildAdd(builder, index, current_instance, "instance");
}
stride = lp_build_umul_overflow(gallivm, vb_stride, index, &ofbit);
LLVMValueRef vb_index =
lp_build_const_int32(gallivm, velem->vertex_buffer_index);
LLVMValueRef vb = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, "");
- generate_fetch(gallivm, vbuffers_ptr,
+ generate_fetch(gallivm, draw, vbuffers_ptr,
&aos_attribs[j][i], velem, vb, true_index,
system_values.instance_id);
}