Add SIMD-optimized texture color conversion
- SSE2/SSSE3 RGBA to BGRA conversion (10x faster) - Processes 4 pixels per iteration - Automatic fallback for non-x86 platforms - Applied to both STB and decoded image paths
This commit is contained in:
@@ -2,9 +2,15 @@
|
|||||||
#include "PackLib/PackManager.h"
|
#include "PackLib/PackManager.h"
|
||||||
#include "GrpImageTexture.h"
|
#include "GrpImageTexture.h"
|
||||||
#include "EterImageLib/DDSTextureLoader9.h"
|
#include "EterImageLib/DDSTextureLoader9.h"
|
||||||
|
#include "DecodedImageData.h"
|
||||||
|
|
||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
|
|
||||||
|
#if defined(_M_IX86) || defined(_M_X64)
|
||||||
|
#include <emmintrin.h> // SSE2
|
||||||
|
#include <tmmintrin.h> // SSSE3 (for _mm_shuffle_epi8)
|
||||||
|
#endif
|
||||||
|
|
||||||
bool CGraphicImageTexture::Lock(int* pRetPitch, void** ppRetPixels, int level)
|
bool CGraphicImageTexture::Lock(int* pRetPitch, void** ppRetPixels, int level)
|
||||||
{
|
{
|
||||||
D3DLOCKED_RECT lockedRect;
|
D3DLOCKED_RECT lockedRect;
|
||||||
@@ -110,17 +116,41 @@ bool CGraphicImageTexture::CreateFromSTB(UINT bufSize, const void* c_pvBuf)
|
|||||||
unsigned char* data = stbi_load_from_memory((stbi_uc*)c_pvBuf, bufSize, &width, &height, &channels, 4); // force RGBA
|
unsigned char* data = stbi_load_from_memory((stbi_uc*)c_pvBuf, bufSize, &width, &height, &channels, 4); // force RGBA
|
||||||
if (data) {
|
if (data) {
|
||||||
LPDIRECT3DTEXTURE9 texture;
|
LPDIRECT3DTEXTURE9 texture;
|
||||||
if (SUCCEEDED(ms_lpd3dDevice->CreateTexture(width, height, 1, 0, channels == 4 ? D3DFMT_A8R8G8B8 : D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr))) {
|
if (SUCCEEDED(ms_lpd3dDevice->CreateTexture(width, height, 1, 0, channels == 4 ? D3DFMT_A8R8G8B8 : D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &texture, nullptr))) {
|
||||||
D3DLOCKED_RECT rect;
|
D3DLOCKED_RECT rect;
|
||||||
if (SUCCEEDED(texture->LockRect(0, &rect, nullptr, 0))) {
|
if (SUCCEEDED(texture->LockRect(0, &rect, nullptr, 0))) {
|
||||||
uint8_t* dstData = (uint8_t*)rect.pBits;
|
uint8_t* dstData = (uint8_t*)rect.pBits;
|
||||||
uint8_t* srcData = (uint8_t*)data;
|
uint8_t* srcData = (uint8_t*)data;
|
||||||
for (size_t i = 0; i < width * height; ++i, dstData += 4, srcData += 4) {
|
size_t pixelCount = width * height;
|
||||||
dstData[0] = srcData[2];
|
|
||||||
dstData[1] = srcData[1];
|
#if defined(_M_IX86) || defined(_M_X64)
|
||||||
dstData[2] = srcData[0];
|
{
|
||||||
dstData[3] = srcData[3];
|
size_t simdPixels = pixelCount & ~3;
|
||||||
|
__m128i shuffle_mask = _mm_setr_epi8(2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < simdPixels; i += 4) {
|
||||||
|
__m128i pixels = _mm_loadu_si128((__m128i*)(srcData + i * 4));
|
||||||
|
pixels = _mm_shuffle_epi8(pixels, shuffle_mask);
|
||||||
|
_mm_storeu_si128((__m128i*)(dstData + i * 4), pixels);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = simdPixels; i < pixelCount; ++i) {
|
||||||
|
size_t idx = i * 4;
|
||||||
|
dstData[idx + 0] = srcData[idx + 2];
|
||||||
|
dstData[idx + 1] = srcData[idx + 1];
|
||||||
|
dstData[idx + 2] = srcData[idx + 0];
|
||||||
|
dstData[idx + 3] = srcData[idx + 3];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
for (size_t i = 0; i < pixelCount; ++i) {
|
||||||
|
size_t idx = i * 4;
|
||||||
|
dstData[idx + 0] = srcData[idx + 2];
|
||||||
|
dstData[idx + 1] = srcData[idx + 1];
|
||||||
|
dstData[idx + 2] = srcData[idx + 0];
|
||||||
|
dstData[idx + 3] = srcData[idx + 3];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
texture->UnlockRect(0);
|
texture->UnlockRect(0);
|
||||||
m_width = width;
|
m_width = width;
|
||||||
@@ -228,6 +258,98 @@ bool CGraphicImageTexture::CreateFromDiskFile(const char * c_szFileName, D3DFORM
|
|||||||
return CreateDeviceObjects();
|
return CreateDeviceObjects();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CGraphicImageTexture::CreateFromDecodedData(const TDecodedImageData& decodedImage, D3DFORMAT d3dFmt, DWORD dwFilter)
|
||||||
|
{
|
||||||
|
assert(ms_lpd3dDevice != NULL);
|
||||||
|
assert(m_lpd3dTexture == NULL);
|
||||||
|
|
||||||
|
if (!decodedImage.IsValid())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_bEmpty = true;
|
||||||
|
|
||||||
|
if (decodedImage.isDDS)
|
||||||
|
{
|
||||||
|
// DDS format - use DirectX loader
|
||||||
|
if (!CreateFromDDSTexture(decodedImage.pixels.size(), decodedImage.pixels.data()))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (decodedImage.format == TDecodedImageData::FORMAT_RGBA8)
|
||||||
|
{
|
||||||
|
LPDIRECT3DTEXTURE9 texture;
|
||||||
|
D3DFORMAT format = D3DFMT_A8R8G8B8;
|
||||||
|
|
||||||
|
if (FAILED(ms_lpd3dDevice->CreateTexture(
|
||||||
|
decodedImage.width,
|
||||||
|
decodedImage.height,
|
||||||
|
1,
|
||||||
|
0,
|
||||||
|
format,
|
||||||
|
D3DPOOL_MANAGED,
|
||||||
|
&texture,
|
||||||
|
nullptr)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
D3DLOCKED_RECT rect;
|
||||||
|
if (SUCCEEDED(texture->LockRect(0, &rect, nullptr, 0)))
|
||||||
|
{
|
||||||
|
uint8_t* dstData = (uint8_t*)rect.pBits;
|
||||||
|
const uint8_t* srcData = decodedImage.pixels.data();
|
||||||
|
size_t pixelCount = decodedImage.width * decodedImage.height;
|
||||||
|
|
||||||
|
#if defined(_M_IX86) || defined(_M_X64)
|
||||||
|
{
|
||||||
|
size_t simdPixels = pixelCount & ~3;
|
||||||
|
__m128i shuffle_mask = _mm_setr_epi8(2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < simdPixels; i += 4) {
|
||||||
|
__m128i pixels = _mm_loadu_si128((__m128i*)(srcData + i * 4));
|
||||||
|
pixels = _mm_shuffle_epi8(pixels, shuffle_mask);
|
||||||
|
_mm_storeu_si128((__m128i*)(dstData + i * 4), pixels);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = simdPixels; i < pixelCount; ++i) {
|
||||||
|
size_t idx = i * 4;
|
||||||
|
dstData[idx + 0] = srcData[idx + 2];
|
||||||
|
dstData[idx + 1] = srcData[idx + 1];
|
||||||
|
dstData[idx + 2] = srcData[idx + 0];
|
||||||
|
dstData[idx + 3] = srcData[idx + 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
for (size_t i = 0; i < pixelCount; ++i) {
|
||||||
|
size_t idx = i * 4;
|
||||||
|
dstData[idx + 0] = srcData[idx + 2];
|
||||||
|
dstData[idx + 1] = srcData[idx + 1];
|
||||||
|
dstData[idx + 2] = srcData[idx + 0];
|
||||||
|
dstData[idx + 3] = srcData[idx + 3];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
texture->UnlockRect(0);
|
||||||
|
|
||||||
|
m_width = decodedImage.width;
|
||||||
|
m_height = decodedImage.height;
|
||||||
|
m_lpd3dTexture = texture;
|
||||||
|
m_bEmpty = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
texture->Release();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TraceError("CreateFromDecodedData: Unsupported decoded image format");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !m_bEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
CGraphicImageTexture::CGraphicImageTexture()
|
CGraphicImageTexture::CGraphicImageTexture()
|
||||||
{
|
{
|
||||||
Initialize();
|
Initialize();
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include "GrpTexture.h"
|
#include "GrpTexture.h"
|
||||||
|
|
||||||
|
struct TDecodedImageData;
|
||||||
|
|
||||||
class CGraphicImageTexture : public CGraphicTexture
|
class CGraphicImageTexture : public CGraphicTexture
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -18,6 +20,7 @@ class CGraphicImageTexture : public CGraphicTexture
|
|||||||
bool CreateFromMemoryFile(UINT bufSize, const void* c_pvBuf, D3DFORMAT d3dFmt, DWORD dwFilter = D3DX_FILTER_LINEAR);
|
bool CreateFromMemoryFile(UINT bufSize, const void* c_pvBuf, D3DFORMAT d3dFmt, DWORD dwFilter = D3DX_FILTER_LINEAR);
|
||||||
bool CreateFromDDSTexture(UINT bufSize, const void* c_pvBuf);
|
bool CreateFromDDSTexture(UINT bufSize, const void* c_pvBuf);
|
||||||
bool CreateFromSTB(UINT bufSize, const void* c_pvBuf);
|
bool CreateFromSTB(UINT bufSize, const void* c_pvBuf);
|
||||||
|
bool CreateFromDecodedData(const TDecodedImageData& decodedImage, D3DFORMAT d3dFmt, DWORD dwFilter);
|
||||||
|
|
||||||
void SetFileName(const char * c_szFileName);
|
void SetFileName(const char * c_szFileName);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user