| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/OGL/OGLTexture.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | #include "Common/CommonTypes.h" |
| 8 | #include "Common/GL/GLUtil.h" |
| 9 | #include "Common/MsgHandler.h" |
| 10 | |
| 11 | #include "VideoBackends/OGL/OGLConfig.h" |
| 12 | #include "VideoBackends/OGL/OGLGfx.h" |
| 13 | |
| 14 | #include "VideoCommon/VideoConfig.h" |
| 15 | |
| 16 | namespace OGL |
| 17 | { |
| 18 | GLenum OGLTexture::GetGLInternalFormatForTextureFormat(AbstractTextureFormat format, bool storage) |
| 19 | { |
| 20 | switch (format) |
| 21 | { |
| 22 | case AbstractTextureFormat::DXT1: |
| 23 | return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
| 24 | case AbstractTextureFormat::DXT3: |
| 25 | return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
| 26 | case AbstractTextureFormat::DXT5: |
| 27 | return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
| 28 | case AbstractTextureFormat::BPTC: |
| 29 | return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB; |
| 30 | case AbstractTextureFormat::RGBA8: |
| 31 | return storage ? GL_RGBA8 : GL_RGBA; |
| 32 | case AbstractTextureFormat::BGRA8: |
| 33 | return storage ? GL_RGBA8 : GL_BGRA; |
| 34 | case AbstractTextureFormat::RGB10_A2: |
| 35 | return GL_RGB10_A2; |
| 36 | case AbstractTextureFormat::RGBA16F: |
| 37 | return GL_RGBA16F; |
| 38 | case AbstractTextureFormat::R16: |
| 39 | return GL_R16; |
| 40 | case AbstractTextureFormat::R32F: |
| 41 | return GL_R32F; |
| 42 | case AbstractTextureFormat::D16: |
| 43 | return GL_DEPTH_COMPONENT16; |
| 44 | case AbstractTextureFormat::D24_S8: |
| 45 | return GL_DEPTH24_STENCIL8; |
| 46 | case AbstractTextureFormat::D32F: |
| 47 | return GL_DEPTH_COMPONENT32F; |
| 48 | case AbstractTextureFormat::D32F_S8: |
| 49 | return GL_DEPTH32F_STENCIL8; |
| 50 | default: |
| 51 | PanicAlertFmt("Unhandled texture format."); |
| 52 | return storage ? GL_RGBA8 : GL_RGBA; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | namespace |
| 57 | { |
| 58 | GLenum GetGLFormatForTextureFormat(AbstractTextureFormat format) |
| 59 | { |
| 60 | switch (format) |
| 61 | { |
| 62 | case AbstractTextureFormat::RGBA8: |
| 63 | return GL_RGBA; |
| 64 | case AbstractTextureFormat::BGRA8: |
| 65 | return GL_BGRA; |
| 66 | case AbstractTextureFormat::RGB10_A2: |
| 67 | return GL_RGB10_A2; |
| 68 | case AbstractTextureFormat::RGBA16F: |
| 69 | return GL_RGBA16F; |
| 70 | case AbstractTextureFormat::R16: |
| 71 | case AbstractTextureFormat::R32F: |
| 72 | return GL_RED; |
| 73 | case AbstractTextureFormat::D16: |
| 74 | case AbstractTextureFormat::D32F: |
| 75 | return GL_DEPTH_COMPONENT; |
| 76 | case AbstractTextureFormat::D24_S8: |
| 77 | case AbstractTextureFormat::D32F_S8: |
| 78 | return GL_DEPTH_STENCIL; |
| 79 | // Compressed texture formats don't use this parameter. |
| 80 | default: |
| 81 | return GL_UNSIGNED_BYTE; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | GLenum GetGLTypeForTextureFormat(AbstractTextureFormat format) |
| 86 | { |
| 87 | switch (format) |
| 88 | { |
| 89 | case AbstractTextureFormat::RGBA8: |
| 90 | case AbstractTextureFormat::BGRA8: |
| 91 | return GL_UNSIGNED_BYTE; |
| 92 | case AbstractTextureFormat::RGB10_A2: |
| 93 | return GL_UNSIGNED_INT_2_10_10_10_REV; |
| 94 | case AbstractTextureFormat::RGBA16F: |
| 95 | return GL_HALF_FLOAT; |
| 96 | case AbstractTextureFormat::R16: |
| 97 | return GL_UNSIGNED_SHORT; |
| 98 | case AbstractTextureFormat::R32F: |
| 99 | return GL_FLOAT; |
| 100 | case AbstractTextureFormat::D16: |
| 101 | return GL_UNSIGNED_SHORT; |
| 102 | case AbstractTextureFormat::D24_S8: |
| 103 | return GL_UNSIGNED_INT_24_8; |
| 104 | case AbstractTextureFormat::D32F: |
| 105 | return GL_FLOAT; |
| 106 | case AbstractTextureFormat::D32F_S8: |
| 107 | return GL_FLOAT_32_UNSIGNED_INT_24_8_REV; |
| 108 | // Compressed texture formats don't use this parameter. |
| 109 | default: |
| 110 | return GL_UNSIGNED_BYTE; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool UsePersistentStagingBuffers() |
| 115 | { |
| 116 | // We require ARB_buffer_storage to create the persistent mapped buffer, |
| 117 | // ARB_shader_image_load_store for glMemoryBarrier, and ARB_sync to ensure |
| 118 | // the GPU has finished the copy before reading the buffer from the CPU. |
| 119 | return g_ogl_config.bSupportsGLBufferStorage && g_ogl_config.bSupportsImageLoadStore && |
| 120 | g_ogl_config.bSupportsGLSync; |
| 121 | } |
| 122 | } // Anonymous namespace |
| 123 | |
| 124 | OGLTexture::OGLTexture(const TextureConfig& tex_config, std::string_view name) |
| 125 | : AbstractTexture(tex_config), m_name(name) |
| 126 | { |
| 127 | DEBUG_ASSERT_MSG(VIDEO, !tex_config.IsMultisampled() || tex_config.levels == 1, |
| 128 | "OpenGL does not support multisampled textures with mip levels"); |
| 129 | |
| 130 | const GLenum target = GetGLTarget(); |
| 131 | glGenTextures(1, &m_texId); |
| 132 | glActiveTexture(GL_MUTABLE_TEXTURE_INDEX); |
| 133 | glBindTexture(target, m_texId); |
| 134 | |
| 135 | if (!m_name.empty() && g_backend_info.bSupportsSettingObjectNames) |
| 136 | { |
| 137 | glObjectLabel(GL_TEXTURE, m_texId, (GLsizei)m_name.size(), m_name.c_str()); |
| 138 | } |
| 139 | |
| 140 | glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, m_config.levels - 1); |
| 141 | |
| 142 | GLenum gl_internal_format = GetGLInternalFormatForTextureFormat(m_config.format, true); |
| 143 | if (g_ogl_config.bSupportsTextureStorage && m_config.type == AbstractTextureType::Texture_CubeMap) |
| 144 | { |
| 145 | glTexStorage2D(target, m_config.levels, gl_internal_format, m_config.width, m_config.height); |
| 146 | } |
| 147 | else if (tex_config.IsMultisampled()) |
| 148 | { |
| 149 | ASSERT(g_ogl_config.bSupportsMSAA); |
| 150 | if (m_config.type == AbstractTextureType::Texture_2DArray) |
| 151 | { |
| 152 | if (g_ogl_config.SupportedMultisampleTexStorage != MultisampleTexStorageType::TexStorageNone) |
| 153 | { |
| 154 | glTexStorage3DMultisample(target, tex_config.samples, gl_internal_format, m_config.width, |
| 155 | m_config.height, m_config.layers, GL_FALSE); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | ASSERT(!g_ogl_config.bIsES); |
| 160 | glTexImage3DMultisample(target, tex_config.samples, gl_internal_format, m_config.width, |
| 161 | m_config.height, m_config.layers, GL_FALSE); |
| 162 | } |
| 163 | } |
| 164 | else if (m_config.type == AbstractTextureType::Texture_2D) |
| 165 | { |
| 166 | if (g_ogl_config.SupportedMultisampleTexStorage != MultisampleTexStorageType::TexStorageNone) |
| 167 | { |
| 168 | glTexStorage2DMultisample(target, tex_config.samples, gl_internal_format, m_config.width, |
| 169 | m_config.height, GL_FALSE); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | ASSERT(!g_ogl_config.bIsES); |
| 174 | glTexImage2DMultisample(target, tex_config.samples, gl_internal_format, m_config.width, |
| 175 | m_config.height, GL_FALSE); |
| 176 | } |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | ASSERT(false); |
| 181 | } |
| 182 | } |
| 183 | else if (g_ogl_config.bSupportsTextureStorage) |
| 184 | { |
| 185 | if (m_config.type == AbstractTextureType::Texture_2DArray) |
| 186 | { |
| 187 | glTexStorage3D(target, m_config.levels, gl_internal_format, m_config.width, m_config.height, |
| 188 | m_config.layers); |
| 189 | } |
| 190 | else if (m_config.type == AbstractTextureType::Texture_2D) |
| 191 | { |
| 192 | glTexStorage2D(target, m_config.levels, gl_internal_format, m_config.width, m_config.height); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | ASSERT(false); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (m_config.IsRenderTarget()) |
| 201 | { |
| 202 | // We can't render to compressed formats. |
| 203 | ASSERT(!IsCompressedFormat(m_config.format)); |
| 204 | if (!g_ogl_config.bSupportsTextureStorage && !tex_config.IsMultisampled()) |
| 205 | { |
| 206 | for (u32 level = 0; level < m_config.levels; level++) |
| 207 | { |
| 208 | glTexImage3D(target, level, gl_internal_format, std::max(m_config.width >> level, 1u), |
| 209 | std::max(m_config.height >> level, 1u), m_config.layers, 0, |
| 210 | GetGLFormatForTextureFormat(m_config.format), |
| 211 | GetGLTypeForTextureFormat(m_config.format), nullptr); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | OGLTexture::~OGLTexture() |
| 218 | { |
| 219 | GetOGLGfx()->UnbindTexture(this); |
| 220 | glDeleteTextures(1, &m_texId); |
| 221 | } |
| 222 | |
| 223 | void OGLTexture::CopyRectangleFromTexture(const AbstractTexture* src, |
| 224 | const MathUtil::Rectangle<int>& src_rect, u32 src_layer, |
| 225 | u32 src_level, const MathUtil::Rectangle<int>& dst_rect, |
| 226 | u32 dst_layer, u32 dst_level) |
| 227 | { |
| 228 | const OGLTexture* src_gltex = static_cast<const OGLTexture*>(src); |
| 229 | ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() && |
| 230 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 231 | if (g_ogl_config.bSupportsCopySubImage) |
| 232 | { |
| 233 | glCopyImageSubData(src_gltex->m_texId, src_gltex->GetGLTarget(), src_level, src_rect.left, |
| 234 | src_rect.top, src_layer, m_texId, GetGLTarget(), dst_level, dst_rect.left, |
| 235 | dst_rect.top, dst_layer, dst_rect.GetWidth(), dst_rect.GetHeight(), 1); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | BlitFramebuffer(const_cast<OGLTexture*>(src_gltex), src_rect, src_layer, src_level, dst_rect, |
| 240 | dst_layer, dst_level); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void OGLTexture::BlitFramebuffer(OGLTexture* srcentry, const MathUtil::Rectangle<int>& src_rect, |
| 245 | u32 src_layer, u32 src_level, |
| 246 | const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer, |
| 247 | u32 dst_level) |
| 248 | { |
| 249 | GetOGLGfx()->BindSharedReadFramebuffer(); |
| 250 | glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, srcentry->m_texId, src_level, |
| 251 | src_layer); |
| 252 | GetOGLGfx()->BindSharedDrawFramebuffer(); |
| 253 | glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_texId, dst_level, |
| 254 | dst_layer); |
| 255 | |
| 256 | // glBlitFramebuffer is still affected by the scissor test, which is enabled by default. |
| 257 | glDisable(GL_SCISSOR_TEST); |
| 258 | |
| 259 | glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom, dst_rect.left, |
| 260 | dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 261 | |
| 262 | // The default state for the scissor test is enabled. We don't need to do a full state |
| 263 | // restore, as the framebuffer and scissor test are the only things we changed. |
| 264 | glEnable(GL_SCISSOR_TEST); |
| 265 | GetOGLGfx()->RestoreFramebufferBinding(); |
| 266 | } |
| 267 | |
| 268 | void OGLTexture::ResolveFromTexture(const AbstractTexture* src, |
| 269 | const MathUtil::Rectangle<int>& rect, u32 layer, u32 level) |
| 270 | { |
| 271 | const OGLTexture* srcentry = static_cast<const OGLTexture*>(src); |
| 272 | DEBUG_ASSERT(m_config.samples == 1 && m_config.width == srcentry->m_config.width && |
| 273 | m_config.height == srcentry->m_config.height && srcentry->m_config.samples > 1); |
| 274 | DEBUG_ASSERT(rect.left + rect.GetWidth() <= static_cast<int>(srcentry->m_config.width) && |
| 275 | rect.top + rect.GetHeight() <= static_cast<int>(srcentry->m_config.height)); |
| 276 | BlitFramebuffer(const_cast<OGLTexture*>(srcentry), rect, layer, level, rect, layer, level); |
| 277 | } |
| 278 | |
| 279 | void OGLTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, |
| 280 | size_t buffer_size, u32 layer) |
| 281 | { |
| 282 | if (level >= m_config.levels) |
| 283 | PanicAlertFmt("Texture only has {} levels, can't update level {}", m_config.levels, level); |
| 284 | |
| 285 | if (layer >= m_config.layers) |
| 286 | PanicAlertFmt("Texture only has {} layer, can't update layer {}", m_config.layers, layer); |
| 287 | |
| 288 | const auto expected_width = std::max(1U, m_config.width >> level); |
| 289 | const auto expected_height = std::max(1U, m_config.height >> level); |
| 290 | if (width != expected_width || height != expected_height) |
| 291 | { |
| 292 | PanicAlertFmt("Size of level {} must be {}x{}, but {}x{} requested", level, expected_width, |
| 293 | expected_height, width, height); |
| 294 | } |
| 295 | |
| 296 | const GLenum target = GetGLTarget(); |
| 297 | glActiveTexture(GL_MUTABLE_TEXTURE_INDEX); |
| 298 | glBindTexture(target, m_texId); |
| 299 | |
| 300 | if (row_length != width) |
| 301 | glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length); |
| 302 | |
| 303 | GLenum gl_internal_format = GetGLInternalFormatForTextureFormat(m_config.format, false); |
| 304 | if (IsCompressedFormat(m_config.format)) |
| 305 | { |
| 306 | if (m_config.type == AbstractTextureType::Texture_CubeMap) |
| 307 | { |
| 308 | if (g_ogl_config.bSupportsTextureStorage) |
| 309 | { |
| 310 | glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, level, 0, 0, width, |
| 311 | height, gl_internal_format, static_cast<GLsizei>(buffer_size), |
| 312 | buffer); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, level, gl_internal_format, |
| 317 | width, height, 0, static_cast<GLsizei>(buffer_size), buffer); |
| 318 | } |
| 319 | } |
| 320 | else if (m_config.type == AbstractTextureType::Texture_2D) |
| 321 | { |
| 322 | if (g_ogl_config.bSupportsTextureStorage) |
| 323 | { |
| 324 | glCompressedTexSubImage2D(target, level, 0, 0, width, height, gl_internal_format, |
| 325 | static_cast<GLsizei>(buffer_size), buffer); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | glCompressedTexImage2D(target, level, gl_internal_format, width, height, 0, |
| 330 | static_cast<GLsizei>(buffer_size), buffer); |
| 331 | } |
| 332 | } |
| 333 | else if (m_config.type == AbstractTextureType::Texture_2DArray) |
| 334 | { |
| 335 | if (g_ogl_config.bSupportsTextureStorage) |
| 336 | { |
| 337 | glCompressedTexSubImage3D(target, level, 0, 0, layer, width, height, 1, gl_internal_format, |
| 338 | static_cast<GLsizei>(buffer_size), buffer); |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | glCompressedTexImage3D(target, level, gl_internal_format, width, height, 1, 0, |
| 343 | static_cast<GLsizei>(buffer_size), buffer); |
| 344 | } |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | PanicAlertFmt("Failed to handle compressed texture load - unhandled type"); |
| 349 | } |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | GLenum gl_format = GetGLFormatForTextureFormat(m_config.format); |
| 354 | GLenum gl_type = GetGLTypeForTextureFormat(m_config.format); |
| 355 | if (m_config.type == AbstractTextureType::Texture_CubeMap) |
| 356 | { |
| 357 | if (g_ogl_config.bSupportsTextureStorage) |
| 358 | { |
| 359 | glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, level, 0, 0, width, height, |
| 360 | gl_format, gl_type, buffer); |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, level, gl_internal_format, width, |
| 365 | height, 0, gl_format, gl_type, buffer); |
| 366 | } |
| 367 | } |
| 368 | else if (m_config.type == AbstractTextureType::Texture_2D) |
| 369 | { |
| 370 | if (g_ogl_config.bSupportsTextureStorage) |
| 371 | { |
| 372 | glTexSubImage2D(target, level, 0, 0, width, height, gl_format, gl_type, buffer); |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | glTexImage2D(target, level, gl_internal_format, width, height, 0, gl_format, gl_type, |
| 377 | buffer); |
| 378 | } |
| 379 | } |
| 380 | else if (m_config.type == AbstractTextureType::Texture_2DArray) |
| 381 | { |
| 382 | if (g_ogl_config.bSupportsTextureStorage) |
| 383 | { |
| 384 | glTexSubImage3D(target, level, 0, 0, layer, width, height, 1, gl_format, gl_type, buffer); |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | glTexImage3D(target, level, gl_internal_format, width, height, 1, 0, gl_format, gl_type, |
| 389 | buffer); |
| 390 | } |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | PanicAlertFmt("Failed to handle texture load - unhandled type"); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (row_length != width) |
| 399 | glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); |
| 400 | } |
| 401 | |
| 402 | GLenum OGLTexture::GetGLFormatForImageTexture() const |
| 403 | { |
| 404 | return GetGLInternalFormatForTextureFormat(m_config.format, true); |
| 405 | } |
| 406 | |
| 407 | OGLStagingTexture::OGLStagingTexture(StagingTextureType type, const TextureConfig& config, |
| 408 | GLenum target, GLuint buffer_name, size_t buffer_size, |
| 409 | char* map_ptr, size_t map_stride) |
| 410 | : AbstractStagingTexture(type, config), m_target(target), m_buffer_name(buffer_name), |
| 411 | m_buffer_size(buffer_size) |
| 412 | { |
| 413 | m_map_pointer = map_ptr; |
| 414 | m_map_stride = map_stride; |
| 415 | } |
| 416 | |
| 417 | OGLStagingTexture::~OGLStagingTexture() |
| 418 | { |
| 419 | if (m_fence != nullptr) |
| 420 | glDeleteSync(m_fence); |
| 421 | if (m_map_pointer) |
| 422 | { |
| 423 | glBindBuffer(GL_PIXEL_PACK_BUFFER, m_buffer_name); |
| 424 | glUnmapBuffer(GL_PIXEL_PACK_BUFFER); |
| 425 | glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); |
| 426 | } |
| 427 | if (m_buffer_name != 0) |
| 428 | glDeleteBuffers(1, &m_buffer_name); |
| 429 | } |
| 430 | |
| 431 | std::unique_ptr<OGLStagingTexture> OGLStagingTexture::Create(StagingTextureType type, |
| 432 | const TextureConfig& config) |
| 433 | { |
| 434 | size_t stride = config.GetStride(); |
| 435 | size_t buffer_size = stride * config.height; |
| 436 | GLenum target = |
| 437 | type == StagingTextureType::Readback ? GL_PIXEL_PACK_BUFFER : GL_PIXEL_UNPACK_BUFFER; |
| 438 | GLuint buffer; |
| 439 | glGenBuffers(1, &buffer); |
| 440 | glBindBuffer(target, buffer); |
| 441 | |
| 442 | // Prefer using buffer_storage where possible. This allows us to skip the map/unmap steps. |
| 443 | char* buffer_ptr; |
| 444 | if (UsePersistentStagingBuffers()) |
| 445 | { |
| 446 | GLenum buffer_flags; |
| 447 | GLenum map_flags; |
| 448 | if (type == StagingTextureType::Readback) |
| 449 | { |
| 450 | buffer_flags = GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT; |
| 451 | map_flags = GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT; |
| 452 | } |
| 453 | else if (type == StagingTextureType::Upload) |
| 454 | { |
| 455 | buffer_flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT; |
| 456 | map_flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_FLUSH_EXPLICIT_BIT; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | buffer_flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT; |
| 461 | map_flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT; |
| 462 | } |
| 463 | |
| 464 | glBufferStorage(target, buffer_size, nullptr, buffer_flags); |
| 465 | buffer_ptr = static_cast<char*>(glMapBufferRange(target, 0, buffer_size, map_flags)); |
| 466 | ASSERT(buffer_ptr != nullptr); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | // Otherwise, fallback to mapping the buffer each time. |
| 471 | glBufferData(target, buffer_size, nullptr, |
| 472 | type == StagingTextureType::Readback ? GL_STREAM_READ : GL_STREAM_DRAW); |
| 473 | buffer_ptr = nullptr; |
| 474 | } |
| 475 | glBindBuffer(target, 0); |
| 476 | |
| 477 | return std::unique_ptr<OGLStagingTexture>( |
| 478 | new OGLStagingTexture(type, config, target, buffer, buffer_size, buffer_ptr, stride)); |
| 479 | } |
| 480 | |
| 481 | void OGLStagingTexture::CopyFromTexture(const AbstractTexture* src, |
| 482 | const MathUtil::Rectangle<int>& src_rect, u32 src_layer, |
| 483 | u32 src_level, const MathUtil::Rectangle<int>& dst_rect) |
| 484 | { |
| 485 | ASSERT(m_type == StagingTextureType::Readback || m_type == StagingTextureType::Mutable); |
| 486 | ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() && |
| 487 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 488 | ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= src->GetConfig().width && |
| 489 | src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= src->GetConfig().height); |
| 490 | ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= m_config.width && |
| 491 | dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= m_config.height); |
| 492 | |
| 493 | // Unmap the buffer before writing when not using persistent mappings. |
| 494 | if (!UsePersistentStagingBuffers()) |
| 495 | OGLStagingTexture::Unmap(); |
| 496 | |
| 497 | // Copy from the texture object to the staging buffer. |
| 498 | glBindBuffer(GL_PIXEL_PACK_BUFFER, m_buffer_name); |
| 499 | glPixelStorei(GL_PACK_ROW_LENGTH, m_config.width); |
| 500 | |
| 501 | const OGLTexture* gltex = static_cast<const OGLTexture*>(src); |
| 502 | const size_t dst_offset = dst_rect.top * m_config.GetStride() + dst_rect.left * m_texel_size; |
| 503 | |
| 504 | // Prefer glGetTextureSubImage(), when available. |
| 505 | if (g_ogl_config.bSupportsTextureSubImage) |
| 506 | { |
| 507 | glGetTextureSubImage( |
| 508 | gltex->GetGLTextureId(), src_level, src_rect.left, src_rect.top, src_layer, |
| 509 | src_rect.GetWidth(), src_rect.GetHeight(), 1, GetGLFormatForTextureFormat(src->GetFormat()), |
| 510 | GetGLTypeForTextureFormat(src->GetFormat()), |
| 511 | static_cast<GLsizei>(m_buffer_size - dst_offset), reinterpret_cast<void*>(dst_offset)); |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | // Mutate the shared framebuffer. |
| 516 | GetOGLGfx()->BindSharedReadFramebuffer(); |
| 517 | if (AbstractTexture::IsDepthFormat(gltex->GetFormat())) |
| 518 | { |
| 519 | glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 0, 0, 0); |
| 520 | glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gltex->GetGLTextureId(), |
| 521 | src_level, src_layer); |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, gltex->GetGLTextureId(), |
| 526 | src_level, src_layer); |
| 527 | glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 0, 0, 0); |
| 528 | } |
| 529 | glReadPixels(src_rect.left, src_rect.top, src_rect.GetWidth(), src_rect.GetHeight(), |
| 530 | GetGLFormatForTextureFormat(src->GetFormat()), |
| 531 | GetGLTypeForTextureFormat(src->GetFormat()), reinterpret_cast<void*>(dst_offset)); |
| 532 | GetOGLGfx()->RestoreFramebufferBinding(); |
| 533 | } |
| 534 | |
| 535 | glPixelStorei(GL_PACK_ROW_LENGTH, 0); |
| 536 | glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); |
| 537 | |
| 538 | // If we support buffer storage, create a fence for synchronization. |
| 539 | if (UsePersistentStagingBuffers()) |
| 540 | { |
| 541 | if (m_fence != nullptr) |
| 542 | glDeleteSync(m_fence); |
| 543 | |
| 544 | glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); |
| 545 | m_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 546 | glFlush(); |
| 547 | } |
| 548 | |
| 549 | m_needs_flush = true; |
| 550 | } |
| 551 | |
| 552 | void OGLStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, |
| 553 | AbstractTexture* dst, |
| 554 | const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer, |
| 555 | u32 dst_level) |
| 556 | { |
| 557 | ASSERT(m_type == StagingTextureType::Upload || m_type == StagingTextureType::Mutable); |
| 558 | ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() && |
| 559 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 560 | ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= m_config.width && |
| 561 | src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= m_config.height); |
| 562 | ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= dst->GetConfig().width && |
| 563 | dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= dst->GetConfig().height); |
| 564 | |
| 565 | const OGLTexture* gltex = static_cast<const OGLTexture*>(dst); |
| 566 | const size_t src_offset = src_rect.top * m_config.GetStride() + src_rect.left * m_texel_size; |
| 567 | const size_t copy_size = src_rect.GetHeight() * m_config.GetStride(); |
| 568 | |
| 569 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, m_buffer_name); |
| 570 | glPixelStorei(GL_UNPACK_ROW_LENGTH, m_config.width); |
| 571 | |
| 572 | if (!UsePersistentStagingBuffers()) |
| 573 | { |
| 574 | // Unmap the buffer before writing when not using persistent mappings. |
| 575 | if (m_map_pointer) |
| 576 | { |
| 577 | glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 578 | m_map_pointer = nullptr; |
| 579 | } |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | // Since we're not using coherent mapping, we must flush the range explicitly. |
| 584 | if (m_type == StagingTextureType::Upload) |
| 585 | glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, src_offset, copy_size); |
| 586 | glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); |
| 587 | } |
| 588 | |
| 589 | // Copy from the staging buffer to the texture object. |
| 590 | const GLenum target = gltex->GetGLTarget(); |
| 591 | glActiveTexture(GL_MUTABLE_TEXTURE_INDEX); |
| 592 | glBindTexture(target, gltex->GetGLTextureId()); |
| 593 | glTexSubImage3D(target, 0, dst_rect.left, dst_rect.top, dst_layer, dst_rect.GetWidth(), |
| 594 | dst_rect.GetHeight(), 1, GetGLFormatForTextureFormat(dst->GetFormat()), |
| 595 | GetGLTypeForTextureFormat(dst->GetFormat()), reinterpret_cast<void*>(src_offset)); |
| 596 | glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); |
| 597 | glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
| 598 | |
| 599 | // If we support buffer storage, create a fence for synchronization. |
| 600 | if (UsePersistentStagingBuffers()) |
| 601 | { |
| 602 | if (m_fence != nullptr) |
| 603 | glDeleteSync(m_fence); |
| 604 | |
| 605 | m_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 606 | glFlush(); |
| 607 | } |
| 608 | |
| 609 | m_needs_flush = true; |
| 610 | } |
| 611 | |
| 612 | void OGLStagingTexture::Flush() |
| 613 | { |
| 614 | // No-op when not using buffer storage, as the transfers happen on Map(). |
| 615 | // m_fence will always be zero in this case. |
| 616 | if (m_fence == nullptr) |
| 617 | { |
| 618 | m_needs_flush = false; |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | glClientWaitSync(m_fence, 0, GL_TIMEOUT_IGNORED); |
| 623 | glDeleteSync(m_fence); |
| 624 | m_fence = nullptr; |
| 625 | m_needs_flush = false; |
| 626 | } |
| 627 | |
| 628 | bool OGLStagingTexture::Map() |
| 629 | { |
| 630 | if (m_map_pointer) |
| 631 | return true; |
| 632 | |
| 633 | // Slow path, map the texture, unmap it later. |
| 634 | GLenum flags; |
| 635 | if (m_type == StagingTextureType::Readback) |
| 636 | flags = GL_MAP_READ_BIT; |
| 637 | else if (m_type == StagingTextureType::Upload) |
| 638 | flags = GL_MAP_WRITE_BIT; |
| 639 | else |
| 640 | flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT; |
| 641 | glBindBuffer(m_target, m_buffer_name); |
| 642 | m_map_pointer = static_cast<char*>(glMapBufferRange(m_target, 0, m_buffer_size, flags)); |
| 643 | glBindBuffer(m_target, 0); |
| 644 | return m_map_pointer != nullptr; |
| 645 | } |
| 646 | |
| 647 | void OGLStagingTexture::Unmap() |
| 648 | { |
| 649 | // No-op with persistent mapped buffers. |
| 650 | if (!m_map_pointer || UsePersistentStagingBuffers()) |
| 651 | return; |
| 652 | |
| 653 | glBindBuffer(m_target, m_buffer_name); |
| 654 | glUnmapBuffer(m_target); |
| 655 | glBindBuffer(m_target, 0); |
| 656 | m_map_pointer = nullptr; |
| 657 | } |
| 658 | |
| 659 | OGLFramebuffer::OGLFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment, |
| 660 | std::vector<AbstractTexture*> additional_color_attachments, |
| 661 | AbstractTextureFormat color_format, |
| 662 | AbstractTextureFormat depth_format, u32 width, u32 height, |
| 663 | u32 layers, u32 samples, GLuint fbo) |
| 664 | : AbstractFramebuffer(color_attachment, depth_attachment, |
| 665 | std::move(additional_color_attachments), color_format, depth_format, |
| 666 | width, height, layers, samples), |
| 667 | m_fbo(fbo) |
| 668 | { |
| 669 | } |
| 670 | |
| 671 | OGLFramebuffer::~OGLFramebuffer() |
| 672 | { |
| 673 | glDeleteFramebuffers(1, &m_fbo); |
| 674 | } |
| 675 | |
| 676 | std::unique_ptr<OGLFramebuffer> |
| 677 | OGLFramebuffer::Create(OGLTexture* color_attachment, OGLTexture* depth_attachment, |
| 678 | std::vector<AbstractTexture*> additional_color_attachments) |
| 679 | { |
| 680 | if (!ValidateConfig(color_attachment, depth_attachment, additional_color_attachments)) |
| 681 | return nullptr; |
| 682 | |
| 683 | const AbstractTextureFormat color_format = |
| 684 | color_attachment ? color_attachment->GetFormat() : AbstractTextureFormat::Undefined; |
| 685 | const AbstractTextureFormat depth_format = |
| 686 | depth_attachment ? depth_attachment->GetFormat() : AbstractTextureFormat::Undefined; |
| 687 | const OGLTexture* either_attachment = color_attachment ? color_attachment : depth_attachment; |
| 688 | const u32 width = either_attachment->GetWidth(); |
| 689 | const u32 height = either_attachment->GetHeight(); |
| 690 | const u32 layers = either_attachment->GetLayers(); |
| 691 | const u32 samples = either_attachment->GetSamples(); |
| 692 | |
| 693 | GLuint fbo; |
| 694 | glGenFramebuffers(1, &fbo); |
| 695 | glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 696 | |
| 697 | std::vector<GLenum> buffers; |
| 698 | if (color_attachment) |
| 699 | { |
| 700 | if (color_attachment->GetConfig().layers > 1) |
| 701 | { |
| 702 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, color_attachment->GetGLTextureId(), |
| 703 | 0); |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 708 | color_attachment->GetGLTextureId(), 0, 0); |
| 709 | } |
| 710 | buffers.push_back(GL_COLOR_ATTACHMENT0); |
| 711 | } |
| 712 | |
| 713 | if (depth_attachment) |
| 714 | { |
| 715 | GLenum attachment = AbstractTexture::IsStencilFormat(depth_format) ? |
| 716 | GL_DEPTH_STENCIL_ATTACHMENT : |
| 717 | GL_DEPTH_ATTACHMENT; |
| 718 | if (depth_attachment->GetConfig().layers > 1) |
| 719 | { |
| 720 | glFramebufferTexture(GL_FRAMEBUFFER, attachment, depth_attachment->GetGLTextureId(), 0); |
| 721 | } |
| 722 | else |
| 723 | { |
| 724 | glFramebufferTextureLayer(GL_FRAMEBUFFER, attachment, depth_attachment->GetGLTextureId(), 0, |
| 725 | 0); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | for (std::size_t i = 0; i < additional_color_attachments.size(); i++) |
| 730 | { |
| 731 | const auto attachment_enum = static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + i + 1); |
| 732 | OGLTexture* attachment = static_cast<OGLTexture*>(additional_color_attachments[i]); |
| 733 | if (attachment->GetConfig().layers > 1) |
| 734 | { |
| 735 | glFramebufferTexture(GL_FRAMEBUFFER, attachment_enum, attachment->GetGLTextureId(), 0); |
| 736 | } |
| 737 | else |
| 738 | { |
| 739 | glFramebufferTextureLayer(GL_FRAMEBUFFER, attachment_enum, attachment->GetGLTextureId(), 0, |
| 740 | 0); |
| 741 | } |
| 742 | buffers.push_back(attachment_enum); |
| 743 | } |
| 744 | |
| 745 | glDrawBuffers(static_cast<GLsizei>(buffers.size()), buffers.data()); |
| 746 | |
| 747 | DEBUG_ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); |
| 748 | GetOGLGfx()->RestoreFramebufferBinding(); |
| 749 | |
| 750 | return std::make_unique<OGLFramebuffer>(color_attachment, depth_attachment, |
| 751 | std::move(additional_color_attachments), color_format, |
| 752 | depth_format, width, height, layers, samples, fbo); |
| 753 | } |
| 754 | |
| 755 | void OGLFramebuffer::UpdateDimensions(u32 width, u32 height) |
| 756 | { |
| 757 | m_width = width; |
| 758 | m_height = height; |
| 759 | } |
| 760 | |
| 761 | } // namespace OGL |