int TextureResourcer::createTextureFromWIC(ID3D11Device* device, ID3D11DeviceContext* deviceContext, const uint8_t * binaryData, int binaryLength, TextureHandle* texHandle)
{
    ID3D11Resource* texture = nullptr;
    ID3D11ShaderResourceView* textureView = nullptr;
    HRESULT hr = CreateWICTextureFromMemory(device, deviceContext, binaryData ,binaryLength, &texture, &textureView);
    //HRESULT hr = DirectX::CreateDDSTextureFromFile( device, , nullptr, &textureView );

    

    D3D11_SHADER_RESOURCE_VIEW_DESC desc;
    textureView->GetDesc(&desc);
    

    if(hr)
        return hr;

    //generate id (which is really an array index)
    int index = generateID();
    
    //get size
    unsigned int width;
    unsigned int height;
    GetTextureSizeD3D(texture, &width, &height);

    //ensure sizing in textures storage
    if(textures.size() <= (unsigned int)index)
        textures.resize(index+1);

    //add to storage
    TextureData& data = textures[index];
    data.ID = index;
    data.name = "";
    data.width = width;
    data.height = height;
    data.textureResource = texture;
    data.textureView = textureView;

    TextureHandle hTex = {index};
    *texHandle = hTex;

    return hr;
}
Example #2
0
 // ----------------------------------------------------------------------------------------------  
 Texture* TextureLib::LoadEmbeddedTexture(ID3D11Device* device, const wchar_t* name)
 {
     assert(s_Inst != NULL);
     Imple* pImple = s_Inst->m_pImple;

     typedef std::pair<std::wstring, Texture*> NameTexPair;
     const wchar_t* resType = L"Texture";
     HRESULT hr = E_FAIL;

     Texture* tex = NULL;
     ID3D11Resource* dxresource = NULL;
     ID3D11ShaderResourceView* dxTexView = NULL;
     uint32_t  resSize = 0;
     uint8_t* data = (uint8_t*)ResUtil::LoadResource(resType, name, &resSize);
     hr = CreateWICTextureFromMemory(device,
         NULL,
         data,
         resSize,
         &dxresource,
         &dxTexView);
     free(data);
     if (!Logger::IsFailureLog(hr, L"Error loading %s\n", name))
     {
         D3D11_RESOURCE_DIMENSION resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
         dxresource->GetType(&resDim);
         assert(resDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D);
         ID3D11Texture2D* dxTex = NULL;
         hr = dxresource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&dxTex);
         dxresource->Release();
         assert(dxTex);
         tex = new Texture(dxTex, dxTexView);
         auto insertResult = pImple->m_textures.insert(NameTexPair(name, tex));
         assert(insertResult.second);
     }
     return tex;
 }
