Skip to content
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

Profile guided optimization pass #2582

Merged
merged 2 commits into from
Sep 16, 2024
Merged
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
4 changes: 4 additions & 0 deletions indra/llmath/llrigginginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class LLJointRiggingInfoTab
const LLJointRiggingInfo& operator[](S32 i) const { return mRigInfoPtr[i]; };
bool needsUpdate() { return mNeedsUpdate; }
void setNeedsUpdate(bool val) { mNeedsUpdate = val; }

LLJointRiggingInfo* begin() { return mRigInfoPtr; }
LLJointRiggingInfo* end() { return mRigInfoPtr + mSize; }

private:
// Not implemented
LLJointRiggingInfoTab& operator=(const LLJointRiggingInfoTab& src);
Expand Down
7 changes: 7 additions & 0 deletions indra/llprimitive/llmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,13 @@ void LLMeshSkinInfo::fromLLSD(LLSD& skin)
mLockScaleIfJointPosition = false;
}

// combine mBindShapeMatrix and mInvBindMatrix into mBindPoseMatrix
mBindPoseMatrix.resize(mInvBindMatrix.size());
for (U32 i = 0; i < mInvBindMatrix.size(); ++i)
{
matMul(mBindShapeMatrix, mInvBindMatrix[i], mBindPoseMatrix[i]);
}

updateHash();
}

Expand Down
5 changes: 4 additions & 1 deletion indra/llprimitive/llmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ class LLMeshSkinInfo : public LLRefCount
LLUUID mMeshID;
std::vector<std::string> mJointNames;
mutable std::vector<S32> mJointNums;
typedef std::vector<LLMatrix4a, boost::alignment::aligned_allocator<LLMatrix4a, 16>> matrix_list_t;
typedef std::vector<LLMatrix4a> matrix_list_t;
matrix_list_t mInvBindMatrix;

// bones/joints position overrides
matrix_list_t mAlternateBindMatrix;

// cached multiply of mBindShapeMatrix and mInvBindMatrix
matrix_list_t mBindPoseMatrix;

LL_ALIGN_16(LLMatrix4a mBindShapeMatrix);

float mPelvisOffset;
Expand Down
62 changes: 50 additions & 12 deletions indra/llrender/llrendersphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "llerror.h"

#include "llglheaders.h"
#include "llvertexbuffer.h"
#include "llglslshader.h"

LLRenderSphere gSphere;

Expand All @@ -53,12 +55,20 @@ inline LLVector3 polar_to_cart(F32 latitude, F32 longitude)

void LLRenderSphere::renderGGL()
{
LL_PROFILE_ZONE_SCOPED;
S32 const LATITUDE_SLICES = 20;
S32 const LONGITUDE_SLICES = 30;

if (mSpherePoints.empty())
if (mVertexBuffer.isNull())
{
mSpherePoints.resize(LATITUDE_SLICES + 1);
mVertexBuffer = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX);

mVertexBuffer->allocateBuffer((U32)(LATITUDE_SLICES + 1) * (LONGITUDE_SLICES + 1), LATITUDE_SLICES * LONGITUDE_SLICES * 6);

LLStrider<LLVector3> v;
mVertexBuffer->getVertexStrider(v);

for (S32 lat_i = 0; lat_i < LATITUDE_SLICES + 1; lat_i++)
{
mSpherePoints[lat_i].resize(LONGITUDE_SLICES + 1);
Expand All @@ -68,24 +78,52 @@ void LLRenderSphere::renderGGL()
F32 lon = (F32)lon_i / LONGITUDE_SLICES;

mSpherePoints[lat_i][lon_i] = polar_to_cart(lat, lon);
v[lat_i * (LONGITUDE_SLICES + 1) + lon_i] = mSpherePoints[lat_i][lon_i];
}
}

LLStrider<U16> i;
mVertexBuffer->getIndexStrider(i);

