2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2012-08-06 21:09:43 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2012-08-06 21:09:43 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <cstdarg>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
2014-02-23 02:45:46 +00:00
|
|
|
#include <iomanip>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <map>
|
2013-04-10 12:25:18 +00:00
|
|
|
#include <string>
|
2012-09-02 18:00:15 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2014-11-02 22:40:52 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
#include "VideoCommon/XFMemory.h"
|
2013-03-31 18:55:57 +00:00
|
|
|
|
2013-04-25 11:30:41 +00:00
|
|
|
/**
|
2016-06-24 08:43:46 +00:00
|
|
|
* Common interface for classes that need to go through the shader generation path
|
|
|
|
* (GenerateVertexShader, GenerateGeometryShader, GeneratePixelShader)
|
2013-04-25 11:30:41 +00:00
|
|
|
* In particular, this includes the shader code generator (ShaderCode).
|
|
|
|
* A different class (ShaderUid) can be used to uniquely identify each ShaderCode object.
|
2016-06-24 08:43:46 +00:00
|
|
|
* 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.
|
|
|
|
* 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
|
2016-01-02 06:20:01 +00:00
|
|
|
* required if the shader has already been generated.
|
2013-04-25 11:30:41 +00:00
|
|
|
*/
|
2013-03-26 22:03:10 +00:00
|
|
|
class ShaderGeneratorInterface
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00: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.
|
|
|
|
*/
|
|
|
|
void Write(const char*, ...)
|
2015-10-16 22:00:44 +00:00
|
|
|
#ifdef __GNUC__
|
2016-06-24 08:43:46 +00:00
|
|
|
__attribute__((format(printf, 2, 3)))
|
2015-10-16 22:00:44 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tells us that a specific constant range (including last_index) is being used by the shader
|
|
|
|
*/
|
|
|
|
void SetConstantsUsed(unsigned int first_index, unsigned int last_index) {}
|
|
|
|
/*
|
|
|
|
* Returns a pointer to an internally stored object of the uid_data type.
|
|
|
|
* @warning since most child classes use the default implementation you shouldn't access this
|
|
|
|
* directly without adding precautions against nullptr access (e.g. via adding a dummy structure,
|
|
|
|
* cf. the vertex/pixel shader generators)
|
|
|
|
*/
|
|
|
|
template <class uid_data>
|
|
|
|
uid_data* GetUidData()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-03-26 22:03:10 +00:00
|
|
|
};
|
|
|
|
|
2016-01-02 06:20:01 +00:00
|
|
|
/*
|
2013-04-25 11:30:41 +00: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.
|
2016-06-24 08:43:46 +00:00
|
|
|
* Shader generators will write to specific uid_data fields; ShaderUid methods will only read raw
|
|
|
|
* u32 values from a union.
|
|
|
|
* NOTE: Because LinearDiskCache reads and writes the storage associated with a ShaderUid instance,
|
|
|
|
* ShaderUid must be trivially copyable.
|
2013-04-25 11:30:41 +00:00
|
|
|
*/
|
2016-06-24 08:43:46 +00:00
|
|
|
template <class uid_data>
|
2013-03-29 21:24:49 +00:00
|
|
|
class ShaderUid : public ShaderGeneratorInterface
|
2012-08-06 21:09:43 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
bool operator==(const ShaderUid& obj) const
|
|
|
|
{
|
|
|
|
return memcmp(this->values, obj.values, data.NumValues() * sizeof(*values)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const ShaderUid& obj) const
|
|
|
|
{
|
|
|
|
return memcmp(this->values, obj.values, data.NumValues() * sizeof(*values)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determines the storage order inside STL containers
|
|
|
|
bool operator<(const ShaderUid& obj) const
|
|
|
|
{
|
|
|
|
return memcmp(this->values, obj.values, data.NumValues() * sizeof(*values)) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class uid_data2>
|
|
|
|
uid_data2* GetUidData()
|
|
|
|
{
|
|
|
|
return &data;
|
|
|
|
}
|
|
|
|
const uid_data* GetUidData() const { return &data; }
|
|
|
|
const u8* GetUidDataRaw() const { return &values[0]; }
|
|
|
|
size_t GetUidDataSize() const { return sizeof(values); }
|
2012-08-06 21:09:43 +00:00
|
|
|
private:
|
2016-06-24 08:43:46 +00:00
|
|
|
union {
|
|
|
|
uid_data data;
|
|
|
|
u8 values[sizeof(uid_data)];
|
|
|
|
};
|
2012-08-06 21:09:43 +00:00
|
|
|
};
|
|
|
|
|
2013-03-29 21:24:49 +00:00
|
|
|
class ShaderCode : public ShaderGeneratorInterface
|
2012-08-06 21:09:43 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
ShaderCode() { m_buffer.reserve(16384); }
|
|
|
|
const std::string& GetBuffer() const { return m_buffer; }
|
|
|
|
void Write(const char* fmt, ...)
|
2015-10-16 22:00:44 +00:00
|
|
|
#ifdef __GNUC__
|
2016-06-24 08:43:46 +00:00
|
|
|
__attribute__((format(printf, 2, 3)))
|
2015-10-16 22:00:44 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
va_list arglist;
|
|
|
|
va_start(arglist, fmt);
|
|
|
|
m_buffer += StringFromFormatV(fmt, arglist);
|
|
|
|
va_end(arglist);
|
|
|
|
}
|
2016-01-02 06:20:01 +00:00
|
|
|
|
|
|
|
protected:
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string m_buffer;
|
2012-08-06 21:09:43 +00:00
|
|
|
};
|
|
|
|
|
2013-04-25 11:30:41 +00:00
|
|
|
/**
|
2016-06-24 08:43:46 +00:00
|
|
|
* Generates a shader constant profile which can be used to query which constants are used in a
|
|
|
|
* shader
|
2013-04-25 11:30:41 +00:00
|
|
|
*/
|
2013-03-29 21:24:49 +00:00
|
|
|
class ShaderConstantProfile : public ShaderGeneratorInterface
|
2012-09-02 18:00:15 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
ShaderConstantProfile(int num_constants) { constant_usage.resize(num_constants); }
|
|
|
|
void SetConstantsUsed(unsigned int first_index, unsigned int last_index)
|
|
|
|
{
|
|
|
|
for (unsigned int i = first_index; i < last_index + 1; ++i)
|
|
|
|
constant_usage[i] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstantIsUsed(unsigned int index) const
|
|
|
|
{
|
|
|
|
// TODO: Not ready for usage yet
|
|
|
|
return true;
|
|
|
|
// return constant_usage[index];
|
|
|
|
}
|
2016-01-02 06:20:01 +00:00
|
|
|
|
2012-09-02 18:00:15 +00:00
|
|
|
private:
|
2016-06-24 08:43:46 +00:00
|
|
|
std::vector<bool> constant_usage; // TODO: Is vector<bool> appropriate here?
|
2012-09-02 18:00:15 +00:00
|
|
|
};
|
2014-05-30 14:17:30 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
template <class T>
|
2016-07-21 23:04:57 +00:00
|
|
|
inline void DefineOutputMember(T& object, APIType api_type, const char* qualifier, const char* type,
|
|
|
|
const char* name, int var_index, const char* semantic = "",
|
|
|
|
int semantic_index = -1)
|
2014-11-02 22:40:52 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
object.Write("\t%s %s %s", qualifier, type, name);
|
2014-12-21 11:52:14 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (var_index != -1)
|
|
|
|
object.Write("%d", var_index);
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2016-07-21 23:04:57 +00:00
|
|
|
if (api_type == APIType::D3D && strlen(semantic) > 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (semantic_index != -1)
|
|
|
|
object.Write(" : %s%d", semantic, semantic_index);
|
|
|
|
else
|
|
|
|
object.Write(" : %s", semantic);
|
|
|
|
}
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
object.Write(";\n");
|
2014-11-02 22:40:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
template <class T>
|
2016-07-21 23:04:57 +00:00
|
|
|
inline void GenerateVSOutputMembers(T& object, APIType api_type, u32 texgens,
|
2016-01-16 09:56:49 +00:00
|
|
|
bool per_pixel_lighting, const char* qualifier)
|
2014-11-02 22:40:52 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "pos", -1, "POSITION");
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 0, "COLOR", 0);
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 1, "COLOR", 1);
|
|
|
|
|
2016-01-16 09:56:49 +00:00
|
|
|
for (unsigned int i = 0; i < texgens; ++i)
|
2016-06-24 08:43:46 +00:00
|
|
|
DefineOutputMember(object, api_type, qualifier, "float3", "tex", i, "TEXCOORD", i);
|
|
|
|
|
2016-01-16 09:56:49 +00:00
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "clipPos", -1, "TEXCOORD", texgens);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-16 09:56:49 +00:00
|
|
|
if (per_pixel_lighting)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float3", "Normal", -1, "TEXCOORD",
|
2016-01-16 09:56:49 +00:00
|
|
|
texgens + 1);
|
2016-06-24 08:43:46 +00:00
|
|
|
DefineOutputMember(object, api_type, qualifier, "float3", "WorldPos", -1, "TEXCOORD",
|
2016-01-16 09:56:49 +00:00
|
|
|
texgens + 2);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-08-04 12:09:35 +00:00
|
|
|
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float", "clipDist", -1, "SV_ClipDistance");
|
2014-12-21 11:52:14 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
template <class T>
|
2016-01-16 09:56:49 +00:00
|
|
|
inline void AssignVSOutputMembers(T& object, const char* a, const char* b, u32 texgens,
|
|
|
|
bool per_pixel_lighting)
|
2014-12-21 11:52:14 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
object.Write("\t%s.pos = %s.pos;\n", a, b);
|
|
|
|
object.Write("\t%s.colors_0 = %s.colors_0;\n", a, b);
|
|
|
|
object.Write("\t%s.colors_1 = %s.colors_1;\n", a, b);
|
2014-12-21 11:52:14 +00:00
|
|
|
|
2016-01-16 09:56:49 +00:00
|
|
|
for (unsigned int i = 0; i < texgens; ++i)
|
2016-06-24 08:43:46 +00:00
|
|
|
object.Write("\t%s.tex%d = %s.tex%d;\n", a, i, b, i);
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
object.Write("\t%s.clipPos = %s.clipPos;\n", a, b);
|
2014-12-21 11:52:14 +00:00
|
|
|
|
2016-01-16 09:56:49 +00:00
|
|
|
if (per_pixel_lighting)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
object.Write("\t%s.Normal = %s.Normal;\n", a, b);
|
|
|
|
object.Write("\t%s.WorldPos = %s.WorldPos;\n", a, b);
|
|
|
|
}
|
2016-08-04 12:09:35 +00:00
|
|
|
|
|
|
|
object.Write("\t%s.clipDist = %s.clipDist;\n", a, b);
|
2014-11-02 22:40:52 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 11:58:18 +00: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.
|
2016-01-16 09:56:49 +00:00
|
|
|
inline const char* GetInterpolationQualifier(bool msaa, bool ssaa,
|
|
|
|
bool in_glsl_interface_block = false, bool in = false)
|
2015-09-06 11:58:18 +00:00
|
|
|
{
|
2016-01-16 09:56:49 +00:00
|
|
|
if (!msaa)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "";
|
|
|
|
|
|
|
|
// Without GL_ARB_shading_language_420pack support, the interpolation qualifier must be
|
|
|
|
// "centroid in" and not "centroid", even within an interface block.
|
|
|
|
if (in_glsl_interface_block && !g_ActiveConfig.backend_info.bSupportsBindingLayout)
|
|
|
|
{
|
2016-01-16 09:56:49 +00:00
|
|
|
if (!ssaa)
|
2016-06-24 08:43:46 +00:00
|
|
|
return in ? "centroid in" : "centroid out";
|
|
|
|
else
|
|
|
|
return in ? "sample in" : "sample out";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-16 09:56:49 +00:00
|
|
|
if (!ssaa)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "centroid";
|
|
|
|
else
|
|
|
|
return "sample";
|
|
|
|
}
|
2015-09-06 11:58:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 14:17:30 +00:00
|
|
|
// Constant variable names
|
2016-06-24 08:43:46 +00:00
|
|
|
#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"
|
|
|
|
#define I_ZSLOPE "czslope"
|
|
|
|
#define I_EFBSCALE "cefbscale"
|
|
|
|
|
|
|
|
#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"
|
2014-05-30 14:17:30 +00:00
|
|
|
#define I_POSTTRANSFORMMATRICES "cpostmtx"
|
2014-10-25 00:59:02 +00:00
|
|
|
#define I_PIXELCENTERCORRECTION "cpixelcenter"
|
2014-12-14 20:23:13 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
#define I_STEREOPARAMS "cstereo"
|
|
|
|
#define I_LINEPTPARAMS "clinept"
|
|
|
|
#define I_TEXOFFSET "ctexoffset"
|
|
|
|
|
|
|
|
static const char s_shader_uniforms[] = "\tfloat4 " I_POSNORMALMATRIX "[6];\n"
|
|
|
|
"\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"
|
|
|
|
"\tfloat4 " I_PIXELCENTERCORRECTION ";\n";
|