gallivm: Support multiple pixels in lp_build_fetch_rgba_aos().

This allows to do the unpacking of formats that fit in 4 x unorm8 in
parallel, 4 pixels at a time.
This commit is contained in:
José Fonseca
2010-07-02 18:36:43 +01:00
parent eb20c57f03
commit 7071eefdb2
6 changed files with 204 additions and 114 deletions

View File

@@ -495,5 +495,5 @@ draw_llvm_translate_from(LLVMBuilderRef builder,
format_desc = util_format_description(from_format); format_desc = util_format_description(from_format);
zero = LLVMConstNull(LLVMInt32Type()); zero = LLVMConstNull(LLVMInt32Type());
return lp_build_fetch_rgba_aos(builder, format_desc, type, vbuffer, zero, zero); return lp_build_fetch_rgba_aos(builder, format_desc, type, vbuffer, zero, zero, zero);
} }

View File

@@ -61,7 +61,8 @@ LLVMValueRef
lp_build_fetch_rgba_aos(LLVMBuilderRef builder, lp_build_fetch_rgba_aos(LLVMBuilderRef builder,
const struct util_format_description *format_desc, const struct util_format_description *format_desc,
struct lp_type type, struct lp_type type,
LLVMValueRef ptr, LLVMValueRef base_ptr,
LLVMValueRef offset,
LLVMValueRef i, LLVMValueRef i,
LLVMValueRef j); LLVMValueRef j);
@@ -105,11 +106,12 @@ lp_build_fetch_rgba_soa(LLVMBuilderRef builder,
LLVMValueRef LLVMValueRef
lp_build_unpack_subsampled_to_rgba_aos(LLVMBuilderRef builder, lp_build_fetch_subsampled_rgba_aos(LLVMBuilderRef builder,
const struct util_format_description *format_desc, const struct util_format_description *format_desc,
unsigned n, unsigned n,
LLVMValueRef packed, LLVMValueRef base_ptr,
LLVMValueRef i, LLVMValueRef offset,
LLVMValueRef j); LLVMValueRef i,
LLVMValueRef j);
#endif /* !LP_BLD_FORMAT_H */ #endif /* !LP_BLD_FORMAT_H */

View File

@@ -102,7 +102,9 @@ format_matches_type(const struct util_format_description *desc,
assert(type.length % 4 == 0); assert(type.length % 4 == 0);
if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN || if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB) { desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB ||
desc->block.width != 1 ||
desc->block.height != 1) {
return FALSE; return FALSE;
} }
@@ -137,18 +139,15 @@ format_matches_type(const struct util_format_description *desc,
* Unpack a single pixel into its RGBA components. * Unpack a single pixel into its RGBA components.
* *
* @param desc the pixel format for the packed pixel value * @param desc the pixel format for the packed pixel value
* @param type the desired return type (float[4] vs. ubyte[4])
* @param packed integer pixel in a format such as PIPE_FORMAT_B8G8R8A8_UNORM * @param packed integer pixel in a format such as PIPE_FORMAT_B8G8R8A8_UNORM
* *
* @return RGBA in a float[4] or ubyte[4] or ushort[4] vector. * @return RGBA in a float[4] or ubyte[4] or ushort[4] vector.
*/ */
static INLINE LLVMValueRef static INLINE LLVMValueRef
lp_build_unpack_rgba_aos(const struct util_format_description *desc, lp_build_unpack_arith_rgba_aos(LLVMBuilderRef builder,
struct lp_build_context *bld, const struct util_format_description *desc,
LLVMValueRef packed) LLVMValueRef packed)
{ {
LLVMBuilderRef builder = bld->builder;
struct lp_type type = bld->type;
LLVMValueRef shifted, casted, scaled, masked; LLVMValueRef shifted, casted, scaled, masked;
LLVMValueRef shifts[4]; LLVMValueRef shifts[4];
LLVMValueRef masks[4]; LLVMValueRef masks[4];
@@ -167,8 +166,7 @@ lp_build_unpack_rgba_aos(const struct util_format_description *desc,
/* Do the intermediate integer computations with 32bit integers since it /* Do the intermediate integer computations with 32bit integers since it
* matches floating point size */ * matches floating point size */
if (desc->block.bits < 32) assert (LLVMTypeOf(packed) == LLVMInt32Type());
packed = LLVMBuildZExt(builder, packed, LLVMInt32Type(), "");
/* Broadcast the packed value to all four channels /* Broadcast the packed value to all four channels
* before: packed = BGRA * before: packed = BGRA
@@ -246,20 +244,6 @@ lp_build_unpack_rgba_aos(const struct util_format_description *desc,
else else
scaled = casted; scaled = casted;
/*
* Type conversion.
*
* TODO: We could avoid floating conversion for integer to
* integer conversions.
*/
lp_build_conv(builder,
lp_float32_vec4_type(),
type,
&scaled, 1, &scaled, 1);
scaled = lp_build_format_swizzle_aos(desc, bld, scaled);
return scaled; return scaled;
} }
@@ -382,17 +366,51 @@ LLVMValueRef
lp_build_fetch_rgba_aos(LLVMBuilderRef builder, lp_build_fetch_rgba_aos(LLVMBuilderRef builder,
const struct util_format_description *format_desc, const struct util_format_description *format_desc,
struct lp_type type, struct lp_type type,
LLVMValueRef ptr, LLVMValueRef base_ptr,
LLVMValueRef offset,
LLVMValueRef i, LLVMValueRef i,
LLVMValueRef j) LLVMValueRef j)
{ {
unsigned num_pixels = type.length / 4;
struct lp_build_context bld; struct lp_build_context bld;
/* XXX: For now we only support one pixel at a time */ assert(type.length <= LP_MAX_VECTOR_LENGTH);
assert(type.length == 4); assert(type.length % 4 == 0);
lp_build_context_init(&bld, builder, type); lp_build_context_init(&bld, builder, type);
/*
* Trivial case
*
* The format matches the type (apart of a swizzle) so no need for
* scaling or converting.
*/
if (format_matches_type(format_desc, type) &&
format_desc->block.bits <= type.width * 4 &&
util_is_pot(format_desc->block.bits)) {
LLVMValueRef packed;
/*
* The format matches the type (apart of a swizzle) so no need for
* scaling or converting.
*/
packed = lp_build_gather(builder, type.length/4,
format_desc->block.bits, type.width*4,
base_ptr, offset);
assert(format_desc->block.bits <= type.width * type.length);
packed = LLVMBuildBitCast(builder, packed, lp_build_vec_type(type), "");
return lp_build_format_swizzle_aos(format_desc, &bld, packed);
}
/*
* Bit arithmetic
*/
if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN && if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB || (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) && format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) &&
@@ -403,56 +421,74 @@ lp_build_fetch_rgba_aos(LLVMBuilderRef builder,
format_desc->is_bitmask && format_desc->is_bitmask &&
!format_desc->is_mixed && !format_desc->is_mixed &&
(format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED || (format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED ||
format_desc->channel[1].type == UTIL_FORMAT_TYPE_UNSIGNED)) format_desc->channel[1].type == UTIL_FORMAT_TYPE_UNSIGNED)) {
{
LLVMValueRef packed;
ptr = LLVMBuildBitCast(builder, ptr, LLVMValueRef tmps[LP_MAX_VECTOR_LENGTH/4];
LLVMPointerType(LLVMIntType(format_desc->block.bits), 0) , LLVMValueRef res;
""); unsigned k;
packed = LLVMBuildLoad(builder, ptr, "packed"); /*
* Unpack a pixel at a time into a <4 x float> RGBA vector
*/
if (format_matches_type(format_desc, type)) { for (k = 0; k < num_pixels; ++k) {
/* LLVMValueRef packed;
* The format matches the type (apart of a swizzle) so no need for
* scaling or converting.
*/
assert(format_desc->block.bits <= type.width * type.length); packed = lp_build_gather_elem(builder, num_pixels,
if (format_desc->block.bits < type.width * type.length) { format_desc->block.bits, 32,
packed = LLVMBuildZExt(builder, packed, base_ptr, offset, k);
LLVMIntType(type.width * type.length), "");
}
packed = LLVMBuildBitCast(builder, packed, lp_build_vec_type(type), ""); tmps[k] = lp_build_unpack_arith_rgba_aos(builder, format_desc,
packed);
return lp_build_format_swizzle_aos(format_desc, &bld, packed);
} else {
return lp_build_unpack_rgba_aos(format_desc, &bld, packed);
} }
}
else if (format_desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
LLVMValueRef packed;
LLVMValueRef rgba;
ptr = LLVMBuildBitCast(builder, ptr, /*
LLVMPointerType(LLVMInt32Type(), 0), * Type conversion.
"packed_ptr"); *
* TODO: We could avoid floating conversion for integer to
packed = LLVMBuildLoad(builder, ptr, "packed"); * integer conversions.
*/
rgba = lp_build_unpack_subsampled_to_rgba_aos(builder, format_desc,
1, packed, i, j);
lp_build_conv(builder, lp_build_conv(builder,
lp_unorm8_vec4_type(), lp_float32_vec4_type(),
type, type,
&rgba, 1, &rgba, 1); tmps, num_pixels, &res, 1);
return rgba; return lp_build_format_swizzle_aos(format_desc, &bld, res);
} }
else if (format_desc->fetch_rgba_float) {
/*
* YUV / subsampled formats
*/
if (format_desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
struct lp_type tmp_type;
LLVMValueRef tmp;
memset(&tmp_type, 0, sizeof tmp_type);
tmp_type.width = 8;
tmp_type.length = num_pixels * 4;
tmp_type.norm = TRUE;
tmp = lp_build_fetch_subsampled_rgba_aos(builder,
format_desc,
num_pixels,
base_ptr,
offset,
i, j);
lp_build_conv(builder,
tmp_type, type,
&tmp, 1, &tmp, 1);
return tmp;
}
/*
* Fallback to util_format_description::fetch_rgba_float().
*/
if (format_desc->fetch_rgba_float) {
/* /*
* Fallback to calling util_format_description::fetch_rgba_float. * Fallback to calling util_format_description::fetch_rgba_float.
* *
@@ -469,8 +505,9 @@ lp_build_fetch_rgba_aos(LLVMBuilderRef builder,
LLVMTypeRef pf32t = LLVMPointerType(f32t, 0); LLVMTypeRef pf32t = LLVMPointerType(f32t, 0);
LLVMValueRef function; LLVMValueRef function;
LLVMValueRef tmp_ptr; LLVMValueRef tmp_ptr;
LLVMValueRef tmp_val; LLVMValueRef tmps[LP_MAX_VECTOR_LENGTH/4];
LLVMValueRef args[4]; LLVMValueRef res;
unsigned k;
util_snprintf(name, sizeof name, "util_format_%s_fetch_rgba_float", util_snprintf(name, sizeof name, "util_format_%s_fetch_rgba_float",
format_desc->short_name); format_desc->short_name);
@@ -508,28 +545,43 @@ lp_build_fetch_rgba_aos(LLVMBuilderRef builder,
* in the SoA vectors. * in the SoA vectors.
*/ */
args[0] = LLVMBuildBitCast(builder, tmp_ptr, pf32t, ""); for (k = 0; k < num_pixels; ++k) {
args[1] = ptr; LLVMValueRef args[4];
args[2] = i;
args[3] = j;
LLVMBuildCall(builder, function, args, Elements(args), ""); args[0] = LLVMBuildBitCast(builder, tmp_ptr, pf32t, "");
args[1] = lp_build_gather_elem_ptr(builder, num_pixels,
base_ptr, offset, k);
tmp_val = LLVMBuildLoad(builder, tmp_ptr, ""); if (num_pixels == 1) {
args[2] = i;
args[3] = j;
}
else {
LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), k, 0);
args[2] = LLVMBuildExtractElement(builder, i, index, "");
args[3] = LLVMBuildExtractElement(builder, j, index, "");
}
if (type.floating) { LLVMBuildCall(builder, function, args, Elements(args), "");
/* No further conversion necessary */
} else { tmps[k] = LLVMBuildLoad(builder, tmp_ptr, "");
lp_build_conv(builder,
lp_float32_vec4_type(),
type,
&tmp_val, 1, &tmp_val, 1);
} }
return tmp_val; /*
} * Type conversion.
else { *
assert(0); * TODO: We could avoid floating conversion for integer to
return lp_build_undef(type); * integer conversions.
*/
lp_build_conv(builder,
lp_float32_vec4_type(),
type,
tmps, num_pixels, &res, 1);
return res;
} }
assert(0);
return lp_build_undef(type);
} }

