Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Injecting Different Varients into a Shader at Runtime

Discussion in 'Editor & General Support' started by PixelNomads, Jun 28, 2020.

  1. PixelNomads

    PixelNomads

    Joined:
    Jul 10, 2015
    Posts:
    1
    I am trying to get a very basic implementation of IUnityShaderCompilerAccess.h to work so I can inject different variants into a shader from a DLL but the little documentation provided is vague and there are no examples on the internet on how to do this.

    The manual says:
    The code of interest (found within Unity\Editor\Data\PluginAPI\IUnityShaderCompilerAccess.h):
    Code (CSharp):
    1. enum UnityShaderCompilerExtEventType
    2. {
    3.     kUnityShaderCompilerExtEventCreateCustomSourceVariant, // The plugin can create a new variant based on the initial snippet. The callback will receive UnityShaderCompilerExtCustomSourceVariantParams as data.
    4.  
    5.     ...
    6.  
    7. };
    8.  
    9.  
    10. struct UnityShaderCompilerExtCustomSourceVariantParams
    11. {
    12.     char* outputSnippet;                                    // snippet after the plugin has finished processing it.
    13.     char* outputKeywords;                                   // keywords exported by the plugin for this specific variant
    14.     const char* inputSnippet;                               // the source shader snippet
    15.     bool vr;                                                // is VR enabled / supported ?
    16.     UnityShaderCompilerExtCompilerPlatform platform;        // compiler platform
    17.     UnityShaderCompilerExtShaderType shaderType;            // source code type
    18. };
    and the brief example:
    Code (CSharp):
    1. extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
    2.         UnityShaderCompilerExtEvent(UnityShaderCompilerExtEventType event, void* data)
    3.         {
    4.             switch (event)
    5.             {
    6.  
    7.                 ...
    8.  
    9.                 case kUnityShaderCompilerExtEventPluginConfigure:
    10.                 {
    11.                     unsigned int GPUCompilerMask = (1 << kUnityShaderCompilerExtGPUProgramTargetDX11VertexSM40)
    12.                     | (1 << kUnityShaderCompilerExtGPUProgramTargetDX11VertexSM50)
    13.                     | (1 << kUnityShaderCompilerExtGPUProgramTargetDX11PixelSM40)
    14.                     | (1 << kUnityShaderCompilerExtGPUProgramTargetDX11PixelSM50)
    15.                     | (1 << kUnityShaderCompilerExtGPUProgramTargetDX11GeometrySM40)
    16.                     | (1 << kUnityShaderCompilerExtGPUProgramTargetDX11GeometrySM50)
    17.                     | (1 << kUnityShaderCompilerExtGPUProgramTargetDX11HullSM50)
    18.                     | (1 << kUnityShaderCompilerExtGPUProgramTargetDX11DomainSM50);
    19.  
    20.                     unsigned int ShaderMask = kUnityShaderCompilerExtGPUProgramVS | kUnityShaderCompilerExtGPUProgramDS;
    21.  
    22.                     IUnityShaderCompilerExtPluginConfigure* pluginConfig = (IUnityShaderCompilerExtPluginConfigure*)data;
    23.                     pluginConfig->ReserveKeyword(SHADER_KEYWORDS);
    24.                     pluginConfig->SetGPUProgramCompilerMask(GPUCompilerMask);
    25.                     pluginConfig->SetShaderProgramMask(ShaderMask);
    26.                     break;
    27.                 }
    28.             }
    29.         }
    It is unclear how we are supposed to tell the plugin which shader we are referring to (because the callback only receives an input "snippet" of shader code rather than the whole shader with the name attached).
     
    Last edited: Jun 28, 2020
    AlanMattano likes this.
  2. AFrisby

    AFrisby

    Joined:
    Apr 14, 2010
    Posts:
    223
    Bumping as I'd like to know as well