forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_vert.cpp
125 lines (104 loc) · 3.54 KB
/
text_vert.cpp
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
#include <vsg/io/VSG.h>
static auto text_vert = []() {std::istringstream str(
R"(#vsga 0.4.3
Root id=1 vsg::ShaderStage
{
userObjects 0
stage 1
entryPointName "main"
module id=2 vsg::ShaderModule
{
userObjects 0
hints id=0
source "#version 450
#extension GL_ARB_separate_shader_objects : enable
#pragma import_defines (GPU_LAYOUT, CPU_LAYOUT)
#ifdef GPU_LAYOUT
// GPU layout provides computes vertex values based on layout computed in the vertex shader
#define GLYPH_DIMENSIONS 0
#define GLYPH_BEARINGS 1
#define GLYPH_UVRECT 2
// specialization constants
layout(constant_id = 0) const uint numTextIndices = 256;
layout(set = 0, binding = 1) uniform sampler2D glyphMetricsSampler;
layout(set = 1, binding = 0) uniform TextLayout {
vec4 position;
vec4 horizontal;
vec4 vertical;
vec4 color;
vec4 outlineColor;
float outlineWidth;
} textLayout;
layout(set = 1, binding = 1) uniform TextIndices {
uvec4 glyph_index[numTextIndices];
} text;
#else
// CPU layout provides all vertex data
layout(location = 1) in vec4 inColor;
layout(location = 2) in vec4 inOutlineColor;
layout(location = 3) in float inOutlineWidth;
layout(location = 4) in vec3 inTexCoord;
#endif
layout(push_constant) uniform PushConstants {
mat4 projection;
mat4 modelview;
} pc;
layout(location = 0) in vec3 inPosition;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 outlineColor;
layout(location = 2) out float outlineWidth;
layout(location = 3) out vec2 fragTexCoord;
out gl_PerVertex {
vec4 gl_Position;
};
void main() {
#ifdef GPU_LAYOUT
// compute the position of the glyph
float horiAdvance = 0.0;
float vertAdvance = 0.0;
for(uint i=0; i<gl_InstanceIndex; ++i)
{
uint glyph_index = text.glyph_index[i / 4][i % 4];
if (glyph_index==0)
{
// treat as a newlline
vertAdvance -= 1.0;
horiAdvance = 0.0;
}
else
{
horiAdvance += texture(glyphMetricsSampler, vec2(GLYPH_DIMENSIONS, glyph_index))[2];
}
}
vec3 cursor = textLayout.position.xyz + textLayout.horizontal.xyz * horiAdvance + textLayout.vertical.xyz * vertAdvance;
// compute the position of vertex
uint glyph_index = text.glyph_index[gl_InstanceIndex / 4][gl_InstanceIndex % 4];
vec4 dimensions = texture(glyphMetricsSampler, vec2(GLYPH_DIMENSIONS, glyph_index));
vec4 bearings = texture(glyphMetricsSampler, vec2(GLYPH_BEARINGS, glyph_index));
vec4 uv_rec = texture(glyphMetricsSampler, vec2(GLYPH_UVRECT, glyph_index));
vec3 pos = cursor + textLayout.horizontal.xyz * (bearings.x + inPosition.x * dimensions.x) + textLayout.vertical.xyz * (bearings.y + (inPosition.y-1.0) * dimensions.y);
gl_Position = (pc.projection * pc.modelview) * vec4(pos, 1.0);
gl_Position.z -= inPosition.z*0.001;
fragColor = textLayout.color;
outlineColor = textLayout.outlineColor;
outlineWidth = textLayout.outlineWidth;
fragTexCoord = vec2(mix(uv_rec[0], uv_rec[2], inPosition.x), mix(uv_rec[1], uv_rec[3], inPosition.y));
#else
// CPU layout provides all vertex data
gl_Position = (pc.projection * pc.modelview) * vec4(inPosition, 1.0);
gl_Position.z -= inTexCoord.z*0.001;
fragColor = inColor;
outlineColor = inOutlineColor;
outlineWidth = inOutlineWidth;
fragTexCoord = inTexCoord.xy;
#endif
}
"
code 0
}
NumSpecializationConstants 0
}
)");
vsg::VSG io;
return io.read_cast<vsg::ShaderStage>(str);
};