Пример #1
0
//===============================================================================================================================
void TextureManager::WriteToFile(BetterString filename, ID3D11ShaderResourceView* textureSRV)
{
    string fileExt = CGlobal::GetFileExt(filename);

    if (fileExt == "dds")
    {
        unique_ptr<wchar_t> name = filename.ToWideStr();
        WriteDDSToFile(name.get(), textureSRV);
    }
    else if (fileExt == "tga")
    {
        unique_ptr<wchar_t> name = filename.ToWideStr();
        WriteTGAToFile(name.get(), textureSRV);
    }
    else if (fileExt == "png")
    {
        unique_ptr<wchar_t> name = filename.ToWideStr();
        WritePNGToFile(name.get(), textureSRV);
    }
}
Пример #2
0
//=================================================================================================================
HRESULT ShaderCompiler::Compile(D3D* d3d, BetterString filename, BetterString shaderFuncName, int type, BetterString macroName, bool loadPrecompiledShader)
{
	// Compiling Shaders with D3DCompileFromFile
	//Google: visual studio 2013 hlsl shader compiling
	//http://msdn.microsoft.com/en-us/library/windows/desktop/hh968107(v=vs.85).aspx
	//http://stackoverflow.com/questions/10759300/how-do-i-use-shader-model-5-0-compiled-shaders-in-visual-studio-11
	/*
		Here "PixelShader.cso" is the precompiled hlsl shader generated by Visual Studio 11 from a .hlsl file in the project.
		The compiled .cso file is usually moved to the Projects/ProjectName/Debug folder by default.
		As a result it must be cut and paste into the same directory as your source code before using.
		Mind you this setting can be changed by right-clicking the HLSL file while inside Visual Studio 11 and
		editing the Output Settings. By default its: $(OutDir)%(Filename).cso, change it to: $(Directory)%(Filename).cso
	*/
	
	//Google: creating a cso file in DirectX11.1
	//http://stackoverflow.com/questions/5020204/loading-a-precompiled-hlsl-shader-into-memory-for-use-with-createpixelshader
	
	BetterString base = "..\\Framework\\Shaders\\";
	filename = base + filename;

	unique_ptr<wchar_t> name = filename.ToWideStr();

	HRESULT hr;
	ID3DBlob* blob = nullptr;
	ID3DBlob* errorBlob = nullptr;
	
    UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    flags |= D3DCOMPILE_DEBUG;
#endif

	ShaderMacros* macros = 0;
	map<string, ShaderMacros*>::iterator it = m_ShaderMacros.find(macroName);
	if (it != m_ShaderMacros.end())
		macros = (*it).second;

	switch (type)
	{
		case EShaderTypes::ST_VERTEX:
		{
			VertexShader* vs = new VertexShader();
			LPCSTR profile = "vs_5_0";
			
			blob = Compile(name.get(), shaderFuncName, profile, flags, errorBlob, macros, loadPrecompiledShader);
			if (blob == nullptr)
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}
			
			hr = d3d->GetDevice11()->CreateVertexShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &vs->mShader);
			if( FAILED(hr) )
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}
			
			vs->mBlobData = blob;
			vs->mName = shaderFuncName;
			
#if defined(DEBUG) || defined(PROFILE)
			if ( SUCCEEDED(hr) )
			{
				vs->mShader->SetPrivateData( WKPDID_D3DDebugObjectName, lstrlenA(shaderFuncName), shaderFuncName );
			}
#endif
			//Add the vertex shader to the list of shaders
			m_Shaders.push_back(vs);
		}
		break;
		case EShaderTypes::ST_HULL:
		{
			HullShader* hs = new HullShader();
			LPCSTR profile = "hs_5_0";
			
			blob = Compile(name.get(), shaderFuncName, profile, flags, errorBlob, macros, loadPrecompiledShader);
			if (blob == nullptr)
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}
			
			hr = d3d->GetDevice11()->CreateHullShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &hs->mShader);
			if (FAILED(hr))
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}
			
			hs->mBlobData = blob;
			hs->mName = shaderFuncName;
			
#if defined(DEBUG) || defined(PROFILE)
			if (SUCCEEDED(hr))
			{
				hs->mShader->SetPrivateData(WKPDID_D3DDebugObjectName, lstrlenA(shaderFuncName), shaderFuncName);
			}
#endif
			
			//Add the hull shader to the list of shaders
			m_Shaders.push_back(hs);
		}
		break;
		case EShaderTypes::ST_DOMAIN:
		{
			DomainShader* ds = new DomainShader();
			LPCSTR profile = "ds_5_0";
			
			blob = Compile(name.get(), shaderFuncName, profile, flags, errorBlob, macros, loadPrecompiledShader);
			if (blob == nullptr)
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}
			
			hr = d3d->GetDevice11()->CreateDomainShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &ds->mShader);
			if (FAILED(hr))
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}
			
			ds->mBlobData = blob;
			ds->mName = shaderFuncName;
			
#if defined(DEBUG) || defined(PROFILE)
			if (SUCCEEDED(hr))
			{
				ds->mShader->SetPrivateData(WKPDID_D3DDebugObjectName, lstrlenA(shaderFuncName), shaderFuncName);
			}
