无法从.cso文件(DirectX,c++)加载“顶点着色器”

Unable to load Vertex Shader from .cso file (DirectX, c++)

本文关键字:顶点 加载 顶点着色器 c++ cso 文件 DirectX      更新时间:2023-10-16

我正在尝试从之前编译的.cso文件加载顶点着色器。

我使用的代码是:

char* bytes = { 0 };
size_t fSize = readFileBytes("vsTex.cso", bytes);
hr = d3d11Device->CreateVertexShader((void*)bytes, fSize, NULL, &TexVertexShader);
if (hr != S_OK){
    MessageBox(NULL, "FAIL", "ERROR", NULL);
}

readFileBytes:

  long readFileBytes(const char *name, char* bytes)
    {
        FILE *fl; 
        fopen_s(&fl, name, "r");
        fseek(fl, 0, SEEK_END);
        long len = ftell(fl);
        bytes = (char*)malloc(len);
        fseek(fl, 0, SEEK_SET);
        fread(bytes, 1, len, fl);
        fclose(fl);
        return len;
    }

d3d11Device是指向ID3D11设备的指针。我一直在使用D3D11CompileFromFile()创建顶点着色器,没有任何错误,所以据我所知,着色器本身没有任何问题。

我不确定是我只是读错了文件,还是在给函数提供字节之前,我必须对它做其他事情。

我注意到的一些事情可能会导致一些问题。

long readFileBytes(const char *name, char* bytes)
long readFileBytes(const char *name, char*& bytes) //pass in bytes by reference
fopen_s(&fl, name, "r");
fopen_s(&fl, name, "rb"); //open file in binary mode

最后,不要忘记在创建着色器后释放字节。

free(bytes);