HRESULT GraphicsManager::CreateShaderResourceViewFromFile(ID3D11Device *device, ID3D11DeviceContext *dc, const wchar_t *filename, ID3D11ShaderResourceView **resourceView)
{
  HRESULT result = E_FAIL;

#ifdef OLD_DX_SDK
	//If not, then we have to load it!
	D3DX11_IMAGE_LOAD_INFO imageInfo;
	result = D3DX11CreateShaderResourceViewFromFile(device, filename, &imageInfo, NULL, resourceView, NULL);
#else
  ID3D11Texture2D *tex;
  result = CreateWICTextureFromFile(device, dc, filename, (ID3D11Resource **)&tex, resourceView);
  if (FAILED(result))
  {
    DirectX::TexMetadata md;
    DirectX::ScratchImage img;
    result = LoadFromDDSFile(filename, 0, &md, img);
    result = CreateShaderResourceView(device, img.GetImages(), img.GetImageCount(), md, resourceView);
  }
#endif

  if (FAILED(result))
  {
    printf("There was a problem loading \"%s\"\n", filename);
  }

	return result;
}
예제 #2
0
	//シェーダリソースビューの作成
	ComPtr<ID3D11ShaderResourceView> GameObject::CreateShaderResView(const wstring& TextureFileName){
		//テクスチャ作成
		DirectX::TexMetadata metadata;
		DirectX::ScratchImage image;
		ThrowIfFailed(
			DirectX::LoadFromWICFile(TextureFileName.c_str(), 0, &metadata, image),
			L"テクスチャの読み込みに失敗しました",
			TextureFileName,
			L"GameObject::CreateShaderResView()"
			);
		//デバイスとコンテキストインターフェイスの取得
		//デバイスの取得
		auto Dev = App::GetApp()->GetDeviceResources();
		ID3D11Device* pDx11Device = Dev->GetD3DDevice();
		ID3D11DeviceContext* pID3D11DeviceContex = Dev->GetD3DDeviceContext();
		//ミューテックス
		std::mutex Mutex;
		ComPtr<ID3D11ShaderResourceView> ResView;
		Util::DemandCreate(ResView, Mutex, [&](ID3D11ShaderResourceView** pResult) -> HRESULT
		{
			// 画像からシェーダリソースViewの作成
			return ThrowIfFailed(CreateShaderResourceView(pDx11Device, image.GetImages(), image.GetImageCount(), metadata, pResult),
				L"シェーダーリソースビューを作成できません",
				L"if( FAILED( CreateShaderResourceView() ) )",
				L"Texture::Impl::Impl()"
				);
		});
		return ResView;
	}
