bool EGL2Framebuffer::bindRenderTexture(GraphicsTexturePtr texture, GLenum attachment, GLint level, GLint layer) noexcept { assert(texture); auto gltexture = texture->downcast<EGL2Texture>(); auto handle = gltexture->getInstanceID(); auto target = gltexture->getTarget(); if (target != GL_TEXTURE_2D && target != GL_TEXTURE_2D_MULTISAMPLE && target != GL_TEXTURE_CUBE_MAP) { GL_PLATFORM_LOG("Invalid texture target"); return false; } GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, target, handle, 0)); return true; }
void EGL2DescriptorSet::apply(const EGL2Program& shaderObject) noexcept { for (auto& it : _activeUniformSets) { auto type = it->getGraphicsParam()->getType(); auto location = it->getGraphicsParam()->getBindingPoint(); switch (type) { case GraphicsUniformType::GraphicsUniformTypeBool: GL_CHECK(glUniform1i(location, it->getBool())); break; case GraphicsUniformType::GraphicsUniformTypeInt: GL_CHECK(glUniform1i(location, it->getInt())); break; case GraphicsUniformType::GraphicsUniformTypeInt2: GL_CHECK(glUniform2iv(location, 1, (GLint*)it->getInt2().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeInt3: GL_CHECK(glUniform3iv(location, 1, (GLint*)it->getInt3().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeInt4: GL_CHECK(glUniform4iv(location, 1, (GLint*)it->getInt4().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeFloat: GL_CHECK(glUniform1f(location, it->getFloat())); break; case GraphicsUniformType::GraphicsUniformTypeFloat2: GL_CHECK(glUniform2fv(location, 1, it->getFloat2().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeFloat3: GL_CHECK(glUniform3fv(location, 1, it->getFloat3().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeFloat4: GL_CHECK(glUniform4fv(location, 1, it->getFloat4().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeFloat3x3: GL_CHECK(glUniformMatrix3fv(location, 1, GL_FALSE, it->getFloat3x3().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeFloat4x4: GL_CHECK(glUniformMatrix4fv(location, 1, GL_FALSE, it->getFloat4x4().ptr())); break; case GraphicsUniformType::GraphicsUniformTypeIntArray: GL_CHECK(glUniform1iv(location, it->getIntArray().size(), it->getIntArray().data())); break; case GraphicsUniformType::GraphicsUniformTypeInt2Array: GL_CHECK(glUniform2iv(location, it->getInt2Array().size(), (GLint*)it->getInt2Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeInt3Array: GL_CHECK(glUniform3iv(location, it->getInt3Array().size(), (GLint*)it->getInt3Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeInt4Array: GL_CHECK(glUniform4iv(location, it->getInt4Array().size(), (GLint*)it->getInt4Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeFloatArray: GL_CHECK(glUniform1fv(location, it->getFloatArray().size(), (GLfloat*)it->getFloatArray().data())); break; case GraphicsUniformType::GraphicsUniformTypeFloat2Array: GL_CHECK(glUniform2fv(location, it->getFloat2Array().size(), (GLfloat*)it->getFloat2Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeFloat3Array: GL_CHECK(glUniform3fv(location, it->getFloat3Array().size(), (GLfloat*)it->getFloat3Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeFloat4Array: GL_CHECK(glUniform4fv(location, it->getFloat4Array().size(), (GLfloat*)it->getFloat4Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeFloat3x3Array: GL_CHECK(glUniformMatrix3fv(location, it->getFloat3x3Array().size(), GL_FALSE, (GLfloat*)it->getFloat3x3Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeFloat4x4Array: GL_CHECK(glUniformMatrix4fv(location, it->getFloat4x4Array().size(), GL_FALSE, (GLfloat*)it->getFloat4x4Array().data())); break; case GraphicsUniformType::GraphicsUniformTypeSampler: assert(false); break; case GraphicsUniformType::GraphicsUniformTypeSamplerImage: { auto& texture = it->getTexture(); if (texture) { auto gltexture = texture->downcast<EGL2Texture>(); GL_CHECK(glActiveTexture(GL_TEXTURE0 + location)); GL_CHECK(glBindTexture(gltexture->getTarget(), gltexture->getInstanceID())); } } break; case GraphicsUniformType::GraphicsUniformTypeCombinedImageSampler: { auto& texture = it->getTexture(); if (texture) { auto gltexture = texture->downcast<EGL2Texture>(); GL_CHECK(glActiveTexture(GL_TEXTURE0 + location)); GL_CHECK(glBindTexture(gltexture->getTarget(), gltexture->getInstanceID())); } } break; case GraphicsUniformType::GraphicsUniformTypeStorageImage: { auto& texture = it->getTexture(); if (texture) { auto gltexture = texture->downcast<EGL2Texture>(); GL_CHECK(glActiveTexture(GL_TEXTURE0 + location)); GL_CHECK(glBindTexture(gltexture->getTarget(), gltexture->getInstanceID())); } } break; case GraphicsUniformType::GraphicsUniformTypeStorageTexelBuffer: case GraphicsUniformType::GraphicsUniformTypeUniformTexelBuffer: GL_PLATFORM_LOG("Can't support TBO."); break; case GraphicsUniformType::GraphicsUniformTypeStorageBuffer: case GraphicsUniformType::GraphicsUniformTypeStorageBufferDynamic: case GraphicsUniformType::GraphicsUniformTypeUniformBuffer: case GraphicsUniformType::GraphicsUniformTypeUniformBufferDynamic: GL_PLATFORM_LOG("Can't support UBO."); break; case GraphicsUniformType::GraphicsUniformTypeInputAttachment: break; case GraphicsUniformType::GraphicsUniformTypeUInt: case GraphicsUniformType::GraphicsUniformTypeUInt2: case GraphicsUniformType::GraphicsUniformTypeUInt3: case GraphicsUniformType::GraphicsUniformTypeUInt4: case GraphicsUniformType::GraphicsUniformTypeUIntArray: case GraphicsUniformType::GraphicsUniformTypeUInt2Array: case GraphicsUniformType::GraphicsUniformTypeUInt3Array: case GraphicsUniformType::GraphicsUniformTypeUInt4Array: GL_PLATFORM_LOG("Can't support unsigned type."); break; default: break; } } }
bool EGL2Framebuffer::setup(const GraphicsFramebufferDesc& framebufferDesc) noexcept { assert(GL_NONE == _fbo); assert(framebufferDesc.getGraphicsFramebufferLayout()); assert(framebufferDesc.getWidth() > 0 && framebufferDesc.getHeight() > 0); std::uint32_t numAttachment = framebufferDesc.getColorAttachments().size(); if (numAttachment > 1) { GL_PLATFORM_LOG("Can't support multi framebuffer"); return false; } GL_CHECK(glGenFramebuffers(1, &_fbo)); if (_fbo == GL_NONE) { GL_PLATFORM_LOG("glCreateFramebuffers() fail"); return false; } GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, _fbo)); const auto& textureComponents = framebufferDesc.getGraphicsFramebufferLayout()->getGraphicsFramebufferLayoutDesc().getComponents(); const auto& colorAttachments = framebufferDesc.getColorAttachments(); for (std::size_t i = 0; i < textureComponents.size(); i++) { auto type = textureComponents[i].getAttachType(); switch (type) { case GraphicsImageLayout::GraphicsImageLayoutGeneral: break; case GraphicsImageLayout::GraphicsImageLayoutColorAttachmentOptimal: { GLint slot = GL_COLOR_ATTACHMENT0 + textureComponents[i].getAttachSlot(); GLint mipLevel = colorAttachments[0].getBindingLevel(); GLint layer = colorAttachments[0].getBindingLayer(); if (!this->bindRenderTexture(colorAttachments[0].getBindingTexture(), slot, mipLevel, layer)) return false; } break; case GraphicsImageLayout::GraphicsImageLayoutDepthStencilAttachmentOptimal: case GraphicsImageLayout::GraphicsImageLayoutDepthStencilReadOnlyOptimal: { const auto& depthStencilAttachment = framebufferDesc.getDepthStencilAttachment(); if (!depthStencilAttachment.getBindingTexture()) { GL_PLATFORM_LOG("Need depth or stencil texture."); return false; } auto texture = depthStencilAttachment.getBindingTexture(); auto format = texture->getGraphicsTextureDesc().getTexFormat(); auto level = depthStencilAttachment.getBindingLevel(); auto layer = depthStencilAttachment.getBindingLayer(); if (EGL2Types::isDepthFormat(format)) { if (!this->bindRenderTexture(texture, GL_DEPTH_ATTACHMENT, level, layer)) return false; } else if (EGL2Types::isStencilFormat(format)) { if (!this->bindRenderTexture(texture, GL_STENCIL_ATTACHMENT, level, layer)) return false; } else { GL_PLATFORM_LOG("Invalid texture format"); return false; } } case GraphicsImageLayout::GraphicsImageLayoutShaderReadOnlyOptimal: break; case GraphicsImageLayout::GraphicsImageLayoutTransferSrcOptimal: break; case GraphicsImageLayout::GraphicsImageLayoutTransferDstOptimal: break; case GraphicsImageLayout::GraphicsImageLayoutPreinitialized: break; case GraphicsImageLayout::GraphicsImageLayoutPresentSrcKhr: break; default: break; } } GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE)); _framebufferDesc = framebufferDesc; return EGL2Check::checkError(); }