【问题标题】:What am I doing wrong when trying to assign a value in hlsl?尝试在 hlsl 中分配值时我做错了什么?
【发布时间】:2021-03-02 17:58:18
【问题描述】:

我在几何着色器中有这个

cbuffer cbFixed
{
    float2 TexC[4] =
    {
        float2(0.0f, 1.0f),
        float2(0.0f, 0.0f),
        float2(1.0f, 1.0f),
        float2(1.0f, 0.0f)
    };
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float4 PosWorld : POSITION;
    float4 Norm : NORMAL;
    float2 Tex : TEXCOORD;
    uint PrimID: SV_PrimitiveID;
};

以后……

PS_INPUT gout;

for (int i = 0; i < 4; i++)
{
// other stuff
gout.Tex = TexC[i];
// other stuff
}

但是,由于某种原因,这不能按预期工作,我的意思是未应用纹理,而这样做:

for (int i = 0; i < 4; i++)
{
    if (i == 0) gout.Tex = float2(0.0f, 1.0f);
    if (i == 1) gout.Tex = float2(0.0f, 0.0f);
    if (i == 2) gout.Tex = float2(1.0f, 1.0f);
    if (i == 3) gout.Tex = float2(1.0f, 0.0f);
}

知道为什么吗?我不想把这篇文章写得太长,所以尽量减少细节。

【问题讨论】:

    标签: arrays geometry shader assign hlsl


    【解决方案1】:

    您的 TexC 值将被忽略,因为 cbuffer 中的变量必须通过调用 *SetConstantBuffers 设置,并且像您这样的默认初始化程序将被忽略,如果您不这样做,您将获得未定义的行为(在大多数情况下,您将阅读零值)。你想要的是这样写:

    static const float2 TexC[4] =
        {
            float2(0.0f, 1.0f),
            float2(0.0f, 0.0f),
            float2(1.0f, 1.0f),
            float2(1.0f, 0.0f)
        };
    

    而不是 cbuffer。

    【讨论】:

    • 非常感谢您的回复。
    • 回复让我改进了我的搜索,我发现了以下内容,这正是我遇到的问题。我的大部分困惑来自于观察到 Luna 演示中的相同代码在他的演示中运行,但是当我将它复制到我的演示中时它失败了(不使用效果框架)。 gamedev.net/forums/topic/…
    • @Hippo 我从未真正使用过效果框架,所以我不知道它在那里工作。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多