- Home /
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!
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.