From ce86e94e6f7b8c41fe5623d69b68eb1b66ed73ed Mon Sep 17 00:00:00 2001 From: sam gateau Date: Wed, 5 Sep 2018 11:34:33 -0700 Subject: [PATCH] Add simple optim and instrumentation for tthe number of input buffer changes per frame" --- libraries/fbx/src/FBXReader_Mesh.cpp | 25 +++---------------- .../src/gpu/gl/GLBackendInput.cpp | 9 +++++++ .../gpu-gl/src/gpu/gl41/GL41BackendInput.cpp | 5 +++- .../gpu-gl/src/gpu/gl45/GL45BackendInput.cpp | 15 ++++++----- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/libraries/fbx/src/FBXReader_Mesh.cpp b/libraries/fbx/src/FBXReader_Mesh.cpp index ce80dfb592..ef2fcc23b4 100644 --- a/libraries/fbx/src/FBXReader_Mesh.cpp +++ b/libraries/fbx/src/FBXReader_Mesh.cpp @@ -781,25 +781,6 @@ void FBXReader::buildModelMesh(FBXMesh& extractedMesh, const QString& url) { attribChannel = 2; totalAttribBufferSize = totalVertsSize - positionsSize - normalsAndTangentsSize; - } else { -/* - auto posBuffer = std::make_shared(); - posBuffer->setData(positionsSize, (const gpu::Byte*) vertBuffer->getData() + positionsOffset); - vertexBufferStream->addBuffer(posBuffer, 0, positionElement.getSize()); - - // update channels and attribBuffer size accordingly - interleavePositions = false; - - auto tangentBuffer = std::make_shared(); - tangentBuffer->setData(normalsAndTangentsSize, (const gpu::Byte*) vertBuffer->getData() + normalsAndTangentsOffset); - vertexBufferStream->addBuffer(tangentBuffer, 0, normalsAndTangentsStride); - - interleaveNormalsTangents = false; - - tangentChannel = 1; - attribChannel = 2; - - totalAttribBufferSize = totalVertsSize - positionsSize - normalsAndTangentsSize;*/ } // Define the vertex format, compute the offset for each attributes as we append them to the vertex format @@ -813,9 +794,9 @@ void FBXReader::buildModelMesh(FBXMesh& extractedMesh, const QString& url) { } if (normalsSize) { vertexFormat->setAttribute(gpu::Stream::NORMAL, tangentChannel, normalElement, bufOffset); - bufOffset = normalElement.getSize(); + bufOffset += normalElement.getSize(); vertexFormat->setAttribute(gpu::Stream::TANGENT, tangentChannel, normalElement, bufOffset); - bufOffset = normalElement.getSize(); + bufOffset += normalElement.getSize(); if (!interleaveNormalsTangents) { bufOffset = 0; } @@ -895,7 +876,7 @@ void FBXReader::buildModelMesh(FBXMesh& extractedMesh, const QString& url) { vertexBufferStream->addBuffer(attribBuffer, 0, vStride); } - // MEsh vertex format and vertex stream is ready + // Mesh vertex format and vertex stream is ready mesh->setVertexFormatAndStream(vertexFormat, vertexBufferStream); // Index and Part Buffers diff --git a/libraries/gpu-gl-common/src/gpu/gl/GLBackendInput.cpp b/libraries/gpu-gl-common/src/gpu/gl/GLBackendInput.cpp index 6ce25bc56c..21553dc2c7 100644 --- a/libraries/gpu-gl-common/src/gpu/gl/GLBackendInput.cpp +++ b/libraries/gpu-gl-common/src/gpu/gl/GLBackendInput.cpp @@ -268,9 +268,18 @@ void GLBackend::updateInput() { auto offset = _input._bufferOffsets.data(); auto stride = _input._bufferStrides.data(); + // Profile the count of buffers to update and use it to short cut the for loop + int numInvalids = _input._invalidBuffers.count(); + _stats._ISNumInputBufferChanges += numInvalids; + PROFILE_COUNTER_IF_CHANGED(render_gpu, "numInputBuffersbound", int, numInvalids); + for (GLuint buffer = 0; buffer < _input._buffers.size(); buffer++, vbo++, offset++, stride++) { if (_input._invalidBuffers.test(buffer)) { glBindVertexBuffer(buffer, (*vbo), (*offset), (GLsizei)(*stride)); + numInvalids--; + if (numInvalids <= 0) { + break; + } } } diff --git a/libraries/gpu-gl/src/gpu/gl41/GL41BackendInput.cpp b/libraries/gpu-gl/src/gpu/gl41/GL41BackendInput.cpp index 9dcb08f0b7..30e393630a 100644 --- a/libraries/gpu-gl/src/gpu/gl41/GL41BackendInput.cpp +++ b/libraries/gpu-gl/src/gpu/gl41/GL41BackendInput.cpp @@ -78,7 +78,10 @@ void GL41Backend::updateInput() { const Stream::Format::AttributeMap& attributes = _input._format->getAttributes(); auto& inputChannels = _input._format->getChannels(); - _stats._ISNumInputBufferChanges++; + _stats._ISNumInputBufferChanges += _input._invalidBuffers.count(); + + // Profile the count of buffers to update + PROFILE_COUNTER_IF_CHANGED(render_gpu, "numInputBuffersbound", int, _input._invalidBuffers.count()); GLuint boundVBO = 0; for (auto& channelIt : inputChannels) { diff --git a/libraries/gpu-gl/src/gpu/gl45/GL45BackendInput.cpp b/libraries/gpu-gl/src/gpu/gl45/GL45BackendInput.cpp index c914b9f84d..de4989eb32 100644 --- a/libraries/gpu-gl/src/gpu/gl45/GL45BackendInput.cpp +++ b/libraries/gpu-gl/src/gpu/gl45/GL45BackendInput.cpp @@ -27,8 +27,6 @@ void GL45Backend::resetInputStage() { } void GL45Backend::updateInput() { - // PROFILE_RANGE(render_gpu, __FUNCTION__); - bool isStereoNow = isStereo(); // track stereo state change potentially happening wihtout changing the input format // this is a rare case requesting to invalid the format @@ -131,21 +129,26 @@ void GL45Backend::updateInput() { } if (_input._invalidBuffers.any()) { - // PROFILE_RANGE(render_gpu, "bindInputBuffers"); auto vbo = _input._bufferVBOs.data(); auto offset = _input._bufferOffsets.data(); auto stride = _input._bufferStrides.data(); - int numSet = 0; + // Profile the count of buffers to update and use it to short cut the for loop + int numInvalids = _input._invalidBuffers.count(); + _stats._ISNumInputBufferChanges += numInvalids; + PROFILE_COUNTER_IF_CHANGED(render_gpu, "numInputBuffersbound", int, numInvalids); + auto numBuffers = _input._buffers.size(); for (GLuint buffer = 0; buffer < numBuffers; buffer++, vbo++, offset++, stride++) { if (_input._invalidBuffers.test(buffer)) { glBindVertexBuffer(buffer, (*vbo), (*offset), (GLsizei)(*stride)); - numSet++; + numInvalids--; + if (numInvalids <= 0) { + break; + } } } - PROFILE_COUNTER_IF_CHANGED(render_gpu, "numVBSbound", int, numSet); _input._invalidBuffers.reset(); (void)CHECK_GL_ERROR();