Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Vertex displacement: tex2Dlod alternatives for GLES 2.0?

Discussion in 'Shaders' started by Boercam, Jun 19, 2015.

  1. Boercam

    Boercam

    Joined:
    Feb 25, 2015
    Posts:
    6
    Hi all,

    I'm using the volumetric explosion shader created by Steve Craeynest (full shader located here). The shader uses tex2Dlod to sample the displacement texture, and I've found this doesn't work on my android device (which is on GLES 2.0). From my research it seems tex2Dlod is not supported (bar using an extension). The troublesome code is below:
    Code (CSharp):
    1. void disp (inout appdata_full v)
    2. {
    3.     float3 dcolor = tex2Dlod (_DispTex, float4(v.texcoord.xy,0,0));
    4.     float d = (dcolor.r*_ChannelFactor.r + dcolor.g*_ChannelFactor.g + dcolor.b*_ChannelFactor.b);
    5.     v.vertex.xyz += v.normal * d * _Displacement;
    6. }
    Does anyone have any recommendations on how I can achieve a similar result without using tex2Dlod? I should note that I'm quite a shader novice so I really appreciate any help you can give, thanks.
     
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602
    use just tex2d AND make sure your texture dont have mips. Unless i miss smth funny (i can) that should be equivalent
     
  3. Boercam

    Boercam

    Joined:
    Feb 25, 2015
    Posts:
    6
    Not sure what is going wrong, using:
    Code (CSharp):
    1. float4 dcolor = tex2D(_DispTex, v.texcoord.xy);
    Getting error:
    Edit:
    I've found various posts that suggest that tex2D cannot be used for a vertex texture fetch - would that be why? Any other alternatives?
     
    Last edited: Jun 20, 2015
  4. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602
    oh, well for dx11 it works different. If you need d3d11 support use UNITY_SAMPLE_TEX2D (declared in HLSLSupport.cginc)
     
  5. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    As far as I know, it's hardware support. The hardware, quite literally, does not support texture fetches in the vertex pipeline. Just keep in mind that GPU programming is vastly different from CPU programming.

    And tex2D cannot be used for a texture fetch, because tex2D calculates the mip levels for the pixel, and the vertex doesn't have the information that is used to calculate the mip level, so you have to select a mip level manually (via tex2Dlod).

    I'm fairly sure that what you want is not possible without rethinking the whole concept (not positive, but fairly sure), or by limiting it to higher end devices.
     
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,602
    Oh, true that, i totally missed that point.
    But yes, in general gles2 dont have texture fetch in vprog generally, so tough luck here
     
  7. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    You could replace the texture lookup with a noise function, but this will likely be too slow unless your explosions only have a few verts. The other option is to animate the verts on the CPU.
     
  8. Propagant

    Propagant

    Joined:
    Nov 18, 2013
    Posts:
    32
    I know this forum is already dead, but I'm just wondering. Is there any solution for using tex2Dlod for android? Or it's just unsupported for this platform?
     
  9. Dorodo

    Dorodo

    Joined:
    Mar 8, 2015
    Posts:
    43
    Tex2DLod is supported on Android as long as the device supports GL ES 3.0. If you need to target older versions, there's no workaround other than what JBooth mentioned above. Note that if you want to read the texture on the CPU it will be a lot slower.
     
    Propagant likes this.
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Step 1: Add
    #pragma target 3.0
    to your shader pass.
    Step 2: Use
    tex2Dlod()
    in your vertex function.
    Step 3: There is no step 3.

    That “target” was originally the Shader Model version the shader needed to support. This is a little messy since OpenGL doesn’t adhere to explicit Shader Model versions as that’s a Direct3D thing, and there are no “half” versions. In fact Unity has no explicit OpenGL ES 2.0 target. Unity shaders default to
    #pragma target 2.5
    , which isn’t explicitly GLES 2.0, but rather “DX11 feature level 9.3” which was used on Windows Phone OS devices and is essentially OpenGL ES 2.0, but slightly worse, and is definitely worse than the Direct3D 9.0c that Unity supported on PC at the time. The problem is there’s a very wide range of features that “OpenGL ES 2.0” devices may or may not support. Some later OpenGL ES 2.0 devices might support
    tex2Dlod()
    without explicitly being fully GLES 3.0 compatible.

    That
    #pragma target 3.0
    is Shader Model 3.0, which added
    tex2Dlod()
    support (among other things) and may, or may not run on OpenGL ES 2.0 devices depending on if it happens to support it or not.

    If you need to allow the game to run on older OpenGL ES 2.0 devices, then the only practical option is to deform the mesh via C#.
     
    Propagant likes this.