diff --git a/libraries/render/src/render/Task.cpp b/libraries/render/src/render/Task.cpp index 434674e198..21cb1e5be1 100644 --- a/libraries/render/src/render/Task.cpp +++ b/libraries/render/src/render/Task.cpp @@ -1,16 +1,15 @@ // -// Task.h +// Task.cpp // render/src/render // -// Created by Zach Pomerantz on 1/6/2015. -// Copyright 2015 High Fidelity, Inc. +// Created by Zach Pomerantz on 1/21/2016. +// Copyright 2016 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#ifndef hifi_render_Task_h -#define hifi_render_Task_h +#include "Task.h" #include // QObject @@ -19,302 +18,8 @@ #include "gpu/Batch.h" #include -// Prepare Qt for auto configurations -Q_DECLARE_METATYPE(std::shared_ptr) +using namespace render; -namespace render { - -// A varying piece of data, to be used as Job/Task I/O -// TODO: Task IO -class Varying { -public: - Varying() {} - Varying(const Varying& var) : _concept(var._concept) {} - template Varying(const T& data) : _concept(std::make_shared>(data)) {} - - template T& edit() { return std::static_pointer_cast>(_concept)->_data; } - template const T& get() { return std::static_pointer_cast>(_concept)->_data; } - -protected: - class Concept { - public: - virtual ~Concept() = default; - }; - template class Model : public Concept { - public: - using Data = T; - - Model(const Data& data) : _data(data) {} - virtual ~Model() = default; - - Data _data; - }; - - std::shared_ptr _concept; -}; - -class JobConfig : public QObject { - Q_OBJECT -}; - -class DefaultJobConfig: public JobConfig { - Q_OBJECT -}; - -template void jobConfigure(T& model, const C& configuration) { - model.configure(configuration); +void TaskConfig::refresh() { + _task.configure(*this); } -template void jobConfigure(T&, const DefaultJobConfig&) { -} - -// FIXME: In c++17, use default classes of nullptr_t to combine these -template void jobRun(T& model, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { - model.run(sceneContext, renderContext); -} -template void jobRunI(T& model, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const I& input) { - model.run(sceneContext, renderContext, input); -} -template void jobRunO(T& model, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, O& output) { - model.run(sceneContext, renderContext, output); -} -template void jobRunIO(T& model, const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const I& input, O& output) { - model.run(sceneContext, renderContext, input, output); -} - -class Job { -public: - using Config = JobConfig; - using QConfig = std::shared_ptr; - - // The guts of a job - class Concept { - public: - Concept(QConfig config) : _config(config) {} - virtual ~Concept() = default; - - bool isEnabled() const { return _isEnabled; } - void setEnabled(bool isEnabled) { _isEnabled = isEnabled; } - - virtual const Varying getInput() const { return Varying(); } - virtual const Varying getOutput() const { return Varying(); } - - virtual QConfig& getConfiguration() { return _config; } - virtual void applyConfiguration() = 0; - - virtual void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) = 0; - - protected: - QConfig _config; - bool _isEnabled = true; - }; - using ConceptPointer = std::shared_ptr; - - template class Model : public Concept { - public: - using Data = T; - using Config = C; - - Data _data; - - Model(Data data = Data()) : _data(data), Concept(std::make_shared()) { - applyConfiguration(); - } - - void applyConfiguration() { - jobConfigure(_data, *std::static_pointer_cast(_config)); - } - - void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { - if (isEnabled()) { - jobRun(_data, sceneContext, renderContext); - } - } - }; - - template class ModelI : public Concept { - public: - using Data = T; - using Config = C; - using Input = I; - - Data _data; - Varying _input; - - const Varying getInput() const { return _input; } - - ModelI(const Varying& input, Data data = Data()) : _input(input), _data(data), Concept(std::make_shared()) { - applyConfiguration(); - } - - void applyConfiguration() { - jobConfigure(_data, *std::static_pointer_cast(_config)); - } - - void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { - if (isEnabled()) { - jobRunI(_data, sceneContext, renderContext, _input.get()); - } - } - }; - - template class ModelO : public Concept { - public: - using Data = T; - using Config = C; - using Output = O; - - Data _data; - Varying _output; - - const Varying getOutput() const { return _output; } - - ModelO(Data data = Data()) : _data(data), _output(Output()), Concept(std::make_shared()) { - applyConfiguration(); - } - - void applyConfiguration() { - jobConfigure(_data, *std::static_pointer_cast(_config)); - } - - void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { - if (isEnabled()) { - jobRunO(_data, sceneContext, renderContext, _output.edit()); - } - } - }; - - template class ModelIO : public Concept { - public: - using Data = T; - using Config = C; - using Input = I; - using Output = O; - - Data _data; - Varying _input; - Varying _output; - - const Varying getInput() const { return _input; } - const Varying getOutput() const { return _output; } - - ModelIO(const Varying& input, Data data = Data()) : _data(data), _input(input), _output(Output()), Concept(std::make_shared()) { - applyConfiguration(); - } - - void applyConfiguration() { - jobConfigure(_data, *std::static_pointer_cast(_config)); - } - - void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { - if (isEnabled()) { - jobRunIO(_data, sceneContext, renderContext, _input.get(), _output.edit()); - } - } - }; - - Job(std::string name, ConceptPointer concept) : _concept(concept), _name(name) {} - - bool isEnabled() const { return _concept->isEnabled(); } - void setEnabled(bool isEnabled) { _concept->setEnabled(isEnabled); } - - const Varying getInput() const { return _concept->getInput(); } - const Varying getOutput() const { return _concept->getOutput(); } - QConfig& getConfiguration() const { return _concept->getConfiguration(); } - void applyConfiguration() { return _concept->applyConfiguration(); } - - template T& edit() { - auto concept = std::static_pointer_cast(_concept); - assert(concept); - return concept->_data; - } - - void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { - PerformanceTimer perfTimer(_name.c_str()); - PROFILE_RANGE(_name.c_str()); - - _concept->run(sceneContext, renderContext); - } - - protected: - ConceptPointer _concept; - std::string _name = ""; -}; - -class Task; - -class TaskConfig : public QObject { - Q_OBJECT -public: - TaskConfig(Task& task) : _task{ task } {} -public slots: - void refresh() { - qDebug() << "ATTEMPTING REFRESH"; - //_task.configure(*this); - } -private: - Task& _task; -}; - -// A task is a specialized job to run a collection of other jobs -// It is defined with JobModel = Task::Model -class Task { -public: - using QConfig = Job::QConfig; - - template class Model : public Job::Concept { - public: - using Data = T; - using Config = TaskConfig; - - Data _data; - - Model(Data data = Data()) : _data(data), Concept(std::make_shared()) { - _config = _data._config; - } - - void applyConfiguration() { - jobConfigure(_data, *_config); - } - - void run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext) { - if (isEnabled()) { - jobRun(_data, sceneContext, renderContext); - } - } - }; - - using Jobs = std::vector; - - Task() : _config{ std::make_shared(*this) } {} - - // Queue a new job to the container; returns the job's output - template const Varying addJob(std::string name, A&&... args) { - _jobs.emplace_back(name, std::make_shared(std::forward(args)...)); - QConfig config = _jobs.back().getConfiguration(); - config->setParent(_config.get()); - config->setObjectName(name.c_str()); - QObject::connect( - config.get(), SIGNAL(dirty()), - _config.get(), SLOT(refresh())); - return _jobs.back().getOutput(); - } - - QConfig getConfiguration() { return _config; } - void configure(const QObject& configuration) { - for (auto& job : _jobs) { - job.applyConfiguration(); - } - } - void enableJob(size_t jobIndex, bool enable = true) { _jobs.at(jobIndex).setEnabled(enable); } - bool getEnableJob(size_t jobIndex) const { return _jobs.at(jobIndex).isEnabled(); } - -protected: - template friend class Model; - - QConfig _config; - Jobs _jobs; -}; - -} - -#endif // hifi_render_Task_h diff --git a/libraries/render/src/render/Task.h b/libraries/render/src/render/Task.h index 434674e198..f592ad1b2b 100644 --- a/libraries/render/src/render/Task.h +++ b/libraries/render/src/render/Task.h @@ -2,8 +2,8 @@ // Task.h // render/src/render // -// Created by Zach Pomerantz on 1/6/2015. -// Copyright 2015 High Fidelity, Inc. +// Created by Zach Pomerantz on 1/6/2016. +// Copyright 2016 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html @@ -55,16 +55,17 @@ protected: class JobConfig : public QObject { Q_OBJECT -}; - -class DefaultJobConfig: public JobConfig { - Q_OBJECT +public: + Q_PROPERTY(bool enabled MEMBER enabled NOTIFY dirty) + bool enabled; +signals: + void dirty(); }; template void jobConfigure(T& model, const C& configuration) { model.configure(configuration); } -template void jobConfigure(T&, const DefaultJobConfig&) { +template void jobConfigure(T&, const JobConfig&) { } // FIXME: In c++17, use default classes of nullptr_t to combine these @@ -109,10 +110,9 @@ public: }; using ConceptPointer = std::shared_ptr; - template class Model : public Concept { + template class Model : public Concept { public: using Data = T; - using Config = C; Data _data; @@ -131,10 +131,9 @@ public: } }; - template class ModelI : public Concept { + template class ModelI : public Concept { public: using Data = T; - using Config = C; using Input = I; Data _data; @@ -157,10 +156,9 @@ public: } }; - template class ModelO : public Concept { + template class ModelO : public Concept { public: using Data = T; - using Config = C; using Output = O; Data _data; @@ -183,10 +181,9 @@ public: } }; - template class ModelIO : public Concept { + template class ModelIO : public Concept { public: using Data = T; - using Config = C; using Input = I; using Output = O; @@ -242,15 +239,12 @@ public: class Task; -class TaskConfig : public QObject { +class TaskConfig : public Job::Config { Q_OBJECT public: TaskConfig(Task& task) : _task{ task } {} public slots: - void refresh() { - qDebug() << "ATTEMPTING REFRESH"; - //_task.configure(*this); - } + void refresh(); private: Task& _task; }; @@ -259,15 +253,16 @@ private: // It is defined with JobModel = Task::Model class Task { public: + using Config = TaskConfig; using QConfig = Job::QConfig; template class Model : public Job::Concept { public: using Data = T; - using Config = TaskConfig; Data _data; + // The _config is unused; the model delegates to its data's _config Model(Data data = Data()) : _data(data), Concept(std::make_shared()) { _config = _data._config; } @@ -285,7 +280,7 @@ public: using Jobs = std::vector; - Task() : _config{ std::make_shared(*this) } {} + Task() : _config{ std::make_shared(*this) } {} // Queue a new job to the container; returns the job's output template const Varying addJob(std::string name, A&&... args) { @@ -293,9 +288,7 @@ public: QConfig config = _jobs.back().getConfiguration(); config->setParent(_config.get()); config->setObjectName(name.c_str()); - QObject::connect( - config.get(), SIGNAL(dirty()), - _config.get(), SLOT(refresh())); + QObject::connect(config.get(), SIGNAL(dirty()), _config.get(), SLOT(refresh())); return _jobs.back().getOutput(); }