gpt4 book ai didi

c++ - 获取着色器字节码以传递给 CreateVertexShader()

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:58:23 24 4
gpt4 key购买 nike

我正在尝试通过 ID3D11Device::CreateVertexShader() 创建一个顶点着色器对象。参数要求指向着色器字节码的指针和字节码的长度。因此,我有以下结构:

struct shaderData
{
char *shaderCode;
UINT size;
};

以及以下函数,它返回此结构以尝试获取着色器字节代码(我使用 visual studio 2012 和 fxc.exe 将我的着色器编译为已编译的着色器对象 (.cso) 文件。

struct shaderData *CubeApp::GetShaderByteCode(char *compiledShader)
{
FILE *pFile = 0;
char *shaderCode = 0;
UINT fSize = 0;
UINT numRead = 0;
struct shaderData *sData = (struct shaderData *)malloc(sizeof(struct shaderData));

pFile = fopen(compiledShader, "rb");

fseek(pFile, 0, SEEK_END);
fSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);

shaderCode = (char *)malloc(fSize);

while (numRead != fSize)
numRead = fread(&shaderCode[numRead], 1, fSize, pFile);

fclose(pFile);

sData->shaderCode = shaderCode;
sData->size = fSize;

return sData;
}

我这样调用函数:

struct shaderData *sData = GetShaderByteCode("VShader.cso");

但是,当我调用 ID3D11Device::CreateVertexShader() 并将 sData->shaderCode 和 sData->size 作为参数传递时,我的顶点着色器对象 (ID3D11VertexShader) 返回 NULL,函数返回 E_INVALIDARG。

也许我对着色器字节码的理解有误,但这个函数不应该给我正确的参数传递给 CreateVertexShader 吗?

最佳答案

试试这个:

struct shaderData
{
BYTE *shaderCode;
UINT size;
};

着色器字节码需要是 unsigned char(这就是 BYTE 类型)。我不确定这是否是您的代码的唯一问题,但在我这边,它适用于这种类型。

关于c++ - 获取着色器字节码以传递给 CreateVertexShader(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14109362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com