-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtonemap.cpp
More file actions
99 lines (77 loc) · 2.96 KB
/
tonemap.cpp
File metadata and controls
99 lines (77 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "pipelinetasks/tonemap.h"
#include "components/private/postprocessorsettings.h"
#include "engine.h"
#include "pipelinecontext.h"
#include "commandbuffer.h"
#include "resources/material.h"
#include "resources/rendertarget.h"
namespace {
const char *lutMap("lutMap");
const char *colorGradingLut("toneMap/colorGradingLut");
}
Tonemap::Tonemap() :
m_resultTexture(Engine::objectCreate<Texture>("toneMap")),
m_defaultLutTexture(Engine::objectCreate<Texture>("defaultLut")),
m_lutTexture(nullptr),
m_resultTarget(Engine::objectCreate<RenderTarget>()),
m_resultMaterial(nullptr) {
setName("Tonemap");
const int side = 16;
const int step = 17;
m_defaultLutTexture->setFormat(Texture::RGBA8);
m_defaultLutTexture->setFiltering(Texture::Bilinear);
m_defaultLutTexture->resize(side, side);
m_defaultLutTexture->setDepth(side);
ByteArray data;
data.resize(side * side * side * 4);
for(int d = 0; d < side; d++) {
for(int h = 0; h < side; h++) {
for(int w = 0; w < side; w++) {
int row = d * side + h;
int index = (row * side + w) * 4;
data[index] = w * step;
data[index + 1] = h * step;
data[index + 2] = d * step;
data[index + 3] = 255;
}
}
}
Texture::Surface &surface = m_defaultLutTexture->surface(0);
surface.clear();
surface.push_back(data);
m_inputs.push_back("In");
PostProcessSettings::registerSetting(colorGradingLut, Variant::fromValue(m_lutTexture));
Material *material = Engine::loadResource<Material>(".embedded/Tonemap.shader");
if(material) {
m_resultMaterial = material->createInstance();
m_resultMaterial->setTexture(lutMap, m_defaultLutTexture);
}
m_resultTexture->setFormat(Texture::RGB8);
m_resultTexture->setFlags(Texture::Render);
m_resultTarget->setColorAttachment(0, m_resultTexture);
m_outputs.push_back(std::make_pair(m_resultTexture->name(), m_resultTexture));
}
Tonemap::~Tonemap() {
m_resultTarget->deleteLater();
}
void Tonemap::exec(PipelineContext &context) {
if(m_resultMaterial) {
CommandBuffer *buffer = context.buffer();
for(auto it : context.culledPostEffectSettings()) {
Texture *texture = it.first->readValue(colorGradingLut).value<Texture *>();
if(texture != m_lutTexture) {
m_lutTexture = texture;
m_resultMaterial->setTexture(lutMap, m_lutTexture ? m_lutTexture : m_defaultLutTexture);
}
}
buffer->beginDebugMarker("Tonemap");
buffer->setRenderTarget(m_resultTarget);
buffer->drawMesh(PipelineContext::defaultPlane(), 0, CommandBuffer::UI, *m_resultMaterial);
buffer->endDebugMarker();
}
}
void Tonemap::setInput(int index, Texture *texture) {
if(m_resultMaterial) {
m_resultMaterial->setTexture("rgbMap", texture);
}
}