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

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

#include "base/assert.hpp"
#include "base/logging.hpp"

#ifdef DEBUG
  #include "../std/map.hpp"
#endif

namespace dp
{

#ifdef DEBUG
  class UniformValidator
  {
  private:
    uint32_t m_programID;
    map<string, UniformTypeAndSize> m_uniformsMap;

  public:
    UniformValidator(uint32_t programId)
      : m_programID(programId)
    {
      int32_t numberOfUnis = GLFunctions::glGetProgramiv(m_programID, gl_const::GLActiveUniforms);
      for (size_t unIndex = 0; unIndex < numberOfUnis; ++unIndex)
      {
        string name;
        glConst type;
        UniformSize size;
        GLCHECK(GLFunctions::glGetActiveUniform(m_programID, unIndex, &size, &type, name));
        m_uniformsMap[name] = make_pair(type, size);
      }
    }

    bool HasValidTypeAndSizeForName(string const & name, glConst type, UniformSize size)
    {
      map<string, UniformTypeAndSize>::iterator it = m_uniformsMap.find(name);
      if (it != m_uniformsMap.end())
      {
        UniformTypeAndSize actualParams = (*it).second;
        return type == actualParams.first && size == actualParams.second;
      }
      else
        return false;
    }
  };

  bool GpuProgram::HasUniform(string const & name, glConst type, UniformSize size)
  {
    return m_validator->HasValidTypeAndSizeForName(name, type, size);
  }
#endif // UniformValidator


GpuProgram::GpuProgram(RefPointer<Shader> vertexShader, RefPointer<Shader> fragmentShader)
{
  m_programID = GLFunctions::glCreateProgram();
  GLFunctions::glAttachShader(m_programID, vertexShader->GetID());
  GLFunctions::glAttachShader(m_programID, fragmentShader->GetID());

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

  GLFunctions::glDetachShader(m_programID, vertexShader->GetID());
  GLFunctions::glDetachShader(m_programID, fragmentShader->GetID());


#ifdef DEBUG
  m_validator.reset(new UniformValidator(m_programID));
#endif
}

GpuProgram::~GpuProgram()
{
  Unbind();
  GLFunctions::glDeleteProgram(m_programID);
}

void GpuProgram::Bind()
{
  GLFunctions::glUseProgram(m_programID);
}

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

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

int8_t GpuProgram::GetUniformLocation(string const & uniformName) const
{
  return GLFunctions::glGetUniformLocation(m_programID, uniformName);
}

} // namespace dp