2018-02-24 15:15:35 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
2018-02-27 09:38:00 +00:00
|
|
|
#include <cstring>
|
2018-02-24 15:15:35 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2018-03-11 04:30:48 +00:00
|
|
|
#include "Common/File.h"
|
2018-02-24 15:15:35 +00:00
|
|
|
#include "Common/LinearDiskCache.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/AbstractPipeline.h"
|
|
|
|
#include "VideoCommon/AbstractShader.h"
|
|
|
|
#include "VideoCommon/AsyncShaderCompiler.h"
|
2018-03-01 09:21:06 +00:00
|
|
|
#include "VideoCommon/GXPipelineTypes.h"
|
2018-02-24 15:15:35 +00:00
|
|
|
#include "VideoCommon/GeometryShaderGen.h"
|
|
|
|
#include "VideoCommon/PixelShaderGen.h"
|
|
|
|
#include "VideoCommon/RenderState.h"
|
|
|
|
#include "VideoCommon/UberShaderPixel.h"
|
|
|
|
#include "VideoCommon/UberShaderVertex.h"
|
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
|
|
|
|
|
|
class NativeVertexFormat;
|
2018-07-17 03:24:36 +00:00
|
|
|
enum class AbstractTextureFormat : u32;
|
2018-02-24 15:15:35 +00:00
|
|
|
|
|
|
|
namespace VideoCommon
|
|
|
|
{
|
|
|
|
class ShaderCache final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ShaderCache();
|
|
|
|
~ShaderCache();
|
|
|
|
|
|
|
|
// Perform at startup, create descriptor layouts, compiles all static shaders.
|
|
|
|
bool Initialize();
|
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
// Changes the shader host config. Shaders will be reloaded if there are changes.
|
|
|
|
void SetHostConfig(const ShaderHostConfig& host_config, u32 efb_multisamples);
|
|
|
|
|
|
|
|
// Reloads/recreates all shaders and pipelines.
|
|
|
|
void Reload();
|
|
|
|
|
|
|
|
// Retrieves all pending shaders/pipelines from the async compiler.
|
|
|
|
void RetrieveAsyncShaders();
|
|
|
|
|
|
|
|
// Get utility shader header based on current config.
|
|
|
|
std::string GetUtilityShaderHeader() const;
|
|
|
|
|
|
|
|
// Accesses ShaderGen shader caches
|
2018-03-01 09:21:06 +00:00
|
|
|
const AbstractPipeline* GetPipelineForUid(const GXPipelineUid& uid);
|
|
|
|
const AbstractPipeline* GetUberPipelineForUid(const GXUberPipelineUid& uid);
|
2018-02-24 15:15:35 +00:00
|
|
|
|
|
|
|
// Accesses ShaderGen shader caches asynchronously.
|
|
|
|
// The optional will be empty if this pipeline is now background compiling.
|
2018-03-01 09:21:06 +00:00
|
|
|
std::optional<const AbstractPipeline*> GetPipelineForUidAsync(const GXPipelineUid& uid);
|
2018-02-24 15:15:35 +00:00
|
|
|
|
|
|
|
private:
|
2018-03-01 09:21:06 +00:00
|
|
|
void WaitForAsyncCompiler();
|
2018-02-24 15:15:35 +00:00
|
|
|
void LoadShaderCaches();
|
|
|
|
void ClearShaderCaches();
|
|
|
|
void LoadPipelineUIDCache();
|
2018-03-11 04:30:48 +00:00
|
|
|
void ClosePipelineUIDCache();
|
2018-02-24 15:15:35 +00:00
|
|
|
void CompileMissingPipelines();
|
|
|
|
void InvalidateCachedPipelines();
|
|
|
|
void ClearPipelineCaches();
|
2018-03-11 04:24:45 +00:00
|
|
|
void QueueUberShaderPipelines();
|
2018-02-24 15:15:35 +00:00
|
|
|
|
|
|
|
// GX shader compiler methods
|
|
|
|
std::unique_ptr<AbstractShader> CompileVertexShader(const VertexShaderUid& uid) const;
|
|
|
|
std::unique_ptr<AbstractShader>
|
|
|
|
CompileVertexUberShader(const UberShader::VertexShaderUid& uid) const;
|
|
|
|
std::unique_ptr<AbstractShader> CompilePixelShader(const PixelShaderUid& uid) const;
|
|
|
|
std::unique_ptr<AbstractShader>
|
|
|
|
CompilePixelUberShader(const UberShader::PixelShaderUid& uid) const;
|
|
|
|
const AbstractShader* InsertVertexShader(const VertexShaderUid& uid,
|
|
|
|
std::unique_ptr<AbstractShader> shader);
|
|
|
|
const AbstractShader* InsertVertexUberShader(const UberShader::VertexShaderUid& uid,
|
|
|
|
std::unique_ptr<AbstractShader> shader);
|
|
|
|
const AbstractShader* InsertPixelShader(const PixelShaderUid& uid,
|
|
|
|
std::unique_ptr<AbstractShader> shader);
|
|
|
|
const AbstractShader* InsertPixelUberShader(const UberShader::PixelShaderUid& uid,
|
|
|
|
std::unique_ptr<AbstractShader> shader);
|
|
|
|
const AbstractShader* CreateGeometryShader(const GeometryShaderUid& uid);
|
|
|
|
bool NeedsGeometryShader(const GeometryShaderUid& uid) const;
|
|
|
|
|
|
|
|
// GX pipeline compiler methods
|
|
|
|
AbstractPipelineConfig
|
|
|
|
GetGXPipelineConfig(const NativeVertexFormat* vertex_format, const AbstractShader* vertex_shader,
|
|
|
|
const AbstractShader* geometry_shader, const AbstractShader* pixel_shader,
|
|
|
|
const RasterizationState& rasterization_state, const DepthState& depth_state,
|
|
|
|
const BlendingState& blending_state);
|
2018-03-01 09:21:06 +00:00
|
|
|
std::optional<AbstractPipelineConfig> GetGXPipelineConfig(const GXPipelineUid& uid);
|
|
|
|
std::optional<AbstractPipelineConfig> GetGXUberPipelineConfig(const GXUberPipelineUid& uid);
|
|
|
|
const AbstractPipeline* InsertGXPipeline(const GXPipelineUid& config,
|
2018-02-24 15:15:35 +00:00
|
|
|
std::unique_ptr<AbstractPipeline> pipeline);
|
2018-03-01 09:21:06 +00:00
|
|
|
const AbstractPipeline* InsertGXUberPipeline(const GXUberPipelineUid& config,
|
2018-02-24 15:15:35 +00:00
|
|
|
std::unique_ptr<AbstractPipeline> pipeline);
|
2018-03-11 04:30:48 +00:00
|
|
|
void AddSerializedGXPipelineUID(const SerializedGXPipelineUid& uid);
|
2018-03-01 09:21:06 +00:00
|
|
|
void AppendGXPipelineUID(const GXPipelineUid& config);
|
2018-02-24 15:15:35 +00:00
|
|
|
|
|
|
|
// ASync Compiler Methods
|
2018-03-16 12:48:56 +00:00
|
|
|
void QueueVertexShaderCompile(const VertexShaderUid& uid, u32 priority);
|
|
|
|
void QueueVertexUberShaderCompile(const UberShader::VertexShaderUid& uid, u32 priority);
|
|
|
|
void QueuePixelShaderCompile(const PixelShaderUid& uid, u32 priority);
|
|
|
|
void QueuePixelUberShaderCompile(const UberShader::PixelShaderUid& uid, u32 priority);
|
|
|
|
void QueuePipelineCompile(const GXPipelineUid& uid, u32 priority);
|
|
|
|
void QueueUberPipelineCompile(const GXUberPipelineUid& uid, u32 priority);
|
|
|
|
|
|
|
|
// Priorities for compiling. The lower the value, the sooner the pipeline is compiled.
|
|
|
|
// The shader cache is compiled last, as it is the least likely to be required. On demand
|
|
|
|
// shaders are always compiled before pending ubershaders, as we want to use the ubershader
|
|
|
|
// for as few frames as possible, otherwise we risk framerate drops.
|
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
COMPILE_PRIORITY_ONDEMAND_PIPELINE = 100,
|
|
|
|
COMPILE_PRIORITY_UBERSHADER_PIPELINE = 200,
|
|
|
|
COMPILE_PRIORITY_SHADERCACHE_PIPELINE = 300
|
|
|
|
};
|
2018-02-24 15:15:35 +00:00
|
|
|
|
|
|
|
// Configuration bits.
|
|
|
|
APIType m_api_type = APIType::Nothing;
|
|
|
|
ShaderHostConfig m_host_config = {};
|
2018-07-17 03:24:36 +00:00
|
|
|
AbstractTextureFormat m_efb_depth_format;
|
2018-02-24 15:15:35 +00:00
|
|
|
u32 m_efb_multisamples = 1;
|
|
|
|
std::unique_ptr<AsyncShaderCompiler> m_async_shader_compiler;
|
|
|
|
|
|
|
|
// GX Shader Caches
|
|
|
|
template <typename Uid>
|
|
|
|
struct ShaderModuleCache
|
|
|
|
{
|
|
|
|
struct Shader
|
|
|
|
{
|
|
|
|
std::unique_ptr<AbstractShader> shader;
|
|
|
|
bool pending;
|
|
|
|
};
|
|
|
|
std::map<Uid, Shader> shader_map;
|
|
|
|
LinearDiskCache<Uid, u8> disk_cache;
|
|
|
|
};
|
|
|
|
ShaderModuleCache<VertexShaderUid> m_vs_cache;
|
|
|
|
ShaderModuleCache<GeometryShaderUid> m_gs_cache;
|
|
|
|
ShaderModuleCache<PixelShaderUid> m_ps_cache;
|
|
|
|
ShaderModuleCache<UberShader::VertexShaderUid> m_uber_vs_cache;
|
|
|
|
ShaderModuleCache<UberShader::PixelShaderUid> m_uber_ps_cache;
|
|
|
|
|
|
|
|
// GX Pipeline Caches - .first - pipeline, .second - pending
|
2018-03-01 09:21:06 +00:00
|
|
|
std::map<GXPipelineUid, std::pair<std::unique_ptr<AbstractPipeline>, bool>> m_gx_pipeline_cache;
|
|
|
|
std::map<GXUberPipelineUid, std::pair<std::unique_ptr<AbstractPipeline>, bool>>
|
2018-02-24 15:15:35 +00:00
|
|
|
m_gx_uber_pipeline_cache;
|
2018-03-11 04:30:48 +00:00
|
|
|
File::IOFile m_gx_pipeline_uid_cache_file;
|
2018-02-24 15:15:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|
|
|
|
|
|
|
|
extern std::unique_ptr<VideoCommon::ShaderCache> g_shader_cache;
|