r300g: workaround index bias on chipsets that do not support it
This commit is contained in:
parent
31a74a6df7
commit
eb2a1521cf
|
@ -815,7 +815,7 @@ void r300_emit_textures_state(struct r300_context *r300,
|
||||||
END_CS;
|
END_CS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void r300_emit_aos(struct r300_context* r300, unsigned offset, boolean indexed)
|
void r300_emit_aos(struct r300_context* r300, int offset, boolean indexed)
|
||||||
{
|
{
|
||||||
struct pipe_vertex_buffer *vb1, *vb2, *vbuf = r300->vertex_buffer;
|
struct pipe_vertex_buffer *vb1, *vb2, *vbuf = r300->vertex_buffer;
|
||||||
struct pipe_vertex_element *velem = r300->velems->velem;
|
struct pipe_vertex_element *velem = r300->velems->velem;
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
struct rX00_fragment_program_code;
|
struct rX00_fragment_program_code;
|
||||||
struct r300_vertex_program_code;
|
struct r300_vertex_program_code;
|
||||||
|
|
||||||
void r300_emit_aos(struct r300_context* r300, unsigned offset, boolean indexed);
|
void r300_emit_aos(struct r300_context* r300, int offset, boolean indexed);
|
||||||
|
|
||||||
void r300_emit_blend_state(struct r300_context* r300,
|
void r300_emit_blend_state(struct r300_context* r300,
|
||||||
unsigned size, void* state);
|
unsigned size, void* state);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
|
* Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
|
||||||
|
* Copyright 2010 Marek Olšák <maraeo@gmail.com>
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
* copy of this software and associated documentation files (the "Software"),
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
|
@ -40,6 +41,8 @@
|
||||||
#include "r300_reg.h"
|
#include "r300_reg.h"
|
||||||
#include "r300_state_derived.h"
|
#include "r300_state_derived.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
static uint32_t r300_translate_primitive(unsigned prim)
|
static uint32_t r300_translate_primitive(unsigned prim)
|
||||||
{
|
{
|
||||||
switch (prim) {
|
switch (prim) {
|
||||||
|
@ -113,55 +116,100 @@ static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
|
||||||
return color_control;
|
return color_control;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void r500_emit_index_offset(struct r300_context *r300, int index_bias)
|
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);
|
CS_LOCALS(r300);
|
||||||
|
|
||||||
if (r300->screen->caps.is_r500 &&
|
BEGIN_CS(2);
|
||||||
r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0)) {
|
OUT_CS_REG(R500_VAP_INDEX_OFFSET,
|
||||||
BEGIN_CS(2);
|
(index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
|
||||||
OUT_CS_REG(R500_VAP_INDEX_OFFSET,
|
END_CS;
|
||||||
(index_bias & 0xFFFFFF) | (index_bias < 0 ? 1<<24 : 0));
|
}
|
||||||
END_CS;
|
|
||||||
} else {
|
/* This function splits the index bias value into two parts:
|
||||||
if (index_bias) {
|
* - buffer_offset: the value that can be safely added to buffer offsets
|
||||||
fprintf(stderr, "r300: Non-zero index bias is unsupported "
|
* in r300_emit_aos (it must yield a positive offset when added to
|
||||||
"on this hardware.\n");
|
* a vertex buffer offset)
|
||||||
assert(0);
|
* - 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 {
|
enum r300_prepare_flags {
|
||||||
PREP_FIRST_DRAW = (1 << 0),
|
PREP_FIRST_DRAW = (1 << 0), /* call emit_dirty_state and friends? */
|
||||||
PREP_VALIDATE_VBOS = (1 << 1),
|
PREP_VALIDATE_VBOS = (1 << 1), /* validate VBOs? */
|
||||||
PREP_EMIT_AOS = (1 << 2),
|
PREP_EMIT_AOS = (1 << 2), /* call emit_aos? */
|
||||||
PREP_EMIT_AOS_SWTCL = (1 << 3),
|
PREP_EMIT_AOS_SWTCL = (1 << 3), /* call emit_aos_swtcl? */
|
||||||
PREP_INDEXED = (1 << 4)
|
PREP_INDEXED = (1 << 4) /* is this draw_elements? */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Check if the requested number of dwords is available in the CS and
|
/**
|
||||||
|
* Check if the requested number of dwords is available in the CS and
|
||||||
* if not, flush. Then validate buffers and emit dirty state.
|
* if not, flush. Then validate buffers and emit dirty state.
|
||||||
* Return TRUE if flush occured. */
|
* \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,
|
static void r300_prepare_for_rendering(struct r300_context *r300,
|
||||||
enum r300_prepare_flags flags,
|
enum r300_prepare_flags flags,
|
||||||
struct pipe_resource *index_buffer,
|
struct pipe_resource *index_buffer,
|
||||||
unsigned cs_dwords,
|
unsigned cs_dwords,
|
||||||
unsigned aos_offset,
|
int aos_offset,
|
||||||
int index_bias,
|
int index_bias,
|
||||||
unsigned *end_cs_dwords)
|
unsigned *end_cs_dwords)
|
||||||
{
|
{
|
||||||
boolean flushed = FALSE;
|
unsigned end_dwords = 0;
|
||||||
boolean first_draw = flags & PREP_FIRST_DRAW;
|
boolean flushed = FALSE;
|
||||||
boolean emit_aos = flags & PREP_EMIT_AOS;
|
boolean first_draw = flags & PREP_FIRST_DRAW;
|
||||||
|
boolean emit_aos = flags & PREP_EMIT_AOS;
|
||||||
boolean emit_aos_swtcl = flags & PREP_EMIT_AOS_SWTCL;
|
boolean emit_aos_swtcl = flags & PREP_EMIT_AOS_SWTCL;
|
||||||
unsigned end_dwords = 0;
|
boolean indexed = flags & PREP_INDEXED;
|
||||||
|
boolean hw_index_bias = index_bias_supported(r300);
|
||||||
|
|
||||||
/* Add dirty state, index offset, and AOS. */
|
/* Add dirty state, index offset, and AOS. */
|
||||||
if (first_draw) {
|
if (first_draw) {
|
||||||
cs_dwords += r300_get_num_dirty_dwords(r300);
|
cs_dwords += r300_get_num_dirty_dwords(r300);
|
||||||
|
|
||||||
if (r300->screen->caps.is_r500)
|
if (hw_index_bias)
|
||||||
cs_dwords += 2; /* emit_index_offset */
|
cs_dwords += 2; /* emit_index_offset */
|
||||||
|
|
||||||
if (emit_aos)
|
if (emit_aos)
|
||||||
|
@ -186,11 +234,18 @@ static void r300_prepare_for_rendering(struct r300_context *r300,
|
||||||
if (first_draw || flushed) {
|
if (first_draw || flushed) {
|
||||||
r300_emit_buffer_validate(r300, flags & PREP_VALIDATE_VBOS, index_buffer);
|
r300_emit_buffer_validate(r300, flags & PREP_VALIDATE_VBOS, index_buffer);
|
||||||
r300_emit_dirty_state(r300);
|
r300_emit_dirty_state(r300);
|
||||||
r500_emit_index_offset(r300, index_bias);
|
if (hw_index_bias) {
|
||||||
|
if (r300->screen->caps.has_tcl)
|
||||||
|
r500_emit_index_bias(r300, index_bias);
|
||||||
|
else
|
||||||
|
r500_emit_index_bias(r300, 0);
|
||||||
|
}
|
||||||
|
|
||||||
if (emit_aos)
|
if (emit_aos)
|
||||||
r300_emit_aos(r300, aos_offset, flags & PREP_INDEXED);
|
r300_emit_aos(r300, aos_offset, indexed);
|
||||||
|
|
||||||
if (emit_aos_swtcl)
|
if (emit_aos_swtcl)
|
||||||
r300_emit_aos_swtcl(r300, flags & PREP_INDEXED);
|
r300_emit_aos_swtcl(r300, indexed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end_cs_dwords)
|
if (end_cs_dwords)
|
||||||
|
@ -429,6 +484,7 @@ static void r300_emit_draw_elements(struct r300_context *r300,
|
||||||
|
|
||||||
static void r300_shorten_ubyte_elts(struct r300_context* r300,
|
static void r300_shorten_ubyte_elts(struct r300_context* r300,
|
||||||
struct pipe_resource** elts,
|
struct pipe_resource** elts,
|
||||||
|
int index_bias,
|
||||||
unsigned start,
|
unsigned start,
|
||||||
unsigned count)
|
unsigned count)
|
||||||
{
|
{
|
||||||
|
@ -450,7 +506,7 @@ static void r300_shorten_ubyte_elts(struct r300_context* r300,
|
||||||
in_map += start;
|
in_map += start;
|
||||||
|
|
||||||
for (i = 0; i < count; i++) {
|
for (i = 0; i < count; i++) {
|
||||||
*out_map = (unsigned short)*in_map;
|
*out_map = (unsigned short)(*in_map + index_bias);
|
||||||
in_map++;
|
in_map++;
|
||||||
out_map++;
|
out_map++;
|
||||||
}
|
}
|
||||||
|
@ -461,27 +517,69 @@ static void r300_shorten_ubyte_elts(struct r300_context* r300,
|
||||||
*elts = new_elts;
|
*elts = new_elts;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void r300_align_ushort_elts(struct r300_context *r300,
|
static void r300_rebuild_ushort_elts(struct r300_context *r300,
|
||||||
struct pipe_resource **elts,
|
struct pipe_resource **elts,
|
||||||
unsigned start, unsigned count)
|
int index_bias,
|
||||||
|
unsigned start, unsigned count)
|
||||||
{
|
{
|
||||||
struct pipe_context* context = &r300->context;
|
struct pipe_context *context = &r300->context;
|
||||||
struct pipe_transfer *in_transfer = NULL;
|
struct pipe_transfer *in_transfer = NULL;
|
||||||
struct pipe_transfer *out_transfer = NULL;
|
struct pipe_transfer *out_transfer = NULL;
|
||||||
struct pipe_resource* new_elts;
|
struct pipe_resource *new_elts;
|
||||||
unsigned short *in_map;
|
unsigned short *in_map;
|
||||||
unsigned short *out_map;
|
unsigned short *out_map;
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
new_elts = pipe_buffer_create(context->screen,
|
new_elts = pipe_buffer_create(context->screen,
|
||||||
PIPE_BIND_INDEX_BUFFER,
|
PIPE_BIND_INDEX_BUFFER,
|
||||||
2 * count);
|
2 * count);
|
||||||
|
|
||||||
in_map = pipe_buffer_map(context, *elts,
|
in_map = pipe_buffer_map(context, *elts,
|
||||||
PIPE_TRANSFER_READ, &in_transfer);
|
PIPE_TRANSFER_READ, &in_transfer);
|
||||||
out_map = pipe_buffer_map(context, new_elts,
|
out_map = pipe_buffer_map(context, new_elts,
|
||||||
PIPE_TRANSFER_WRITE, &out_transfer);
|
PIPE_TRANSFER_WRITE, &out_transfer);
|
||||||
|
|
||||||
memcpy(out_map, in_map+start, 2 * count);
|
in_map += start;
|
||||||
|
for (i = 0; i < count; i++) {
|
||||||
|
*out_map = (unsigned short)(*in_map + index_bias);
|
||||||
|
in_map++;
|
||||||
|
out_map++;
|
||||||
|
}
|
||||||
|
|
||||||
|
pipe_buffer_unmap(context, *elts, in_transfer);
|
||||||
|
pipe_buffer_unmap(context, new_elts, out_transfer);
|
||||||
|
|
||||||
|
*elts = new_elts;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void r300_rebuild_uint_elts(struct r300_context *r300,
|
||||||
|
struct pipe_resource **elts,
|
||||||
|
int index_bias,
|
||||||
|
unsigned start, unsigned count)
|
||||||
|
{
|
||||||
|
struct pipe_context *context = &r300->context;
|
||||||
|
struct pipe_transfer *in_transfer = NULL;
|
||||||
|
struct pipe_transfer *out_transfer = NULL;
|
||||||
|
struct pipe_resource *new_elts;
|
||||||
|
unsigned int *in_map;
|
||||||
|
unsigned int *out_map;
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
new_elts = pipe_buffer_create(context->screen,
|
||||||
|
PIPE_BIND_INDEX_BUFFER,
|
||||||
|
2 * count);
|
||||||
|
|
||||||
|
in_map = pipe_buffer_map(context, *elts,
|
||||||
|
PIPE_TRANSFER_READ, &in_transfer);
|
||||||
|
out_map = pipe_buffer_map(context, new_elts,
|
||||||
|
PIPE_TRANSFER_WRITE, &out_transfer);
|
||||||
|
|
||||||
|
in_map += start;
|
||||||
|
for (i = 0; i < count; i++) {
|
||||||
|
*out_map = (unsigned int)(*in_map + index_bias);
|
||||||
|
in_map++;
|
||||||
|
out_map++;
|
||||||
|
}
|
||||||
|
|
||||||
pipe_buffer_unmap(context, *elts, in_transfer);
|
pipe_buffer_unmap(context, *elts, in_transfer);
|
||||||
pipe_buffer_unmap(context, new_elts, out_transfer);
|
pipe_buffer_unmap(context, new_elts, out_transfer);
|
||||||
|
@ -506,6 +604,7 @@ static void r300_draw_range_elements(struct pipe_context* pipe,
|
||||||
count > 65536 &&
|
count > 65536 &&
|
||||||
r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
|
r300->rws->get_value(r300->rws, R300_VID_DRM_2_3_0);
|
||||||
unsigned short_count;
|
unsigned short_count;
|
||||||
|
int buffer_offset = 0, index_offset = 0; /* for index bias emulation */
|
||||||
|
|
||||||
if (r300->skip_rendering) {
|
if (r300->skip_rendering) {
|
||||||
return;
|
return;
|
||||||
|
@ -515,13 +614,31 @@ static void r300_draw_range_elements(struct pipe_context* pipe,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indexSize == 1) {
|
if (indexBias && !index_bias_supported(r300)) {
|
||||||
r300_shorten_ubyte_elts(r300, &indexBuffer, start, count);
|
r300_split_index_bias(r300, indexBias, &buffer_offset, &index_offset);
|
||||||
indexSize = 2;
|
}
|
||||||
start = 0;
|
|
||||||
} else if (indexSize == 2 && start % 2 != 0) {
|
/* Rebuild the index buffer if needed. */
|
||||||
r300_align_ushort_elts(r300, &indexBuffer, start, count);
|
switch (indexSize) {
|
||||||
start = 0;
|
case 1:
|
||||||
|
r300_shorten_ubyte_elts(r300, &indexBuffer, index_offset, start, count);
|
||||||
|
indexSize = 2;
|
||||||
|
start = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
if (start % 2 != 0 || index_offset) {
|
||||||
|
r300_rebuild_ushort_elts(r300, &indexBuffer, index_offset, start, count);
|
||||||
|
start = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
if (index_offset) {
|
||||||
|
r300_rebuild_uint_elts(r300, &indexBuffer, index_offset, start, count);
|
||||||
|
start = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
r300_update_derived_state(r300);
|
r300_update_derived_state(r300);
|
||||||
|
@ -530,7 +647,7 @@ static void r300_draw_range_elements(struct pipe_context* pipe,
|
||||||
/* 15 dwords for emit_draw_elements */
|
/* 15 dwords for emit_draw_elements */
|
||||||
r300_prepare_for_rendering(r300,
|
r300_prepare_for_rendering(r300,
|
||||||
PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
|
PREP_FIRST_DRAW | PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
|
||||||
indexBuffer, 15, 0, indexBias, NULL);
|
indexBuffer, 15, buffer_offset, indexBias, NULL);
|
||||||
|
|
||||||
u_upload_flush(r300->upload_vb);
|
u_upload_flush(r300->upload_vb);
|
||||||
u_upload_flush(r300->upload_ib);
|
u_upload_flush(r300->upload_ib);
|
||||||
|
@ -551,7 +668,7 @@ static void r300_draw_range_elements(struct pipe_context* pipe,
|
||||||
if (count) {
|
if (count) {
|
||||||
r300_prepare_for_rendering(r300,
|
r300_prepare_for_rendering(r300,
|
||||||
PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
|
PREP_VALIDATE_VBOS | PREP_EMIT_AOS | PREP_INDEXED,
|
||||||
indexBuffer, 15, 0, indexBias, NULL);
|
indexBuffer, 15, buffer_offset, indexBias, NULL);
|
||||||
}
|
}
|
||||||
} while (count);
|
} while (count);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue