Skip to content

Add support for second set of UVs; #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions etc/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#define LOCATION_POSITION 10
#define LOCATION_NORMAL 11
#define LOCATION_UV 12
#define LOCATION_COLOR 13
#define LOCATION_TANGENT 14
#define LOCATION_UV2 13
#define LOCATION_COLOR 14
#define LOCATION_TANGENT 15

#define LAST_BUILTIN_BINDING 3
16 changes: 10 additions & 6 deletions etc/shaders/lovr.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ layout(push_constant) uniform PushConstants {
layout(location = 10) in vec4 VertexPosition;
layout(location = 11) in vec3 VertexNormal;
layout(location = 12) in vec2 VertexUV;
layout(location = 13) in vec4 VertexColor;
layout(location = 14) in vec4 VertexTangent;
layout(location = 13) in vec2 VertexUV2;
layout(location = 14) in vec4 VertexColor;
layout(location = 15) in vec4 VertexTangent;
#endif

// Framebuffer
Expand All @@ -88,16 +89,18 @@ layout(location = 0) out vec4 PixelColor;
layout(location = 10) out vec3 PositionWorld;
layout(location = 11) out vec3 Normal;
layout(location = 12) out vec2 UV;
layout(location = 13) out vec4 Color;
layout(location = 14) out vec4 Tangent;
layout(location = 13) out vec2 UV2;
layout(location = 14) out vec4 Color;
layout(location = 15) out vec4 Tangent;
#endif

#ifdef GL_FRAGMENT_SHADER
layout(location = 10) in vec3 PositionWorld;
layout(location = 11) in vec3 Normal;
layout(location = 12) in vec2 UV;
layout(location = 13) in vec4 Color;
layout(location = 14) in vec4 Tangent;
layout(location = 13) in vec2 UV2;
layout(location = 14) in vec4 Color;
layout(location = 15) in vec4 Tangent;
#endif

// Builtins
Expand Down Expand Up @@ -526,6 +529,7 @@ void main() {
PositionWorld = vec3(WorldFromLocal * VertexPosition);
Normal = NormalMatrix * VertexNormal;
UV = VertexUV;
UV2 = VertexUV2;

Color = vec4(1.0);
if (flag_passColor) Color *= PassColor;
Expand Down
1 change: 1 addition & 0 deletions src/api/l_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ StringEntry lovrDefaultAttribute[] = {
[ATTR_POSITION] = ENTRY("position"),
[ATTR_NORMAL] = ENTRY("normal"),
[ATTR_UV] = ENTRY("uv"),
[ATTR_UV2] = ENTRY("uv2"),
[ATTR_COLOR] = ENTRY("color"),
[ATTR_TANGENT] = ENTRY("tangent"),
[ATTR_JOINTS] = ENTRY("joints"),
Expand Down
16 changes: 14 additions & 2 deletions src/modules/data/modelData.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,23 @@ void lovrModelDataCopyAttribute(ModelData* data, ModelAttribute* attribute, char
} else {
lovrUnreachable();
}
} else if (type == U16 && components == 1 && !normalized && !attribute->normalized) {
if (attribute->type == U8) {
} else if (type == U16) {
if (attribute->type == U8 && !attribute->normalized && !normalized && components == 1) {
for (uint32_t i = 0; i < count; i++, src += attribute->stride, dst += stride) {
*((uint16_t*) dst) = *(uint8_t*) src;
}
} else if (attribute->type == F32 && normalized) {
for (uint32_t i = 0; i < count; i++, src += attribute->stride, dst += stride) {
for (uint32_t j = 0; j < components; j++) {
((uint16_t*) dst)[j] = (uint16_t) (((float*) src)[j] * 65535.f + .5f);
}
}
} else if (attribute->type == U8 && attribute->normalized && normalized) {
for (uint32_t i = 0; i < count; i++, src += attribute->stride, dst += stride) {
for (uint32_t j = 0; j < components; j++) {
((uint16_t*) dst)[j] = (uint16_t) ((((uint8_t*) src)[j] / 255.f) * 65535.f + .5f);
}
}
} else {
lovrUnreachable();
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/data/modelData.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef enum {
ATTR_POSITION,
ATTR_NORMAL,
ATTR_UV,
ATTR_UV2,
ATTR_COLOR,
ATTR_TANGENT,
ATTR_JOINTS,
Expand Down
1 change: 1 addition & 0 deletions src/modules/data/modelData_gltf.c
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ bool lovrModelDataInitGltf(ModelData** result, Blob* source, ModelDataIO* io) {
if (STR_EQ(name, "POSITION")) { attributeType = ATTR_POSITION; }
else if (STR_EQ(name, "NORMAL")) { attributeType = ATTR_NORMAL; }
else if (STR_EQ(name, "TEXCOORD_0")) { attributeType = ATTR_UV; }
else if (STR_EQ(name, "TEXCOORD_1")) { attributeType = ATTR_UV2; }
else if (STR_EQ(name, "COLOR_0")) { attributeType = ATTR_COLOR; }
else if (STR_EQ(name, "TANGENT")) { attributeType = ATTR_TANGENT; }
else if (STR_EQ(name, "JOINTS_0")) { attributeType = ATTR_JOINTS; }
Expand Down
48 changes: 29 additions & 19 deletions src/modules/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ typedef struct {
typedef struct {
struct { float x, y, z; } position;
uint32_t normal;
struct { float u, v; } uv;
struct { uint16_t u, v; } uv;
struct { uint16_t u, v; } uv2;
struct { uint8_t r, g, b, a; } color;
uint32_t tangent;
} ModelVertex;
Expand Down Expand Up @@ -804,56 +805,61 @@ bool lovrGraphicsInit(GraphicsConfig* config) {

state.vertexFormats[VERTEX_SHAPE] = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 5,
.attributeCount = 6,
.bufferStrides[0] = sizeof(ShapeVertex),
.attributes[0] = { 0, 10, offsetof(ShapeVertex, position), GPU_TYPE_F32x3 },
.attributes[1] = { 0, 11, offsetof(ShapeVertex, normal), GPU_TYPE_F32x3 },
.attributes[2] = { 0, 12, offsetof(ShapeVertex, uv), GPU_TYPE_F32x2 },
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
};

state.vertexFormats[VERTEX_POINT] = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 5,
.attributeCount = 6,
.bufferStrides[0] = 12,
.attributes[0] = { 0, 10, 0, GPU_TYPE_F32x3 },
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x4 },
.attributes[2] = { 1, 12, 0, GPU_TYPE_F32x4 },
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
};

state.vertexFormats[VERTEX_GLYPH] = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 5,
.attributeCount = 6,
.bufferStrides[0] = sizeof(GlyphVertex),
.attributes[0] = { 0, 10, offsetof(GlyphVertex, position), GPU_TYPE_F32x2 },
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x4 },
.attributes[2] = { 0, 12, offsetof(GlyphVertex, uv), GPU_TYPE_UN16x2 },
.attributes[3] = { 0, 13, offsetof(GlyphVertex, color), GPU_TYPE_UN8x4 },
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
.attributes[4] = { 0, 14, offsetof(GlyphVertex, color), GPU_TYPE_UN8x4 },
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
};

state.vertexFormats[VERTEX_MODEL] = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 5,
.attributeCount = 6,
.bufferStrides[0] = sizeof(ModelVertex),
.attributes[0] = { 0, 10, offsetof(ModelVertex, position), GPU_TYPE_F32x3 },
.attributes[1] = { 0, 11, offsetof(ModelVertex, normal), GPU_TYPE_SN10x3 },
.attributes[2] = { 0, 12, offsetof(ModelVertex, uv), GPU_TYPE_F32x2 },
.attributes[3] = { 0, 13, offsetof(ModelVertex, color), GPU_TYPE_UN8x4 },
.attributes[4] = { 0, 14, offsetof(ModelVertex, tangent), GPU_TYPE_SN10x3 }
.attributes[2] = { 0, 12, offsetof(ModelVertex, uv), GPU_TYPE_UN16x2 },
.attributes[3] = { 0, 13, offsetof(ModelVertex, uv2), GPU_TYPE_UN16x2 },
.attributes[4] = { 0, 14, offsetof(ModelVertex, color), GPU_TYPE_UN8x4 },
.attributes[5] = { 0, 15, offsetof(ModelVertex, tangent), GPU_TYPE_SN10x3 }
};

state.vertexFormats[VERTEX_EMPTY] = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 5,
.attributeCount = 6,
.attributes[0] = { 1, 10, 0, GPU_TYPE_F32x3 },
.attributes[1] = { 1, 11, 0, GPU_TYPE_F32x3 },
.attributes[2] = { 1, 12, 0, GPU_TYPE_F32x2 },
.attributes[3] = { 1, 13, 16, GPU_TYPE_F32x4 },
.attributes[4] = { 1, 14, 0, GPU_TYPE_F32x4 }
.attributes[3] = { 1, 13, 0, GPU_TYPE_F32x2 },
.attributes[4] = { 1, 14, 16, GPU_TYPE_F32x4 },
.attributes[5] = { 1, 15, 0, GPU_TYPE_F32x4 }
};

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

ModelAttribute* uv2 = attributes[ATTR_UV2] ? attributes[ATTR_UV2] : attributes[ATTR_UV];

lovrModelDataCopyAttribute(data, attributes[ATTR_POSITION], vertexData + 0, F32, 3, false, count, stride, 0);
lovrModelDataCopyAttribute(data, attributes[ATTR_NORMAL], vertexData + 12, SN10x3, 1, false, count, stride, 0);
lovrModelDataCopyAttribute(data, attributes[ATTR_UV], vertexData + 16, F32, 2, false, count, stride, 0);
lovrModelDataCopyAttribute(data, attributes[ATTR_UV], vertexData + 16, U16, 2, true, count, stride, 0);
lovrModelDataCopyAttribute(data, uv2, vertexData + 20, U16, 2, true, count, stride, 0);
lovrModelDataCopyAttribute(data, attributes[ATTR_COLOR], vertexData + 24, U8, 4, true, count, stride, 255);
lovrModelDataCopyAttribute(data, attributes[ATTR_TANGENT], vertexData + 28, SN10x3, 1, false, count, stride, 0);
vertexData += count * stride;
Expand Down
Loading