How to include Commom.hlsl in custom HLSL shader?


I tried to include it inside a simple custom HLSL shader, but it keeps giving me error, what could be the problem?

I checked with the package, the common file is there, and my spelling is correct

i have the same issue
what i found so far was following post:

but I have not got really smart from it yet

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?

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

how exactly you wrote the include method in your working hlsl file? something like how did you include common.hlsl or core.hlsl?

CUBUFFER_START(UnityPerFrame) must be CBUFFER_START(UnityPerFrame).
I’m using the same Common.hlsl path and it’s working for me.

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.

Ok but it’s just … same path.

#ifndef MY_CORE_INCLUDED
#define MY_CORE_INCLUDED

// I'm using camera relative world space
#define SHADEROPTIONS_CAMERA_RELATIVE_RENDERING (1)
#define MODIFY_MATRIX_FOR_CAMERA_RELATIVE_RENDERING

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"

CBUFFER_START(UnityPerDraw)
    float4x4 unity_ObjectToWorld;
    float4x4 unity_WorldToObject;
    float4   unity_LODFade;
    float4   unity_WorldTransformParams;

    float4   unity_RenderingLayer;
CBUFFER_END

CBUFFER_START(UnityPerCamera)
    float4x4 _ViewMatrix;
    float4x4 _InvViewMatrix;
    float4x4 _ProjMatrix;
    float4x4 _InvProjMatrix;
    float4x4 _ViewProjMatrix;
    float4x4 _InvViewProjMatrix;

    float4 _ProjectionParams;
    float4 _ScreenParams;
    float4 _ZBufferParams;
    float4 unity_OrthoParams;
CBUFFER_END

#define UNITY_MATRIX_M     ApplyCameraTranslationToMatrix(unity_ObjectToWorld)
#define UNITY_MATRIX_I_M   ApplyCameraTranslationToInverseMatrix(unity_WorldToObject)
#define UNITY_MATRIX_V     _ViewMatrix
#define UNITY_MATRIX_I_V   _InvViewMatrix
#define UNITY_MATRIX_P     OptimizeProjectionMatrix(_ProjMatrix)
#define UNITY_MATRIX_I_P   _InvProjMatrix
#define UNITY_MATRIX_VP    _ViewProjMatrix
#define UNITY_MATRIX_I_VP  _InvViewProjMatrix

#include "Instancing.hlsl"

#define CB_CAMERA_POS_AWS   _WorldSpaceCameraPos.xyz

float4x4 ApplyCameraTranslationToMatrix(float4x4 modelMatrix)
{
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
    modelMatrix._m03_m13_m23 -= CB_CAMERA_POS_AWS;
#endif
    return modelMatrix;
}

float4x4 ApplyCameraTranslationToInverseMatrix(float4x4 inverseModelMatrix)
{
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
    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 } };
    return mul(inverseModelMatrix, translationMatrix);
#else
    return inverseModelMatrix;
#endif
}

#endif
1 Like