Texture lookup in vertex shader behaves differently on iPad device vs iPad simulator - OpenGL ES 2.0

后端 未结 5 1458
刺人心
刺人心 2020-12-31 18:07

I have a vertex shader in which I do a texture lookup to determine gl_Position. I am using this as part of a GPU particle simulation system, where particle positions are sto

相关标签:
5条回答
  • 2020-12-31 18:38

    This isn't the only thing that behaves differently in the sim versus the device. I'll make you the same suggestion I make everyone else: Ignore the simulator when you need to test that things look how they should on the device. Only test things like logic, functionality, not look on the sim, and only if you can.

    0 讨论(0)
  • 2020-12-31 18:40

    I have a feeling GLES on the iPad (or iPhone) does not support texture look up in a vertex shader, but don't quote me.

    If it does support texel lookup in vertex shaders, perhaps you have your texture coordinates clipped or wrapping? Because 1.0x1.0 is outside of the texture IIRC.

    1.0x1.0 in wrapping mode should be 0.0x0.0. 1.0x1.0 in clipping should be the last texel.

    0 讨论(0)
  • 2020-12-31 18:55

    from the official "OpenGL ES Programming Guide for iOS", section "Platform Notes"

    "You cannot use texture lookups in a vertex shader."

    0 讨论(0)
  • 2020-12-31 18:55

    I tried this out myself and Texture2D does work on iPad (both device and simulator) under iOS 4.2 when used in the vertex shader.

    My best guess is that you have mip mapping enabled, and that is the problem. I noticed that mip-mapped lookups in the vertex shader using Texture2D work in the simulator but not on the device. You cannot use mip maps with Texture2D in the vertex shader because there is no way for it to select which mip-map level to use. You need to disable mip-mapping for that texture, or use Texture2DLod instead, which supports mip maps.

    0 讨论(0)
  • 2020-12-31 19:02

    I just tested this as well. Using iOS 4.3, you can do a texture lookup on the vertex shader on both the device and the simulator. There is a bit of strangeness though (which is maybe why it's not "official" as szu mentioned). On the actual device (I tested on the iPad 2) you have to do a lookup on the fragment shader as well as on the vertex shader. That is, if you are not actually using it on the fragment shader, you'll still have to reference it in some way. Here's a trivial example where I'm passing in a texture and using the red pixel to reposition the y value of the vertex by a little bit:

    /////fragment shader
    uniform sampler2D tex; //necessary even though not actually used
    
    void main() {
      vec4 notUsed = texture2D(tex, vec2(0.0,0.0)); //necessary even though not actually used
      gl_FragColor = vec4(1.0,1.0,1.0,1.0);
    }
    
    /////vertex shader
    attribute vec4 position;
    attribute vec2 texCoord;   
    uniform sampler2D tex; 
    
    void main() {
      float offset = texture2D(tex, texCoord).x;
      offset = (offset - 0.5) * 2.0; //map 0->1 to -1 to +1
      float posx = position.x;
      float posy = position.y + offset/8.0;
    
      gl_Position = vec4(posx, posy, 0.0, 1.0);  
    }
    

    I have a slightly fuller write-up of this at http://www.mat.ucsb.edu/a.forbes/blog/?p=453

    0 讨论(0)
提交回复
热议问题