Commit d93cd6c2 by Geoff Lang Committed by Commit Bot

Refactor the CPU copy texture code into a function in renderer utils.

BUG=angleproject:1932 Change-Id: Iab79f2a09c2d8a85d2a9dde34acf4d2151072c2b Reviewed-on: https://chromium-review.googlesource.com/612561Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org>
parent d7487b13
...@@ -23,84 +23,6 @@ ...@@ -23,84 +23,6 @@
namespace rx namespace rx
{ {
namespace
{
void CopyColor(gl::ColorF *color)
{
// No-op
}
void PremultiplyAlpha(gl::ColorF *color)
{
color->red *= color->alpha;
color->green *= color->alpha;
color->blue *= color->alpha;
}
void UnmultiplyAlpha(gl::ColorF *color)
{
if (color->alpha != 0.0f)
{
float invAlpha = 1.0f / color->alpha;
color->red *= invAlpha;
color->green *= invAlpha;
color->blue *= invAlpha;
}
}
void ClipChannelsR(gl::ColorF *color)
{
color->green = 0.0f;
color->blue = 0.0f;
color->alpha = 1.0f;
}
void ClipChannelsRG(gl::ColorF *color)
{
color->blue = 0.0f;
color->alpha = 1.0f;
}
void ClipChannelsRGB(gl::ColorF *color)
{
color->alpha = 1.0f;
}
void ClipChannelsLuminance(gl::ColorF *color)
{
color->alpha = 1.0f;
}
void ClipChannelsAlpha(gl::ColorF *color)
{
color->red = 0.0f;
color->green = 0.0f;
color->blue = 0.0f;
}
void ClipChannelsNoOp(gl::ColorF *color)
{
}
void WriteUintColor(const gl::ColorF &color,
ColorWriteFunction colorWriteFunction,
uint8_t *destPixelData)
{
gl::ColorUI destColor(
static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.alpha * 255));
colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
}
void WriteFloatColor(const gl::ColorF &color,
ColorWriteFunction colorWriteFunction,
uint8_t *destPixelData)
{
colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
}
} // anonymous namespace
Image11::Image11(Renderer11 *renderer) Image11::Image11(Renderer11 *renderer)
: mRenderer(renderer), : mRenderer(renderer),
mDXGIFormat(DXGI_FORMAT_UNKNOWN), mDXGIFormat(DXGI_FORMAT_UNKNOWN),
...@@ -190,79 +112,16 @@ gl::Error Image11::CopyImage(const gl::Context *context, ...@@ -190,79 +112,16 @@ gl::Error Image11::CopyImage(const gl::Context *context,
gl::GetSizedInternalFormatInfo(destFormat.fboImplementationInternalFormat); gl::GetSizedInternalFormatInfo(destFormat.fboImplementationInternalFormat);
GLuint destPixelBytes = destFormatInfo.pixelBytes; GLuint destPixelBytes = destFormatInfo.pixelBytes;
const uint8_t *sourceData = reinterpret_cast<const uint8_t *>(srcMapped.pData); const uint8_t *sourceData = reinterpret_cast<const uint8_t *>(srcMapped.pData) +
uint8_t *destData = reinterpret_cast<uint8_t *>(destMapped.pData); sourceRect.x * sourcePixelBytes + sourceRect.y * srcMapped.RowPitch;
uint8_t *destData = reinterpret_cast<uint8_t *>(destMapped.pData) +
destOffset.x * destPixelBytes + destOffset.y * destMapped.RowPitch;
using ConversionFunction = void (*)(gl::ColorF *); CopyImageCHROMIUM(sourceData, srcMapped.RowPitch, sourcePixelBytes,
ConversionFunction conversionFunction = CopyColor; sourceFormat.colorReadFunction, destData, destMapped.RowPitch, destPixelBytes,
if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha) destFormat.colorWriteFunction, destUnsizedFormat,
{ destFormatInfo.componentType, sourceRect.width, sourceRect.height,
if (unpackPremultiplyAlpha) unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha);
{
conversionFunction = PremultiplyAlpha;
}
else
{
conversionFunction = UnmultiplyAlpha;
}
}
auto clipChannelsFunction = ClipChannelsNoOp;
switch (destUnsizedFormat)
{
case GL_RED:
clipChannelsFunction = ClipChannelsR;
break;
case GL_RG:
clipChannelsFunction = ClipChannelsRG;
break;
case GL_RGB:
clipChannelsFunction = ClipChannelsRGB;
break;
case GL_LUMINANCE:
clipChannelsFunction = ClipChannelsLuminance;
break;
case GL_ALPHA:
clipChannelsFunction = ClipChannelsAlpha;
break;
}
auto writeFunction =
(destFormatInfo.componentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
for (int y = 0; y < sourceRect.height; y++)
{
for (int x = 0; x < sourceRect.width; x++)
{
int sourceX = sourceRect.x + x;
int sourceY = sourceRect.y + y;
const uint8_t *sourcePixelData =
sourceData + sourceY * srcMapped.RowPitch + sourceX * sourcePixelBytes;
gl::ColorF sourceColor;
sourceFormat.colorReadFunction(sourcePixelData,
reinterpret_cast<uint8_t *>(&sourceColor));
conversionFunction(&sourceColor);
clipChannelsFunction(&sourceColor);
int destX = destOffset.x + x;
int destY = destOffset.y;
if (unpackFlipY)
{
destY += (sourceRect.height - 1);
destY -= y;
}
else
{
destY += y;
}
uint8_t *destPixelData =
destData + destY * destMapped.RowPitch + destX * destPixelBytes;
writeFunction(sourceColor, destFormat.colorWriteFunction, destPixelData);
}
}
dest->unmap(); dest->unmap();
source->unmap(); source->unmap();
......
...@@ -148,6 +148,81 @@ static FormatWriteFunctionMap BuildFormatWriteFunctionMap() ...@@ -148,6 +148,81 @@ static FormatWriteFunctionMap BuildFormatWriteFunctionMap()
return map; return map;
} }
void CopyColor(gl::ColorF *color)
{
// No-op
}
void PremultiplyAlpha(gl::ColorF *color)
{
color->red *= color->alpha;
color->green *= color->alpha;
color->blue *= color->alpha;
}
void UnmultiplyAlpha(gl::ColorF *color)
{
if (color->alpha != 0.0f)
{
float invAlpha = 1.0f / color->alpha;
color->red *= invAlpha;
color->green *= invAlpha;
color->blue *= invAlpha;
}
}
void ClipChannelsR(gl::ColorF *color)
{
color->green = 0.0f;
color->blue = 0.0f;
color->alpha = 1.0f;
}
void ClipChannelsRG(gl::ColorF *color)
{
color->blue = 0.0f;
color->alpha = 1.0f;
}
void ClipChannelsRGB(gl::ColorF *color)
{
color->alpha = 1.0f;
}
void ClipChannelsLuminance(gl::ColorF *color)
{
color->alpha = 1.0f;
}
void ClipChannelsAlpha(gl::ColorF *color)
{
color->red = 0.0f;
color->green = 0.0f;
color->blue = 0.0f;
}
void ClipChannelsNoOp(gl::ColorF *color)
{
}
void WriteUintColor(const gl::ColorF &color,
ColorWriteFunction colorWriteFunction,
uint8_t *destPixelData)
{
gl::ColorUI destColor(
static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255));
colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
}
void WriteFloatColor(const gl::ColorF &color,
ColorWriteFunction colorWriteFunction,
uint8_t *destPixelData)
{
colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
}
} // anonymous namespace } // anonymous namespace
PackPixelsParams::PackPixelsParams() PackPixelsParams::PackPixelsParams()
...@@ -314,4 +389,85 @@ bool ShouldUseDebugLayers(const egl::AttributeMap &attribs) ...@@ -314,4 +389,85 @@ bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
#endif // !defined(NDEBUG) #endif // !defined(NDEBUG)
} }
void CopyImageCHROMIUM(const uint8_t *sourceData,
size_t sourceRowPitch,
size_t sourcePixelBytes,
ColorReadFunction colorReadFunction,
uint8_t *destData,
size_t destRowPitch,
size_t destPixelBytes,
ColorWriteFunction colorWriteFunction,
GLenum destUnsizedFormat,
GLenum destComponentType,
size_t width,
size_t height,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha)
{
using ConversionFunction = void (*)(gl::ColorF *);
ConversionFunction conversionFunction = CopyColor;
if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
{
if (unpackPremultiplyAlpha)
{
conversionFunction = PremultiplyAlpha;
}
else
{
conversionFunction = UnmultiplyAlpha;
}
}
auto clipChannelsFunction = ClipChannelsNoOp;
switch (destUnsizedFormat)
{
case GL_RED:
clipChannelsFunction = ClipChannelsR;
break;
case GL_RG:
clipChannelsFunction = ClipChannelsRG;
break;
case GL_RGB:
clipChannelsFunction = ClipChannelsRGB;
break;
case GL_LUMINANCE:
clipChannelsFunction = ClipChannelsLuminance;
break;
case GL_ALPHA:
clipChannelsFunction = ClipChannelsAlpha;
break;
}
auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes;
gl::ColorF sourceColor;
colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
conversionFunction(&sourceColor);
clipChannelsFunction(&sourceColor);
size_t destY = 0;
if (unpackFlipY)
{
destY += (height - 1);
destY -= y;
}
else
{
destY += y;
}
uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes;
writeFunction(sourceColor, colorWriteFunction, destPixelData);
}
}
}
} // namespace rx } // namespace rx
...@@ -189,6 +189,22 @@ using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum); ...@@ -189,6 +189,22 @@ using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum);
bool ShouldUseDebugLayers(const egl::AttributeMap &attribs); bool ShouldUseDebugLayers(const egl::AttributeMap &attribs);
void CopyImageCHROMIUM(const uint8_t *sourceData,
size_t sourceRowPitch,
size_t sourcePixelBytes,
ColorReadFunction readFunction,
uint8_t *destData,
size_t destRowPitch,
size_t destPixelBytes,
ColorWriteFunction colorWriteFunction,
GLenum destUnsizedFormat,
GLenum destComponentType,
size_t width,
size_t height,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha);
} // namespace rx } // namespace rx
#endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_ #endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment