Advertisement
Ember

Texture Creation

May 2nd, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. bool Texture::Create(Texture::Description description, Array<Texture::Mip>& mips)
  2. {
  3.     //D3D11_SUBRESOURCE_DATA* textureData;
  4.     HRESULT result;
  5.  
  6.     D3D11_TEXTURE2D_DESC textureDesc;
  7.     ZeroMemory(&textureDesc, sizeof(textureDesc));
  8.     textureDesc.Width = description.Width;
  9.     textureDesc.Height = description.Height;
  10.     textureDesc.MipLevels = description.Mips;
  11.     textureDesc.ArraySize = 1; //description.Depth;
  12.     textureDesc.Format = (DXGI_FORMAT)description.Format;
  13.     textureDesc.SampleDesc.Count = 1;
  14.     textureDesc.SampleDesc.Quality = 0;
  15.     textureDesc.Usage = (description.Flags & Texture::Flags::Immutable) ? D3D11_USAGE_IMMUTABLE : D3D11_USAGE_DEFAULT;
  16.     textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  17.     textureDesc.CPUAccessFlags = 0;
  18.     textureDesc.MiscFlags = 0;
  19.  
  20.     auto textureData = Array<D3D11_SUBRESOURCE_DATA>(description.Mips);
  21.  
  22.     Byte i = description.Mips;
  23.     while(i--)
  24.     {
  25.         textureData[i].pSysMem = mips[i].Data;
  26.         textureData[i].SysMemPitch = mips[i].Width;
  27.         textureData[i].SysMemSlicePitch = mips[i].Height;
  28.     }
  29.  
  30.     result = Handle::Window->Device->CreateTexture2D(&textureDesc, textureData.data(), &mTexture);
  31.  
  32.     D3D11_SHADER_RESOURCE_VIEW_DESC rvDesc;
  33.     ZeroMemory(&rvDesc, sizeof(rvDesc));
  34.     rvDesc.Format = textureDesc.Format;
  35.     rvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  36.     rvDesc.Texture2D.MostDetailedMip = 0;
  37.     rvDesc.Texture2D.MipLevels = description.Mips;
  38.  
  39.     result = Handle::Window->Device->CreateShaderResourceView(mTexture, &rvDesc, &mTextureRV);
  40.  
  41.     return 1;
  42. }
  43.  
  44. bool Texture::LoadDDS(String filename)
  45. {
  46.     File textureFile;
  47.     Texture::DDS::Attributes header;
  48.     HRESULT result;
  49.  
  50.     // Open the texture file
  51.     textureFile.Open(filename, File::Mode::Read);
  52.  
  53.     Handle::Log->Write("Importing DDS texture: " + filename);
  54.  
  55.     UInt identifier = textureFile.Read<UInt>();
  56.     header = *textureFile.Read<Texture::DDS::Attributes>(1);
  57.  
  58.     Texture::Description textureDesc;
  59.     textureDesc.Width = header.Width;
  60.     textureDesc.Height = header.Height;
  61.     textureDesc.Depth = header.Depth;
  62.     textureDesc.Mips = header.MipMapCount;
  63.     textureDesc.Flags = Texture::Flags::Texture2D | Texture::Flags::Immutable;
  64.     textureDesc.Format = Texture::Format::BGRA8UNorm;
  65.  
  66.     //Texture::Mip* textureMips = new Texture::Mip[textureDesc.Mips];
  67.     auto textureData = Array<Array<UByte>>(textureDesc.Mips);
  68.     auto textureMips = Array<Texture::Mip>(textureDesc.Mips);
  69.  
  70.     for(Byte i = 0; i < textureDesc.Mips; ++i)
  71.     {
  72.         UInt bw = max(textureDesc.Width >> i, 1);
  73.         UInt bh = max(textureDesc.Height >> i, 1);
  74.         UInt bb = Texture::FormatBitsPerPixel[(UByte)textureDesc.Format] / 8;
  75.  
  76.         UInt bytesToRead = bw * bh * Texture::FormatBitsPerPixel[(UByte)textureDesc.Format] / 8;
  77.  
  78.         textureData[i].reserve(bytesToRead);
  79.         textureFile.Read<UByte>(textureData[i].data(), bytesToRead);
  80.  
  81.         textureMips[i].Data = textureData[i].data();
  82.         textureMips[i].Width = bw * bb;
  83.         textureMips[i].Height = bh * bb;
  84.     }
  85.  
  86.     Texture::Create(textureDesc, textureMips);
  87.  
  88.     textureFile.Close();
  89.  
  90.     return 1;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement