Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Nokola · Jul 20, 2016 at 12:02 PM · shaderprecisionfloatingpoint

How to force shader to use high-precision floats on mobile?

I have OnePlus One (Adreno 330 GPU.) We're writing an app to do a liquify (displacement map) effect like Photoshop. When running on PC, it looks great. However, on phone the result is the same as if we used half instead of float in our shaders. I wonder - am I missing something? Is there a way to force a shader to use 32-bit floating point? Here's the "apply" shader that applies a disp map on top of an image: Shader "DisplacementMapApply" { Properties { _MainTex("Brush Displacement Texture", 2D) = "white" {}

     _OriginalTex("Original Texture", 2D) = "white" {}
     _OriginalTexWidth("Original Texture Width", Float) = 0
     _OriginalTexHeight("Original Texture Height", Float) = 0

     _DispMapX("Displacement Map X", 2D) = "white" {}
     _DispMapY("Displacement Map Y", 2D) = "white" {}
     
     _OriginalTexStartU("Updated Texture U start", Float) = 0
     _OriginalTexStartV("Updated Texture V start", Float) = 0
     _OriginalTexEndU("Updated Texture U end", Float) = 1
     _OriginalTexEndV("Updated Texture V end", Float) = 1
 }
 
 SubShader {
     Tags
     {
         "Queue" = "Transparent"
         "RenderType" = "Transparent"
         "IgnoreProjector" = "True"
     }

     Blend One Zero
     Lighting Off
     Fog{ Mode Off }
     ZWrite Off
     Cull Off
 
     Pass
     {
         CGPROGRAM
         // use "vert" function as the vertex shader
         #pragma vertex vert
         // use "frag" function as the pixel (fragment) shader
         #pragma fragment frag
         //#pragma enable_d3d11_debug_symbols 

         #include "UnityCG.cginc"

         sampler2D_float _MainTex; 
         float4 _MainTex_ST;
         sampler2D _OriginalTex; 
         sampler2D_float _DispMapX; 
         sampler2D_float _DispMapY; 
         float _OriginalTexStartU;
         float _OriginalTexStartV;
         float _OriginalTexEndU;
         float _OriginalTexEndV;
         float _OriginalTexWidth;
         float _OriginalTexHeight;
 
         struct vin_vct
         {
             float4 vertex : POSITION;
             float2 uv : TEXCOORD0;
         };

         struct v2f_vct
         {
             float4 vertex : POSITION;
             float2 uv : TEXCOORD0;
         };

         v2f_vct vert(vin_vct v)
         {
             v2f_vct o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = v.uv;
             return o;
         }
 
         fixed4 frag(v2f_vct i) : COLOR
         {
             float2 updatedUV;
             updatedUV.x = lerp(_OriginalTexStartU, _OriginalTexEndU, i.uv.x);
             updatedUV.y = lerp(_OriginalTexStartV, _OriginalTexEndV, i.uv.y);
             
             updatedUV.x += tex2D(_DispMapX, i.uv).r / _OriginalTexWidth;
             updatedUV.y += tex2D(_DispMapY, i.uv).r / _OriginalTexHeight;
             updatedUV = clamp(updatedUV, 0.0, 1.0);

             return tex2D(_OriginalTex, updatedUV);
         }

         ENDCG
     }
 }

}

and the displacement shader that "draws" more displacement into the map:

Shader "DisplacementMapDisplace" { Properties { _MainTex("Brush Displacement Texture", 2D) = "white" {} _UnderTheBrushTex("UnderTheBrush Texture", 2D) = "white" {} }

 SubShader {
     Tags
     {
         "Queue" = "Transparent"
         "RenderType" = "Transparent"
         "IgnoreProjector" = "True"
     }

     Blend One Zero
     Lighting Off
     Fog{ Mode Off }
     ZWrite Off
     Cull Off
 
     Pass
     {
         CGPROGRAM
         // use "vert" function as the vertex shader
         #pragma vertex vert
         // use "frag" function as the pixel (fragment) shader
         #pragma fragment frag
         //#pragma enable_d3d11_debug_symbols 

         #include "UnityCG.cginc"

         sampler2D_float _MainTex; 
         sampler2D_float _UnderTheBrushTex; 

         struct vin_vct
         {
             float4 vertex : POSITION;
             float2 uv : TEXCOORD0;
         };

         struct v2f_vct
         {
             float4 vertex : POSITION;
             float2 uv : TEXCOORD0;
         };

         v2f_vct vert(vin_vct v)
         {
             v2f_vct o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = v.uv;
             return o;
         }

         float frag(v2f_vct i) : COLOR
         {
             return (tex2D(_MainTex, i.uv).r + tex2D(_UnderTheBrushTex, i.uv).r);
         }

         ENDCG
     }
 }

}

Any ideas? Thanks!

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
0

Answer by Jessespike · Jul 20, 2016 at 06:14 PM

There's a fragment option that can be set. Not sure if any differences will be noticeable. Still might be worth a try.

 #pragma fragmentoption ARB_precision_hint_nicest

You can verify what precision is being used, by looking at the compiled shader code. Unfortunaly I'm not aware of a way to force a shader to use high-precision floats. I mean, other than specifying float instead of half or fixed.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

fixed half float performance on iOS? 1 Answer

maximum distance from origin (float precision) 1 Answer

Scene scale and float precision 1 Answer

How to force the compilation of a shader in Unity? 5 Answers

Starting mesh generation thread before changing chunk transform position 1 Answer


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