LCOV - code coverage report
Current view: top level - source/renderer/backend/gl - Texture.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 173 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "Texture.h"
      21             : 
      22             : #include "graphics/Color.h"
      23             : #include "lib/config2.h"
      24             : #include "lib/debug.h"
      25             : #include "renderer/backend/Sampler.h"
      26             : #include "renderer/backend/gl/Device.h"
      27             : #include "renderer/backend/gl/DeviceCommandContext.h"
      28             : #include "renderer/backend/gl/Mapping.h"
      29             : 
      30             : #include <algorithm>
      31             : 
      32             : namespace Renderer
      33             : {
      34             : 
      35             : namespace Backend
      36             : {
      37             : 
      38             : namespace GL
      39             : {
      40             : 
      41             : namespace
      42             : {
      43             : 
      44           0 : GLint CalculateMinFilter(const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount)
      45             : {
      46           0 :     if (MIPLevelCount == 1)
      47           0 :         return defaultSamplerDesc.minFilter == Sampler::Filter::LINEAR ? GL_LINEAR : GL_NEAREST;
      48             : 
      49           0 :     if (defaultSamplerDesc.minFilter == Sampler::Filter::LINEAR)
      50           0 :         return defaultSamplerDesc.mipFilter == Sampler::Filter::LINEAR ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST;
      51             : 
      52           0 :     return defaultSamplerDesc.mipFilter == Sampler::Filter::LINEAR ? GL_NEAREST_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST;
      53             : }
      54             : 
      55           0 : GLint AddressModeToGLEnum(Sampler::AddressMode addressMode)
      56             : {
      57           0 :     switch (addressMode)
      58             :     {
      59             :     case Sampler::AddressMode::REPEAT: return GL_REPEAT;
      60             :     case Sampler::AddressMode::MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
      61             :     case Sampler::AddressMode::CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
      62             :     case Sampler::AddressMode::CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
      63             :     }
      64             :     return GL_REPEAT;
      65             : }
      66             : 
      67           0 : GLenum TypeToGLEnum(CTexture::Type type)
      68             : {
      69           0 :     GLenum target = GL_TEXTURE_2D;
      70           0 :     switch (type)
      71             :     {
      72             :     case CTexture::Type::TEXTURE_2D:
      73             :         target = GL_TEXTURE_2D;
      74             :         break;
      75           0 :     case CTexture::Type::TEXTURE_2D_MULTISAMPLE:
      76             : #if CONFIG2_GLES
      77             :         ENSURE(false && "Multisample textures are unsupported on GLES");
      78             : #else
      79           0 :         target = GL_TEXTURE_2D_MULTISAMPLE;
      80             : #endif
      81           0 :         break;
      82           0 :     case CTexture::Type::TEXTURE_CUBE:
      83           0 :         target = GL_TEXTURE_CUBE_MAP;
      84           0 :         break;
      85             :     }
      86           0 :     return target;
      87             : }
      88             : 
      89             : } // anonymous namespace
      90             : 
      91             : // static
      92           0 : std::unique_ptr<CTexture> CTexture::Create(
      93             :     CDevice* device, const char* name, const Type type, const uint32_t usage,
      94             :     const Format format, const uint32_t width, const uint32_t height,
      95             :     const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount)
      96             : {
      97           0 :     std::unique_ptr<CTexture> texture(new CTexture());
      98             : 
      99           0 :     ENSURE(format != Format::UNDEFINED);
     100           0 :     ENSURE(width > 0 && height > 0 && MIPLevelCount > 0);
     101           0 :     ENSURE((type == Type::TEXTURE_2D_MULTISAMPLE && sampleCount > 1 && !(usage & ITexture::Usage::SAMPLED)) || sampleCount == 1);
     102             : 
     103           0 :     texture->m_Device = device;
     104           0 :     texture->m_Type = type;
     105           0 :     texture->m_Usage = usage;
     106           0 :     texture->m_Format = format;
     107           0 :     texture->m_Width = width;
     108           0 :     texture->m_Height = height;
     109           0 :     texture->m_MIPLevelCount = MIPLevelCount;
     110             : 
     111           0 :     glGenTextures(1, &texture->m_Handle);
     112             : 
     113           0 :     const GLenum target = TypeToGLEnum(type);
     114             : 
     115           0 :     CDeviceCommandContext::ScopedBind scopedBind(
     116           0 :         texture->m_Device->GetActiveCommandContext(), target, texture->m_Handle);
     117             : 
     118             :     // It's forbidden to set sampler state for multisample textures.
     119           0 :     if (type != Type::TEXTURE_2D_MULTISAMPLE)
     120             :     {
     121           0 :         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, CalculateMinFilter(defaultSamplerDesc, MIPLevelCount));
     122           0 :         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, defaultSamplerDesc.magFilter == Sampler::Filter::LINEAR ? GL_LINEAR : GL_NEAREST);
     123             : 
     124           0 :         glTexParameteri(target, GL_TEXTURE_WRAP_S, AddressModeToGLEnum(defaultSamplerDesc.addressModeU));
     125           0 :         glTexParameteri(target, GL_TEXTURE_WRAP_T, AddressModeToGLEnum(defaultSamplerDesc.addressModeV));
     126             :     }
     127             : 
     128             : #if !CONFIG2_GLES
     129           0 :     if (type == Type::TEXTURE_CUBE)
     130           0 :         glTexParameteri(target, GL_TEXTURE_WRAP_R, AddressModeToGLEnum(defaultSamplerDesc.addressModeW));
     131             : #endif
     132             : 
     133             : #if !CONFIG2_GLES
     134           0 :     glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
     135           0 :     glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, MIPLevelCount - 1);
     136             : 
     137           0 :     if (defaultSamplerDesc.mipLODBias != 0.0f)
     138           0 :         glTexParameteri(target, GL_TEXTURE_LOD_BIAS, defaultSamplerDesc.mipLODBias);
     139             : #endif // !CONFIG2_GLES
     140             : 
     141           0 :     if (type == Type::TEXTURE_2D && defaultSamplerDesc.anisotropyEnabled)
     142             :     {
     143           0 :         ENSURE(texture->m_Device->GetCapabilities().anisotropicFiltering);
     144           0 :         const float maxAnisotropy = std::min(
     145           0 :             defaultSamplerDesc.maxAnisotropy, texture->m_Device->GetCapabilities().maxAnisotropy);
     146           0 :         glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);
     147             :     }
     148             : 
     149           0 :     if (defaultSamplerDesc.addressModeU == Sampler::AddressMode::CLAMP_TO_BORDER ||
     150           0 :         defaultSamplerDesc.addressModeV == Sampler::AddressMode::CLAMP_TO_BORDER ||
     151           0 :         defaultSamplerDesc.addressModeW == Sampler::AddressMode::CLAMP_TO_BORDER)
     152             :     {
     153           0 :         CColor borderColor(0.0f, 0.0f, 0.0f, 0.0f);
     154           0 :         switch (defaultSamplerDesc.borderColor)
     155             :         {
     156             :         case Sampler::BorderColor::TRANSPARENT_BLACK:
     157             :             break;
     158           0 :         case Sampler::BorderColor::OPAQUE_BLACK:
     159           0 :             borderColor = CColor(0.0f, 0.0f, 0.0f, 1.0f);
     160           0 :             break;
     161           0 :         case Sampler::BorderColor::OPAQUE_WHITE:
     162           0 :             borderColor = CColor(1.0f, 1.0f, 1.0f, 1.0f);
     163           0 :             break;
     164             :         }
     165           0 :         glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, borderColor.AsFloatArray().data());
     166             :     }
     167             : 
     168           0 :     if (type == CTexture::Type::TEXTURE_2D)
     169             :     {
     170           0 :         bool compressedFormat = false;
     171           0 :         GLint internalFormat = GL_RGBA;
     172             :         // Actually pixel data is nullptr so it doesn't make sense to account
     173             :         // it, but in theory some buggy drivers might complain about invalid
     174             :         // pixel format.
     175           0 :         GLenum pixelFormat = GL_RGBA;
     176           0 :         GLenum pixelType = GL_UNSIGNED_BYTE;
     177             : #if !CONFIG2_GLES
     178             :         // load/store operation requires sized formats. See Table 8.25:
     179             :         //   https://registry.khronos.org/OpenGL/specs/gl/glspec43.core.pdf
     180           0 :         if (usage & ITexture::Usage::STORAGE)
     181           0 :             internalFormat = GL_RGBA8;
     182             : #endif
     183           0 :         switch (format)
     184             :         {
     185           0 :         case Format::UNDEFINED:
     186           0 :             debug_warn("Texture should defined format");
     187             :             break;
     188             :         case Format::R8G8B8A8_UNORM:
     189             :             break;
     190             :         case Format::R8G8B8A8_SRGB:
     191           0 :             internalFormat = GL_SRGB_ALPHA_EXT;
     192             :             break;
     193           0 :         case Format::R8G8B8_UNORM:
     194           0 :             internalFormat = GL_RGB;
     195           0 :             pixelFormat = GL_RGB;
     196           0 :             pixelType = GL_UNSIGNED_BYTE;
     197           0 :             break;
     198             : #if !CONFIG2_GLES
     199           0 :         case Format::R8_UNORM:
     200           0 :             internalFormat = GL_RED;
     201           0 :             pixelFormat = GL_RED;
     202           0 :             pixelType = GL_UNSIGNED_BYTE;
     203           0 :             break;
     204             : #endif
     205           0 :         case Format::A8_UNORM:
     206           0 :             internalFormat = GL_ALPHA;
     207           0 :             pixelFormat = GL_ALPHA;
     208           0 :             pixelType = GL_UNSIGNED_BYTE;
     209           0 :             break;
     210           0 :         case Format::L8_UNORM:
     211           0 :             internalFormat = GL_LUMINANCE;
     212           0 :             pixelFormat = GL_LUMINANCE;
     213           0 :             pixelType = GL_UNSIGNED_BYTE;
     214           0 :             break;
     215             : #if CONFIG2_GLES
     216             :         // GLES requires pixel type == UNSIGNED_SHORT or UNSIGNED_INT for depth.
     217             :         case Format::D16_UNORM: [[fallthrough]];
     218             :         case Format::D24_UNORM: [[fallthrough]];
     219             :         case Format::D32_SFLOAT:
     220             :             internalFormat = GL_DEPTH_COMPONENT;
     221             :             pixelFormat = GL_DEPTH_COMPONENT;
     222             :             pixelType = GL_UNSIGNED_SHORT;
     223             :             break;
     224             :         case Format::D24_UNORM_S8_UINT:
     225             :             debug_warn("Unsupported format");
     226             :             break;
     227             : #else
     228           0 :         case Format::D16_UNORM:
     229           0 :             internalFormat = GL_DEPTH_COMPONENT16;
     230           0 :             pixelFormat = GL_DEPTH_COMPONENT;
     231           0 :             pixelType = GL_UNSIGNED_SHORT;
     232           0 :             break;
     233           0 :         case Format::D24_UNORM:
     234           0 :             internalFormat = GL_DEPTH_COMPONENT24;
     235           0 :             pixelFormat = GL_DEPTH_COMPONENT;
     236           0 :             pixelType = GL_UNSIGNED_SHORT;
     237           0 :             break;
     238           0 :         case Format::D32_SFLOAT:
     239           0 :             internalFormat = GL_DEPTH_COMPONENT32;
     240           0 :             pixelFormat = GL_DEPTH_COMPONENT;
     241           0 :             pixelType = GL_UNSIGNED_SHORT;
     242           0 :             break;
     243           0 :         case Format::D24_UNORM_S8_UINT:
     244           0 :             internalFormat = GL_DEPTH24_STENCIL8_EXT;
     245           0 :             pixelFormat = GL_DEPTH_STENCIL_EXT;
     246           0 :             pixelType = GL_UNSIGNED_INT_24_8_EXT;
     247           0 :             break;
     248           0 :         case Format::B10G11R11_UFLOAT:
     249           0 :             internalFormat = GL_R11F_G11F_B10F_EXT;
     250           0 :             pixelFormat = GL_RGB;
     251           0 :             pixelType = GL_UNSIGNED_INT_10F_11F_11F_REV_EXT;
     252           0 :             break;
     253           0 :         case Format::R16G16B16A16_SFLOAT:
     254           0 :             internalFormat = GL_RGBA16F_ARB;
     255           0 :             pixelFormat = GL_RGBA;
     256           0 :             pixelType = GL_HALF_FLOAT;
     257           0 :             break;
     258           0 :         case Format::R32G32B32A32_SFLOAT:
     259           0 :             internalFormat = GL_RGBA32F_ARB;
     260           0 :             pixelFormat = GL_RGBA;
     261           0 :             pixelType = GL_FLOAT;
     262           0 :             break;
     263             : #endif
     264           0 :         case Format::BC1_RGB_UNORM:
     265           0 :         case Format::BC1_RGB_SRGB:
     266           0 :         case Format::BC1_RGBA_UNORM:
     267           0 :         case Format::BC1_RGBA_SRGB:
     268           0 :         case Format::BC2_UNORM:
     269           0 :         case Format::BC2_SRGB:
     270           0 :         case Format::BC3_UNORM:
     271           0 :         case Format::BC3_SRGB:
     272           0 :             compressedFormat = true;
     273           0 :             break;
     274           0 :         default:
     275           0 :             debug_warn("Unsupported format.");
     276             :         }
     277             :         // glCompressedTexImage2D can't accept a null data, so we will initialize it during uploading.
     278           0 :         if (!compressedFormat)
     279             :         {
     280           0 :             for (uint32_t level = 0; level < MIPLevelCount; ++level)
     281             :             {
     282           0 :                 glTexImage2D(target, level, internalFormat,
     283           0 :                     std::max(1u, width >> level), std::max(1u, height >> level),
     284             :                     0, pixelFormat, pixelType, nullptr);
     285             :             }
     286             :         }
     287             :     }
     288           0 :     else if (type == CTexture::Type::TEXTURE_2D_MULTISAMPLE)
     289             :     {
     290           0 :         ENSURE(MIPLevelCount == 1);
     291             : #if !CONFIG2_GLES
     292           0 :         if (format == Format::R8G8B8A8_UNORM)
     293             :         {
     294           0 :             glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, GL_RGBA8, width, height, GL_TRUE);
     295             :         }
     296           0 :         else if (format == Format::R16G16B16A16_SFLOAT)
     297             :         {
     298           0 :             glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, GL_RGBA16F_ARB, width, height, GL_TRUE);
     299             :         }
     300           0 :         else if (format == Format::R32G32B32A32_SFLOAT)
     301             :         {
     302           0 :             glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, GL_RGBA32F_ARB, width, height, GL_TRUE);
     303             :         }
     304           0 :         else if (format == Format::D24_UNORM_S8_UINT)
     305             :         {
     306           0 :             glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, GL_DEPTH24_STENCIL8_EXT, width, height, GL_TRUE);
     307             :         }
     308             :         else
     309             : #endif // !CONFIG2_GLES
     310             :         {
     311           0 :             debug_warn("Unsupported format");
     312             :         }
     313             :     }
     314             : 
     315             : 
     316             : #if !CONFIG2_GLES
     317           0 :     if (IsDepthFormat(format))
     318             :     {
     319           0 :         if (defaultSamplerDesc.compareEnabled)
     320             :         {
     321           0 :             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
     322           0 :             glTexParameteri(
     323             :                 GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC,
     324           0 :                 Mapping::FromCompareOp(defaultSamplerDesc.compareOp));
     325             :         }
     326             :         else
     327           0 :             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
     328             :     }
     329             : #endif
     330             : 
     331           0 :     if (texture->m_Device->GetCapabilities().debugLabels)
     332             :     {
     333           0 :         glObjectLabel(GL_TEXTURE, texture->m_Handle, -1, name);
     334             :     }
     335             : 
     336           0 :     return texture;
     337           0 : }
     338             : 
     339           0 : CTexture::CTexture() = default;
     340             : 
     341           0 : CTexture::~CTexture()
     342             : {
     343           0 :     m_Device->GetActiveCommandContext()->OnTextureDestroy(this);
     344           0 :     if (m_Handle)
     345           0 :         glDeleteTextures(1, &m_Handle);
     346           0 : }
     347             : 
     348           0 : IDevice* CTexture::GetDevice()
     349             : {
     350           0 :     return m_Device;
     351             : }
     352             : 
     353             : } // namespace GL
     354             : 
     355             : } // namespace Backend
     356             : 
     357             : } // namespace Renderer

Generated by: LCOV version 1.16