<commit_before>/**
 ******************************************************************************
 * Xenia : Xbox 360 Emulator Research Project                                 *
 ******************************************************************************
 * Copyright 2014 <NAME>. All rights reserved.                             *
 * Released under the BSD license - see LICENSE in the root for more details. *
 ******************************************************************************
 */

#include "xenia/gpu/gl4/texture_cache.h"

#include <cstring>

#include "xenia/base/assert.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/memory.h"
#include "xenia/gpu/gpu-private.h"
#include "xenia/profiling.h"

namespace xe {
namespace gpu {
namespace gl4 {

using namespace xe::gpu::xenos;

extern "C" GLEWContext* glewGetContext();
extern "C" WGLEWContext* wglewGetContext();

struct TextureConfig {
  TextureFormat texture_format;
  GLenum internal_format;
  GLenum format;
  GLenum type;
};

// https://code.google.com/p/glsnewton/source/browse/trunk/Source/uDDSLoader.pas?r=62
// http://dench.flatlib.jp/opengl/textures
// http://fossies.org/linux/WebKit/Source/ThirdParty/ANGLE/src/libGLESv2/formatutils.cpp
static const TextureConfig texture_configs[64] = {
    {TextureFormat::k_1_REVERSE, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_1, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8, GL_R8, GL_RED, GL_UNSIGNED_BYTE},
    {TextureFormat::k_1_5_5_5, GL_RGB5_A1, GL_RGBA,
     GL_UNSIGNED_SHORT_1_5_5_5_REV},
    {TextureFormat::k_5_6_5, GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5},
    {TextureFormat::k_6_5_5, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8_8_8_8, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE},
    {TextureFormat::k_2_10_10_10, GL_RGB10_A2, GL_RGBA,
     GL_UNSIGNED_INT_2_10_10_10_REV},
    {TextureFormat::k_8_A, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8_B, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8_8, GL_RG8, GL_RG, GL_UNSIGNED_BYTE},
    {TextureFormat::k_Cr_Y1_Cb_Y0, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_Y1_Cr_Y0_Cb, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_8_8_8_8_A, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_4_4_4_4, GL_RGBA4, GL_RGBA,
     GL_UNSIGNED_SHORT_4_4_4_4_REV},
    {TextureFormat::k_10_11_11, GL_R11F_G11F_B10F, GL_RGB,
     GL_UNSIGNED_INT_10F_11F_11F_REV},  // ?
    {TextureFormat::k_11_11_10, GL_R11F_G11F_B10F, GL_RGB,
     GL_UNSIGNED_INT_10F_11F_11F_REV},  // ?
    {TextureFormat::k_DXT1, GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
     GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT2_3, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT4_5, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_24_8, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL,
     GL_UNSIGNED_INT_24_8},
    {TextureFormat::k_24_8_FLOAT, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL,
     GL_FLOAT_32_UNSIGNED_INT_24_8_REV},
    {TextureFormat::k_16, GL_R16, GL_RED, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16, GL_RG16, GL_RG, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16_16_16, GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_EXPAND, GL_R16, GL_RED, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16_EXPAND, GL_RG16, GL_RG, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16_16_16_EXPAND, GL_RGBA16, GL_RGBA,
     GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_FLOAT, GL_R16F, GL_RED, GL_HALF_FLOAT},
    {TextureFormat::k_16_16_FLOAT, GL_RG16F, GL_RG, GL_HALF_FLOAT},
    {TextureFormat::k_16_16_16_16_FLOAT, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT},
    {TextureFormat::k_32, GL_R32, GL_RED, GL_UNSIGNED_INT},
    {TextureFormat::k_32_32, GL_RG32, GL_RG, GL_UNSIGNED_INT},
    {TextureFormat::k_32_32_32_32, GL_RGBA32, GL_RGBA, GL_UNSIGNED_INT},
    {TextureFormat::k_32_FLOAT, GL_R32F, GL_RED, GL_FLOAT},
    {TextureFormat::k_32_32_FLOAT, GL_RG32F, GL_RG, GL_FLOAT},
    {TextureFormat::k_32_32_32_32_FLOAT, GL_RGBA32F, GL_RGBA, GL_FLOAT},
    {TextureFormat::k_32_AS_8, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_32_AS_8_8, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_MPEG, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_16_MPEG, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_8_INTERLACED, GL_R8I, GL_RED, GL_UNSIGNED_BYTE},
    {TextureFormat::k_32_AS_8_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_32_AS_8_8_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_INTERLACED, GL_R16I, GL_RED, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_MPEG_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_16_MPEG_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_DXN, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2,
     GL_INVALID_ENUM},
    {TextureFormat::k_8_8_8_8_AS_16_16_16_16, GL_RGBA8, GL_RGBA,
     GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT1_AS_16_16_16_16, GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
     GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT2_3_AS_16_16_16_16, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT4_5_AS_16_16_16_16, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_2_10_10_10_AS_16_16_16_16, GL_RGB10_A2, GL_RGBA,
     GL_UNSIGNED_INT_2_10_10_10_REV},
    {TextureFormat::k_10_11_11_AS_16_16_16_16, GL_R11F_G11F_B10F, GL_RGB,
     GL_UNSIGNED_INT_10F_11F_11F_REV},
    {TextureFormat::k_11_11_10_AS_16_16_16_16, GL_R11F_G11F_B10F,
     GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_32_32_32_FLOAT, GL_RGB32F, GL_RGB, GL_FLOAT},
    {TextureFormat::k_DXT3A, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_DXT5A, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_CTX1, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_DXT3A_AS_1_1_1_1, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
};

TextureCache::TextureCache() : memory_(nullptr), scratch_buffer_(nullptr) {
  invalidated_textures_sets_[0].reserve(64);
  invalidated_textures_sets_[1].reserve(64);
  invalidated_textures_ = &invalidated_textures_sets_[0];
}

TextureCache::~TextureCache() { Shutdown(); }

bool TextureCache::Initialize(Memory* memory, CircularBuffer* scratch_buffer) {
  memory_ = memory;
  scratch_buffer_ = scratch_buffer;
  return true;<commit_msg>Typo fix<commit_after>/**
 ******************************************************************************
 * Xenia : Xbox 360 Emulator Research Project                                 *
 ******************************************************************************
 * Copyright 2014 <NAME>. All rights reserved.                             *
 * Released under the BSD license - see LICENSE in the root for more details. *
 ******************************************************************************
 */

#include "xenia/gpu/gl4/texture_cache.h"

#include <cstring>

#include "xenia/base/assert.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/base/memory.h"
#include "xenia/gpu/gpu-private.h"
#include "xenia/profiling.h"

namespace xe {
namespace gpu {
namespace gl4 {

using namespace xe::gpu::xenos;

extern "C" GLEWContext* glewGetContext();
extern "C" WGLEWContext* wglewGetContext();

struct TextureConfig {
  TextureFormat texture_format;
  GLenum internal_format;
  GLenum format;
  GLenum type;
};

// https://code.google.com/p/glsnewton/source/browse/trunk/Source/uDDSLoader.pas?r=62
// http://dench.flatlib.jp/opengl/textures
// http://fossies.org/linux/WebKit/Source/ThirdParty/ANGLE/src/libGLESv2/formatutils.cpp
static const TextureConfig texture_configs[64] = {
    {TextureFormat::k_1_REVERSE, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_1, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8, GL_R8, GL_RED, GL_UNSIGNED_BYTE},
    {TextureFormat::k_1_5_5_5, GL_RGB5_A1, GL_RGBA,
     GL_UNSIGNED_SHORT_1_5_5_5_REV},
    {TextureFormat::k_5_6_5, GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5},
    {TextureFormat::k_6_5_5, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8_8_8_8, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE},
    {TextureFormat::k_2_10_10_10, GL_RGB10_A2, GL_RGBA,
     GL_UNSIGNED_INT_2_10_10_10_REV},
    {TextureFormat::k_8_A, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8_B, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_8_8, GL_RG8, GL_RG, GL_UNSIGNED_BYTE},
    {TextureFormat::k_Cr_Y1_Cb_Y0, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_Y1_Cr_Y0_Cb, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_8_8_8_8_A, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_4_4_4_4, GL_RGBA4, GL_RGBA,
     GL_UNSIGNED_SHORT_4_4_4_4_REV},
    {TextureFormat::k_10_11_11, GL_R11F_G11F_B10F, GL_RGB,
     GL_UNSIGNED_INT_10F_11F_11F_REV},  // ?
    {TextureFormat::k_11_11_10, GL_R11F_G11F_B10F, GL_RGB,
     GL_UNSIGNED_INT_10F_11F_11F_REV},  // ?
    {TextureFormat::k_DXT1, GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
     GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT2_3, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT4_5, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_24_8, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL,
     GL_UNSIGNED_INT_24_8},
    {TextureFormat::k_24_8_FLOAT, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL,
     GL_FLOAT_32_UNSIGNED_INT_24_8_REV},
    {TextureFormat::k_16, GL_R16, GL_RED, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16, GL_RG16, GL_RG, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16_16_16, GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_EXPAND, GL_R16, GL_RED, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16_EXPAND, GL_RG16, GL_RG, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_16_16_16_EXPAND, GL_RGBA16, GL_RGBA,
     GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_FLOAT, GL_R16F, GL_RED, GL_HALF_FLOAT},
    {TextureFormat::k_16_16_FLOAT, GL_RG16F, GL_RG, GL_HALF_FLOAT},
    {TextureFormat::k_16_16_16_16_FLOAT, GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT},
    {TextureFormat::k_32, GL_R32I, GL_RED, GL_UNSIGNED_INT},
    {TextureFormat::k_32_32, GL_RG32I, GL_RG, GL_UNSIGNED_INT},
    {TextureFormat::k_32_32_32_32, GL_RGBA32I, GL_RGBA, GL_UNSIGNED_INT},
    {TextureFormat::k_32_FLOAT, GL_R32F, GL_RED, GL_FLOAT},
    {TextureFormat::k_32_32_FLOAT, GL_RG32F, GL_RG, GL_FLOAT},
    {TextureFormat::k_32_32_32_32_FLOAT, GL_RGBA32F, GL_RGBA, GL_FLOAT},
    {TextureFormat::k_32_AS_8, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_32_AS_8_8, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_MPEG, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_16_MPEG, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_8_INTERLACED, GL_R8I, GL_RED, GL_UNSIGNED_BYTE},
    {TextureFormat::k_32_AS_8_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_32_AS_8_8_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_INTERLACED, GL_R16I, GL_RED, GL_UNSIGNED_SHORT},
    {TextureFormat::k_16_MPEG_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_16_16_MPEG_INTERLACED, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::k_DXN, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2,
     GL_INVALID_ENUM},
    {TextureFormat::k_8_8_8_8_AS_16_16_16_16, GL_RGBA8, GL_RGBA,
     GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT1_AS_16_16_16_16, GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
     GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT2_3_AS_16_16_16_16, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT4_5_AS_16_16_16_16, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_2_10_10_10_AS_16_16_16_16, GL_RGB10_A2, GL_RGBA,
     GL_UNSIGNED_INT_2_10_10_10_REV},
    {TextureFormat::k_10_11_11_AS_16_16_16_16, GL_R11F_G11F_B10F, GL_RGB,
     GL_UNSIGNED_INT_10F_11F_11F_REV},
    {TextureFormat::k_11_11_10_AS_16_16_16_16, GL_R11F_G11F_B10F,
     GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_32_32_32_FLOAT, GL_RGB32F, GL_RGB, GL_FLOAT},
    {TextureFormat::k_DXT3A, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_DXT5A, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
     GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_UNSIGNED_BYTE},
    {TextureFormat::k_CTX1, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM},
    {TextureFormat::k_DXT3A_AS_1_1_1_1, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
    {TextureFormat::kUnknown, GL_INVALID_ENUM, GL_INVALID_ENUM,
     GL_INVALID_ENUM},
};

TextureCache::TextureCache() : memory_(nullptr), scratch_buffer_(nullptr) {
  invalidated_textures_sets_[0].reserve(64);
  invalidated_textures_sets_[1].reserve(64);
  invalidated_textures_ = &invalidated_textures_sets_[0];
}

TextureCache::~TextureCache() { Shutdown(); }

bool TextureCache::Initialize(Memory* memory, CircularBuffer* scratch_buffer) {
  memory_ = memory;
  scratch_buffer_ = scratch_buffer;
  return true;
{'fraction_non_alphanumeric': 0.12754757727088675, 'fraction_numerical': 0.06006208665136996, 'mean_word_length': 6.398402396405392, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3204299', 'n_tokens_mistral': 6598, 'n_tokens_neox': 6337, 'n_words': 704}