|
本人最近封装了DX中关于着色器的代码,代码如下:
void ShaderPipe: oadShader(LPCSTR shaderPath,/*着色器文件路径*/
LPCSTR importFunName,/*着色器入口函数名*/
int type/*着色器类型,0为默认顶点着色器, 1为像素着色器*/)
{
//编译着色器文件
HRESULT hr = D3DXCompileShaderFromFile(shaderPath, 0, 0,
importFunName, "vs_2_0",D3DXSHADER_DEBUG,&m_compiledCode,
&m_error, &m_contantTable);
if(m_error)
{
::MessageBox(NULL, (char*)m_error->GetBufferPointer(), "ShaderPipe Error", MB_OK);
//d3d::Release<ID3DXBuffer*>(m_error);
delete m_error;
m_error = NULL;
}
if(FAILED(hr))
{
::MessageBox(NULL, "Compile Shader failed!", "ShaderPipe Error", MB_OK);
}
//创建顶点着色器
if(!type)
{
hr = m_device->CreateVertexShader((DWORD*)m_compiledCode->GetBufferPointer(), &m_vertexshader);
if(FAILED(hr))
{
::MessageBox(NULL, "Create VertexShader failed!", "ShaderPipe Error", MB_OK);
delete m_compiledCode;
m_compiledCode = NULL;
}
}
//创建像素着色器
else
{
hr = m_device->CreatePixelShader((DWORD*)m_compiledCode->GetBufferPointer(), &m_pixelshader);
if(FAILED(hr))
{
::MessageBox(NULL, "Create PixelShader failed!", "ShaderPipe Error", MB_OK);
delete m_compiledCode;
m_compiledCode = NULL;
}
}
}
/*设置当前加载的着色器到渲染管道*/
void ShaderPipe::UseShader(int type)
{
if(!type)
m_device->SetVertexShader(this->m_vertexshader);
else
m_device->SetPixelShader(this->m_pixelshader);
}
/*通过名字访问变量*/
D3DXHANDLE ShaderPipe::GetConstantByName(LPCSTR varName, /*变量名称*/
D3DXHANDLE handle/*变量的父结构*/)
{
constantVar = m_contantTable->GetConstantByName(handle, varName);
if(!constantVar)
{
char str[50];
sprintf(str,"Can't get constantVar '%s'",varName);
::MessageBox(NULL,str, "ShaderPipe Error", MB_OK);
}
return constantVar;
}
但是使用加载着色器之后,调用ShaderPipe::GetConstantByName方法获取着色器中的常量总是返回为空值,请问大家,上述代码中有什么问题吗?本人检查了很久,未能找出问题所在 |
|