<commit_before>/*!
 * \file image_gl.cpp
 * \brief file image_gl.cpp
 *
 * Copyright 2016 by Intel.
 *
 * Contact: <EMAIL>
 *
 * This Source Code Form is subject to the
 * terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with
 * this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 *
 * \author <NAME> <<EMAIL>>
 *
 */


#include <vector>
#include <fastuidraw/gl_backend/ngl_header.hpp>
#include <fastuidraw/gl_backend/gl_get.hpp>
#include <fastuidraw/gl_backend/image_gl.hpp>
#include "private/texture_gl.hpp"
#include "private/bindless.hpp"
#include "../private/util_private.hpp"



namespace
{
  template<GLenum internal_format,
           GLenum external_format,
           GLenum mag_filter,
           GLenum min_filter>
  class Texture
  {
  public:
    typedef fastuidraw::gl::detail::TextureGL<GL_TEXTURE_2D_ARRAY,
                                              internal_format, external_format,
                                              GL_UNSIGNED_BYTE,
                                              mag_filter, min_filter> type;
  };

  class ColorBackingStoreGL:public fastuidraw::AtlasColorBackingStoreBase
  {
  public:
    ColorBackingStoreGL(int log2_tile_size, int log2_num_tiles_per_row_per_col, int number_layers, bool delayed);
    ~ColorBackingStoreGL() {}

    virtual
    void
    set_data(int mipmap_level, fastuidraw::ivec2 dst_xy, int dst_l, fastuidraw::ivec2 src_xy,
             unsigned int size, const fastuidraw::ImageSourceBase &data);
    virtual
    void
    set_data(int mipmap_level, fastuidraw::ivec2 dst_xy, int dst_l,
             unsigned int size, fastuidraw::u8vec4 color_value);

    virtual
    void
    flush(void)
    {
      m_backing_store.flush();
    }

    GLuint
    texture(void) const
    {
      return m_backing_store.texture();
    }

    static
    fastuidraw::ivec3
    store_size(int log2_tile_size, int log2_num_tiles_per_row_per_col, int num_layers);

    static
    fastuidraw::reference_counted_ptr<fastuidraw::AtlasColorBackingStoreBase>
    create(int log2_tile_size, int log2_num_tiles_per_row_per_col, int num_layers, bool delayed)
    {
      ColorBackingStoreGL *p;
      p = FASTUIDRAWnew ColorBackingStoreGL(log2_tile_size, log2_num_tiles_per_row_per_col, num_layers, delayed);
      return fastuidraw::reference_counted_ptr<fastuidraw::AtlasColorBackingStoreBase>(p);
    }

  protected:
    virtual
    void
    resize_implement(int new_num_layers)
    {
      fastuidraw::ivec3 dims(dimensions());
      dims.z() = new_num_layers;
      m_backing_store.resize(dims);
    }

  private:
    typedef Texture<GL_RGBA8, GL_RGBA, GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR>::type TextureGL;
    TextureGL m_backing_store;
    unsigned int m_number_mipmap_levels;
  };

  class IndexBackingStoreGL:public fastuidraw::AtlasIndexBackingStoreBase
  {
  public:
    IndexBackingStoreGL(int log2_tile_size,
                        int log2_num_index_tiles_per_row_per_col,
                        int num_layers,
                        bool delayed);

    ~IndexBackingStoreGL()
    {}

    virtual
    void
    set_data(int x, int y, int l,
             int w, int h,
             fastuidraw::c_array<const fastuidraw::ivec3> data,
             int slack,
             const fastuidraw::AtlasColorBackingStoreBase *C,
             int pcolor_tile_size);

    virtual
    void
    set_data(int x, int y, int l,
             int w, int h,
             fastuidraw::c_array<const fastuidraw::ivec3> data);

    virtual
    void
    flush(void)
    {
      m_backing_store.flush();
    }

    GLuint
    texture(void) const
    {
      return m_backing_store.texture();
    }

