2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-08-06 15:09:43 -06:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2012-08-06 15:09:43 -06:00
|
|
|
|
2016-01-17 14:54:31 -07:00
|
|
|
#include <cstring>
|
2019-12-03 02:17:23 -07:00
|
|
|
#include <iterator>
|
2013-04-10 06:25:18 -06:00
|
|
|
#include <string>
|
2019-05-29 23:05:06 -06:00
|
|
|
#include <type_traits>
|
2012-09-02 12:00:15 -06:00
|
|
|
#include <vector>
|
|
|
|
|
2019-12-03 02:17:23 -07:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-07-28 22:11:15 -06:00
|
|
|
#include "Common/BitField.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-06-02 23:08:54 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2019-12-22 11:40:40 -07:00
|
|
|
|
|
|
|
enum class APIType;
|
2013-03-31 12:55:57 -06:00
|
|
|
|
2013-04-25 05:30:41 -06:00
|
|
|
/**
|
2016-01-01 23:20:01 -07:00
|
|
|
* Common interface for classes that need to go through the shader generation path
|
|
|
|
* (GenerateVertexShader, GenerateGeometryShader, GeneratePixelShader)
|
2013-04-25 05:30:41 -06:00
|
|
|
* In particular, this includes the shader code generator (ShaderCode).
|
|
|
|
* A different class (ShaderUid) can be used to uniquely identify each ShaderCode object.
|
|
|
|
* More interesting things can be done with this, e.g. ShaderConstantProfile checks what shader
|
|
|
|
* constants are being used. This can be used to optimize buffer management.
|
2016-01-01 23:20:01 -07:00
|
|
|
* If the class does not use one or more of these methods (e.g. Uid class does not need code), the
|
|
|
|
* method will be defined as a no-op by the base class, and the call
|
|
|
|
* should be optimized out. The reason for this implementation is so that shader
|
|
|
|
* selection/generation can be done in two passes, with only a cache lookup being
|
|
|
|
* required if the shader has already been generated.
|
2013-04-25 05:30:41 -06:00
|
|
|
*/
|
2013-03-26 16:03:10 -06:00
|
|
|
class ShaderGeneratorInterface
|
|
|
|
{
|
|
|
|
public:
|
2013-04-25 05:30:41 -06:00
|
|
|
/*
|
|
|
|
* Used when the shader generator would write a piece of ShaderCode.
|
|
|
|
* Can be used like printf.
|
|
|
|
* @note In the ShaderCode implementation, this does indeed write the parameter string to an
|
|
|
|
* internal buffer. However, you're free to do whatever you like with the parameter.
|
|
|
|
*/
|
2016-01-01 23:20:01 -07:00
|
|
|
void Write(const char*, ...)
|
2015-10-16 16:00:44 -06:00
|
|
|
#ifdef __GNUC__
|
|
|
|
__attribute__((format(printf, 2, 3)))
|
|
|
|
#endif
|
2015-12-26 14:00:23 -07:00
|
|
|
{
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-04-25 05:30:41 -06:00
|
|
|
/*
|
|
|
|
* Tells us that a specific constant range (including last_index) is being used by the shader
|
|
|
|
*/
|
2016-01-01 23:20:01 -07:00
|
|
|
void SetConstantsUsed(unsigned int first_index, unsigned int last_index) {}
|
2013-03-26 16:03:10 -06:00
|
|
|
};
|
|
|
|
|
2016-01-01 23:20:01 -07:00
|
|
|
/*
|
2013-04-25 05:30:41 -06:00
|
|
|
* Shader UID class used to uniquely identify the ShaderCode output written in the shader generator.
|
|
|
|
* uid_data can be any struct of parameters that uniquely identify each shader code output.
|
|
|
|
* Unless performance is not an issue, uid_data should be tightly packed to reduce memory footprint.
|
|
|
|
* Shader generators will write to specific uid_data fields; ShaderUid methods will only read raw
|
|
|
|
* u32 values from a union.
|
2016-01-01 23:20:01 -07:00
|
|
|
* NOTE: Because LinearDiskCache reads and writes the storage associated with a ShaderUid instance,
|
|
|
|
* ShaderUid must be trivially copyable.
|
2013-04-25 05:30:41 -06:00
|
|
|
*/
|
2013-03-26 16:03:10 -06:00
|
|
|
template <class uid_data>
|
2013-03-29 15:24:49 -06:00
|
|
|
class ShaderUid : public ShaderGeneratorInterface
|
2012-08-06 15:09:43 -06:00
|
|
|
{
|
|
|
|
public:
|
2019-05-29 23:05:06 -06:00
|
|
|
static_assert(std::is_trivially_copyable_v<uid_data>,
|
|
|
|
"uid_data must be a trivially copyable type");
|
|
|
|
|
2012-08-06 15:09:43 -06:00
|
|
|
bool operator==(const ShaderUid& obj) const
|
|
|
|
{
|
2019-05-30 13:56:47 -06:00
|
|
|
return memcmp(GetUidData(), obj.GetUidData(), GetUidDataSize()) == 0;
|
2012-08-06 15:09:43 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-05-30 00:13:37 -06:00
|
|
|
bool operator!=(const ShaderUid& obj) const { return !operator==(obj); }
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-04-25 05:30:41 -06:00
|
|
|
// determines the storage order inside STL containers
|
2012-08-06 15:09:43 -06:00
|
|
|
bool operator<(const ShaderUid& obj) const
|
|
|
|
{
|
2019-05-30 13:56:47 -06:00
|
|
|
return memcmp(GetUidData(), obj.GetUidData(), GetUidDataSize()) < 0;
|
2012-08-06 15:09:43 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-05-29 23:05:06 -06:00
|
|
|
// Returns a pointer to an internally stored object of the uid_data type.
|
|
|
|
uid_data* GetUidData() { return &data; }
|
|
|
|
|
|
|
|
// Returns a pointer to an internally stored object of the uid_data type.
|
2014-10-21 00:52:45 -06:00
|
|
|
const uid_data* GetUidData() const { return &data; }
|
2019-05-29 23:05:06 -06:00
|
|
|
|
|
|
|
// Returns the raw bytes that make up the shader UID.
|
2019-05-30 00:06:55 -06:00
|
|
|
const u8* GetUidDataRaw() const { return reinterpret_cast<const u8*>(&data); }
|
2019-05-29 23:05:06 -06:00
|
|
|
|
|
|
|
// Returns the size of the underlying UID data structure in bytes.
|
2019-05-30 00:06:55 -06:00
|
|
|
size_t GetUidDataSize() const { return sizeof(data); }
|
2018-04-12 06:18:04 -06:00
|
|
|
|
2012-08-06 15:09:43 -06:00
|
|
|
private:
|
2019-05-30 00:06:55 -06:00
|
|
|
uid_data data{};
|
2012-08-06 15:09:43 -06:00
|
|
|
};
|
|
|
|
|
2013-03-29 15:24:49 -06:00
|
|
|
class ShaderCode : public ShaderGeneratorInterface
|
2012-08-06 15:09:43 -06:00
|
|
|
{
|
|
|
|
public:
|
2015-12-26 14:00:23 -07:00
|
|
|
ShaderCode() { m_buffer.reserve(16384); }
|
2016-01-01 23:20:01 -07:00
|
|
|
const std::string& GetBuffer() const { return m_buffer; }
|
2019-12-03 02:17:23 -07:00
|
|
|
|
|
|
|
// Writes format strings using fmtlib format strings.
|
|
|
|
template <typename... Args>
|
2020-11-09 00:23:32 -07:00
|
|
|
void Write(std::string_view format, Args&&... args)
|
2019-12-03 02:17:23 -07:00
|
|
|
{
|
|
|
|
fmt::format_to(std::back_inserter(m_buffer), format, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-01-01 23:20:01 -07:00
|
|
|
protected:
|
|
|
|
std::string m_buffer;
|
2012-08-06 15:09:43 -06:00
|
|
|
};
|
|
|
|
|
2013-04-25 05:30:41 -06:00
|
|
|
/**
|
|
|
|
* Generates a shader constant profile which can be used to query which constants are used in a
|
|
|
|
* shader
|
|
|
|
*/
|
2013-03-29 15:24:49 -06:00
|
|
|
class ShaderConstantProfile : public ShaderGeneratorInterface
|
2012-09-02 12:00:15 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ShaderConstantProfile(int num_constants) { constant_usage.resize(num_constants); }
|
2016-01-01 23:20:01 -07:00
|
|
|
void SetConstantsUsed(unsigned int first_index, unsigned int last_index)
|
2012-09-02 12:00:15 -06:00
|
|
|
{
|
2015-02-15 12:43:31 -07:00
|
|
|
for (unsigned int i = first_index; i < last_index + 1; ++i)
|
2012-09-02 12:00:15 -06:00
|
|
|
constant_usage[i] = true;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-12-26 14:00:23 -07:00
|
|
|
bool ConstantIsUsed(unsigned int index) const
|
2012-09-02 12:00:15 -06:00
|
|
|
{
|
2013-04-25 05:30:41 -06:00
|
|
|
// TODO: Not ready for usage yet
|
2013-03-29 13:33:28 -06:00
|
|
|
return true;
|
|
|
|
// return constant_usage[index];
|
2012-09-02 12:00:15 -06:00
|
|
|
}
|
2016-01-01 23:20:01 -07:00
|
|
|
|
2012-09-02 12:00:15 -06:00
|
|
|
private:
|
|
|
|
std::vector<bool> constant_usage; // TODO: Is vector<bool> appropriate here?
|
|
|
|
};
|
2014-05-30 08:17:30 -06:00
|
|
|
|
2017-07-20 01:10:02 -06:00
|
|
|
// Host config contains the settings which can influence generated shaders.
|
|
|
|
union ShaderHostConfig
|
|
|
|
{
|
|
|
|
u32 bits;
|
|
|
|
|
2021-07-28 22:11:15 -06:00
|
|
|
BitField<0, 1, bool, u32> msaa;
|
|
|
|
BitField<1, 1, bool, u32> ssaa;
|
|
|
|
BitField<2, 1, bool, u32> stereo;
|
|
|
|
BitField<3, 1, bool, u32> wireframe;
|
|
|
|
BitField<4, 1, bool, u32> per_pixel_lighting;
|
|
|
|
BitField<5, 1, bool, u32> vertex_rounding;
|
|
|
|
BitField<6, 1, bool, u32> fast_depth_calc;
|
|
|
|
BitField<7, 1, bool, u32> bounding_box;
|
|
|
|
BitField<8, 1, bool, u32> backend_dual_source_blend;
|
|
|
|
BitField<9, 1, bool, u32> backend_geometry_shaders;
|
|
|
|
BitField<10, 1, bool, u32> backend_early_z;
|
|
|
|
BitField<11, 1, bool, u32> backend_bbox;
|
|
|
|
BitField<12, 1, bool, u32> backend_gs_instancing;
|
|
|
|
BitField<13, 1, bool, u32> backend_clip_control;
|
|
|
|
BitField<14, 1, bool, u32> backend_ssaa;
|
|
|
|
BitField<15, 1, bool, u32> backend_atomics;
|
|
|
|
BitField<16, 1, bool, u32> backend_depth_clamp;
|
|
|
|
BitField<17, 1, bool, u32> backend_reversed_depth_range;
|
|
|
|
BitField<18, 1, bool, u32> backend_bitfield;
|
|
|
|
BitField<19, 1, bool, u32> backend_dynamic_sampler_indexing;
|
|
|
|
BitField<20, 1, bool, u32> backend_shader_framebuffer_fetch;
|
|
|
|
BitField<21, 1, bool, u32> backend_logic_op;
|
|
|
|
BitField<22, 1, bool, u32> backend_palette_conversion;
|
2017-07-20 01:10:02 -06:00
|
|
|
|
|
|
|
static ShaderHostConfig GetCurrent();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Gets the filename of the specified type of cache object (e.g. vertex shader, pipeline).
|
|
|
|
std::string GetDiskShaderCacheFileName(APIType api_type, const char* type, bool include_gameid,
|
2018-02-24 08:15:35 -07:00
|
|
|
bool include_host_config, bool include_api = true);
|
2017-07-20 01:10:02 -06:00
|
|
|
|
2019-12-22 11:40:40 -07:00
|
|
|
void GenerateVSOutputMembers(ShaderCode& object, APIType api_type, u32 texgens,
|
|
|
|
const ShaderHostConfig& host_config, std::string_view qualifier);
|
2016-08-04 06:09:35 -06:00
|
|
|
|
2019-12-22 11:40:40 -07:00
|
|
|
void AssignVSOutputMembers(ShaderCode& object, std::string_view a, std::string_view b, u32 texgens,
|
|
|
|
const ShaderHostConfig& host_config);
|
2014-11-02 15:40:52 -07:00
|
|
|
|
2015-09-06 05:58:18 -06:00
|
|
|
// We use the flag "centroid" to fix some MSAA rendering bugs. With MSAA, the
|
|
|
|
// pixel shader will be executed for each pixel which has at least one passed sample.
|
|
|
|
// So there may be rendered pixels where the center of the pixel isn't in the primitive.
|
|
|
|
// As the pixel shader usually renders at the center of the pixel, this position may be
|
|
|
|
// outside the primitive. This will lead to sampling outside the texture, sign changes, ...
|
|
|
|
// As a workaround, we interpolate at the centroid of the coveraged pixel, which
|
|
|
|
// is always inside the primitive.
|
|
|
|
// Without MSAA, this flag is defined to have no effect.
|
2019-12-22 11:40:40 -07:00
|
|
|
const char* GetInterpolationQualifier(bool msaa, bool ssaa, bool in_glsl_interface_block = false,
|
|
|
|
bool in = false);
|
2015-09-06 05:58:18 -06:00
|
|
|
|
2014-05-30 08:17:30 -06:00
|
|
|
// Constant variable names
|
|
|
|
#define I_COLORS "color"
|
|
|
|
#define I_KCOLORS "k"
|
|
|
|
#define I_ALPHA "alphaRef"
|
|
|
|
#define I_TEXDIMS "texdim"
|
|
|
|
#define I_ZBIAS "czbias"
|
|
|
|
#define I_INDTEXSCALE "cindscale"
|
|
|
|
#define I_INDTEXMTX "cindmtx"
|
|
|
|
#define I_FOGCOLOR "cfogcolor"
|
|
|
|
#define I_FOGI "cfogi"
|
|
|
|
#define I_FOGF "cfogf"
|
2018-02-03 00:01:34 -07:00
|
|
|
#define I_FOGRANGE "cfogrange"
|
2014-12-25 00:34:22 -07:00
|
|
|
#define I_ZSLOPE "czslope"
|
2015-01-02 10:06:56 -07:00
|
|
|
#define I_EFBSCALE "cefbscale"
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-05-30 08:17:30 -06:00
|
|
|
#define I_POSNORMALMATRIX "cpnmtx"
|
|
|
|
#define I_PROJECTION "cproj"
|
|
|
|
#define I_MATERIALS "cmtrl"
|
|
|
|
#define I_LIGHTS "clights"
|
|
|
|
#define I_TEXMATRICES "ctexmtx"
|
|
|
|
#define I_TRANSFORMMATRICES "ctrmtx"
|
|
|
|
#define I_NORMALMATRICES "cnmtx"
|
|
|
|
#define I_POSTTRANSFORMMATRICES "cpostmtx"
|
2014-10-24 18:59:02 -06:00
|
|
|
#define I_PIXELCENTERCORRECTION "cpixelcenter"
|
2017-02-18 17:22:41 -07:00
|
|
|
#define I_VIEWPORT_SIZE "cviewport"
|
2014-12-14 13:23:13 -07:00
|
|
|
|
2014-12-15 16:21:07 -07:00
|
|
|
#define I_STEREOPARAMS "cstereo"
|
|
|
|
#define I_LINEPTPARAMS "clinept"
|
|
|
|
#define I_TEXOFFSET "ctexoffset"
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-19 23:25:27 -06:00
|
|
|
static const char s_shader_uniforms[] = "\tuint components;\n"
|
|
|
|
"\tuint xfmem_dualTexInfo;\n"
|
|
|
|
"\tuint xfmem_numColorChans;\n"
|
2021-02-22 20:03:06 -07:00
|
|
|
"\tuint missing_color_hex;\n"
|
|
|
|
"\tfloat4 missing_color_value;\n"
|
2017-07-19 23:25:27 -06:00
|
|
|
"\tfloat4 " I_POSNORMALMATRIX "[6];\n"
|
2014-10-29 07:15:12 -06:00
|
|
|
"\tfloat4 " I_PROJECTION "[4];\n"
|
|
|
|
"\tint4 " I_MATERIALS "[4];\n"
|
|
|
|
"\tLight " I_LIGHTS "[8];\n"
|
|
|
|
"\tfloat4 " I_TEXMATRICES "[24];\n"
|
|
|
|
"\tfloat4 " I_TRANSFORMMATRICES "[64];\n"
|
|
|
|
"\tfloat4 " I_NORMALMATRICES "[32];\n"
|
|
|
|
"\tfloat4 " I_POSTTRANSFORMMATRICES "[64];\n"
|
2017-02-18 17:22:41 -07:00
|
|
|
"\tfloat4 " I_PIXELCENTERCORRECTION ";\n"
|
2017-07-19 23:25:27 -06:00
|
|
|
"\tfloat2 " I_VIEWPORT_SIZE ";\n"
|
|
|
|
"\tuint4 xfmem_pack1[8];\n"
|
|
|
|
"\t#define xfmem_texMtxInfo(i) (xfmem_pack1[(i)].x)\n"
|
|
|
|
"\t#define xfmem_postMtxInfo(i) (xfmem_pack1[(i)].y)\n"
|
|
|
|
"\t#define xfmem_color(i) (xfmem_pack1[(i)].z)\n"
|
|
|
|
"\t#define xfmem_alpha(i) (xfmem_pack1[(i)].w)\n";
|