tgjones / HlslTools

A Visual Studio extension that provides enhanced support for editing High Level Shading Language (HLSL) files
http://timjones.io/blog/archive/2016/04/25/hlsl-tools-for-visual-studio-v1.0-released
Other
565 stars 97 forks source link

Support relative path in virtual directory mapping #232

Closed zongsheng-zhao closed 2 years ago

zongsheng-zhao commented 2 years ago

Supported use of relative path in virtual directory key, value and include path.

For example, we write shadertoolsconfig.json like this:

{
    "hlsl.additionalIncludeDirectories": [
        ".",
        "/UnityProjects\\PlaygroundHDRP"
    ],
    "hlsl.virtualDirectoryMappings": {
        "Packages\\com.unity.render-pipelines.high-definition": "D:\\UnityProjects\\PlaygroundHDRP\\Library\\PackageCache\\com.unity.render-pipelines.high-definition@12.1.6", 
        "D:\\UnityProjects/PlaygroundHDRP\\Packages\\com.unity.render-pipelines.core": "Library/PackageCache\\com.unity.render-pipelines.core@12.1.6"
    }
}

Then HlslTools extension can correctly parse the include paths like these:

#include "/UnityProjects/PlaygroundHDRP\\Packages\\com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMapping.hlsl"
#include "/UnityProjects\\PlaygroundHDRP\\Library\\PackageCache\\com.unity.render-pipelines.high-definition@12.1.6/Runtime/Material/MaterialUtilities.hlsl"
#include "D:\\UnityProjects/PlaygroundHDRP\\Packages\\com.unity.render-pipelines.core/ShaderLibrary/Sampling/SampleUVMapping.hlsl"
#include "D:\\UnityProjects\\PlaygroundHDRP\\Library\\PackageCache\\com.unity.render-pipelines.high-definition@12.1.6/Runtime/Material/MaterialUtilities.hlsl"
#include "Packages/com.unity.render-pipelines.core\\ShaderLibrary/Sampling\\SampleUVMapping.hlsl"
#include "Packages\\com.unity.render-pipelines.high-definition/Runtime/Material/MaterialUtilities.hlsl"

The logic is like this: In the json file we have "<vpath>": "<rpath>" pairs. The extension will create a virtual directory at <vpath>, which will be mapped to the real path <rpath>. If <vpath> or <rpath> is not a fully qualified path, then absolute fully qualified path will be calculated based on the path of json file. And when the extension searches include files, it will obey the normal search rule. If the include path is a relative path, tries to prefix include directories and convert it to absolute path. And then tries to find the file in that path, considering virtual directories.

zongsheng-zhao commented 2 years ago

related issue: https://github.com/tgjones/HlslTools/issues/204#issue-825714819

tgjones commented 2 years ago

Hi @zongsheng-zhao - sorry for the big delay in replying. Thank you for taking the time to prepare this PR! I think I want to go another way though (#235) for a couple of reasons.

  1. I'm not sure I see the value in resolving virtual directory keys to absolute paths.
  2. I'd prefer to keep additionalIncludeDirectories and virtualDirectoryMappings separate, mainly to keep the implementation simple.