Welcome to mirror list, hosted at ThFree Co, Russian Federation.

texture_manager.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58fe6b1a3fa5dfed4ef6566629a892c634b5a6e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "texture_manager.hpp"
#include "symbols_texture.hpp"

#include "glfunctions.hpp"

#include "../base/stl_add.hpp"

#include "../coding/file_name_utils.hpp"

#include "../platform/platform.hpp"

#include "../std/vector.hpp"

class TextureManager::TextureSet
{
public:
  TextureSet(uint32_t maxSize)
    : m_maxSize(maxSize)
  {
  }

  ~TextureSet()
  {
    GetRangeDeletor(m_textures, MasterPointerDeleter())();
  }

  bool IsFull() const
  {
    return !(m_textures.size() < m_maxSize);
  }

  void AddTexture(TransferPointer<Texture> texture)
  {
    ASSERT(!IsFull(), ());
    m_textures.push_back(MasterPointer<Texture>(texture));
  }

  int32_t FindResource(Texture::Key const & key, m2::RectF & texRect, m2::PointU & pixelSize) const
  {
    for (size_t i = 0; i < m_textures.size(); ++i)
    {
      if (m_textures[i]->FindResource(key, texRect, pixelSize))
        return i;
    }

    return -1;
  }

  void BindTextureSet() const
  {
    for (size_t i = 0; i < m_textures.size(); ++i)
    {
      GLFunctions::glActiveTexture(GLConst::GLTexture0 + i);
      m_textures[i]->Bind();
    }
  }

  uint32_t GetSize() const
  {
    return m_textures.size();
  }

private:
  vector<MasterPointer<Texture> > m_textures;
  uint32_t m_maxSize;
};

void TextureManager::Init(const string & resourcePostfix)
{
  // in shader we handle only 8 textures
  m_maxTextureBlocks = min(8, GLFunctions::glGetInteger(GLConst::GLMaxFragmentTextures));
  SymbolsTexture * symbols = new SymbolsTexture();
  symbols->Load(my::JoinFoldersToPath(string("resources-") + resourcePostfix, "symbols"));

  m_textures.Reset(new TextureSet(m_maxTextureBlocks));
  m_textures->AddTexture(MovePointer<Texture>(symbols));
}

void TextureManager::Release()
{
  m_textures.Destroy();
}

void TextureManager::GetSymbolRegion(const string & symbolName, TextureRegion & symbol) const
{
  SymbolsTexture::SymbolKey key(symbolName);
  symbol.m_textureSet = 0;
  symbol.m_textureOffset = m_textures->FindResource(key, symbol.m_stRect, symbol.m_pixelSize);
  ASSERT(symbol.m_textureOffset != -1, ());
}

void TextureManager::BindTextureSet(uint32_t textureSet) const
{
  ASSERT_LESS(textureSet, 1, ()); // TODO replace 1 to m_textureSets.size()
  m_textures->BindTextureSet();
}

uint32_t TextureManager::GetTextureCount(uint32_t textureSet) const
{
  ASSERT_LESS(textureSet, 1, ()); // TODO replace 1 to m_textureSets.size()
  return m_textures->GetSize();
}