- change tonemaps to steps

This commit is contained in:
Magnus Norddahl 2018-06-23 19:25:49 +02:00
commit b2fad453fa
9 changed files with 114 additions and 182 deletions

View file

@ -514,3 +514,79 @@ void PPColormap::UpdateSteps(int fixedcm)
steps.Push(step);
hw_postprocess.Effects["ColormapScene"] = steps;
}
/////////////////////////////////////////////////////////////////////////////
void PPTonemap::DeclareShaders()
{
hw_postprocess.Shaders["Tonemap.Linear"] = { "shaders/glsl/tonemap.fp", "#define LINEAR\n", {} };
hw_postprocess.Shaders["Tonemap.Reinhard"] = { "shaders/glsl/tonemap.fp", "#define REINHARD\n", {} };
hw_postprocess.Shaders["Tonemap.HejlDawson"] = { "shaders/glsl/tonemap.fp", "#define HEJLDAWSON\n", {} };
hw_postprocess.Shaders["Tonemap.Uncharted2"] = { "shaders/glsl/tonemap.fp", "#define UNCHARTED2\n", {} };
hw_postprocess.Shaders["Tonemap.Palette"] = { "shaders/glsl/tonemap.fp", "#define PALETTE\n", {} };
}
void PPTonemap::UpdateTextures()
{
if (gl_tonemap == Palette)
{
auto &texture = hw_postprocess.Textures["Tonemap.Palette"];
if (!texture.Data)
{
std::shared_ptr<void> data(new uint32_t[512 * 512], [](void *p) { delete[](uint32_t*)p; });
uint8_t *lut = (uint8_t *)data.get();
for (int r = 0; r < 64; r++)
{
for (int g = 0; g < 64; g++)
{
for (int b = 0; b < 64; b++)
{
PalEntry color = GPalette.BaseColors[(uint8_t)PTM_BestColor((uint32_t *)GPalette.BaseColors, (r << 2) | (r >> 4), (g << 2) | (g >> 4), (b << 2) | (b >> 4),
gl_paltonemap_reverselookup, gl_paltonemap_powtable, 0, 256)];
int index = ((r * 64 + g) * 64 + b) * 4;
lut[index] = color.r;
lut[index + 1] = color.g;
lut[index + 2] = color.b;
lut[index + 3] = 255;
}
}
}
texture = { 512, 512, PixelFormat::Rgba8, data };
}
}
}
void PPTonemap::UpdateSteps()
{
if (gl_tonemap == 0)
{
hw_postprocess.Effects["TonemapScene"] = {};
return;
}
PPShaderName shader;
switch (gl_tonemap)
{
default:
case Linear: shader = "Tonemap.Linear"; break;
case Reinhard: shader = "Tonemap.Reinhard"; break;
case HejlDawson: shader = "Tonemap.HejlDawson"; break;
case Uncharted2: shader = "Tonemap.Uncharted2"; break;
case Palette: shader = "Tonemap.Palette"; break;
}
PPStep step;
step.ShaderName = shader;
step.Viewport = screen->mScreenViewport;
step.SetInputCurrent(0);
if (gl_tonemap == Palette)
step.SetInputTexture(1, "Tonemap.Palette");
step.SetOutputNext();
step.SetNoBlend();
TArray<PPStep> steps;
steps.Push(step);
hw_postprocess.Effects["TonemapScene"] = steps;
}

View file

@ -179,11 +179,12 @@ class PPTextureDesc
{
public:
PPTextureDesc() { }
PPTextureDesc(int width, int height, PixelFormat format) : Width(width), Height(height), Format(format) { }
PPTextureDesc(int width, int height, PixelFormat format, std::shared_ptr<void> data = {}) : Width(width), Height(height), Format(format), Data(data) { }
int Width;
int Height;
PixelFormat Format;
std::shared_ptr<void> Data;
};
class PPShader
@ -411,3 +412,24 @@ public:
void DeclareShaders();
void UpdateSteps(int fixedcm);
};
/////////////////////////////////////////////////////////////////////////////
class PPTonemap
{
public:
void DeclareShaders();
void UpdateTextures();
void UpdateSteps();
enum TonemapMode
{
None,
Uncharted2,
HejlDawson,
Reinhard,
Linear,
Palette,
NumTonemapModes
};
};

View file

@ -1,63 +0,0 @@
//
//---------------------------------------------------------------------------
//
// Copyright(C) 2016 Magnus Norddahl
// All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/
//
//--------------------------------------------------------------------------
//
/*
** gl_tonemapshader.cpp
** Converts a HDR texture to 0-1 range by applying a tonemap operator
**
*/
#include "v_video.h"
#include "hwrenderer/utility/hw_cvars.h"
#include "hw_tonemapshader.h"
void FTonemapShader::Bind(IRenderQueue *q)
{
auto &shader = mShader[gl_tonemap];
if (!shader)
{
auto prolog = GetDefines(gl_tonemap);
shader.reset(screen->CreateShaderProgram());
shader->Compile(IShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
shader->Compile(IShaderProgram::Fragment, "shaders/glsl/tonemap.fp", prolog, 330);
shader->Link("shaders/glsl/tonemap");
}
shader->Bind(q);
}
bool FTonemapShader::IsPaletteMode()
{
return gl_tonemap == Palette;
}
const char *FTonemapShader::GetDefines(int mode)
{
switch (mode)
{
default:
case Linear: return "#define LINEAR\n";
case Reinhard: return "#define REINHARD\n";
case HejlDawson: return "#define HEJLDAWSON\n";
case Uncharted2: return "#define UNCHARTED2\n";
case Palette: return "#define PALETTE\n";
}
}

View file

@ -1,30 +0,0 @@
#ifndef __GL_TONEMAPSHADER_H
#define __GL_TONEMAPSHADER_H
#include "hwrenderer/postprocessing/hw_shaderprogram.h"
class FTonemapShader
{
public:
void Bind(IRenderQueue *q);
static bool IsPaletteMode();
private:
enum TonemapMode
{
None,
Uncharted2,
HejlDawson,
Reinhard,
Linear,
Palette,
NumTonemapModes
};
static const char *GetDefines(int mode);
std::unique_ptr<IShaderProgram> mShader[NumTonemapModes];
};
#endif