Search Unity

OpenGLES2 and fixed1 mul(fixed4 v, fixed4x1 M)

Discussion in 'Shaders' started by mpiastowski, Mar 22, 2017.

  1. mpiastowski

    mpiastowski

    Joined:
    Jun 16, 2015
    Posts:
    6
    Hi

    I want to multiply two vectors (one transposed) and get scalar value.
    Function fixed1 mul(fixed4 v, fixed4x1 M) is described here: http://http.developer.nvidia.com/Cg/mul.html
    It works fine on Android phone with OpenGLES3 API, but when I switch to OpenGLES2 (in PlayerSettings/Other Settings/Graphics API) my shader doesn't work on Android device.

    I made very simple shader, to prove this is the problem:
    Code (csharp):
    1. Shader "my/test" {
    2.  
    3.   Properties {
    4.   }
    5.   SubShader{
    6.     Tags {
    7.       "LightMode" = "ForwardBase"
    8.     }
    9.     Pass {
    10.       Cull Off
    11.        
    12.       CGPROGRAM
    13.      
    14.       #pragma vertex   vert
    15.       #pragma fragment frag
    16.      
    17.       struct VertIn {
    18.         fixed4 posM : POSITION;
    19.         fixed3 norm : NORMAL;
    20.         fixed4 uv   : TEXCOORD0;
    21.       };
    22.      
    23.       struct FragIn {
    24.         fixed4 posP   : SV_POSITION;
    25.       };
    26.      
    27.       FragIn vert ( VertIn i ) {
    28.         FragIn o;
    29.         o.posP       = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, i.posM));
    30.         fixed test   = mul(fixed4(0,1,2,3),fixed4(1,0,2,1));
    31.         return o;
    32.       }
    33.      
    34.       float4 frag (FragIn i) : COLOR {
    35.         return fixed4(1,1,1,1);
    36.       }
    37.      
    38.       ENDCG
    39.     }
    40.   }
    41.   //Fallback "Diffuse"
    42. }
    43.  
    As you can see, it's super simple and returns white (fixed4(1,1,1,1)) for all fragments.
    For some reason on Android device with OpenGLES2 it's pink.
    It's white, when I comment line
    Code (csharp):
    1. fixed test   = mul(fixed4(0,1,2,3),fixed4(1,0,2,1));
    What am I doing wrong?

    Maybe I shouldn't use fixed4 like fixed1x4, but, to be honest, I don't know how to declare fixed1x4 value.
    I tried fixed1x4(1,2,3,4) and transpose(fixed4(1,2,3,4)) and both do not compile.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    OpenGLES 2.0 doesn't support non-square, non-power of 2 matrices. However it sounds like what you actually want is a dot product.

    dot(fixed4(1,2,3,4),fixed4(1,2,3,4));
     
  3. mpiastowski

    mpiastowski

    Joined:
    Jun 16, 2015
    Posts:
    6
    Thank you
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,741
    I'm struggling to find link for this, so I'm going by memory, but:

    While for most devices fixed and half are the same thing, some (older) devices, treat fixed (lowp) differently and it's generally clamped to -2 and 2, so you may get unpredictable results if you use values higher than that.
     
    bgolus likes this.