/*
Copyright 2021 flyinghead
This file is part of Flycast.
Flycast is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Flycast is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Flycast. If not, see .
*/
#pragma once
#include
#include
#include
#include
#include "types.h"
#include "windows/comptr.h"
class CachedDX11Shaders
{
protected:
void enableCache(bool enable) { this->enabled = enable; }
void saveCache(const std::string& filename);
void loadCache(const std::string& filename);
u64 hashShader(const char* source, const char* function, const char* profile, const D3D_SHADER_MACRO *pDefines = nullptr, const char *includeFile = nullptr);
bool lookupShader(u64 hash, ComPtr& blob);
void cacheShader(u64 hash, const ComPtr& blob);
private:
struct ShaderBlob
{
u32 size;
std::unique_ptr blob;
};
std::unordered_map shaderCache;
bool enabled = true;
};
class DX11Shaders : CachedDX11Shaders
{
public:
void init(const ComPtr& device, pD3DCompile D3DCompile);
void term();
const ComPtr& getShader(bool pp_Texture, bool pp_UseAlpha, bool pp_IgnoreTexA, u32 pp_ShadInstr,
bool pp_Offset, u32 pp_FogCtrl, bool pp_BumpMap, bool fog_clamping, bool trilinear, bool palette, bool gouraud,
bool alphaTest, bool clipInside, bool nearestWrapFix);
const ComPtr& getVertexShader(bool gouraud);
const ComPtr& getModVolShader();
const ComPtr& getMVVertexShader();
const ComPtr& getQuadPixelShader();
const ComPtr& getQuadVertexShader(bool rotate);
ComPtr getVertexShaderBlob();
ComPtr getMVVertexShaderBlob();
ComPtr getQuadVertexShaderBlob();
private:
ComPtr compileShader(const char *source, const char* function, const char* profile, const D3D_SHADER_MACRO *pDefines);
ComPtr compileVS(const char *source, const char* function, const D3D_SHADER_MACRO *pDefines);
ComPtr compilePS(const char *source, const char* function, const D3D_SHADER_MACRO *pDefines);
ComPtr device;
std::unordered_map> shaders;
ComPtr gouraudVertexShader;
ComPtr flatVertexShader;
ComPtr modVolShader;
ComPtr modVolVertexShader;
ComPtr quadPixelShader;
ComPtr quadVertexShader;
ComPtr quadRotateVertexShader;
pD3DCompile D3DCompile = nullptr;
constexpr static const char *CacheFile = "dx11_shader_cache.bin";
};