From 249c9f52c8418aadfd53ac74f02eb279f96baee2 Mon Sep 17 00:00:00 2001 From: "M. Waisberg" Date: Mon, 13 Jan 2020 23:03:55 +0200 Subject: [PATCH 1/2] multiplication is expensive. --- ViroRenderer/VROGLTFLoader.cpp | 14 +++++++------- ViroRenderer/VROMatrix4f.cpp | 17 +++++++++-------- ViroRenderer/VROMorpher.cpp | 18 ++++++++++-------- ViroRenderer/VROParticleUBO.cpp | 13 +++++++------ 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/ViroRenderer/VROGLTFLoader.cpp b/ViroRenderer/VROGLTFLoader.cpp index e073f813..e86284f6 100644 --- a/ViroRenderer/VROGLTFLoader.cpp +++ b/ViroRenderer/VROGLTFLoader.cpp @@ -1795,7 +1795,7 @@ std::shared_ptr VROGLTFLoader::buildBoneWeightSource(GLTFType int sizeOfSingleBoneWeight = getTypeSize(gType) * getComponentTypeSize(gTypeComponent); float *dataOut = new float[sizeOfSingleBoneWeight * gAttributeAccesor.count](); for (int elementIndex = 0; elementIndex < gAttributeAccesor.count; elementIndex++) { - + int elementIndex4 = elementIndex << 2; // For the current element, cycle through each of its float or type component // and convert them into a float through the math conversions required by gLTF. buffer.setPosition(elementIndex * bufferViewStride); @@ -1806,11 +1806,11 @@ std::shared_ptr VROGLTFLoader::buildBoneWeightSource(GLTFType weight.push_back(floatData); } else if (gTypeComponent == GLTFTypeComponent::UnsignedByte) { unsigned uByteData = buffer.readUnsignedByte(); - float point = uByteData / 255.0; + float point = uByteData / 255.0F; weight.push_back(point); } else if (gTypeComponent == GLTFTypeComponent::UnsignedShort) { unsigned short uShortData = buffer.readUnsignedShort(); - float point = uShortData / 65535.0; + float point = uShortData / 65535.0F; weight.push_back(point); } else { perr("Invalid weighted bone data provided for the 3D glTF skinner."); @@ -1821,10 +1821,10 @@ std::shared_ptr VROGLTFLoader::buildBoneWeightSource(GLTFType float totalWeight = weight[0] + weight[1] + weight[2] + weight[3]; VROVector4f normalizedWeight; VROVector4f(weight[0], weight[1], weight[2], weight[3]).scale(1/totalWeight, &normalizedWeight); - dataOut[(elementIndex * 4)] = normalizedWeight.x; - dataOut[(elementIndex * 4) + 1] = normalizedWeight.y; - dataOut[(elementIndex * 4) + 2] = normalizedWeight.z; - dataOut[(elementIndex * 4) + 3] = normalizedWeight.w; + dataOut[elementIndex4] = normalizedWeight.x; + dataOut[elementIndex4 + 1] = normalizedWeight.y; + dataOut[elementIndex4 + 2] = normalizedWeight.z; + dataOut[elementIndex4 + 3] = normalizedWeight.w; } // Finally create our geometry sources with the normalized data. diff --git a/ViroRenderer/VROMatrix4f.cpp b/ViroRenderer/VROMatrix4f.cpp index 9d1e654e..5032b822 100644 --- a/ViroRenderer/VROMatrix4f.cpp +++ b/ViroRenderer/VROMatrix4f.cpp @@ -46,10 +46,11 @@ VROMatrix4f::VROMatrix4f(const float *matrix) { VROMatrix4f::VROMatrix4f(const glm::mat4x4 mat) { for (int i = 0; i < 4; i++) { - _mtx[i * 4 + 0] = mat[i].x; - _mtx[i * 4 + 1] = mat[i].y; - _mtx[i * 4 + 2] = mat[i].z; - _mtx[i * 4 + 3] = mat[i].w; + int i0 = i << 2; + _mtx[i0 + 0] = mat[i].x; + _mtx[i0 + 1] = mat[i].y; + _mtx[i0 + 2] = mat[i].z; + _mtx[i0 + 3] = mat[i].w; } } @@ -90,7 +91,7 @@ void VROMatrix4f::rotateX(float angleRad) { float rcos = sincosr[1]; for (int i = 0; i < 3; i++) { - int i1 = i * 4 + 1; + int i1 = (i << 2) + 1; int i2 = i1 + 1; float t = _mtx[i1]; _mtx[i1] = t * rcos - _mtx[i2] * rsin; @@ -106,7 +107,7 @@ void VROMatrix4f::rotateY(float angleRad) { float rcos = sincosr[1]; for (int i = 0; i < 3; i++) { - int i0 = i * 4; + int i0 = i << 2; int i2 = i0 + 2; float t = _mtx[i0]; _mtx[i0] = t * rcos + _mtx[i2] * rsin; @@ -122,7 +123,7 @@ void VROMatrix4f::rotateZ(float angleRad) { float rcos = sincosr[1]; for (int i = 0; i < 3; i++) { - int i0 = i * 4; + int i0 = i << 2; int i1 = i0 + 1; float t = _mtx[i0]; _mtx[i0] = t * rcos - _mtx[i1] * rsin; @@ -199,7 +200,7 @@ void VROMatrix4f::translate(const VROVector3f &vector) { void VROMatrix4f::scale(float x, float y, float z) { for (int i = 0; i < 3; i++) { - int i0 = i * 4; + int i0 = i << 2; _mtx[i0] *= x; _mtx[i0 + 1] *= y; _mtx[i0 + 2] *= z; diff --git a/ViroRenderer/VROMorpher.cpp b/ViroRenderer/VROMorpher.cpp index 9dbc415c..77c06adf 100644 --- a/ViroRenderer/VROMorpher.cpp +++ b/ViroRenderer/VROMorpher.cpp @@ -787,10 +787,11 @@ inline void VROMorpher::addWeightedMorphToSrc4(float *srcDataOut, float weight) { int morphSize = (int) morphData.size(); for (int i = 0; i < morphSize; i ++) { - srcDataOut[i * 4] += (morphData[i].x * (weight)); - srcDataOut[(i * 4) + 1] += (morphData[i].y * (weight)); - srcDataOut[(i * 4) + 2] += (morphData[i].z * (weight)); - srcDataOut[(i * 4) + 3] += (morphData[i].z * (weight)); + int i0 = i << 2; + srcDataOut[i0] += (morphData[i].x * (weight)); + srcDataOut[i0 + 1] += (morphData[i].y * (weight)); + srcDataOut[i0 + 2] += (morphData[i].z * (weight)); + srcDataOut[i0 + 3] += (morphData[i].z * (weight)); } } @@ -804,9 +805,10 @@ inline void VROMorpher::resetSrc3(float *srcDataOut, int size) { inline void VROMorpher::resetSrc4(float *srcDataOut, int size) { for (int i = 0; i < size; i ++) { - srcDataOut[i * 4] = 0; - srcDataOut[(i * 4) + 1] = 0; - srcDataOut[(i * 4) + 2] = 0; - srcDataOut[(i * 4) + 3] = 0; + int i0 = i << 2; + srcDataOut[i0] = 0; + srcDataOut[i0 + 1] = 0; + srcDataOut[i0 + 2] = 0; + srcDataOut[i0 + 3] = 0; } } diff --git a/ViroRenderer/VROParticleUBO.cpp b/ViroRenderer/VROParticleUBO.cpp index 0d8696df..38f6d664 100644 --- a/ViroRenderer/VROParticleUBO.cpp +++ b/ViroRenderer/VROParticleUBO.cpp @@ -120,16 +120,17 @@ int VROParticleUBO::bindDrawData(int currentDrawCallIndex) { // Parse / serialize the data into the uniform buffer object VROParticlesUBOVertexData vertexData; VROParticlesUBOFragmentData fragmentData; - for (int i = start; i < end; i++) { + for (int i = start, i0 = 0; i < end; i++, i0++) { + int i1 = i0 << 2; const float *transformArray = _lastKnownParticles[i].currentWorldTransform.getArray(); - memcpy(&vertexData.particles_transform[(i - start) * kMaxFloatsPerTransform], + memcpy(&vertexData.particles_transform[i0 * kMaxFloatsPerTransform], transformArray, kMaxFloatsPerTransform * sizeof(float)); - fragmentData.frag_particles_color[(i - start) * 4 + 0] = _lastKnownParticles[i].colorCurrent.x; - fragmentData.frag_particles_color[(i - start) * 4 + 1] = _lastKnownParticles[i].colorCurrent.y; - fragmentData.frag_particles_color[(i - start) * 4 + 2] = _lastKnownParticles[i].colorCurrent.z; - fragmentData.frag_particles_color[(i - start) * 4 + 3] = _lastKnownParticles[i].colorCurrent.w; + fragmentData.frag_particles_color[i1 + 0] = _lastKnownParticles[i].colorCurrent.x; + fragmentData.frag_particles_color[i1 + 1] = _lastKnownParticles[i].colorCurrent.y; + fragmentData.frag_particles_color[i1 + 2] = _lastKnownParticles[i].colorCurrent.z; + fragmentData.frag_particles_color[i1 + 3] = _lastKnownParticles[i].colorCurrent.w; } // Finally bind the UBO to its corresponding buffers. From 715459133fa3adbdff35ae13569d88de524ac3dc Mon Sep 17 00:00:00 2001 From: "M. Waisberg" Date: Mon, 13 Jan 2020 23:33:24 +0200 Subject: [PATCH 2/2] multiplication is expensive, so calculate it once. multiplication is multiples of addition. --- ViroRenderer/VROBoneUBO.cpp | 35 +++++++------- ViroRenderer/VROBox.cpp | 18 ++++---- ViroRenderer/VROGLTFLoader.cpp | 10 ++-- ViroRenderer/VROGeometryElement.cpp | 4 +- ViroRenderer/VROGeometrySource.cpp | 12 ++--- ViroRenderer/VROHDRLoader.cpp | 8 ++-- ViroRenderer/VROLightingUBO.cpp | 5 +- ViroRenderer/VROMatrix4f.cpp | 34 +++++++------- ViroRenderer/VROMorpher.cpp | 46 ++++++++++--------- ViroRenderer/VROSparseBitSet.cpp | 10 ++-- ViroRenderer/capi/ViroUtils_JNI.cpp | 7 +-- .../app/src/main/cpp/arcore/ARUtils_JNI.cpp | 9 ++-- .../src/main/cpp/arcore/VROARFrameARCore.cpp | 7 +-- .../com/viro/metrics/java/ViroKeenUtils.java | 9 ++-- 14 files changed, 112 insertions(+), 102 deletions(-) diff --git a/ViroRenderer/VROBoneUBO.cpp b/ViroRenderer/VROBoneUBO.cpp index 5d6f4743..7bb994c9 100644 --- a/ViroRenderer/VROBoneUBO.cpp +++ b/ViroRenderer/VROBoneUBO.cpp @@ -113,8 +113,8 @@ VROBoneUBO::VROBoneUBO(std::shared_ptr driver) : // Adreno + OVR) VROBonesData data; VROMatrix4f identity; - for (int i = 0; i < kMaxBones; i++) { - memcpy(&data.bone_transforms[i * kFloatsPerBone], identity.getArray(), kFloatsPerBone * sizeof(float)); + for (int i = 0, iFPB = 0; i < kMaxBones; i++, iFPB += kFloatsPerBone) { + memcpy(&data.bone_transforms[iFPB], identity.getArray(), kFloatsPerBone * sizeof(float)); } GL( glBindBuffer(GL_UNIFORM_BUFFER, _bonesUBO) ); GL( glBufferData(GL_UNIFORM_BUFFER, sizeof(VROBonesData), &data, GL_DYNAMIC_DRAW) ); @@ -136,11 +136,11 @@ void VROBoneUBO::update(const std::shared_ptr &skinner) { VROBonesData data; int numBones = skinner->getSkeleton()->getNumBones(); - for (int i = 0; i < numBones; i++) { + for (int i = 0, iFPB = 0; i < numBones; i++, iFPB += kFloatsPerBone) { if (i >= kMaxBones) { break; } - + VROMatrix4f transform = skinner->getModelTransform(i); if (kDualQuaternionEnabled) { VROVector3f translation = transform.extractTranslation(); @@ -155,22 +155,21 @@ void VROBoneUBO::update(const std::shared_ptr &skinner) { VROQuaternion real = dq.getReal(); VROQuaternion dual = dq.getDual(); - int floatsPerBone = kFloatsPerBone; - data.bone_transforms[i * floatsPerBone + 0] = real.X; - data.bone_transforms[i * floatsPerBone + 1] = real.Y; - data.bone_transforms[i * floatsPerBone + 2] = real.Z; - data.bone_transforms[i * floatsPerBone + 3] = real.W; - data.bone_transforms[i * floatsPerBone + 4] = dual.X; - data.bone_transforms[i * floatsPerBone + 5] = dual.Y; - data.bone_transforms[i * floatsPerBone + 6] = dual.Z; - data.bone_transforms[i * floatsPerBone + 7] = dual.W; - data.bone_transforms[i * floatsPerBone + 8] = scale.x; - data.bone_transforms[i * floatsPerBone + 9] = scale.y; - data.bone_transforms[i * floatsPerBone + 10] = scale.z; - data.bone_transforms[i * floatsPerBone + 11] = 1.0; + data.bone_transforms[iFPB + 0] = real.X; + data.bone_transforms[iFPB + 1] = real.Y; + data.bone_transforms[iFPB + 2] = real.Z; + data.bone_transforms[iFPB + 3] = real.W; + data.bone_transforms[iFPB + 4] = dual.X; + data.bone_transforms[iFPB + 5] = dual.Y; + data.bone_transforms[iFPB + 6] = dual.Z; + data.bone_transforms[iFPB + 7] = dual.W; + data.bone_transforms[iFPB + 8] = scale.x; + data.bone_transforms[iFPB + 9] = scale.y; + data.bone_transforms[iFPB + 10] = scale.z; + data.bone_transforms[iFPB + 11] = 1.0; } else { - memcpy(&data.bone_transforms[i * kFloatsPerBone], transform.getArray(), kFloatsPerBone * sizeof(float)); + memcpy(&data.bone_transforms[iFPB], transform.getArray(), kFloatsPerBone * sizeof(float)); } } diff --git a/ViroRenderer/VROBox.cpp b/ViroRenderer/VROBox.cpp index 60447174..5694f2ac 100644 --- a/ViroRenderer/VROBox.cpp +++ b/ViroRenderer/VROBox.cpp @@ -254,14 +254,16 @@ void VROBox::buildBoxVAR(VROShapeVertexLayout *vertexLayout) { }; for (int i = 0; i < kNumBoxVertices; i++) { - vertexLayout[i].x = cubeVertices[i * 3 + 0]; - vertexLayout[i].y = cubeVertices[i * 3 + 1]; - vertexLayout[i].z = cubeVertices[i * 3 + 2]; - vertexLayout[i].u = cubeTex[i * 2 + 0]; - vertexLayout[i].v = cubeTex[i * 2 + 1]; - vertexLayout[i].nx = cubeNormals[i * 3 + 0]; - vertexLayout[i].ny = cubeNormals[i * 3 + 1]; - vertexLayout[i].nz = cubeNormals[i * 3 + 2]; + int i2 = i << 1; + int i3 = i + i2; + vertexLayout[i].x = cubeVertices[i3 + 0]; + vertexLayout[i].y = cubeVertices[i3 + 1]; + vertexLayout[i].z = cubeVertices[i3 + 2]; + vertexLayout[i].u = cubeTex[i2 + 0]; + vertexLayout[i].v = cubeTex[i2 + 1]; + vertexLayout[i].nx = cubeNormals[i3 + 0]; + vertexLayout[i].ny = cubeNormals[i3 + 1]; + vertexLayout[i].nz = cubeNormals[i3 + 2]; } } diff --git a/ViroRenderer/VROGLTFLoader.cpp b/ViroRenderer/VROGLTFLoader.cpp index e86284f6..81d1d7ea 100644 --- a/ViroRenderer/VROGLTFLoader.cpp +++ b/ViroRenderer/VROGLTFLoader.cpp @@ -1390,11 +1390,11 @@ void VROGLTFLoader::processTangent(std::vectorgetVertexCount(); float *dataOut = new float[vertexSize * sizeOfTangent]; - for (int i = 0; i < vertexSize; i ++ ) { - dataOut[(i * sizeOfTangent)] = generatedTangents[i].x; - dataOut[(i * sizeOfTangent) + 1] = generatedTangents[i].y; - dataOut[(i * sizeOfTangent) + 2] = generatedTangents[i].z; - dataOut[(i * sizeOfTangent) + 3] = generatedTangents[i].w; + for (int i = 0, j = 0; i < vertexSize; i++, j += sizeOfTangent) { + dataOut[j] = generatedTangents[i].x; + dataOut[j + 1] = generatedTangents[i].y; + dataOut[j + 2] = generatedTangents[i].z; + dataOut[j + 3] = generatedTangents[i].w; } int sizeOfSingleTangent = getTypeSize(GLTFType::Vec4) * getComponentTypeSize(GLTFTypeComponent::Float); diff --git a/ViroRenderer/VROGeometryElement.cpp b/ViroRenderer/VROGeometryElement.cpp index 35abfcfb..cae473c8 100644 --- a/ViroRenderer/VROGeometryElement.cpp +++ b/ViroRenderer/VROGeometryElement.cpp @@ -83,8 +83,8 @@ void VROGeometryElement::processIndices(std::function function) int indexCount = _primitiveCount * 3; - for (int i = 0; i < indexCount; i++) { - buffer.setPosition(i * _bytesPerIndex); + for (int i = 0, o = 0; i < indexCount; i++, o += _bytesPerIndex) { + buffer.setPosition(o); int idx; if (_bytesPerIndex == 2) { idx = _signed ? buffer.readShort() : buffer.readUnsignedShort(); diff --git a/ViroRenderer/VROGeometrySource.cpp b/ViroRenderer/VROGeometrySource.cpp index 1768f671..c97dfc98 100644 --- a/ViroRenderer/VROGeometrySource.cpp +++ b/ViroRenderer/VROGeometrySource.cpp @@ -46,8 +46,8 @@ void VROGeometrySource::processVertices(std::function f std::shared_ptr data = getData(); VROByteBuffer buffer(data->getData(), data->getDataLength(), false); - for (int i = 0; i < _vertexCount; i++) { - buffer.setPosition(i * _dataStride + _dataOffset); + for (int i = 0, o = _dataOffset; i < _vertexCount; i++, o += _dataStride) { + buffer.setPosition(o); float x = 0, y = 0, z = 0, w = 0; @@ -139,9 +139,9 @@ void VROGeometrySource::processVertices(std::function f void VROGeometrySource::modifyVertices(std::function function) const { std::shared_ptr data = getData(); VROByteBuffer buffer(data->getData(), data->getDataLength(), false); - - for (int i = 0; i < _vertexCount; i++) { - buffer.setPosition(i * _dataStride + _dataOffset); + + for (int i = 0, o = _dataOffset; i < _vertexCount; i++, o += _dataStride) { + buffer.setPosition(o); float x = 0, y = 0, z = 0, w = 0; @@ -228,7 +228,7 @@ void VROGeometrySource::modifyVertices(std::function 0) { diff --git a/ViroRenderer/VROHDRLoader.cpp b/ViroRenderer/VROHDRLoader.cpp index 29fa8d55..afd43941 100644 --- a/ViroRenderer/VROHDRLoader.cpp +++ b/ViroRenderer/VROHDRLoader.cpp @@ -66,10 +66,10 @@ std::shared_ptr VROHDRLoader::loadTexture(float *data, int width, in if (kCompressHDR) { int packedLength = numPixels * sizeof(uint32_t); uint32_t *packedF9E5 = (uint32_t *) malloc(packedLength); - for (int i = 0; i < numPixels; i++) { - float r = data[i * componentsPerPixel + 0]; - float g = data[i * componentsPerPixel + 1]; - float b = data[i * componentsPerPixel + 2]; + for (int i = 0, j = 0; i < numPixels; i++, j += componentsPerPixel) { + float r = data[j]; + float g = data[j + 1]; + float b = data[j + 2]; // alpha is disregarded const glm::vec3 v(r, g, b); diff --git a/ViroRenderer/VROLightingUBO.cpp b/ViroRenderer/VROLightingUBO.cpp index 6737abbb..54d01901 100644 --- a/ViroRenderer/VROLightingUBO.cpp +++ b/ViroRenderer/VROLightingUBO.cpp @@ -147,11 +147,12 @@ void VROLightingUBO::updateLightsVertex() { continue; } int i = vertexData.num_lights; + int iFPM = i * kFloatsPerMatrix; const VROMatrix4f &shadowView = light->getShadowViewMatrix(); - memcpy(&vertexData.shadow_view_matrices[i * kFloatsPerMatrix], shadowView.getArray(), kFloatsPerMatrix * sizeof(float)); + memcpy(&vertexData.shadow_view_matrices[iFPM], shadowView.getArray(), kFloatsPerMatrix * sizeof(float)); const VROMatrix4f &shadowProjection = light->getShadowProjectionMatrix(); - memcpy(&vertexData.shadow_projection_matrices[i * kFloatsPerMatrix], shadowProjection.getArray(), kFloatsPerMatrix * sizeof(float)); + memcpy(&vertexData.shadow_projection_matrices[iFPM], shadowProjection.getArray(), kFloatsPerMatrix * sizeof(float)); vertexData.num_lights++; if (vertexData.num_lights >= kMaxLights) { diff --git a/ViroRenderer/VROMatrix4f.cpp b/ViroRenderer/VROMatrix4f.cpp index 5032b822..1dd6babf 100644 --- a/ViroRenderer/VROMatrix4f.cpp +++ b/ViroRenderer/VROMatrix4f.cpp @@ -46,11 +46,11 @@ VROMatrix4f::VROMatrix4f(const float *matrix) { VROMatrix4f::VROMatrix4f(const glm::mat4x4 mat) { for (int i = 0; i < 4; i++) { - int i0 = i << 2; - _mtx[i0 + 0] = mat[i].x; - _mtx[i0 + 1] = mat[i].y; - _mtx[i0 + 2] = mat[i].z; - _mtx[i0 + 3] = mat[i].w; + int i4 = i << 2; + _mtx[i4 + 0] = mat[i].x; + _mtx[i4 + 1] = mat[i].y; + _mtx[i4 + 2] = mat[i].z; + _mtx[i4 + 3] = mat[i].w; } } @@ -107,10 +107,10 @@ void VROMatrix4f::rotateY(float angleRad) { float rcos = sincosr[1]; for (int i = 0; i < 3; i++) { - int i0 = i << 2; - int i2 = i0 + 2; - float t = _mtx[i0]; - _mtx[i0] = t * rcos + _mtx[i2] * rsin; + int i4 = i << 2; + int i2 = i4 + 2; + float t = _mtx[i4]; + _mtx[i4] = t * rcos + _mtx[i2] * rsin; _mtx[i2] = _mtx[i2] * rcos - t * rsin; } } @@ -123,10 +123,10 @@ void VROMatrix4f::rotateZ(float angleRad) { float rcos = sincosr[1]; for (int i = 0; i < 3; i++) { - int i0 = i << 2; - int i1 = i0 + 1; - float t = _mtx[i0]; - _mtx[i0] = t * rcos - _mtx[i1] * rsin; + int i4 = i << 2; + int i1 = i4 + 1; + float t = _mtx[i4]; + _mtx[i4] = t * rcos - _mtx[i1] * rsin; _mtx[i1] = t * rsin + _mtx[i1] * rcos; } } @@ -200,10 +200,10 @@ void VROMatrix4f::translate(const VROVector3f &vector) { void VROMatrix4f::scale(float x, float y, float z) { for (int i = 0; i < 3; i++) { - int i0 = i << 2; - _mtx[i0] *= x; - _mtx[i0 + 1] *= y; - _mtx[i0 + 2] *= z; + int i4 = i << 2; + _mtx[i4] *= x; + _mtx[i4 + 1] *= y; + _mtx[i4 + 2] *= z; } } diff --git a/ViroRenderer/VROMorpher.cpp b/ViroRenderer/VROMorpher.cpp index 77c06adf..fe6e3f55 100644 --- a/ViroRenderer/VROMorpher.cpp +++ b/ViroRenderer/VROMorpher.cpp @@ -699,12 +699,12 @@ std::shared_ptr VROMorpher::convertVecToGeoSource( float *sourcesData = (dataVecIn.size() > 0) ? new float[dataVecIn.size() * componentsPerVertex] : nullptr; int morphSize = (int) dataVecIn.size(); - for (int i = 0; i < morphSize; i ++) { - sourcesData[ i * componentsPerVertex] = dataVecIn[i].x; - sourcesData[(i * componentsPerVertex) + 1] = dataVecIn[i].y; - sourcesData[(i * componentsPerVertex) + 2] = dataVecIn[i].z; - if (componentsPerVertex > 3){ - sourcesData[(i * componentsPerVertex) + 3] = dataVecIn[i].w; + for (int i = 0, j = 0; i < morphSize; i++, j += componentsPerVertex) { + sourcesData[j] = dataVecIn[i].x; + sourcesData[j + 1] = dataVecIn[i].y; + sourcesData[j + 2] = dataVecIn[i].z; + if (componentsPerVertex > 3) { + sourcesData[j + 3] = dataVecIn[i].w; } } std::shared_ptr data = std::make_shared((void *) sourcesData, dataVecIn.size() * componentsPerVertex * sizeof(float)); @@ -776,9 +776,10 @@ inline void VROMorpher::addWeightedMorphToSrc3(float *srcDataOut, float weight) { int morphSize = (int) morphData.size(); for (int i = 0; i < morphSize; i ++) { - srcDataOut[i * 3] += (morphData[i].x * (weight)); - srcDataOut[(i * 3) + 1] += (morphData[i].y * (weight)); - srcDataOut[(i * 3) + 2] += (morphData[i].z * (weight)); + int i3 = i * 3; + srcDataOut[i3] += (morphData[i].x * (weight)); + srcDataOut[i3 + 1] += (morphData[i].y * (weight)); + srcDataOut[i3 + 2] += (morphData[i].z * (weight)); } } @@ -787,28 +788,29 @@ inline void VROMorpher::addWeightedMorphToSrc4(float *srcDataOut, float weight) { int morphSize = (int) morphData.size(); for (int i = 0; i < morphSize; i ++) { - int i0 = i << 2; - srcDataOut[i0] += (morphData[i].x * (weight)); - srcDataOut[i0 + 1] += (morphData[i].y * (weight)); - srcDataOut[i0 + 2] += (morphData[i].z * (weight)); - srcDataOut[i0 + 3] += (morphData[i].z * (weight)); + int i4 = i << 2; + srcDataOut[i4] += (morphData[i].x * (weight)); + srcDataOut[i4 + 1] += (morphData[i].y * (weight)); + srcDataOut[i4 + 2] += (morphData[i].z * (weight)); + srcDataOut[i4 + 3] += (morphData[i].z * (weight)); } } inline void VROMorpher::resetSrc3(float *srcDataOut, int size) { for (int i = 0; i < size; i ++) { - srcDataOut[i * 3] = 0; - srcDataOut[(i * 3) + 1] = 0; - srcDataOut[(i * 3) + 2] = 0; + int i3 = i * 3; + srcDataOut[i3] = 0; + srcDataOut[i3 + 1] = 0; + srcDataOut[i3 + 2] = 0; } } inline void VROMorpher::resetSrc4(float *srcDataOut, int size) { for (int i = 0; i < size; i ++) { - int i0 = i << 2; - srcDataOut[i0] = 0; - srcDataOut[i0 + 1] = 0; - srcDataOut[i0 + 2] = 0; - srcDataOut[i0 + 3] = 0; + int i4 = i << 2; + srcDataOut[i4] = 0; + srcDataOut[i4 + 1] = 0; + srcDataOut[i4 + 2] = 0; + srcDataOut[i4 + 3] = 0; } } diff --git a/ViroRenderer/VROSparseBitSet.cpp b/ViroRenderer/VROSparseBitSet.cpp index bfcf2b03..22899100 100644 --- a/ViroRenderer/VROSparseBitSet.cpp +++ b/ViroRenderer/VROSparseBitSet.cpp @@ -54,8 +54,9 @@ uint32_t VROSparseBitSet::calcNumPages(const uint32_t* ranges, size_t nRanges) { uint32_t nonzeroPageEnd = 0; uint32_t nPages = 0; for (size_t i = 0; i < nRanges; i++) { - uint32_t start = ranges[i * 2]; - uint32_t end = ranges[i * 2 + 1]; + size_t i2 = i << 1; + uint32_t start = ranges[i2]; + uint32_t end = ranges[i2 + 1]; uint32_t startPage = start >> kLogValuesPerPage; uint32_t endPage = (end - 1) >> kLogValuesPerPage; if (startPage >= nonzeroPageEnd) { @@ -89,8 +90,9 @@ void VROSparseBitSet::initFromRanges(const uint32_t* ranges, size_t nRanges) { uint32_t nonzeroPageEnd = 0; uint32_t currentPage = 0; for (size_t i = 0; i < nRanges; i++) { - uint32_t start = ranges[i * 2]; - uint32_t end = ranges[i * 2 + 1]; + size_t i2 = i << 1; + uint32_t start = ranges[i2]; + uint32_t end = ranges[i2 + 1]; passert(start < end); // make sure range size is nonnegative uint32_t startPage = start >> kLogValuesPerPage; uint32_t endPage = (end - 1) >> kLogValuesPerPage; diff --git a/ViroRenderer/capi/ViroUtils_JNI.cpp b/ViroRenderer/capi/ViroUtils_JNI.cpp index e8380ce0..50997050 100644 --- a/ViroRenderer/capi/ViroUtils_JNI.cpp +++ b/ViroRenderer/capi/ViroUtils_JNI.cpp @@ -66,9 +66,10 @@ VRO_FLOAT_ARRAY ARUtilsCreatePointsArray(std::vector points) { // populate the array with Vector objects for (int i = 0; i < points.size(); i++) { - tempPointsArr[i * 3] = points[i].x; - tempPointsArr[i * 3 + 1] = points[i].y; - tempPointsArr[i * 3 + 2] = points[i].z; + int i3 = i * 3; + tempPointsArr[i3] = points[i].x; + tempPointsArr[i3 + 1] = points[i].y; + tempPointsArr[i3 + 2] = points[i].z; } VRO_FLOAT_ARRAY jPointsArray = VRO_NEW_FLOAT_ARRAY(points.size() * 3); diff --git a/android/app/src/main/cpp/arcore/ARUtils_JNI.cpp b/android/app/src/main/cpp/arcore/ARUtils_JNI.cpp index a9ebe656..f92b1388 100644 --- a/android/app/src/main/cpp/arcore/ARUtils_JNI.cpp +++ b/android/app/src/main/cpp/arcore/ARUtils_JNI.cpp @@ -186,10 +186,11 @@ VRO_OBJECT ARUtilsCreateARPointCloud(std::shared_ptr pointCloud // populate the array with Vector objects for (int i = 0; i < points.size(); i++) { - tempConfidencesArr[i * 4] = points[i].x; - tempConfidencesArr[i * 4 + 1] = points[i].y; - tempConfidencesArr[i * 4 + 2] = points[i].z; - tempConfidencesArr[i * 4 + 3] = points[i].w; + int i4 = i << 2; + tempConfidencesArr[i4] = points[i].x; + tempConfidencesArr[i4 + 1] = points[i].y; + tempConfidencesArr[i4 + 2] = points[i].z; + tempConfidencesArr[i4 + 3] = points[i].w; } for(int i = 0; i< identifiers.size(); i++) { diff --git a/android/app/src/main/cpp/arcore/VROARFrameARCore.cpp b/android/app/src/main/cpp/arcore/VROARFrameARCore.cpp index fedef1e2..fd1bd1d1 100644 --- a/android/app/src/main/cpp/arcore/VROARFrameARCore.cpp +++ b/android/app/src/main/cpp/arcore/VROARFrameARCore.cpp @@ -285,12 +285,13 @@ std::shared_ptr VROARFrameARCore::getPointCloud() { int numPoints = pointCloud->getNumPoints(); for (int i = 0; i < numPoints; i++) { + int i4 = i << 2; // Only use points with > 0.1. This is just meant to make the display of the points // look good (if low confidence points are used, we may end up with points very close // to the camera). - if (pointsArray[i * 4 + 3] > .1) { - VROVector4f point = VROVector4f(pointsArray[i * 4 + 0], pointsArray[i * 4 + 1], - pointsArray[i * 4 + 2], pointsArray[i * 4 + 3]); + if (pointsArray[i4 + 3] > .1) { + VROVector4f point = VROVector4f(pointsArray[i4 + 0], pointsArray[i4 + 1], + pointsArray[i4 + 2], pointsArray[i4 + 3]); if(pointsIdArray != NULL) { int pointId = pointsIdArray[i]; points.push_back(point); diff --git a/android/app/src/main/java/com/viro/metrics/java/ViroKeenUtils.java b/android/app/src/main/java/com/viro/metrics/java/ViroKeenUtils.java index d0ca3927..eb9ce905 100644 --- a/android/app/src/main/java/com/viro/metrics/java/ViroKeenUtils.java +++ b/android/app/src/main/java/com/viro/metrics/java/ViroKeenUtils.java @@ -85,12 +85,13 @@ public static byte[] hexStringToByteArray(String s) { } public static String byteArrayToHexString(byte[] bytes) { - char[] hexChars = new char[bytes.length * 2]; + final int length = bytes.length; + char[] hexChars = new char[length * 2]; - for (int i = 0; i < bytes.length; i++) { + for (int i = 0, i2 = 0; i < length; i++, i2 += 2) { int b = bytes[i] & 0xFF; - hexChars[i * 2] = hexArray[b >>> 4]; - hexChars[(i * 2) + 1] = hexArray[b & 0x0F]; + hexChars[i2] = hexArray[b >>> 4]; + hexChars[i2 + 1] = hexArray[b & 0x0F]; } return new String(hexChars);