Merge pull request #51376 from The-O-King/vertex_buffer_alignment

Align Vertex Buffer to 4 Bytes
This commit is contained in:
Rémi Verschelde 2021-08-12 07:13:45 +02:00 committed by GitHub
commit 9e3e7b03e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 56 deletions

View File

@ -2099,13 +2099,8 @@ static PoolVector<uint8_t> _unpack_half_floats(const PoolVector<uint8_t> &array,
} break; } break;
case VS::ARRAY_NORMAL: { case VS::ARRAY_NORMAL: {
if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
if (p_format & VS::ARRAY_COMPRESS_NORMAL) { src_size[i] = 4;
src_size[i] = 2; dst_size[i] = 4;
dst_size[i] = 2;
} else {
src_size[i] = 4;
dst_size[i] = 4;
}
} else { } else {
if (p_format & VS::ARRAY_COMPRESS_NORMAL) { if (p_format & VS::ARRAY_COMPRESS_NORMAL) {
src_size[i] = 4; src_size[i] = 4;
@ -2119,13 +2114,12 @@ static PoolVector<uint8_t> _unpack_half_floats(const PoolVector<uint8_t> &array,
} break; } break;
case VS::ARRAY_TANGENT: { case VS::ARRAY_TANGENT: {
if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
if (p_format & VS::ARRAY_COMPRESS_TANGENT) { if (!(p_format & VS::ARRAY_COMPRESS_TANGENT)) {
src_size[i] = 2; src_size[VS::ARRAY_NORMAL] = 8;
dst_size[i] = 2; dst_size[VS::ARRAY_NORMAL] = 8;
} else {
src_size[i] = 4;
dst_size[i] = 4;
} }
src_size[i] = 0;
dst_size[i] = 0;
} else { } else {
if (p_format & VS::ARRAY_COMPRESS_TANGENT) { if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
src_size[i] = 4; src_size[i] = 4;
@ -2309,15 +2303,15 @@ void RasterizerStorageGLES2::mesh_add_surface(RID p_mesh, uint32_t p_format, VS:
} break; } break;
case VS::ARRAY_NORMAL: { case VS::ARRAY_NORMAL: {
if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
// Always pack normal and tangent into vec4
// normal will be xy tangent will be zw
// normal will always be oct32 encoded
// UNLESS tangent exists and is also compressed
// then it will be oct16 encoded along with tangent
attribs[i].normalized = GL_TRUE; attribs[i].normalized = GL_TRUE;
attribs[i].size = 2; attribs[i].size = 4;
if (p_format & VS::ARRAY_COMPRESS_NORMAL) { attribs[i].type = GL_SHORT;
attribs[i].type = GL_BYTE; attributes_stride += 4;
attributes_stride += 2;
} else {
attribs[i].type = GL_SHORT;
attributes_stride += 4;
}
} else { } else {
attribs[i].size = 3; attribs[i].size = 3;
@ -2335,13 +2329,14 @@ void RasterizerStorageGLES2::mesh_add_surface(RID p_mesh, uint32_t p_format, VS:
} break; } break;
case VS::ARRAY_TANGENT: { case VS::ARRAY_TANGENT: {
if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
attribs[i].normalized = GL_TRUE; attribs[i].enabled = false;
attribs[i].size = 2;
if (p_format & VS::ARRAY_COMPRESS_TANGENT) { if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
attribs[i].type = GL_BYTE; // normal and tangent will each be oct16 (2 bytes each)
attributes_stride += 2; // pack into single vec4<GL_BYTE> for memory bandwidth
// savings while keeping 4 byte alignment
attribs[VS::ARRAY_NORMAL].type = GL_BYTE;
} else { } else {
attribs[i].type = GL_SHORT; // normal and tangent will each be oct32 (4 bytes each)
attributes_stride += 4; attributes_stride += 4;
} }
} else { } else {

View File

@ -32,14 +32,14 @@ precision highp int;
attribute highp vec4 vertex_attrib; // attrib:0 attribute highp vec4 vertex_attrib; // attrib:0
/* clang-format on */ /* clang-format on */
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
attribute vec2 normal_attrib; // attrib:1 attribute vec4 normal_tangent_attrib; // attrib:1
#else #else
attribute vec3 normal_attrib; // attrib:1 attribute vec3 normal_attrib; // attrib:1
#endif #endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
attribute vec2 tangent_attrib; // attrib:2 // packed into normal_attrib zw component
#else #else
attribute vec4 tangent_attrib; // attrib:2 attribute vec4 tangent_attrib; // attrib:2
#endif #endif
@ -359,15 +359,15 @@ void main() {
#endif #endif
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
vec3 normal = oct_to_vec3(normal_attrib); vec3 normal = oct_to_vec3(normal_tangent_attrib.xy);
#else #else
vec3 normal = normal_attrib; vec3 normal = normal_attrib;
#endif #endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP)
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
vec3 tangent = oct_to_vec3(vec2(tangent_attrib.x, abs(tangent_attrib.y) * 2.0 - 1.0)); vec3 tangent = oct_to_vec3(vec2(normal_tangent_attrib.z, abs(normal_tangent_attrib.w) * 2.0 - 1.0));
float binormalf = sign(tangent_attrib.y); float binormalf = sign(normal_tangent_attrib.w);
#else #else
vec3 tangent = tangent_attrib.xyz; vec3 tangent = tangent_attrib.xyz;
float binormalf = tangent_attrib.a; float binormalf = tangent_attrib.a;

View File

@ -3397,15 +3397,15 @@ void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS:
} break; } break;
case VS::ARRAY_NORMAL: { case VS::ARRAY_NORMAL: {
if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
// Always pack normal and tangent into vec4
// normal will be xy tangent will be zw
// normal will always be oct32 (4 byte) encoded
// UNLESS tangent exists and is also compressed
// then it will be oct16 encoded along with tangent
attribs[i].normalized = GL_TRUE; attribs[i].normalized = GL_TRUE;
attribs[i].size = 2; attribs[i].size = 4;
if (p_format & VS::ARRAY_COMPRESS_NORMAL) { attribs[i].type = GL_SHORT;
attribs[i].type = GL_BYTE; attributes_stride += 4;
attributes_stride += 2;
} else {
attribs[i].type = GL_SHORT;
attributes_stride += 4;
}
} else { } else {
attribs[i].size = 3; attribs[i].size = 3;
@ -3423,13 +3423,14 @@ void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS:
} break; } break;
case VS::ARRAY_TANGENT: { case VS::ARRAY_TANGENT: {
if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & VS::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
attribs[i].normalized = GL_TRUE; attribs[i].enabled = false;
attribs[i].size = 2;
if (p_format & VS::ARRAY_COMPRESS_TANGENT) { if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
attribs[i].type = GL_BYTE; // normal and tangent will each be oct16 (2 bytes each)
attributes_stride += 2; // pack into single vec4<GL_BYTE> for memory bandwidth
// savings while keeping 4 byte alignment
attribs[VS::ARRAY_NORMAL].type = GL_BYTE;
} else { } else {
attribs[i].type = GL_SHORT; // normal and tangent will each be oct32 (4 bytes each)
attributes_stride += 4; attributes_stride += 4;
} }
} else { } else {

View File

@ -26,13 +26,11 @@ ARRAY_INDEX=8,
layout(location = 0) in highp vec4 vertex_attrib; layout(location = 0) in highp vec4 vertex_attrib;
/* clang-format on */ /* clang-format on */
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
layout(location = 1) in vec2 normal_attrib; layout(location = 1) in vec4 normal_tangent_attrib;
#else
layout(location = 1) in vec3 normal_attrib;
#endif #endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
layout(location = 2) in vec2 tangent_attrib; // packed into normal_attrib zw component
#else #else
layout(location = 2) in vec4 tangent_attrib; layout(location = 2) in vec4 tangent_attrib;
#endif #endif
@ -340,15 +338,15 @@ void main() {
#endif #endif
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
vec3 normal = oct_to_vec3(normal_attrib); vec3 normal = oct_to_vec3(normal_tangent_attrib.xy);
#else #else
vec3 normal = normal_attrib; vec3 normal = normal_attrib;
#endif #endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION #ifdef ENABLE_OCTAHEDRAL_COMPRESSION
vec3 tangent = oct_to_vec3(vec2(tangent_attrib.x, abs(tangent_attrib.y) * 2.0 - 1.0)); vec3 tangent = oct_to_vec3(vec2(normal_tangent_attrib.z, abs(normal_tangent_attrib.w) * 2.0 - 1.0));
float binormalf = sign(tangent_attrib.y); float binormalf = sign(normal_tangent_attrib.w);
#else #else
vec3 tangent = tangent_attrib.xyz; vec3 tangent = tangent_attrib.xyz;
float binormalf = tangent_attrib.a; float binormalf = tangent_attrib.a;

View File

@ -491,7 +491,7 @@ Error VisualServer::_surface_set_data(Array p_arrays, uint32_t p_format, uint32_
// setting vertices means regenerating the AABB // setting vertices means regenerating the AABB
if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
if (p_format & ARRAY_COMPRESS_NORMAL) { if ((p_format & ARRAY_COMPRESS_NORMAL) && (p_format & ARRAY_FORMAT_TANGENT) && (p_format & ARRAY_COMPRESS_TANGENT)) {
for (int i = 0; i < p_vertex_array_len; i++) { for (int i = 0; i < p_vertex_array_len; i++) {
Vector2 res = norm_to_oct(src[i]); Vector2 res = norm_to_oct(src[i]);
int8_t vector[2] = { int8_t vector[2] = {
@ -878,7 +878,10 @@ uint32_t VisualServer::mesh_surface_make_offsets_from_format(uint32_t p_format,
} break; } break;
case VS::ARRAY_NORMAL: { case VS::ARRAY_NORMAL: {
if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
if (p_format & ARRAY_COMPRESS_NORMAL) { // normal will always be oct32 (4 byte) encoded
// UNLESS tangent exists and is also compressed
// then it will be oct16 encoded along with tangent
if ((p_format & ARRAY_COMPRESS_NORMAL) && (p_format & ARRAY_FORMAT_TANGENT) && (p_format & ARRAY_COMPRESS_TANGENT)) {
elem_size = sizeof(uint8_t) * 2; elem_size = sizeof(uint8_t) * 2;
} else { } else {
elem_size = sizeof(uint16_t) * 2; elem_size = sizeof(uint16_t) * 2;
@ -1083,7 +1086,10 @@ void VisualServer::mesh_add_surface_from_arrays(RID p_mesh, PrimitiveType p_prim
} break; } break;
case VS::ARRAY_NORMAL: { case VS::ARRAY_NORMAL: {
if (p_compress_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_compress_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
if (p_compress_format & ARRAY_COMPRESS_NORMAL) { // normal will always be oct32 (4 byte) encoded
// UNLESS tangent exists and is also compressed
// then it will be oct16 encoded along with tangent
if ((p_compress_format & ARRAY_COMPRESS_NORMAL) && (format & ARRAY_FORMAT_TANGENT) && (p_compress_format & ARRAY_COMPRESS_TANGENT)) {
elem_size = sizeof(uint8_t) * 2; elem_size = sizeof(uint8_t) * 2;
} else { } else {
elem_size = sizeof(uint16_t) * 2; elem_size = sizeof(uint16_t) * 2;
@ -1286,7 +1292,10 @@ Array VisualServer::_get_array_from_surface(uint32_t p_format, PoolVector<uint8_
} break; } break;
case VS::ARRAY_NORMAL: { case VS::ARRAY_NORMAL: {
if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
if (p_format & ARRAY_COMPRESS_NORMAL) { // normal will always be oct32 (4 byte) encoded
// UNLESS tangent exists and is also compressed
// then it will be oct16 encoded along with tangent
if ((p_format & ARRAY_COMPRESS_NORMAL) && (p_format & ARRAY_FORMAT_TANGENT) && (p_format & ARRAY_COMPRESS_TANGENT)) {
elem_size = sizeof(uint8_t) * 2; elem_size = sizeof(uint8_t) * 2;
} else { } else {
elem_size = sizeof(uint16_t) * 2; elem_size = sizeof(uint16_t) * 2;
@ -1443,7 +1452,7 @@ Array VisualServer::_get_array_from_surface(uint32_t p_format, PoolVector<uint8_
arr.resize(p_vertex_len); arr.resize(p_vertex_len);
if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) { if (p_format & ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION) {
if (p_format & ARRAY_COMPRESS_NORMAL) { if (p_format & ARRAY_COMPRESS_NORMAL && (p_format & ARRAY_FORMAT_TANGENT) && (p_format & ARRAY_COMPRESS_TANGENT)) {
PoolVector<Vector3>::Write w = arr.write(); PoolVector<Vector3>::Write w = arr.write();
for (int j = 0; j < p_vertex_len; j++) { for (int j = 0; j < p_vertex_len; j++) {