Rise
The Vieneo Province
Loading...
Searching...
No Matches
TextureMips11.cpp
Go to the documentation of this file.
1#include "TextureMips11.h"
2#include <algorithm>
3#include <cfloat>
4#include <cassert>
5#include <dxgiformat.h>
8#include <cmath>
9#include <cstring>
10#include <new>
11using namespace DirectX;
12namespace Rendering
13{
14 namespace
15 {
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)
20 {
21 if(!mask)return fallback;UINT shift=0;while(!(mask&1)){mask>>=1;++shift;}
22 return float((pixel>>shift)&mask)/float(mask);
23 }
24 XMVECTOR Average(FXMVECTOR a,FXMVECTOR b,FXMVECTOR c,CXMVECTOR d)
25 {
26 return XMVectorScale(XMVectorAdd(XMVectorAdd(a,b),XMVectorAdd(c,d)),.25f);
27 }
28 XMVECTOR DecodePixel(const uint8_t* data,UINT bytes,const DDS_PIXELFORMAT& pf)
29 {
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));
33 }
34 std::vector<XMFLOAT4> DecodeNextMip(const uint8_t* data,UINT width,UINT height,UINT block,UINT bytes,const DDS_PIXELFORMAT& pf)
35 {
36 const UINT nextWidth=(std::max)(1u,width/2),nextHeight=(std::max)(1u,height/2);
37 std::vector<XMFLOAT4> pixels(size_t(nextWidth)*nextHeight);
38 // Decode only a block (or four texels) at a time directly into the
39 // first missing mip. A full float4 copy of the authored base can
40 // exhaust a fragmented Win32 address space during cockpit changes.
41 if(block)
42 {
43 for(UINT y=0;y<nextHeight;y+=2)for(UINT x=0;x<nextWidth;x+=2)
44 {
45 XMVECTOR colors[16];const auto* input=data+(size_t(y/2)*Blocks(width)+x/2)*block;
46 if(pf.fourCC==0x31545844u)D3DXDecodeBC1(colors,input);
47 else if(pf.fourCC==0x33545844u)D3DXDecodeBC2(colors,input);else D3DXDecodeBC3(colors,input);
48 for(UINT v=0;v<2 && y+v<nextHeight;++v)for(UINT u=0;u<2 && x+u<nextWidth;++u)
49 {
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]));
52 }
53 }
54 }
55 else for(UINT y=0;y<nextHeight;++y)for(UINT x=0;x<nextWidth;++x)
56 {
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)));
61 }
62 return pixels;
63 }
64 void Downsample(std::vector<XMFLOAT4>& pixels,UINT width,UINT height,UINT nextWidth,UINT nextHeight)
65 {
66 // Match the shipped BOX mip filter: average adjacent 2x2 texels,
67 // retaining one sample on dimensions that have reached one pixel.
68 // Each write is at or before its source texels, so the next level
69 // can reuse this storage without overwriting unread input.
70 for(UINT y=0;y<nextHeight;++y)for(UINT x=0;x<nextWidth;++x)
71 {
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);
76 }
77 pixels.resize(size_t(nextWidth)*nextHeight);
78 }
79 void Encode(uint8_t* destination,const std::vector<XMFLOAT4>& pixels,UINT width,UINT height,UINT block,UINT bytes,const DDS_PIXELFORMAT& pf)
80 {
81 if(block)
82 {
83 for(UINT y=0;y<height;y+=4)for(UINT x=0;x<width;x+=4)
84 {
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;
88 if(pf.fourCC==0x31545844u)D3DXEncodeBC1(output,colors,.5f,BC_FLAGS_NONE);
89 else if(pf.fourCC==0x33545844u)D3DXEncodeBC2(output,colors,BC_FLAGS_NONE);else D3DXEncodeBC3(output,colors,BC_FLAGS_NONE);
90 }
91 }
92 else for(size_t i=0;i<pixels.size();++i)
93 {
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;};
96 const auto& p=pixels[i];const uint32_t value=channel(p.x,pf.RBitMask)|channel(p.y,pf.GBitMask)|channel(p.z,pf.BBitMask)|channel(p.w,pf.ABitMask);
97 memcpy(destination+i*bytes,&value,bytes);
98 }
99 }
100 }
101 HRESULT PrepareTextureDDS(const uint8_t* data,size_t size,UINT requested,UINT requestedWidth,UINT requestedHeight,std::vector<uint8_t>& output)
102 try
103 {
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));
106 if(header.size!=124 || header.ddspf.size!=32 || !header.width || !header.height || header.width>16384 || header.height>16384)return E_INVALIDARG;
107 if(header.caps2&(0x200000|0x200))return S_FALSE; // Authored volume and cube chains are already complete.
108 const auto& pf=header.ddspf;UINT block=0,bytes=0;
109 if(pf.flags&4)
110 {
111 if(pf.fourCC==0x31545844u)block=8;
112 else if(pf.fourCC==0x33545844u || pf.fourCC==0x35545844u)block=16;
113 else return S_FALSE;
114 }
115 else if((pf.flags&(0x40|0x20000)) && (pf.RGBBitCount==8 || pf.RGBBitCount==16 || pf.RGBBitCount==32))bytes=pf.RGBBitCount/8;
116 else return S_FALSE;
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);
126 // Reserve the complete encoded chain once; growing it while the float
127 // workspace is live would briefly retain both the old and new buffers.
128 size_t outputSize=128;UINT mipWidth=width,mipHeight=height;
129 for(UINT level=0;level<levels;++level)
130 {
131 outputSize+=Size(mipWidth,mipHeight,block,bytes);
132 mipWidth=(std::max)(1u,mipWidth/2);mipHeight=(std::max)(1u,mipHeight/2);
133 }
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)
137 {
138 const size_t count=Size(width,height,block,bytes),offset=output.size();output.resize(offset+count);
139 if(!crop && level<authored)
140 {
141 if(count>size-inputOffset){output.clear();return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);}
142 memcpy(output.data()+offset,data+inputOffset,count);inputOffset+=count;
143 // Start generated levels from the last authored level.
144 if(level+1==authored && level+1<levels)pixels=DecodeNextMip(output.data()+offset,width,height,block,bytes,pf);
145 }
146 else if(level==0)
147 {
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);
151 }
152 else
153 {
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));
156 }
157 const UINT nextWidth=(std::max)(1u,width/2),nextHeight=(std::max)(1u,height/2);
158 width=nextWidth;height=nextHeight;
159 }
160 header.width=baseWidth;header.height=baseHeight;header.mipMapCount=levels;header.flags|=0x20000;header.caps|=0x400008;
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;
163 }
164 catch(const std::bad_alloc&)
165 {
166 output.clear();return E_OUTOFMEMORY;
167 }
168}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
Definition DDS.h:24
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
@ BC_FLAGS_NONE
Definition BC.h:32
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)
Definition FlightPath.h:24
HRESULT PrepareTextureDDS(const uint8_t *data, size_t size, UINT requested, UINT requestedWidth, UINT requestedHeight, std::vector< uint8_t > &output)
uint32_t size
Definition DDS.h:264
uint32_t mipMapCount
Definition DDS.h:270
uint32_t caps2
Definition DDS.h:274
uint32_t flags
Definition DDS.h:265
uint32_t pitchOrLinearSize
Definition DDS.h:268
uint32_t width
Definition DDS.h:267
uint32_t height
Definition DDS.h:266
DDS_PIXELFORMAT ddspf
Definition DDS.h:272
uint32_t caps
Definition DDS.h:273
uint32_t ABitMask
Definition DDS.h:39
uint32_t BBitMask
Definition DDS.h:38
uint32_t RGBBitCount
Definition DDS.h:35
uint32_t GBitMask
Definition DDS.h:37
uint32_t RBitMask
Definition DDS.h:36