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

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

#include "shaders/programs.hpp"

#include "drape/attribute_provider.hpp"
#include "drape/batcher.hpp"
#include "drape/glsl_func.hpp"
#include "drape/glsl_types.hpp"
#include "drape/graphics_context.hpp"
#include "drape/texture_manager.hpp"

#include <memory>

namespace df
{
namespace
{
uint32_t const kDynamicStreamID = 0x7F;

struct CirclesPackStaticVertex
{
  using TNormal = glsl::vec3;

  CirclesPackStaticVertex() = default;
  explicit CirclesPackStaticVertex(TNormal const & normal) : m_normal(normal) {}

  TNormal m_normal;
};

dp::RenderState GetCirclesPackState(ref_ptr<dp::TextureManager> texMng)
{
  auto state = CreateRenderState(gpu::Program::CirclePoint, DepthLayer::OverlayLayer);
  state.SetColorTexture(texMng->GetSymbolsTexture());
  state.SetDepthTestEnabled(false);
  return state;
}

dp::BindingInfo const & GetCirclesPackStaticBindingInfo()
{
  static std::unique_ptr<dp::BindingInfo> s_info;
  if (s_info == nullptr)
  {
    dp::BindingFiller<CirclesPackStaticVertex> filler(1);
    filler.FillDecl<CirclesPackStaticVertex::TNormal>("a_normal");
    s_info.reset(new dp::BindingInfo(filler.m_info));
  }
  return *s_info;
}

dp::BindingInfo const & GetCirclesPackDynamicBindingInfo()
{
  static std::unique_ptr<dp::BindingInfo> s_info;
  if (s_info == nullptr)
  {
    dp::BindingFiller<CirclesPackDynamicVertex> filler(2, kDynamicStreamID);
    filler.FillDecl<CirclesPackDynamicVertex::TPosition>("a_position");
    filler.FillDecl<CirclesPackDynamicVertex::TColor>("a_color");
    s_info.reset(new dp::BindingInfo(filler.m_info));
  }
  return *s_info;
}
}  // namespace

CirclesPackHandle::CirclesPackHandle(size_t pointsCount)
  : OverlayHandle(FeatureID(), dp::Anchor::Center, 0 /* priority */, 1 /* minVisibleScale */, false)
  , m_needUpdate(false)
{
  m_buffer.resize(pointsCount * dp::Batcher::VertexPerQuad);
}

void CirclesPackHandle::GetAttributeMutation(ref_ptr<dp::AttributeBufferMutator> mutator) const
{
  if (!m_needUpdate)
    return;

  TOffsetNode const & node = GetOffsetNode(kDynamicStreamID);
  ASSERT(node.first.GetElementSize() == sizeof(CirclesPackDynamicVertex), ());
  ASSERT(node.second.m_count == m_buffer.size(), ());

  uint32_t const byteCount =
      static_cast<uint32_t>(m_buffer.size()) * sizeof(CirclesPackDynamicVertex);
  void * buffer = mutator->AllocateMutationBuffer(byteCount);
  memcpy(buffer, m_buffer.data(), byteCount);

  dp::MutateNode mutateNode;
  mutateNode.m_region = node.second;
  mutateNode.m_data = make_ref(buffer);
  mutator->AddMutation(node.first, mutateNode);

  m_needUpdate = false;
}

bool CirclesPackHandle::Update(ScreenBase const & screen)
{
  UNUSED_VALUE(screen);
  return true;
}

bool CirclesPackHandle::IndexesRequired() const { return false; }

m2::RectD CirclesPackHandle::GetPixelRect(ScreenBase const & screen, bool perspective) const
{
  UNUSED_VALUE(screen);
  UNUSED_VALUE(perspective);
  return m2::RectD();
}

void CirclesPackHandle::GetPixelShape(ScreenBase const & screen, bool perspective,
                                      Rects & rects) const
{
  UNUSED_VALUE(screen);
  UNUSED_VALUE(perspective);
}

void CirclesPackHandle::SetPoint(size_t index, m2::PointD const & position, float radius,
                                 dp::Color const & color)
{
  size_t bufferIndex = index * dp::Batcher::VertexPerQuad;
  ASSERT_GREATER_OR_EQUAL(bufferIndex, 0, ());
  ASSERT_LESS(bufferIndex, m_buffer.size(), ());

  for (size_t i = 0; i < dp::Batcher::VertexPerQuad; i++)
  {
    m_buffer[bufferIndex + i].m_position = glsl::vec3(position.x, position.y, radius);
    m_buffer[bufferIndex + i].m_color = glsl::ToVec4(color);
  }
  m_needUpdate = true;
}

void CirclesPackHandle::Clear()
{
  memset(m_buffer.data(), 0, m_buffer.size() * sizeof(CirclesPackDynamicVertex));
  m_needUpdate = true;
}

size_t CirclesPackHandle::GetPointsCount() const
{
  return m_buffer.size() / dp::Batcher::VertexPerQuad;
}

void CirclesPackShape::Draw(ref_ptr<dp::GraphicsContext> context, ref_ptr<dp::TextureManager> texMng,
                            CirclesPackRenderData & data)
{
  ASSERT_NOT_EQUAL(data.m_pointsCount, 0, ());

  uint32_t const kVerticesInPoint = dp::Batcher::VertexPerQuad;
  uint32_t const kIndicesInPoint = dp::Batcher::IndexPerQuad;
  std::vector<CirclesPackStaticVertex> staticVertexData;
  staticVertexData.reserve(data.m_pointsCount * kVerticesInPoint);
  for (size_t i = 0; i < data.m_pointsCount; i++)
  {
    staticVertexData.emplace_back(CirclesPackStaticVertex::TNormal(-1.0f, 1.0f, 1.0f));
    staticVertexData.emplace_back(CirclesPackStaticVertex::TNormal(-1.0f, -1.0f, 1.0f));
    staticVertexData.emplace_back(CirclesPackStaticVertex::TNormal(1.0f, 1.0f, 1.0f));
    staticVertexData.emplace_back(CirclesPackStaticVertex::TNormal(1.0f, -1.0f, 1.0f));
  }

  std::vector<CirclesPackDynamicVertex> dynamicVertexData;
  dynamicVertexData.resize(data.m_pointsCount * kVerticesInPoint);

  dp::Batcher batcher(data.m_pointsCount * kIndicesInPoint, data.m_pointsCount * kVerticesInPoint);
  batcher.SetBatcherHash(static_cast<uint64_t>(BatcherBucket::Overlay));
  dp::SessionGuard guard(context, batcher,
                         [&data](dp::RenderState const & state, drape_ptr<dp::RenderBucket> && b)
  {
    data.m_bucket = std::move(b);
    data.m_state = state;
  });

  drape_ptr<dp::OverlayHandle> handle = make_unique_dp<CirclesPackHandle>(data.m_pointsCount);

  dp::AttributeProvider provider(2 /* stream count */,
                                 static_cast<uint32_t>(staticVertexData.size()));
  provider.InitStream(0 /* stream index */, GetCirclesPackStaticBindingInfo(),
                      make_ref(staticVertexData.data()));
  provider.InitStream(1 /* stream index */, GetCirclesPackDynamicBindingInfo(),
                      make_ref(dynamicVertexData.data()));
  batcher.InsertListOfStrip(context, GetCirclesPackState(texMng), make_ref(&provider),
                            std::move(handle), kVerticesInPoint);

  context->Flush();
}
}  // namespace df