How do you port old shaders to hlsl so they work with the new post-processing stack?
I recently upgraded my project's graphics pipeline to the new lightweight srp. When it comes to shaders I'm a complete novice. I tried to port several of my old shaders that I had attached to my camera to the new format so they work with the new post-processing stack, following this guide: https://github.com/Unity-Technologies/PostProcessing/wiki/Writing-Custom-Effects. However I'm completely stumped as to what to do exactly. For example I tried to port this publicly available shader:
Shader "Hidden/Pixelation"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float2 BlockCount;
float2 BlockSize;
fixed4 frag (v2f_img i) : SV_Target
{
float2 blockPos = floor(i.uv * BlockCount);
float2 blockCenter = blockPos * BlockSize + BlockSize * 0.5;
float4 tex = tex2D(_MainTex, blockCenter);
return tex;
}
ENDCG
}
}
}
And this was my attempt:
Shader "Hidden/Custom/Pixelisation"
{
HLSLINCLUDE
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
float2 BlockCount;
float2 BlockSize;
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment Frag
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
struct appdata
{
float4 vertex:POSITION;
float2 uv:TEXCOORD0;
};
struct v2f
{
float2 uv:TEXCOORD0;
float4 vertex:SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex=UnityObjectToClipPos(v.vertex);
o.uv=v.uv;
return o;
}
float4 Frag(v2f i) : SV_Target
{
float2 blockPos = floor(i.uv * BlockCount);
float2 blockCenter = blockPos * BlockSize + BlockSize * 0.5;
float4 tex = tex2D(_MainTex, blockCenter);
return tex;
}
ENDHLSL
}
}
}
here it throws an error on UnityObjectToClipPos, saying it's an undeclared identifier.
Can someone tell me where I'm going wrong? Or maybe point me in the right direction on this? I'm completely lost here.
Answer by mhwan97 · Sep 05, 2019 at 04:34 AM
@SpasticDad I know this answer is very late and you've probably solved your problem by now, but as there are no responses and it might help others:
The error you're getting is because UnityObjectToClipPos is a helper function defined in UnityCG.cginc, which you shouldn't (can't?) use in PPv2 and SRP's. I'm not sure if there's an equivalent function in stdlib.hlsl, which is the helper include file for PPv2 because I'm not too good with vertex shaders myself, but if you look here: https://docs.unity3d.com/Manual/SL-BuiltinFunctions.html you can see that you could probably write your own version of UnityObjectToClipPos. Otherwise look through the stdlib.hlsl file here: https://github.com/Unity-Technologies/PostProcessing/blob/v2/PostProcessing/Shaders/StdLib.hlsl to see if it has what you need.
As for porting old CG shaders to PPv2 HLSL shaders, the code you've posted has the right idea. You need to convert all CG blocks into HLSL blocks, use stdlib.hlsl instead of UnityCG.cginc, (and do the necessary cleanup that implies), and you will need to convert your scripts from a MonoBehaviour to a PostProcessingEffectRenderer. I'd recommend looking at the PPv2 documentation to see what that looks like: https://docs.unity3d.com/Packages/com.unity.postprocessing@2.1/manual/Writing-Custom-Effects.html
Hope this helps somebody, nothing worse than see a question you need answered come up on Google and its unanswered when you click in.
Thanks for the help mhwan97! Just like OP, I am a total amateur in shaders. I changed
o.vertex=UnityObjectToClipPos(v.vertex);
To
v2f vert(appdata v)
{
v2f o;
o.vertex=mul(mul(unity_$$anonymous$$atrixVP, unity_ObjectToWorld), (v.vertex));
o.uv=v.uv;
return o;
}
Which is basically the definition of UnityObjectToClipPos
And now I get:
'tex2D': no matching 2 parameter intrinsic function; Possible intrinsic functions are: tex2D(sampler2D, float2|half2|$$anonymous$$10float2|$$anonymous$$16float2) tex2D(sampler2D, float2|half2|$$anonymous$$10float2|$$anonymous$$16float2, float2|half2|$$anonymous$$10float2|$$anonymous$$16float2, float2|half2|$$anonymous$$10float2|$$anonymous$$16float2) at line 52 (on d3d11)
Any idea on what to do? Thanks!
@Zellator Sorry for the late reply, I don't seem to get notifications for this :/ You havn't given much context but that vertex shader seems fine, especially if you based it on UnityObjectToClipPos. The error you're getting seems unrelated to the vert shader from what I can see - it seems to me the error is saying you've made an invalid texture read call with tex2D, by giving it incorrect arguments. If you're working with PPv2 so you can't use UnityCG.cginc then you should use the texture read macros that are defined in the API hlsl files: https://github.com/Unity-Technologies/PostProcessing/tree/v2/PostProcessing/Shaders/API
Hope this helps and isn't too late
Your answer
Follow this Question
Related Questions
Broken HDRP Lit and Unlit Shaders 0 Answers
Shaders don't work properly on android 0 Answers
Blur shader not working after converting render Pipeline 0 Answers
Why do I get black screen in editor when using custom render feature with URP? 0 Answers
Making a Kuwahara Filter Post process Shader (I'm using HDRP) 0 Answers