summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/OGL/PostProcessing.cpp
blob: 928480567bd1e43626d203ae58491413d18ab02e (plain)
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
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "VideoBackends/OGL/PostProcessing.h"

#include "Common/CommonTypes.h"
#include "Common/GL/GLUtil.h"
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"

#include "VideoBackends/OGL/FramebufferManager.h"
#include "VideoBackends/OGL/ProgramShaderCache.h"
#include "VideoBackends/OGL/SamplerCache.h"

#include "VideoCommon/VideoCommon.h"
#include "VideoCommon/VideoConfig.h"

namespace OGL
{
static const char s_vertex_shader[] = "out vec2 uv0;\n"
                                      "uniform vec4 src_rect;\n"
                                      "void main(void) {\n"
                                      "	vec2 rawpos = vec2(gl_VertexID&1, gl_VertexID&2);\n"
                                      "	gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
                                      "	uv0 = rawpos * src_rect.zw + src_rect.xy;\n"
                                      "}\n";

OpenGLPostProcessing::OpenGLPostProcessing() : m_initialized(false)
{
  CreateHeader();
}

OpenGLPostProcessing::~OpenGLPostProcessing()
{
  m_shader.Destroy();
}

void OpenGLPostProcessing::BlitFromTexture(TargetRectangle src, TargetRectangle dst,
                                           int src_texture, int src_width, int src_height,
                                           int layer)
{
  ApplyShader();

  glViewport(dst.left, dst.bottom, dst.GetWidth(), dst.GetHeight());

  OpenGL_BindAttributelessVAO();

  m_shader.Bind();

  glUniform4f(m_uniform_resolution, (float)src_width, (float)src_height, 1.0f / (float)src_width,
              1.0f / (float)src_height);
  glUniform4f(m_uniform_src_rect, src.left / (float)src_width, src.bottom / (float)src_height,
              src.GetWidth() / (float)src_width, src.GetHeight() / (float)src_height);
  glUniform1ui(m_uniform_time, (GLuint)m_timer.GetTimeElapsed());
  glUniform1i(m_uniform_layer, layer);

  if (m_config.IsDirty())
  {
    for (auto& it : m_config.GetOptions())
    {
      if (it.second.m_dirty)
      {
        switch (it.second.m_type)
        {
        case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_BOOL:
          glUniform1i(m_uniform_bindings[it.first], it.second.m_bool_value);
          break;
        case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_INTEGER:
          switch (it.second.m_integer_values.size())
          {
          case 1:
            glUniform1i(m_uniform_bindings[it.first], it.second.m_integer_values[0]);
            break;
          case 2:
            glUniform2i(m_uniform_bindings[it.first], it.second.m_integer_values[0],
                        it.second.m_integer_values[1]);
            break;
          case 3:
            glUniform3i(m_uniform_bindings[it.first], it.second.m_integer_values[0],
                        it.second.m_integer_values[1], it.second.m_integer_values[2]);
            break;
          case 4:
            glUniform4i(m_uniform_bindings[it.first], it.second.m_integer_values[0],
                        it.second.m_integer_values[1], it.second.m_integer_values[2],
                        it.second.m_integer_values[3]);
            break;
          }
          break;
        case PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_FLOAT:
          switch (it.second.m_float_values.size())
          {
          case 1:
            glUniform1f(m_uniform_bindings[it.first], it.second.m_float_values[0]);
            break;
          case 2:
            glUniform2f(m_uniform_bindings[it.first], it.second.m_float_values[0],
                        it.second.m_float_values[1]);
            break;
          case 3:
            glUniform3f(m_uniform_bindings[it.first], it.second.m_float_values[0],
                        it.second.m_float_values[1], it.second.m_float_values[2]);
            break;
          case 4:
            glUniform4f(m_uniform_bindings[it.first], it.second.m_float_values[0],
                        it.second.m_float_values[1], it.second.m_float_values[2],
                        it.second.m_float_values[3]);
            break;
          }
          break;
        }
        it.second.m_dirty = false;
      }
    }
    m_config.SetDirty(false);
  }

  glActiveTexture(GL_TEXTURE9);
  glBindTexture(GL_TEXTURE_2D_ARRAY, src_texture);
  g_sampler_cache->BindLinearSampler(9);
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

void OpenGLPostProcessing::ApplyShader()
{
  // shader didn't changed
  if (m_initialized && m_config.GetShader() == g_ActiveConfig.sPostProcessingShader)
    return;

  m_shader.Destroy();
  m_uniform_bindings.clear();

  // load shader code
  std::string main_code = m_config.LoadShader();
  std::string options_code = LoadShaderOptions();
  std::string code = m_glsl_header + options_code + main_code;

  // and compile it
  if (!ProgramShaderCache::CompileShader(m_shader, s_vertex_shader, code))
  {
    ERROR_LOG(VIDEO, "Failed to compile post-processing shader %s", m_config.GetShader().c_str());
    g_ActiveConfig.sPostProcessingShader.clear();
    code = m_config.LoadShader();
    ProgramShaderCache::CompileShader(m_shader, s_vertex_shader, code);
  }

  // read uniform locations
  m_uniform_resolution = glGetUniformLocation(m_shader.glprogid, "resolution");
  m_uniform_time = glGetUniformLocation(m_shader.glprogid, "time");
  m_uniform_src_rect = glGetUniformLocation(m_shader.glprogid, "src_rect");
  m_uniform_layer = glGetUniformLocation(m_shader.glprogid, "layer");

  for (const auto& it : m_config.GetOptions())
  {
    std::string glsl_name = "options." + it.first;
    m_uniform_bindings[it.first] = glGetUniformLocation(m_shader.glprogid, glsl_name.c_str());
  }
  m_initialized = true;
}

void OpenGLPostProcessing::CreateHeader()
{
  m_glsl_header =
      // Required variables
      // Shouldn't be accessed directly by the PP shader
      // Texture sampler
      "SAMPLER_BINDING(8) uniform sampler2D samp8;\n"
      "SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"

      // Output variable
      "out float4 ocol0;\n"
      // Input coordinates
      "in float2 uv0;\n"
      // Resolution
      "uniform float4 resolution;\n"
      // Time
      "uniform uint time;\n"
      // Layer
      "uniform int layer;\n"

      // Interfacing functions
      "float4 Sample()\n"
      "{\n"
      "\treturn texture(samp9, float3(uv0, layer));\n"
      "}\n"

      "float4 SampleLocation(float2 location)\n"
      "{\n"
      "\treturn texture(samp9, float3(location, layer));\n"
      "}\n"

      "float4 SampleLayer(int layer)\n"
      "{\n"
      "\treturn texture(samp9, float3(uv0, layer));\n"
      "}\n"

      "#define SampleOffset(offset) textureOffset(samp9, float3(uv0, layer), offset)\n"

      "float4 SampleFontLocation(float2 location)\n"
      "{\n"
      "\treturn texture(samp8, location);\n"
      "}\n"

      "float2 GetResolution()\n"
      "{\n"
      "\treturn resolution.xy;\n"
      "}\n"

      "float2 GetInvResolution()\n"
      "{\n"
      "\treturn resolution.zw;\n"
      "}\n"

      "float2 GetCoordinates()\n"
      "{\n"
      "\treturn uv0;\n"
      "}\n"

      "uint GetTime()\n"
      "{\n"
      "\treturn time;\n"
      "}\n"

      "void SetOutput(float4 color)\n"
      "{\n"
      "\tocol0 = color;\n"
      "}\n"

      "#define GetOption(x) (options.x)\n"
      "#define OptionEnabled(x) (options.x != 0)\n";
}

std::string OpenGLPostProcessing::LoadShaderOptions()
{
  m_uniform_bindings.clear();
  if (m_config.GetOptions().empty())
    return "";

  std::string glsl_options = "struct Options\n{\n";

  for (const auto& it : m_config.GetOptions())
  {
    if (it.second.m_type ==
        PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_BOOL)
    {
      glsl_options += StringFromFormat("int     %s;\n", it.first.c_str());
    }
    else if (it.second.m_type ==
             PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_INTEGER)
    {
      u32 count = static_cast<u32>(it.second.m_integer_values.size());
      if (count == 1)
        glsl_options += StringFromFormat("int     %s;\n", it.first.c_str());
      else
        glsl_options += StringFromFormat("int%d   %s;\n", count, it.first.c_str());
    }
    else if (it.second.m_type ==
             PostProcessingShaderConfiguration::ConfigurationOption::OptionType::OPTION_FLOAT)
    {
      u32 count = static_cast<u32>(it.second.m_float_values.size());
      if (count == 1)
        glsl_options += StringFromFormat("float   %s;\n", it.first.c_str());
      else
        glsl_options += StringFromFormat("float%d %s;\n", count, it.first.c_str());
    }

    m_uniform_bindings[it.first] = 0;
  }

  glsl_options += "};\n";
  glsl_options += "uniform Options options;\n";

  return glsl_options;
}

}  // namespace OGL