Skip to content

Commit 98885f4

Browse files
committed
2D oBezierCoord -> 1D glyphDataOffset
1 parent f829dab commit 98885f4

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

include/gllabel.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class GLFontManager
3838
{
3939
uint16_t size[2]; // Width and height in FT units
4040
int16_t offset[2]; // Offset of glyph in FT units
41-
uint16_t bezierAtlasPos[3]; // XYZ pixel coordinates (Z being atlas index)
41+
uint16_t bezierAtlasPos[2]; // XZ pixel coordinates (Z being atlas index)
4242
int16_t advance; // Amount to advance after character in FT units
4343
};
4444

@@ -94,11 +94,11 @@ class GLLabel
9494
// XY coords of the vertex
9595
glm::vec2 pos;
9696

97-
// The UV coords of the data for this glyph in the bezier atlas
98-
// Also contains a vertex-dependent norm coordiate by encoding:
99-
// encode: data[n] = coord[n] * 2 + norm[n]
100-
// decode: norm[n] = data[n] % 2, coord[n] = int(data[n] / 2)
101-
uint16_t data[2];
97+
// Bit 0 (low) is norm coord X (varies per vertex)
98+
// Bit 1 is norm coord Y (varies per vertex)
99+
// Bits 2-31 are texel offset (byte offset / 4) into
100+
// glyphDataBuf (same for all verticies of a glyph)
101+
uint32_t data;
102102

103103
// RGBA color [0,255]
104104
Color color;

lib/gllabel.cpp

+23-21
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ void GLLabel::InsertText(std::u32string text, size_t index, glm::vec4 color, FT_
124124
// This theoretically could overflow, but the atlas position will
125125
// never be over half the size of a uint16, so it's fine.
126126
unsigned int k = (j < 4) ? j : 6 - j;
127-
unsigned int norm[2] = { k & 1, k > 1 };
128-
v[j].data[0] = (glyph->bezierAtlasPos[0]<<1) + norm[0];
129-
v[j].data[1] = (glyph->bezierAtlasPos[1]<<1) + norm[1];
127+
unsigned int normX = k & 1;
128+
unsigned int normY = k > 1;
129+
unsigned int norm = (normX << 1) + normY;
130+
v[j].data = (glyph->bezierAtlasPos[0] << 2) + norm;
130131
this->verts[(index + i)*6 + j] = v[j];
131132
}
132133

@@ -240,7 +241,7 @@ void GLLabel::Render(float time, glm::mat4 transform)
240241
glEnableVertexAttribArray(1);
241242
glEnableVertexAttribArray(2);
242243
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, pos));
243-
glVertexAttribPointer(1, 2, GL_UNSIGNED_SHORT, GL_FALSE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, data));
244+
glVertexAttribPointer(1, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, data));
244245
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, color));
245246

246247
glDrawArrays(GL_TRIANGLES, 0, this->verts.size());
@@ -276,14 +277,16 @@ void GLLabel::Render(float time, glm::mat4 transform)
276277
// This theoretically could overflow, but the atlas position will
277278
// never be over half the size of a uint16, so it's fine.
278279
unsigned int k = (j < 4) ? j : 6 - j;
279-
x[j].data[0] = pipe->bezierAtlasPos[0]*2 + ((k & 1) ? 1 : 0);
280-
x[j].data[1] = pipe->bezierAtlasPos[1]*2 + ((k > 1) ? 1 : 0);
280+
unsigned int normX = k & 1;
281+
unsigned int normY = k > 1;
282+
unsigned int norm = (normX << 1) + normY;
283+
x[j].data = (pipe->bezierAtlasPos[0] << 2) + norm;
281284
// this->verts[(index + i)*6 + j] = v[j];
282285
}
283286

284287
glBindBuffer(GL_ARRAY_BUFFER, this->caretBuffer);
285288
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, pos));
286-
glVertexAttribPointer(1, 2, GL_UNSIGNED_SHORT, GL_FALSE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, data));
289+
glVertexAttribPointer(1, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, data));
287290
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(GLLabel::GlyphVertex), (void*)offsetof(GLLabel::GlyphVertex, color));
288291

289292
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GlyphVertex), &x[0], GL_STREAM_DRAW);
@@ -582,7 +585,7 @@ GLFontManager::Glyph * GLFontManager::GetGlyphForCodepoint(FT_Face face, uint32_
582585
}
583586

