duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

postprocessing_shader_fx.h (3872B)


      1 // SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
      3 
      4 #pragma once
      5 
      6 #include "postprocessing_shader.h"
      7 
      8 #include "common/thirdparty/SmallVector.h"
      9 #include "common/timer.h"
     10 
     11 // reshadefx
     12 #include "effect_module.hpp"
     13 
     14 #include <random>
     15 
     16 class Error;
     17 
     18 namespace PostProcessing {
     19 
     20 class ReShadeFXShader final : public Shader
     21 {
     22 public:
     23   ReShadeFXShader();
     24   ~ReShadeFXShader();
     25 
     26   bool IsValid() const override;
     27   bool WantsDepthBuffer() const override;
     28 
     29   bool LoadFromFile(std::string name, std::string filename, bool only_config, Error* error);
     30   bool LoadFromString(std::string name, std::string filename, std::string code, bool only_config, Error* error);
     31 
     32   bool ResizeOutput(GPUTexture::Format format, u32 width, u32 height) override;
     33   bool CompilePipeline(GPUTexture::Format format, u32 width, u32 height, ProgressCallback* progress) override;
     34   bool Apply(GPUTexture* input_color, GPUTexture* input_depth, GPUTexture* final_target, GSVector4i final_rect,
     35              s32 orig_width, s32 orig_height, s32 native_width, s32 native_height, u32 target_width,
     36              u32 target_height) override;
     37 
     38 private:
     39   using TextureID = s32;
     40 
     41   static constexpr TextureID INPUT_COLOR_TEXTURE = -1;
     42   static constexpr TextureID INPUT_DEPTH_TEXTURE = -2;
     43   static constexpr TextureID OUTPUT_COLOR_TEXTURE = -3;
     44 
     45   enum class SourceOptionType
     46   {
     47     None,
     48     Zero,
     49     HasDepth,
     50     Timer,
     51     FrameTime,
     52     FrameCount,
     53     FrameCountF,
     54     PingPong,
     55     MousePoint,
     56     Random,
     57     RandomF,
     58     BufferWidth,
     59     BufferHeight,
     60     BufferWidthF,
     61     BufferHeightF,
     62     InternalWidth,
     63     InternalHeight,
     64     InternalWidthF,
     65     InternalHeightF,
     66     NativeWidth,
     67     NativeHeight,
     68     NativeWidthF,
     69     NativeHeightF,
     70     UpscaleMultiplier,
     71     ViewportX,
     72     ViewportY,
     73     ViewportWidth,
     74     ViewportHeight,
     75     ViewportOffset,
     76     ViewportSize,
     77     InternalPixelSize,
     78     InternalNormPixelSize,
     79     NativePixelSize,
     80     NativeNormPixelSize,
     81     BufferToViewportRatio,
     82 
     83     MaxCount
     84   };
     85 
     86   struct SourceOption
     87   {
     88     SourceOptionType source;
     89     u32 offset;
     90     float min;
     91     float max;
     92     float smoothing;
     93     std::array<float, 2> step;
     94     ShaderOption::ValueVector value;
     95   };
     96 
     97   bool CreateModule(s32 buffer_width, s32 buffer_height, reshadefx::module* mod, std::string code, Error* error);
     98   bool CreateOptions(const reshadefx::module& mod, Error* error);
     99   bool GetSourceOption(const reshadefx::uniform_info& ui, SourceOptionType* si, Error* error);
    100   bool CreatePasses(GPUTexture::Format backbuffer_format, reshadefx::module& mod, Error* error);
    101 
    102   const char* GetTextureNameForID(TextureID id) const;
    103   GPUTexture* GetTextureByID(TextureID id, GPUTexture* input_color, GPUTexture* input_depth,
    104                              GPUTexture* final_target) const;
    105 
    106   std::string m_filename;
    107 
    108   struct Texture
    109   {
    110     std::unique_ptr<GPUTexture> texture;
    111     std::string reshade_name; // TODO: we might be able to drop this
    112     GPUTexture::Format format;
    113     float rt_scale;
    114   };
    115 
    116   struct Sampler
    117   {
    118     u32 slot;
    119     TextureID texture_id;
    120     std::string reshade_name;
    121     GPUSampler* sampler;
    122   };
    123 
    124   struct Pass
    125   {
    126     std::unique_ptr<GPUPipeline> pipeline;
    127     llvm::SmallVector<TextureID, GPUDevice::MAX_RENDER_TARGETS> render_targets;
    128     llvm::SmallVector<Sampler, GPUDevice::MAX_TEXTURE_SAMPLERS> samplers;
    129     u32 num_vertices;
    130 
    131 #ifdef _DEBUG
    132     std::string name;
    133 #endif
    134   };
    135 
    136   std::vector<Pass> m_passes;
    137   std::vector<Texture> m_textures;
    138   std::vector<SourceOption> m_source_options;
    139   u32 m_uniforms_size = 0;
    140   bool m_valid = false;
    141   bool m_wants_depth_buffer = false;
    142 
    143   Common::Timer m_frame_timer;
    144   u32 m_frame_count = 0;
    145 
    146   // Specifically using a fixed seed, so that it's consistent from run-to-run.
    147   std::mt19937 m_random{0x1337};
    148 };
    149 
    150 } // namespace PostProcessing