Skip to content

Switching code analysis context for hlsl cginc files in Rider

Vladimir Krasnotsvetov edited this page Oct 5, 2020 · 1 revision

A .hlsl shader file can use preprocessor symbols and #if statements to modify behaviour at compile time, by changing the definition, availability or implementation of methods and data structures. When analysing one of these files, Rider will by default use an “automatic” context of default defined symbols. This can lead to sections of the .hlsl file being marked as inactive, without syntax highlighting, code completion, inspections and so on.

If the .hlsl file is included from multiple .shader files, or from multiple CGPROGRAM blocks within a .shader file, then Rider can use the context defined at the include point to analyse the .hlsl file. You can switch the context using the Unity Shader Context picker in the status bar:

For example, let us assume we have a file example.hlsl that defines different methods if the FOO symbol is defined or not. In the Auto context, Rider will default to FOO being undefined, and mark the first branch of this #if statement as inactive. The bar function is defined in this context, and any attempt to use the foo function will result in an unresolved symbol error.

#if defined(FOO)
    void foo() {}
#else
    void bar() {}
#endif

Let us also assume that we also have a Foo.shader file that defines multiple shader programs, and includes example.hlsl multiple times from each CGPROGRAM block. Each shader program has its own context. The first, starting at line 14, defines the symbol FOO before including the example.hlsl, while the second program, starting at line 22, defines BAR, and then includes the HLSL file.

...
CGPROGRAM // line 14
#pragma multi_compile FOO
#include “example.hlsl”
ENDCG
...
CGPROGRAM // line 22
#pragma multi_compile BAR
#include “example.hlsl”
ENDCG
...

When editing example.hlsl, you can switch context by clicking the context picker and choosing the location of the shader program from the popup. Rider will show the Auto context, but will also list the locations that include example.hlsl. If you select Foo.shader:14, then Rider will analyse the example.hlsl file using the context of the CGPROGRAM shader at line 14 of Foo.shader. In other words, the FOO symbol will be defined, and the first branch of the #if statement above will become active, and the foo function will be defined.

Clone this wiki locally