4

我想IOSurface用 mipmapping 渲染纹理。

我正在生成一个这样的OpenGL纹理IOSurface

let error = CGLTexImageIOSurface2D(context.cglContextObj!, 
GLenum(GL_TEXTURE_RECTANGLE_ARB),GLenum(GL_RGBA), GLsizei(width), GLsizei(height),
GLenum(GL_BGRA), GLenum(GL_UNSIGNED_INT_8_8_8_8_REV), surface!, 0)

并用一个简单的片段着色器渲染它:

#version 120
uniform sampler2DRect texture;
varying vec2 texCoord;
uniform vec2 size;

void main(){
    vec4 tex = texture2DRect(texture, texCoord*size);
    gl_FragColor = tex;
}

这工作正常。但不幸的是,纹理没有经过 mipmap 处理,并且在以较小尺寸查看时非常粗糙/像素化。经过进一步研究,似乎不仅IOSurface支持的纹理仅限于GL_TEXTURE_RECTANGLE_ARB目标,它们也不支持 mipmapping。

好的。所以我最好的选择似乎是:

  1. GL_TEXTURE_RECTANGLE_ARB纹理复制到GL_TEXTURE_2D纹理
  2. 为这个新纹理生成 mipmap

请注意,我是在不支持 OpenGL 4.3 的 Mac 上开发的,所以我不能在这里使用方便glCopyImageSubData。相反,我在这里的第 1 步方法基于这个答案:如何有效地将纹理 1 复制到纹理 2?

我的完整代码现在如下所示:

if !hasCreatedTexture {
    glGenTextures(1, &texture);
    glGenTextures(1, &arbTexture);
    glGenFramebuffers(1, &fbo)
    hasCreatedTexture = true
}
glEnable(GLenum(GL_TEXTURE_RECTANGLE_ARB))
glBindTexture(GLenum(GL_TEXTURE_RECTANGLE_ARB), arbTexture)

let error = CGLTexImageIOSurface2D(context.cglContextObj!, GLenum(GL_TEXTURE_RECTANGLE_ARB), GLenum(GL_RGBA), GLsizei(width), GLsizei(height), GLenum(GL_BGRA), GLenum(GL_UNSIGNED_INT_8_8_8_8_REV), surface!, 0)


glBindFramebuffer(GLenum(GL_FRAMEBUFFER), fbo);
glFramebufferTexture2D(GLenum(GL_READ_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0),
                           GLenum(GL_TEXTURE_RECTANGLE_ARB), arbTexture, 0);
glFramebufferTexture2D(GLenum(GL_DRAW_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT1),
                           GLenum(GL_TEXTURE_2D), texture, 0);
glDrawBuffer(GLenum(GL_COLOR_ATTACHMENT1));
glBlitFramebuffer(0, 0, GLint(width), GLint(height), 0, 0, GLint(width), GLint(height), GLbitfield(GL_COLOR_BUFFER_BIT), GLenum(GL_NEAREST));
glBindTexture(GLenum(GL_TEXTURE_2D), texture)

我改变了我的着色器来支持GL_TEXTURE_2D

#version 120
uniform sampler2D texture;
varying vec2 texCoord;

void main(){
    vec4 tex = texture2D(texture, texCoord);
    gl_FragColor = tex;
}

这就是我卡住的地方。图像全黑,我不知道为什么它不起作用。

我试着像这样查看像素数据:

var pixelData = UnsafeMutablePointer<GLubyte>(allocatingCapacity: Int(width * height * 4))

glGetTexImage(GLenum(GL_TEXTURE_2D), 0, GLenum(GL_BGRA),
GLenum(GL_UNSIGNED_INT_8_8_8_8_REV), pixelData)
print(NSData(bytes: pixelData, length: 50000).description)

而且都是零。我的方法是否有问题,或者问题是否与处理IOSurface支持的纹理特别相关?

4

1 回答 1

4

最后使用以下方法完成了这项工作:

func createMipTexture() {
    guard let surface = self.surface else { return }
    glBindTexture(GLenum(GL_TEXTURE_2D), texture);
    glEnable(GLenum(GL_TEXTURE_2D))

    guard let address = IOSurfaceGetBaseAddress(surface) as UnsafeMutableRawPointer? else { return }
    IOSurfaceLock(surface, .readOnly, nil)
    glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0,  GLenum(GL_BGRA), GLenum(GL_UNSIGNED_INT_8_8_8_8_REV), address)
    IOSurfaceUnlock(surface, .readOnly, nil);
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAX_LEVEL), 100)
    glHint(GLenum(GL_GENERATE_MIPMAP_HINT), GLenum(GL_NICEST));
    glGenerateMipmap(GLenum(GL_TEXTURE_2D))
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GL_REPEAT);
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_T), GL_REPEAT);
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAG_FILTER), GL_LINEAR);
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_LINEAR_MIPMAP_LINEAR);
}
于 2017-04-24T11:00:45.967 回答