Search Unity

Sample2D_Half and unity_Lightmap

Discussion in 'Shaders' started by Jonny-Roy, Oct 2, 2015.

  1. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Does anyone know how to sample the unity_Lightmap as a mediump instead of a lowp, so in the generated code I'm getting:

    Code (CSharp):
    1.  
    2. uniform sampler2D unity_Lightmap;
    3. uniform mediump sampler2D _MainTex;
    4.  
    5. void main ()
    6. {
    7. lowp vec4 tmpvar_1;
    8. tmpvar_1 = texture2D (unity_Lightmap, xlv_TEXCOORD1);
    9.  
    10. mediump vec4 tmpvar_2;
    11. tmpvar_2= texture2D (_MainTex, xlv_TEXCOORD0);
    I really want the unity_Lightmap ones to be the same as _MainTex, my only way of doing this is to write them as GLSL which means having 2 shader versions :S

    Is there a way to override the unity_Lightmap declaration?

    Thanks,

    Jon
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Why do you need mediump light maps?
     
  3. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Performance, they are sampled into a lowp variable then converted to mediump to be applied to the texture, which has quite a hit on low end android devices, so sampling into a mediump would avoid this performance loss.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Okay, I'll give the reverse question then, why do you need _MainTex to be mediump? The definition of lowp is it can accurately reproduce any 8 bit per channel color (max error is 2^-8 = 0.00390625 = 1/256).

    edit: I don't do much android dev anymore, and when I did we didn't use lightmaps, so I haven't had to deal with most of this, but my understanding is just defining things as fixed, half, float get converted to lowp, mediump, highp for GLSL automatically so there's no need to write a separate GLSL version of shaders.
     
  5. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Again performance, math operations on lowp are slower than highp or mediump, and Unity does not allow you to specify lowp as a sampler2D, only half or default which is mediump or highp respectively, so for best performance, it would really need to be mediump.
     
  6. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Okay, so having experimented, it appears the only way to do this is to write 2 sets of shaders, one for direct in HLSL and the other in GLSL for OpenGL, as you can then declare your own unity_Lightmap variables, there doesn't seem to be any way to delcare it via HLSL as it seems to be declared regardless of whether you include the cginc with the declaration or not....very odd.