这是我对着色器的第一次体验,我正在学习directx编程的教程,我的项目中包含两个着色器文件,其中包含.vs和.ps文件类型。我不确定这些文件类型是否被识别为视觉工作室中hlsl的文件类型最后有.hlsl。有没有办法让这些文件类型被识别,如果没有,我将如何更改下面的代码以包含不同的文件类型?我的代码编译,它只是返回我的错误消息,说color.vs丢失。
在colorshaderclass.cpp的初始化函数下,返回函数调用如下:
result = InitializeShader(device, hwnd, L"../Engine/color.hlsl", L"../Engine/color.ps");
initializeShader函数本身定义如下:
bool ColorShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
{
HRESULT result;
ID3D10Blob* errorMessage;
ID3D10Blob* vertexShaderBuffer;
ID3D10Blob* pixelShaderBuffer;
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
unsigned int numElements;
D3D11_BUFFER_DESC matrixBufferDesc;
//initializ the pointers this function will use to null
errorMessage = 0;
vertexShaderBuffer = 0;
pixelShaderBuffer = 0;
//compile vertex shader code
result = D3DX11CompileFromFile(vsFilename, NULL, NULL, "ColorVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL,
&vertexShaderBuffer, &errorMessage, NULL);
if (FAILED(result))
{
//if the shader failed to compile it should have written something to error message
if (errorMessage)
{
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
}
//if there was nothing in error message then it couldnt find shader file
else
{
MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
}
return false;
}
//compile pixel shader code
if (FAILED(result))
{
if (errorMessage)
{
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
}
else
{
MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
}
return false;
}
非常感谢任何帮助。我不确定这是一个设置问题,还是我需要更改文件类型或者我是否需要包含头文件(尽管尝试了这个选项,VS尝试使用c ++语法调试hlsl文件)。
答案 0 :(得分:0)
您的着色器中是否有#include
个语句?如MSDN上的文档中所述,如果您提供'NULL'的pInclude处理程序,那么任何#include
语句都将失败。
此外,您不需要使用旧版DirectX SDK中不推荐使用的D3DX11库,特别是因为您拥有VS 2013工具集。您可以直接使用D3DCompileFromFile
,并且应该将D3D_COMPILE_STANDARD_FILE_INCLUDE
用于pInclude处理程序。请参阅HLSL FXC and D3DCompile以及Living without D3DX。
result = D3DCompileFromFile(vsFilename, nullptr,
D3D_COMPILE_STANDARD_FILE_INCLUDE , "ColorVertexShader", "vs_5_0",
D3DCOMPILE_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage );
您可能需要查看this tutorial以获取有关创建和使用自己的着色器的一些提示,特别是如果您希望使用Visual Studio在编译时将它们构建到.cso中,这样就无需使用{{1在运行时完全没有。
答案 1 :(得分:0)
正如Chuck所提到的,你正在使用一个弃用的API,你还需要确保将着色器文件编译为.cso,因为这是你的应用程序要读取的内容。当您编译.cpp文件时,VS 2013会自动将.hlsl文件编译为.cso,因此这不应该是一个问题。假设在编写LoadShaderFile函数时,.cso文件就在那里。在“使用着色器”部分的this excellent tutorial中提取的示例:
#include <fstream>
// this function loads a file into an Array^
Array<byte>^ LoadShaderFile(std::string File)
{
Array<byte>^ FileData = nullptr;
// open the file
std::ifstream VertexFile(File, std::ios::in | std::ios::binary | std::ios::ate);
// if open was successful
if(VertexFile.is_open())
{
// find the length of the file
int Length = (int)VertexFile.tellg();
// collect the file data
FileData = ref new Array<byte>(Length);
VertexFile.seekg(0, std::ios::beg);
VertexFile.read(reinterpret_cast<char*>(FileData->Data), Length);
VertexFile.close();
}
return FileData;
}
//... And this is the call and use.
Array<byte>^ VSFile = LoadShaderFile("VertexShader.cso");
device->CreateVertexShader(VSFile->Data, VSFile->Length, nullptr, &vertexshader);