Hash :
716c5d00
Author :
Date :
2023-11-13T10:23:10
Vulkan: Add RGB8-to-RGBA8 ubyte loading function
Currently, to update RGB8 on desktop, ANGLE uses memcpy for each
pixel, which is suboptimal. This CL adds a loading function to improve
the copy time for RGB textures where RGBA is needed on the hardware.
* Added a specialization to LoadToNative3To4() for unsigned bytes
using 0xFF as the fourth component.
* It is optimized for unsigned bytes when converting an RGB format
to its corresponding RGBA format, e.g., RGB8 to RGBA8.
* It uses uint32_t operations to speed up the process.
* Added unit tests for the specialized LoadToNative3To4.
* LoadToNative_unittest.cpp
* Added perf test for RGB8 image allocation and loading.
* RGBImageAllocationBenchmark in RGBImageAllocation.cpp.
* RGBImageAllocationBenchmark shows some improvement in cpu_time
and wall_time on a Linux and a Windows device. (Results below using
aligned source pointer and texture size of 2048):
* On Windows: +~85% cpu_time, +~71% wall_time
* On Linux: +~26% cpu_time, +~27% wall_time
Bug: b/308177124
Change-Id: I421d83f75fdc513b0111dffb0a5d5e74682dd6fb
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4995489
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Charlie Lao <cclao@google.com>
Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// LoadToNative_unittest.cpp: Unit tests for pixel loading functions.
#include <gmock/gmock.h>
#include <vector>
#include "common/debug.h"
#include "common/mathutil.h"
#include "image_util/loadimage.h"
using namespace angle;
using namespace testing;
namespace
{
template <uint8_t fourthValue>
void TestLoadUbyteRGBToRGBA(ImageLoadContext &context,
size_t inputCase,
size_t width,
size_t height,
size_t depth,
size_t inputByteOffset,
size_t outputByteOffset,
size_t inputRowAlignment)
{
constexpr uint8_t kInitialByteValue = 0xAA;
size_t inputPixelBytes = 3;
size_t inputRowPitch = rx::roundUpPow2(width * inputPixelBytes, inputRowAlignment);
size_t inputDepthPitch = height * inputRowPitch;
size_t inputActualBytes = depth * inputDepthPitch;
size_t outputPixelBytes = 4;
size_t outputRowPitch = width * outputPixelBytes;
size_t outputDepthPitch = height * outputRowPitch;
size_t outputActualBytes = depth * outputDepthPitch;
// Prepare the RGB input and RGBA output for copy. The offset values are used to add unused
// bytes to the beginning of the input and output data, in order to test address alignments.
std::vector<uint8_t> rgbInput(inputByteOffset + inputActualBytes, kInitialByteValue);
for (size_t z = 0; z < depth; z++)
{
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
size_t inputIndex =
inputByteOffset + z * inputDepthPitch + y * inputRowPitch + x * inputPixelBytes;
rgbInput[inputIndex] = x % 256;
rgbInput[inputIndex + 1] = y % 256;
rgbInput[inputIndex + 2] = z % 256;
}
}
}
std::vector<uint8_t> rgbaOutput(outputByteOffset + outputActualBytes, kInitialByteValue);
// Call loading function.
LoadToNative3To4<uint8_t, fourthValue>(
context, width, height, depth, rgbInput.data() + inputByteOffset, inputRowPitch,
inputDepthPitch, rgbaOutput.data() + outputByteOffset, outputRowPitch, outputDepthPitch);
// Compare the input and output data.
for (size_t z = 0; z < depth; z++)
{
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
size_t inputIndex =
inputByteOffset + z * inputDepthPitch + y * inputRowPitch + x * inputPixelBytes;
size_t outputIndex = outputByteOffset + z * outputDepthPitch + y * outputRowPitch +
x * outputPixelBytes;
bool rMatch = rgbInput[inputIndex] == rgbaOutput[outputIndex];
bool gMatch = rgbInput[inputIndex + 1] == rgbaOutput[outputIndex + 1];
bool bMatch = rgbInput[inputIndex + 2] == rgbaOutput[outputIndex + 2];
bool aMatch = rgbaOutput[outputIndex + 3] == fourthValue;
EXPECT_TRUE(rMatch && gMatch && bMatch && aMatch)
<< "Case " << inputCase << ": Mismatch at Index (" << x << ", " << y << ", "
<< z << ")" << std::endl
<< "Expected output: (" << static_cast<uint32_t>(rgbInput[inputIndex]) << ", "
<< static_cast<uint32_t>(rgbInput[inputIndex + 1]) << ", "
<< static_cast<uint32_t>(rgbInput[inputIndex + 2]) << ", "
<< static_cast<uint32_t>(fourthValue) << ")" << std::endl
<< "Actual output: (" << static_cast<uint32_t>(rgbaOutput[outputIndex]) << ", "
<< static_cast<uint32_t>(rgbaOutput[outputIndex + 1]) << ", "
<< static_cast<uint32_t>(rgbaOutput[outputIndex + 2]) << ", "
<< static_cast<uint32_t>(rgbaOutput[outputIndex + 3]) << ")";
}
}
}
}
// Tests the ubyte RGB to RGBA loading function for one RGB pixel.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBADataOnePixel)
{
ImageLoadContext context;
uint8_t rgbInput[] = {1, 2, 3};
uint8_t rgbaOutput[] = {0, 0, 0, 0};
constexpr uint8_t kFourthValue = 0xFF;
LoadToNative3To4<uint8_t, kFourthValue>(context, 1, 1, 1, rgbInput, 3, 3, rgbaOutput, 4, 4);
EXPECT_TRUE(rgbaOutput[0] == rgbInput[0] && rgbaOutput[1] == rgbInput[1] &&
rgbaOutput[2] == rgbInput[2] && rgbaOutput[3] == kFourthValue)
<< "Pixel mismatch";
}
// Tests the ubyte RGB to RGBA loading function for 4 RGB pixels, which should be read together.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBADataFourPixels)
{
ImageLoadContext context;
constexpr uint8_t kPixelCount = 4;
std::vector<uint8_t> rgbInput(kPixelCount * 3);
std::vector<uint8_t> rgbaOutput(kPixelCount * 4, 0);
size_t index = 0;
for (auto &inputComponent : rgbInput)
{
inputComponent = ++index;
}
constexpr uint8_t kFourthValue = 0xFF;
LoadToNative3To4<uint8_t, kFourthValue>(context, 4, 1, 1, rgbInput.data(), 3, 3,
rgbaOutput.data(), 4, 4);
for (index = 0; index < kPixelCount; index++)
{
EXPECT_TRUE(rgbaOutput[index * 4] == rgbInput[index * 3] &&
rgbaOutput[index * 4 + 1] == rgbInput[index * 3 + 1] &&
rgbaOutput[index * 4 + 2] == rgbInput[index * 3 + 2] &&
rgbaOutput[index * 4 + 3] == kFourthValue)
<< "Mismatch at pixel " << index;
}
}
// Tests the ubyte RGB to RGBA loading function when the width is 4-byte aligned. This loading
// function can copy 4 bytes at a time in a row.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBADataAlignedWidth)
{
ImageLoadContext context;
size_t alignedTestWidths[] = {4, 20, 128, 1000, 4096};
for (auto &width : alignedTestWidths)
{
ASSERT(width % 4 == 0);
TestLoadUbyteRGBToRGBA<0xFF>(context, width, width, 3, 1, 0, 0, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when the width is not 4-byte aligned, which will
// cause the loading function to copy some bytes in the beginning and end of some rows individually.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBADataUnalignedWidth)
{
ImageLoadContext context;
size_t unalignedTestWidths[] = {5, 22, 127, 1022, 4097};
for (auto &width : unalignedTestWidths)
{
ASSERT(width % 4 != 0);
TestLoadUbyteRGBToRGBA<0xFF>(context, width, width, 3, 1, 0, 0, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when there is depth.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBADataWithDepth)
{
ImageLoadContext context;
size_t unalignedTestDepths[] = {3};
for (auto &depth : unalignedTestDepths)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, depth, 3, 3, depth, 0, 0, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when the width is less than 4 bytes. Therefore the
// loading function will copy data one byte at a time.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBADataWidthLessThanUint32)
{
ImageLoadContext context;
size_t smallTestWidths[] = {1, 2, 3};
for (auto &width : smallTestWidths)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, width, width, 3, 1, 0, 0, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when when the width is 4-byte-aligned and the input
// address has an offset.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBAWithAlignedWidthAndInputAddressOffset)
{
ImageLoadContext context;
size_t inputOffsetList[] = {1, 2, 3};
for (auto &inputOffset : inputOffsetList)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, inputOffset, 8, 8, 1, inputOffset, 0, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when when the width is not 4-byte-aligned and the
// input address has an offset.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBAWithUnalignedWidthAndInputAddressOffset)
{
ImageLoadContext context;
size_t inputOffsetList[] = {1, 2, 3};
for (auto &inputOffset : inputOffsetList)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, inputOffset, 7, 7, 1, inputOffset, 0, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when the width is 4-byte-aligned and the output
// address has an offset.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBAWithAlignedWidthAndOutputAddressOffset)
{
ImageLoadContext context;
size_t outputOffsetList[] = {1, 2, 3};
for (auto &outputOffset : outputOffsetList)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, outputOffset, 8, 8, 1, 0, outputOffset, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when the width is not 4-byte-aligned and the output
// address has an offset.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBAWithUnalignedWidthAndOutputAddressOffset)
{
ImageLoadContext context;
size_t outputOffsetList[] = {1, 2, 3};
for (auto &outputOffset : outputOffsetList)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, outputOffset, 7, 7, 1, 0, outputOffset, 1);
}
}
// Tests the ubyte RGB to RGBA loading function when the width is 4-byte-aligned and the input row
// alignment is 4.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBAWithAlignedWidthAndAlignment4)
{
ImageLoadContext context;
size_t inputRowAlignmentList[] = {4};
for (auto &alignment : inputRowAlignmentList)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, alignment, 4, 4, 1, 0, 0, alignment);
}
}
// Tests the ubyte RGB to RGBA loading function when the width is not 4-byte-aligned and the input
// row alignment is 4.
TEST(LoadToNative3To4, LoadUbyteRGBToRGBAWithUnalignedWidthAndAlignment4)
{
ImageLoadContext context;
size_t inputRowAlignmentList[] = {4};
for (auto &alignment : inputRowAlignmentList)
{
TestLoadUbyteRGBToRGBA<0xFF>(context, alignment, 5, 5, 1, 0, 0, alignment);
}
}
} // namespace