Help us understand the problem. What is going on with this article?

[material dynamic branch] X4014 divergent gradient operation error

More than 1 year has passed since last update.

アドベントカレンダー向けにちょっとマテリアルを調査してたら動的分岐にまつわるコンパイルエラーに遭遇したのでメモっておく

ブランチブロックと勾配加工

動的分岐を使った最適化をおこなうとX4014のdivergent gradient operationがなんとかなんとかというエラーが出ることがあります。

image.png

大雑把に言ってサンプラーに渡されるUVの値をブランチブロック内部で加工するとこのエラーが発生します。
加工するということはサンプラーがMIPMAPを選択するときに使う勾配が変化してしまうためです。

BiasでUVに加工なし > OK

float4  Local3 = ProcessMaterialColorTextureLookup(
    Texture2DSampleBias(Material_Texture2D_0,Material_Texture2D_0Sampler,Parameters.TexCoords[0].xy,View_MaterialTextureMipBias)
);

BiasでUVに加工あり > Compile error

float4  Local3 = ProcessMaterialColorTextureLookup(
    Texture2DSampleBias(Material_Texture2D_0,Material_Texture2D_0Sampler,Parameters.TexCoords[0].xy+float2(1.0f,0.5f),View_MaterialTextureMipBias)
);

レベルを直接指定するもの(SampleLevel) > OK

float4  Local7 = ProcessMaterialColorTextureLookup(
    Texture2DSampleLevel(Material_Texture2D_0,Material_Texture2D_0Sampler,Parameters.TexCoords[0].xy+float2(1.0f,0.5f), float (Local6))
);

まとめ

1.シェーダー内部で動的分岐を使う場合は、サーフェスに使う場合はブロック内部でUVを加工せずに使う
2.ポストプロセスなどのSampleLevelを使って直接指定するものは気にしないでOK

サーフェス用のマテリアルで動的分岐を使う場合はちょっと気を付けないといけない。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away