| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../../Precompiled.h"
- #include "../../Core/Profiler.h"
- #include "../../Graphics/Graphics.h"
- #include "../../Graphics/GraphicsImpl.h"
- #include "../../Graphics/Material.h"
- #include "../../IO/FileSystem.h"
- #include "../../IO/Log.h"
- #include "../../Resource/ResourceCache.h"
- #include "../../Resource/XMLFile.h"
- #include "../../DebugNew.h"
- namespace Atomic
- {
- static const D3D11_FILTER d3dFilterMode[] =
- {
- D3D11_FILTER_MIN_MAG_MIP_POINT,
- D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT,
- D3D11_FILTER_MIN_MAG_MIP_LINEAR,
- D3D11_FILTER_ANISOTROPIC,
- D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR,
- D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT,
- D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT,
- D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR,
- D3D11_FILTER_COMPARISON_ANISOTROPIC,
- D3D11_FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR
- };
- static const D3D11_TEXTURE_ADDRESS_MODE d3dAddressMode[] =
- {
- D3D11_TEXTURE_ADDRESS_WRAP,
- D3D11_TEXTURE_ADDRESS_MIRROR,
- D3D11_TEXTURE_ADDRESS_CLAMP,
- D3D11_TEXTURE_ADDRESS_BORDER
- };
- void Texture::SetSRGB(bool enable)
- {
- if (graphics_)
- enable &= graphics_->GetSRGBSupport();
- if (enable != sRGB_)
- {
- sRGB_ = enable;
- // If texture had already been created, must recreate it to set the sRGB texture format
- if (object_.name_)
- Create();
- }
- }
- bool Texture::GetParametersDirty() const
- {
- return parametersDirty_ || !sampler_;
- }
- bool Texture::IsCompressed() const
- {
- return format_ == DXGI_FORMAT_BC1_UNORM || format_ == DXGI_FORMAT_BC2_UNORM || format_ == DXGI_FORMAT_BC3_UNORM;
- }
- unsigned Texture::GetRowDataSize(int width) const
- {
- switch (format_)
- {
- case DXGI_FORMAT_R8_UNORM:
- case DXGI_FORMAT_A8_UNORM:
- return (unsigned)width;
- case DXGI_FORMAT_R8G8_UNORM:
- case DXGI_FORMAT_R16_UNORM:
- case DXGI_FORMAT_R16_FLOAT:
- case DXGI_FORMAT_R16_TYPELESS:
- return (unsigned)(width * 2);
- case DXGI_FORMAT_R8G8B8A8_UNORM:
- case DXGI_FORMAT_R16G16_UNORM:
- case DXGI_FORMAT_R16G16_FLOAT:
- case DXGI_FORMAT_R32_FLOAT:
- case DXGI_FORMAT_R24G8_TYPELESS:
- case DXGI_FORMAT_R32_TYPELESS:
- return (unsigned)(width * 4);
- case DXGI_FORMAT_R16G16B16A16_UNORM:
- case DXGI_FORMAT_R16G16B16A16_FLOAT:
- return (unsigned)(width * 8);
- case DXGI_FORMAT_R32G32B32A32_FLOAT:
- return (unsigned)(width * 16);
- case DXGI_FORMAT_BC1_UNORM:
- return (unsigned)(((width + 3) >> 2) * 8);
- case DXGI_FORMAT_BC2_UNORM:
- case DXGI_FORMAT_BC3_UNORM:
- return (unsigned)(((width + 3) >> 2) * 16);
- default:
- return 0;
- }
- }
- void Texture::UpdateParameters()
- {
- if ((!parametersDirty_ && sampler_) || !object_.ptr_)
- return;
- // Release old sampler
- ATOMIC_SAFE_RELEASE(sampler_);
- D3D11_SAMPLER_DESC samplerDesc;
- memset(&samplerDesc, 0, sizeof samplerDesc);
- unsigned filterModeIndex = filterMode_ != FILTER_DEFAULT ? filterMode_ : graphics_->GetDefaultTextureFilterMode();
- if (shadowCompare_)
- filterModeIndex += 5;
- samplerDesc.Filter = d3dFilterMode[filterModeIndex];
- samplerDesc.AddressU = d3dAddressMode[addressMode_[0]];
- samplerDesc.AddressV = d3dAddressMode[addressMode_[1]];
- samplerDesc.AddressW = d3dAddressMode[addressMode_[2]];
- samplerDesc.MaxAnisotropy = anisotropy_ ? anisotropy_ : graphics_->GetDefaultTextureAnisotropy();
- samplerDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL;
- samplerDesc.MinLOD = -M_INFINITY;
- samplerDesc.MaxLOD = M_INFINITY;
- memcpy(&samplerDesc.BorderColor, borderColor_.Data(), 4 * sizeof(float));
- HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateSamplerState(&samplerDesc, (ID3D11SamplerState**)&sampler_);
- if (FAILED(hr))
- {
- ATOMIC_SAFE_RELEASE(sampler_);
- ATOMIC_LOGD3DERROR("Failed to create sampler state", hr);
- }
- parametersDirty_ = false;
- }
- unsigned Texture::GetSRVFormat(unsigned format)
- {
- if (format == DXGI_FORMAT_R24G8_TYPELESS)
- return DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
- else if (format == DXGI_FORMAT_R16_TYPELESS)
- return DXGI_FORMAT_R16_UNORM;
- else if (format == DXGI_FORMAT_R32_TYPELESS)
- return DXGI_FORMAT_R32_FLOAT;
- else
- return format;
- }
- unsigned Texture::GetDSVFormat(unsigned format)
- {
- if (format == DXGI_FORMAT_R24G8_TYPELESS)
- return DXGI_FORMAT_D24_UNORM_S8_UINT;
- else if (format == DXGI_FORMAT_R16_TYPELESS)
- return DXGI_FORMAT_D16_UNORM;
- else if (format == DXGI_FORMAT_R32_TYPELESS)
- return DXGI_FORMAT_D32_FLOAT;
- else
- return format;
- }
- unsigned Texture::GetSRGBFormat(unsigned format)
- {
- if (format == DXGI_FORMAT_R8G8B8A8_UNORM)
- return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
- else if (format == DXGI_FORMAT_BC1_UNORM)
- return DXGI_FORMAT_BC1_UNORM_SRGB;
- else if (format == DXGI_FORMAT_BC2_UNORM)
- return DXGI_FORMAT_BC2_UNORM_SRGB;
- else if (format == DXGI_FORMAT_BC3_UNORM)
- return DXGI_FORMAT_BC3_UNORM_SRGB;
- else
- return format;
- }
- void Texture::RegenerateLevels()
- {
- if (!shaderResourceView_)
- return;
- graphics_->GetImpl()->GetDeviceContext()->GenerateMips((ID3D11ShaderResourceView*)shaderResourceView_);
- levelsDirty_ = false;
- }
- // ATOMIC BEGIN
- // Satisfy script binding link
- unsigned Texture::GetExternalFormat(unsigned format)
- {
- return 0;
- }
- unsigned Texture::GetDataType(unsigned format)
- {
- return 0;
- }
- // ATOMIC END
- }
|