Search Unity

HDRP 7.0 HDShaderIDs is inaccessible due to its protection level

Discussion in 'Graphics Experimental Previews' started by iamarugin, Aug 8, 2019.

  1. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    173
    So, what we should use instead? For example when writing custom sky I used HDShaderIDs._PixelCoordToViewDirWS
     
  2. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    173
  3. darkydoodle

    darkydoodle

    Joined:
    Oct 27, 2018
    Posts:
    45
    I think I had the same problem with HDShaderPassNames, so I just modified the source class so that it's public. You could do the same with HDShaderIDs. I had 2 or 3 little problems like this with the last SRP/HDRP master from GitHub (a problem with a shadow parameter with a fixed max value also). Anyway, things are bound to change until it's stable so I'm not bothered so much by it. It would be more of a problem if there was lots of little things to modify at every new master version, or if it was somewhat final.
     
  4. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    173
  5. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    173
  6. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    173
  7. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    21
    Hello, for the out of preview of HDRP we decided to cleanup the classes accessibility levels and document our scripting API. Your issue is related to these changes and we currently don't have any plan to make this public, so i'd say that the best way to use shader ids or pass names now is just to copy-paste the values you need from this file: https://github.com/Unity-Technologi...n/Runtime/RenderPipeline/HDStringConstants.cs.
    We know that it's not ideal but on the other side, the content of these fields are very unlikely to be changed so it shouldn't break.
     
  8. andybak

    andybak

    Joined:
    Jan 14, 2017
    Posts:
    186
    This is why protection levels should be optional and easily overridden. People who are using frameworks and libraries are capable of understanding the cost vs benefit of using internal APIs better than the person who wrote the framework and library for the simple reason that they know their own use cases.

    Things might get broken in future updates? Sure - that's why you don't upgrade libraries casually.

    Python gets this right - not so much technically but philosophically.

    There's also some cargo-culting of OOP principles. Much of the bondage and discipline thinking in OOP-land makes sense in an enterprise context - or in long-lived application development involving teams with diverse skill levels.

    Those principles don't always translate well to other contexts.
     
    Last edited: Aug 26, 2019
  9. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    173
  10. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,721
    If anyone really needs this, this is my workaround to get access to internal methods that does not require making direct changes to a package or duplicating code.

    Code (CSharp):
    1. // <copyright file="UpdateShaderGraphAssemblyInfo.cs" company="BovineLabs">
    2. // Copyright (c) BovineLabs. All rights reserved.
    3. // </copyright>
    4.  
    5. namespace BovineLabs.Vision.Editor.ShaderGraph
    6. {
    7.     using System;
    8.     using System.IO;
    9.     using System.Linq;
    10.     using UnityEditor;
    11.     using UnityEditor.Compilation;
    12.  
    13.     /// <summary>
    14.     /// Adds our assembly to the shader graph AssemblyInfo to give us internal access. A nasty hack required for now.
    15.     /// </summary>
    16.     [InitializeOnLoad]
    17.     public static class UpdateShaderGraphAssemblyInfo
    18.     {
    19.         private const string ShaderGraphAssemblyName = "Unity.ShaderGraph.Editor";
    20.         private const string AssemblyInfoName = "AssemblyInfo.cs";
    21.         private const string VisibleLine = "[assembly: InternalsVisibleTo(\"BovineLabs.Vision.Editor\")]";
    22.  
    23.         static UpdateShaderGraphAssemblyInfo()
    24.         {
    25.             CompilationPipeline.assemblyCompilationStarted += OnAssemblyCompilationStarted;
    26.         }
    27.  
    28.         private static void OnAssemblyCompilationStarted(string assemblyName)
    29.         {
    30.             var asmdef = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName(assemblyName);
    31.             var name = Path.GetFileNameWithoutExtension(asmdef);
    32.  
    33.             if (name == null || !name.Equals(ShaderGraphAssemblyName))
    34.             {
    35.                 return;
    36.             }
    37.  
    38.             var directory = Path.GetDirectoryName(asmdef);
    39.             var assemblyInfoPath = Path.Combine(directory ?? throw new NullReferenceException(), AssemblyInfoName);
    40.  
    41.             var assemblyInfo = File.ReadAllLines(assemblyInfoPath);
    42.  
    43.             // Already setup
    44.             if (assemblyInfo.Contains(VisibleLine))
    45.             {
    46.                 return;
    47.             }
    48.  
    49.             TurnOffReadOnly(assemblyInfoPath);
    50.             File.AppendAllText(assemblyInfoPath, VisibleLine);
    51.         }
    52.  
    53.         private static void TurnOffReadOnly(string file)
    54.         {
    55.             File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly);
    56.         }
    57.     }
    58. }
    Change this line to your own assembly.

    Code (CSharp):
    1. private const string VisibleLine = "[assembly: InternalsVisibleTo(\"BovineLabs.Vision.Editor\")]";
    This disables readonly file permission on the shadergraph AssemblyInfo.cs, then gives your assembly internal access. I use to create my own MasterNodes.

    You can utilize this technique for other packages (HDRP etc) but you need to create the AssemblyInfo if it doesn't have one included.

    This is a hack, use at your own discretion. Unity will at some point change something internal and it will break your code on an update so you will have to update as well.
     
    Last edited: Aug 29, 2019
    andybak likes this.