2014-02-10 11:54:46 -07:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
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
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
2014-02-22 19:45:46 -07:00
|
|
|
#include <iomanip>
|
2013-04-10 06:25:18 -06:00
|
|
|
#include <string>
|
2012-09-02 12:00:15 -06:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-06-02 23:08:54 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2013-03-31 12:55:57 -06:00
|
|
|
|
2013-04-25 05:30:41 -06:00
|
|
|
/**
|
|
|
|
* Common interface for classes that need to go through the shader generation path (GenerateVertexShader, GeneratePixelShader)
|
|
|
|
* 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.
|
|
|
|
* Each of the ShaderCode, ShaderUid and ShaderConstantProfile child classes only implement the subset of ShaderGeneratorInterface methods that are required for the specific tasks.
|
|
|
|
*/
|
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.
|
|
|
|
*/
|
2014-10-16 12:39:58 -06:00
|
|
|
template<typename... Args>
|
|
|
|
void Write(const char*, Args...) {}
|
2013-04-25 05:30:41 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a read pointer to the internal buffer.
|
|
|
|
* @note When implementing this method in a child class, you likely want to return the argument of the last SetBuffer call here
|
|
|
|
* @note SetBuffer() should be called before using GetBuffer().
|
|
|
|
*/
|
2014-03-09 14:14:26 -06:00
|
|
|
const char* GetBuffer() { return nullptr; }
|
2013-04-25 05:30:41 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Can be used to give the object a place to write to. This should be called before using Write().
|
|
|
|
* @param buffer pointer to a char buffer that the object can write to
|
|
|
|
*/
|
2013-03-26 16:03:10 -06:00
|
|
|
void SetBuffer(char* buffer) { }
|
2013-04-25 05:30:41 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tells us that a specific constant range (including last_index) is being used by the shader
|
|
|
|
*/
|
2013-03-26 16:03:10 -06:00
|
|
|
inline void SetConstantsUsed(unsigned int first_index, unsigned int last_index) {}
|
2013-03-29 15:24:49 -06:00
|
|
|
|
2013-04-25 05:30:41 -06:00
|
|
|
/*
|
|
|
|
* Returns a pointer to an internally stored object of the uid_data type.
|
2014-03-09 14:14:26 -06:00
|
|
|
* @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)
|
2013-04-25 05:30:41 -06:00
|
|
|
*/
|
2013-03-29 15:24:49 -06:00
|
|
|
template<class uid_data>
|
2014-10-21 00:52:45 -06:00
|
|
|
uid_data* GetUidData() { return nullptr; }
|
2013-03-26 16:03:10 -06: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.
|
|
|
|
*/
|
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:
|
|
|
|
ShaderUid()
|
|
|
|
{
|
2012-08-07 06:36:56 -06:00
|
|
|
// TODO: Move to Shadergen => can be optimized out
|
2012-08-06 15:09:43 -06:00
|
|
|
memset(values, 0, sizeof(values));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator == (const ShaderUid& obj) const
|
|
|
|
{
|
2013-09-11 09:34:23 -06:00
|
|
|
return memcmp(this->values, obj.values, data.NumValues() * sizeof(*values)) == 0;
|
2012-08-06 15:09:43 -06:00
|
|
|
}
|
|
|
|
|
2013-03-29 07:54:44 -06:00
|
|
|
bool operator != (const ShaderUid& obj) const
|
|
|
|
{
|
2013-09-11 09:34:23 -06:00
|
|
|
return memcmp(this->values, obj.values, data.NumValues() * sizeof(*values)) != 0;
|
2013-03-29 07:54:44 -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
|
|
|
|
{
|
2013-09-11 09:34:23 -06:00
|
|
|
return memcmp(this->values, obj.values, data.NumValues() * sizeof(*values)) < 0;
|
2012-08-06 15:09:43 -06:00
|
|
|
}
|
|
|
|
|
2014-10-21 00:52:45 -06:00
|
|
|
template<class uid_data2>
|
|
|
|
uid_data2* GetUidData() { return &data; }
|
|
|
|
const uid_data* GetUidData() const { return &data; }
|
2014-10-24 11:52:31 -06:00
|
|
|
const u8* GetUidDataRaw() const { return &values[0]; }
|
2012-08-06 15:09:43 -06:00
|
|
|
|
2013-04-29 13:00:39 -06:00
|
|
|
size_t GetUidDataSize() const { return sizeof(values); }
|
|
|
|
|
2012-08-06 15:09:43 -06:00
|
|
|
private:
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uid_data data;
|
2013-07-02 06:32:52 -06:00
|
|
|
u8 values[sizeof(uid_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:
|
2014-03-09 14:14:26 -06:00
|
|
|
ShaderCode() : buf(nullptr), write_ptr(nullptr)
|
2012-08-06 15:09:43 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list arglist;
|
|
|
|
va_start(arglist, fmt);
|
|
|
|
write_ptr += vsprintf(write_ptr, fmt, arglist);
|
|
|
|
va_end(arglist);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetBuffer() { return buf; }
|
|
|
|
void SetBuffer(char* buffer) { buf = buffer; write_ptr = buffer; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* buf;
|
|
|
|
char* write_ptr;
|
|
|
|
};
|
|
|
|
|
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); }
|
|
|
|
|
|
|
|
inline 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ConstantIsUsed(unsigned int index)
|
|
|
|
{
|
2013-04-25 05:30:41 -06:00
|
|
|
// TODO: Not ready for usage yet
|
2013-03-29 13:33:28 -06:00
|
|
|
return true;
|
2014-02-16 21:51:41 -07:00
|
|
|
//return constant_usage[index];
|
2012-09-02 12:00:15 -06:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::vector<bool> constant_usage; // TODO: Is vector<bool> appropriate here?
|
|
|
|
};
|
2013-04-25 05:30:41 -06:00
|
|
|
/**
|
|
|
|
* Checks if there has been
|
|
|
|
*/
|
2013-04-10 04:54:22 -06:00
|
|
|
template<class UidT, class CodeT>
|
2013-04-29 13:00:39 -06:00
|
|
|
class UidChecker
|
2013-04-10 04:54:22 -06:00
|
|
|
{
|
2013-04-29 13:00:39 -06:00
|
|
|
public:
|
|
|
|
void Invalidate()
|
2013-04-10 04:54:22 -06:00
|
|
|
{
|
2013-04-29 13:00:39 -06:00
|
|
|
m_shaders.clear();
|
|
|
|
m_uids.clear();
|
2013-04-10 04:54:22 -06:00
|
|
|
}
|
2013-04-29 13:00:39 -06:00
|
|
|
|
|
|
|
void AddToIndexAndCheck(CodeT& new_code, const UidT& new_uid, const char* shader_type, const char* dump_prefix)
|
2013-04-10 04:54:22 -06:00
|
|
|
{
|
2013-04-29 13:00:39 -06:00
|
|
|
bool uid_is_indexed = std::find(m_uids.begin(), m_uids.end(), new_uid) != m_uids.end();
|
|
|
|
if (!uid_is_indexed)
|
2013-04-10 04:54:22 -06:00
|
|
|
{
|
2013-04-29 13:00:39 -06:00
|
|
|
m_uids.push_back(new_uid);
|
|
|
|
m_shaders[new_uid] = new_code.GetBuffer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// uid is already in the index => check if there's a shader with the same uid but different code
|
|
|
|
auto& old_code = m_shaders[new_uid];
|
|
|
|
if (strcmp(old_code.c_str(), new_code.GetBuffer()) != 0)
|
|
|
|
{
|
|
|
|
static int num_failures = 0;
|
|
|
|
|
2014-06-02 23:08:54 -06:00
|
|
|
std::string temp = StringFromFormat("%s%ssuid_mismatch_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(),
|
|
|
|
dump_prefix, ++num_failures);
|
2013-04-29 13:00:39 -06:00
|
|
|
|
|
|
|
// TODO: Should also dump uids
|
|
|
|
std::ofstream file;
|
2014-06-02 23:08:54 -06:00
|
|
|
OpenFStream(file, temp, std::ios_base::out);
|
2013-04-29 13:00:39 -06:00
|
|
|
file << "Old shader code:\n" << old_code;
|
|
|
|
file << "\n\nNew shader code:\n" << new_code.GetBuffer();
|
|
|
|
file << "\n\nShader uid:\n";
|
|
|
|
for (unsigned int i = 0; i < new_uid.GetUidDataSize(); ++i)
|
|
|
|
{
|
2014-10-24 11:52:31 -06:00
|
|
|
u8 value = new_uid.GetUidDataRaw()[i];
|
2013-04-29 13:00:39 -06:00
|
|
|
if ((i % 4) == 0)
|
|
|
|
{
|
2013-11-13 02:03:46 -07:00
|
|
|
auto last_value = (i+3 < new_uid.GetUidDataSize()-1) ? i+3 : new_uid.GetUidDataSize();
|
2013-04-29 13:00:39 -06:00
|
|
|
file << std::setfill(' ') << std::dec;
|
|
|
|
file << "Values " << std::setw(2) << i << " - " << last_value << ": ";
|
|
|
|
}
|
|
|
|
|
2014-10-24 11:52:31 -06:00
|
|
|
file << std::setw(2) << std::setfill('0') << std::hex << value << std::setw(1);
|
2013-04-29 13:00:39 -06:00
|
|
|
if ((i % 4) < 3)
|
|
|
|
file << ' ';
|
|
|
|
else
|
|
|
|
file << std::endl;
|
|
|
|
}
|
|
|
|
|
2014-06-02 23:08:54 -06:00
|
|
|
ERROR_LOG(VIDEO, "%s shader uid mismatch! See %s for details", shader_type, temp.c_str());
|
2013-04-29 13:00:39 -06:00
|
|
|
}
|
2013-04-10 04:54:22 -06:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-04-29 13:00:39 -06:00
|
|
|
private:
|
|
|
|
std::map<UidT,std::string> m_shaders;
|
|
|
|
std::vector<UidT> m_uids;
|
|
|
|
};
|
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"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#define I_DEPTHPARAMS "cDepth" // farZ, zRange
|
2014-11-08 06:31:26 -07:00
|
|
|
#define I_STEREOPARAMS "cstereo"
|
2014-10-29 07:15:12 -06:00
|
|
|
|
|
|
|
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_DEPTHPARAMS";\n"
|
2014-11-08 06:31:26 -07:00
|
|
|
"\tfloat4 " I_STEREOPARAMS";\n";
|