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

hw_texture.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e6f09bef757b8f87649764dec3daca49e0812b0 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "hw_texture.hpp"

#include "glfunctions.hpp"
#include "glextensions_list.hpp"

#include "base/math.hpp"

#if defined(OMIM_OS_IPHONE)
#include "hw_texture_ios.hpp"
#endif

#define ASSERT_ID ASSERT(GetID() != -1, ())

namespace dp
{

HWTexture::HWTexture()
  : m_width(0)
  , m_height(0)
  , m_format(UNSPECIFIED)
  , m_textureID(-1)
  , m_filter(gl_const::GLLinear)
{
}

void HWTexture::Create(Params const & params)
{
  Create(params, nullptr);
}

void HWTexture::Create(Params const & params, ref_ptr<void> /*data*/)
{
  m_width = params.m_width;
  m_height = params.m_height;
  m_format = params.m_format;
  m_filter = params.m_filter;

#if defined(TRACK_GPU_MEM)
  glConst layout;
  glConst pixelType;
  UnpackFormat(format, layout, pixelType);

  uint32_t channelBitSize = 8;
  uint32_t channelCount = 4;
  if (pixelType == gl_const::GL4BitOnChannel)
    channelBitSize = 4;

  if (layout == gl_const::GLAlpha)
    channelCount = 1;

  uint32_t bitCount = channelBitSize * channelCount * m_width * m_height;
  uint32_t memSize = bitCount >> 3;
  dp::GPUMemTracker::Inst().AddAllocated("Texture", m_textureID, memSize);
  dp::GPUMemTracker::Inst().SetUsed("Texture", m_textureID, memSize);
#endif
}

TextureFormat HWTexture::GetFormat() const
{
  return m_format;
}

uint32_t HWTexture::GetWidth() const
{
  ASSERT_ID;
  return m_width;
}

uint32_t HWTexture::GetHeight() const
{
  ASSERT_ID;
  return m_height;
}

float HWTexture::GetS(uint32_t x) const
{
  ASSERT_ID;
  return x / (float)m_width;
}

float HWTexture::GetT(uint32_t y) const
{
  ASSERT_ID;
  return y / (float)m_height;
}

void HWTexture::UnpackFormat(TextureFormat format, glConst & layout, glConst & pixelType)
{
  switch (format)
  {
  case RGBA8:
    layout = gl_const::GLRGBA;
    pixelType = gl_const::GL8BitOnChannel;
    break;
  case ALPHA:
    layout = gl_const::GLAlpha;
    pixelType = gl_const::GL8BitOnChannel;
    break;
  default:
    ASSERT(false, ());
    break;
  }
}

void HWTexture::Bind() const
{
  ASSERT_ID;
  GLFunctions::glBindTexture(GetID());
}

void HWTexture::SetFilter(glConst filter)
{
  if (m_filter != filter)
  {
    m_filter = filter;
    GLFunctions::glTexParameter(gl_const::GLMinFilter, m_filter);
    GLFunctions::glTexParameter(gl_const::GLMagFilter, m_filter);
  }
}

int32_t HWTexture::GetID() const
{
  return m_textureID;
}

OpenGLHWTexture::~OpenGLHWTexture()
{
  if (m_textureID != -1)
    GLFunctions::glDeleteTexture(m_textureID);
}

void OpenGLHWTexture::Create(Params const & params, ref_ptr<void> data)
{
  TBase::Create(params, data);

  if (!GLExtensionsList::Instance().IsSupported(GLExtensionsList::TextureNPOT))
  {
    m_width = my::NextPowOf2(m_width);
    m_height = my::NextPowOf2(m_height);
  }

  m_textureID = GLFunctions::glGenTexture();
  Bind();

  glConst layout;
  glConst pixelType;
  UnpackFormat(m_format, layout, pixelType);

  GLFunctions::glTexImage2D(m_width, m_height, layout, pixelType, data.get());
  GLFunctions::glTexParameter(gl_const::GLMinFilter, params.m_filter);
  GLFunctions::glTexParameter(gl_const::GLMagFilter, params.m_filter);
  GLFunctions::glTexParameter(gl_const::GLWrapS, params.m_wrapSMode);
  GLFunctions::glTexParameter(gl_const::GLWrapT, params.m_wrapTMode);

  GLFunctions::glFlush();
  GLFunctions::glBindTexture(0);
}

void OpenGLHWTexture::UploadData(uint32_t x, uint32_t y, uint32_t width, uint32_t height, ref_ptr<void> data)
{
  ASSERT_ID;
  glConst layout;
  glConst pixelType;
  UnpackFormat(m_format, layout, pixelType);

  GLFunctions::glTexSubImage2D(x, y, width, height, layout, pixelType, data.get());
}

drape_ptr<HWTexture> OpenGLHWTextureAllocator::CreateTexture()
{
  return make_unique_dp<OpenGLHWTexture>();
}

drape_ptr<HWTextureAllocator> CreateAllocator()
{
#if defined(OMIM_OS_IPHONE) && !defined(OMIM_OS_IPHONE_SIMULATOR)
  return make_unique_dp<HWTextureAllocatorApple>();
#else
  return make_unique_dp<OpenGLHWTextureAllocator>();
#endif
}

ref_ptr<HWTextureAllocator> GetDefaultAllocator()
{
  static OpenGLHWTextureAllocator s_allocator;
  return make_ref<HWTextureAllocator>(&s_allocator);
}

}