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

gl_gpu_program.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a3bfedbcf3d6a0e9c071d373309238ffb030f93 (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
#include "drape/gl_gpu_program.hpp"
#include "drape/gl_functions.hpp"
#include "drape/render_state.hpp"
#include "drape/support_manager.hpp"

#include "base/logging.hpp"

#include <set>

namespace dp
{
GLGpuProgram::GLGpuProgram(std::string const & programName,
                           ref_ptr<Shader> vertexShader, ref_ptr<Shader> fragmentShader)
  : GpuProgram(programName)
  , m_vertexShader(vertexShader)
  , m_fragmentShader(fragmentShader)
{
  m_programID = GLFunctions::glCreateProgram();
  GLFunctions::glAttachShader(m_programID, m_vertexShader->GetID());
  GLFunctions::glAttachShader(m_programID, m_fragmentShader->GetID());

  std::string errorLog;
  if (!GLFunctions::glLinkProgram(m_programID, errorLog))
    LOG(LERROR, ("Program ", programName, " link error = ", errorLog));

  // On Tegra3 glGetActiveUniform isn't work if you detach shaders after linking.
  LoadUniformLocations();

  // On Tegra2 we cannot detach shaders at all.
  // https://devtalk.nvidia.com/default/topic/528941/alpha-blending-not-working-on-t20-and-t30-under-ice-cream-sandwich/
  if (!SupportManager::Instance().IsTegraDevice())
  {
    GLFunctions::glDetachShader(m_programID, m_vertexShader->GetID());
    GLFunctions::glDetachShader(m_programID, m_fragmentShader->GetID());
  }
}

GLGpuProgram::~GLGpuProgram()
{
  if (SupportManager::Instance().IsTegraDevice())
  {
    GLFunctions::glDetachShader(m_programID, m_vertexShader->GetID());
    GLFunctions::glDetachShader(m_programID, m_fragmentShader->GetID());
  }

  GLFunctions::glDeleteProgram(m_programID);
}

void GLGpuProgram::Bind()
{
  // Deactivate all unused textures.
  uint8_t const usedSlots = TextureState::GetLastUsedSlots();
  for (uint8_t i = m_textureSlotsCount; i < usedSlots; i++)
  {
    GLFunctions::glActiveTexture(gl_const::GLTexture0 + i);
    GLFunctions::glBindTexture(0);
  }

  GLFunctions::glUseProgram(m_programID);
}

void GLGpuProgram::Unbind()
{
  GLFunctions::glUseProgram(0);
}

int8_t GLGpuProgram::GetAttributeLocation(std::string const & attributeName) const
{
  return GLFunctions::glGetAttribLocation(m_programID, attributeName);
}

int8_t GLGpuProgram::GetUniformLocation(std::string const & uniformName) const
{
  auto const it = m_uniforms.find(uniformName);
  if (it == m_uniforms.end())
    return -1;

  return it->second.m_location;
}

glConst GLGpuProgram::GetUniformType(std::string const & uniformName) const
{
  auto const it = m_uniforms.find(uniformName);
  if (it == m_uniforms.end())
    return -1;

  return it->second.m_type;
}

GLGpuProgram::UniformsInfo const & GLGpuProgram::GetUniformsInfo() const
{
  return m_uniforms;
}

void GLGpuProgram::LoadUniformLocations()
{
  static std::set<glConst> const kSupportedTypes = {
      gl_const::GLFloatType, gl_const::GLFloatVec2, gl_const::GLFloatVec3, gl_const::GLFloatVec4,
      gl_const::GLIntType,   gl_const::GLIntVec2,   gl_const::GLIntVec3,   gl_const::GLIntVec4,
      gl_const::GLFloatMat4, gl_const::GLSampler2D};

  auto const uniformsCount = GLFunctions::glGetProgramiv(m_programID, gl_const::GLActiveUniforms);
  for (int i = 0; i < uniformsCount; ++i)
  {
    int32_t size = 0;
    UniformInfo info;
    std::string name;
    GLFunctions::glGetActiveUniform(m_programID, static_cast<uint32_t>(i), &size, &info.m_type, name);
    CHECK(kSupportedTypes.find(info.m_type) != kSupportedTypes.cend(),
          ("Used uniform has unsupported type. Program =", m_programName, "Type =", info.m_type));

    info.m_location = GLFunctions::glGetUniformLocation(m_programID, name);
    m_uniforms[name] = std::move(info);
  }
  m_numericUniformsCount = CalculateNumericUniformsCount();
  m_textureSlotsCount = static_cast<uint8_t>(m_uniforms.size() - m_numericUniformsCount);
}

uint32_t GLGpuProgram::CalculateNumericUniformsCount() const
{
  uint32_t counter = 0;
  for (auto const & u : m_uniforms)
  {
    if (u.second.m_type != gl_const::GLSampler2D)
      counter++;
  }
  return counter;
}
}  // namespace dp