-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderPresetGL.cpp
465 lines (389 loc) · 13.6 KB
/
ShaderPresetGL.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Copyright (C) 2019 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "ShaderPresetGL.h"
#include "ServiceBroker.h"
#include "ShaderUtilsGL.h"
#include "cores/RetroPlayer/rendering/RenderContext.h"
#include "cores/RetroPlayer/shaders/ShaderPresetFactory.h"
#include "cores/RetroPlayer/shaders/ShaderUtils.h"
#include "cores/RetroPlayer/shaders/gl/ShaderGL.h"
#include "cores/RetroPlayer/shaders/gl/ShaderLutGL.h"
#include "rendering/gl/RenderSystemGL.h"
#include "utils/log.h"
#include <regex>
#define MAX_FLOAT 3.402823466E+38
using namespace KODI;
using namespace SHADER;
CShaderPresetGL::CShaderPresetGL(RETRO::CRenderContext& context,
unsigned int videoWidth,
unsigned int videoHeight)
: m_context(context), m_videoSize(videoWidth, videoHeight)
{
//m_textureSize = CShaderUtils::GetOptimalTextureSize(m_videoSize);
CRect viewPort;
m_context.GetViewPort(viewPort);
m_outputSize = {viewPort.Width(), viewPort.Height()};
}
CShaderPresetGL::~CShaderPresetGL()
{
DisposeShaders();
// The gui is going to render after this, so apply the state required
m_context.ApplyStateBlock();
}
ShaderParameterMap CShaderPresetGL::GetShaderParameters(
const std::vector<ShaderParameter>& parameters, const std::string& sourceStr) const
{
static const std::regex pragmaParamRegex("#pragma parameter ([a-zA-Z_][a-zA-Z0-9_]*)");
std::smatch matches;
std::vector<std::string> validParams;
std::string::const_iterator searchStart(sourceStr.cbegin());
while (regex_search(searchStart, sourceStr.cend(), matches, pragmaParamRegex))
{
validParams.push_back(matches[1].str());
searchStart += matches.position() + matches.length();
}
ShaderParameterMap matchParams;
// For each param found in the source code
for (const auto& match : validParams)
{
// For each param found in the preset file
for (const auto& parameter : parameters)
{
// Check if they match
if (match == parameter.strId)
{
// The add-on has already handled parsing and overwriting default
// parameter values from the preset file. The final value we
// should use is in the 'current' field.
matchParams[match] = parameter.current;
break;
}
}
}
return matchParams;
}
bool CShaderPresetGL::RenderUpdate(const CPoint* dest,
IShaderTexture* source,
IShaderTexture* target)
{
// Save the viewport
CRect viewPort;
m_context.GetViewPort(viewPort);
// Handle resizing of the viewport (window)
UpdateViewPort(viewPort);
// Update shaders/shader textures if required
if (!Update())
return false;
PrepareParameters(target, dest);
IShader* firstShader = m_pShaders.front().get();
CShaderTextureGL* firstShaderTexture = m_pShaderTextures.front().get();
IShader* lastShader = m_pShaders.back().get();
int screenWidth = m_context.GetScreenWidth();
int screenHeight = m_context.GetScreenHeight();
const unsigned passesNum = static_cast<unsigned int>(m_pShaderTextures.size());
if (passesNum == 1) {
firstShader->PrepareParameters(m_dest, true, static_cast<uint64_t>(m_frameCount));
firstShader->Render(source, target);
}
else if (passesNum == 2)
{
// Initialize FBO
firstShaderTexture->CreateFBO(screenWidth, screenHeight);
// Apply first pass
firstShaderTexture->BindFBO();
firstShader->PrepareParameters(m_dest, false, static_cast<uint64_t>(m_frameCount));
RenderShader(firstShader, source, target);
firstShaderTexture->UnbindFBO();
// Apply last pass
lastShader->PrepareParameters(m_dest, true, static_cast<uint64_t>(m_frameCount));
RenderShader(lastShader, firstShaderTexture, target);
}
else
{
// Initialize FBO
firstShaderTexture->CreateFBO(screenWidth, screenHeight);
// Apply first pass
firstShaderTexture->BindFBO();
firstShader->PrepareParameters(m_dest, false, static_cast<uint64_t>(m_frameCount));
RenderShader(firstShader, source, target);
firstShaderTexture->UnbindFBO();
// Apply all passes except the first and last one (which needs to be applied to the backbuffer)
for (unsigned int shaderIdx = 1; shaderIdx < static_cast<unsigned int>(m_pShaders.size()) - 1;
++shaderIdx)
{
IShader* shader = m_pShaders[shaderIdx].get();
CShaderTextureGL* prevTexture = m_pShaderTextures[shaderIdx - 1].get();
CShaderTextureGL* texture = m_pShaderTextures[shaderIdx].get();
texture->CreateFBO(screenWidth, screenHeight);
texture->BindFBO();
shader->PrepareParameters(m_dest, false, static_cast<uint64_t>(m_frameCount));
RenderShader(shader, prevTexture,
target); // The target on each call is only used for setting the viewport
texture->UnbindFBO();
}
// TODO: Remove last texture, useless
// Apply last pass
CShaderTextureGL* secToLastTexture = m_pShaderTextures[m_pShaderTextures.size() - 2].get();
lastShader->PrepareParameters(m_dest, true, static_cast<uint64_t>(m_frameCount));
RenderShader(lastShader, secToLastTexture, target);
}
m_frameCount += static_cast<float>(m_speed);
// Restore our view port.
m_context.SetViewPort(viewPort);
return true;
}
bool CShaderPresetGL::Update()
{
auto updateFailed = [this](const std::string& msg)
{
m_failedPaths.insert(m_presetPath);
CLog::Log(LOGWARNING, "CShaderPresetGL::Update: {}. Disabling video shaders.", msg);
DisposeShaders();
return false;
};
if (m_bPresetNeedsUpdate && !HasPathFailed(m_presetPath))
{
DisposeShaders();
if (m_presetPath.empty())
return false;
if (!ReadPresetFile(m_presetPath))
{
CLog::LogF(LOGERROR, "Couldn't load shader preset {} or the shaders it references",
m_presetPath);
return false;
}
if (!CreateShaders())
return updateFailed("Failed to initialize shaders");
if (!CreateBuffers())
return updateFailed("Failed to initialize buffers");
if (!CreateShaderTextures())
return updateFailed("A shader texture failed to init");
}
if (m_pShaders.empty())
return false;
// Each pass must have its own texture and the opposite is also true
if (m_pShaders.size() != m_pShaderTextures.size())
return updateFailed("A shader or texture failed to init");
m_bPresetNeedsUpdate = false;
return true;
}
void CShaderPresetGL::SetVideoSize(const unsigned videoWidth, const unsigned videoHeight)
{
if (videoWidth != m_videoSize.x || videoHeight != m_videoSize.y) {
m_videoSize = {videoWidth, videoHeight};
//m_textureSize = CShaderUtils::GetOptimalTextureSize(m_videoSize);
m_bPresetNeedsUpdate = true;
}
}
bool CShaderPresetGL::SetShaderPreset(const std::string& shaderPresetPath)
{
m_bPresetNeedsUpdate = true;
m_presetPath = shaderPresetPath;
return Update();
}
const std::string& CShaderPresetGL::GetShaderPreset() const
{
return m_presetPath;
}
bool CShaderPresetGL::CreateShaderTextures()
{
float2 prevSize = m_videoSize;
unsigned int numPasses = static_cast<unsigned int>(m_passes.size());
for (unsigned shaderIdx = 0; shaderIdx < numPasses; ++shaderIdx)
{
ShaderPass& pass = m_passes[shaderIdx];
// resolve final texture resolution, taking scale type and scale multiplier into account
float2 scaledSize;
switch (pass.fbo.scaleX.type)
{
case SCALE_TYPE_ABSOLUTE:
scaledSize.x = static_cast<float>(pass.fbo.scaleX.abs);
break;
case SCALE_TYPE_VIEWPORT:
scaledSize.x = m_outputSize.x;
break;
case SCALE_TYPE_INPUT:
default:
scaledSize.x = prevSize.x;
break;
}
switch (pass.fbo.scaleY.type)
{
case SCALE_TYPE_ABSOLUTE:
scaledSize.y = static_cast<float>(pass.fbo.scaleY.abs);
break;
case SCALE_TYPE_VIEWPORT:
scaledSize.y = m_outputSize.y;
break;
case SCALE_TYPE_INPUT:
default:
scaledSize.y = prevSize.y;
break;
}
// if the scale was unspecified
if (pass.fbo.scaleX.scale == 0 && pass.fbo.scaleY.scale == 0)
{
// if the last shader has the scale unspecified
if (shaderIdx == numPasses - 1)
{
// we're supposed to output at full (viewport) res
scaledSize.x = m_outputSize.x;
scaledSize.y = m_outputSize.y;
}
}
else
{
scaledSize.x *= pass.fbo.scaleX.scale;
scaledSize.y *= pass.fbo.scaleY.scale;
}
// Determine the framebuffer data format
unsigned int textureFormat;
if (pass.fbo.floatFramebuffer)
{
// Give priority to float framebuffer parameter (we can't use both float and sRGB)
textureFormat = GL_RGB32F;
}
else
{
if (pass.fbo.sRgbFramebuffer)
textureFormat = GL_SRGB8;
else
textureFormat = GL_RGBA;
}
auto textureGL = new CGLTexture(static_cast<unsigned int>(scaledSize.x),
static_cast<unsigned int>(scaledSize.y),
static_cast<XB_FMT>(textureFormat)); //! @todo Format translation?
textureGL->CreateTextureObject();
if (textureGL->getMTexture() <= 0)
{
CLog::Log(LOGERROR, "Couldn't create a texture for video shader {}.", pass.sourcePath);
return false;
}
auto wrapType = CShaderUtilsGL::TranslateWrapType(WRAP_TYPE_BORDER);
glBindTexture(GL_TEXTURE_2D, textureGL->getMTexture());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapType);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapType);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, wrapType);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_NEVER);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0.0);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, MAX_FLOAT);
#ifndef HAS_GLES
GLfloat blackBorder[4] = {0.0f, 0.0f, 0.0f, 0.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, blackBorder);
#endif
m_pShaderTextures.emplace_back(new CShaderTextureGL(*textureGL));
m_pShaders[shaderIdx]->SetSizes(prevSize, scaledSize);
m_pShaders[shaderIdx]->UpdateMVP();
prevSize = scaledSize;
}
return true;
}
bool CShaderPresetGL::CreateShaders()
{
auto numPasses = m_passes.size();
//m_textureSize = CShaderUtils::GetOptimalTextureSize(m_videoSize);
ShaderLutVec passLUTsGL;
for (unsigned shaderIdx = 0; shaderIdx < numPasses; ++shaderIdx)
{
const auto& pass = m_passes[shaderIdx];
for (unsigned i = 0; i < pass.luts.size(); ++i)
{
auto& lutStruct = pass.luts[i];
ShaderLutPtr passLut(new CShaderLutGL(lutStruct.strId, lutStruct.path));
if (passLut->Create(m_context, lutStruct))
passLUTsGL.emplace_back(std::move(passLut));
}
std::unique_ptr<CShaderGL> videoShader(new CShaderGL(m_context));
auto shaderSource = pass.vertexSource; //also contains fragment source
auto shaderPath = pass.sourcePath;
// Get only the parameters belonging to this specific shader
ShaderParameterMap passParameters = GetShaderParameters(pass.parameters, pass.vertexSource);
if (!videoShader->Create(shaderSource, shaderPath, passParameters, nullptr, passLUTsGL,
m_outputSize, pass.frameCountMod))
{
CLog::Log(LOGERROR, "Couldn't create a video shader");
return false;
}
m_pShaders.push_back(std::move(videoShader));
}
return true;
}
bool CShaderPresetGL::CreateBuffers()
{
for (auto& videoShader : m_pShaders)
videoShader->CreateInputBuffer();
return true;
}
void CShaderPresetGL::UpdateViewPort()
{
CRect viewPort;
m_context.GetViewPort(viewPort);
UpdateViewPort(viewPort);
}
void CShaderPresetGL::UpdateViewPort(CRect viewPort)
{
float2 currentViewPortSize = {viewPort.Width(), viewPort.Height()};
if (currentViewPortSize != m_outputSize)
{
m_outputSize = currentViewPortSize;
m_bPresetNeedsUpdate = true;
Update();
}
}
void CShaderPresetGL::UpdateMVPs()
{
for (auto& videoShader : m_pShaders)
videoShader->UpdateMVP();
}
void CShaderPresetGL::DisposeShaders()
{
m_pShaders.clear();
m_pShaderTextures.clear();
m_passes.clear();
m_bPresetNeedsUpdate = true;
}
void CShaderPresetGL::PrepareParameters(const IShaderTexture* texture, const CPoint* dest)
{
if (m_dest[0] != dest[0] || m_dest[1] != dest[1] || m_dest[2] != dest[2] ||
m_dest[3] != dest[3] || texture->GetWidth() != m_outputSize.x ||
texture->GetHeight() != m_outputSize.y)
{
for (size_t i = 0; i < 4; ++i)
m_dest[i] = dest[i];
m_outputSize = {texture->GetWidth(), texture->GetHeight()};
UpdateMVPs();
UpdateViewPort();
}
}
void CShaderPresetGL::RenderShader(IShader* shader,
IShaderTexture* source,
IShaderTexture* target) const
{
CRect newViewPort(0.f, 0.f, target->GetWidth(), target->GetHeight());
m_context.SetViewPort(newViewPort);
m_context.SetScissors(newViewPort);
shader->Render(source, target);
}
bool CShaderPresetGL::ReadPresetFile(const std::string& presetPath)
{
return CServiceBroker::GetGameServices().VideoShaders().LoadPreset(presetPath, *this);
}
void CShaderPresetGL::SetSpeed(double speed)
{
m_speed = speed;
}
ShaderPassVec& CShaderPresetGL::GetPasses()
{
return m_passes;
}
bool CShaderPresetGL::HasPathFailed(const std::string& path) const
{
return m_failedPaths.find(path) != m_failedPaths.end();
}