Merge pull request #6861 from yzct12345/const-mempy-is-all-the-speed

decoders: Optimize memcpy for the other functions
This commit is contained in:
bunnei 2021-08-15 02:38:12 -07:00 committed by GitHub
commit 87d63b858a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 147 additions and 88 deletions

View File

@ -84,34 +84,107 @@ template <bool TO_LINEAR>
void Swizzle(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width, void Swizzle(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width,
u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) { u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) {
switch (bytes_per_pixel) { switch (bytes_per_pixel) {
case 1: #define BPP_CASE(x) \
return SwizzleImpl<TO_LINEAR, 1>(output, input, width, height, depth, block_height, case x: \
block_depth, stride_alignment); return SwizzleImpl<TO_LINEAR, x>(output, input, width, height, depth, block_height, \
case 2:
return SwizzleImpl<TO_LINEAR, 2>(output, input, width, height, depth, block_height,
block_depth, stride_alignment);
case 3:
return SwizzleImpl<TO_LINEAR, 3>(output, input, width, height, depth, block_height,
block_depth, stride_alignment);
case 4:
return SwizzleImpl<TO_LINEAR, 4>(output, input, width, height, depth, block_height,
block_depth, stride_alignment);
case 6:
return SwizzleImpl<TO_LINEAR, 6>(output, input, width, height, depth, block_height,
block_depth, stride_alignment);
case 8:
return SwizzleImpl<TO_LINEAR, 8>(output, input, width, height, depth, block_height,
block_depth, stride_alignment);
case 12:
return SwizzleImpl<TO_LINEAR, 12>(output, input, width, height, depth, block_height,
block_depth, stride_alignment);
case 16:
return SwizzleImpl<TO_LINEAR, 16>(output, input, width, height, depth, block_height,
block_depth, stride_alignment); block_depth, stride_alignment);
BPP_CASE(1)
BPP_CASE(2)
BPP_CASE(3)
BPP_CASE(4)
BPP_CASE(6)
BPP_CASE(8)
BPP_CASE(12)
BPP_CASE(16)
#undef BPP_CASE
default: default:
UNREACHABLE_MSG("Invalid bytes_per_pixel={}", bytes_per_pixel); UNREACHABLE_MSG("Invalid bytes_per_pixel={}", bytes_per_pixel);
} }
} }
template <u32 BYTES_PER_PIXEL>
void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width,
u8* swizzled_data, const u8* unswizzled_data, u32 block_height_bit,
u32 offset_x, u32 offset_y) {
const u32 block_height = 1U << block_height_bit;
const u32 image_width_in_gobs =
(swizzled_width * BYTES_PER_PIXEL + (GOB_SIZE_X - 1)) / GOB_SIZE_X;
for (u32 line = 0; line < subrect_height; ++line) {
const u32 dst_y = line + offset_y;
const u32 gob_address_y =
(dst_y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs +
((dst_y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE;
const auto& table = SWIZZLE_TABLE[dst_y % GOB_SIZE_Y];
for (u32 x = 0; x < subrect_width; ++x) {
const u32 dst_x = x + offset_x;
const u32 gob_address =
gob_address_y + (dst_x * BYTES_PER_PIXEL / GOB_SIZE_X) * GOB_SIZE * block_height;
const u32 swizzled_offset = gob_address + table[(dst_x * BYTES_PER_PIXEL) % GOB_SIZE_X];
const u32 unswizzled_offset = line * source_pitch + x * BYTES_PER_PIXEL;
const u8* const source_line = unswizzled_data + unswizzled_offset;
u8* const dest_addr = swizzled_data + swizzled_offset;
std::memcpy(dest_addr, source_line, BYTES_PER_PIXEL);
}
}
}
template <u32 BYTES_PER_PIXEL>
void UnswizzleSubrect(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 block_height,
u32 origin_x, u32 origin_y, u8* output, const u8* input) {
const u32 stride = width * BYTES_PER_PIXEL;
const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height);
const u32 block_height_mask = (1U << block_height) - 1;
const u32 x_shift = GOB_SIZE_SHIFT + block_height;
for (u32 line = 0; line < line_count; ++line) {
const u32 src_y = line + origin_y;
const auto& table = SWIZZLE_TABLE[src_y % GOB_SIZE_Y];
const u32 block_y = src_y >> GOB_SIZE_Y_SHIFT;
const u32 src_offset_y = (block_y >> block_height) * block_size +
((block_y & block_height_mask) << GOB_SIZE_SHIFT);
for (u32 column = 0; column < line_length_in; ++column) {
const u32 src_x = (column + origin_x) * BYTES_PER_PIXEL;
const u32 src_offset_x = (src_x >> GOB_SIZE_X_SHIFT) << x_shift;
const u32 swizzled_offset = src_offset_y + src_offset_x + table[src_x % GOB_SIZE_X];
const u32 unswizzled_offset = line * pitch + column * BYTES_PER_PIXEL;
std::memcpy(output + unswizzled_offset, input + swizzled_offset, BYTES_PER_PIXEL);
}
}
}
template <u32 BYTES_PER_PIXEL>
void SwizzleSliceToVoxel(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 height,
u32 block_height, u32 block_depth, u32 origin_x, u32 origin_y, u8* output,
const u8* input) {
UNIMPLEMENTED_IF(origin_x > 0);
UNIMPLEMENTED_IF(origin_y > 0);
const u32 stride = width * BYTES_PER_PIXEL;
const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth);
const u32 block_height_mask = (1U << block_height) - 1;
const u32 x_shift = static_cast<u32>(GOB_SIZE_SHIFT) + block_height + block_depth;
for (u32 line = 0; line < line_count; ++line) {
const auto& table = SWIZZLE_TABLE[line % GOB_SIZE_Y];
const u32 block_y = line / GOB_SIZE_Y;
const u32 dst_offset_y =
(block_y >> block_height) * block_size + (block_y & block_height_mask) * GOB_SIZE;
for (u32 x = 0; x < line_length_in; ++x) {
const u32 dst_offset =
((x / GOB_SIZE_X) << x_shift) + dst_offset_y + table[x % GOB_SIZE_X];
const u32 src_offset = x * BYTES_PER_PIXEL + line * pitch;
std::memcpy(output + dst_offset, input + src_offset, BYTES_PER_PIXEL);
}
}
}
} // Anonymous namespace } // Anonymous namespace
void UnswizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, void UnswizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel,
@ -131,81 +204,67 @@ void SwizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_p
void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width, void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width,
u32 bytes_per_pixel, u8* swizzled_data, const u8* unswizzled_data, u32 bytes_per_pixel, u8* swizzled_data, const u8* unswizzled_data,
u32 block_height_bit, u32 offset_x, u32 offset_y) { u32 block_height_bit, u32 offset_x, u32 offset_y) {
const u32 block_height = 1U << block_height_bit; switch (bytes_per_pixel) {
const u32 image_width_in_gobs = #define BPP_CASE(x) \
(swizzled_width * bytes_per_pixel + (GOB_SIZE_X - 1)) / GOB_SIZE_X; case x: \
for (u32 line = 0; line < subrect_height; ++line) { return SwizzleSubrect<x>(subrect_width, subrect_height, source_pitch, swizzled_width, \
const u32 dst_y = line + offset_y; swizzled_data, unswizzled_data, block_height_bit, offset_x, \
const u32 gob_address_y = offset_y);
(dst_y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs + BPP_CASE(1)
((dst_y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE; BPP_CASE(2)
const auto& table = SWIZZLE_TABLE[dst_y % GOB_SIZE_Y]; BPP_CASE(3)
for (u32 x = 0; x < subrect_width; ++x) { BPP_CASE(4)
const u32 dst_x = x + offset_x; BPP_CASE(6)
const u32 gob_address = BPP_CASE(8)
gob_address_y + (dst_x * bytes_per_pixel / GOB_SIZE_X) * GOB_SIZE * block_height; BPP_CASE(12)
const u32 swizzled_offset = gob_address + table[(dst_x * bytes_per_pixel) % GOB_SIZE_X]; BPP_CASE(16)
const u32 unswizzled_offset = line * source_pitch + x * bytes_per_pixel; #undef BPP_CASE
default:
const u8* const source_line = unswizzled_data + unswizzled_offset; UNREACHABLE_MSG("Invalid bytes_per_pixel={}", bytes_per_pixel);
u8* const dest_addr = swizzled_data + swizzled_offset;
std::memcpy(dest_addr, source_line, bytes_per_pixel);
}
} }
} }
void UnswizzleSubrect(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 bytes_per_pixel, void UnswizzleSubrect(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 bytes_per_pixel,
u32 block_height, u32 origin_x, u32 origin_y, u8* output, const u8* input) { u32 block_height, u32 origin_x, u32 origin_y, u8* output, const u8* input) {
const u32 stride = width * bytes_per_pixel; switch (bytes_per_pixel) {
const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X; #define BPP_CASE(x) \
const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height); case x: \
return UnswizzleSubrect<x>(line_length_in, line_count, pitch, width, block_height, \
const u32 block_height_mask = (1U << block_height) - 1; origin_x, origin_y, output, input);
const u32 x_shift = GOB_SIZE_SHIFT + block_height; BPP_CASE(1)
BPP_CASE(2)
for (u32 line = 0; line < line_count; ++line) { BPP_CASE(3)
const u32 src_y = line + origin_y; BPP_CASE(4)
const auto& table = SWIZZLE_TABLE[src_y % GOB_SIZE_Y]; BPP_CASE(6)
BPP_CASE(8)
const u32 block_y = src_y >> GOB_SIZE_Y_SHIFT; BPP_CASE(12)
const u32 src_offset_y = (block_y >> block_height) * block_size + BPP_CASE(16)
((block_y & block_height_mask) << GOB_SIZE_SHIFT); #undef BPP_CASE
for (u32 column = 0; column < line_length_in; ++column) { default:
const u32 src_x = (column + origin_x) * bytes_per_pixel; UNREACHABLE_MSG("Invalid bytes_per_pixel={}", bytes_per_pixel);
const u32 src_offset_x = (src_x >> GOB_SIZE_X_SHIFT) << x_shift;
const u32 swizzled_offset = src_offset_y + src_offset_x + table[src_x % GOB_SIZE_X];
const u32 unswizzled_offset = line * pitch + column * bytes_per_pixel;
std::memcpy(output + unswizzled_offset, input + swizzled_offset, bytes_per_pixel);
}
} }
} }
void SwizzleSliceToVoxel(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 height, void SwizzleSliceToVoxel(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 height,
u32 bytes_per_pixel, u32 block_height, u32 block_depth, u32 origin_x, u32 bytes_per_pixel, u32 block_height, u32 block_depth, u32 origin_x,
u32 origin_y, u8* output, const u8* input) { u32 origin_y, u8* output, const u8* input) {
UNIMPLEMENTED_IF(origin_x > 0); switch (bytes_per_pixel) {
UNIMPLEMENTED_IF(origin_y > 0); #define BPP_CASE(x) \
case x: \
const u32 stride = width * bytes_per_pixel; return SwizzleSliceToVoxel<x>(line_length_in, line_count, pitch, width, height, \
const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X; block_height, block_depth, origin_x, origin_y, output, \
const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth); input);
BPP_CASE(1)
const u32 block_height_mask = (1U << block_height) - 1; BPP_CASE(2)
const u32 x_shift = static_cast<u32>(GOB_SIZE_SHIFT) + block_height + block_depth; BPP_CASE(3)
BPP_CASE(4)
for (u32 line = 0; line < line_count; ++line) { BPP_CASE(6)
const auto& table = SWIZZLE_TABLE[line % GOB_SIZE_Y]; BPP_CASE(8)
const u32 block_y = line / GOB_SIZE_Y; BPP_CASE(12)
const u32 dst_offset_y = BPP_CASE(16)
(block_y >> block_height) * block_size + (block_y & block_height_mask) * GOB_SIZE; #undef BPP_CASE
for (u32 x = 0; x < line_length_in; ++x) { default:
const u32 dst_offset = UNREACHABLE_MSG("Invalid bytes_per_pixel={}", bytes_per_pixel);
((x / GOB_SIZE_X) << x_shift) + dst_offset_y + table[x % GOB_SIZE_X];
const u32 src_offset = x * bytes_per_pixel + line * pitch;
std::memcpy(output + dst_offset, input + src_offset, bytes_per_pixel);
}
} }
} }
@ -228,7 +287,7 @@ void SwizzleKepler(const u32 width, const u32 height, const u32 dst_x, const u32
u8* dest_addr = swizzle_data + swizzled_offset; u8* dest_addr = swizzle_data + swizzled_offset;
count++; count++;
std::memcpy(dest_addr, source_line, 1); *dest_addr = *source_line;
} }
} }
} }