for (S32 lat_i = 0; lat_i < LATITUDE_SLICES; lat_i++)
{
for (S32 lon_i = 0; lon_i < LONGITUDE_SLICES; lon_i++)
{
i[(lat_i * LONGITUDE_SLICES + lon_i) * 6 + 0] = lat_i * (LONGITUDE_SLICES + 1) + lon_i;
i[(lat_i * LONGITUDE_SLICES + lon_i) * 6 + 1] = lat_i * (LONGITUDE_SLICES + 1) + lon_i + 1;
i[(lat_i * LONGITUDE_SLICES + lon_i) * 6 + 2] = (lat_i + 1) * (LONGITUDE_SLICES + 1) + lon_i;

i[(lat_i * LONGITUDE_SLICES + lon_i) * 6 + 3] = (lat_i + 1) * (LONGITUDE_SLICES + 1) + lon_i;
i[(lat_i * LONGITUDE_SLICES + lon_i) * 6 + 4] = lat_i * (LONGITUDE_SLICES + 1) + lon_i + 1;
i[(lat_i * LONGITUDE_SLICES + lon_i) * 6 + 5] = (lat_i + 1) * (LONGITUDE_SLICES + 1) + lon_i + 1;
}
}

mVertexBuffer->unmapBuffer();
}

gGL.begin(LLRender::TRIANGLES);

for (S32 lat_i = 0; lat_i < LATITUDE_SLICES; lat_i++)
{
for (S32 lon_i = 0; lon_i < LONGITUDE_SLICES; lon_i++)
if (LLGLSLShader::sCurBoundShaderPtr->mAttributeMask == LLVertexBuffer::MAP_VERTEX)
{ // shader expects only vertex positions in vertex buffer, use fast path
mVertexBuffer->setBuffer();
mVertexBuffer->drawRange(LLRender::TRIANGLES, 0, mVertexBuffer->getNumVerts(), mVertexBuffer->getNumIndices(), 0);
}
else
{ //shader wants colors in the vertex stream, use slow path
gGL.begin(LLRender::TRIANGLES);
for (S32 lat_i = 0; lat_i < LATITUDE_SLICES; lat_i++)
{
gGL.vertex3fv(mSpherePoints[lat_i][lon_i].mV);
gGL.vertex3fv(mSpherePoints[lat_i][lon_i+1].mV);
gGL.vertex3fv(mSpherePoints[lat_i+1][lon_i].mV);
for (S32 lon_i = 0; lon_i < LONGITUDE_SLICES; lon_i++)
{
gGL.vertex3fv(mSpherePoints[lat_i][lon_i].mV);
gGL.vertex3fv(mSpherePoints[lat_i][lon_i + 1].mV);
gGL.vertex3fv(mSpherePoints[lat_i + 1][lon_i].mV);

gGL.vertex3fv(mSpherePoints[lat_i+1][lon_i].mV);
gGL.vertex3fv(mSpherePoints[lat_i][lon_i+1].mV);
gGL.vertex3fv(mSpherePoints[lat_i+1][lon_i+1].mV);
gGL.vertex3fv(mSpherePoints[lat_i + 1][lon_i].mV);
gGL.vertex3fv(mSpherePoints[lat_i][lon_i + 1].mV);
gGL.vertex3fv(mSpherePoints[lat_i + 1][lon_i + 1].mV);
}
}
gGL.end();
}
gGL.end();
}
1 change: 1 addition & 0 deletions indra/llrender/llrendersphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class LLRenderSphere

private:
std::vector< std::vector<LLVector3> > mSpherePoints;
LLPointer<LLVertexBuffer> mVertexBuffer;
};

extern LLRenderSphere gSphere;
Expand Down
2 changes: 0 additions & 2 deletions indra/llrender/llvertexbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,8 +1341,6 @@ void LLVertexBuffer::unmapBuffer()

