Search Unity

Question How to include Commom.hlsl in custom HLSL shader?

Discussion in 'Shaders' started by Chahorain, Feb 23, 2022.

  1. Chahorain

    Chahorain

    Joined:
    Dec 20, 2017
    Posts:
    25
    upload_2022-2-23_13-52-19.png
    I tried to include it inside a simple custom HLSL shader, but it keeps giving me error, what could be the problem?
     
  2. Chahorain

    Chahorain

    Joined:
    Dec 20, 2017
    Posts:
    25
    I checked with the package, the common file is there, and my spelling is correct
     
  3. kobsyy

    kobsyy

    Joined:
    Oct 28, 2020
    Posts:
    2
  4. Chahorain

    Chahorain

    Joined:
    Dec 20, 2017
    Posts:
    25
    this is exactly where i stopped from searching. the last reply in that post was saying to change the route, but i tried to put the Common.hlsl in front of other paths, or just simply put #include "Common.hlsl". None of the methods works for me. Do you have any idea or any tried methods to share?
     
  5. kobsyy

    kobsyy

    Joined:
    Oct 28, 2020
    Posts:
    2
    i only have some suggestions so far, in my case i think it has simply to do something with the unity and pipeline version in the first place, the custom shader i want to use works well in urp version 10+, and its recognize the #include correct, so maybe it should simply contain the correct including method/syntax for the specific versions or something like this waste of time thing.. , unity is really something of funkiller sometimes :b

    i'll keep trying my best to resolve and when i have something i'll let you know
     
  6. Chahorain

    Chahorain

    Joined:
    Dec 20, 2017
    Posts:
    25
    how exactly you wrote the include method in your working hlsl file? something like how did you include common.hlsl or core.hlsl?
     
  7. ekakiya

    ekakiya

    Joined:
    Jul 25, 2011
    Posts:
    79
    CUBUFFER_START(UnityPerFrame) must be CBUFFER_START(UnityPerFrame).
    I'm using the same Common.hlsl path and it's working for me.
     
  8. Chahorain

    Chahorain

    Joined:
    Dec 20, 2017
    Posts:
    25
    yeah that was my typo, after i correcting it i still cannot successfully include the common file. do you mind to share a basic hlsl file to show how you include the common.hlsl? i need to fix this desperately.
     
  9. ekakiya

    ekakiya

    Joined:
    Jul 25, 2011
    Posts:
    79
    Ok but it's just .. same path.
    Code (CSharp):
    1. #ifndef MY_CORE_INCLUDED
    2. #define MY_CORE_INCLUDED
    3.  
    4. // I'm using camera relative world space
    5. #define SHADEROPTIONS_CAMERA_RELATIVE_RENDERING (1)
    6. #define MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING
    7.  
    8. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    9.  
    10. CBUFFER_START(UnityPerDraw)
    11.     float4x4 unity_ObjectToWorld;
    12.     float4x4 unity_WorldToObject;
    13.     float4   unity_LODFade;
    14.     float4   unity_WorldTransformParams;
    15.  
    16.     float4   unity_RenderingLayer;
    17. CBUFFER_END
    18.  
    19. CBUFFER_START(UnityPerCamera)
    20.     float4x4 _ViewMatrix;
    21.     float4x4 _InvViewMatrix;
    22.     float4x4 _ProjMatrix;
    23.     float4x4 _InvProjMatrix;
    24.     float4x4 _ViewProjMatrix;
    25.     float4x4 _InvViewProjMatrix;
    26.  
    27.     float4 _ProjectionParams;
    28.     float4 _ScreenParams;
    29.     float4 _ZBufferParams;
    30.     float4 unity_OrthoParams;
    31. CBUFFER_END
    32.  
    33. #define UNITY_MATRIX_M     ApplyCameraTranslationToMatrix(unity_ObjectToWorld)
    34. #define UNITY_MATRIX_I_M   ApplyCameraTranslationToInverseMatrix(unity_WorldToObject)
    35. #define UNITY_MATRIX_V     _ViewMatrix
    36. #define UNITY_MATRIX_I_V   _InvViewMatrix
    37. #define UNITY_MATRIX_P     OptimizeProjectionMatrix(_ProjMatrix)
    38. #define UNITY_MATRIX_I_P   _InvProjMatrix
    39. #define UNITY_MATRIX_VP    _ViewProjMatrix
    40. #define UNITY_MATRIX_I_VP  _InvViewProjMatrix
    41.  
    42. #include "Instancing.hlsl"
    43.  
    44. #define CB_CAMERA_POS_AWS   _WorldSpaceCameraPos.xyz
    45.  
    46. float4x4 ApplyCameraTranslationToMatrix(float4x4 modelMatrix)
    47. {
    48. #if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
    49.     modelMatrix._m03_m13_m23 -= CB_CAMERA_POS_AWS;
    50. #endif
    51.     return modelMatrix;
    52. }
    53.  
    54. float4x4 ApplyCameraTranslationToInverseMatrix(float4x4 inverseModelMatrix)
    55. {
    56. #if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
    57.     float4x4 translationMatrix = { { 1.0, 0.0, 0.0, CB_CAMERA_POS_AWS.x }, { 0.0, 1.0, 0.0, CB_CAMERA_POS_AWS.y }, { 0.0, 0.0, 1.0, CB_CAMERA_POS_AWS.z }, { 0.0, 0.0, 0.0, 1.0 } };
    58.     return mul(inverseModelMatrix, translationMatrix);
    59. #else
    60.     return inverseModelMatrix;
    61. #endif
    62. }
    63.  
    64. #endif
    65.  
     
    AxeManDan likes this.