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 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::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 }