View File

@@ -346,18 +346,49 @@ lp_build_fetch_rgba_soa(LLVMBuilderRef builder,
format_desc, format_desc,
type, type,
packed, rgba_out); packed, rgba_out);
return;
} }
else {
/*
* Fallback to calling lp_build_fetch_rgba_aos for each pixel.
*
* This is not the most efficient way of fetching pixels, as we
* miss some opportunities to do vectorization, but this is
* convenient for formats or scenarios for which there was no
* opportunity or incentive to optimize.
*/
/*
* Try calling lp_build_fetch_rgba_aos for all pixels.
*/
if (util_format_fits_8unorm(format_desc) &&
type.floating && type.width == 32 && type.length == 4) {
struct lp_type tmp_type;
LLVMValueRef tmp;
memset(&tmp_type, 0, sizeof tmp_type);
tmp_type.width = 8;
tmp_type.length = type.length * 4;
tmp_type.norm = TRUE;
tmp = lp_build_fetch_rgba_aos(builder, format_desc, tmp_type,
base_ptr, offset, i, j);
lp_build_rgba8_to_f32_soa(builder,
type,
tmp,
rgba_out);
return;
}
/*
* Fallback to calling lp_build_fetch_rgba_aos for each pixel.
*
* This is not the most efficient way of fetching pixels, as we
* miss some opportunities to do vectorization, but this is
* convenient for formats or scenarios for which there was no
* opportunity or incentive to optimize.
*/
{
unsigned k, chan; unsigned k, chan;
struct lp_type tmp_type;
tmp_type = type;
tmp_type.length = 4;
for (chan = 0; chan < 4; ++chan) { for (chan = 0; chan < 4; ++chan) {
rgba_out[chan] = lp_build_undef(type); rgba_out[chan] = lp_build_undef(type);
@@ -367,18 +398,17 @@ lp_build_fetch_rgba_soa(LLVMBuilderRef builder,
for(k = 0; k < type.length; ++k) { for(k = 0; k < type.length; ++k) {
LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), k, 0); LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), k, 0);
LLVMValueRef offset_elem; LLVMValueRef offset_elem;
LLVMValueRef ptr;
LLVMValueRef i_elem, j_elem; LLVMValueRef i_elem, j_elem;
LLVMValueRef tmp; LLVMValueRef tmp;
offset_elem = LLVMBuildExtractElement(builder, offset, index, ""); offset_elem = LLVMBuildExtractElement(builder, offset, index, "");
ptr = LLVMBuildGEP(builder, base_ptr, &offset_elem, 1, "");
i_elem = LLVMBuildExtractElement(builder, i, index, ""); i_elem = LLVMBuildExtractElement(builder, i, index, "");
j_elem = LLVMBuildExtractElement(builder, j, index, ""); j_elem = LLVMBuildExtractElement(builder, j, index, "");
/* Get a single float[4]={R,G,B,A} pixel */ /* Get a single float[4]={R,G,B,A} pixel */
tmp = lp_build_fetch_rgba_aos(builder, format_desc, type, ptr, tmp = lp_build_fetch_rgba_aos(builder, format_desc, tmp_type,
base_ptr, offset_elem,
i_elem, j_elem); i_elem, j_elem);
/* /*

View File

@@ -35,9 +35,6 @@
#include "util/u_format.h" #include "util/u_format.h"
#include "util/u_memory.h"
#include "util/u_math.h"
#include "util/u_string.h"
#include "lp_bld_arit.h" #include "lp_bld_arit.h"
#include "lp_bld_init.h" #include "lp_bld_init.h"
@@ -359,16 +356,23 @@ grgb_to_rgba_aos(LLVMBuilderRef builder,
* @return a <4*n x i8> vector with the pixel RGBA values in AoS * @return a <4*n x i8> vector with the pixel RGBA values in AoS
*/ */
LLVMValueRef LLVMValueRef
lp_build_unpack_subsampled_to_rgba_aos(LLVMBuilderRef builder, lp_build_fetch_subsampled_rgba_aos(LLVMBuilderRef builder,
const struct util_format_description *format_desc, const struct util_format_description *format_desc,
unsigned n, unsigned n,
LLVMValueRef packed, LLVMValueRef base_ptr,
LLVMValueRef i, LLVMValueRef offset,
LLVMValueRef j) LLVMValueRef i,
LLVMValueRef j)
{ {
LLVMValueRef packed;
LLVMValueRef rgba; LLVMValueRef rgba;
assert(format_desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED); assert(format_desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED);
assert(format_desc->block.bits == 32);
assert(format_desc->block.width == 2);
assert(format_desc->block.height == 1);
packed = lp_build_gather(builder, n, 32, 32, base_ptr, offset);
(void)j; (void)j;

View File

@@ -86,6 +86,7 @@ add_fetch_rgba_test(unsigned verbose,
LLVMTypeRef args[4]; LLVMTypeRef args[4];
LLVMValueRef func; LLVMValueRef func;
LLVMValueRef packed_ptr; LLVMValueRef packed_ptr;
LLVMValueRef offset = LLVMConstNull(LLVMInt32Type());
LLVMValueRef rgba_ptr; LLVMValueRef rgba_ptr;
LLVMValueRef i; LLVMValueRef i;
LLVMValueRef j; LLVMValueRef j;
@@ -112,7 +113,8 @@ add_fetch_rgba_test(unsigned verbose,
builder = LLVMCreateBuilder(); builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(builder, block); LLVMPositionBuilderAtEnd(builder, block);
rgba = lp_build_fetch_rgba_aos(builder, desc, type, packed_ptr, i, j); rgba = lp_build_fetch_rgba_aos(builder, desc, type,
packed_ptr, offset, i, j);
LLVMBuildStore(builder, rgba, rgba_ptr); LLVMBuildStore(builder, rgba, rgba_ptr);