void display() { // Clear the framebuffer glBindFramebuffer(GL_FRAMEBUFFER, 0); glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]); glBindProgramPipeline(PipelineName); // Step 1: Render the scene in a multisampled framebuffer glEnable(GL_MULTISAMPLE); renderFBO(FramebufferRenderName); glDisable(GL_MULTISAMPLE); // Step 2: Resolved multisampling glBindFramebuffer(GL_READ_FRAMEBUFFER, FramebufferRenderName); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FramebufferResolveName); glBlitFramebuffer( 0, 0, FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y, 0, 0, FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y, GL_COLOR_BUFFER_BIT, GL_NEAREST); // Step 3: Generated mipmaps glGenerateTextureMipmapEXT(ColorTextureName, GL_TEXTURE_2D); // Step 4: Render the colorbuffer from the multisampled framebuffer renderFB(ColorTextureName); glf::checkError("display"); glf::swapBuffers(); }
/** * @brief * Constructor */ Texture2DArrayDsa::Texture2DArrayDsa(OpenGLRenderer &openGLRenderer, unsigned int width, unsigned int height, unsigned int numberOfSlices, Renderer::TextureFormat::Enum textureFormat, void *data, unsigned int flags) : Texture2DArray(openGLRenderer, width, height, numberOfSlices) { #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Backup the currently set alignment GLint openGLAlignmentBackup = 0; glGetIntegerv(GL_UNPACK_ALIGNMENT, &openGLAlignmentBackup); #endif // Set correct alignment glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Upload the base map of the texture (mipmaps are automatically created as soon as the base map is changed) glTextureImage3DEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, 0, Mapping::getOpenGLInternalFormat(textureFormat), static_cast<GLsizei>(width), static_cast<GLsizei>(height), static_cast<GLsizei>(numberOfSlices), 0, Mapping::getOpenGLFormat(textureFormat), Mapping::getOpenGLType(textureFormat), data); // Build mipmaps automatically on the GPU? if (flags & Renderer::TextureFlag::MIPMAPS) { glGenerateTextureMipmapEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT); glTextureParameteriEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); } else { glTextureParameteriEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } glTextureParameteriEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR); #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Restore previous alignment glPixelStorei(GL_UNPACK_ALIGNMENT, openGLAlignmentBackup); #endif }
bool initTexture2D() { glGenTextures(1, &TextureName); glTextureParameteriEXT(TextureName, GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); glTextureParameteriEXT(TextureName, GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN); glTextureParameteriEXT(TextureName, GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE); glTextureParameteriEXT(TextureName, GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA); glTextureParameteriEXT(TextureName, GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); glTextureParameteriEXT(TextureName, GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000); gli::texture2D Image = gli::load(TEXTURE_DIFFUSE); for(std::size_t Level = 0; Level < Image.levels(); ++Level) { glTextureImage2DEXT( TextureName, GL_TEXTURE_2D, GLint(Level), GL_RGB, GLsizei(Image[Level].dimensions().x), GLsizei(Image[Level].dimensions().y), 0, GL_RGB, GL_UNSIGNED_BYTE, Image[Level].data()); } if(Image.levels() == 1) glGenerateTextureMipmapEXT(TextureName, GL_TEXTURE_2D); return glf::checkError("initTexture2D"); }
bool initTexture() { gli::texture2D Texture(gli::loadStorageDDS(glf::DATA_DIRECTORY + TEXTURE_DIFFUSE)); assert(!Texture.empty()); glGenTextures(texture::MAX, TextureName); //glTextureImage2DEXT(TextureName[texture::DIFFUSE], GL_TEXTURE_2D, 0, GL_RGB8, GLsizei(Texture[0].dimensions().x), GLsizei(Texture[0].dimensions().y), 0, GL_BGRA, GL_UNSIGNED_BYTE, 0); glTextureStorage2DEXT(TextureName[texture::DIFFUSE], GL_TEXTURE_2D, GLint(Texture.levels()), GL_RGB8, GLsizei(Texture[0].dimensions().x), GLsizei(Texture[0].dimensions().y)); for(std::size_t Level = 0; Level < Texture.levels(); ++Level) { glTextureSubImage2DEXT( TextureName[texture::DIFFUSE], GL_TEXTURE_2D, GLint(Level), 0, 0, GLsizei(Texture[Level].dimensions().x), GLsizei(Texture[Level].dimensions().y), GL_BGR, GL_UNSIGNED_BYTE, Texture[Level].data()); } if(Texture.levels() == 1) glGenerateTextureMipmapEXT(TextureName[texture::DIFFUSE], GL_TEXTURE_2D); // From GL_ARB_texture_storage_multisample glTextureStorage2DMultisampleEXT(TextureName[texture::MULTISAMPLE], GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA8, FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y, GL_TRUE); glTextureStorage2DMultisampleEXT(TextureName[texture::DEPTH], GL_TEXTURE_2D_MULTISAMPLE, 4, GL_DEPTH_COMPONENT24, FRAMEBUFFER_SIZE.x, FRAMEBUFFER_SIZE.y, GL_TRUE); return true; }
static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data) { GLNVGcontext* gl = (GLNVGcontext*)uptr; GLNVGtexture* tex = glnvg__allocTexture(gl); if (tex == NULL) return 0; glGenTextures(1, &tex->tex); tex->width = w; tex->height = h; tex->type = type; glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->width); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); if (type == NVG_TEXTURE_RGBA) glTextureImage2DEXT(tex->tex, GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); else glTextureImage2DEXT(tex->tex, GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data); if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) { glTextureParameteriEXT(tex->tex, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); } else { glTextureParameteriEXT(tex->tex, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } glTextureParameteriEXT(tex->tex, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); // The new way to build mipmaps on GLES and GL3 if (imageFlags & NVG_IMAGE_GENERATE_MIPMAPS) { glGenerateTextureMipmapEXT(tex->tex, GL_TEXTURE_2D); } return tex->id; }
//[-------------------------------------------------------] //[ Public methods ] //[-------------------------------------------------------] Texture2DArrayDsa::Texture2DArrayDsa(OpenGLRenderer &openGLRenderer, uint32_t width, uint32_t height, uint32_t numberOfSlices, Renderer::TextureFormat::Enum textureFormat, const void *data, uint32_t flags) : Texture2DArray(openGLRenderer, width, height, numberOfSlices) { #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Backup the currently set alignment GLint openGLAlignmentBackup = 0; glGetIntegerv(GL_UNPACK_ALIGNMENT, &openGLAlignmentBackup); #endif // Set correct alignment glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Create the OpenGL texture instance const bool isARB_DSA = openGLRenderer.getExtensions().isGL_ARB_direct_state_access(); if (isARB_DSA) { glCreateTextures(GL_TEXTURE_2D_ARRAY_EXT, 1, &mOpenGLTexture); } else { glGenTextures(1, &mOpenGLTexture); } // Upload the base map of the texture (mipmaps are automatically created as soon as the base map is changed) if (isARB_DSA) { glTextureStorage3D(mOpenGLTexture, 1, Mapping::getOpenGLInternalFormat(textureFormat), static_cast<GLsizei>(width), static_cast<GLsizei>(height), static_cast<GLsizei>(numberOfSlices)); if (nullptr != data) { glTextureSubImage3D(mOpenGLTexture, 0, 0, 0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height), static_cast<GLsizei>(numberOfSlices), Mapping::getOpenGLFormat(textureFormat), Mapping::getOpenGLType(textureFormat), data); } } else { glTextureImage3DEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, 0, static_cast<GLint>(Mapping::getOpenGLInternalFormat(textureFormat)), static_cast<GLsizei>(width), static_cast<GLsizei>(height), static_cast<GLsizei>(numberOfSlices), 0, Mapping::getOpenGLFormat(textureFormat), Mapping::getOpenGLType(textureFormat), data); } // Build mipmaps automatically on the GPU? (or GPU driver) if (flags & Renderer::TextureFlag::GENERATE_MIPMAPS) { if (isARB_DSA) { glGenerateTextureMipmap(mOpenGLTexture); glTextureParameteri(mOpenGLTexture, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); } else { glGenerateTextureMipmapEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT); glTextureParameteriEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); } } else { if (isARB_DSA) { glTextureParameteri(mOpenGLTexture, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } else { glTextureParameteriEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } } if (isARB_DSA) { glTextureParameteri(mOpenGLTexture, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else { glTextureParameteriEXT(mOpenGLTexture, GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Restore previous alignment glPixelStorei(GL_UNPACK_ALIGNMENT, openGLAlignmentBackup); #endif }
void AbstractTexture::mipmapImplementationDSA() { glGenerateTextureMipmapEXT(_id, _target); }