584587
GLFontManager::Glyph glyph{};
585-
glyph.bezierAtlasPos[2] = -1;
588+
glyph.bezierAtlasPos[1] = -1;
586589
glyph.size[0] = glyphWidth;
587590
glyph.size[1] = glyphHeight;
588591
glyph.offset[0] = face->glyph->metrics.horiBearingX;
@@ -632,8 +635,7 @@ GLFontManager::Glyph * GLFontManager::GetGlyphForCodepoint(FT_Face face, uint32_
632635

633636
GLFontManager::Glyph glyph{};
634637
glyph.bezierAtlasPos[0] = atlas->glyphDataBufOffset;
635-
glyph.bezierAtlasPos[1] = 0;
636-
glyph.bezierAtlasPos[2] = this->atlases.size()-1;
638+
glyph.bezierAtlasPos[1] = this->atlases.size()-1;
637639
glyph.size[0] = glyphWidth;
638640
glyph.size[1] = glyphHeight;
639641
glyph.offset[0] = face->glyph->metrics.horiBearingX;
@@ -775,11 +777,11 @@ uniform samplerBuffer uGlyphData;
775777
uniform mat4 uTransform;
776778
777779
layout(location = 0) in vec2 vPosition;
778-
layout(location = 1) in vec2 vData;
780+
layout(location = 1) in uint vData;
779781
layout(location = 2) in vec4 vColor;
780782
781783
out vec4 oColor;
782-
flat out ivec2 oBezierCoord;
784+
flat out uint glyphDataOffset;
783785
flat out ivec4 oGridRect;
784786
out vec2 oNormCoord;
785787
@@ -788,18 +790,18 @@ float ushortFromVec2(vec2 v)
788790
return (v.y * 65280.0 + v.x * 255.0);
789791
}
790792
791-
ivec2 vec2FromPixel(ivec2 coord)
793+
ivec2 vec2FromPixel(uint offset)
792794
{
793-
vec4 pixel = texelFetch(uGlyphData, coord.x);
795+
vec4 pixel = texelFetch(uGlyphData, int(offset));
794796
return ivec2(ushortFromVec2(pixel.xy), ushortFromVec2(pixel.zw));
795797
}
796798
797799
void main()
798800
{
799801
oColor = vColor;
800-
oBezierCoord = ivec2(vData) / 2;
801-
oNormCoord = mod(vData, 2.0);
802-
oGridRect = ivec4(vec2FromPixel(oBezierCoord), vec2FromPixel(oBezierCoord + ivec2(1,0)));
802+
glyphDataOffset = vData >> 2u;
803+
oNormCoord = vec2((vData & 2u) >> 1, vData & 1u);
804+
oGridRect = ivec4(vec2FromPixel(glyphDataOffset), vec2FromPixel(glyphDataOffset + 1u));
803805
gl_Position = uTransform*vec4(vPosition, 0.0, 1.0);
804806
}
805807
)";
@@ -819,7 +821,7 @@ uniform sampler2D uGridAtlas;
819821
uniform samplerBuffer uGlyphData;
820822
821823
in vec4 oColor;
822-
flat in ivec2 oBezierCoord;
824+
flat in uint glyphDataOffset;
823825
flat in ivec4 oGridRect;
824826
in vec2 oNormCoord;
825827
@@ -846,15 +848,15 @@ float normalizedUshortFromVec2(vec2 v)
846848
return (v.y * 65280.0 + v.x * 255.0) / 65536.0;
847849
}
848850
849-
vec4 getPixelByXY(ivec2 coord)
851+
vec4 getPixelByOffset(int offset)
850852
{
851-
return texelFetch(uGlyphData, coord.x);
853+
return texelFetch(uGlyphData, offset);
852854
}
853855
854856
void fetchBezier(int coordIndex, out vec2 p[3])
855857
{
856858
for (int i=0; i<3; i++) {
857-
vec4 pixel = getPixelByXY(ivec2(oBezierCoord.x + 2 + coordIndex*3 + i, oBezierCoord.y));
859+
vec4 pixel = getPixelByOffset(int(glyphDataOffset) + 2 + coordIndex*3 + i);
858860
p[i] = vec2(normalizedUshortFromVec2(pixel.xy), normalizedUshortFromVec2(pixel.zw)) - oNormCoord;
859861
}
860862
}

0 commit comments

Comments
 (0)