Skip to content

Commit 1998e48

Browse files
committed
move packLineStyleMetadata into helper
1 parent aa0637d commit 1998e48

4 files changed

Lines changed: 23 additions & 36 deletions

File tree

ios/graphics/Shader/Metal/LineShader.metal

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ struct LineVertexIn {
2727
float2 lineA [[attribute(0)]];
2828
float2 lineB [[attribute(1)]];
2929
/*
30-
lineStyleInfo is packed as a single float but contains multiple pieces of information:
30+
lineMetadata is packed as a single float but contains multiple pieces of information:
3131
- Bits 0-1 : Vertex Index (0-3) (used for rendering different parts of the quad)
3232
- Bits 2-9 : Line Style Index (0-255) (defines the appearance of the line)
3333
- Bits 10-11 : Segment Type (0 = inner, 1 = start, 2 = end, 3 = single-segment)
3434
*/
35-
float lineStyleInfo [[attribute(2)]];
35+
float lineMetadata [[attribute(2)]];
3636
};
3737

3838
struct LineVertexUnitSphereIn {
3939
float3 lineA [[attribute(0)]];
4040
float3 lineB [[attribute(1)]];
4141
/*
42-
lineStyleInfo is packed as a single float but contains multiple pieces of information:
42+
lineMetadata is packed as a single float but contains multiple pieces of information:
4343
- Bits 0-1 : Vertex Index (0-3) (used for rendering different parts of the quad)
4444
- Bits 2-9 : Line Style Index (0-255) (defines the appearance of the line)
4545
- Bits 10-11 : Segment Type (0 = inner, 1 = start, 2 = end, 3 = single-segment)
4646
*/
47-
float lineStyleInfo [[attribute(2)]];
47+
float lineMetadata [[attribute(2)]];
4848
};
4949

5050
struct LineVertexOut {
@@ -115,7 +115,7 @@ unitSphereSimpleLineGroupVertexShader(const LineVertexUnitSphereIn vertexIn [[st
115115
constant float4 &originOffset [[buffer(5)]],
116116
constant float4 &tileOrigin [[buffer(6)]])
117117
{
118-
const uint packed = as_type<uint>(vertexIn.lineStyleInfo); // Reinterpret float as uint
118+
const uint packed = as_type<uint>(vertexIn.lineMetadata); // Reinterpret float as uint
119119
const uint vertexIndex = packed & 0x3; // Bits 0-1
120120
const uint styleIndex = (packed >> 2) & 0xFF; // Bits 2-9
121121
const uint segmentType = (packed >> 10) & 0x3; // Bits 10-11
@@ -184,7 +184,7 @@ simpleLineGroupVertexShader(const LineVertexIn vertexIn [[stage_in]],
184184
constant float4 &originOffset [[buffer(5)]],
185185
constant float4 &tileOrigin [[buffer(6)]])
186186
{
187-
const uint packed = as_type<uint>(vertexIn.lineStyleInfo); // Reinterpret float as uint
187+
const uint packed = as_type<uint>(vertexIn.lineMetadata); // Reinterpret float as uint
188188
const uint vertexIndex = packed & 0x3; // Bits 0-1
189189
const uint styleIndex = (packed >> 2) & 0xFF; // Bits 2-9
190190
const uint segmentType = (packed >> 10) & 0x3; // Bits 10-11
@@ -304,7 +304,7 @@ unitSphereLineGroupVertexShader(const LineVertexUnitSphereIn vertexIn [[stage_in
304304
constant float4 &tileOrigin [[buffer(6)]])
305305
{
306306

307-
const uint packed = as_type<uint>(vertexIn.lineStyleInfo); // Reinterpret float as uint
307+
const uint packed = as_type<uint>(vertexIn.lineMetadata); // Reinterpret float as uint
308308
const uint vertexIndex = packed & 0x3; // Bits 0-1
309309
const uint styleIndex = (packed >> 2) & 0xFF; // Bits 2-9
310310
const uint segmentType = (packed >> 10) & 0x3; // Bits 10-11
@@ -384,7 +384,7 @@ lineGroupVertexShader(const LineVertexIn vertexIn [[stage_in]],
384384
constant float4 &originOffset [[buffer(5)]],
385385
constant float4 &tileOrigin [[buffer(6)]])
386386
{
387-
const uint packed = as_type<uint>(vertexIn.lineStyleInfo); // Reinterpret float as uint
387+
const uint packed = as_type<uint>(vertexIn.lineMetadata); // Reinterpret float as uint
388388
const uint vertexIndex = packed & 0x3; // Bits 0-1
389389
const uint styleIndex = (packed >> 2) & 0xFF; // Bits 2-9
390390
const uint segmentType = (packed >> 10) & 0x3; // Bits 10-11

shared/public/LineHelper.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,17 @@ class LineHelper {
7272
return newPolyline;
7373
}
7474

75-
75+
inline static float packLineStyleMetadata(uint8_t vertexIndex, uint8_t lineStyleIndex, uint8_t segmentType, float prefixTotalLineLength) {
76+
uint32_t packed =
77+
(vertexIndex & 0x3)
78+
| ((lineStyleIndex & 0xFF) << 2)
79+
| ((segmentType & 0x3) << 10)
80+
| ((int(prefixTotalLineLength * 1000.0) & 0x1FFFFF) << 11);
81+
82+
float result;
83+
std::memcpy(&result, &packed, sizeof(float)); // Preserve all 32 bits
84+
return result;
85+
}
7686

7787
private:
7888
static float distanceSquared(const Coord& pt, const Coord &p1, const Coord &p2)

shared/src/map/layers/objects/Line2dLayerObject.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "Line2dLayerObject.h"
1212
#include "Vec3D.h"
13+
#include "LineHelper.h"
1314
#include <cmath>
1415

1516
Line2dLayerObject::Line2dLayerObject(const std::shared_ptr<CoordinateConversionHelperInterface> &conversionHelper,
@@ -63,12 +64,6 @@ void Line2dLayerObject::setPositions(const std::vector<Coord> &positions, const
6364
uint8_t segmentType = (i == 0 && i == iSecondToLast) ? 3 :
6465
(i == 0) ? 1 :
6566
(i == iSecondToLast) ? 2 : 0;
66-
const int lineStyleIndex = 0;
67-
float lineStyleInfo = static_cast<float>(
68-
(segmentType << 10) | // 2 bits for segmentType (shifted left)
69-
(lineStyleIndex << 2) | // 8 bits for lineStyleIndex
70-
0 // Reserve 2 bits for vertexIndex
71-
);
7267

7368
for (uint8_t vertexIndex = 4; vertexIndex > 0; --vertexIndex) {
7469
// Vertex
@@ -84,12 +79,7 @@ void Line2dLayerObject::setPositions(const std::vector<Coord> &positions, const
8479
lineAttributes.push_back(pNext.z);
8580
}
8681

87-
// Segment Start Length Position (length prefix sum)
88-
lineAttributes.push_back(prefixTotalLineLength);
89-
90-
// Vertex Index (store in last 2 bits)
91-
float packedStyleInfo = lineStyleInfo + (vertexIndex - 1);
92-
lineAttributes.push_back(packedStyleInfo);
82+
lineAttributes.push_back(LineHelper::packLineStyleMetadata(vertexIndex, 0, segmentType, prefixTotalLineLength));
9383
}
9484

9585
// Vertex indices

shared/src/map/layers/objects/LineGroup2dLayerObject.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "RenderLineDescription.h"
1313
#include "Logger.h"
1414
#include "ShaderLineStyle.h"
15-
15+
#include "LineHelper.h"
1616
#include <cmath>
1717

1818
LineGroup2dLayerObject::LineGroup2dLayerObject(const std::shared_ptr<CoordinateConversionHelperInterface> &conversionHelper,
@@ -29,18 +29,6 @@ void LineGroup2dLayerObject::update() {}
2929

3030
std::vector<std::shared_ptr<RenderConfigInterface>> LineGroup2dLayerObject::getRenderConfig() { return {renderConfig}; }
3131

32-
float packLineStyleMetadata(uint8_t vertexIndex, uint8_t lineStyleIndex, uint8_t segmentType, float prefixTotalLineLength) {
33-
uint32_t packed =
34-
(vertexIndex & 0x3)
35-
| ((lineStyleIndex & 0xFF) << 2)
36-
| ((segmentType & 0x3) << 10)
37-
| ((int(prefixTotalLineLength * 1000.0) & 0x1FFFFF) << 11);
38-
39-
float result;
40-
std::memcpy(&result, &packed, sizeof(float)); // Preserve all 32 bits
41-
return result;
42-
}
43-
4432
void LineGroup2dLayerObject::setLines(const std::vector<std::tuple<std::vector<Coord>, int>> &lines, const Vec3D & origin) {
4533

4634
std::vector<uint32_t> lineIndices;
@@ -95,8 +83,7 @@ void LineGroup2dLayerObject::setLines(const std::vector<std::tuple<std::vector<C
9583
lineAttributes.push_back(pNext.z);
9684
}
9785

98-
float lineStyleMetadata = packLineStyleMetadata(vertexIndex, lineStyleIndex, segmentType, prefixTotalLineLength);
99-
lineAttributes.push_back(lineStyleMetadata);
86+
lineAttributes.push_back(LineHelper::packLineStyleMetadata(vertexIndex, lineStyleIndex, segmentType, prefixTotalLineLength));
10087
}
10188

10289
// Vertex indices

0 commit comments

Comments
 (0)