Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by SpasticDad · Dec 28, 2018 at 03:56 PM · shaderspost processingcgpost-process-effecthlsl

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zellator · Sep 20, 2019 at 08:59 PM 0
Share

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!

avatar image mhwan97 Zellator · Oct 04, 2019 at 11:49 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a Kuwahara Filter Post process Shader (I'm using HDRP) 0 Answers

When im trying build with post processing unity crashes 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges