简体   繁体   English

顶点着色器中的纹理查找在iPad设备与iPad模拟器 - OpenGL ES 2.0上的行为有所不同

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

I have a vertex shader in which I do a texture lookup to determine gl_Position. 我有一个顶点着色器,我在其中进行纹理查找以确定gl_Position。 I am using this as part of a GPU particle simulation system, where particle positions are stored in a texture. 我将其用作GPU粒子模拟系统的一部分,其中粒子位置存储在纹理中。

It seems that: vec4 textureValue = texture2D(dataTexture, vec2(1.0, 1.0)); 看来: vec4 textureValue = texture2D(dataTexture, vec2(1.0, 1.0)); behaves differently on the simulator than the iPad device. 在模拟器上的行为与iPad设备不同。 On the simulator, the texture lookup succeeds (the value at that location is 0.5, 0.5) and my particle appears there. 在模拟器上,纹理查找成功(该位置的值为0.5,0.5),我的粒子出现在那里。 However, on the iPad itself the texture lookup is constantly returning 0.0, 0.0. 但是,在iPad本身,纹理查找不断返回0.0,0.0。

I have tried both textures of the format GL_FLOAT and GL_UNSIGNED_BYTE. 我尝试了格式为GL_FLOAT和GL_UNSIGNED_BYTE的纹理。

Has anyone else experienced this? 还有其他人经历过这个吗? The GLSL ES spec says that texture lookups can be done in both the vertex and fragment shaders, so I don't see what the problem is. GLSL ES规范说,纹理查找可以在顶点和片段着色器中完成,所以我看不出问题是什么。

I am using the latest GM Beta of iOS SDK 4.2 我正在使用iOS SDK 4.2的最新GM Beta

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. 使用iOS 4.3,您可以在设备和模拟器上的顶点着色器上进行纹理查找。 There is a bit of strangeness though (which is maybe why it's not "official" as szu mentioned). 虽然有点奇怪(这也许是为什么它不像szu所说的那样“官方”)。 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. 在实际设备上(我在iPad 2上测试过),你必须在片段着色器和顶点着色器上进行查找。 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: 这是一个简单的例子,我在传递纹理并使用红色像素重新定位顶点的y值:

/////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 我在http://www.mat.ucsb.edu/a.forbes/blog/?p=453上对此进行了更为全面的描述

from the official "OpenGL ES Programming Guide for iOS", section "Platform Notes" 来自官方的“OpenGL ES Programming Guide for iOS”,“平台笔记”部分

"You cannot use texture lookups in a vertex shader." “你不能在顶点着色器中使用纹理查找。”

This isn't the only thing that behaves differently in the sim versus the device. 这不是在sim与设备中表现不同的唯一因素。 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. 只测试逻辑,功能等内容,而不是在SIM卡上查看,只有你可以。

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. 我有一种感觉iPad(或iPhone)上的GLES不支持在顶点着色器中查找纹理,但不引用我的话。

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在纹理IIRC之外。

1.0x1.0 in wrapping mode should be 0.0x0.0. 包装模式下的1.0x1.0应为0.0x0.0。 1.0x1.0 in clipping should be the last texel. 削波中的1.0x1.0应该是最后一个纹素。

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. 我自己尝试了这一点,当在顶点着色器中使用时,Texture2D 可以在iOS 4.2下在iPad(设备和模拟器) 工作。

My best guess is that you have mip mapping enabled, and that is the problem. 我最好的猜测是你启用了mip映射,这就是问题所在。 I noticed that mip-mapped lookups in the vertex shader using Texture2D work in the simulator but not on the device. 我注意到使用Texture2D在顶点着色器中进行的mip映射查找在模拟器中工作,但在设备上没有。 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. 您不能在顶点着色器中使用带有Texture2D的mip贴图,因为它无法选择要使用的mip-map级别。 You need to disable mip-mapping for that texture, or use Texture2DLod instead, which supports mip maps. 您需要为该纹理禁用mip-mapping,或者使用支持mip贴图的Texture2DLod。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM