/
resize.cpp
195 lines (152 loc) · 4.55 KB
/
resize.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/** @file fx/resize.cpp Change the size (pixel density) of the view.
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 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 General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "render/fx/resize.h"
#include "ui/clientwindow.h"
#include "clientapp.h"
#include <de/VRConfig>
#include <de/Drawable>
#include <de/GLFramebuffer>
#include <de/Variable>
#include <QList>
using namespace de;
namespace fx {
static ddouble const MIN_FACTOR = 1.0 / 16.0;
DENG2_PIMPL(Resize)
{
Variable const &pixelDensity;
Variable const &resizeFactor;
GLFramebuffer framebuf;
Drawable frame;
GLUniform uMvpMatrix { "uMvpMatrix", GLUniform::Mat4 };
GLUniform uFrame { "uTex", GLUniform::Sampler2D };
typedef GLBufferT<Vertex2Tex> VBuf;
Instance(Public *i)
: Base(i)
, pixelDensity { App::config("render.pixelDensity") }
, resizeFactor { App::config("render.fx.resize.factor") }
{}
GuiRootWidget &root() const
{
return ClientWindow::main().game().root();
}
float factor() const
{
return max(MIN_FACTOR, pixelDensity * resizeFactor);
}
/// Determines if the post-processing shader will be applied.
bool isActive() const
{
// This kind of scaling is not compatible with Oculus Rift -- LibOVR does its
// own pixel density scaling.
if(ClientApp::vr().mode() == VRConfig::OculusRift) return false;
return !fequal(factor(), 1.f);
}
void glInit()
{
framebuf.glInit();
uMvpMatrix = Matrix4f::ortho(0, 1, 0, 1);
uFrame = framebuf.colorTexture();
// Drawable for drawing stuff back to the original target.
VBuf *buf = new VBuf;
buf->setVertices(gl::TriangleStrip,
VBuf::Builder().makeQuad(Rectanglef(0, 0, 1, 1),
Rectanglef(0, 1, 1, -1)),
gl::Static);
frame.addBuffer(buf);
ClientApp::shaders().build(frame.program(), "generic.texture")
<< uMvpMatrix << uFrame;
}
void glDeinit()
{
LOGDEV_GL_XVERBOSE("Releasing GL resources");
framebuf.glDeinit();
frame.clear();
}
void update()
{
framebuf.resize(GLState::current().target().rectInUse().size() * factor());
framebuf.setSampleCount(GLFramebuffer::defaultMultisampling());
}
void begin()
{
if(!isActive()) return;
update();
GLState::push()
.setTarget(framebuf.target())
.setViewport(Rectangleui::fromSize(framebuf.size()))
.setColorMask(gl::WriteAll)
.apply();
framebuf.target().clear(GLTarget::ColorDepthStencil);
}
void end()
{
if(!isActive()) return;
GLState::pop().apply();
}
void draw()
{
if(!isActive()) return;
glEnable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);
GLState::push()
.setBlend(false)
.setDepthTest(false)
.setViewport(Rectangleui::fromSize(GLState::current().target().size()))
.apply();
frame.draw();
GLState::pop().apply();
glEnable(GL_ALPHA_TEST);
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
}
};
Resize::Resize(int console)
: ConsoleEffect(console), d(new Instance(this))
{}
bool Resize::isActive() const
{
return d->isActive();
}
void Resize::glInit()
{
if(!d->isActive()) return;
LOG_AS("fx::Resize");
ConsoleEffect::glInit();
d->glInit();
}
void Resize::glDeinit()
{
LOG_AS("fx::Resize");
d->glDeinit();
ConsoleEffect::glDeinit();
}
void Resize::beginFrame()
{
d->begin();
}
void Resize::endFrame()
{
LOG_AS("fx::Resize");
d->end();
d->draw();
if(!d->isActive() && isInited())
{
glDeinit();
}
}
} // namespace fx