+static boolean index_bias_supported(struct r300_context *r300)
+{
+ return r300->screen->caps.is_r500 &&
+ r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
+}
+
+static void r500_emit_index_bias(struct r300_context *r300, int index_bias)
+{
+ CS_LOCALS(r300);
+
+ BEGIN_CS(2);
+ OUT_CS_REG(R500_VAP_INDEX_OFFSET,
+ (index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
+ END_CS;
+}
+
+/* This function splits the index bias value into two parts:
+ * - buffer_offset: the value that can be safely added to buffer offsets
+ * in r300_emit_aos (it must yield a positive offset when added to
+ * a vertex buffer offset)
+ * - index_offset: the value that must be manually subtracted from indices
+ * in an index buffer to achieve negative offsets. */
+static void r300_split_index_bias(struct r300_context *r300, int index_bias,
+ int *buffer_offset, int *index_offset)
+{
+ struct pipe_vertex_buffer *vb, *vbufs = r300->vertex_buffer;
+ struct pipe_vertex_element *velem = r300->velems->velem;
+ unsigned i, size;
+ int max_neg_bias;
+
+ if (index_bias < 0) {
+ /* See how large index bias we may subtract. We must be careful
+ * here because negative buffer offsets are not allowed
+ * by the DRM API. */
+ max_neg_bias = INT_MAX;
+ for (i = 0; i < r300->velems->count; i++) {
+ vb = &vbufs[velem[i].vertex_buffer_index];
+ size = (vb->buffer_offset + velem[i].src_offset) / vb->stride;
+ max_neg_bias = MIN2(max_neg_bias, size);
+ }
+
+ /* Now set the minimum allowed value. */
+ *buffer_offset = MAX2(-max_neg_bias, index_bias);
+ } else {
+ /* A positive index bias is OK. */
+ *buffer_offset = index_bias;
+ }
+
+ *index_offset = index_bias - *buffer_offset;
+}
+
+enum r300_prepare_flags {
+ PREP_FIRST_DRAW = (1 << 0), /* call emit_dirty_state and friends? */
+ PREP_VALIDATE_VBOS = (1 << 1), /* validate VBOs? */
+ PREP_EMIT_AOS = (1 << 2), /* call emit_aos? */
+ PREP_EMIT_AOS_SWTCL = (1 << 3), /* call emit_aos_swtcl? */
+ PREP_INDEXED = (1 << 4) /* is this draw_elements? */
+};
+
+/**
+ * Check if the requested number of dwords is available in the CS and
+ * if not, flush. Then validate buffers and emit dirty state.
+ * \param r300 The context.
+ * \param flags See r300_prepare_flags.
+ * \param index_buffer The index buffer to validate. The parameter may be NULL.
+ * \param cs_dwords The number of dwords to reserve in CS.
+ * \param aos_offset The offset passed to emit_aos.
+ * \param index_bias The index bias to emit.
+ * \param end_cs_dwords The number of free dwords which must be available
+ * at the end of CS after drawing in case the CS space
+ * management is performed by a draw_* function manually.
+ * The parameter may be NULL.
+ */
+static void r300_prepare_for_rendering(struct r300_context *r300,
+ enum r300_prepare_flags flags,
+ struct pipe_resource *index_buffer,
+ unsigned cs_dwords,
+ int aos_offset,
+ int index_bias,
+ unsigned *end_cs_dwords)