void LLVertexBuffer::_mapBuffer()
{
// must only be called from main thread
llassert(LLCoros::on_main_thread_main_coro());
if (!mMapped)
{
mMapped = true;
Expand Down
79 changes: 72 additions & 7 deletions indra/newview/llface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "llvoavatar.h"
#include "llsculptidsize.h"
#include "llmeshrepository.h"
#include "llskinningutil.h"

#if LL_LINUX
// Work-around spurious used before init warning on Vector4a
Expand Down Expand Up @@ -2184,28 +2185,88 @@ F32 LLFace::getTextureVirtualSize()

bool LLFace::calcPixelArea(F32& cos_angle_to_view_dir, F32& radius)
{
constexpr F32 PIXEL_AREA_UPDATE_PERIOD = 0.1f;
// this is an expensive operation and the result is valid (enough) for several frames
// don't update every frame
if (gFrameTimeSeconds - mLastPixelAreaUpdate < PIXEL_AREA_UPDATE_PERIOD)
{
return true;
}

LL_PROFILE_ZONE_SCOPED_CATEGORY_FACE;

//get area of circle around face

LLVector4a center;
LLVector4a size;


if (isState(LLFace::RIGGED))
{
//override with avatar bounding box
LL_PROFILE_ZONE_NAMED_CATEGORY_FACE("calcPixelArea - rigged");
//override with joint volume face joint bounding boxes
LLVOAvatar* avatar = mVObjp->getAvatar();
bool hasRiggedExtents = false;

if (avatar && avatar->mDrawable)
{
center.load3(avatar->getPositionAgent().mV);
const LLVector4a* exts = avatar->mDrawable->getSpatialExtents();
size.setSub(exts[1], exts[0]);
LLVolume* volume = mVObjp->getVolume();
if (volume)
{
LLVolumeFace& face = volume->getVolumeFace(mTEOffset);

auto& rigInfo = face.mJointRiggingInfoTab;

if (rigInfo.needsUpdate())
{
LLVOVolume* vo_volume = (LLVOVolume*)mVObjp.get();
LLVOAvatar* avatar = mVObjp->getAvatar();
const LLMeshSkinInfo* skin = vo_volume->getSkinInfo();
LLSkinningUtil::updateRiggingInfo(skin, avatar, face);
}

// calculate the world space bounding box of the face by combining the bounding boxes of all the joints
LLVector4a& minp = mRiggedExtents[0];
LLVector4a& maxp = mRiggedExtents[1];
minp = LLVector4a(FLT_MAX, FLT_MAX, FLT_MAX);
maxp = LLVector4a(-FLT_MAX, -FLT_MAX, -FLT_MAX);

for (S32 i = 0; i < rigInfo.size(); i++)
{
auto& jointInfo = rigInfo[i];
if (jointInfo.isRiggedTo())
{
LLJoint* joint = avatar->getJoint(i);

if (joint)
{
LLVector4a jointPos;

LLMatrix4a worldMat;
worldMat.loadu((F32*)&joint->getWorldMatrix().mMatrix[0][0]);

LLVector4a extents[2];

matMulBoundBox(worldMat, jointInfo.getRiggedExtents(), extents);

minp.setMin(minp, extents[0]);
maxp.setMax(maxp, extents[1]);
hasRiggedExtents = true;
}
}
}
}
}
else

if (!hasRiggedExtents)
{
// no rigged extents, zero out bounding box and skip update
mRiggedExtents[0] = mRiggedExtents[1] = LLVector4a(0.f, 0.f, 0.f);

return false;
}

center.setAdd(mRiggedExtents[1], mRiggedExtents[0]);
center.mul(0.5f);
size.setSub(mRiggedExtents[1], mRiggedExtents[0]);
}
else
{
Expand All @@ -2231,6 +2292,10 @@ bool LLFace::calcPixelArea(F32& cos_angle_to_view_dir, F32& radius)
F32 app_angle = atanf((F32) sqrt(size_squared) / dist);
radius = app_angle*LLDrawable::sCurPixelAngle;
mPixelArea = radius*radius * 3.14159f;

// remember last update time, add 10% noise to avoid all faces updating at the same time
mLastPixelAreaUpdate = gFrameTimeSeconds + ll_frand() * PIXEL_AREA_UPDATE_PERIOD * 0.1f;

LLVector4a x_axis;
x_axis.load3(camera->getXAxis().mV);
cos_angle_to_view_dir = lookAt.dot3(x_axis).getF32();
Expand Down
13 changes: 13 additions & 0 deletions indra/newview/llface.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,14 @@ class alignas(16) LLFace
// return true if this face is in an alpha draw pool
bool isInAlphaPool() const;
public: //aligned members

// bounding box of face in drawable space
LLVector4a mExtents[2];

// cached bounding box of rigged face in world space
// calculated on-demand by LLFace::calcPixelArea and may not be up-to-date
LLVector4a mRiggedExtents[2] = { LLVector4a(0,0,0), LLVector4a(0,0,0) };

private:
friend class LLViewerTextureList;
F32 adjustPartialOverlapPixelArea(F32 cos_angle_to_view_dir, F32 radius );
Expand Down Expand Up @@ -301,7 +307,14 @@ class alignas(16) LLFace
S32 mReferenceIndex;
std::vector<S32> mRiggedIndex;

// gFrameTimeSeconds when mPixelArea was last updated
F32 mLastPixelAreaUpdate = 0.f;

// virtual size of face in texture area (mPixelArea adjusted by texture repeats)
// used to determine desired resolution of texture
F32 mVSize;

// pixel area face covers on screen
F32 mPixelArea;

//importance factor, in the range [0, 1.0].
Expand Down
Loading
Loading