#endif
			
			//Add the domain shader to the list of shaders
			m_Shaders.push_back(ds);
		}
		break;
		case EShaderTypes::ST_PIXEL:
		{
			PixelShader* ps = new PixelShader();
			LPCSTR profile = "ps_5_0";
			
			blob = Compile(name.get(), shaderFuncName, profile, flags, errorBlob, macros, loadPrecompiledShader);
			if (blob == nullptr)
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}

			hr = d3d->GetDevice11()->CreatePixelShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &ps->mShader);
			if (FAILED(hr))
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}

			ps->mBlobData = blob;
			ps->mName = shaderFuncName;

#if defined(DEBUG) || defined(PROFILE)
			if (SUCCEEDED(hr))
			{
				ps->mShader->SetPrivateData(WKPDID_D3DDebugObjectName, lstrlenA(shaderFuncName), shaderFuncName);
			}
#endif
			
			//Add the pixel shader to the list of shaders
			m_Shaders.push_back(ps);
		}
		break;
		case EShaderTypes::ST_GEOMETRY:
		{
			GeometryShader* gs = new GeometryShader();
			LPCSTR profile = "gs_5_0";
			
			blob = Compile(name.get(), shaderFuncName, profile, flags, errorBlob, macros, loadPrecompiledShader);
			if (blob == nullptr)
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}

			hr = d3d->GetDevice11()->CreateGeometryShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &gs->mShader);
			if (FAILED(hr))
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}

			gs->mBlobData = blob;
			gs->mName = shaderFuncName;

#if defined(DEBUG) || defined(PROFILE)
			if (SUCCEEDED(hr))
			{
				gs->mShader->SetPrivateData(WKPDID_D3DDebugObjectName, lstrlenA(shaderFuncName), shaderFuncName);
			}
#endif
			
			//Add the geometry shader to the list of shaders
			m_Shaders.push_back(gs);
		}
		break;
		case EShaderTypes::ST_COMPUTE:
		{
			ComputeShader* cs = new ComputeShader();
			LPCSTR profile = "cs_5_0";
			
			blob = Compile(name.get(), shaderFuncName, profile, flags, errorBlob, macros, loadPrecompiledShader);
			if (blob == nullptr)
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}

			hr = d3d->GetDevice11()->CreateComputeShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &cs->mShader);
			if (FAILED(hr))
			{
				OutputShaderErrorMessage(errorBlob, NULL, shaderFuncName, filename);
				exit(0);
			}

			cs->mBlobData = blob;
			cs->mName = shaderFuncName;

#if defined(DEBUG) || defined(PROFILE)
			if (SUCCEEDED(hr))
			{
				cs->mShader->SetPrivateData(WKPDID_D3DDebugObjectName, lstrlenA(shaderFuncName), shaderFuncName);
			}
#endif
			
			//Add the compute shader to the list of shaders
			m_Shaders.push_back(cs);
		}
		break;
		default:
		{
			ZShadeMessageCenter::MsgBoxError(NULL, "ShaderCompiler: ZShadeSandboxShader::Shader type not defined !!!");
			exit(0);
		}
		break;
	}
	
	return S_OK;
}
Пример #3
0
//===============================================================================================================================
bool TextureManager::loadTexture(BetterString filename, ID3D11Texture2D** texture, int& textureDataSize, byte*& srcTextureData, TextureType tt)
{
    ID3D11Device* device = mD3DSystem->GetDevice11();
    ID3D11DeviceContext* immediateContext = mD3DSystem->GetDeviceContext();
    ID3D11Texture2D* tempTexture;

    //string file = "Textures\\";
    //file += filename.toString();

    HRESULT hr = E_FAIL;

    unique_ptr<wchar_t> name = filename.ToWideStr();

    switch (tt)
    {
    case DDS:
    {
        hr = CreateDDSTextureFromFile(device, name.get(), (ID3D11Resource **)texture, NULL);
    }
    break;
    //case TGA:
    case PNG:
    {
        hr = CreateWICTextureFromFile(device, name.get(), (ID3D11Resource **)texture, NULL);
    }
    break;
    }

    //if(SUCCEEDED(D3DX11CreateTextureFromFile(device, file.c_str(), NULL, NULL, (ID3D11Resource **)texture, NULL)))
    if(SUCCEEDED(hr))
    {
        // Load the texture and store it
        GetTexture(filename, tt);

        D3D11_TEXTURE2D_DESC desc;
        (*texture)->GetDesc(&desc);

        // To keep it simple, we limit the textures we load to RGBA 8bits per channel
        if(desc.Format != DXGI_FORMAT_R8G8B8A8_UNORM)
        {
            OutputDebugStringA( "We want to read a simple RGBA texture 8 bits per channel but the required texture has a different format." );
            return false;
        }

        desc.Usage = D3D11_USAGE_STAGING;
        desc.BindFlags = 0;
        desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;

        if (device->CreateTexture2D(&desc, NULL, &tempTexture) != S_OK)
            return false;

        immediateContext->CopyResource(tempTexture, *texture);

        D3D11_MAPPED_SUBRESOURCE mappedResource;
        if (immediateContext->Map(tempTexture, 0, D3D11_MAP_READ, 0, &mappedResource) != S_OK)
            return false;

        textureDataSize =  mappedResource.RowPitch * desc.Height;
        if(srcTextureData)
            delete [] srcTextureData;
        srcTextureData = new byte[textureDataSize];
        memcpy(srcTextureData, mappedResource.pData, textureDataSize);

        immediateContext->Unmap(tempTexture, 0);

        return true;
    }
    else
        return false;
}