PostProcessing.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2014 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <map>
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "Common/CommonTypes.h"
  9. #include "Common/MathUtil.h"
  10. #include "Common/Timer.h"
  11. #include "VideoCommon/TextureConfig.h"
  12. class AbstractPipeline;
  13. class AbstractShader;
  14. class AbstractTexture;
  15. class AbstractFramebuffer;
  16. namespace VideoCommon
  17. {
  18. class PostProcessingConfiguration
  19. {
  20. public:
  21. // User defined post process
  22. struct ConfigurationOption
  23. {
  24. enum class OptionType
  25. {
  26. Bool = 0,
  27. Float,
  28. Integer,
  29. };
  30. bool m_bool_value = false;
  31. std::vector<float> m_float_values;
  32. std::vector<s32> m_integer_values;
  33. std::vector<float> m_float_min_values;
  34. std::vector<s32> m_integer_min_values;
  35. std::vector<float> m_float_max_values;
  36. std::vector<s32> m_integer_max_values;
  37. std::vector<float> m_float_step_values;
  38. std::vector<s32> m_integer_step_values;
  39. OptionType m_type = OptionType::Bool;
  40. std::string m_gui_name;
  41. std::string m_option_name;
  42. std::string m_dependent_option;
  43. bool m_dirty = false;
  44. };
  45. using ConfigMap = std::map<std::string, ConfigurationOption>;
  46. PostProcessingConfiguration();
  47. virtual ~PostProcessingConfiguration();
  48. // Loads the configuration with a shader
  49. // If the argument is "" the class will load the shader from the g_activeConfig option.
  50. // Returns the loaded shader source from file
  51. void LoadShader(const std::string& shader);
  52. void LoadDefaultShader();
  53. void SaveOptionsConfiguration();
  54. const std::string& GetShader() const { return m_current_shader; }
  55. const std::string& GetShaderCode() const { return m_current_shader_code; }
  56. bool IsDirty() const { return m_any_options_dirty; }
  57. void SetDirty(bool dirty) { m_any_options_dirty = dirty; }
  58. bool HasOptions() const { return m_options.size() > 0; }
  59. const ConfigMap& GetOptions() const { return m_options; }
  60. ConfigMap& GetOptions() { return m_options; }
  61. const ConfigurationOption& GetOption(const std::string& option) { return m_options[option]; }
  62. // For updating option's values
  63. void SetOptionf(const std::string& option, int index, float value);
  64. void SetOptioni(const std::string& option, int index, s32 value);
  65. void SetOptionb(const std::string& option, bool value);
  66. private:
  67. bool m_any_options_dirty = false;
  68. std::string m_current_shader;
  69. std::string m_current_shader_code;
  70. ConfigMap m_options;
  71. void LoadOptions(const std::string& code);
  72. void LoadOptionsConfiguration();
  73. };
  74. class PostProcessing
  75. {
  76. public:
  77. PostProcessing();
  78. virtual ~PostProcessing();
  79. static std::vector<std::string> GetShaderList();
  80. static std::vector<std::string> GetPassiveShaderList();
  81. static std::vector<std::string> GetAnaglyphShaderList();
  82. PostProcessingConfiguration* GetConfig() { return &m_config; }
  83. bool Initialize(AbstractTextureFormat format);
  84. void RecompileShader();
  85. void RecompilePipeline();
  86. void BlitFromTexture(const MathUtil::Rectangle<int>& dst, const MathUtil::Rectangle<int>& src,
  87. const AbstractTexture* src_tex, int src_layer = -1);
  88. bool IsColorCorrectionActive() const;
  89. bool NeedsIntermediaryBuffer() const;
  90. protected:
  91. std::string GetUniformBufferHeader(bool user_post_process) const;
  92. std::string GetHeader(bool user_post_process) const;
  93. std::string GetFooter() const;
  94. bool CompileVertexShader();
  95. bool CompilePixelShader();
  96. bool CompilePipeline();
  97. size_t CalculateUniformsSize(bool user_post_process) const;
  98. void FillUniformBuffer(const MathUtil::Rectangle<int>& src, const AbstractTexture* src_tex,
  99. int src_layer, const MathUtil::Rectangle<int>& dst,
  100. const MathUtil::Rectangle<int>& wnd, u8* buffer, bool user_post_process,
  101. bool intermediary_buffer);
  102. // Timer for determining our time value
  103. Common::Timer m_timer;
  104. // Dolphin fixed post process:
  105. PostProcessingConfiguration::ConfigMap m_default_options;
  106. std::unique_ptr<AbstractShader> m_default_vertex_shader;
  107. std::unique_ptr<AbstractShader> m_default_pixel_shader;
  108. std::unique_ptr<AbstractPipeline> m_default_pipeline;
  109. std::unique_ptr<AbstractFramebuffer> m_intermediary_frame_buffer;
  110. std::unique_ptr<AbstractTexture> m_intermediary_color_texture;
  111. std::vector<u8> m_default_uniform_staging_buffer;
  112. // User post process:
  113. PostProcessingConfiguration m_config;
  114. std::unique_ptr<AbstractShader> m_vertex_shader;
  115. std::unique_ptr<AbstractShader> m_pixel_shader;
  116. std::unique_ptr<AbstractPipeline> m_pipeline;
  117. std::vector<u8> m_uniform_staging_buffer;
  118. AbstractTextureFormat m_framebuffer_format = AbstractTextureFormat::Undefined;
  119. };
  120. } // namespace VideoCommon