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
8#include "Renderer/Renderer.h"
9
10using namespace Cogs;
11
12namespace
13{
14 Cogs::Logging::Log logger = Cogs::Logging::getLogger("ComputeTask");
15}
16
17void Cogs::Core::ComputeTask::initialize(RenderTaskContext * context)
18{
19 EffectDescription desc = {};
20
21 for (auto & p : effectParameter.values) {
22 if (p.key == "definitions") {
23 for (auto & d : p.values) {
24 desc.definitions.push_back({ d.key, d.value });
25 }
26 }
27 if (p.key == "source") {
28 desc.cs = p.value;
29 }
30 }
31
32 effect = context->renderer->getEffectCache().loadEffect(context, desc);
33}
34
35void Cogs::Core::ComputeTask::cleanup(RenderTaskContext * context)
36{
37 ProcessTask::cleanup(context);
38}
39
40void Cogs::Core::ComputeTask::apply(RenderTaskContext * context)
41{
42 DynamicRenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING, "ComputeTask", (std::string("ComputeTask<") + name + ">::apply").c_str());
43
44 if (!HandleIsValid(effect->handle)) {
45 LOG_ERROR(logger, "Invalid compute effect handle.");
46 return;
47 }
48
49 auto device = context->renderer->getDevice();
50 auto deviceContext = device->getImmediateContext();
51
52 // Reset render target to avoid input/output locks.
53 deviceContext->setRenderTarget(RenderTargetHandle::NoHandle, DepthStencilHandle::NoHandle);
54
55 deviceContext->setEffect(effect->handle);
56
57 setProperties(context);
58
59 deviceContext->setSamplerState("linearSampler", 0, context->states->getSamplerState(SamplerState::DefaultState()));
60
61 uint32_t groups[3] = { 1, 1, 1 };
62
63 if (this->groups.expressions.size() == 3) {
64 for (size_t i = 0; i < 3; i++) {
65 groups[i] = this->scope->update(this->groups.expressions[i].second, 0);
66 }
67 }
68 else {
69 for (auto & ep : effectParameter.values) {
70 if (ep.key != "groups") continue;
71
72 for (uint32_t i = 0; i < 3; i++) {
73 groups[i] = static_cast<uint32_t>(ep.float3Value[i]);
74 }
75 }
76 }
77
78 if (groups[0] && groups[1] && groups[2]) {
79 deviceContext->dispatchCompute(groups[0], groups[1], groups[2]);
80 } else {
81 LOG_WARNING(logger, "Empty compute shader dimensions.");
82 }
83}
Log implementation class.
Definition: LogManager.h:139
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:180
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
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:77
static SamplerState & DefaultState()
Constructs a sampler state initialized with the default values.
Definition: SamplerState.h:85