Example #3
0
void TextureLib::InitInstance(ID3D11Device* device)
{
    s_Inst = new TextureLib();
    Imple* pImple = s_Inst->m_pImple;
    
    // color format ABGR
    pImple->m_defaultTextures[TextureType::DIFFUSE] = CreateCheckerboardTexture2D(device, 128, 128, 0xFF404040, 0xFF808080);
    pImple->m_defaultTextures[TextureType::Cubemap] = CreateCheckerboardTexture2D(device, 128, 128, 0xff000040, 0xff000080, true);

    pImple->m_defaultTextures[TextureType::NORMAL] = CreateSolidTexture2D(device, 8, 8, 0xFFFF8080);
    pImple->m_defaultTextures[TextureType::LIGHT] = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);
    pImple->m_defaultTextures[TextureType::SPEC] = CreateSolidTexture2D(device, 8, 8, 0xFF000000);    
    pImple->m_defaultTextures[TextureType::BlankMask] = CreateSolidTexture2D(device, 4, 4, 0x00);
    pImple->m_defaultTextures[TextureType::FullMask] = CreateSolidTexture2D(device, 4, 4, 0xFFFFFFFF);
    
    pImple->m_whiteTexture = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);

    typedef std::pair<std::wstring, Texture*> NameTexPair;


    uint32_t resCount = ResUtil::ResourceCount();
    for(uint32_t i = 0; i < resCount; i++)
    {        
        const wchar_t* resName = ResUtil::GetResourceName(i);
        const std::wstring ext = FileUtils::GetExtensionLower(resName);
        HRESULT hr = E_FAIL;

        Texture* tex = NULL;
        ID3D11Resource* dxresource = NULL;
        ID3D11ShaderResourceView* dxTexView = NULL;
        uint32_t  resSize = 0;

        if(ext == L".dds")
        {            
            uint8_t* data = ResUtil::LoadResource(resName,&resSize);
            if(resSize == 0) continue;
            
            hr = CreateDDSTextureFromMemory( device,
                                             data,
                                             resSize,
                                             &dxresource,
                                             &dxTexView);   
            free(data);
        }
        else if(ext == L".png" || ext == L".bmp" || ext == L".jpeg")
        {
            uint8_t* data = ResUtil::LoadResource(resName,&resSize);
            if(resSize == 0) continue;
            
            hr = CreateWICTextureFromMemory( device,
                                             NULL,
                                             data,
                                             resSize,
                                             &dxresource,
                                             &dxTexView);
            free(data);

        }

        if (Logger::IsFailureLog(hr, L"Error loading %s\n", resName))
        {
            continue;
        }

        D3D11_RESOURCE_DIMENSION resType = D3D11_RESOURCE_DIMENSION_UNKNOWN;
        dxresource->GetType( &resType );
        assert( resType == D3D11_RESOURCE_DIMENSION_TEXTURE2D);
        ID3D11Texture2D* dxTex = NULL;
        hr = dxresource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &dxTex );
        dxresource->Release();
        assert(dxTex);
        tex = new Texture(dxTex,dxTexView);
        auto insertResult = pImple->m_textures.insert(NameTexPair(resName,tex));
        assert(insertResult.second);            
    }
}
Example #4
0
void TextureLib::InitInstance(ID3D11Device* device)
{
    s_Inst = new TextureLib();
    Imple* pImple = s_Inst->m_pImple;
    
    
    pImple->m_defaultTextures[TextureType::DIFFUSE] = CreateCheckerboardTexture2D(device, 128, 128, 0xFF404040, 0xFF808080, false,true);
    pImple->m_defaultTextures[TextureType::DIFFUSE]->SetTextureType(TextureType::DIFFUSE);

    pImple->m_defaultTextures[TextureType::Cubemap] = CreateCheckerboardTexture2D(device, 128, 128, 0xff000040, 0xff000080, true);
    pImple->m_defaultTextures[TextureType::Cubemap]->SetTextureType(TextureType::Cubemap);

    pImple->m_defaultTextures[TextureType::NORMAL] = CreateSolidTexture2D(device, 8, 8, 0xFFFF8080);
    pImple->m_defaultTextures[TextureType::NORMAL]->SetTextureType(TextureType::NORMAL);

    pImple->m_defaultTextures[TextureType::LIGHT] = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);
    pImple->m_defaultTextures[TextureType::LIGHT]->SetTextureType(TextureType::LIGHT);

    pImple->m_defaultTextures[TextureType::SPEC] = CreateSolidTexture2D(device, 8, 8, 0xFF000000);    
    pImple->m_defaultTextures[TextureType::SPEC]->SetTextureType(TextureType::SPEC);

    pImple->m_defaultTextures[TextureType::BlankMask] = CreateSolidTexture2D(device, 4, 4, 0x00);
    pImple->m_defaultTextures[TextureType::BlankMask]->SetTextureType(TextureType::BlankMask);

    pImple->m_defaultTextures[TextureType::FullMask] = CreateSolidTexture2D(device, 4, 4, 0xFFFFFFFF);
    pImple->m_defaultTextures[TextureType::FullMask]->SetTextureType(TextureType::FullMask);
    
    pImple->m_whiteTexture = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF);

    typedef std::pair<std::wstring, Texture*> NameTexPair;

    const wchar_t* resName = L"Light.png";
    const wchar_t* resType = L"Texture";
    HRESULT hr = E_FAIL;

    Texture* tex = NULL;
    ID3D11Resource* dxresource = NULL;
    ID3D11ShaderResourceView* dxTexView = NULL;
    uint32_t  resSize = 0;
    uint8_t* data = (uint8_t*)ResUtil::LoadResource(resType, resName,&resSize);
    hr = CreateWICTextureFromMemory( device,
                                             NULL,
                                             data,
                                             resSize,
                                             &dxresource,
                                             &dxTexView);
   free(data);
   if (!Logger::IsFailureLog(hr, L"Error loading %s\n", resName))
   {
       D3D11_RESOURCE_DIMENSION resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
       dxresource->GetType( &resDim );
       assert( resDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D);
       ID3D11Texture2D* dxTex = NULL;
       hr = dxresource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &dxTex );
       dxresource->Release();
       assert(dxTex);
       tex = new Texture(dxTex,dxTexView);
       auto insertResult = pImple->m_textures.insert(NameTexPair(resName,tex));
       assert(insertResult.second);
   }
}
bool BitmapFontClass::InitializeTextureView(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* textureFilename)
{
	HRESULT result;

	// Texture View
	D3D11_TEXTURE2D_DESC texture2DDesc;
	D3D11_SHADER_RESOURCE_VIEW_DESC shaderDesc;

	int fileNameLength = strlen(textureFilename);
	string fileExtenstion;
	string strFileName(textureFilename);

	for (int i = fileNameLength - 3; i < fileNameLength; i++)
	{
		fileExtenstion += strFileName.at(i);
	}

	if (fileExtenstion != "tga" && fileExtenstion != "dds")
	{
		HRESULT hResult;
		textureFilename = "Resource Files/font_bitmap.png";
		std::basic_ifstream<unsigned char> file(textureFilename, std::ios::binary);

		if (file.is_open())
		{
			file.seekg(0, ios::end);
			int length = (int)file.tellg();
			file.seekg(0, ios::beg);

			unsigned char* buffer = new unsigned char[length];
			file.read(&buffer[0], length);
			file.close();

			hResult = CreateWICTextureFromMemory(device, deviceContext, &buffer[0], (size_t)length, NULL, &m_textureView, NULL);
			if (FAILED(hResult))
			{
				MessageBox(0, L"Failed to create texture from memory!", L"Compile Error", MB_OK);
				return false;
			}
		}
		else
		{
			MessageBox(0, L"Failed to open texture file!", L"Compile Error", MB_OK);
			return false;
		}
	}
	else if (fileExtenstion == "dds")
	{
		HRESULT hResult;
		WCHAR* tempFilePath;
		int nCharsVertex = MultiByteToWideChar(CP_ACP, 0, textureFilename, -1, NULL, 0);
		tempFilePath = new WCHAR[nCharsVertex];
		MultiByteToWideChar(CP_ACP, 0, textureFilename, -1, tempFilePath, nCharsVertex);

		hResult = CreateWICTextureFromFile(device, tempFilePath, NULL, &m_textureView, NULL);
		if (FAILED(hResult))
		{
			MessageBox(0, L"Failed to load the texture image!", L"Compile Error", MB_OK);
			delete tempFilePath;
			return false;
		}
		delete tempFilePath;
	}
	else
	{
		int height, width;
		unsigned int rowPitch;

		// Load the targa image data into memory.
		result = LoadTarga(textureFilename, height, width);
		if (FAILED(result))
		{
			if (m_targaData)
			{
				delete[] m_targaData;
				m_targaData = NULL;
			}
			MessageBox(0, L"Failed to load Targa image data into memory!", L"Compile Error", MB_OK);
			return false;
		}

		// Setup the description of the texture.
		texture2DDesc.Height = height;
		texture2DDesc.Width = width;
		texture2DDesc.MipLevels = 0;
		texture2DDesc.ArraySize = 1;
		texture2DDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
		texture2DDesc.SampleDesc.Count = 1;
		texture2DDesc.SampleDesc.Quality = 0;
		texture2DDesc.Usage = D3D11_USAGE_DEFAULT;
		texture2DDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
		texture2DDesc.CPUAccessFlags = 0;
		texture2DDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;

		// Create the empty texture.
		result = device->CreateTexture2D(&texture2DDesc, NULL, &m_texture);
		if (FAILED(result))
		{
			delete[] m_targaData;
			m_targaData = NULL;
			MessageBox(0, L"Failed to create empty Targa texture!", L"Compile Error", MB_OK);
			return false;
		}

		// Set the row pitch of the targa image data.
		rowPitch = (width * 4) * sizeof(unsigned char);

		// Copy the targa image data into the texture.
		deviceContext->UpdateSubresource(m_texture, 0, NULL, m_targaData, rowPitch, 0);

		// Setup the shader resource view description.
		shaderDesc.Format = texture2DDesc.Format;
		shaderDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
		shaderDesc.Texture2D.MostDetailedMip = 0;
		shaderDesc.Texture2D.MipLevels = -1;

		// Create the shader resource view for the texture.
		result = device->CreateShaderResourceView(m_texture, &shaderDesc, &m_textureView);
		if (FAILED(result))
		{
			delete[] m_targaData;
			m_targaData = NULL;
			MessageBox(0, L"Failed to create shader resource view for Targa texture", L"Compile Error", MB_OK);
			return false;
		}
	}
	deviceContext->GenerateMips(m_textureView);

	if (m_targaData)
	{
		delete[] m_targaData;
		m_targaData = NULL;
	}

	// Sample State
	D3D11_SAMPLER_DESC sampleStateDesc;
	ZeroMemory(&sampleStateDesc, sizeof(sampleStateDesc));
	sampleStateDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	sampleStateDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	sampleStateDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	sampleStateDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
	sampleStateDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
	sampleStateDesc.MaxLOD = D3D11_FLOAT32_MAX;

	result = device->CreateSamplerState(&sampleStateDesc, &m_samplerState);
	if (FAILED(result))
	{
		MessageBox(0, L"Failed to create sampler state!", L"Compile Error", MB_OK);
		return false;
	}

	return true;
}