bool Texture3D::Create() { Release(); #ifdef GL_ES_VERSION_2_0 URHO3D_LOGERROR("Failed to create 3D texture, currently unsupported on OpenGL ES 2"); return false; #else if (!graphics_ || !width_ || !height_ || !depth_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture creation while device is lost"); return true; } unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; unsigned externalFormat = GetExternalFormat(format_); unsigned dataType = GetDataType(format_); glGenTextures(1, &object_.name_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); // If not compressed, create the initial level 0 texture with null data bool success = true; if (!IsCompressed()) { glGetError(); glTexImage3D(target_, 0, format, width_, height_, depth_, 0, externalFormat, dataType, 0); if (glGetError()) { URHO3D_LOGERROR("Failed to create texture"); success = false; } } // Set mipmapping levels_ = CheckMaxLevels(width_, height_, depth_, requestedLevels_); glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1); // Set initial parameters, then unbind the texture UpdateParameters(); graphics_->SetTexture(0, 0); return success; #endif }
bool Texture3D::Create() { Release(); #ifdef GL_ES_VERSION_2_0 URHO3D_LOGERROR("Failed to create 3D texture, currently unsupported on OpenGL ES 2"); return false; #else if (!graphics_ || !width_ || !height_ || !depth_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture creation while device is lost"); return true; } unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; unsigned externalFormat = GetExternalFormat(format_); unsigned dataType = GetDataType(format_); glGenTextures(1, &object_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); // If not compressed, create the initial level 0 texture with null data bool success = true; if (!IsCompressed()) { glGetError(); glTexImage3D(target_, 0, format, width_, height_, depth_, 0, externalFormat, dataType, 0); if (glGetError()) { URHO3D_LOGERROR("Failed to create texture"); success = false; } } // Set mipmapping levels_ = requestedLevels_; if (!levels_) { unsigned maxSize = (unsigned)Max(Max((int)width_, (int)height_), (int)depth_); while (maxSize) { maxSize >>= 1; ++levels_; } }
bool TextureCube::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { LOGWARNING("Texture creation while device is lost"); return true; } glGenTextures(1, &object_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); // If not compressed, create the initial level 0 texture with null data unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; unsigned externalFormat = GetExternalFormat(format_); unsigned dataType = GetDataType(format_); bool success = true; if (!IsCompressed()) { glGetError(); for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, 0); if (glGetError()) success = false; } } if (!success) LOGERROR("Failed to create texture"); // Set mipmapping levels_ = requestedLevels_; if (!levels_) { unsigned maxSize = Max((int)width_, (int)height_); while (maxSize) { maxSize >>= 1; ++levels_; } }
bool Texture2D::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture creation while device is lost"); return true; } unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; unsigned externalFormat = GetExternalFormat(format_); unsigned dataType = GetDataType(format_); // Create a renderbuffer instead of a texture if depth texture is not properly supported, or if this will be a packed // depth stencil texture #ifndef GL_ES_VERSION_2_0 if (format == Graphics::GetDepthStencilFormat()) #else if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24_OES || format == GL_DEPTH24_STENCIL8_OES || (format == GL_DEPTH_COMPONENT && !graphics_->GetShadowMapFormat())) #endif { if (renderSurface_) { renderSurface_->CreateRenderBuffer(width_, height_, format); return true; } else return false; } glGenTextures(1, &object_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); // If not compressed, create the initial level 0 texture with null data bool success = true; if (!IsCompressed()) { glGetError(); glTexImage2D(target_, 0, format, width_, height_, 0, externalFormat, dataType, 0); if (glGetError()) { URHO3D_LOGERROR("Failed to create texture"); success = false; } } // Set mipmapping levels_ = requestedLevels_; if (!levels_) { unsigned maxSize = Max((int)width_, (int)height_); while (maxSize) { maxSize >>= 1; ++levels_; } }
bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data) { URHO3D_PROFILE(SetTextureData); if (!object_ || !graphics_) { URHO3D_LOGERROR("No texture created, can not set data"); return false; } if (!data) { URHO3D_LOGERROR("Null source for setting data"); return false; } if (level >= levels_) { URHO3D_LOGERROR("Illegal mip level for setting data"); return false; } if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture data assignment while device is lost"); dataPending_ = true; return true; } if (IsCompressed()) { x &= ~3; y &= ~3; } int levelWidth = GetLevelWidth(level); int levelHeight = GetLevelHeight(level); if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0) { URHO3D_LOGERROR("Illegal dimensions for setting data"); return false; } graphics_->SetTextureForUpdate(this); bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight; unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; if (!IsCompressed()) { if (wholeLevel) glTexImage2D(target_, level, format, width, height, 0, GetExternalFormat(format_), GetDataType(format_), data); else glTexSubImage2D(target_, level, x, y, width, height, GetExternalFormat(format_), GetDataType(format_), data); } else { if (wholeLevel) glCompressedTexImage2D(target_, level, format, width, height, 0, GetDataSize(width, height), data); else glCompressedTexSubImage2D(target_, level, x, y, width, height, format, GetDataSize(width, height), data); } graphics_->SetTexture(0, 0); return true; }
bool Texture2D::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture creation while device is lost"); return true; } #ifdef GL_ES_VERSION_2_0 if (multiSample_ > 1) { URHO3D_LOGWARNING("Multisampled texture is not supported on OpenGL ES"); multiSample_ = 1; autoResolve_ = false; } #endif unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; unsigned externalFormat = GetExternalFormat(format_); unsigned dataType = GetDataType(format_); // Create a renderbuffer instead of a texture if depth texture is not properly supported, or if this will be a packed // depth stencil texture #ifndef GL_ES_VERSION_2_0 if (format == Graphics::GetDepthStencilFormat()) #else if (format == GL_DEPTH_COMPONENT16 || format == GL_DEPTH_COMPONENT24_OES || format == GL_DEPTH24_STENCIL8_OES || (format == GL_DEPTH_COMPONENT && !graphics_->GetShadowMapFormat())) #endif { if (renderSurface_) { renderSurface_->CreateRenderBuffer(width_, height_, format, multiSample_); return true; } else return false; } else { if (multiSample_ > 1) { if (autoResolve_) { // Multisample with autoresolve: create a renderbuffer for rendering, but also a texture renderSurface_->CreateRenderBuffer(width_, height_, format, multiSample_); } else { // Multisample without autoresolve: create a texture only #ifndef GL_ES_VERSION_2_0 if (!Graphics::GetGL3Support() && !GLEW_ARB_texture_multisample) { URHO3D_LOGERROR("Multisampled texture extension not available"); return false; } target_ = GL_TEXTURE_2D_MULTISAMPLE; if (renderSurface_) renderSurface_->target_ = GL_TEXTURE_2D_MULTISAMPLE; #endif } } } glGenTextures(1, &object_.name_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); // If not compressed, create the initial level 0 texture with null data bool success = true; if (!IsCompressed()) { glGetError(); #ifndef GL_ES_VERSION_2_0 if (multiSample_ > 1 && !autoResolve_) glTexImage2DMultisample(target_, multiSample_, format, width_, height_, GL_TRUE); else #endif glTexImage2D(target_, 0, format, width_, height_, 0, externalFormat, dataType, 0); if (glGetError()) { URHO3D_LOGERROR("Failed to create texture"); success = false; } } // Set mipmapping if (usage_ == TEXTURE_DEPTHSTENCIL) requestedLevels_ = 1; else if (usage_ == TEXTURE_RENDERTARGET) { #ifdef __EMSCRIPTEN__ // glGenerateMipmap appears to not be working on WebGL, disable rendertarget mipmaps for now requestedLevels_ = 1; #else if (requestedLevels_ != 1) { // Generate levels for the first time now RegenerateLevels(); // Determine max. levels automatically requestedLevels_ = 0; } #endif } levels_ = CheckMaxLevels(width_, height_, requestedLevels_); #ifndef GL_ES_VERSION_2_0 glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1); #endif // Set initial parameters, then unbind the texture UpdateParameters(); graphics_->SetTexture(0, 0); return success; }
bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data) { if (!object_ || !graphics_) { LOGERROR("No texture created, can not set data"); return false; } if (!data) { LOGERROR("Null source for setting data"); return false; } if (level >= levels_) { LOGERROR("Illegal mip level for setting data"); return false; } if (graphics_->IsDeviceLost()) { LOGWARNING("Texture data assignment while device is lost"); dataPending_ = true; return true; } if (IsCompressed()) { x &= ~3; y &= ~3; } int levelWidth = GetLevelWidth(level); int levelHeight = GetLevelHeight(level); if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0) { LOGERROR("Illegal dimensions for setting data"); return false; } bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight; // Use Direct3D convention with the vertical coordinates ie. 0 is top y = levelHeight - (y + height); graphics_->SetTextureForUpdate(this); unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; if (!IsCompressed()) { if (wholeLevel) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0, GetExternalFormat(format_), GetDataType(format_), data); else glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, GetExternalFormat(format_), GetDataType(format_), data); } else { if (wholeLevel) glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0, GetDataSize(width, height), data); else glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, format, GetDataSize(width, height), data); } graphics_->SetTexture(0, 0); return true; }
bool TextureCube::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture creation while device is lost"); return true; } #ifdef GL_ES_VERSION_2_0 if (multiSample_ > 1) { URHO3D_LOGWARNING("Multisampled texture is not supported on OpenGL ES"); multiSample_ = 1; autoResolve_ = false; } #endif glGenTextures(1, &object_.name_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); // If not compressed, create the initial level 0 texture with null data unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; unsigned externalFormat = GetExternalFormat(format_); unsigned dataType = GetDataType(format_); // If multisample, create renderbuffers for each face if (multiSample_ > 1) { for (auto& renderSurface : renderSurfaces_) renderSurface->CreateRenderBuffer(width_, height_, format, multiSample_); } bool success = true; if (!IsCompressed()) { glGetError(); for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, nullptr); if (glGetError()) success = false; } } if (!success) URHO3D_LOGERROR("Failed to create texture"); // Set mipmapping if (usage_ == TEXTURE_DEPTHSTENCIL || usage_ == TEXTURE_DYNAMIC) requestedLevels_ = 1; else if (usage_ == TEXTURE_RENDERTARGET) { #if defined(__EMSCRIPTEN__) || defined(IOS) || defined(TVOS) // glGenerateMipmap appears to not be working on WebGL or iOS/tvOS, disable rendertarget mipmaps for now requestedLevels_ = 1; #else if (requestedLevels_ != 1) { // Generate levels for the first time now RegenerateLevels(); // Determine max. levels automatically requestedLevels_ = 0; } #endif } levels_ = CheckMaxLevels(width_, height_, requestedLevels_); #ifndef GL_ES_VERSION_2_0 glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1); #endif // Set initial parameters, then unbind the texture UpdateParameters(); graphics_->SetTexture(0, nullptr); return success; }
bool TextureCube::Create() { Release(); if (!graphics_ || !width_ || !height_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture creation while device is lost"); return true; } glGenTextures(1, &object_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); // If not compressed, create the initial level 0 texture with null data GLenum format = GetSRGB() ? GetSRGBFormat(format_) : format_; GLenum externalFormat = GetExternalFormat(format_); GLenum dataType = GetDataType(format_); // If multisample, create renderbuffers for each face if (multiSample_ > 1) { for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i) renderSurfaces_[i]->CreateRenderBuffer(width_, height_, format, multiSample_); } bool success = true; if (!IsCompressed()) { glGetError(); for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, nullptr); if (GL_NO_ERROR!=glGetError()) success = false; } } if (!success) URHO3D_LOGERROR("Failed to create texture"); // Set mipmapping if (usage_ == TEXTURE_DEPTHSTENCIL) requestedLevels_ = 1; else if (usage_ == TEXTURE_RENDERTARGET) { if (requestedLevels_ != 1) { // Generate levels for the first time now RegenerateLevels(); // Determine max. levels automatically requestedLevels_ = 0; } } levels_ = CheckMaxLevels(width_, height_, requestedLevels_); glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1); // Set initial parameters, then unbind the texture UpdateParameters(); graphics_->SetTexture(0, nullptr); return success; }
bool Texture2DArray::Create() { Release(); #ifdef GL_ES_VERSION_2_0 URHO3D_LOGERROR("Failed to create 2D array texture, currently unsupported on OpenGL ES 2"); return false; #else if (!graphics_ || !width_ || !height_ || !layers_) return false; if (graphics_->IsDeviceLost()) { URHO3D_LOGWARNING("Texture array creation while device is lost"); return true; } glGenTextures(1, &object_.name_); // Ensure that our texture is bound to OpenGL texture unit 0 graphics_->SetTextureForUpdate(this); unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_; unsigned externalFormat = GetExternalFormat(format_); unsigned dataType = GetDataType(format_); // If not compressed, create the initial level 0 texture with null data bool success = true; if (!IsCompressed()) { glGetError(); glTexImage3D(target_, 0, format, width_, height_, layers_, 0, externalFormat, dataType, 0); if (glGetError()) success = false; } if (!success) URHO3D_LOGERROR("Failed to create texture array"); // Set mipmapping if (usage_ == TEXTURE_DEPTHSTENCIL) requestedLevels_ = 1; else if (usage_ == TEXTURE_RENDERTARGET) { #ifdef __EMSCRIPTEN__ // glGenerateMipmap appears to not be working on WebGL, disable rendertarget mipmaps for now requestedLevels_ = 1; #else if (requestedLevels_ != 1) { // Generate levels for the first time now RegenerateLevels(); // Determine max. levels automatically requestedLevels_ = 0; } #endif } levels_ = CheckMaxLevels(width_, height_, requestedLevels_); glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1); // Set initial parameters, then unbind the texture UpdateParameters(); graphics_->SetTexture(0, 0); return success; #endif }