#ifdef USE_D3D9
D3D9VertexShader::D3D9VertexShader()
{
_Shader = NULL;
_device = NULL;
_constantTable = NULL;
}
D3D9VertexShader::~D3D9VertexShader()
{
FreeMemory();
}
void D3D9VertexShader::FreeMemory()
{
if(_Shader)
{
_Shader->Release();
_Shader = NULL;
}
if(_constantTable)
{
_constantTable->Release();
_constantTable = NULL;
}
}
void D3D9VertexShader::ReleaseMemory()
{
FreeMemory();
}
void D3D9VertexShader::Reset(GraphicsDevice &graphics)
{
FreeMemory();
HRESULT hr;
_device = graphics.CastD3D9().GetDevice();
Assert(_device != NULL, "_device == NULL");
_declaration.Init(_device);
DWORD dwShaderFlags = 0;
#ifdef DEBUG_VS
dwShaderFlags |= D3DXSHADER_SKIPOPTIMIZATION|D3DXSHADER_DEBUG;
#endif
LPD3DXBUFFER pCode = NULL;
LPD3DXBUFFER pErrors = NULL;
PersistentAssert(Utility::FileExists(_shaderFile), String(_shaderFile) + String(" not found."));
hr = D3DXCompileShaderFromFile( _shaderFile.CString(), NULL, NULL, "VShaderEntry",
"vs_3_0", dwShaderFlags, &pCode,
&pErrors, &_constantTable );
String ErrorString;
if(pErrors)
{
char *ErrorMessage = (char *)pErrors->GetBufferPointer();
DWORD ErrorLength = pErrors->GetBufferSize();
ofstream file("ShaderDebug.txt");
for(UINT i = 0; i < ErrorLength; i++)
{
file << ErrorMessage[i];
ErrorString += String(ErrorMessage[i]);
}
file.close();
}
PersistentAssert(!FAILED(hr), String("D3DXCompileShaderFromFile failed: ") + ErrorString);
hr = _device->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(),
&_Shader );
if(pErrors)
{
pErrors->Release();
}
if(pCode)
{
pCode->Release();
}
Assert(!FAILED(hr), "CreateVertexShader failed");
}
void D3D9VertexShader::Init(GraphicsDevice &graphics, const String &filename)
{
_shaderFile = filename;
Reset(graphics);
}
void D3D9VertexShader::Set()
{
_declaration.Set();
_device->SetVertexShader(_Shader);
}
#endif