Skip to content

Commit 6f60f27

Browse files
committed
Add UV2 attribute;
1 parent eea0a7a commit 6f60f27

File tree

8 files changed

+58
-28
lines changed

8 files changed

+58
-28
lines changed

etc/shaders.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#define LOCATION_POSITION 10
1818
#define LOCATION_NORMAL 11
1919
#define LOCATION_UV 12
20-
#define LOCATION_COLOR 13
21-
#define LOCATION_TANGENT 14
20+
#define LOCATION_UV2 13
21+
#define LOCATION_COLOR 14
22+
#define LOCATION_TANGENT 15
2223

2324
#define LAST_BUILTIN_BINDING 3

etc/shaders/animator.comp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct ModelVertex {
1616
float x, y, z;
1717
uint normal;
1818
float u, v;
19+
uint uv2;
1920
uint color;
2021
uint tangent;
2122
};

etc/shaders/blender.comp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct ModelVertex {
1717
float px, py, pz;
1818
uint normal;
1919
float u, v;
20+
uint uv2;
2021
uint color;
2122
uint tangent;
2223
};

etc/shaders/lovr.glsl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ layout(push_constant) uniform PushConstants {
7474
layout(location = 10) in vec4 VertexPosition;
7575
layout(location = 11) in vec3 VertexNormal;
7676
layout(location = 12) in vec2 VertexUV;
77-
layout(location = 13) in vec4 VertexColor;
78-
layout(location = 14) in vec4 VertexTangent;
77+
layout(location = 13) in vec2 VertexUV2;
78+
layout(location = 14) in vec4 VertexColor;
79+
layout(location = 15) in vec4 VertexTangent;
7980
#endif
8081

8182
// Framebuffer
@@ -88,16 +89,18 @@ layout(location = 0) out vec4 PixelColor;
8889
layout(location = 10) out vec3 PositionWorld;
8990
layout(location = 11) out vec3 Normal;
9091
layout(location = 12) out vec2 UV;
91-
layout(location = 13) out vec4 Color;
92-
layout(location = 14) out vec4 Tangent;
92+
layout(location = 13) out vec2 UV2;
93+
layout(location = 14) out vec4 Color;
94+
layout(location = 15) out vec4 Tangent;
9395
#endif
9496

9597
#ifdef GL_FRAGMENT_SHADER
9698
layout(location = 10) in vec3 PositionWorld;
9799
layout(location = 11) in vec3 Normal;
98100
layout(location = 12) in vec2 UV;
99-
layout(location = 13) in vec4 Color;
100-
layout(location = 14) in vec4 Tangent;
101+
layout(location = 13) in vec2 UV2;
102+
layout(location = 14) in vec4 Color;
103+
layout(location = 15) in vec4 Tangent;
101104
#endif
102105

103106
// Builtins
@@ -526,6 +529,7 @@ void main() {
526529
PositionWorld = vec3(WorldFromLocal * VertexPosition);
527530
Normal = NormalMatrix * VertexNormal;
528531
UV = VertexUV;
532+
UV2 = VertexUV2;
529533

530534
Color = vec4(1.0);
531535
if (flag_passColor) Color *= PassColor;

src/api/l_data_modelData.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,16 @@ static int l_lovrModelDataGetMeshVertex(lua_State* L) {
387387
lua_pushnumber(L, MAX(((int32_t) (vertex->normal << 2) >> 22) / 511.f, -1.f));
388388
lua_pushnumber(L, vertex->uv.u);
389389
lua_pushnumber(L, vertex->uv.v);
390+
lua_pushnumber(L, vertex->uv2.u / 65535.f);
391+
lua_pushnumber(L, vertex->uv2.v / 65535.f);
390392
lua_pushinteger(L, vertex->color.r);
391393
lua_pushinteger(L, vertex->color.g);
392394
lua_pushinteger(L, vertex->color.b);
393395
lua_pushinteger(L, vertex->color.a);
394396
lua_pushnumber(L, MAX(((int32_t) (vertex->tangent << 22) >> 22) / 511.f, -1.f));
395397
lua_pushnumber(L, MAX(((int32_t) (vertex->tangent << 12) >> 22) / 511.f, -1.f));
396398
lua_pushnumber(L, MAX(((int32_t) (vertex->tangent << 2) >> 22) / 511.f, -1.f));
397-
return 15;
399+
return 17;
398400
}
399401

400402
static int l_lovrModelDataGetMeshIndex(lua_State* L) {

src/modules/data/modelData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef struct {
1212
struct { float x, y, z; } position;
1313
uint32_t normal;
1414
struct { float u, v; } uv;
15+
struct { uint16_t u, v; } uv2;
1516
struct { uint8_t r, g, b, a; } color;
1617
uint32_t tangent;
1718
} ModelVertex;

src/modules/data/modelData_gltf.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,23 @@ void copyAttribute(void* destination, gltfAccessor* accessor, ComponentType type
365365
} else {
366366
lovrUnreachable();
367367
}
368-
} else if (type == U16 && components == 1 && !normalized && !accessor->normalized) {
369-
if (accessor->type == U8) {
368+
} else if (type == U16) {
369+
if (accessor->type == U8 && !accessor->normalized && !normalized) {
370370
for (uint32_t i = 0; i < count; i++, src += srcStride, dst += stride) {
371371
*((uint16_t*) dst) = *(uint8_t*) src;
372372
}
373+
} else if (accessor->type == U8 && accessor->normalized && normalized) {
374+
for (uint32_t i = 0; i < count; i++, src += srcStride, dst += stride) {
375+
for (uint32_t j = 0; j < components; j++) {
376+
((uint16_t*) dst)[j] = ((uint16_t) ((uint8_t*) src)[j]) * 257; // Yes, 257
377+
}
378+
}
379+
} else if (accessor->type == F32 && normalized) {
380+
for (uint32_t i = 0; i < count; i++, src += srcStride, dst += stride) {
381+
for (uint32_t j = 0; j < components; j++) {
382+
((uint16_t*) dst)[j] = (uint16_t) (((float*) src)[j] * 65535.f + .5f);
383+
}
384+
}
373385
} else {
374386
lovrUnreachable();
375387
}
@@ -1061,7 +1073,8 @@ bool lovrModelDataInitGltf(ModelData** result, Blob* source, ModelDataIO* io) {
10611073
for (uint32_t j = (token++)->size; j > 0; j--, part++) {
10621074
gltfAccessor* positions = NULL;
10631075
gltfAccessor* normals = NULL;
1064-
gltfAccessor* uvs = NULL;
1076+
gltfAccessor* uv1 = NULL;
1077+
gltfAccessor* uv2 = NULL;
10651078
gltfAccessor* colors = NULL;
10661079
gltfAccessor* tangents = NULL;
10671080
gltfAccessor* indices = NULL;
@@ -1087,7 +1100,8 @@ bool lovrModelDataInitGltf(ModelData** result, Blob* source, ModelDataIO* io) {
10871100
uint32_t accessor = NOM_U32(json, token);
10881101
if (STR_EQ(name, "POSITION")) positions = &accessors[accessor];
10891102
else if (STR_EQ(name, "NORMAL")) normals = &accessors[accessor];
1090-
else if (STR_EQ(name, "TEXCOORD_0")) uvs = &accessors[accessor];
1103+
else if (STR_EQ(name, "TEXCOORD_0")) uv1 = &accessors[accessor];
1104+
else if (STR_EQ(name, "TEXCOORD_1")) uv2 = &accessors[accessor];
10911105
else if (STR_EQ(name, "COLOR_0")) colors = &accessors[accessor];
10921106
else if (STR_EQ(name, "TANGENT")) tangents = &accessors[accessor];
10931107
else if (STR_EQ(name, "JOINTS_0")) joints = &accessors[accessor];
@@ -1143,9 +1157,10 @@ bool lovrModelDataInitGltf(ModelData** result, Blob* source, ModelDataIO* io) {
11431157
ModelVertex* vertices = model->vertices + vertexOffset;
11441158
copyAttribute(vertices, positions, F32, 3, false, 0, sizeof(ModelVertex), vertexCount, 0);
11451159
copyAttribute(vertices, normals, SN10x3, 1, false, 12, sizeof(ModelVertex), vertexCount, 0);
1146-
copyAttribute(vertices, uvs, F32, 2, false, 16, sizeof(ModelVertex), vertexCount, 0);
1147-
copyAttribute(vertices, colors, U8, 4, true, 24, sizeof(ModelVertex), vertexCount, 0xff);
1148-
copyAttribute(vertices, tangents, SN10x3, 1, false, 28, sizeof(ModelVertex), vertexCount, 0);
1160+
copyAttribute(vertices, uv1, F32, 2, false, 16, sizeof(ModelVertex), vertexCount, 0);
1161+
copyAttribute(vertices, uv2, U16, 2, true, 24, sizeof(ModelVertex), vertexCount, 0);
1162+
copyAttribute(vertices, colors, U8, 4, true, 28, sizeof(ModelVertex), vertexCount, 0xff);
1163+
copyAttribute(vertices, tangents, SN10x3, 1, false, 32, sizeof(ModelVertex), vertexCount, 0);
11491164
mesh->vertexCount += vertexCount;
11501165
vertexOffset += vertexCount;
11511166

src/modules/graphics/graphics.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -815,45 +815,49 @@ bool lovrGraphicsInit(GraphicsConfig* config) {
815815

816816
state.vertexFormats[VERTEX_SHAPE] = (gpu_vertex_format) {
817817
.bufferCount = 2,
818-
.attributeCount = 5,
818+
.attributeCount = 6,
819819
.bufferStrides[0] = sizeof(ShapeVertex),
820820
.attributes[0] = { 0, 10, offsetof(ShapeVertex, position), GPU_TYPE_F32x3 },
821821
.attributes[1] = { 0, 11, offsetof(ShapeVertex, normal), GPU_TYPE_F32x3 },
822822
.attributes[2] = { 0, 12, offsetof(ShapeVertex, uv), GPU_TYPE_F32x2 },
823-
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
824-
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
823+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
824+
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
825+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
825826
};
826827

827828
state.vertexFormats[VERTEX_POINT] = (gpu_vertex_format) {
828829
.bufferCount = 2,
829-
.attributeCount = 5,
830+
.attributeCount = 6,
830831
.bufferStrides[0] = 12,
831832
.attributes[0] = { 0, 10, 0, GPU_TYPE_F32x3 },
832833
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x4 },
833834
.attributes[2] = { 1, 12, 0, GPU_TYPE_F32x4 },
834-
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
835-
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
835+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
836+
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
837+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
836838
};
837839

838840
state.vertexFormats[VERTEX_GLYPH] = (gpu_vertex_format) {
839841
.bufferCount = 2,
840-
.attributeCount = 5,
842+
.attributeCount = 6,
841843
.bufferStrides[0] = sizeof(GlyphVertex),
842844
.attributes[0] = { 0, 10, offsetof(GlyphVertex, position), GPU_TYPE_F32x2 },
843845
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x4 },
844846
.attributes[2] = { 0, 12, offsetof(GlyphVertex, uv), GPU_TYPE_UN16x2 },
845-
.attributes[3] = { 0, 13, offsetof(GlyphVertex, color), GPU_TYPE_UN8x4 },
846-
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
847+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
848+
.attributes[4] = { 0, 14, offsetof(GlyphVertex, color), GPU_TYPE_UN8x4 },
849+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
847850
};
848851

849852
state.vertexFormats[VERTEX_EMPTY] = (gpu_vertex_format) {
850853
.bufferCount = 2,
851-
.attributeCount = 5,
854+
.attributeCount = 6,
852855
.attributes[0] = { 1, 10, 0, GPU_TYPE_F32x3 },
853856
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x3 },
854857
.attributes[2] = { 1, 12, 0, GPU_TYPE_F32x2 },
855-
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
856-
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
858+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
859+
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
860+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
857861
};
858862

859863
float16Init();
@@ -5199,6 +5203,7 @@ Model* lovrModelCreate(const ModelInfo* info) {
51995203
{ .name = "VertexPosition", .type = TYPE_F32x3, .offset = offsetof(ModelVertex, position) },
52005204
{ .name = "VertexNormal", .type = TYPE_SN10x3, .offset = offsetof(ModelVertex, normal) },
52015205
{ .name = "VertexUV", .type = TYPE_F32x2, .offset = offsetof(ModelVertex, uv) },
5206+
{ .name = "VertexUV2", .type = TYPE_UN16x2, .offset = offsetof(ModelVertex, uv2) },
52025207
{ .name = "VertexColor", .type = TYPE_UN8x4, .offset = offsetof(ModelVertex, color) },
52035208
{ .name = "VertexTangent", .type = TYPE_SN10x3, .offset = offsetof(ModelVertex, tangent) }
52045209
}

0 commit comments

Comments
 (0)