    static
    fastuidraw::ivec3
    store_size(int log2_tile_size,
               int log2_num_index_tiles_per_row_per_col,
               int num_layers);

    static
    fastuidraw::reference_counted_ptr<fastuidraw::AtlasIndexBackingStoreBase>
    create(int log2_tile_size,
           int log2_num_index_tiles_per_row_per_col,
           int num_layers, bool delayed)
    {
      IndexBackingStoreGL *p;
      p = FASTUIDRAWnew IndexBackingStoreGL(log2_tile_size,
                                           log2_num_index_tiles_per_row_per_col,
                                           num_layers, delayed);
      return fastuidraw::reference_counted_ptr<fastuidraw::AtlasIndexBackingStoreBase>(p);
    }

  protected:
    virtual
    void
    resize_implement(int new_num_layers)
    {
      fastuidraw::ivec3 dims(dimensions());
      dims.z() = new_num_layers;
      m_backing_store.resize(dims);
    }

  private:
    /*
     * Data packed is:
     *  - .x    ===> which x-tile.
     *  - .y    ===> which y-tile.
     *  - .z/.w ===> which layer packed as layer = 2*z + w
     * Note:
     *  ColorBackingStore must be no larger than 2^8 * color_tile_size
     *  For color_tile_size = 2^5, then value is 2^13 = 8192
     */
    typedef Texture<GL_RGBA8UI, GL_RGBA_INTEGER, GL_NEAREST, GL_NEAREST>::type TextureGL;
    TextureGL m_backing_store;
  };

  class TextureImagePrivate
  {
  public:
    TextureImagePrivate(GLuint tex, bool owns):
      m_texture(tex),
      m_owns_texture(owns)
    {}

    GLuint m_texture;
    bool m_owns_texture;
  };

  class ImageAtlasGLParamsPrivate
  {
  public:
    ImageAtlasGLParamsPrivate(void):
      m_log2_color_tile_size(5),
      m_log2_num_color_tiles_per_row_per_col(8),
      m_num_color_layers(1),
      m_log2_index_tile_size(2),
      m_log2_num_index_tiles_per_row_per_col(6),
      m_num_index_layers(4),
      m_delayed(false)
    {}

    int m_log2_color_tile_size;
    int m_log2_num_color_tiles_per_row_per_col;
    int m_num_color_layers;
    int m_log2_index_tile_size;
    int m_log2_num_index_tiles_per_row_per_col;
    int m_num_index_layers;
    bool m_delayed;
  };

  class ImageAtlasGLPrivate
  {
  public:
    ImageAtlasGLPrivate(const fastuidraw::gl::ImageAtlasGL::params &P):
      m_params(P)
    {}

    fastuidraw::gl::ImageAtlasGL::params m_params;
  };

} //namespace

////////////////////////////////////////////
// ColorBackingStoreGL methods
ColorBackingStoreGL::
ColorBackingStoreGL(int log2_tile_size,
                    int log2_num_tiles_per_row_per_col,
                    int number_layers,
                    bool delayed):
  fastuidraw::AtlasColorBackingStoreBase(store_size(log2_tile_size, log2_num_tiles_per_row_per_col, number_layers),
                                         true),
  m_backing_store(dimensions(), delayed, log2_tile_size),
  m_number_mipmap_levels(log2_tile_size)
{}

