microsoft / DirectXTex

DirectXTex texture processing library
https://walbourn.github.io/directxtex/
MIT License
1.79k stars 438 forks source link

D3DX11Create*From* Sample #8

Open walbourn opened 9 years ago

walbourn commented 9 years ago

There's no directly equivalent DirectXTex function to the legacy D3DX11 functions D3DX11CreateTextureFromFile, D3DX11CreateShaderResourceViewFromFile, D3DX11CreateTextureFromMemory, and D3DX11CreateShaderResourceViewFromMemory. The same operations can be performed by DirectXTex, and generally these old functions are total overkill for most users.

However, a utility that demonstrates how to implement all the old 'full-fat' functionality' could be useful for porting as well as be a good sample of how to use DirectXTex in complex ways (rather than just texconv/texassemble).

Note that I won't be implementing the "ThreadPump" async loader behavior that is tied into some D3DX11 only tech

typedef enum D3DX11_FILTER_FLAG { 
  D3DX11_FILTER_NONE              = (1 << 0),
  D3DX11_FILTER_POINT             = (2 << 0),
  D3DX11_FILTER_LINEAR            = (3 << 0),
  D3DX11_FILTER_TRIANGLE          = (4 << 0),
  D3DX11_FILTER_BOX               = (5 << 0),
  D3DX11_FILTER_MIRROR_U          = (1 << 16),
  D3DX11_FILTER_MIRROR_V          = (2 << 16),
  D3DX11_FILTER_MIRROR_W          = (4 << 16),
  D3DX11_FILTER_MIRROR            = (7 << 16),
  D3DX11_FILTER_DITHER            = (1 << 19),
  D3DX11_FILTER_DITHER_DIFFUSION  = (2 << 19),
  D3DX11_FILTER_SRGB_IN           = (1 << 21),
  D3DX11_FILTER_SRGB_OUT          = (2 << 21),
  D3DX11_FILTER_SRGB              = (3 << 21)
} D3DX11_FILTER_FLAG, *LPD3DX11_FILTER_FLAG;

typedef struct D3DX11_IMAGE_LOAD_INFO {
  UINT              Width;
  UINT              Height;
  UINT              Depth;
  UINT              FirstMipLevel;
  UINT              MipLevels;
  D3D11_USAGE       Usage;
  UINT              BindFlags;
  UINT              CpuAccessFlags;
  UINT              MiscFlags;
  DXGI_FORMAT       Format;
  UINT              Filter;
  UINT              MipFilter;
  D3DX11_IMAGE_INFO *pSrcInfo;
} D3DX11_IMAGE_LOAD_INFO, *LPD3DX11_IMAGE_LOAD_INFO;

HRESULT D3DX11CreateTextureFromFile(
  _In_   ID3D11Device *pDevice,
  _In_   LPCTSTR pSrcFile,
  _In_   D3DX11_IMAGE_LOAD_INFO *pLoadInfo,
  _Out_  ID3D11Resource **ppTexture
);

HRESULT D3DX11CreateTextureFromMemory(
  _In_   ID3D11Device *pDevice,
  _In_   LPCVOID pSrcData,
  _In_   SIZE_T SrcDataSize,
  _In_   D3DX11_IMAGE_LOAD_INFO *pLoadInfo,
  _Out_  ID3D11Resource **ppTexture
);

HRESULT D3DX11CreateShaderResourceViewFromFile(
  _In_   ID3D11Device *pDevice,
  _In_   LPCTSTR pSrcFile,
  _In_   D3DX11_IMAGE_LOAD_INFO *pLoadInfo,
  _Out_  ID3D11ShaderResourceView **ppShaderResourceView
);

HRESULT D3DX11CreateShaderResourceViewFromMemory(
  _In_   ID3D11Device *pDevice,
  _In_   LPCVOID pSrcData,
  _In_   SIZE_T SrcDataSize,
  _In_   D3DX11_IMAGE_LOAD_INFO *pLoadInfo,
 _Out_  ID3D11ShaderResourceView **ppShaderResourceView
);
m-a-v commented 8 years ago

What is the status of this enhancement?

I need to port a call from the good old D3DX9-Library (D3DXLoadSurfaceFromMemory) to DirectXTex. Could you describe how I would implement this the most efficient way using the DirectXTex library?

walbourn commented 8 years ago

This item is really a sample/documentation issue. These legacy D3DX11 functions do a lot of processing, so I wanted to capture just how heavy-weight they really are.

For other D3DX functions, see Living without D3DX which covers it in detail. The most common use case for D3DXLoadSurfaceFromMemory is best handled by the DDSTextureLoader or WICTextureLoader in the DirectX Tool Kit which are much lighter weight.

If you need more complex processing, I'd first recommend doing it offline and storing the result in a DDS for quick & efficient loading with DDSTextureLoader. If you need more complex support (such as in a tool) where you need to do format conversions, mipmap generation, resizing at runtime more complex than what is supported by WICTextureLoader, then you'd look at using the full DirectXTex library. See this blog post.