예제 #3
0
static ff::SpriteType UpdateSpriteType(const OptimizedSpriteInfo &sprite, const DirectX::ScratchImage &alphaTexture)
{
	const ff::SpriteData &data = sprite._sprite->GetSpriteData();
	if (data._type != ff::SpriteType::Unknown || alphaTexture.GetImageCount() == 0)
	{
		return data._type;
	}

	ff::RectInt rect = sprite._destRect;
	ff::SpriteType newType = ff::SpriteType::Opaque;
	const DirectX::Image &image = *alphaTexture.GetImages();

	for (int y = rect.top; y < rect.bottom && newType == ff::SpriteType::Opaque; y++)
	{
		const uint8_t *alpha = image.pixels + y * image.rowPitch + rect.left;
		for (int x = rect.left; x < rect.right; x++, alpha++)
		{
			if (*alpha && *alpha != 0xFF)
			{
				newType = ff::SpriteType::Transparent;
				break;
			}
		}
	}

	return newType;
}
예제 #4
0
static bool ConvertOptimizedTextures(
	DXGI_FORMAT format,
	size_t mipMapLevels,
	ff::Vector<ff::ComPtr<ff::IGraphTexture>> &textures,
	ff::Vector<ff::ComPtr<ff::IGraphTexture>> &finalTextures,
	ff::Vector<std::shared_ptr<DirectX::ScratchImage>> &alphaTextures)
{
	for (size_t i = 0; i < textures.Size(); i++)
	{
		ff::IGraphTexture *texture = textures[i];
		ff::IGraphDevice *device = texture->GetDevice();

		ff::ComPtr<ff::IGraphTexture> newTexture;
		assertRetVal(texture->Convert(format, mipMapLevels, &newTexture), false);
		finalTextures.Push(newTexture);

		DirectX::ScratchImage scratch;
		assertHrRetVal(DirectX::CaptureTexture(device->Get3d(), device->GetContext(), texture->GetTexture(), scratch), false);

		std::shared_ptr<DirectX::ScratchImage> alphaScratch = std::make_shared<DirectX::ScratchImage>();
		assertHrRetVal(DirectX::Convert(scratch.GetImages(), 1, scratch.GetMetadata(), DXGI_FORMAT_A8_UNORM, DirectX::TEX_FILTER_DEFAULT, 0, *alphaScratch), false);
		alphaTextures.Push(alphaScratch);
	}

	return true;
}
예제 #5
0
// -------------------------------------------------------------------------------------------------
bool TextureFactory::LoadResource(Resource* resource, const WCHAR * filename)
{

    if(!FileUtils::Exists(filename)) return false;
    HRESULT hr = S_OK;
    DirectX::TexMetadata metadata;
    DirectX::ScratchImage sourceScratch;
    hr = DXUtil::LoadTexture(filename,&metadata,sourceScratch);    
    if (Logger::IsFailureLog(hr, L"LoadTexture"))
    {
        return false;
    }

    ID3D11Texture2D* dxtex = NULL;
    std::wstring ext = FileUtils::GetExtensionLower(filename);
    // generate full mip chains for non dds file.
    if(ext != L".dds")
    {
        const DirectX::Image* srcImage = sourceScratch.GetImage(0, 0, 0);
        DirectX::ScratchImage mipScratch;
        hr = DirectX::GenerateMipMaps(*srcImage, DirectX::TEX_FILTER_LINEAR, 0, mipScratch, false);
        if (Logger::IsFailureLog(hr, L"DirectX::GenerateMipMaps"))
            return false;    

        hr = DirectX::CreateTexture(m_device, mipScratch.GetImages(), mipScratch.GetImageCount(), mipScratch.GetMetadata(),
                                    (ID3D11Resource**)&dxtex);
       

    }
    else
    {
        hr = DirectX::CreateTexture(m_device, sourceScratch.GetImages(), sourceScratch.GetImageCount(), metadata,(ID3D11Resource**)&dxtex);
    }

    if (Logger::IsFailureLog(hr, L"DirectX::CreateTexture"))
            return false;    
    
    ID3D11ShaderResourceView* texview = CreateTextureView(m_device, dxtex);
    if(!dxtex) return false;

    Texture* tex = (Texture*)resource;    
    tex->Set(dxtex,texview);    
    return true;
}
예제 #6
0
void SaveTextureToDDSFile(ID3D11Resource* tex, LPCWSTR filename)
{   // from http://directxtex.codeplex.com/wikipage?title=CaptureTexture 
    DirectX::ScratchImage image;

    HRESULT hr;
    hr = DirectX::CaptureTexture(d3dDevice, immediateContext, tex, image);
    if (FAILED(hr))
    {
        return;
    }
    hr = DirectX::SaveToDDSFile(image.GetImages(), image.GetImageCount(), image.GetMetadata(), DDS_FLAGS_NONE, filename);
    if (FAILED(hr))
    {
        return;
    }
}
예제 #7
0
void convertTexture(const std::wstring& source, const std::wstring& dest, TextureFormat sourceFormat, TextureFormat destFormat, bool alpha){
	DirectX::ScratchImage srcImg;
	DirectX::TexMetadata srcMetadata;
	switch (sourceFormat)
	{	
	case TextureFormat::Dds:
		DirectX::LoadFromDDSFile(source.c_str(), 0, &srcMetadata, srcImg);
		break;
	case TextureFormat::Tga:
		DirectX::LoadFromTGAFile(source.c_str(), &srcMetadata, srcImg);
		break;
	default:
		DirectX::LoadFromWICFile(source.c_str(), 0, &srcMetadata, srcImg);
		break;
	}

	switch (destFormat)
	{
	case TextureFormat::Jpg:
		DirectX::SaveToWICFile(*srcImg.GetImage(0, 0, 0), 0, DirectX::GetWICCodec(DirectX::WICCodecs::WIC_CODEC_JPEG), dest.c_str());
		break;
	case TextureFormat::Png:
		DirectX::SaveToWICFile(*srcImg.GetImage(0, 0, 0), 0, DirectX::GetWICCodec(DirectX::WICCodecs::WIC_CODEC_PNG), dest.c_str());
		break;
	case TextureFormat::Dds:
	{
		DirectX::ScratchImage imgWithMipMaps;
		auto hr = DirectX::GenerateMipMaps(*srcImg.GetImage(0, 0, 0), DirectX::TEX_FILTER_FLAGS::TEX_FILTER_CUBIC, 0, imgWithMipMaps);

		DirectX::ScratchImage imgInBC2;
		//hr = DirectX::Convert(imgWithMipMaps.GetImages(), imgWithMipMaps.GetImageCount(), srcMetadata, DXGI_FORMAT_BC2_UNORM, DirectX::TEX_FILTER_FLAGS::TEX_FILTER_CUBIC, 0.5f, imgInBC2);
		hr = DirectX::Compress(imgWithMipMaps.GetImages(), imgWithMipMaps.GetImageCount(), imgWithMipMaps.GetMetadata(), alpha ? DXGI_FORMAT_BC3_UNORM : DXGI_FORMAT_BC1_UNORM, DirectX::TEX_COMPRESS_FLAGS::TEX_COMPRESS_DEFAULT | DirectX::TEX_COMPRESS_PARALLEL, 0.5f, imgInBC2);
		
		hr = DirectX::SaveToDDSFile(imgInBC2.GetImages(), imgInBC2.GetImageCount(), imgInBC2.GetMetadata(), 0, dest.c_str());
	}
		break;
	default:
		break;
	}
}