michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // ImageSSE2.cpp: Implements SSE2-based functions of rx::Image class. It's michael@0: // in a separated file for GCC, which can enable SSE usage only per-file, michael@0: // not for code blocks that use SSE2 explicitly. michael@0: michael@0: #include "libGLESv2/Texture.h" michael@0: #include "libGLESv2/renderer/Image.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: void Image::loadRGBAUByteDataToBGRASSE2(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned int *source = NULL; michael@0: unsigned int *dest = NULL; michael@0: __m128i brMask = _mm_set1_epi32(0x00ff00ff); michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = reinterpret_cast(static_cast(input) + y * inputPitch); michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: int x = 0; michael@0: michael@0: // Make output writes aligned michael@0: for (x = 0; ((reinterpret_cast(&dest[x]) & 15) != 0) && x < width; x++) michael@0: { michael@0: unsigned int rgba = source[x]; michael@0: dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00); michael@0: } michael@0: michael@0: for (; x + 3 < width; x += 4) michael@0: { michael@0: __m128i sourceData = _mm_loadu_si128(reinterpret_cast(&source[x])); michael@0: // Mask out g and a, which don't change michael@0: __m128i gaComponents = _mm_andnot_si128(brMask, sourceData); michael@0: // Mask out b and r michael@0: __m128i brComponents = _mm_and_si128(sourceData, brMask); michael@0: // Swap b and r michael@0: __m128i brSwapped = _mm_shufflehi_epi16(_mm_shufflelo_epi16(brComponents, _MM_SHUFFLE(2, 3, 0, 1)), _MM_SHUFFLE(2, 3, 0, 1)); michael@0: __m128i result = _mm_or_si128(gaComponents, brSwapped); michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x]), result); michael@0: } michael@0: michael@0: // Perform leftover writes michael@0: for (; x < width; x++) michael@0: { michael@0: unsigned int rgba = source[x]; michael@0: dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Image::loadAlphaDataToBGRASSE2(GLsizei width, GLsizei height, michael@0: int inputPitch, const void *input, size_t outputPitch, void *output) michael@0: { michael@0: const unsigned char *source = NULL; michael@0: unsigned int *dest = NULL; michael@0: __m128i zeroWide = _mm_setzero_si128(); michael@0: michael@0: for (int y = 0; y < height; y++) michael@0: { michael@0: source = static_cast(input) + y * inputPitch; michael@0: dest = reinterpret_cast(static_cast(output) + y * outputPitch); michael@0: michael@0: int x; michael@0: // Make output writes aligned michael@0: for (x = 0; ((reinterpret_cast(&dest[x]) & 0xF) != 0 && x < width); x++) michael@0: { michael@0: dest[x] = static_cast(source[x]) << 24; michael@0: } michael@0: michael@0: for (; x + 7 < width; x += 8) michael@0: { michael@0: __m128i sourceData = _mm_loadl_epi64(reinterpret_cast(&source[x])); michael@0: // Interleave each byte to 16bit, make the lower byte to zero michael@0: sourceData = _mm_unpacklo_epi8(zeroWide, sourceData); michael@0: // Interleave each 16bit to 32bit, make the lower 16bit to zero michael@0: __m128i lo = _mm_unpacklo_epi16(zeroWide, sourceData); michael@0: __m128i hi = _mm_unpackhi_epi16(zeroWide, sourceData); michael@0: michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x]), lo); michael@0: _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x + 4]), hi); michael@0: } michael@0: michael@0: // Handle the remainder michael@0: for (; x < width; x++) michael@0: { michael@0: dest[x] = static_cast(source[x]) << 24; michael@0: } michael@0: } michael@0: } michael@0: michael@0: }