无法从cso文件创建顶点着色器(从fx文件创建)

问题描述 投票:1回答:1

Shader在生成时成功编译为cso文件。但是,当我尝试调用CreateVertexShader时,出现错误:

D3D11错误:ID3D11Device :: CreateVertexShader:编码的顶点着色器大小与指定的大小不匹配。

我设置了着色器类型“ Effect(/ fx)”,入口点:“ VS”,着色器模型-5.0。

我尝试过4.0、4.1模型-错误有所不同:

D3D11错误:ID3D11Device :: CreateVertexShader:着色器已损坏或格式无法识别。

我的代码:

ID3DBlob* pVSBlob = NULL;
HRESULT hr = D3DReadFileToBlob((WCHAR*)name.c_str(), &pVSBlob);
if (FAILED(hr))
{
    return;
}

hr = g_pd3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &vertexShader); 

设备功能:

D3D_FEATURE_LEVEL featureLevels[] =
{
    D3D_FEATURE_LEVEL_12_1,
    D3D_FEATURE_LEVEL_12_0,
    D3D_FEATURE_LEVEL_11_1,
    D3D_FEATURE_LEVEL_11_0,
    D3D_FEATURE_LEVEL_10_1,
    D3D_FEATURE_LEVEL_10_0,
};

我的着色器代码:

cbuffer cbTrunkCutMode : register(b1)
{
    float HighlightAdj;
};

cbuffer cbChangesEveryFrame : register(b0)
{
    matrix Matrices[2];
};


struct VS_INPUT
{
    float3 VertexPosition_modelspace : POSITION;
    float3 vertexNormal_modelspace : NORMAL;
    float age : AGE;
};

struct PS_INPUT
{
    float4 Position : SV_POSITION;
    float3 Position_worldspace : WSPOSITION;
    float3 Normal_cameraspace : NORMAL;
    float3 EyeDirection_cameraspace : EYEDIRECTION;
    float age : AGE;
};

PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output;
    output.Position= mul(Matrices[1], float4(input.VertexPosition_modelspace, 1.0));
    output.Position_worldspace = input.VertexPosition_modelspace;

    float3 vertexPosition_cameraspace = mul(Matrices[0], float4(input.VertexPosition_modelspace,1.0)).xyz;
    output.EyeDirection_cameraspace = float3(0.0, 0.0, 0.0) - vertexPosition_cameraspace;

    output.Normal_cameraspace = mul((float3x3)Matrices[0], input.vertexNormal_modelspace); // Only correct if ModelMatrix does not scale the model ! Use its inverse transpose if not.
    output.age = input.age;

    return output;
}

static const float3 LightPosition = normalize(float3(1.0, 1.0, 1.0));

float4 PS(PS_INPUT input) : SV_Target
{
    float3 MaterialDiffuseColor = float3(0.05,0.3,0.05);
    float3 MaterialAmbientColor = float3(0.3, 0.3, 0.3) * MaterialDiffuseColor;
    float3 MaterialSpecularColor = 0.5*MaterialDiffuseColor;

    float3 n = normalize(input.Normal_cameraspace);
    float3 l = LightPosition;
    float cosTheta = clamp(dot(n,l), 0.0,1.0);

    float3 E = normalize(input.EyeDirection_cameraspace);
    float3 R = reflect(-l,n);
    float cosAlpha = clamp(dot(E,R), 0.0,1.0);

    return float4((
        float3(HighlightAdj - input.age, -HighlightAdj, -input.age - HighlightAdj) +
        MaterialAmbientColor +
        MaterialDiffuseColor * cosTheta +
        MaterialSpecularColor * pow(cosAlpha, 5.0)),1);
}
c++ uwp directx-11
1个回答
2
投票

Visual Studio的内置HLSL生成规则对小型项目很有用,但是要牢记一个概念:Visual Studio的项目系统假定每个源文件都用于生成一个编译的目标文件(在本例中为.cso) 。

要在同一文件中合并着色器类型时使此工作有效,只需创建一些其他文件。例如:

第一个文件是MyShader.hlsli。这将包含您为VS和PS组合的着色器代码]

第二个文件MyShaderVS.hlsl

#include "MyShader.hlsli"

第三文件MyShaderPS.hlsl

#include "MyShader.hlsli"

然后您将VS Project的构建属性设置为将MyShaderVS.hlsl构建为顶点着色器(/vs)。您将MyShaderPS.hlsl构建为像素着色器(/ps)。设置适合每个像素的入口点。

结果将使您的原始着色器代码多次构建。

具有一个.fx且在一个已编译对象中全部编译成很多着色器blob的Effects文件系统,可与Visual Studio系统一起使用,但是(a)已弃用编译器支持,并且(b)您需要运行时库从GitHub使用它。建议改用HLSL着色器类型。参见the wiki

© www.soinside.com 2019 - 2024. All rights reserved.