Skip to content

Commit 4fed42d

Browse files
committed
Add support for second set of UVs;
1 parent c9ffca9 commit 4fed42d

File tree

6 files changed

+52
-24
lines changed

6 files changed

+52
-24
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/lovr.glsl

Lines changed: 9 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

src/modules/data/modelData.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,23 @@ void lovrModelDataCopyAttribute(ModelData* data, ModelAttribute* attribute, char
256256
} else {
257257
lovrUnreachable();
258258
}
259-
} else if (type == U16 && components == 1 && !normalized && !attribute->normalized) {
260-
if (attribute->type == U8) {
259+
} else if (type == U16) {
260+
if (attribute->type == U8 && !attribute->normalized && !normalized && components == 1) {
261261
for (uint32_t i = 0; i < count; i++, src += attribute->stride, dst += stride) {
262262
*((uint16_t*) dst) = *(uint8_t*) src;
263263
}
264+
} else if (attribute->type == F32 && normalized) {
265+
for (uint32_t i = 0; i < count; i++, src += attribute->stride, dst += stride) {
266+
for (uint32_t j = 0; j < components; j++) {
267+
((uint16_t*) dst)[j] = (uint16_t) (((float*) src)[j] * 65535.f + .5f);
268+
}
269+
}
270+
} else if (attribute->type == U8 && attribute->normalized && normalized) {
271+
for (uint32_t i = 0; i < count; i++, src += attribute->stride, dst += stride) {
272+
for (uint32_t j = 0; j < components; j++) {
273+
((uint16_t*) dst)[j] = (uint16_t) ((((uint8_t*) src)[j] / 255.f) * 65535.f + .5f);
274+
}
275+
}
264276
} else {
265277
lovrUnreachable();
266278
}

src/modules/data/modelData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ typedef enum {
2525
ATTR_POSITION,
2626
ATTR_NORMAL,
2727
ATTR_UV,
28+
ATTR_UV2,
2829
ATTR_COLOR,
2930
ATTR_TANGENT,
3031
ATTR_JOINTS,

src/modules/data/modelData_gltf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ bool lovrModelDataInitGltf(ModelData** result, Blob* source, ModelDataIO* io) {
858858
if (STR_EQ(name, "POSITION")) { attributeType = ATTR_POSITION; }
859859
else if (STR_EQ(name, "NORMAL")) { attributeType = ATTR_NORMAL; }
860860
else if (STR_EQ(name, "TEXCOORD_0")) { attributeType = ATTR_UV; }
861+
else if (STR_EQ(name, "TEXCOORD_1")) { attributeType = ATTR_UV2; }
861862
else if (STR_EQ(name, "COLOR_0")) { attributeType = ATTR_COLOR; }
862863
else if (STR_EQ(name, "TANGENT")) { attributeType = ATTR_TANGENT; }
863864
else if (STR_EQ(name, "JOINTS_0")) { attributeType = ATTR_JOINTS; }

src/modules/graphics/graphics.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ typedef struct {
361361
typedef struct {
362362
struct { float x, y, z; } position;
363363
uint32_t normal;
364-
struct { float u, v; } uv;
364+
struct { uint16_t u, v; } uv;
365+
struct { uint16_t u, v; } uv2;
365366
struct { uint8_t r, g, b, a; } color;
366367
uint32_t tangent;
367368
} ModelVertex;
@@ -809,8 +810,9 @@ bool lovrGraphicsInit(GraphicsConfig* config) {
809810
.attributes[0] = { 0, 10, offsetof(ShapeVertex, position), GPU_TYPE_F32x3 },
810811
.attributes[1] = { 0, 11, offsetof(ShapeVertex, normal), GPU_TYPE_F32x3 },
811812
.attributes[2] = { 0, 12, offsetof(ShapeVertex, uv), GPU_TYPE_F32x2 },
812-
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
813-
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
813+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
814+
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
815+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
814816
};
815817

816818
state.vertexFormats[VERTEX_POINT] = (gpu_vertex_format) {
@@ -820,8 +822,9 @@ bool lovrGraphicsInit(GraphicsConfig* config) {
820822
.attributes[0] = { 0, 10, 0, GPU_TYPE_F32x3 },
821823
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x4 },
822824
.attributes[2] = { 1, 12, 0, GPU_TYPE_F32x4 },
823-
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
824-
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
825+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
826+
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
827+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
825828
};
826829

827830
state.vertexFormats[VERTEX_GLYPH] = (gpu_vertex_format) {
@@ -831,8 +834,9 @@ bool lovrGraphicsInit(GraphicsConfig* config) {
831834
.attributes[0] = { 0, 10, offsetof(GlyphVertex, position), GPU_TYPE_F32x2 },
832835
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x4 },
833836
.attributes[2] = { 0, 12, offsetof(GlyphVertex, uv), GPU_TYPE_UN16x2 },
834-
.attributes[3] = { 0, 13, offsetof(GlyphVertex, color), GPU_TYPE_UN8x4 },
835-
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
837+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
838+
.attributes[4] = { 0, 14, offsetof(GlyphVertex, color), GPU_TYPE_UN8x4 },
839+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
836840
};
837841

838842
state.vertexFormats[VERTEX_MODEL] = (gpu_vertex_format) {
@@ -841,9 +845,10 @@ bool lovrGraphicsInit(GraphicsConfig* config) {
841845
.bufferStrides[0] = sizeof(ModelVertex),
842846
.attributes[0] = { 0, 10, offsetof(ModelVertex, position), GPU_TYPE_F32x3 },
843847
.attributes[1] = { 0, 11, offsetof(ModelVertex, normal), GPU_TYPE_SN10x3 },
844-
.attributes[2] = { 0, 12, offsetof(ModelVertex, uv), GPU_TYPE_F32x2 },
845-
.attributes[3] = { 0, 13, offsetof(ModelVertex, color), GPU_TYPE_UN8x4 },
846-
.attributes[4] = { 0, 14, offsetof(ModelVertex, tangent), GPU_TYPE_SN10x3 }
848+
.attributes[2] = { 0, 12, offsetof(ModelVertex, uv), GPU_TYPE_UN16x2 },
849+
.attributes[3] = { 0, 13, offsetof(ModelVertex, uv2), GPU_TYPE_UN16x2 },
850+
.attributes[4] = { 0, 14, offsetof(ModelVertex, color), GPU_TYPE_UN8x4 },
851+
.attributes[5] = { 0, 15, offsetof(ModelVertex, tangent), GPU_TYPE_SN10x3 }
847852
};
848853

849854
state.vertexFormats[VERTEX_EMPTY] = (gpu_vertex_format) {
@@ -852,8 +857,9 @@ bool lovrGraphicsInit(GraphicsConfig* config) {
852857
.attributes[0] = { 1, 10, 0, GPU_TYPE_F32x3 },
853858
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x3 },
854859
.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 }
860+
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
861+
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
862+
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
857863
};
858864

859865
float16Init();
@@ -4958,7 +4964,8 @@ Model* lovrModelCreate(const ModelInfo* info) {
49584964
{ .length = data->vertexCount, .stride = sizeof(ModelVertex), .fieldCount = 5 },
49594965
{ .name = "VertexPosition", .type = TYPE_F32x3, .offset = offsetof(ModelVertex, position) },
49604966
{ .name = "VertexNormal", .type = TYPE_SN10x3, .offset = offsetof(ModelVertex, normal) },
4961-
{ .name = "VertexUV", .type = TYPE_F32x2, .offset = offsetof(ModelVertex, uv) },
4967+
{ .name = "VertexUV", .type = TYPE_UN16x2, .offset = offsetof(ModelVertex, uv) },
4968+
{ .name = "VertexUV2", .type = TYPE_UN16x2, .offset = offsetof(ModelVertex, uv2) },
49624969
{ .name = "VertexColor", .type = TYPE_UN8x4, .offset = offsetof(ModelVertex, color) },
49634970
{ .name = "VertexTangent", .type = TYPE_SN10x3, .offset = offsetof(ModelVertex, tangent) }
49644971
}
@@ -5105,9 +5112,12 @@ Model* lovrModelCreate(const ModelInfo* info) {
51055112
uint32_t count = attributes[ATTR_POSITION]->count;
51065113
size_t stride = sizeof(ModelVertex);
51075114

5115+
ModelAttribute* uv2 = attributes[ATTR_UV2] ? attributes[ATTR_UV2] : attributes[ATTR_UV];
5116+
51085117
lovrModelDataCopyAttribute(data, attributes[ATTR_POSITION], vertexData + 0, F32, 3, false, count, stride, 0);
51095118
lovrModelDataCopyAttribute(data, attributes[ATTR_NORMAL], vertexData + 12, SN10x3, 1, false, count, stride, 0);
5110-
lovrModelDataCopyAttribute(data, attributes[ATTR_UV], vertexData + 16, F32, 2, false, count, stride, 0);
5119+
lovrModelDataCopyAttribute(data, attributes[ATTR_UV], vertexData + 16, U16, 2, true, count, stride, 0);
5120+
lovrModelDataCopyAttribute(data, uv2, vertexData + 20, U16, 2, true, count, stride, 0);
51115121
lovrModelDataCopyAttribute(data, attributes[ATTR_COLOR], vertexData + 24, U8, 4, true, count, stride, 255);
51125122
lovrModelDataCopyAttribute(data, attributes[ATTR_TANGENT], vertexData + 28, SN10x3, 1, false, count, stride, 0);
51135123
vertexData += count * stride;

0 commit comments

Comments
 (0)