bool c_packed_file_processor_shader::ProcessElement(c_packed_file::s_element_editor& element, const boost::filesystem::path& file_path)
		{
			c_auto_release<ID3DXEffectCompiler> compiler;
			c_auto_release<ID3DXBuffer> error_buffer;
			c_auto_release<ID3DXBuffer> compiled_effect;

			HRESULT hr;

			// Attempt to create an effect compiler from the shader file
			hr = D3DXCreateEffectCompilerFromFile(file_path.string().c_str(), 
				nullptr,
				nullptr, 
				D3DXSHADER_OPTIMIZATION_LEVEL3, 
				&compiler,
				&error_buffer);

			// If compilation failed, print the debug text to the console
			if(FAILED(hr)) 
			{ 
				if(error_buffer)
				{
					puts((char*)error_buffer->GetBufferPointer());
				}
				return false;
			}

			// Attempt to compile the effect
			hr = compiler->CompileEffect(D3DXSHADER_OPTIMIZATION_LEVEL3,
				&compiled_effect,
				&error_buffer);

			// If compilation failed, print the debug text to the console
			if(FAILED(hr)) 
			{ 
				puts((char*)error_buffer->GetBufferPointer());
				return false;
			}

			// Copy the compiled data to the element
			element.element_size = compiled_effect->GetBufferSize();
			element.source_data = new char[element.element_size];
			errno_t error = memcpy_s(element.source_data, 
				element.element_size, 
				compiled_effect->GetBufferPointer(), 
				element.element_size);

			if(error != 0)
			{
				return false;
			}

			return true;
		}
Esempio n. 2
0
HRESULT ScalingEffect::LoadEffect(const TCHAR *filename)
{
    KillThis();

    LPD3DXBUFFER		lpBufferEffect = 0;
    LPD3DXBUFFER		lpErrors = 0;
    LPD3DXEFFECTCOMPILER	lpEffectCompiler = 0;

    m_strErrors += filename;
    m_strErrors += ":\n";

    // First create an effect compiler
    HRESULT hr = D3DXCreateEffectCompilerFromFile(filename, NULL, NULL,NULL,
						&lpEffectCompiler, &lpErrors);
	
    // Errors...
    if(FAILED(hr)) {
	if(lpErrors) {
	    m_strErrors += (char*) lpErrors->GetBufferPointer();
	    SAFE_RELEASE(lpErrors);
	}

	m_strErrors += "Unable to create effect compiler from ";
	m_strErrors += filename;
    }

    if(SUCCEEDED(hr)) {
#ifdef C_D3DSHADERS_COMPILE_WITH_DEBUG
	hr = lpEffectCompiler->CompileEffect(D3DXSHADER_DEBUG, &lpBufferEffect, &lpErrors);
#else
	hr = lpEffectCompiler->CompileEffect(0, &lpBufferEffect, &lpErrors);
#endif
	
	// Errors...
	if(FAILED(hr)) {
	    if(lpErrors) {
		m_strErrors += (char*) lpErrors->GetBufferPointer();
		SAFE_RELEASE(lpErrors);
	    }

	    m_strErrors += "Unable to compile effect from ";
	    m_strErrors += filename;
	}
    }

    if(SUCCEEDED(hr)) {
	hr = D3DXCreateEffect(m_pd3dDevice,
			    lpBufferEffect->GetBufferPointer(),
			    lpBufferEffect->GetBufferSize(),
			    NULL, NULL,
			    0,
			    NULL, &m_pEffect, &lpErrors);

	// Errors...
	if(FAILED(hr)) {
	    if(lpErrors) {
		m_strErrors += (char*) lpErrors->GetBufferPointer();
		SAFE_RELEASE(lpErrors);
	    }

	    m_strErrors += "Unable to create effect from compiled ";
	    m_strErrors += filename;
	}
    }

    if(SUCCEEDED(hr)) {
        m_pEffect->GetDesc(&m_EffectDesc);
	hr = ParseParameters(lpEffectCompiler);
    }

    SAFE_RELEASE(lpErrors);
    SAFE_RELEASE(lpBufferEffect);
    SAFE_RELEASE(lpEffectCompiler);
    return hr;
}