Metal:Speed up BGRA8 to RGBA8 copy for readPixels

On a 2048x2048 texture on Intel this goes from 26-27ms to 17-18ms

Bug: angleproject:7117
Change-Id: I4f48521b64e54669d180f0d2d8fdda78f83f89b5
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3579510
Reviewed-by: Jamie Madill <[email protected]>
Reviewed-by: Kenneth Russell <[email protected]>
Commit-Queue: Gregg Tavares <[email protected]>
This commit is contained in:
Gregg Tavares authored and Angle LUCI CQ committed 2022-04-13 06:10:09 +00:00
1 parent 8074061d24
commit 985d429340
4 files changed
+97 -25

No files matched your search

+57 -5
View File
@@ -11,12 +11,64 @@
namespace angle
{
void CopyBGRA8ToRGBA8(const uint8_t *source, uint8_t *dest)
namespace
{
uint32_t argb = *reinterpret_cast<const uint32_t *>(source);
*reinterpret_cast<uint32_t *>(dest) = (argb & 0xFF00FF00) | // Keep alpha and green
(argb & 0x00FF0000) >> 16 | // Move red to blue
(argb & 0x000000FF) << 16; // Move blue to red
inline uint32_t SwizzleBGRAToRGBA(uint32_t argb)
{
return ((argb & 0x000000FF) << 16) | // Move BGRA blue to RGBA blue
((argb & 0x00FF0000) >> 16) | // Move BGRA red to RGBA red
((argb & 0xFF00FF00)); // Keep alpha and green
}
void CopyBGRA8ToRGBA8Fast(const uint8_t *source,
int srcYAxisPitch,
uint8_t *dest,
int destYAxisPitch,
int destWidth,
int destHeight)
{
for (int y = 0; y < destHeight; ++y)
{
const uint32_t *src32 = reinterpret_cast<const uint32_t *>(source + y * srcYAxisPitch);
uint32_t *dest32 = reinterpret_cast<uint32_t *>(dest + y * destYAxisPitch);
const uint32_t *end32 = src32 + destWidth;
while (src32 != end32)
{
*dest32++ = SwizzleBGRAToRGBA(*src32++);
}
}
}
} // namespace
void CopyBGRA8ToRGBA8(const uint8_t *source,
int srcXAxisPitch,
int srcYAxisPitch,
uint8_t *dest,
int destXAxisPitch,
int destYAxisPitch,
int destWidth,
int destHeight)
{
if (srcXAxisPitch == 4 && destXAxisPitch == 4)
{
CopyBGRA8ToRGBA8Fast(source, srcYAxisPitch, dest, destYAxisPitch, destWidth, destHeight);
return;
}
for (int y = 0; y < destHeight; ++y)
{
uint8_t *dst = dest + y * destYAxisPitch;
const uint8_t *src = source + y * srcYAxisPitch;
const uint8_t *end = src + destWidth * srcXAxisPitch;
while (src != end)
{
*reinterpret_cast<uint32_t *>(dst) =
SwizzleBGRAToRGBA(*reinterpret_cast<const uint32_t *>(src));
src += srcXAxisPitch;
dst += destXAxisPitch;
}
}
}
} // namespace angle
+8 -1
View File
@@ -30,7 +30,14 @@ void ReadDepthStencil(const uint8_t *source, uint8_t *dest);
template <typename DestType>
void WriteDepthStencil(const uint8_t *source, uint8_t *dest);
void CopyBGRA8ToRGBA8(const uint8_t *source, uint8_t *dest);
void CopyBGRA8ToRGBA8(const uint8_t *source,
int srcXAxisPitch,
int srcYAxisPitch,
uint8_t *dest,
int destXAxisPitch,
int destYAxisPitch,
int destWidth,
int destHeight);
} // namespace angle
+22 -16
View File
@@ -382,22 +382,13 @@ void PackPixels(const PackPixelsParams &params,
return;
}
PixelCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id);
FastCopyFunction fastCopyFunc = sourceFormat.fastCopyFunctions.get(params.destFormat->id);
if (fastCopyFunc)
{
// Fast copy is possible through some special function
for (int y = 0; y < destHeight; ++y)
{
for (int x = 0; x < destWidth; ++x)
{
uint8_t *dest =
destWithOffset + y * params.outputPitch + x * params.destFormat->pixelBytes;
const uint8_t *src = source + y * yAxisPitch + x * xAxisPitch;
fastCopyFunc(src, dest);
}
}
fastCopyFunc(source, xAxisPitch, yAxisPitch, destWithOffset, params.destFormat->pixelBytes,
params.outputPitch, destWidth, destHeight);
return;
}
@@ -435,19 +426,34 @@ bool FastCopyFunctionMap::has(angle::FormatID formatID) const
return (get(formatID) != nullptr);
}
PixelCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const
namespace
{
for (size_t index = 0; index < mSize; ++index)
const FastCopyFunctionMap::Entry *getEntry(const FastCopyFunctionMap::Entry *entry,
size_t numEntries,
angle::FormatID formatID)
{
const FastCopyFunctionMap::Entry *end = entry + numEntries;
while (entry != end)
{
if (mData[index].formatID == formatID)
if (entry->formatID == formatID)
{
return mData[index].func;
return entry;
}
++entry;
}
return nullptr;
}
} // namespace
FastCopyFunction FastCopyFunctionMap::get(angle::FormatID formatID) const
{
const FastCopyFunctionMap::Entry *entry = getEntry(mData, mSize, formatID);
return entry ? entry->func : nullptr;
}
bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
{
EGLAttrib debugSetting =
+10 -3
View File
@@ -82,7 +82,14 @@ using MipGenerationFunction = void (*)(size_t sourceWidth,
typedef void (*PixelReadFunction)(const uint8_t *source, uint8_t *dest);
typedef void (*PixelWriteFunction)(const uint8_t *source, uint8_t *dest);
typedef void (*PixelCopyFunction)(const uint8_t *source, uint8_t *dest);
typedef void (*FastCopyFunction)(const uint8_t *source,
int srcXAxisPitch,
int srcYAxisPitch,
uint8_t *dest,
int destXAxisPitch,
int destYAxisPitch,
int width,
int height);
class FastCopyFunctionMap
{
@@ -90,7 +97,7 @@ class FastCopyFunctionMap
struct Entry
{
angle::FormatID formatID;
PixelCopyFunction func;
FastCopyFunction func;
};
constexpr FastCopyFunctionMap() : FastCopyFunctionMap(nullptr, 0) {}
@@ -98,7 +105,7 @@ class FastCopyFunctionMap
constexpr FastCopyFunctionMap(const Entry *data, size_t size) : mSize(size), mData(data) {}
bool has(angle::FormatID formatID) const;
PixelCopyFunction get(angle::FormatID formatID) const;
FastCopyFunction get(angle::FormatID formatID) const;
private:
size_t mSize;