diff options
Diffstat (limited to 'src/render')
-rw-r--r-- | src/render/backend/resourceaccessor.cpp | 2 | ||||
-rw-r--r-- | src/render/jobs/job_common_p.h | 3 | ||||
-rw-r--r-- | src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp | 2 | ||||
-rw-r--r-- | src/render/renderers/opengl/renderer/renderer.cpp | 40 | ||||
-rw-r--r-- | src/render/renderers/opengl/renderer/renderer_p.h | 5 | ||||
-rw-r--r-- | src/render/renderers/opengl/textures/gltexture.cpp | 33 | ||||
-rw-r--r-- | src/render/renderers/opengl/textures/gltexture_p.h | 8 | ||||
-rw-r--r-- | src/render/texture/texture.cpp | 87 | ||||
-rw-r--r-- | src/render/texture/texture_p.h | 6 |
9 files changed, 115 insertions, 71 deletions
diff --git a/src/render/backend/resourceaccessor.cpp b/src/render/backend/resourceaccessor.cpp index 8ca11e7a7..e69907f3f 100644 --- a/src/render/backend/resourceaccessor.cpp +++ b/src/render/backend/resourceaccessor.cpp @@ -87,7 +87,7 @@ bool ResourceAccessor::accessResource(ResourceType type, Qt3DCore::QNodeId nodeI glTex->setExternalRenderingEnabled(true); QOpenGLTexture **glTextureHandle = reinterpret_cast<QOpenGLTexture **>(handle); - *glTextureHandle = glTex->getOrCreateGLTexture(); + *glTextureHandle = glTex->getGLTexture(); *lock = glTex->externalRenderingLock(); return true; } diff --git a/src/render/jobs/job_common_p.h b/src/render/jobs/job_common_p.h index 03e2cc90e..2ae7d81ff 100644 --- a/src/render/jobs/job_common_p.h +++ b/src/render/jobs/job_common_p.h @@ -106,7 +106,8 @@ namespace JobTypes { ProximityFiltering, SyncFilterEntityByLayer, SyncMaterialGatherer, - UpdateLayerEntity + UpdateLayerEntity, + SendTextureChangesToFrontend }; } // JobTypes diff --git a/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp b/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp index cdc895f4d..6e1cd5d47 100644 --- a/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp +++ b/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp @@ -863,7 +863,7 @@ void SubmissionContext::bindFrameBufferAttachmentHelper(GLuint fboId, const Atta for (const Attachment &attachment : attachments_) { GLTexture *rTex = glTextureManager->lookupResource(attachment.m_textureUuid); if (!m_glHelper->frameBufferNeedsRenderBuffer(attachment)) { - QOpenGLTexture *glTex = rTex ? rTex->getOrCreateGLTexture() : nullptr; + QOpenGLTexture *glTex = rTex ? rTex->getGLTexture() : nullptr; if (glTex != nullptr) { // The texture can not be rendered simultaniously by another renderer Q_ASSERT(!rTex->isExternalRenderingEnabled()); diff --git a/src/render/renderers/opengl/renderer/renderer.cpp b/src/render/renderers/opengl/renderer/renderer.cpp index a970a0479..fd4e7bed4 100644 --- a/src/render/renderers/opengl/renderer/renderer.cpp +++ b/src/render/renderers/opengl/renderer/renderer.cpp @@ -194,6 +194,7 @@ Renderer::Renderer(QRenderAspect::RenderType type) , m_bufferGathererJob(Render::GenericLambdaJobPtr<std::function<void ()>>::create([this] { lookForDirtyBuffers(); }, JobTypes::DirtyBufferGathering)) , m_vaoGathererJob(Render::GenericLambdaJobPtr<std::function<void ()>>::create([this] { lookForAbandonedVaos(); }, JobTypes::DirtyVaoGathering)) , m_textureGathererJob(Render::GenericLambdaJobPtr<std::function<void ()>>::create([this] { lookForDirtyTextures(); }, JobTypes::DirtyTextureGathering)) + , m_sendTextureChangesToFrontendJob(Render::GenericLambdaJobPtr<std::function<void ()>>::create([this] { sendTextureChangesToFrontend(); }, JobTypes::SendTextureChangesToFrontend)) , m_introspectShaderJob(Render::GenericLambdaJobPtr<std::function<void ()>>::create([this] { reloadDirtyShaders(); }, JobTypes::DirtyShaderGathering)) , m_syncTextureLoadingJob(Render::GenericLambdaJobPtr<std::function<void ()>>::create([] {}, JobTypes::SyncTextureLoading)) , m_ownedContext(false) @@ -1170,6 +1171,28 @@ void Renderer::reloadDirtyShaders() } } +// Executed in a job +void Renderer::sendTextureChangesToFrontend() +{ + const QVector<QPair<TextureProperties, Qt3DCore::QNodeIdVector>> updateTextureProperties = std::move(m_updatedTextureProperties); + for (const auto &pair : updateTextureProperties) { + // Prepare change notification + + const Qt3DCore::QNodeIdVector targetIds = pair.second; + for (const Qt3DCore::QNodeId targetId: targetIds) { + // Lookup texture + Texture *t = m_nodesManager->textureManager()->lookupResource(targetId); + + // Texture might have been deleted between previous and current frame + if (t == nullptr) + continue; + + // Send change and update backend + t->updatePropertiesAndNotify(pair.first); + } + } +} + // Render Thread (or QtQuick RenderThread when using Scene3D) // Scene3D: When using Scene3D rendering, we can't assume that when // updateGLResources is called, the resource handles points to still existing @@ -1230,7 +1253,7 @@ void Renderer::updateGLResources() if (texture == nullptr) continue; - // Update texture properties + // Create or Update GLTexture updateTexture(texture); } // We want to upload textures data at this point as the SubmissionThread and @@ -1240,8 +1263,16 @@ void Renderer::updateGLResources() GLTextureManager *glTextureManager = m_nodesManager->glTextureManager(); const QVector<GLTexture *> glTextures = glTextureManager->activeResources(); // Upload texture data - for (GLTexture *glTexture : glTextures) - glTexture->getOrCreateGLTexture(); + for (GLTexture *glTexture : glTextures) { + const GLTexture::TextureUpdateInfo info = glTexture->createOrUpdateGLTexture(); + + // GLTexture creation provides us width/height/format ... information + // for textures which had not initially specified these information (TargetAutomatic...) + // Gather these information and store them to be distributed by a change next frame + const QNodeIdVector referenceTextureIds = glTextureManager->referencedTextureIds(glTexture); + // Store properties and referenceTextureIds + m_updatedTextureProperties.push_back({info.properties, referenceTextureIds}); + } } } // When Textures are cleaned up, their id is saved so that they can be @@ -1664,6 +1695,9 @@ QVector<Qt3DCore::QAspectJobPtr> Renderer::renderBinJobs() renderBinJobs.push_back(m_sendRenderCaptureJob); } + // Do we need to notify any texture about property changes? + if (m_updatedTextureProperties.size() > 0) + renderBinJobs.push_back(m_sendTextureChangesToFrontendJob); renderBinJobs.push_back(m_sendBufferCaptureJob); renderBinJobs.append(bufferJobs); diff --git a/src/render/renderers/opengl/renderer/renderer_p.h b/src/render/renderers/opengl/renderer/renderer_p.h index b501fe6a0..23708e3e8 100644 --- a/src/render/renderers/opengl/renderer/renderer_p.h +++ b/src/render/renderers/opengl/renderer/renderer_p.h @@ -79,6 +79,7 @@ #include <Qt3DRender/private/updateskinningpalettejob_p.h> #include <Qt3DRender/private/updateentitylayersjob_p.h> #include <Qt3DRender/private/renderercache_p.h> +#include <Qt3DRender/private/texture_p.h> #include <QHash> #include <QMatrix4x4> @@ -216,6 +217,7 @@ public: inline IntrospectShadersJobPtr introspectShadersJob() const { return m_introspectShaderJob; } inline Qt3DCore::QAspectJobPtr bufferGathererJob() const { return m_bufferGathererJob; } inline Qt3DCore::QAspectJobPtr textureGathererJob() const { return m_textureGathererJob; } + inline Qt3DCore::QAspectJobPtr sendTextureChangesToFrontendJob() const { return m_sendTextureChangesToFrontendJob; } inline UpdateEntityLayersJobPtr updateEntityLayersJob() const { return m_updateEntityLayersJob; } Qt3DCore::QAbstractFrameAdvanceService *frameAdvanceService() const override; @@ -370,6 +372,7 @@ private: GenericLambdaJobPtr<std::function<void ()>> m_bufferGathererJob; GenericLambdaJobPtr<std::function<void ()>> m_vaoGathererJob; GenericLambdaJobPtr<std::function<void ()>> m_textureGathererJob; + GenericLambdaJobPtr<std::function<void ()>> m_sendTextureChangesToFrontendJob; IntrospectShadersJobPtr m_introspectShaderJob; SynchronizerJobPtr m_syncTextureLoadingJob; @@ -379,6 +382,7 @@ private: void lookForDownloadableBuffers(); void lookForDirtyTextures(); void reloadDirtyShaders(); + void sendTextureChangesToFrontend(); QMutex m_abandonedVaosMutex; QVector<HVao> m_abandonedVaos; @@ -387,6 +391,7 @@ private: QVector<HBuffer> m_downloadableBuffers; QVector<HShader> m_dirtyShaders; QVector<HTexture> m_dirtyTextures; + QVector<QPair<TextureProperties, Qt3DCore::QNodeIdVector>> m_updatedTextureProperties; bool m_ownedContext; diff --git a/src/render/renderers/opengl/textures/gltexture.cpp b/src/render/renderers/opengl/textures/gltexture.cpp index 3d8664e78..bb7e06e60 100644 --- a/src/render/renderers/opengl/textures/gltexture.cpp +++ b/src/render/renderers/opengl/textures/gltexture.cpp @@ -105,10 +105,13 @@ void GLTexture::destroyGLTexture() destroyResources(); } -QOpenGLTexture* GLTexture::getOrCreateGLTexture() +GLTexture::TextureUpdateInfo GLTexture::createOrUpdateGLTexture() { QMutexLocker locker(&m_textureMutex); bool needUpload = false; + TextureUpdateInfo textureInfo; + + m_properties.status = QAbstractTexture::Error; // on the first invocation in the render thread, make sure to // evaluate the texture data generator output @@ -140,7 +143,8 @@ QOpenGLTexture* GLTexture::getOrCreateGLTexture() needUpload = true; } else { qWarning() << "[Qt3DRender::GLTexture] No QTextureData generated from Texture Generator yet. Texture will be invalid for this frame"; - return nullptr; + textureInfo.properties.status = QAbstractTexture::Loading; + return textureInfo; } } @@ -188,8 +192,10 @@ QOpenGLTexture* GLTexture::getOrCreateGLTexture() } // don't try to create the texture if the format was not set - if (m_properties.format == QAbstractTexture::Automatic) - return nullptr; + if (m_properties.format == QAbstractTexture::Automatic) { + textureInfo.properties.status = QAbstractTexture::Error; + return textureInfo; + } // if the properties changed, we need to re-allocate the texture if (testDirtyFlag(Properties)) { @@ -197,15 +203,24 @@ QOpenGLTexture* GLTexture::getOrCreateGLTexture() m_gl = nullptr; } + if (!m_gl) { m_gl = buildGLTexture(); - if (!m_gl) - return nullptr; + if (!m_gl) { + textureInfo.properties.status = QAbstractTexture::Error; + return textureInfo; + } + m_gl->allocateStorage(); if (!m_gl->isStorageAllocated()) { - return nullptr; + textureInfo.properties.status = QAbstractTexture::Error; + return textureInfo; } } + m_properties.status = QAbstractTexture::Ready; + + textureInfo.properties = m_properties; + textureInfo.texture = m_gl; // need to (re-)upload texture data? if (needUpload) { @@ -223,7 +238,7 @@ QOpenGLTexture* GLTexture::getOrCreateGLTexture() setDirtyFlag(Properties, false); setDirtyFlag(Parameters, false); - return m_gl; + return textureInfo; } RenderBuffer *GLTexture::getOrCreateRenderBuffer() @@ -442,7 +457,7 @@ void GLTexture::uploadGLTextureData() } // Upload all QTexImageData references by the TextureImages - for (int i = 0; i < m_images.size(); i++) { + for (int i = 0; i < std::min(m_images.size(), m_imageData.size()); i++) { const QTextureImageDataPtr &imgData = m_imageData.at(i); // Here the bytes in the QTextureImageData contain data for a single // layer, face or mip level, unlike the QTextureGenerator case where diff --git a/src/render/renderers/opengl/textures/gltexture_p.h b/src/render/renderers/opengl/textures/gltexture_p.h index 5f4a20d30..e26fdf397 100644 --- a/src/render/renderers/opengl/textures/gltexture_p.h +++ b/src/render/renderers/opengl/textures/gltexture_p.h @@ -140,7 +140,13 @@ public: * If the texture properties or parameters have changed, these changes * will be applied to the resulting OpenGL texture. */ - QOpenGLTexture* getOrCreateGLTexture(); + struct TextureUpdateInfo + { + QOpenGLTexture *texture = nullptr; + TextureProperties properties; + }; + + TextureUpdateInfo createOrUpdateGLTexture(); /** * @brief diff --git a/src/render/texture/texture.cpp b/src/render/texture/texture.cpp index 440a08e26..d8008b60c 100644 --- a/src/render/texture/texture.cpp +++ b/src/render/texture/texture.cpp @@ -117,24 +117,8 @@ void Texture::cleanup() m_textureImageIds.clear(); // set default values - m_properties.width = 1; - m_properties.height = 1; - m_properties.depth = 1; - m_properties.layers = 1; - m_properties.mipLevels = 1; - m_properties.samples = 1; - m_properties.generateMipMaps = false; - m_properties.format = QAbstractTexture::RGBA8_UNorm; - m_properties.target = QAbstractTexture::Target2D; - - m_parameters.magnificationFilter = QAbstractTexture::Nearest; - m_parameters.minificationFilter = QAbstractTexture::Nearest; - m_parameters.wrapModeX = QTextureWrapMode::ClampToEdge; - m_parameters.wrapModeY = QTextureWrapMode::ClampToEdge; - m_parameters.wrapModeZ = QTextureWrapMode::ClampToEdge; - m_parameters.maximumAnisotropy = 1.0f; - m_parameters.comparisonFunction = QAbstractTexture::CompareLessEqual; - m_parameters.comparisonMode = QAbstractTexture::CompareNone; + m_properties = {}; + m_parameters = {}; m_dirty = NotDirty; } @@ -229,80 +213,79 @@ void Texture::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) BackendNode::sceneChangeEvent(e); } -void Texture::notifyStatus(QAbstractTexture::Status status) +// Called by sceneChangeEvent or TextureDownloadRequest (both in AspectThread context) +void Texture::setDataGenerator(const QTextureGeneratorPtr &generator) { - auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId()); - change->setDeliveryFlags(Qt3DCore::QSceneChange::Nodes); - change->setPropertyName("status"); - change->setValue(status); - notifyObservers(change); + m_dataFunctor = generator; + addDirtyFlag(DirtyDataGenerator); } -void Texture::updateFromData(QTextureDataPtr data) +// Called by sendTextureChangesToFrontendJob once GLTexture and sharing +// has been performed +void Texture::updatePropertiesAndNotify(const TextureProperties &properties) { - if (data->width() != m_properties.width) { - m_properties.width = data->width(); + // If we are Dirty, some property has changed and the properties we have + // received are potentially already outdated + if (m_dirty != NotDirty) + return; + + // Note we don't update target has it is constant for frontend nodes + + if (properties.width != m_properties.width) { + m_properties.width = properties.width; auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId()); change->setDeliveryFlags(Qt3DCore::QSceneChange::Nodes); change->setPropertyName("width"); - change->setValue(data->width()); + change->setValue(properties.width); notifyObservers(change); } - if (data->height() != m_properties.height) { - m_properties.height = data->height(); + if (properties.height != m_properties.height) { + m_properties.height = properties.height; auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId()); change->setDeliveryFlags(Qt3DCore::QSceneChange::Nodes); change->setPropertyName("height"); - change->setValue(data->height()); + change->setValue(properties.height); notifyObservers(change); } - if (data->depth() != m_properties.depth) { - m_properties.depth = data->depth(); + if (properties.depth != m_properties.depth) { + m_properties.depth = properties.depth; auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId()); change->setDeliveryFlags(Qt3DCore::QSceneChange::Nodes); change->setPropertyName("depth"); - change->setValue(data->depth()); + change->setValue(properties.depth); notifyObservers(change); } - if (data->layers() != m_properties.layers) { - m_properties.layers = data->layers(); + if (properties.layers != m_properties.layers) { + m_properties.layers = properties.layers; auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId()); change->setDeliveryFlags(Qt3DCore::QSceneChange::Nodes); change->setPropertyName("layers"); - change->setValue(data->layers()); + change->setValue(properties.layers); notifyObservers(change); } - if (data->format() != m_properties.format) { - m_properties.format = data->format(); + if (properties.format != m_properties.format) { + m_properties.format = properties.format; auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId()); change->setDeliveryFlags(Qt3DCore::QSceneChange::Nodes); change->setPropertyName("format"); - change->setValue(data->format()); + change->setValue(properties.format); notifyObservers(change); } - if (data->target() != m_properties.target) { - // TODO frontend property is actually constant - m_properties.target = data->target(); + if (properties.status != m_properties.status) { + m_properties.status = properties.status; auto change = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId()); change->setDeliveryFlags(Qt3DCore::QSceneChange::Nodes); - change->setPropertyName("target"); - change->setValue(data->target()); + change->setPropertyName("status"); + change->setValue(properties.status); notifyObservers(change); } } -// Called by sceneChangeEvent or TextureDownloadRequest (both in AspectThread context) -void Texture::setDataGenerator(const QTextureGeneratorPtr &generator) -{ - m_dataFunctor = generator; - addDirtyFlag(DirtyDataGenerator); -} - bool Texture::isValid(TextureImageManager *manager) const { for (const QNodeId id : m_textureImageIds) { diff --git a/src/render/texture/texture_p.h b/src/render/texture/texture_p.h index b9ef0385f..9e385cefe 100644 --- a/src/render/texture/texture_p.h +++ b/src/render/texture/texture_p.h @@ -86,12 +86,13 @@ struct TextureProperties QAbstractTexture::Target target = QAbstractTexture::Target2D; QAbstractTexture::TextureFormat format = QAbstractTexture::RGBA8_UNorm; bool generateMipMaps = false; + QAbstractTexture::Status status = QAbstractTexture::None; bool operator==(const TextureProperties &o) const { return (width == o.width) && (height == o.height) && (depth == o.depth) && (layers == o.layers) && (mipLevels == o.mipLevels) && (target == o.target) && (format == o.format) && (generateMipMaps == o.generateMipMaps) - && (samples == o.samples); + && (samples == o.samples) && (status == o.status); } inline bool operator!=(const TextureProperties &o) const { return !(*this == o); } }; @@ -155,9 +156,8 @@ public: inline const Qt3DCore::QNodeIdVector textureImageIds() const { return m_textureImageIds; } inline const QTextureGeneratorPtr& dataGenerator() const { return m_dataFunctor; } - void notifyStatus(QAbstractTexture::Status status); - void updateFromData(QTextureDataPtr data); void setDataGenerator(const QTextureGeneratorPtr &generator); + void updatePropertiesAndNotify(const TextureProperties &propreties); bool isValid(TextureImageManager *manager) const; private: void initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) final; |