let's start reusing the Batch per model from frame to frame

This commit is contained in:
Sam Gateau 2014-11-10 10:04:19 -08:00
parent f3fe934fe2
commit 631ed9da78
5 changed files with 106 additions and 60 deletions

View file

@ -108,7 +108,8 @@ static unsigned STARFIELD_SEED = 1;
static const int BANDWIDTH_METER_CLICK_MAX_DRAG_LENGTH = 6; // farther dragged clicks are ignored
const unsigned MAXIMUM_CACHE_SIZE = 10737418240; // 10GB
//const unsigned MAXIMUM_CACHE_SIZE = 10737418240; // 10GB
const quint64 MAXIMUM_CACHE_SIZE = 10737418240L; // 10GB
static QTimer* idleTimer = NULL;
@ -186,7 +187,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) :
_lastNackTime(usecTimestampNow()),
_lastSendDownstreamAudioStats(usecTimestampNow()),
_isVSyncOn(true),
_aboutToQuit(false)
_aboutToQuit(false),
_viewTransform(new gpu::Transform())
{
// read the ApplicationInfo.ini file for Name/Version/Domain information
@ -3115,6 +3117,8 @@ void Application::displaySide(Camera& whichCamera, bool selfAvatarOnly) {
void Application::updateUntranslatedViewMatrix(const glm::vec3& viewMatrixTranslation) {
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&_untranslatedViewMatrix);
_viewMatrixTranslation = viewMatrixTranslation;
_viewTransform->evalFromRawMatrix(_untranslatedViewMatrix);
_viewTransform->postTranslate(viewMatrixTranslation);
}
void Application::loadTranslatedViewMatrix(const glm::vec3& translation) {

View file

@ -233,6 +233,8 @@ public:
const glm::vec3& getViewMatrixTranslation() const { return _viewMatrixTranslation; }
void setViewMatrixTranslation(const glm::vec3& translation) { _viewMatrixTranslation = translation; }
const gpu::TransformPointer& getViewTransform() const { return _viewTransform; }
/// if you need to access the application settings, use lockSettings()/unlockSettings()
QSettings* lockSettings() { _settingsMutex.lock(); return _settings; }
void unlockSettings() { _settingsMutex.unlock(); }
@ -526,6 +528,7 @@ private:
QRect _mirrorViewRect;
RearMirrorTools* _rearMirrorTools;
gpu::TransformPointer _viewTransform;
glm::mat4 _untranslatedViewMatrix;
glm::vec3 _viewMatrixTranslation;
glm::mat4 _projectionMatrix;

View file

@ -11,7 +11,6 @@
#include "Transform.h"
#include <glm/gtx/quaternion.hpp>
using namespace gpu;
@ -26,32 +25,6 @@ Transform::Transform(const Mat4& raw) {
evalFromRawMatrix(raw);
}
void Transform::updateCache() const {
if (isCacheInvalid()) {
glm::mat3x3 rot = glm::mat3_cast(_rotation);
if ((_scale.x != 1.f) || (_scale.y != 1.f) || (_scale.z != 1.f)) {
rot[0] *= _scale.x;
rot[1] *= _scale.y;
rot[2] *= _scale.z;
}
_matrix[0] = Vec4(rot[0], 0.f);
_matrix[1] = Vec4(rot[1], 0.f);
_matrix[2] = Vec4(rot[2], 0.f);
_matrix[3] = Vec4(_translation, 1.f);
validCache();
}
}
void Transform::postTranslate(const Vec3& translation) {
invalidCache();
Vec3 tt = glm::rotate(_rotation, translation * _scale);
_translation += tt;
}
Transform::Mat4& Transform::evalRelativeTransform( Mat4& result, const Vec3& origin) {
updateCache();
result = _matrix;
@ -97,16 +70,5 @@ Transform& Transform::evalInverseTranspose(Transform& result) {
return result;
}
Transform& Transform::mult( Transform& result, const Transform& left, const Transform& right) {
right.updateCache();
left.updateCache();
result.setTranslation(Vec3(left.getMatrix() * Vec4(right.getTranslation(), 1.f)));
Mat4 mat = left.getMatrix() * right.getMatrix();
result.evalRotationScale(Mat3(mat));
return result;
}

View file

@ -22,6 +22,7 @@
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/quaternion.hpp>
#include <bitset>
@ -37,23 +38,68 @@ public:
typedef glm::quat Quat;
Transform();
Transform(const Transform& transform) :
_translation(transform._translation),
_rotation(transform._rotation),
_scale(transform._scale),
_flags(transform._flags)
{
invalidCache();
}
Transform(const Mat4& raw);
~Transform() {}
void setTranslation(const Vec3& translation) { invalidCache(); flagTranslation(); _translation = translation; }
const Vec3& getTranslation() const { return _translation; }
void preTranslate(const Vec3& translation) { invalidCache(); _translation += translation; }
void postTranslate( Vec3 const & translation);
void preTranslate(const Vec3& translation) { invalidCache(); flagTranslation(); _translation += translation; }
void postTranslate(const Vec3& translation) {
invalidCache();
flagTranslation();
if (isRotating()) {
_translation += glm::rotate(_rotation, translation * _scale);
} else {
_translation += translation * _scale;
}
}
void setRotation(const Quat& rotation) { invalidCache(); flagRotation(); _rotation = glm::normalize(rotation); }
void setRotation(const Quat& rotation) { invalidCache(); flagRotation(); _rotation = rotation; }
const Quat& getRotation() const { return _rotation; }
void setNoScale() { invalidCache(); flagNoScaling(); _scale = Vec3(1.f); }
void setScale(float scale) { invalidCache(); flagUniformScaling(); _scale = Vec3(scale); }
void setScale(const Vec3& scale) { invalidCache(); flagNonUniformScaling(); _scale = scale; }
void preRotate(const Quat& rotation) {
invalidCache();
if (isRotating()) {
_rotation = rotation * _rotation;
} else {
_rotation = rotation;
}
flagRotation();
_translation = glm::rotate(rotation, _translation);
}
void postRotate(const Quat& rotation) {
invalidCache();
if (isRotating()) {
_rotation *= rotation;
} else {
_rotation = rotation;
}
flagRotation();
}
void setScale(float scale) { invalidCache(); flagScaling(); _scale = Vec3(scale); }
void setScale(const Vec3& scale) { invalidCache(); flagScaling(); _scale = scale; }
const Vec3& getScale() const { return _scale; }
void postScale(const Vec3& scale) {
invalidCache();
if (isScaling()) {
_scale *= scale;
} else {
_scale = scale;
}
flagScaling();
}
const Mat4& getMatrix() const { updateCache(); return _matrix; }
Mat4& evalRelativeTransform(Mat4& result, const Vec3& origin);
@ -62,11 +108,17 @@ public:
void evalFromRawMatrix(const Mat4& matrix);
void evalRotationScale(const Mat3& rotationScalematrix);
static Transform& mult( Transform& result, const Transform& left, const Transform& right);
static Transform& mult( Transform& result, const Transform& left, const Transform& right) {
result = left;
if ( right.isTranslating()) result.postTranslate(right.getTranslation());
if ( right.isRotating()) result.postRotate(right.getRotation());
if (right.isScaling()) result.postScale(right.getScale());
return result;
}
bool isScaling() const { return _flags[FLAG_UNIFORM_SCALING] || _flags[FLAG_NON_UNIFORM_SCALING]; }
bool isUniform() const { return !isNonUniform(); }
bool isNonUniform() const { return _flags[FLAG_NON_UNIFORM_SCALING]; }
bool isTranslating() const { return _flags[FLAG_TRANSLATION]; }
bool isRotating() const { return _flags[FLAG_ROTATION]; }
bool isScaling() const { return _flags[FLAG_SCALING]; }
protected:
@ -75,9 +127,7 @@ protected:
FLAG_TRANSLATION,
FLAG_ROTATION,
FLAG_UNIFORM_SCALING,
FLAG_NON_UNIFORM_SCALING,
FLAG_SHEARING,
FLAG_SCALING,
FLAG_PROJECTION,
@ -103,11 +153,32 @@ protected:
void flagTranslation() { _flags.set(FLAG_TRANSLATION, true); }
void flagRotation() { _flags.set(FLAG_ROTATION, true); }
void flagNoScaling() { _flags.set(FLAG_UNIFORM_SCALING, false); _flags.set(FLAG_NON_UNIFORM_SCALING, false); }
void flagUniformScaling() { _flags.set(FLAG_UNIFORM_SCALING, true); _flags.set(FLAG_NON_UNIFORM_SCALING, false); }
void flagNonUniformScaling() { _flags.set(FLAG_UNIFORM_SCALING, false); _flags.set(FLAG_NON_UNIFORM_SCALING, true); }
void flagScaling() { _flags.set(FLAG_SCALING, true); }
void updateCache() const;
void updateCache() const {
if (isCacheInvalid()) {
if (isRotating()) {
glm::mat3x3 rot = glm::mat3_cast(_rotation);
if ((_scale.x != 1.f) || (_scale.y != 1.f) || (_scale.z != 1.f)) {
rot[0] *= _scale.x;
rot[1] *= _scale.y;
rot[2] *= _scale.z;
}
_matrix[0] = Vec4(rot[0], 0.f);
_matrix[1] = Vec4(rot[1], 0.f);
_matrix[2] = Vec4(rot[2], 0.f);
} else {
_matrix[0] = Vec4(_scale.x, 0.f, 0.f, 0.f);
_matrix[1] = Vec4(0.f, _scale.y, 0.f, 0.f);
_matrix[2] = Vec4(0.f, 0.f, _scale.z, 0.f);
}
_matrix[3] = Vec4(_translation, 1.f);
validCache();
}
}
};
typedef QSharedPointer< Transform > TransformPointer;

View file

@ -563,7 +563,13 @@ bool Model::render(float alpha, RenderMode mode, RenderArgs* args) {
_transforms.push_back(gpu::TransformPointer(new gpu::Transform()));
}
_transforms[0]->evalFromRawMatrix(Application::getInstance()->getUntranslatedViewMatrix());
_transforms[0]->postTranslate(Application::getInstance()->getViewMatrixTranslation() + _translation);
gpu::TransformPointer currentView(Application::getInstance()->getViewTransform());
_transforms[0]->setTranslation(currentView->getTranslation());
_transforms[0]->setRotation(currentView->getRotation());
_transforms[0]->setScale(currentView->getScale());
_transforms[0]->postTranslate(_translation);
batch.setViewTransform(_transforms[0]);
@ -1862,7 +1868,7 @@ int Model::renderMeshes(gpu::Batch& batch, RenderMode mode, bool translucent, fl
}
}
// GLBATCH(glPushMatrix)();
GLBATCH(glPushMatrix)();
// Application::getInstance()->loadTranslatedViewMatrix(_translation);
// GLBATCH(glLoadMatrixf)((const GLfloat*)&Application::getInstance()->getUntranslatedViewMatrix());
@ -1995,7 +2001,7 @@ int Model::renderMeshes(gpu::Batch& batch, RenderMode mode, bool translucent, fl
GLBATCH(glActiveTexture)(GL_TEXTURE0);
}
// GLBATCH(glPopMatrix)();
GLBATCH(glPopMatrix)();
}