Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShaderGen: Remove virtual methods and string from ShaderGeneratorInterface. #3431

Merged
merged 1 commit into from
Jan 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Source/Core/Common/LinearDiskCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstring>
#include <fstream>
#include <string>
#include <type_traits>

#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
Expand Down Expand Up @@ -53,6 +54,15 @@ class LinearDiskCache
{
using std::ios_base;

// Since we're reading/writing directly to the storage of K instances,
// K must be trivially copyable. TODO: Remove #if once GCC 5.0 is a
// minimum requirement.
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
static_assert(std::has_trivial_copy_constructor<K>::value, "K must be a trivially copyable type");
#else
static_assert(std::is_trivially_copyable<K>::value, "K must be a trivially copyable type");
#endif

// close any currently opened file
Close();
m_num_entries = 0;
Expand Down
4 changes: 0 additions & 4 deletions Source/Core/VideoBackends/OGL/ProgramShaderCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ class SHADERUID
PixelShaderUid puid;
GeometryShaderUid guid;

SHADERUID() {}

SHADERUID(const SHADERUID& r) : vuid(r.vuid), puid(r.puid), guid(r.guid) {}

bool operator <(const SHADERUID& r) const
{
if (puid < r.puid)
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/VideoCommon/GeometryShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ static T GenerateGeometryShader(u32 primitive_type, API_TYPE ApiType)
// Non-uid template parameters will write to the dummy data (=> gets optimized out)
geometry_shader_uid_data dummy_data;
geometry_shader_uid_data* uid_data = out.template GetUidData<geometry_shader_uid_data>();
if (uid_data == nullptr)
if (uid_data != nullptr)
memset(uid_data, 0, sizeof(*uid_data));
else
uid_data = &dummy_data;

uid_data->primitive_type = primitive_type;
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/VideoCommon/PixelShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ static T GeneratePixelShader(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType)
// Non-uid template parameters will write to the dummy data (=> gets optimized out)
pixel_shader_uid_data dummy_data;
pixel_shader_uid_data* uid_data = out.template GetUidData<pixel_shader_uid_data>();
if (uid_data == nullptr)
if (uid_data != nullptr)
memset(uid_data, 0, sizeof(*uid_data));
else
uid_data = &dummy_data;

unsigned int numStages = bpmem.genMode.numtevstages + 1;
Expand Down
41 changes: 16 additions & 25 deletions Source/Core/VideoCommon/ShaderGenCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,23 @@
#include "VideoCommon/XFMemory.h"

/**
* Common interface for classes that need to go through the shader generation path (GenerateVertexShader, GeneratePixelShader)
* Common interface for classes that need to go through the shader generation path (GenerateVertexShader, GenerateGeometryShader, 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.
* 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.
*/
class ShaderGeneratorInterface
{
public:
virtual ~ShaderGeneratorInterface()
{
}

/*
* Returns a read pointer to the internal buffer.
*/
const std::string& GetBuffer() const { return m_buffer; }

/*
* 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.
*/
virtual void Write(const char*, ...)
void Write(const char*, ...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
Expand All @@ -51,35 +44,27 @@ class ShaderGeneratorInterface
/*
* Tells us that a specific constant range (including last_index) is being used by the shader
*/
virtual void SetConstantsUsed(unsigned int first_index, unsigned int last_index) {}
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; }

protected:
std::string m_buffer;
};

/**
/*
* 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.
* NOTE: Because LinearDiskCache reads and writes the storage associated with a ShaderUid instance, ShaderUid must be trivially copyable.
*/
template<class uid_data>
class ShaderUid : public ShaderGeneratorInterface
{
public:
ShaderUid()
{
// TODO: Move to Shadergen => can be optimized out
memset(values, 0, sizeof(values));
}

bool operator == (const ShaderUid& obj) const
{
return memcmp(this->values, obj.values, data.NumValues() * sizeof(*values)) == 0;
Expand Down Expand Up @@ -119,7 +104,9 @@ class ShaderCode : public ShaderGeneratorInterface
m_buffer.reserve(16384);
}

void Write(const char* fmt, ...) override
const std::string& GetBuffer() const { return m_buffer; }

void Write(const char* fmt, ...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
Expand All @@ -129,6 +116,9 @@ class ShaderCode : public ShaderGeneratorInterface
m_buffer += StringFromFormatV(fmt, arglist);
va_end(arglist);
}

protected:
std::string m_buffer;
};

/**
Expand All @@ -139,7 +129,7 @@ class ShaderConstantProfile : public ShaderGeneratorInterface
public:
ShaderConstantProfile(int num_constants) { constant_usage.resize(num_constants); }

void SetConstantsUsed(unsigned int first_index, unsigned int last_index) override
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;
Expand All @@ -151,6 +141,7 @@ class ShaderConstantProfile : public ShaderGeneratorInterface
return true;
//return constant_usage[index];
}

private:
std::vector<bool> constant_usage; // TODO: Is vector<bool> appropriate here?
};
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/VideoCommon/VertexShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ static T GenerateVertexShader(API_TYPE api_type)
// Non-uid template parameters will write to the dummy data (=> gets optimized out)
vertex_shader_uid_data dummy_data;
vertex_shader_uid_data* uid_data = out.template GetUidData<vertex_shader_uid_data>();
if (uid_data == nullptr)
if (uid_data != nullptr)
memset(uid_data, 0, sizeof(*uid_data));
else
uid_data = &dummy_data;

_assert_(bpmem.genMode.numtexgens == xfmem.numTexGen.numTexGens);
Expand Down