コード例 #1
0
// interface functions
void MFTexture_CreatePlatformSpecific(MFTexture *pTexture, bool generateMipChain)
{
	MFCALLSTACK;

	HRESULT hr;
	MFTextureTemplateData *pTemplate = pTexture->pTemplateData;
	pTexture->pInternalData = NULL;

	// create texture
	DXGI_FORMAT platformFormat = (DXGI_FORMAT)MFTexture_GetPlatformFormatID(pTemplate->imageFormat, MFDD_D3D11);
	//hr = D3DX11CreateTextureFromMemory(pd3dDevice, pTemplate->pSurfaces[0].width, pTemplate->pSurfaces[0].height, generateMipChain ? 0 : 1, 0, platformFormat, D3DPOOL_MANAGED, (IDirect3DTexture9**)&pTexture->pInternalData);

	int pitch = (MFTexture_GetBitsPerPixel(pTemplate->imageFormat) / 8) * pTemplate->pSurfaces[0].width;

	D3D11_TEXTURE2D_DESC desc;
	MFZeroMemory(&desc, sizeof(desc));
	desc.Width = pTemplate->pSurfaces[0].width;
	desc.Height = pTemplate->pSurfaces[0].height;
	desc.MipLevels = 1; //generateMipChain ? 0 : 1;
	desc.ArraySize = 1;
	desc.Format = platformFormat;
	desc.SampleDesc.Count = 1;
	desc.SampleDesc.Quality = 0;
	desc.Usage = D3D11_USAGE_DEFAULT; // = D3D11_USAGE_IMMUTABLE;
	desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
	desc.CPUAccessFlags = 0;
	desc.MiscFlags = generateMipChain ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0;

	D3D11_SUBRESOURCE_DATA data[16];
	MFZeroMemory(&data, sizeof(data));
	data[0].pSysMem = pTemplate->pSurfaces[0].pImageData;
	data[0].SysMemPitch = pitch;

	ID3D11Texture2D* pTex = NULL;

	hr = g_pd3dDevice->CreateTexture2D(&desc, data, &pTex);

	//MFDebug_Assert(hr != D3DERR_NOTAVAILABLE, MFStr("LoadTexture failed: D3DERR_NOTAVAILABLE, 0x%08X", hr));
	//MFDebug_Assert(hr != D3DERR_OUTOFVIDEOMEMORY, MFStr("LoadTexture failed: D3DERR_OUTOFVIDEOMEMORY, 0x%08X", hr));
	//MFDebug_Assert(hr != D3DERR_INVALIDCALL, MFStr("LoadTexture failed: D3DERR_INVALIDCALL, 0x%08X", hr));
	//MFDebug_Assert(hr != D3DXERR_INVALIDDATA, MFStr("LoadTexture failed: D3DXERR_INVALIDDATA, 0x%08X", hr));
	
	MFDebug_Assert(hr == S_OK, MFStr("Failed to create texture '%s'.", pTexture->name));

	if (SUCCEEDED(hr))
	{	
		MFRenderer_D3D11_SetDebugName(pTex, pTexture->name);

		//// filter mip levels
		if (generateMipChain)
			D3DX11FilterTexture(NULL, pTex, 0, D3DX11_FILTER_BOX);

		ID3D11ShaderResourceView *pSRV = NULL;

		D3D11_SHADER_RESOURCE_VIEW_DESC desc;
		MFZeroMemory(&desc, sizeof(desc));
		desc.Format = platformFormat;
		desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
		desc.Texture2D.MostDetailedMip = 0;
		desc.Texture2D.MipLevels = (uint32)-1;

		hr = g_pd3dDevice->CreateShaderResourceView(pTex, &desc, &pSRV);
		pTex->Release();

		pTexture->pInternalData = pSRV;
	}
}
コード例 #2
0
ファイル: D3D11Texture.cpp プロジェクト: mavaL/NeoEngine
	//------------------------------------------------------------------------------------
	void D3D11Texture::_CreateManual(const char* pTexData)
	{
		HRESULT hr = S_OK;
		const DXGI_FORMAT dxformat = ConvertToDXFormat(m_texFormat);
		const DWORD bytesPerPixel = GetBytesPerPixelFromFormat(m_texFormat);

		DWORD pitch = bytesPerPixel * m_width;
		char* tmpBuf = (char*)pTexData;

		if (!pTexData)
		{
			tmpBuf = new char[pitch * m_height];
			ZeroMemory(tmpBuf, pitch * m_height * sizeof(char));
		}

		D3D11_SUBRESOURCE_DATA subData;
		D3D11_SUBRESOURCE_DATA* subDataArray = nullptr;
		subData.pSysMem = tmpBuf;
		subData.SysMemPitch = pitch;

		CD3D11_TEXTURE2D_DESC desc(dxformat, m_width, m_height);

		if (m_bMipMap)
		{
			int wid = m_width, hei = m_height;
			// Get mipmap level
			desc.MipLevels = 1;
			while((wid > 1) || (hei > 1))
			{
				wid = Max(wid / 2, 1);
				hei = Max(hei / 2, 1);
				++desc.MipLevels;
			}

			// Not correct mipmap data, just make room for later D3DX11FilterTexture
			subDataArray = new D3D11_SUBRESOURCE_DATA[desc.MipLevels];
			for (size_t i=0; i<desc.MipLevels; ++i)
			{
				subDataArray[i].pSysMem = tmpBuf;
				subDataArray[i].SysMemPitch = pitch;
			}
		}
		else
		{
			desc.MipLevels = 1;
			subDataArray = &subData;
		}

		// Validate usage
		_AST(!((m_usage&eTextureUsage_WriteOnly)&&(m_usage&eTextureUsage_ReadWrite)) && "Invalid usage!");

		if (m_usage & eTextureUsage_RenderTarget)
		{
			desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;

			if (m_usage & eTextureUsage_AutoGenMips)
			{
				desc.MiscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
			}
		}

		if (GetTextureType() == eTextureType_CubeMap)
		{
			desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
			desc.ArraySize = 6;
		}

		// Assign usage
		if (m_usage & eTextureUsage_WriteOnly)
		{
			desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
			desc.Usage = D3D11_USAGE_DYNAMIC;
		}
		else if (m_usage & eTextureUsage_ReadWrite)
		{
			D3D11_TEXTURE2D_DESC descStaging = desc;
			descStaging.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
			descStaging.Usage = D3D11_USAGE_STAGING;
			descStaging.BindFlags = 0;

			g_pRenderSys->GetDevice()->CreateTexture2D(&descStaging, subDataArray, &m_pTexStaging);
		}

		if (m_usage & eTextureUsage_Depth)
		{
			desc.Usage			= D3D11_USAGE_DEFAULT;
			desc.BindFlags		= D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;

			g_pRenderSys->GetDevice()->CreateTexture2D(&desc, nullptr, &m_pTexture2D);
		}
		else
		{
			g_pRenderSys->GetDevice()->CreateTexture2D(&desc, subDataArray, &m_pTexture2D);
		}

		if(!pTexData)
		{
			SAFE_DELETE_ARRAY(tmpBuf);
		}

		_AST(SUCCEEDED(hr) && "Create texture failed!");

		// Generate mipmap levels
		if (m_bMipMap && !(m_usage & eTextureUsage_RenderTarget))
		{
			V(D3DX11FilterTexture(g_pRenderSys->GetDeviceContext(), m_pTexture2D, 0, D3DX11_DEFAULT));
		}

		// Create SRV
		CreateSRV();

		// Create DSV
		if (m_usage & eTextureUsage_Depth)
		{
			CreateDSV();
		}

		// Create RTV
		if (m_usage & eTextureUsage_RenderTarget)
		{
			CreateRTV();
		}
	}
