/
pipeline.cpp
299 lines (239 loc) · 7.91 KB
/
pipeline.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Minetest
Copyright (C) 2022 x2048, Dmitry Kostenko <codeforsmile@gmail.com>
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 2.1 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "pipeline.h"
#include "client/client.h"
#include "client/hud.h"
#include "IRenderTarget.h"
#include <vector>
#include <memory>
TextureBuffer::~TextureBuffer()
{
for (u32 index = 0; index < m_textures.size(); index++)
m_driver->removeTexture(m_textures[index]);
m_textures.clear();
}
video::ITexture *TextureBuffer::getTexture(u8 index)
{
if (index >= m_textures.size())
return nullptr;
return m_textures[index];
}
void TextureBuffer::setTexture(u8 index, core::dimension2du size, const std::string &name, video::ECOLOR_FORMAT format, bool clear)
{
assert(index != NO_DEPTH_TEXTURE);
if (m_definitions.size() <= index)
m_definitions.resize(index + 1);
auto &definition = m_definitions[index];
definition.valid = true;
definition.dirty = true;
definition.fixed_size = true;
definition.size = size;
definition.name = name;
definition.format = format;
definition.clear = clear;
}
void TextureBuffer::setTexture(u8 index, v2f scale_factor, const std::string &name, video::ECOLOR_FORMAT format, bool clear)
{
assert(index != NO_DEPTH_TEXTURE);
if (m_definitions.size() <= index)
m_definitions.resize(index + 1);
auto &definition = m_definitions[index];
definition.valid = true;
definition.dirty = true;
definition.fixed_size = false;
definition.scale_factor = scale_factor;
definition.name = name;
definition.format = format;
definition.clear = clear;
}
void TextureBuffer::reset(PipelineContext &context)
{
if (!m_driver)
m_driver = context.device->getVideoDriver();
// remove extra textures
if (m_textures.size() > m_definitions.size()) {
for (unsigned i = m_definitions.size(); i < m_textures.size(); i++)
if (m_textures[i])
m_driver->removeTexture(m_textures[i]);
m_textures.set_used(m_definitions.size());
}
// add placeholders for new definitions
while (m_textures.size() < m_definitions.size())
m_textures.push_back(nullptr);
// change textures to match definitions
for (u32 i = 0; i < m_definitions.size(); i++) {
video::ITexture **ptr = &m_textures[i];
ensureTexture(ptr, m_definitions[i], context);
m_definitions[i].dirty = false;
}
RenderSource::reset(context);
}
void TextureBuffer::swapTextures(u8 texture_a, u8 texture_b)
{
assert(m_definitions[texture_a].valid && m_definitions[texture_b].valid);
video::ITexture *temp = m_textures[texture_a];
m_textures[texture_a] = m_textures[texture_b];
m_textures[texture_b] = temp;
}
bool TextureBuffer::ensureTexture(video::ITexture **texture, const TextureDefinition& definition, PipelineContext &context)
{
bool modify;
core::dimension2du size;
if (definition.valid) {
if (definition.fixed_size)
size = definition.size;
else
size = core::dimension2du(
(u32)(context.target_size.X * definition.scale_factor.X),
(u32)(context.target_size.Y * definition.scale_factor.Y));
modify = definition.dirty || (*texture == nullptr) || (*texture)->getSize() != size;
}
else {
modify = (*texture != nullptr);
}
if (!modify)
return false;
if (*texture)
m_driver->removeTexture(*texture);
if (definition.valid) {
if (definition.clear) {
video::IImage *image = m_driver->createImage(definition.format, size);
// Cannot use image->fill because it's not implemented for all formats.
std::memset(image->getData(), 0, image->getDataSizeFromFormat(definition.format, size.Width, size.Height));
*texture = m_driver->addTexture(definition.name.c_str(), image);
image->drop();
}
else {
*texture = m_driver->addRenderTargetTexture(size, definition.name.c_str(), definition.format);
}
}
else {
*texture = nullptr;
}
return true;
}
TextureBufferOutput::TextureBufferOutput(TextureBuffer *_buffer, u8 _texture_index)
: buffer(_buffer), texture_map({_texture_index})
{}
TextureBufferOutput::TextureBufferOutput(TextureBuffer *_buffer, const std::vector<u8> &_texture_map)
: buffer(_buffer), texture_map(_texture_map)
{}
TextureBufferOutput::TextureBufferOutput(TextureBuffer *_buffer, const std::vector<u8> &_texture_map, u8 _depth_stencil)
: buffer(_buffer), texture_map(_texture_map), depth_stencil(_depth_stencil)
{}
TextureBufferOutput::~TextureBufferOutput()
{
if (render_target && driver)
driver->removeRenderTarget(render_target);
}
void TextureBufferOutput::activate(PipelineContext &context)
{
if (!driver)
driver = context.device->getVideoDriver();
if (!render_target)
render_target = driver->addRenderTarget();
core::array<video::ITexture *> textures;
core::dimension2du size(0, 0);
for (size_t i = 0; i < texture_map.size(); i++) {
video::ITexture *texture = buffer->getTexture(texture_map[i]);
textures.push_back(texture);
if (texture && size.Width == 0)
size = texture->getSize();
}
// Use legacy call when there's single texture without depth texture
// This binds default depth buffer to the FBO
if (textures.size() == 1 && depth_stencil == NO_DEPTH_TEXTURE) {
driver->setRenderTarget(textures[0], m_clear, m_clear, context.clear_color);
return;
}
video::ITexture *depth_texture = nullptr;
if (depth_stencil != NO_DEPTH_TEXTURE)
depth_texture = buffer->getTexture(depth_stencil);
render_target->setTexture(textures, depth_texture);
driver->setRenderTargetEx(render_target, m_clear ? video::ECBF_ALL : video::ECBF_NONE, context.clear_color);
driver->OnResize(size);
RenderTarget::activate(context);
}
u8 DynamicSource::getTextureCount()
{
assert(isConfigured());
return upstream->getTextureCount();
}
video::ITexture *DynamicSource::getTexture(u8 index)
{
assert(isConfigured());
return upstream->getTexture(index);
}
void ScreenTarget::activate(PipelineContext &context)
{
auto driver = context.device->getVideoDriver();
driver->setRenderTarget(nullptr, m_clear, m_clear, context.clear_color);
driver->OnResize(size);
RenderTarget::activate(context);
}
void DynamicTarget::activate(PipelineContext &context)
{
if (!isConfigured())
throw std::logic_error("Dynamic render target is not configured before activation.");
upstream->activate(context);
}
void ScreenTarget::reset(PipelineContext &context)
{
RenderTarget::reset(context);
size = context.device->getVideoDriver()->getScreenSize();
}
SetRenderTargetStep::SetRenderTargetStep(RenderStep *_step, RenderTarget *_target)
: step(_step), target(_target)
{
}
void SetRenderTargetStep::run(PipelineContext &context)
{
step->setRenderTarget(target);
}
SwapTexturesStep::SwapTexturesStep(TextureBuffer *_buffer, u8 _texture_a, u8 _texture_b)
: buffer(_buffer), texture_a(_texture_a), texture_b(_texture_b)
{
}
void SwapTexturesStep::run(PipelineContext &context)
{
buffer->swapTextures(texture_a, texture_b);
}
RenderSource *RenderPipeline::getInput()
{
return &m_input;
}
RenderTarget *RenderPipeline::getOutput()
{
return &m_output;
}
void RenderPipeline::run(PipelineContext &context)
{
v2u32 original_size = context.target_size;
context.target_size = v2u32(original_size.X * scale.X, original_size.Y * scale.Y);
for (auto &object : m_objects)
object->reset(context);
for (auto &step: m_pipeline)
step->run(context);
context.target_size = original_size;
}
void RenderPipeline::setRenderSource(RenderSource *source)
{
m_input.setRenderSource(source);
}
void RenderPipeline::setRenderTarget(RenderTarget *target)
{
m_output.setRenderTarget(target);
}