16 UINT Blocks(UINT size){
return (size+3)/4;}
17 size_t Pitch(UINT width,UINT block,UINT bytes){
return block ? size_t(Blocks(width))*block : size_t(width)*bytes;}
18 size_t Size(UINT width,UINT height,UINT block,UINT bytes){
return Pitch(width,block,bytes)*(block ? Blocks(height) : height);}
19 float Channel(uint32_t pixel,uint32_t mask,
float fallback)
21 if(!mask)
return fallback;UINT shift=0;
while(!(mask&1)){
mask>>=1;++shift;}
22 return float((pixel>>shift)&mask)/float(mask);
24 XMVECTOR Average(FXMVECTOR a,FXMVECTOR b,FXMVECTOR c,CXMVECTOR d)
26 return XMVectorScale(XMVectorAdd(XMVectorAdd(a,b),XMVectorAdd(c,d)),.25f);
28 XMVECTOR DecodePixel(
const uint8_t* data,UINT bytes,
const DDS_PIXELFORMAT& pf)
30 uint32_t value=0;memcpy(&value,data,bytes);
31 const float red=Channel(value,pf.
RBitMask,0);
const bool luminance=(pf.
flags&0x20000)!=0;
32 return XMVectorSet(red,luminance ? red : Channel(value,pf.
GBitMask,0),luminance ? red : Channel(value,pf.
BBitMask,0),Channel(value,pf.
ABitMask,1));
34 std::vector<XMFLOAT4> DecodeNextMip(
const uint8_t* data,UINT width,UINT height,UINT block,UINT bytes,
const DDS_PIXELFORMAT& pf)
36 const UINT nextWidth=(std::max)(1u,width/2),nextHeight=(std::max)(1u,height/2);
37 std::vector<XMFLOAT4> pixels(
size_t(nextWidth)*nextHeight);
43 for(UINT y=0;y<nextHeight;y+=2)
for(UINT x=0;x<nextWidth;x+=2)
45 XMVECTOR colors[16];
const auto* input=data+(size_t(y/2)*Blocks(width)+x/2)*block;
48 for(UINT v=0;v<2 && y+v<nextHeight;++v)
for(UINT u=0;u<2 && x+u<nextWidth;++u)
50 const UINT x0=u*2,x1=(std::min)(x0+1,width-x*2-1),y0=v*2,y1=(std::min)(y0+1,height-y*2-1);
51 XMStoreFloat4(&pixels[
size_t(y+v)*nextWidth+x+u],Average(colors[y0*4+x0],colors[y0*4+x1],colors[y1*4+x0],colors[y1*4+x1]));
55 else for(UINT y=0;y<nextHeight;++y)
for(UINT x=0;x<nextWidth;++x)
57 const UINT x0=x*2,x1=(std::min)(x0+1,width-1),y0=y*2,y1=(std::min)(y0+1,height-1);
58 XMStoreFloat4(&pixels[
size_t(y)*nextWidth+x],Average(
59 DecodePixel(data+(
size_t(y0)*width+x0)*bytes,bytes,pf),DecodePixel(data+(
size_t(y0)*width+x1)*bytes,bytes,pf),
60 DecodePixel(data+(
size_t(y1)*width+x0)*bytes,bytes,pf),DecodePixel(data+(
size_t(y1)*width+x1)*bytes,bytes,pf)));
64 void Downsample(std::vector<XMFLOAT4>& pixels,UINT width,UINT height,UINT nextWidth,UINT nextHeight)
70 for(UINT y=0;y<nextHeight;++y)
for(UINT x=0;x<nextWidth;++x)
72 const UINT x0=x*2,x1=(std::min)(x0+1,width-1),y0=y*2,y1=(std::min)(y0+1,height-1);
73 const XMVECTOR average=Average(XMLoadFloat4(&pixels[
size_t(y0)*width+x0]),XMLoadFloat4(&pixels[
size_t(y0)*width+x1]),
74 XMLoadFloat4(&pixels[
size_t(y1)*width+x0]),XMLoadFloat4(&pixels[
size_t(y1)*width+x1]));
75 XMStoreFloat4(&pixels[
size_t(y)*nextWidth+x],average);
77 pixels.resize(
size_t(nextWidth)*nextHeight);
79 void Encode(uint8_t* destination,
const std::vector<XMFLOAT4>& pixels,UINT width,UINT height,UINT block,UINT bytes,
const DDS_PIXELFORMAT& pf)
83 for(UINT y=0;y<height;y+=4)
for(UINT x=0;x<width;x+=4)
85 XMVECTOR colors[16];
for(UINT v=0;v<4;++v)
for(UINT u=0;u<4;++u)
86 colors[v*4+u]=XMLoadFloat4(&pixels[
size_t((std::min)(y+v,height-1))*width+(std::min)(x+u,width-1)]);
87 auto*
output=destination+(size_t(y/4)*Blocks(width)+x/4)*block;
92 else for(
size_t i=0;i<pixels.size();++i)
94 const auto channel=[](
float value,uint32_t
mask)->uint32_t
95 {
if(!mask)
return 0;UINT shift=0;
while(!(mask&1)){
mask>>=1;++shift;}
return uint32_t((std::clamp)(value,0.f,1.f)*mask+.5f)<<shift;};
97 memcpy(destination+i*bytes,&value,bytes);
101 HRESULT PrepareTextureDDS(
const uint8_t* data,
size_t size,UINT requested,UINT requestedWidth,UINT requestedHeight,std::vector<uint8_t>& output)
104 output.clear();
if(!data || size<4)
return E_INVALIDARG;
if(memcmp(data,
"DDS ",4))
return S_FALSE;
105 if(size<128)
return E_INVALIDARG;
DDS_HEADER header{};memcpy(&header,data+4,
sizeof(header));
107 if(header.
caps2&(0x200000|0x200))
return S_FALSE;
108 const auto& pf=header.
ddspf;UINT block=0,bytes=0;
111 if(pf.
fourCC==0x31545844u)block=8;
112 else if(pf.
fourCC==0x33545844u || pf.
fourCC==0x35545844u)block=16;
117 UINT width=requestedWidth ? requestedWidth : header.
width,height=requestedHeight ? requestedHeight : header.
height;
118 if(block){
if(requestedWidth)width=(width+3)&~3u;
if(requestedHeight)height=(height+3)&~3u;}
119 if(!width || !height || width>header.
width || height>header.
height)
return E_INVALIDARG;
120 const bool crop=width!=header.
width || height!=header.
height;
121 UINT levels=1;
for(UINT extent=(std::max)(width,height);extent>1;extent>>=1)++levels;
122 if(requested && requested!=UINT_MAX)levels=(std::min)(levels,requested);
123 const UINT authored=(std::max)(1u,header.
mipMapCount);
124 if(!crop && authored>=levels)
return S_FALSE;
125 if(Size(header.
width,header.
height,block,bytes)>size-128)
return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
128 size_t outputSize=128;UINT mipWidth=width,mipHeight=height;
129 for(UINT level=0;level<levels;++level)
131 outputSize+=Size(mipWidth,mipHeight,block,bytes);
132 mipWidth=(std::max)(1u,mipWidth/2);mipHeight=(std::max)(1u,mipHeight/2);
134 output.reserve(outputSize);output.assign(data,data+128);
const UINT baseWidth=width,baseHeight=height;
135 size_t inputOffset=128;std::vector<XMFLOAT4> pixels;
136 for(UINT level=0;level<levels;++level)
138 const size_t count=Size(width,height,block,bytes),offset=output.size();output.resize(offset+count);
139 if(!crop && level<authored)
141 if(count>size-inputOffset){output.clear();
return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);}
142 memcpy(output.data()+offset,data+inputOffset,count);inputOffset+=count;
144 if(level+1==authored && level+1<levels)pixels=DecodeNextMip(output.data()+offset,width,height,block,bytes,pf);
148 const size_t rowBytes=Pitch(width,block,bytes),sourcePitch=Pitch(header.
width,block,bytes);
149 for(UINT row=0;row<(block ? Blocks(height) : height);++row)memcpy(output.data()+offset+row*rowBytes,data+128+row*sourcePitch,rowBytes);
150 if(level+1<levels)pixels=DecodeNextMip(output.data()+offset,width,height,block,bytes,pf);
154 Encode(output.data()+offset,pixels,width,height,block,bytes,pf);
155 if(level+1<levels)Downsample(pixels,width,height,(std::max)(1u,width/2),(std::max)(1u,height/2));
157 const UINT nextWidth=(std::max)(1u,width/2),nextHeight=(std::max)(1u,height/2);
158 width=nextWidth;height=nextHeight;
161 header.
pitchOrLinearSize=UINT(block ? Size(baseWidth,baseHeight,block,bytes) : Pitch(baseWidth,block,bytes));
162 memcpy(output.data()+4,&header,
sizeof(header));
return S_OK;
164 catch(
const std::bad_alloc&)
166 output.clear();
return E_OUTOFMEMORY;
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
void D3DXEncodeBC2(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXDecodeBC2(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
void D3DXDecodeBC1(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(8) const uint8_t *pBC) noexcept
void D3DXDecodeBC3(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_reads_(16) const uint8_t *pBC) noexcept
void D3DXEncodeBC3(_Out_writes_(16) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ uint32_t flags) noexcept
void D3DXEncodeBC1(_Out_writes_(8) uint8_t *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const XMVECTOR *pColor, _In_ float threshold, _In_ uint32_t flags) noexcept
double Pitch(double selected, double horizontal, double vertical, double pitch, double previous, double dt)
HRESULT PrepareTextureDDS(const uint8_t *data, size_t size, UINT requested, UINT requestedWidth, UINT requestedHeight, std::vector< uint8_t > &output)
uint32_t pitchOrLinearSize