bool GLTextureCubeMap::set( GLCubeMapFace face, Array2DView< const uint8x4 > data, GLImageFormat format, const Vector2i& dstOffset ) { if( dstOffset.x + data.width() > sideLength() || dstOffset.y + data.height() > sideLength() ) { return false; } if( format != GLImageFormat::RGBA && format != GLImageFormat::BGRA ) { return false; } //glPushClientAttribDefaultEXT( GL_CLIENT_PIXEL_STORE_BIT ); // TODO: alignment, strides, ..., has to be packed // DSA treats a cube map as a 2D array texture. glTextureSubImage3D ( id(), 0, dstOffset.x, dstOffset.y, static_cast< int >( face ), data.width(), data.height(), 1, // 1 face static_cast< GLenum >( format ), GL_UNSIGNED_BYTE, data.pointer() ); //glPopClientAttrib(); return true; }
void Texture2DArray::pushToGPU(bool deleteAfterPush) { glTextureStorage3D(name, mipmapLevels, images[0].format.sizedFormat, images[0].width, images[0].height, images.size()); //allocate space for all for (unsigned int i = 0; i < images.size(); ++i) { ImageData &imageData = images[i]; if (imageData.data != 0) glTextureSubImage3D(name, 0, 0, 0, i, imageData.width, imageData.height, 1, imageData.format.baseFormat, imageData.format.type, imageData.data); if (deleteAfterPush) { delete[] imageData.data; imageData.data = 0; } } if (mipmapLevels > 1) { glGenerateTextureMipmap(name); //generate the remaining mipmaps glTextureParameterf(name, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); } else glTextureParameterf(name, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTextureParameterf(name, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTextureParameterf(name, GL_TEXTURE_WRAP_S, GL_REPEAT); glTextureParameterf(name, GL_TEXTURE_WRAP_T, GL_REPEAT); }
bool initTexture() { bool Validated(true); gli::gl GL; gli::texture2d Texture(gli::load_dds((getDataDirectory() + TEXTURE_DIFFUSE).c_str())); gli::gl::format const Format = GL.translate(Texture.format()); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &TextureName); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_R, GL_RED); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_G, GL_GREEN); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_B, GL_BLUE); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_A, GL_ALPHA); glTextureParameteri(TextureName, GL_TEXTURE_BASE_LEVEL, 0); glTextureParameteri(TextureName, GL_TEXTURE_MAX_LEVEL, static_cast<GLint>(Texture.levels() - 1)); glTextureParameteri(TextureName, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTextureParameteri(TextureName, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTextureStorage3D(TextureName, static_cast<GLint>(Texture.levels()), Format.Internal, static_cast<GLsizei>(Texture[0].extent().x), static_cast<GLsizei>(Texture[0].extent().y), static_cast<GLsizei>(1)); for(gli::texture2d::size_type Level = 0; Level < Texture.levels(); ++Level) { glTextureSubImage3D(TextureName, static_cast<GLint>(Level), 0, 0, 0, static_cast<GLsizei>(Texture[Level].extent().x), static_cast<GLsizei>(Texture[Level].extent().y), static_cast<GLsizei>(1), Format.External, Format.Type, Texture[Level].data()); } return Validated; }
bufferPtr glPixelBuffer::begin() const { GL_API::getStateTracker().setPixelPackUnpackAlignment(); glNamedBufferSubData(_pixelBufferHandle, 0, _bufferSize, NULL); GL_API::getStateTracker().setActiveBuffer(GL_PIXEL_UNPACK_BUFFER, _pixelBufferHandle); switch (_pbtype) { case PBType::PB_TEXTURE_1D: glTextureSubImage1D(_textureID, 0, 0, _width, _format, _dataType, NULL); break; case PBType::PB_TEXTURE_2D: glTextureSubImage2D(_textureID, 0, 0, 0, _width, _height, _format, _dataType, NULL); break; case PBType::PB_TEXTURE_3D: glTextureSubImage3D(_textureID, 0, 0, 0, 0, _width, _height, _depth, _format, _dataType, NULL); break; }; return glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); }
kit::Texture::Ptr kit::Texture::create3DFromFile(std::string filename, kit::Texture::InternalFormat format, kit::Texture::EdgeSamplingMode edgemode, kit::Texture::FilteringMode minfilter, kit::Texture::FilteringMode magfilter) { kit::Texture::Ptr returner = std::make_shared<kit::Texture>(Texture3D); returner->m_internalFormat = format; // Try to load data from file unsigned char* bufferdata; int x, y, n; stbi_set_flip_vertically_on_load(0); bufferdata = stbi_load(filename.c_str(), &x, &y, &n, 4); if (bufferdata == nullptr) { KIT_ERR(stbi_failure_reason()); return nullptr; } if (y != x*x || y%y != 0) { KIT_ERR("Failed to load 3d texture from file, not perfectly cubical"); } // Set resolution returner->m_resolution = glm::uvec3(x, x, x); // Specify storage and upload data to GPU KIT_GL(glTextureStorage3D(returner->m_glHandle, 1, returner->m_internalFormat, x, x, x)); KIT_GL(glTextureSubImage3D(returner->m_glHandle, 0, 0, 0, 0, x, x, x, GL_RGBA, GL_UNSIGNED_BYTE, bufferdata)); // Free loaded data stbi_image_free(bufferdata); // Set parameters returner->setEdgeSamplingMode(edgemode); returner->setMinFilteringMode(minfilter); returner->setMagFilteringMode(magfilter); // Generate mipmap //returner->generateMipmap(); return returner; }
bool initTexture() { bool Validated(true); gli::gl GL(gli::gl::PROFILE_GL33); gli::texture2d Texture(gli::load_dds((getDataDirectory() + TEXTURE_DIFFUSE).c_str())); assert(!Texture.empty()); gli::gl::format const Format = GL.translate(Texture.format(), Texture.swizzles()); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &TextureName); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_R, Format.Swizzles[0]); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_G, Format.Swizzles[1]); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_B, Format.Swizzles[2]); glTextureParameteri(TextureName, GL_TEXTURE_SWIZZLE_A, Format.Swizzles[3]); glTextureParameteri(TextureName, GL_TEXTURE_BASE_LEVEL, 0); glTextureParameteri(TextureName, GL_TEXTURE_MAX_LEVEL, GLint(Texture.levels() - 1)); glTextureParameteri(TextureName, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTextureParameteri(TextureName, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTextureStorage3D(TextureName, static_cast<GLint>(Texture.levels()), Format.Internal, static_cast<GLsizei>(Texture.extent().x), static_cast<GLsizei>(Texture.extent().y), 1); for(gli::texture2d::size_type Level(0); Level < Texture.levels(); ++Level) { glTextureSubImage3D(TextureName, static_cast<GLint>(Level), 0, 0, 0, static_cast<GLsizei>(Texture[Level].extent().x), static_cast<GLsizei>(Texture[Level].extent().y), 1, Format.External, Format.Type, Texture[Level].data()); } glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // Query the texture handle and make the texture resident TextureHandle = glGetTextureHandleNV(TextureName); glMakeTextureHandleResidentNV(TextureHandle); return Validated; }
kit::Texture::Texture(const std::string & filename, kit::Texture::InternalFormat format, uint8_t levels, Type t) : kit::Texture(t) { std::cout << "Loading texture from file \"" << filename.c_str() << "\"" << std::endl; m_filename = filename; if(t == Type::Texture2D) { m_internalFormat = format; // Try to load data from file unsigned char* bufferdata; int x, y, n; stbi_set_flip_vertically_on_load(1); bufferdata = stbi_load(filename.c_str(), &x, &y, &n, 4); if (bufferdata == nullptr) { KIT_THROW(stbi_failure_reason()); } // Set resolution m_resolution = glm::uvec3(x, y, 0); uint8_t mipLevels = levels > 0 ? levels : calculateMipLevels(); // Specify storage and upload data to GPU #ifndef KIT_SHITTY_INTEL glTextureStorage2D(m_glHandle, mipLevels, m_internalFormat, m_resolution.x, m_resolution.y); glTextureSubImage2D(m_glHandle, 0, 0, 0, x, y, GL_RGBA, GL_UNSIGNED_BYTE, bufferdata); #else bind(); glTexStorage2D(m_type, mipLevels, m_internalFormat, m_resolution.x, m_resolution.y); glTexSubImage2D(m_type, 0, 0, 0, x, y, GL_RGBA, GL_UNSIGNED_BYTE, bufferdata); #endif // Free loaded data stbi_image_free(bufferdata); // Set parameters setEdgeSamplingMode(EdgeSamplingMode::Repeat); setMinFilteringMode(m_minFilteringMode); setMagFilteringMode(m_magFilteringMode); setAnisotropicLevel(1.0f); } if(t == Type::Texture3D) { m_internalFormat = format; // Try to load data from file unsigned char* bufferdata; int x, y, n; stbi_set_flip_vertically_on_load(0); bufferdata = stbi_load(filename.c_str(), &x, &y, &n, 4); if (bufferdata == nullptr) { KIT_THROW(stbi_failure_reason()); } if (y != x*x || y%y != 0) { KIT_THROW("Failed to load 3d texture from file, not perfectly cubical"); } // Set resolution m_resolution = glm::uvec3(x, x, x); // Specify storage and upload data to GPU #ifndef KIT_SHITTY_INTEL glTextureStorage3D(m_glHandle, 1, m_internalFormat, x, x, x); glTextureSubImage3D(m_glHandle, 0, 0, 0, 0, x, x, x, GL_RGBA, GL_UNSIGNED_BYTE, bufferdata); #else returner->bind(); glTexStorage3D(returner->m_type, 1, m_internalFormat, x, x, x); glTexSubImage3D(returner->m_type, 0, 0, 0, 0, x, x, x, GL_RGBA, GL_UNSIGNED_BYTE, bufferdata); #endif // Free loaded data stbi_image_free(bufferdata); setEdgeSamplingMode(EdgeSamplingMode::Repeat); setMinFilteringMode(m_minFilteringMode); setMagFilteringMode(m_magFilteringMode); setAnisotropicLevel(1.0f); } }
void CubeMapTexture::subImageImplementationDSA(const Coordinate coordinate, const GLint level, const Vector2i& offset, const Vector2i& size, const ColorFormat format, const ColorType type, const GLvoid* const data) { glTextureSubImage3D(_id, level, offset.x(), offset.y(), GLenum(coordinate) - GL_TEXTURE_CUBE_MAP_POSITIVE_X, size.x(), size.y(), 1, GLenum(format), GLenum(type), data); }
PIGLIT_GL_TEST_CONFIG_END /** Test texture size errors and subtexture position errors */ static bool test_pos_and_sizes(void) { bool pass = true; GLuint name; /* all of these should generate GL_INVALID_VALUE */ glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, -16, 0, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, -6, -5, 0, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; glTexImage2D(GL_TEXTURE_2D, -2, GL_RGBA, 16, 16, 0, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; glTexImage2D(GL_TEXTURE_2D, 2000, GL_RGBA, 16, 16, 0, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; /* Setup dsa. */ glCreateTextures(GL_TEXTURE_2D, 1, &name); glBindTextureUnit(0, name); /* Since next command isn't bindless. */ /* setup valid 2D texture for subsequent TexSubImage calls */ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_FLOAT, NULL); glTextureSubImage2D(name, 0, 6, 6, 100, 100, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; glTextureSubImage2D(name, 0, -6, -6, 10, 10, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; glCopyTextureSubImage2D(name, 0, -6, -6, 2, 2, 10, 10); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; glCopyTextureSubImage2D(name, 0, 6, 6, 2, 2, 200, 200); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; /* mipmap level 1 doesn't exist */ glTextureSubImage2D(name, 1, 0, 0, 8, 8, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass; /* mipmap level 2 doesn't exist */ glCopyTextureSubImage2D(name, 2, 0, 0, 0, 0, 4, 4); pass = piglit_check_gl_error(GL_INVALID_OPERATION) && pass; /* To test 1D and 3D entry points, let's try using the wrong functions. */ glTextureSubImage1D(name, 0, 0, 4, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_ENUM) && pass; glTextureSubImage3D(name, 0, 0, 0, 0, 4, 4, 4, GL_RGBA, GL_FLOAT, NULL); pass = piglit_check_gl_error(GL_INVALID_ENUM) && pass; glCopyTextureSubImage1D(name, 0, 0, 0, 0, 4); pass = piglit_check_gl_error(GL_INVALID_ENUM) && pass; glCopyTextureSubImage3D(name, 0, 0, 0, 0, 0, 0, 4, 4); pass = piglit_check_gl_error(GL_INVALID_ENUM) && pass; return pass; }
//[-------------------------------------------------------] //[ 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 }