Multisample texture rendering in ios

I am trying to render a texture in ios with multisampling enabled and then use that texture in my final release. Is it possible?

So far, I only got black textures or aliases. The code I'm using is:

glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); glGenRenderbuffers(1, &colorRenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); //glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, width, height); glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_RGBA4, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); glGenRenderbuffers(1, &depthRenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer); //glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, self.view.bounds.size.height); glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ; if(status != GL_FRAMEBUFFER_COMPLETE) { NSLog(@"failed to make complete framebuffer object %x", status); } // Render my scene glBindFramebuffer( GL_FRAMEBUFFER, framebuffer ); glViewport(0,0,width,height); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Draw scene // Then bind default framebuffer glBindFramebuffer( GL_FRAMEBUFFER, 1 ); // Draw other things // Now resolve the multisampling and draw texture glResolveMultisampleFramebufferAPPLE(); glUseTexture( GL_TEXTURE_2D, texture ); // Draw with texture 

This code does not work. It fails if I do a depth buffer multisample. If I just use a normal fbo for depth, then it works, but it creates aliases.

Does anyone know where I am going wrong?

Thanks!

+6
source share
2 answers

Yes! I found myself doing wrong. I mistakenly thought I could have the following:

Framebuffer

  • Multisampled color rendering buffer attached to texture

  • Multisampled Depth Buffer

But you cannot do that. D: You must have the following:

Multisampled framebuffer:

  • Multisampled color rendering buffer (not tied to texture)

  • Multi-Level Depth Visualization Buffer

Normal framebuffer:

  • A color render buffer attached to a texture. This is what glResolveMultisampleFramebufferAPPLE () will be written and that we will use to render the result.

  • No depth buffer.

those. you need to copy the results of multisampled rendering into a completely new framebuffer.

Some codes:

  glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, &resolved_framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, resolved_framebuffer); glGenRenderbuffers(1, &resolvedColorRenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, resolvedColorRenderbuffer); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); glGenFramebuffers(1, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); glGenRenderbuffers(1, &colorRenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_RGBA8_OES, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); glGenRenderbuffers(1, &depthRenderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer); glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ; if(status != GL_FRAMEBUFFER_COMPLETE) { NSLog(@"failed to make complete framebuffer object %x", status); } // Render my scene glBindFramebuffer( GL_FRAMEBUFFER, framebuffer ); glViewport(0,0,width,height); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Draw scene // Then bind default framebuffer glBindFramebuffer( GL_FRAMEBUFFER, 1 ); // Draw other things // Now resolve the multisampling into the other fbo glBindFramebuffer( GL_READ_FRAMEBUFFER_APPLE, framebuffer ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER_APPLE, resolved_framebuffer ); glResolveMultisampleFramebufferAPPLE(); glBindTexture( GL_TEXTURE_2D, texture ); // Draw with texture 

Thank you Goz, you're in the right direction!

+14
source

I assume that you are working with a sample on this page ?

Delete the glFramebufferTexture2D call, as this may cause the multisample visualization buffer to detach, and therefore you have a multisampled feedback buffer and one sample visualization buffer. In addition, creating a single buffer depth buffer will solve your problems, because it will not be paired with one sample render buffer.

Edit: when will you receive the error message? On what creates a visualization buffer? If so, you might be better off trying it in the exact same way as in the link I posted (which I suppose you are working on).

t

 glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_RGBA8_OES, width, height); 
+2
source

Source: https://habr.com/ru/post/897233/


All Articles