Cogs.Core
ComputeTask.cpp
1#include "ComputeTask.h"
2
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IContext.h"
5
6#include "Foundation/Logging/Logger.h"
7#include "Resources/ShaderBuilderPostProcess.h"
8
9#include "Renderer/Renderer.h"
10
11using namespace Cogs;
12
13namespace
14{
15 Cogs::Logging::Log logger = Cogs::Logging::getLogger("ComputeTask");
16}
17
18void Cogs::Core::ComputeTask::initialize(RenderTaskContext * context)
19{
20 EffectDescription desc = {};
21
22 for (auto & p : effectParameter.values) {
23 if (p.key == "definitions") {
24 for (auto & d : p.values) {
25 desc.definitions.push_back({ d.key, d.value });
26 }
27 }
28 if (p.key == "source") {
29 desc.cs = p.value;
30 }
31 }
32
33 effect = context->renderer->getEffectCache().loadEffect(context, desc);
34}
35
36void Cogs::Core::ComputeTask::initialize(RenderTaskContext* context, const RenderTaskDefinition& taskDefinition) {
37 ProcessTask::initialize(context, taskDefinition);
38 EffectDescription desc = {};
39
40 for (auto& p : effectParameter.values) {
41 if (p.key == "definitions") {
42 for (auto& d : p.values) {
43 desc.definitions.push_back({ d.key, d.value });
44 }
45 }
46 if (p.key == "source") {
47 desc.cs = p.value;
48 }
49 }
50
51 {
52 Cogs::GraphicsDeviceType graphicsDeviceType = context->renderer->getDevice()->getType();
53 bool success = false;
54 switch (graphicsDeviceType)
55 {
56#if 0
57
59 success = Cogs::Core::buildPostProcessEffectES3(context, desc, this);
60 break;
62 success = buildPostProcessEffectWebGPU(context, desc);
63 break;
64#endif
67 success = buildComputeEffect(context, desc, this);
68 break;
69 default:
70 break;
71 }
72 if (!success) {
73 LOG_ERROR(logger, "Failed to build post processing shader source");
74 }
75 }
76
77 effect = context->renderer->getEffectCache().loadEffect(context, desc);
78}
79
80void Cogs::Core::ComputeTask::cleanup(RenderTaskContext * context)
81{
82 ProcessTask::cleanup(context);
83}
84
85void Cogs::Core::ComputeTask::apply(RenderTaskContext * context)
86{
87 DynamicRenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING, "ComputeTask", (std::string("ComputeTask<") + name + ">::apply").c_str());
88
89 if (!HandleIsValid(effect->handle)) {
90 LOG_ERROR(logger, "Invalid compute effect handle.");
91 return;
92 }
93
94 auto device = context->renderer->getDevice();
95 auto deviceContext = device->getImmediateContext();
96
97 // Reset render target to avoid input/output locks.
98 deviceContext->setRenderTarget(RenderTargetHandle::NoHandle, DepthStencilHandle::NoHandle);
99
100 deviceContext->setEffect(effect->handle);
101
102 setProperties(context);
103
104 deviceContext->setSamplerState("linearSampler", 0, context->states->getSamplerState(SamplerState::DefaultState()));
105
106 uint32_t groups[3] = { 1, 1, 1 };
107
108 if (this->groups.expressions.size() == 3) {
109 for (size_t i = 0; i < 3; i++) {
110 groups[i] = this->scope->update(this->groups.expressions[i].second, 0);
111 }
112 }
113 else {
114 for (auto & ep : effectParameter.values) {
115 if (ep.key != "groups") continue;
116
117 for (uint32_t i = 0; i < 3; i++) {
118 groups[i] = static_cast<uint32_t>(ep.float3Value[i]);
119 }
120 }
121 }
122
123 if (groups[0] && groups[1] && groups[2]) {
124 deviceContext->dispatchCompute(groups[0], groups[1], groups[2]);
125 } else {
126 LOG_WARNING(logger, "Empty compute shader dimensions.");
127 }
128}
Log implementation class.
Definition: LogManager.h:140
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:181
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
GraphicsDeviceType
Contains types of graphics devices that may be supported.
Definition: Base.h:48
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ Direct3D11
Graphics device using the Direct3D 11 API.
@ OpenGL20
Graphics device using OpenGL, supporting at least OpenGL 2.0.
@ WebGPU
Graphics device using the WebGPU API Backend.
Contains an effect description used to load a single effect.
Definition: IEffects.h:55
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:78
static SamplerState & DefaultState()
Constructs a sampler state initialized with the default values.
Definition: SamplerState.h:85