Cogs.Core
EffectManager.cpp
1#include "EffectManager.h"
2
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IEffects.h"
5
6#include "Foundation/Logging/Logger.h"
7
8#include "Renderer/IRenderer.h"
9
10#include "Services/Variables.h"
11
12#include "ResourceStore.h"
13
14#include "Context.h"
15
16#include "ShaderBuilder.h"
17
18namespace
19{
20 Cogs::Logging::Log logger = Cogs::Logging::getLogger("EffectManager");
21}
22
24{
25 reportLeaks("Effect");
26}
27
29{
30 auto & dataDir = context->resourceStore->getDataDir();
31
32 context->resourceStore->addSearchPath(dataDir + "Shaders/");
33 context->resourceStore->addSearchPath("Shaders/");
34 context->resourceStore->addSearchPath("shaders/");
35}
36
38{
39 bool preLoad = context->variables->get("resources.effects.preLoad", false);
40
41 if (preLoad) {
42 if (!context->resourceStore->hasResources(loadInfo->dependencies)) {
43 if (!loadInfo->preloading) {
44 context->resourceStore->preloadResources(loadInfo->dependencies);
45 loadInfo->preloading = true;
46 }
47
48 // Re-queue until all dependencies available.
49 queueResource(loadInfo);
50
51 return;
52 }
53 }
54
55 auto effect = get(loadInfo->handle);
56 effect->definition = std::move(loadInfo->definition);
57
58 auto & definition = effect->definition;
59
60 if(definition.vertexShader.loadPath.find(extension) == std::string::npos) {
61
62 if (definition.vertexShader.loadPath.size()) {
63 definition.vertexShader.loadPath += extension;
64 }
65
66 if (definition.hullShader.loadPath.size()) {
67 definition.hullShader.loadPath += extension;
68 }
69
70 if (definition.domainShader.loadPath.size()) {
71 definition.domainShader.loadPath += extension;
72 }
73
74 if (definition.geometryShader.loadPath.size()) {
75 definition.geometryShader.loadPath += extension;
76 }
77
78 if (definition.pixelShader.loadPath.size()) {
79 definition.pixelShader.loadPath += extension;
80 }
81 }
82
83 setProcessed(loadInfo);
84}
85
86void Cogs::Core::EffectManager::handleReload(ResourceHandleBase handle)
87{
88 EffectHandle effect(handle);
89
90 auto & loadInfo = *createLoadInfo();
91 loadInfo.resourceId = effect->getId();
92 loadInfo.resourcePath = effect->getSource().to_string();
93 loadInfo.resourceName = effect->getName().to_string();
94 loadInfo.loadFlags = ResourceLoadFlags::Reload;
95 loadInfo.handle = handle;
96 loadInfo.definition = effect->definition;
97
98 loadResource(&loadInfo);
99}
100
102{
103 return context->renderer->getResources()->updateResource(handle);
104}
105
107{
108 switch (context->renderer->getDevice()->getType()) {
110 extension = ".es30.glsl";
111 break;
113 extension = ".wgsl";
114 break;
115 default:
116 extension = ".hlsl";
117 }
118
119 EffectLoadInfo & loadInfo = *createLoadInfo();
120 loadInfo.definition = definition;
121 loadInfo.resourceName = definition.name;
122
123 for (auto & d : definition.dependencies) {
124 loadInfo.dependencies.emplace_back("Shaders/" + d + extension);
125 }
126
127 if (definition.vertexShader.loadPath.size()) {
128 loadInfo.dependencies.emplace_back("Shaders/" + definition.vertexShader.loadPath + extension);
129 }
130
131 if (definition.pixelShader.loadPath.size()) {
132 loadInfo.dependencies.emplace_back("Shaders/" + definition.pixelShader.loadPath + extension);
133 }
134
135 return loadResource(&loadInfo);
136}
137
139{
140 context->renderer->getResources()->releaseResource(resource);
141}
void handleLoad(EffectLoadInfo *loadInfo) override
Overridden to handle effect loading.
ActivationResult handleActivation(EffectHandle handle, Effect *effect) override
Overridden to handle effect activation, updating the effect in the renderer.
void initialize() override
Initialize the effect manager.
EffectHandle loadEffect(const EffectDefinition &definition)
Load the effect described by the given definition.
~EffectManager()
Destructs the EffectManager.
void handleDeletion(Effect *resource) override
Overridden to handle deletion, removing the effect resource from the renderer.
Log implementation class.
Definition: LogManager.h:139
ActivationResult
Defines results for resource activation.
Definition: ResourceBase.h:14
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ WebGPU
Graphics device using the WebGPU API Backend.
Defines a loadable effect.
std::string name
Name of the effect.
std::vector< std::string > dependencies
Load information for Effect resources.
Definition: EffectManager.h:20
std::vector< std::string > dependencies
Paths to all dependencies that need to be resident before activating the effect.
Definition: EffectManager.h:25
EffectDefinition definition
Definition of the effect and its dependencies.
Definition: EffectManager.h:22
bool preloading
If the effect is currently in the preloading phase, still resolving dependencies.
Definition: EffectManager.h:28
Effect resources contain data to control the shader stages of the GPU pipeline.
Definition: Effect.h:24
Resource handle base class handling reference counting of resources derived from ResourceBase.
std::string resourceName
Desired resource name. If no name is given, a default name will be chosen.
ResourceHandleBase handle
Handle to resource structure for holding actual resource data.