Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Texture::Create(Texture::Description description, Array<Texture::Mip>& mips)
- {
- //D3D11_SUBRESOURCE_DATA* textureData;
- HRESULT result;
- D3D11_TEXTURE2D_DESC textureDesc;
- ZeroMemory(&textureDesc, sizeof(textureDesc));
- textureDesc.Width = description.Width;
- textureDesc.Height = description.Height;
- textureDesc.MipLevels = description.Mips;
- textureDesc.ArraySize = 1; //description.Depth;
- textureDesc.Format = (DXGI_FORMAT)description.Format;
- textureDesc.SampleDesc.Count = 1;
- textureDesc.SampleDesc.Quality = 0;
- textureDesc.Usage = (description.Flags & Texture::Flags::Immutable) ? D3D11_USAGE_IMMUTABLE : D3D11_USAGE_DEFAULT;
- textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
- textureDesc.CPUAccessFlags = 0;
- textureDesc.MiscFlags = 0;
- auto textureData = Array<D3D11_SUBRESOURCE_DATA>(description.Mips);
- Byte i = description.Mips;
- while(i--)
- {
- textureData[i].pSysMem = mips[i].Data;
- textureData[i].SysMemPitch = mips[i].Width;
- textureData[i].SysMemSlicePitch = mips[i].Height;
- }
- result = Handle::Window->Device->CreateTexture2D(&textureDesc, textureData.data(), &mTexture);
- D3D11_SHADER_RESOURCE_VIEW_DESC rvDesc;
- ZeroMemory(&rvDesc, sizeof(rvDesc));
- rvDesc.Format = textureDesc.Format;
- rvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
- rvDesc.Texture2D.MostDetailedMip = 0;
- rvDesc.Texture2D.MipLevels = description.Mips;
- result = Handle::Window->Device->CreateShaderResourceView(mTexture, &rvDesc, &mTextureRV);
- return 1;
- }
- bool Texture::LoadDDS(String filename)
- {
- File textureFile;
- Texture::DDS::Attributes header;
- HRESULT result;
- // Open the texture file
- textureFile.Open(filename, File::Mode::Read);
- Handle::Log->Write("Importing DDS texture: " + filename);
- UInt identifier = textureFile.Read<UInt>();
- header = *textureFile.Read<Texture::DDS::Attributes>(1);
- Texture::Description textureDesc;
- textureDesc.Width = header.Width;
- textureDesc.Height = header.Height;
- textureDesc.Depth = header.Depth;
- textureDesc.Mips = header.MipMapCount;
- textureDesc.Flags = Texture::Flags::Texture2D | Texture::Flags::Immutable;
- textureDesc.Format = Texture::Format::BGRA8UNorm;
- //Texture::Mip* textureMips = new Texture::Mip[textureDesc.Mips];
- auto textureData = Array<Array<UByte>>(textureDesc.Mips);
- auto textureMips = Array<Texture::Mip>(textureDesc.Mips);
- for(Byte i = 0; i < textureDesc.Mips; ++i)
- {
- UInt bw = max(textureDesc.Width >> i, 1);
- UInt bh = max(textureDesc.Height >> i, 1);
- UInt bb = Texture::FormatBitsPerPixel[(UByte)textureDesc.Format] / 8;
- UInt bytesToRead = bw * bh * Texture::FormatBitsPerPixel[(UByte)textureDesc.Format] / 8;
- textureData[i].reserve(bytesToRead);
- textureFile.Read<UByte>(textureData[i].data(), bytesToRead);
- textureMips[i].Data = textureData[i].data();
- textureMips[i].Width = bw * bb;
- textureMips[i].Height = bh * bb;
- }
- Texture::Create(textureDesc, textureMips);
- textureFile.Close();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement