Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions engine/includes/pipelinetasks/tonemap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef TONEMAP_H
#define TONEMAP_H

#include "pipelinetask.h"

class RenderTarget;
class MaterialInstance;

class Tonemap : public PipelineTask {
A_REGISTER(Tonemap, PipelineTask, Pipeline)

public:
Tonemap();
~Tonemap();

private:
void exec(PipelineContext &context) override;

void setInput(int index, Texture *texture) override;

private:
Texture *m_resultTexture;
Texture *m_defaultLutTexture;
Texture *m_lutTexture;

RenderTarget *m_resultTarget;

MaterialInstance *m_resultMaterial;

};

#endif // TONEMAP_H
13 changes: 9 additions & 4 deletions engine/includes/resources/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ENGINE_EXPORT Texture : public Resource {
A_PROPERTIES(
A_PROPERTY(int, width, Texture::width, Texture::setWidth),
A_PROPERTY(int, height, Texture::height, Texture::setHeight),
A_PROPERTY(int, depth, Texture::depth, Texture::setDepth),
A_PROPERTY(int, format, Texture::format, Texture::setFormat),
A_PROPERTY(int, wrap, Texture::wrap, Texture::setWrap),
A_PROPERTY(int, filtering, Texture::filtering, Texture::setFiltering)
Expand Down Expand Up @@ -95,6 +96,9 @@ class ENGINE_EXPORT Texture : public Resource {
int height() const;
void setHeight(int height);

int depth() const;
void setDepth(int depth);

int flags() const;
void setFlags(int flags);

Expand Down Expand Up @@ -145,9 +149,9 @@ class ENGINE_EXPORT Texture : public Resource {
void switchState(Resource::State state) override;
bool isUnloadable() override;

int32_t size(int32_t width, int32_t height) const;
int32_t sizeDXTc(int32_t width, int32_t height) const;
int32_t sizeRGB(int32_t width, int32_t height) const;
int32_t size(int32_t width, int32_t height, int32_t depth) const;
int32_t sizeDXTc(int32_t width, int32_t height, int32_t depth) const;
int32_t sizeRGB(int32_t width, int32_t height, int32_t depth) const;

bool isDwordAligned();
int32_t dwordAlignedLineSize(int32_t width, int32_t bpp);
Expand All @@ -164,9 +168,10 @@ class ENGINE_EXPORT Texture : public Resource {

int32_t m_width;
int32_t m_height;

int32_t m_depth;

int32_t m_depthBits;

int32_t m_flags;

static uint32_t s_maxTextureSize;
Expand Down
99 changes: 99 additions & 0 deletions engine/src/pipelinetasks/tonemap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
57 changes: 36 additions & 21 deletions engine/src/resources/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Texture::Texture() :
m_wrap(Texture::Clamp),
m_width(1),
m_height(1),
m_depth(0),
m_depth(1),
m_depthBits(0),
m_flags(0) {

}
Expand All @@ -96,10 +97,11 @@ void Texture::loadUserData(const VariantMap &data) {
Surface img;
int32_t w = m_width;
int32_t h = m_height;
int32_t d = m_depth;
const VariantList &lods = s.value<VariantList>();
for(auto &l : lods) {
ByteArray bits = l.toByteArray();
uint32_t s = size(w, h);
uint32_t s = size(w, h, d);
if(s && !bits.empty()) {
ByteArray pixels;
pixels.resize(s);
Expand All @@ -108,6 +110,7 @@ void Texture::loadUserData(const VariantMap &data) {
}
w = MAX(w / 2, 1);
h = MAX(h / 2, 1);
d = MAX(d / 2, 1);
}
addSurface(img);
}
Expand Down Expand Up @@ -202,24 +205,36 @@ ByteArray Texture::getPixels(int level) const {
int Texture::width() const {
return m_width;
}
/*!
Returns height for the texture.
*/
int Texture::height() const {
return m_height;
}
/*!
Sets new \a width for the texture.
*/
void Texture::setWidth(int width) {
resize(width, m_height);
}
/*!
Returns height for the texture.
*/
int Texture::height() const {
return m_height;
}
/*!
Sets new \a height for the texture.
*/
void Texture::setHeight(int height) {
resize(m_width, height);
}
/*!
Returns depth dimension for the texture.
*/
int Texture::depth() const {
return m_depth;
}
/*!
Sets new \a depth dimension for the texture.
*/
void Texture::setDepth(int depth) {
m_depth = depth;
}
/*!
Sets new \a width and \a height for the texture.
*/
Expand All @@ -231,7 +246,7 @@ void Texture::resize(int width, int height) {
if(!(m_flags & Flags::Render) || (m_flags & Flags::Feedback)) {
clear();

int32_t length = size(m_width, m_height);
int32_t length = size(m_width, m_height, m_depth);
ByteArray pixels;
pixels.resize(length);

Expand Down Expand Up @@ -284,18 +299,18 @@ void Texture::setWrap(int type) {
m_wrap = type;
}
/*!
Returns the number of depth bits.
Returns the number of depth buffer bits.
\note This value is valid only for the depth textures.
*/
int Texture::depthBits() const {
return m_depth;
return m_depthBits;
}
/*!
Sets the number of \a depth bits.
Sets the number of \a depth buffer bits.
\note This value is valid only for the depth textures.
*/
void Texture::setDepthBits(int depth) {
m_depth = depth;
m_depthBits = depth;
}
/*!
Returns true if texture is can be attached to framebuffer; otherwise returns false.
Expand Down Expand Up @@ -327,7 +342,7 @@ void Texture::setFlags(int flags) {
m_flags = flags;

if(isFeedback() && sides() == 0) {
int32_t length = size(m_width, m_height);
int32_t length = size(m_width, m_height, m_depth);
ByteArray pixels;
pixels.resize(length);

Expand Down Expand Up @@ -411,24 +426,24 @@ void Texture::setMaxCubemapSize(uint32_t size) {
/*!
\internal
*/
int32_t Texture::size(int32_t width, int32_t height) const {
int32_t (Texture::*sizefunc)(int32_t, int32_t) const;
int32_t Texture::size(int32_t width, int32_t height, int32_t depth) const {
int32_t (Texture::*sizefunc)(int32_t, int32_t, int32_t) const;
sizefunc = (isCompressed() ? &Texture::sizeDXTc : &Texture::sizeRGB);

return (this->*sizefunc)(width, height);
return (this->*sizefunc)(width, height, depth);
}
/*!
\internal
*/
inline int32_t Texture::sizeDXTc(int32_t width, int32_t height) const {
return ((width + 3) / 4) * ((height + 3) / 4) * (m_compress == DXT1 ? 8 : 16);
inline int32_t Texture::sizeDXTc(int32_t width, int32_t height, int32_t depth) const {
return ((width + 3) / 4) * ((height + 3) / 4) * ((depth + 3) / 4) * (m_compress == DXT1 ? 8 : 16);
}
/*!
\internal
*/
inline int32_t Texture::sizeRGB(int32_t width, int32_t height) const {
inline int32_t Texture::sizeRGB(int32_t width, int32_t height, int32_t depth) const {
int32_t s = (m_format == RGBA32Float) ? 4 : 1;
return width * height * components() * s;
return width * height * depth * components() * s;
}
/*!
\internal
Expand Down
3 changes: 3 additions & 0 deletions engine/src/systems/rendersystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "pipelinetasks/indirect.h"
#include "pipelinetasks/shadowmap.h"
#include "pipelinetasks/translucent.h"
#include "pipelinetasks/tonemap.h"

#include "pipelinecontext.h"
#include "commandbuffer.h"
Expand Down Expand Up @@ -79,6 +80,7 @@ RenderSystem::RenderSystem() :
DeferredIndirect::registerClassFactory(this);
ShadowMap::registerClassFactory(this);
Translucent::registerClassFactory(this);
Tonemap::registerClassFactory(this);
}
++m_registered;

Expand Down Expand Up @@ -124,6 +126,7 @@ RenderSystem::~RenderSystem() {
DeferredIndirect::unregisterClassFactory(this);
ShadowMap::unregisterClassFactory(this);
Translucent::unregisterClassFactory(this);
Tonemap::unregisterClassFactory(this);
}
}

Expand Down
Loading