// Compile with: // c++ layered_renderbuffer.cc $(pkg-config --cflags --libs sdl2 gl) #define GL_GLEXT_PROTOTYPES 1 #include #include #include int main() { if (SDL_Init(SDL_INIT_VIDEO) < 0) assert(false); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_Window * const window = SDL_CreateWindow( "", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL); assert(window); const SDL_GLContext context = SDL_GL_CreateContext(window); assert(context); GLuint framebuffer; glGenFramebuffers(1, &framebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D_ARRAY, texture); glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, 640, 480, 2); GLuint renderbuffers[2]; glGenRenderbuffers(2, &renderbuffers[0]); glBindRenderbuffer(GL_RENDERBUFFER, renderbuffers[0]); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 640, 480); glBindRenderbuffer(GL_RENDERBUFFER, renderbuffers[1]); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 640, 480); // This makes GL_COLOR_ATTACHMENT0 layered. glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0); // Replace the binding on GL_COLOR_ATTACHMENT0. Renderbuffers don't // have layred state, but currently the layered state is preserved // from the previously bound texture and this is used for framebuffer // completeness tests. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffers[0]); // This is not layered, so it conflicts with the incorrectly layered // GL_COLOR_ATTACHMENT0. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffers[1]); // The draw framebuffer should be framebuffer complete now. const GLenum status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); assert(glGetError() == GL_NO_ERROR); glClear(GL_COLOR_BUFFER_BIT); const GLenum error = glGetError(); assert(status == GL_FRAMEBUFFER_COMPLETE); assert(glGetError() == GL_NO_ERROR); }