As we all know the D3DX## helper libraries have been deprecated, and I've ran into a couple of issues trying to adapt to this change.
So far the coolest (even though not greatest) piece of code I've switch over to the new standard is some code to load a texture array into DirectX properly.
I know most of you guys could probably figure it out in a couple of hours, but I want to save you the headache.
Ok, so here it is...
Note: You need the DirectX Toll Kit Lib, which you can find the source code here...
directxtk.codeplex.com/
Display All
So far the coolest (even though not greatest) piece of code I've switch over to the new standard is some code to load a texture array into DirectX properly.
I know most of you guys could probably figure it out in a couple of hours, but I want to save you the headache.
Ok, so here it is...
Note: You need the DirectX Toll Kit Lib, which you can find the source code here...
directxtk.codeplex.com/
Source Code
- static ID3D11ShaderResourceView* CreateTexture2DArray(ID3D11Device* d3dDEvice, ID3D11DeviceContext* context,
- const vector<wstring>& filesname)
- {
- uint textureCount = filesname.size();
- vector<ID3D11Texture2D*> vResources2D(textureCount);
- //Create a 2DTexture for each texture file
- for(uint i = 0; i < textureCount; ++i)
- {
- //Note: This function is in the DirectX Tool Kit Library
- CreateDDSTextureFromFileEx(d3dDEvice,
- filesname[i].c_str(),
- NULL,
- D3D11_USAGE_IMMUTABLE,//You can also use D3D11_USAGE_DEFAULT
- D3D11_BIND_SHADER_RESOURCE,
- NULL,
- NULL,
- false,
- reinterpret_cast<ID3D11Resource**>(&vResources2D[i]),
- nullptr);//Ensure the last parameter is NULL so a view to the texture is not created!
- }
- //Get the description from at least 1 of the textures since both HAVE to be the same dimension and format
- D3D11_TEXTURE2D_DESC vtexDesc;
- vResources2D[0]->GetDesc(&vtexDesc);
- D3D11_TEXTURE2D_DESC texDesc;
- texDesc.Width = vtexDesc.Width;
- texDesc.Height = vtexDesc.Height;
- texDesc.MipLevels = vtexDesc.MipLevels;
- texDesc.ArraySize = textureCount;
- texDesc.Format = vtexDesc.Format;
- texDesc.SampleDesc.Count = 1;
- texDesc.SampleDesc.Quality = 0;
- texDesc.Usage = D3D11_USAGE_DEFAULT;
- texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
- texDesc.CPUAccessFlags = NULL;
- texDesc.MiscFlags = NULL;
- ID3D11Texture2D* textureArray = nullptr;
- d3dDEvice->CreateTexture2D(&texDesc, NULL, &textureArray);
- //Per texture
- for(uint itextureIndex = 0; itextureIndex < textureCount; ++itextureIndex)
- {
- //Per MipMap level
- for(uint miplevel = 0; miplevel < texDesc.MipLevels; miplevel++)
- {
- context->CopySubresourceRegion(textureArray,
- D3D11CalcSubresource(miplevel, itextureIndex, texDesc.MipLevels),
- NULL,
- NULL,
- NULL,
- vResources2D[itextureIndex],
- miplevel,
- nullptr);
- }
- }
- //Release the Video Memory used per temporary textures
- for(uint i = 0; i < textureCount; ++i)
- ReleaseCOM(vResources2D[i]);
- //Create the view to the Texture array by filling out the Texture2DArray union from D3D11_SHADER_RESOURCE_VIEW_DESC
- D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
- srvDesc.Format = texDesc.Format;
- srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
- srvDesc.Texture2DArray.MostDetailedMip = 0;
- srvDesc.Texture2DArray.MipLevels = texDesc.MipLevels;
- srvDesc.Texture2DArray.FirstArraySlice = 0;
- srvDesc.Texture2DArray.ArraySize = textureCount;
- ID3D11ShaderResourceView* TextureSRV = nullptr;
- d3dDEvice->CreateShaderResourceView(textureArray, &srvDesc, &TextureSRV);
- ReleaseCOM(textureArray);
- //Remember that the pointer returned here needs to be properly release by YOUR application
- return TextureSRV;
- }
Intel i7 3930k
8GB Mushkin LP @ 2133 mhz
GTX 680
Asus Rampage IV Extreme
Corsair 650w
8GB Mushkin LP @ 2133 mhz
GTX 680
Asus Rampage IV Extreme
Corsair 650w
The post was edited 2 times, last by hondero ().