void
ColorBackingStoreGL::
set_data(int mipmap_level, fastuidraw::ivec2 dst_xy, int dst_l, fastuidraw::ivec2 src_xy,
         unsigned int size, const fastuidraw::ImageSourceBase &image_data)
{
  using namespace fastuidraw;

  if (mipmap_level >= m_backing_store.num_mipmaps())
    {
      return;<commit_msg>fastuidraw/gl_backend/image_gl: the number of mipmap levels possible is 1 + log2_color_tile_size<commit_after>/*!
 * \file image_gl.cpp
 * \brief file image_gl.cpp
 *
 * Copyright 2016 by Intel.
 *
 * Contact: <EMAIL>
 *
 * This Source Code Form is subject to the
 * terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with
 * this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 *
 * \author <NAME> <<EMAIL>>
 *
 */


#include <vector>
#include <fastuidraw/gl_backend/ngl_header.hpp>
#include <fastuidraw/gl_backend/gl_get.hpp>
#include <fastuidraw/gl_backend/image_gl.hpp>
#include "private/texture_gl.hpp"
#include "private/bindless.hpp"
#include "../private/util_private.hpp"



namespace
{
  template<GLenum internal_format,
           GLenum external_format,
           GLenum mag_filter,
           GLenum min_filter>
  class Texture
  {
  public:
    typedef fastuidraw::gl::detail::TextureGL<GL_TEXTURE_2D_ARRAY,
                                              internal_format, external_format,
                                              GL_UNSIGNED_BYTE,
                                              mag_filter, min_filter> type;
  };

  class ColorBackingStoreGL:public fastuidraw::AtlasColorBackingStoreBase
  {
  public:
    ColorBackingStoreGL(int log2_tile_size, int log2_num_tiles_per_row_per_col, int number_layers, bool delayed);
    ~ColorBackingStoreGL() {}

    virtual
    void
    set_data(int mipmap_level, fastuidraw::ivec2 dst_xy, int dst_l, fastuidraw::ivec2 src_xy,
             unsigned int size, const fastuidraw::ImageSourceBase &data);
    virtual
    void
    set_data(int mipmap_level, fastuidraw::ivec2 dst_xy, int dst_l,
             unsigned int size, fastuidraw::u8vec4 color_value);

    virtual
    void
    flush(void)
    {
      m_backing_store.flush();
    }

    GLuint
    texture(void) const
    {
      return m_backing_store.texture();
    }

    static
    fastuidraw::ivec3
    store_size(int log2_tile_size, int log2_num_tiles_per_row_per_col, int num_layers);

    static
    fastuidraw::reference_counted_ptr<fastuidraw::AtlasColorBackingStoreBase>
    create(int log2_tile_size, int log2_num_tiles_per_row_per_col, int num_layers, bool delayed)
    {
      ColorBackingStoreGL *p;
      p = FASTUIDRAWnew ColorBackingStoreGL(log2_tile_size, log2_num_tiles_per_row_per_col, num_layers, delayed);
      return fastuidraw::reference_counted_ptr<fastuidraw::AtlasColorBackingStoreBase>(p);
    }

  protected:
    virtual
    void
    resize_implement(int new_num_layers)
    {
      fastuidraw::ivec3 dims(dimensions());
      dims.z() = new_num_layers;
      m_backing_store.resize(dims);
    }

  private:
    typedef Texture<GL_RGBA8, GL_RGBA, GL_LINEAR, GL_NEAREST_MIPMAP_LINEAR>::type TextureGL;
    TextureGL m_backing_store;
    unsigned int m_number_mipmap_levels;
  };

  class IndexBackingStoreGL:public fastuidraw::AtlasIndexBackingStoreBase
  {
  public:
    IndexBackingStoreGL(int log2_tile_size,
                        int log2_num_index_tiles_per_row_per_col,
                        int num_layers,
                        bool delayed);

    ~IndexBackingStoreGL()
    {}

    virtual
    void
    set_data(int x, int y, int l,
             int w, int h,
             fastuidraw::c_array<const fastuidraw::ivec3> data,
             int slack,
             const fastuidraw::AtlasColorBackingStoreBase *C,
             int pcolor_tile_size);

    virtual
    void
    set_data(int x, int y, int l,
             int w, int h,
             fastuidraw::c_array<const fastuidraw::ivec3> data);

    virtual
    void
    flush(void)
    {
      m_backing_store.flush();
    }

    GLuint
    texture(void) const
    {
      return m_backing_store.texture();
    }

    static
    fastuidraw::ivec3
    store_size(int log2_tile_size,
               int log2_num_index_tiles_per_row_per_col,
               int num_layers);

    static
    fastuidraw::reference_counted_ptr<fastuidraw::AtlasIndexBackingStoreBase>
    create(int log2_tile_size,
           int log2_num_index_tiles_per_row_per_col,
           int num_layers, bool delayed)
    {
      IndexBackingStoreGL *p;
      p = FASTUIDRAWnew IndexBackingStoreGL(log2_tile_size,
                                           log2_num_index_tiles_per_row_per_col,
                                           num_layers, delayed);
      return fastuidraw::reference_counted_ptr<fastuidraw::AtlasIndexBackingStoreBase>(p);
    }

  protected:
    virtual
    void
    resize_implement(int new_num_layers)
    {
      fastuidraw::ivec3 dims(dimensions());
      dims.z() = new_num_layers;
      m_backing_store.resize(dims);
    }

  private:
    /*
     * Data packed is:
     *  - .x    ===> which x-tile.
     *  - .y    ===> which y-tile.
     *  - .z/.w ===> which layer packed as layer = 2*z + w
     * Note:
     *  ColorBackingStore must be no larger than 2^8 * color_tile_size
     *  For color_tile_size = 2^5, then value is 2^13 = 8192
     */
    typedef Texture<GL_RGBA8UI, GL_RGBA_INTEGER, GL_NEAREST, GL_NEAREST>::type TextureGL;
    TextureGL m_backing_store;
  };

  class TextureImagePrivate
  {
  public:
    TextureImagePrivate(GLuint tex, bool owns):
      m_texture(tex),
      m_owns_texture(owns)
    {}

    GLuint m_texture;
    bool m_owns_texture;
  };

  class ImageAtlasGLParamsPrivate
  {
  public:
    ImageAtlasGLParamsPrivate(void):
      m_log2_color_tile_size(5),
      m_log2_num_color_tiles_per_row_per_col(8),
      m_num_color_layers(1),
      m_log2_index_tile_size(2),
      m_log2_num_index_tiles_per_row_per_col(6),
      m_num_index_layers(4),
      m_delayed(false)
    {}

    int m_log2_color_tile_size;
    int m_log2_num_color_tiles_per_row_per_col;
    int m_num_color_layers;
    int m_log2_index_tile_size;
    int m_log2_num_index_tiles_per_row_per_col;
    int m_num_index_layers;
    bool m_delayed;
  };

  class ImageAtlasGLPrivate
  {
  public:
    ImageAtlasGLPrivate(const fastuidraw::gl::ImageAtlasGL::params &P):
      m_params(P)
    {}

    fastuidraw::gl::ImageAtlasGL::params m_params;
  };

} //namespace

////////////////////////////////////////////
// ColorBackingStoreGL methods
ColorBackingStoreGL::
ColorBackingStoreGL(int log2_tile_size,
                    int log2_num_tiles_per_row_per_col,
                    int number_layers,
                    bool delayed):
  fastuidraw::AtlasColorBackingStoreBase(store_size(log2_tile_size, log2_num_tiles_per_row_per_col, number_layers),
                                         true),
  m_backing_store(dimensions(), delayed, log2_tile_size),
  m_number_mipmap_levels(log2_tile_size + 1)
{}

void
ColorBackingStoreGL::
set_data(int mipmap_level, fastuidraw::ivec2 dst_xy, int dst_l, fastuidraw::ivec2 src_xy,
         unsigned int size, const fastuidraw::ImageSourceBase &image_data)
{
  using namespace fastuidraw;

  if (mipmap_level >= m_backing_store.num_mipmaps())
    {
      return;
{'fraction_non_alphanumeric': 0.08867883685691608, 'fraction_numerical': 0.010606825889313803, 'mean_word_length': 2.671523178807947, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067921', 'n_tokens_mistral': 4805, 'n_tokens_neox': 4687, 'n_words': 837}