コード例 #3
0
HRESULT MyTexture::InitResource(  ID3D11Device* pd3dDevice, const char* filename )
{
	HRESULT hr = S_OK;
	FILE* pInput = NULL;
	fopen_s( &pInput, filename, "rb");
	if( pInput == NULL )
	{
		MessageBoxA( NULL, "Can not load Texture file ! ", filename, MB_OK );
		return S_FALSE;
	}

	DWORD width = 0;
	DWORD length = 0;
	DWORD format = 0;
	fread( &width, sizeof(width), 1, pInput );
	fread( &length, sizeof( length), 1, pInput );
	fread( &format, sizeof( format), 1, pInput );

	assert( width != 0 );
	assert( length != 0 );
	assert( format == 4 );

	m_width = width;
	m_height = length;

	int    pixelCounts = width*length ;
	float* pValues = new float[ 4*pixelCounts ];
	fread( pValues, sizeof(float),4*pixelCounts, pInput );

	D3D11_TEXTURE2D_DESC texDes;
	ZeroMemory( &texDes, sizeof(texDes) );
	texDes.Width = width;
	texDes.Height = length;
	texDes.MipLevels = 0;
	texDes.ArraySize = 1;
	texDes.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	texDes.SampleDesc.Count = 1;
	texDes.SampleDesc.Quality = 0;
	texDes.Usage = D3D11_USAGE_DEFAULT;
	texDes.BindFlags = D3D11_BIND_SHADER_RESOURCE ;
	texDes.CPUAccessFlags = 0;
	texDes.MiscFlags = 0 ;

	ID3D11Texture2D* pTexture2D = NULL;
	D3D11_SUBRESOURCE_DATA data;
	data.pSysMem = pValues;
	data.SysMemPitch =  4*sizeof( float )*width;
	data.SysMemSlicePitch = 0;
	hr = pd3dDevice->CreateTexture2D( &texDes, NULL, &pTexture2D );
	V_RETURN(hr);

	//auto gener mipmaps
	ID3D11DeviceContext* pd3dContext = DXUTGetD3D11DeviceContext();
	pd3dContext->UpdateSubresource( pTexture2D, 0, NULL, pValues, data.SysMemPitch, 0 );
	D3DX11FilterTexture( pd3dContext, pTexture2D, 0, D3DX11_FILTER_BOX );

	delete [] pValues;
	fclose( pInput);

	pTexture2D->GetDesc(&texDes);
	D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
	srvDesc.Format = texDes.Format;
	srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	srvDesc.Texture2D.MipLevels = texDes.MipLevels;
	srvDesc.Texture2D.MostDetailedMip = 0;

	hr = pd3dDevice->CreateShaderResourceView( pTexture2D, &srvDesc, &m_pSRV  );
	V_RETURN(hr);
	SAFE_RELEASE( pTexture2D );

	return hr;
}