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

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

#include "base/assert.hpp"

#include "std/cstring.hpp"

namespace dp
{

namespace
{
  bool IsMapBufferSupported()
  {
    static bool const isSupported = GLExtensionsList::Instance().IsSupported(GLExtensionsList::MapBuffer);
    return isSupported;
  }
}

glConst glTarget(GPUBuffer::Target t)
{
  if (t == GPUBuffer::ElementBuffer)
    return gl_const::GLArrayBuffer;

  return gl_const::GLElementArrayBuffer;
}

GPUBuffer::GPUBuffer(Target t, uint8_t elementSize, uint16_t capacity)
  : base_t(elementSize, capacity)
  , m_t(t)
#ifdef DEBUG
  , m_isMapped(false)
#endif
{
  m_bufferID = GLFunctions::glGenBuffer();
  Resize(capacity);
}

GPUBuffer::~GPUBuffer()
{
  GLFunctions::glBindBuffer(0, glTarget(m_t));
  GLFunctions::glDeleteBuffer(m_bufferID);
}

void GPUBuffer::UploadData(void const * data, uint16_t elementCount)
{
  ASSERT(m_isMapped == false, ());

  uint16_t currentSize = GetCurrentSize();
  uint8_t elementSize = GetElementSize();
  ASSERT(GetCapacity() >= elementCount + currentSize, ("Not enough memory to upload ", elementCount, " elements"));
  Bind();
  GLFunctions::glBufferSubData(glTarget(m_t), elementCount * elementSize, data, currentSize * elementSize);
  base_t::UploadData(elementCount);
}

void GPUBuffer::Bind()
{
  GLFunctions::glBindBuffer(m_bufferID, glTarget(m_t));
}

void * GPUBuffer::Map()
{
#ifdef DEBUG
  ASSERT(m_isMapped == false, ());
  m_isMapped = true;
#endif

  if (IsMapBufferSupported())
    return GLFunctions::glMapBuffer(glTarget(m_t));

  return NULL;
}

void GPUBuffer::UpdateData(void * gpuPtr, void const * data, uint16_t elementOffset, uint16_t elementCount)
{
  uint16_t const elementSize = GetElementSize();
  uint32_t const byteOffset = elementOffset * (uint32_t)elementSize;
  uint32_t const byteCount = elementCount * (uint32_t)elementSize;
  ASSERT(m_isMapped == true, ());
  if (IsMapBufferSupported())
  {
    ASSERT(gpuPtr != NULL, ());
    memcpy((uint8_t *)gpuPtr + byteOffset, data, byteCount);
  }
  else
  {
    ASSERT(gpuPtr == NULL, ());
    if (byteOffset == 0 && byteCount == GetCapacity())
      GLFunctions::glBufferData(glTarget(m_t), byteCount, data, gl_const::GLStaticDraw);
    else
      GLFunctions::glBufferSubData(glTarget(m_t), byteCount, data, byteOffset);
  }
}

void GPUBuffer::Unmap()
{
#ifdef DEBUG
  ASSERT(m_isMapped == true, ());
  m_isMapped = false;
#endif
  if (IsMapBufferSupported())
    GLFunctions::glUnmapBuffer(glTarget(m_t));
}

void GPUBuffer::Resize(uint16_t elementCount)
{
  base_t::Resize(elementCount);
  Bind();
  GLFunctions::glBufferData(glTarget(m_t), GetCapacity() * GetElementSize(), NULL, gl_const::GLStaticDraw);
}

////////////////////////////////////////////////////////////////////////////
GPUBufferMapper::GPUBufferMapper(RefPointer<GPUBuffer> buffer)
  : m_buffer(buffer)
{
#ifdef DEBUG
  if (m_buffer->m_t == GPUBuffer::ElementBuffer)
  {
    ASSERT(m_mappedDataBuffer == 0, ());
    m_mappedDataBuffer = m_buffer->m_bufferID;
  }
  else
  {
    ASSERT(m_mappedIndexBuffer == 0, ());
    m_mappedIndexBuffer = m_buffer->m_bufferID;
  }
#endif

  m_buffer->Bind();
  m_gpuPtr = m_buffer->Map();
}

GPUBufferMapper::~GPUBufferMapper()
{
#ifdef DEBUG
  if (m_buffer->m_t == GPUBuffer::ElementBuffer)
  {
    ASSERT(m_mappedDataBuffer == m_buffer->m_bufferID, ());
    m_mappedDataBuffer = 0;
  }
  else
  {
    ASSERT(m_mappedIndexBuffer == m_buffer->m_bufferID, ());
    m_mappedIndexBuffer = 0;
  }
#endif

  m_buffer->Unmap();
}

void GPUBufferMapper::UpdateData(void const * data, uint16_t elementOffset, uint16_t elementCount)
{
  m_buffer->UpdateData(m_gpuPtr, data, elementOffset, elementCount);
}

#ifdef DEBUG
  uint32_t GPUBufferMapper::m_mappedDataBuffer;
  uint32_t GPUBufferMapper::m_mappedIndexBuffer;
#endif

} // namespace dp