ios - GL_HALF_FLOAT_OES 和 glReadPixels

标签 ios glsl fragment-shader

当我使用 GL_HALF_FLOAT_OES 纹理时;片段着色器似乎可以很好地写入它,当我在 xCode 中使用 gpuTrace 时,我可以观察到它具有正确的值。但是当我使用

读回它时
glReadPixels( 0, 0, self.imageSize->v[0], self.imageSize->v[1], GL_RGBA, type, pixels );

像素分量要么 float -0 要么 float 1。这是因为 float 值被意外剪切了吗?有没有办法读回最初写入的浮点值?如果我使用 GL_RGBA16F_EXTGL_RGBA32F_EXT 而不是 GL_RGBA 我会得到无效的枚举。

基于 Rabbid76 的反馈

int formatExt, typeExt;
glGetIntegerv( GL_IMPLEMENTATION_COLOR_READ_FORMAT, &formatExt );
glGetIntegerv( GL_IMPLEMENTATION_COLOR_READ_TYPE, &typeExt );

[ self traceLog:[ NSString stringWithFormat:@"Implementation format is %@, type is %@", [ TextureImage strForFormat:formatExt ], [ TextureImage strForType:typeExt ] ] ];

返回值是“实现格式为 GL_RGBA,类型为 GL_HALF_FLOAT_OES”。这可能意味着 HALF_FLOAT 是它能够读回的唯一值。我仍在调查它是否有可能读回 GL_FLOAT,因为尽管 glGetIntegerv 返回了什么,glReadPixels 不会用无效枚举标记 GL_FLOAT;它接受并处理它,但 float 似乎不是正确的 float ;所以它似乎在进行一些意想不到的转换。

最佳答案

请参阅 glReadPixels (OpenGL ES 3.1) 的文档其中说:

void glReadPixels( GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid * data);

format
Specifies the format of the pixel data. The following symbolic values are accepted: GL_RGBA, and GL_RGBA_INTEGER. An implementation-chosen format will also be accepted. This can be queried with glGet and GL_IMPLEMENTATION_COLOR_READ_FORMAT.

type
Specifies the data type of the pixel data. Must be one of GL_UNSIGNED_BYTE, GL_UNSIGNED_INT, GL_UNSIGNED_INT_2_10_10_10_REV, GL_INT, or GL_FLOAT. An implementation-chosen type will also be accepted. This can be queried with glGet and GL_IMPLEMENTATION_COLOR_READ_TYPE.


这意味着您必须读取实现选择的 formattype,以读回最初写入的浮点值:

int format = glGet(GL_IMPLEMENTATION_COLOR_READ_FORMAT);
int type   = glGet(GL_IMPLEMENTATION_COLOR_READ_TYPE);
glReadPixels( 
    0, 0, self.imageSize->v[0], self.imageSize->v[1],
    format, type, pixels );


请参阅 glGet (OpenGL ES 3.1) 的文档:

GL_IMPLEMENTATION_COLOR_READ_FORMAT params returns one value, the format chosen by the implementation in which pixels may be read from the color buffer of the currently bound framebuffer.

GL_IMPLEMENTATION_COLOR_READ_TYPE params returns one value, the type chosen by the implementation with which pixels may be read from the color buffer of the currently bound framebuffer.

关于ios - GL_HALF_FLOAT_OES 和 glReadPixels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46626336/

相关文章:

ios - XCDYouTubeVideoPlayer 文件是为存档构建的,它不是被链接的体系结构

ios - iOS 中 NSURLConnection 的间歇性 SSL 错误

c++ - GLM 如何处理翻译

c# - 通过比较另一个图像的强度来改变图像的强度 - OpenTK

c++ - OpenGL 代码在一台计算机上运行缓慢(但在其他计算机上则不然)

ios - 将 SCNGeometry 渲染为线框

iOS:使用 NSUrlConnection 使用 RESTFUL Web 服务时出现 415(不支持的媒体类型)错误

c++ - 是什么导致我的光线追踪器中出现伪影?

java - GLSL Shader 无法编译,Java 和 LWJGL 着色器错误

c# - 将本地 CSS 文件加载到 Xamarin IOS WebView 中?