Adding gpu timer feature to actually the GPU cost of rendering

This commit is contained in:
samcake 2016-01-15 19:00:18 -08:00
parent fef5e3c8da
commit 483c28dc2d
8 changed files with 88 additions and 16 deletions

View file

@ -100,6 +100,7 @@ void GLBackend::do_getQuery(Batch& batch, size_t paramOffset) {
#else
glGetQueryObjectui64v(glquery->_qo, GL_QUERY_RESULT, &glquery->_result);
#endif
query->triggerReturnHandler(glquery->_result);
(void)CHECK_GL_ERROR();
}
}

View file

@ -10,11 +10,13 @@
//
#include "Query.h"
#include <QDebug>
#include "GPULogging.h"
#include "Batch.h"
using namespace gpu;
Query::Query()
Query::Query(const Handler& returnHandler) :
_returnHandler(returnHandler)
{
}
@ -22,6 +24,45 @@ Query::~Query()
{
}
double Query::getElapsedTime() {
return 0.0;
double Query::getElapsedTime() const {
return ((double) _queryResult) * 0.000001;
}
void Query::triggerReturnHandler(uint64_t queryResult) {
_queryResult = queryResult;
if (_returnHandler) {
_returnHandler(*this);
}
}
Timer::Timer() {
_timerQueries.resize(6, std::make_shared<gpu::Query>([&] (const Query& query) {
auto elapsedTime = query.getElapsedTime();
_movingAverage.addSample(elapsedTime);
static int frameNum = 0;
frameNum++;
frameNum %= 10;
if (frameNum == 0) {
auto value = _movingAverage.average;
qCDebug(gpulogging) << "AO on gpu = " << value;
}
}));
}
void Timer::begin(gpu::Batch& batch) {
batch.beginQuery(_timerQueries[_currentTimerQueryIndex]);
}
void Timer::end(gpu::Batch& batch) {
batch.endQuery(_timerQueries[_currentTimerQueryIndex]);
_currentTimerQueryIndex = (_currentTimerQueryIndex + 1) % _timerQueries.size();
auto returnQuery = _timerQueries[_currentTimerQueryIndex];
batch.getQuery(returnQuery);
}
void Timer::onQueryReturned(const Query& query) {
}

View file

@ -13,26 +13,56 @@
#include <assert.h>
#include <memory>
#include <functional>
#include <vector>
#include <SimpleMovingAverage.h>
#include "Format.h"
namespace gpu {
class Batch;
class Query {
public:
Query();
using Handler = std::function<void(const Query&)>;
Query(const Handler& returnHandler);
~Query();
uint32 queryResult;
double getElapsedTime();
double getElapsedTime() const;
const GPUObjectPointer gpuObject {};
void triggerReturnHandler(uint64_t queryResult);
protected:
Handler _returnHandler;
uint64_t _queryResult = 0;
};
typedef std::shared_ptr<Query> QueryPointer;
typedef std::vector< QueryPointer > Queries;
// gpu timer is just returning an estimate of the time taken by a chunck of work delimited by the
// begin and end calls repeated for several times.
// The result is always a late average of the time spent for that same task a few cycles ago.
class Timer {
public:
Timer();
void begin(gpu::Batch& batch);
void end(gpu::Batch& batch);
double getAverage() const;
protected:
void onQueryReturned(const Query& query);
gpu::Queries _timerQueries;
int _currentTimerQueryIndex = 0;
MovingAverage<double, 5> _movingAverage;
};
};
#endif

View file

@ -19,6 +19,7 @@
#include <SharedUtil.h>
#include <gpu/Context.h>
#include <gpu/StandardShaderLib.h>
#include "RenderUtilsLogging.h"
#include "AmbientOcclusionEffect.h"
#include "TextureCache.h"
@ -240,8 +241,6 @@ void AmbientOcclusionEffect::run(const render::SceneContextPointer& sceneContext
args->_context->getStereoProjections(projMats);
args->_context->getStereoViews(eyeViews);
float halfWidth = 0.5f * sWidth;
for (int i = 0; i < 2; i++) {
// Compose the mono Eye space to Stereo clip space Projection Matrix
auto sideViewMat = projMats[i] * eyeViews[i];
@ -260,6 +259,8 @@ void AmbientOcclusionEffect::run(const render::SceneContextPointer& sceneContext
gpu::doInBatch(args->_context, [=](gpu::Batch& batch) {
batch.enableStereo(false);
_gpuTimer.begin(batch);
batch.setViewportTransform(args->_viewport);
batch.setProjectionTransform(glm::mat4());
batch.setViewTransform(Transform());
@ -302,5 +303,7 @@ void AmbientOcclusionEffect::run(const render::SceneContextPointer& sceneContext
batch.setResourceTexture(AmbientOcclusionEffect_OcclusionMapSlot, occlusionBlurredFBO->getRenderBuffer(0));
batch.draw(gpu::TRIANGLE_STRIP, 4);
_gpuTimer.end(batch);
});
}

View file

@ -16,6 +16,7 @@
#include "render/DrawTask.h"
class AmbientOcclusionEffect {
public:
@ -85,6 +86,8 @@ private:
gpu::PipelinePointer _occlusionPipeline;
gpu::PipelinePointer _hBlurPipeline;
gpu::PipelinePointer _vBlurPipeline;
gpu::Timer _gpuTimer;
};
#endif // hifi_AmbientOcclusionEffect_h

View file

@ -68,7 +68,6 @@ void RenderDeferred::run(const SceneContextPointer& sceneContext, const RenderCo
}
void ToneMappingDeferred::run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) {
PerformanceTimer perfTimer("ToneMappingDeferred");
_toneMappingEffect.render(renderContext->getArgs());
}

View file

@ -156,10 +156,6 @@ public:
int getToneMappingToneCurve() const;
virtual void run(const render::SceneContextPointer& sceneContext, const render::RenderContextPointer& renderContext);
gpu::Queries _timerQueries;
int _currentTimerQueryIndex = 0;
};

View file

@ -32,7 +32,6 @@ vec3 evalEyePositionFromZeye(ivec2 side, float Zeye, vec2 texcoord) {
return vec3(Xe, Ye, Zeye);
}
in vec2 varTexCoord0;
out vec4 outFragColor;
uniform sampler2D normalMap;