From File Hot! - Nd3d11 Texture Create

return hr; ID3D11Texture2D* pTexture = nullptr; ID3D11ShaderResourceView* pSRV = nullptr; HRESULT hr = CreateTextureFromFile( g_pd3dDevice, L"assets/texture.png", &pTexture, &pSRV ); 4. Method 2: Manual WIC + Direct3D (Without External Library) If you cannot use DirectXTex, you can manually decode and upload. Step 1: Initialize WIC IWICImagingFactory* pWICFactory = nullptr; CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); CoCreateInstance( CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pWICFactory) ); Step 2: Load and Decode Image IWICBitmapDecoder* pDecoder = nullptr; IWICBitmapFrameDecode* pFrame = nullptr; IWICFormatConverter* pConverter = nullptr; pWICFactory->CreateDecoderFromFilename( L"image.png", nullptr, GENERIC_READ, WICDecodeMetadataCacheOnLoad, &pDecoder ); pDecoder->GetFrame(0, &pFrame);

// Create the D3D11 texture and SRV hr = DirectX::CreateTexture( pDevice, scratchImage.GetImages(), scratchImage.GetImageCount(), metadata, ppTexture ); nd3d11 texture create from file

UINT width, height; pFrame->GetSize(&width, &height); UINT stride = width * 4; UINT imageSize = stride * height; BYTE* imageData = new BYTE[imageSize]; pConverter->CopyPixels(nullptr, stride, imageSize, imageData); D3D11_TEXTURE2D_DESC desc = {}; desc.Width = width; desc.Height = height; desc.MipLevels = 1; desc.ArraySize = 1; desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; desc.SampleDesc.Count = 1; desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; desc.CPUAccessFlags = 0; D3D11_SUBRESOURCE_DATA initData = {}; initData.pSysMem = imageData; initData.SysMemPitch = stride; ID3D11Texture2D* pTexture = nullptr

| Error Code | Meaning | Solution | |------------|---------|----------| | HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) | File missing | Check file path | | WINCODEC_ERR_UNKNOWNIMAGEFORMAT | Unsupported format | Convert to PNG/JPG/DDS | | E_OUTOFMEMORY | Texture too large | Reduce resolution or use tiling | | DXGI_ERROR_UNSUPPORTED | Format not supported | Use CreateTexture with conversion flags | ID3D11ShaderResourceView* pSRV = nullptr

HRESULT hr = DirectX::LoadFromDDSFile( L"texture.dds", DirectX::DDS_FLAGS_NONE, &metadata, scratchImage );

Наверх