bool Framebuffer::assignDepthStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource) { if (isSwapchain()) { return false; } Q_ASSERT(!texture || TextureUsageType::RENDERBUFFER == texture->getUsageType()); // Check for the compatibility of size if (texture) { if (!validateTargetCompatibility(*texture)) { return false; } if (texture->source().empty()) { texture->setSource(_name + "::depthStencil"); } } ++_depthStamp; updateSize(texture); // assign the new one _depthStencilBuffer = TextureView(texture, subresource, format); return true; }
// Render buffers int Framebuffer::setRenderBuffer(uint32 slot, const TexturePointer& texture, uint32 subresource) { if (isSwapchain()) { return -1; } Q_ASSERT(!texture || TextureUsageType::RENDERBUFFER == texture->getUsageType()); // Check for the slot if (slot >= getMaxNumRenderBuffers()) { return -1; } // Check for the compatibility of size if (texture) { if (!validateTargetCompatibility(*texture, subresource)) { return -1; } if (texture->source().empty()) { texture->setSource(_name + "::color::" + std::to_string(slot)); } } ++_colorStamps[slot]; updateSize(texture); // assign the new one _renderBuffers[slot] = TextureView(texture, subresource); // update the mask int mask = (1<<slot); _bufferMask = (_bufferMask & ~(mask)); if (texture) { _bufferMask |= mask; } return slot; }
void update() override { gl::GLTexture* gltexture = nullptr; TexturePointer surface; if (_gpuObject.getColorStamps() != _colorStamps) { if (_gpuObject.hasColor()) { _colorBuffers.clear(); static const GLenum colorAttachments[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, GL_COLOR_ATTACHMENT6, GL_COLOR_ATTACHMENT7, GL_COLOR_ATTACHMENT8, GL_COLOR_ATTACHMENT9, GL_COLOR_ATTACHMENT10, GL_COLOR_ATTACHMENT11, GL_COLOR_ATTACHMENT12, GL_COLOR_ATTACHMENT13, GL_COLOR_ATTACHMENT14, GL_COLOR_ATTACHMENT15 }; int unit = 0; auto backend = _backend.lock(); for (auto& b : _gpuObject.getRenderBuffers()) { surface = b._texture; if (surface) { Q_ASSERT(TextureUsageType::RENDERBUFFER == surface->getUsageType()); gltexture = backend->syncGPUObject(surface); } else { gltexture = nullptr; } if (gltexture) { if (gltexture->_target == GL_TEXTURE_2D) { glNamedFramebufferTexture(_id, colorAttachments[unit], gltexture->_texture, 0); } else if (gltexture->_target == GL_TEXTURE_2D_MULTISAMPLE) { glNamedFramebufferTexture(_id, colorAttachments[unit], gltexture->_texture, 0); } else { glNamedFramebufferTextureLayer(_id, colorAttachments[unit], gltexture->_texture, 0, b._subresource); } _colorBuffers.push_back(colorAttachments[unit]); } else { glNamedFramebufferTexture(_id, colorAttachments[unit], 0, 0); } unit++; } } _colorStamps = _gpuObject.getColorStamps(); } GLenum attachement = GL_DEPTH_STENCIL_ATTACHMENT; if (!_gpuObject.hasStencil()) { attachement = GL_DEPTH_ATTACHMENT; } else if (!_gpuObject.hasDepth()) { attachement = GL_STENCIL_ATTACHMENT; } if (_gpuObject.getDepthStamp() != _depthStamp) { auto surface = _gpuObject.getDepthStencilBuffer(); auto backend = _backend.lock(); if (_gpuObject.hasDepthStencil() && surface) { Q_ASSERT(TextureUsageType::RENDERBUFFER == surface->getUsageType()); gltexture = backend->syncGPUObject(surface); } if (gltexture) { if (gltexture->_target == GL_TEXTURE_2D) { glNamedFramebufferTexture(_id, attachement, gltexture->_texture, 0); } else if (gltexture->_target == GL_TEXTURE_2D_MULTISAMPLE) { glNamedFramebufferTexture(_id, attachement, gltexture->_texture, 0); } else { glNamedFramebufferTextureLayer(_id, attachement, gltexture->_texture, 0, _gpuObject.getDepthStencilBufferSubresource()); } } else { glNamedFramebufferTexture(_id, attachement, 0, 0); } _depthStamp = _gpuObject.getDepthStamp(); } // Last but not least, define where we draw if (!_colorBuffers.empty()) { glNamedFramebufferDrawBuffers(_id, (GLsizei)_colorBuffers.size(), _colorBuffers.data()); } else { glNamedFramebufferDrawBuffer(_id, GL_NONE); } // Now check for completness _status = glCheckNamedFramebufferStatus(_id, GL_DRAW_FRAMEBUFFER); // restore the current framebuffer checkStatus(); }