Exemplo n.º 1
0
void MyD3DAssets::loadPSTexture(int textureIndex)
{
    ID3D11ShaderResourceView *view = nullptr;
    context->base->PSGetShaderResources(textureIndex, 1, &view);

    if (view == nullptr)
    {
        PSTexture.free();
    }
    else
    {
        ID3D11Resource *textureResource = nullptr;
        view->GetResource(&textureResource);

        ID3D11Texture2D* texture = nullptr;
        HRESULT hr = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&texture));
        if (texture == nullptr || FAILED(hr))
        {
            PSTexture.free();
            g_logger->logErrorFile << "textureResource->QueryInterface failed" << endl;
        }
        else
        {
            readTexture(texture, PSTexture);
            texture->Release();
        }

        textureResource->Release();
        view->Release();
    }
}
Exemplo n.º 2
0
XMFLOAT2 const ShaderResourceView::CalPicSize(std::wstring filePath) const
{
	ID3D11ShaderResourceView* pISpriteSRV = GetSRV(filePath);
	ID3D11Texture2D* pISpriteTex;
	float TextureWidth = 0.0f,TextureHeight = 0.0f;

	pISpriteSRV->GetResource( (ID3D11Resource**) &pISpriteTex);
	D3D11_TEXTURE2D_DESC SpriteTexDesc;
	pISpriteTex->GetDesc(&SpriteTexDesc);

	TextureWidth = static_cast<float>(SpriteTexDesc.Width);
	TextureHeight = static_cast<float>(SpriteTexDesc.Height);

	SAFE_RELEASE(pISpriteTex);

	return XMFLOAT2(TextureWidth,TextureHeight);
}
		virtual void Unload( void* source ){
			for (auto it = resources.begin(); it != resources.end(); it++) {
				if (it->second.texture == source) {
					it->second.referenceCount--;
					if (it->second.referenceCount <= 0) {
						if (g_RendererType == kUnityGfxRendererD3D11)
						{
							// ID3D11Texture2Dに戻してUnityにアンロードしてもらう
							ID3D11Resource* resourceDX11 = nullptr;
							ID3D11ShaderResourceView* resView = (ID3D11ShaderResourceView*)source;
							resView->GetResource(&resourceDX11);
							resView->Release();
						}
						unload( it->first.c_str() );
						resources.erase(it);
					}
				}
			}
		}
Exemplo n.º 4
0
HTEXTURE HGE_CALL HGE_Impl::Texture_Load(const char *filename, hgeU32 size, bool bMipmap)
{
	HRESULT hr;

	void *data;
	hgeU32 _size;
	ID3D11Resource* pRes;
	ID3D11Resource* pStagingRes;
	ID3D11Texture2D* pTex;
	ID3D11ShaderResourceView* pSRV;
	//D3DXIMAGE_INFO info;
	CTextureList *texItem;

	//load file to buffer
	if (size)
	{
		//If argument is already the buffer
		data = (void *)filename; _size = size;
	}
	else
	{
		data = pHGE->Resource_Load(filename, &_size);
		if (!data) return NULL;
	}

	//Get image info
	D3DX11_IMAGE_INFO imgInfo;
	hr = D3DX11GetImageInfoFromMemory(
		(LPCSTR)data,
		_size,
		NULL,
		&imgInfo,
		NULL);
	if (hr)
	{
		_PostError("Cannot get image info.\n");
		if (!size) Resource_Free(data);
		return NULL;
	}

	D3DX11_IMAGE_LOAD_INFO loadImgInfo;
	loadImgInfo.MipLevels = bMipmap ? 0 : 1;
	loadImgInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	loadImgInfo.Usage = D3D11_USAGE_STAGING;
	loadImgInfo.BindFlags = 0;// (autogen) ? (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) : (D3D11_BIND_SHADER_RESOURCE);
	loadImgInfo.CpuAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
	loadImgInfo.MiscFlags = 0;

	//Create Staging texture first
	hr = D3DX11CreateTextureFromMemory(
		m_pD3DDevice,
		(LPCVOID)data,
		_size,
		&loadImgInfo,
		NULL,
		&pStagingRes,
		0);
	if (hr)
	{
		_PostError("Can't create texture");
		if (!size) Resource_Free(data);
		return NULL;
	}

	//shouldn't access the file buffer anymore
	if (!size) Resource_Free(data);

	//Convert 0xFF00FFFF to transparent color
	D3D11_TEXTURE2D_DESC TDesc;
	((ID3D11Texture2D*)pStagingRes)->GetDesc(&TDesc);
	if (bUseTransparentColor)
	{
		DWORD* pLockPtr = (DWORD*)Texture_Lock((HTEXTURE)pStagingRes, false);

		int end = TDesc.Width * TDesc.Height;
		for (int i = 0; i < end; i++)
		{
			DWORD& currentPixel = pLockPtr[i];

			if (GETR(currentPixel) == 0xFF &&
				GETG(currentPixel) == 0x00 &&
				GETB(currentPixel) == 0xFF)
			{
				currentPixel = ARGB(0x00, 0x00, 0x00, 0x00);
			}

		}
		//Create default texture with data
		//from the staging texture
		TDesc.Usage = D3D11_USAGE_DEFAULT;
		TDesc.CPUAccessFlags = 0;
		TDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
		TDesc.MipLevels = bMipmap ? 0 : 1;

		D3D11_SUBRESOURCE_DATA sr;
		sr.pSysMem = pLockPtr;
		sr.SysMemPitch = TDesc.Width * 4;
		sr.SysMemSlicePitch = TDesc.Width * TDesc.Height * 4;

		hr = m_pD3DDevice->CreateTexture2D(&TDesc, &sr, &pTex);
		if (hr)
		{
			_PostError("Can't create texture");
			if (!size) Resource_Free(data);
			pStagingRes->Release();
			return NULL;
		}
		Texture_Unlock((HTEXTURE)pStagingRes);
		SAFE_RELEASE(pStagingRes);
	}

	D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
	memset(&SRVDesc, 0, sizeof(SRVDesc));
	SRVDesc.Format = TDesc.Format;
	SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	SRVDesc.Texture2D.MipLevels = bMipmap ? 0 : 1;

	hr = m_pD3DDevice->CreateShaderResourceView(pTex, &SRVDesc, &pSRV);
	if (hr)
	{
		_PostError("Can't create texture");
		if (!size) Resource_Free(data);
		return NULL;
	}

	pSRV->GetResource(&pRes);

	// 	hr = CreateWICTextureFromMemory(
	// 		pd3dDevice,
	// 		pd3dImmediateContext,
	// 		(uint8_t*)data,
	// 		_size,
	// 		&pRes,
	// 		&pSRV,
	// 		0);

	texItem = new CTextureList;
	texItem->tex = (HTEXTURE)pTex;
	texItem->m_pSRV = pSRV;
	texItem->width = TDesc.Width;
	texItem->height = TDesc.Height;
	texItem->next = textures;
	textures = texItem;
	return (HTEXTURE)texItem;
}