ShaderGeneration: Get rid of static buffers

This commit is contained in:
Lioncash 2015-12-26 16:00:23 -05:00
parent be8410dcad
commit 8ce3a4aa70
11 changed files with 63 additions and 118 deletions

View File

@ -347,8 +347,8 @@ FramebufferManager::FramebufferManager(int targetWidth, int targetHeight, int ms
" }\n"
"}\n";
ProgramShaderCache::CompileShader(m_pixel_format_shaders[0], vs, ps_rgb8_to_rgba6.c_str(), (m_EFBLayers > 1) ? gs.c_str() : nullptr);
ProgramShaderCache::CompileShader(m_pixel_format_shaders[1], vs, ps_rgba6_to_rgb8.c_str(), (m_EFBLayers > 1) ? gs.c_str() : nullptr);
ProgramShaderCache::CompileShader(m_pixel_format_shaders[0], vs, ps_rgb8_to_rgba6.c_str(), (m_EFBLayers > 1) ? gs : "");
ProgramShaderCache::CompileShader(m_pixel_format_shaders[1], vs, ps_rgba6_to_rgb8.c_str(), (m_EFBLayers > 1) ? gs : "");
ProgramShaderCache::CompileShader(m_EfbPokes,
StringFromFormat(
@ -362,7 +362,7 @@ FramebufferManager::FramebufferManager(int targetWidth, int targetHeight, int ms
" gl_PointSize = %d.0 / 640.0;\n"
" v_c = color0.bgra;\n"
" v_z = float(color1 & 0xFFFFFF) / 16777216.0;\n"
"}\n", m_targetWidth).c_str(),
"}\n", m_targetWidth),
StringFromFormat(
"in vec4 %s_c;\n"
@ -371,7 +371,7 @@ FramebufferManager::FramebufferManager(int targetWidth, int targetHeight, int ms
"void main(void) {\n"
" ocol0 = %s_c;\n"
" gl_FragDepth = %s_z;\n"
"}\n", m_EFBLayers > 1 ? "g" : "v", m_EFBLayers > 1 ? "g" : "v", m_EFBLayers > 1 ? "g" : "v", m_EFBLayers > 1 ? "g" : "v").c_str(),
"}\n", m_EFBLayers > 1 ? "g" : "v", m_EFBLayers > 1 ? "g" : "v", m_EFBLayers > 1 ? "g" : "v", m_EFBLayers > 1 ? "g" : "v"),
m_EFBLayers > 1 ? StringFromFormat(
"layout(points) in;\n"
@ -391,7 +391,7 @@ FramebufferManager::FramebufferManager(int targetWidth, int targetHeight, int ms
" EmitVertex();\n"
" EndPrimitive();\n"
" }\n"
"}\n", m_EFBLayers, m_EFBLayers, m_targetWidth).c_str() : nullptr);
"}\n", m_EFBLayers, m_EFBLayers, m_targetWidth) : "");
glGenBuffers(1, &m_EfbPokes_VBO);
glGenVertexArrays(1, &m_EfbPokes_VAO);
glBindBuffer(GL_ARRAY_BUFFER, m_EfbPokes_VBO);

View File

@ -149,15 +149,13 @@ void OpenGLPostProcessing::ApplyShader()
std::string code = m_config.LoadShader();
code = LoadShaderOptions(code);
const char* vertex_shader = s_vertex_shader;
// and compile it
if (!ProgramShaderCache::CompileShader(m_shader, vertex_shader, code.c_str()))
if (!ProgramShaderCache::CompileShader(m_shader, s_vertex_shader, code))
{
ERROR_LOG(VIDEO, "Failed to compile post-processing shader %s", m_config.GetShader().c_str());
g_ActiveConfig.sPostProcessingShader.clear();
code = m_config.LoadShader();
ProgramShaderCache::CompileShader(m_shader, vertex_shader, code.c_str());
ProgramShaderCache::CompileShader(m_shader, s_vertex_shader, code);
}
// read uniform locations

View File

@ -230,7 +230,7 @@ SHADER* ProgramShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 primitive_
filename = StringFromFormat("%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
SaveData(filename, pcode.GetBuffer());
if (gcode.GetBuffer() != nullptr)
if (!gcode.GetBuffer().empty())
{
filename = StringFromFormat("%sgs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
SaveData(filename, gcode.GetBuffer());
@ -252,17 +252,17 @@ SHADER* ProgramShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 primitive_
return &last_entry->shader;
}
bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const char* pcode, const char* gcode)
bool ProgramShaderCache::CompileShader(SHADER& shader, const std::string& vcode, const std::string& pcode, const std::string& gcode)
{
GLuint vsid = CompileSingleShader(GL_VERTEX_SHADER, vcode);
GLuint psid = CompileSingleShader(GL_FRAGMENT_SHADER, pcode);
// Optional geometry shader
GLuint gsid = 0;
if (gcode)
if (!gcode.empty())
gsid = CompileSingleShader(GL_GEOMETRY_SHADER, gcode);
if (!vsid || !psid || (gcode && !gsid))
if (!vsid || !psid || (!gcode.empty() && !gsid))
{
glDeleteShader(vsid);
glDeleteShader(psid);
@ -304,7 +304,7 @@ bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const
std::ofstream file;
OpenFStream(file, filename, std::ios_base::out);
file << s_glsl_header << vcode << s_glsl_header << pcode;
if (gcode)
if (!gcode.empty())
file << s_glsl_header << gcode;
file << infoLog;
file.close();
@ -334,11 +334,11 @@ bool ProgramShaderCache::CompileShader(SHADER& shader, const char* vcode, const
return true;
}
GLuint ProgramShaderCache::CompileSingleShader(GLuint type, const char* code)
GLuint ProgramShaderCache::CompileSingleShader(GLuint type, const std::string& code)
{
GLuint result = glCreateShader(type);
const char *src[] = {s_glsl_header.c_str(), code};
const char *src[] = {s_glsl_header.c_str(), code.c_str()};
glShaderSource(result, 2, src, nullptr);
glCompileShader(result);

View File

@ -91,8 +91,8 @@ public:
static SHADER* SetShader(DSTALPHA_MODE dstAlphaMode, u32 primitive_type);
static void GetShaderId(SHADERUID *uid, DSTALPHA_MODE dstAlphaMode, u32 primitive_type);
static bool CompileShader(SHADER &shader, const char* vcode, const char* pcode, const char* gcode = nullptr);
static GLuint CompileSingleShader(GLuint type, const char *code);
static bool CompileShader(SHADER &shader, const std::string& vcode, const std::string& pcode, const std::string& gcode = "");
static GLuint CompileSingleShader(GLuint type, const std::string& code);
static void UploadConstants();
static void Init();

View File

@ -337,7 +337,7 @@ void TextureCache::SetStage()
void TextureCache::CompileShaders()
{
const char *pColorCopyProg =
constexpr const char* color_copy_program =
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
"in vec3 f_uv0;\n"
"out vec4 ocol0;\n"
@ -347,7 +347,7 @@ void TextureCache::CompileShaders()
" ocol0 = texcol;\n"
"}\n";
const char *pColorMatrixProg =
constexpr const char* color_matrix_program =
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
"uniform vec4 colmat[7];\n"
"in vec3 f_uv0;\n"
@ -359,7 +359,7 @@ void TextureCache::CompileShaders()
" ocol0 = texcol * mat4(colmat[0], colmat[1], colmat[2], colmat[3]) + colmat[4];\n"
"}\n";
const char *pDepthMatrixProg =
constexpr const char* depth_matrix_program =
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
"uniform vec4 colmat[5];\n"
"in vec3 f_uv0;\n"
@ -384,7 +384,7 @@ void TextureCache::CompileShaders()
" ocol0 = texcol * mat4(colmat[0], colmat[1], colmat[2], colmat[3]) + colmat[4];\n"
"}\n";
const char *VProgram =
constexpr const char* vertex_program =
"out vec3 %s_uv0;\n"
"SAMPLER_BINDING(9) uniform sampler2DArray samp9;\n"
"uniform vec4 copy_position;\n" // left, top, right, bottom
@ -395,7 +395,7 @@ void TextureCache::CompileShaders()
" gl_Position = vec4(rawpos*2.0-1.0, 0.0, 1.0);\n"
"}\n";
const char *GProgram = g_ActiveConfig.iStereoMode > 0 ?
const std::string geo_program = g_ActiveConfig.iStereoMode > 0 ?
"layout(triangles) in;\n"
"layout(triangle_strip, max_vertices = 6) out;\n"
"in vec3 v_uv0[3];\n"
@ -413,14 +413,14 @@ void TextureCache::CompileShaders()
" }\n"
" EndPrimitive();\n"
" }\n"
"}\n" : nullptr;
"}\n" : "";
const char* prefix = (GProgram == nullptr) ? "f" : "v";
const char* depth_layer = (g_ActiveConfig.bStereoEFBMonoDepth) ? "0.0" : "f_uv0.z";
const char* prefix = geo_program.empty() ? "f" : "v";
const char* depth_layer = g_ActiveConfig.bStereoEFBMonoDepth ? "0.0" : "f_uv0.z";
ProgramShaderCache::CompileShader(s_ColorCopyProgram, StringFromFormat(VProgram, prefix, prefix).c_str(), pColorCopyProg, GProgram);
ProgramShaderCache::CompileShader(s_ColorMatrixProgram, StringFromFormat(VProgram, prefix, prefix).c_str(), pColorMatrixProg, GProgram);
ProgramShaderCache::CompileShader(s_DepthMatrixProgram, StringFromFormat(VProgram, prefix, prefix).c_str(), StringFromFormat(pDepthMatrixProg, depth_layer).c_str(), GProgram);
ProgramShaderCache::CompileShader(s_ColorCopyProgram, StringFromFormat(vertex_program, prefix, prefix).c_str(), color_copy_program, geo_program);
ProgramShaderCache::CompileShader(s_ColorMatrixProgram, StringFromFormat(vertex_program, prefix, prefix).c_str(), color_matrix_program, geo_program);
ProgramShaderCache::CompileShader(s_DepthMatrixProgram, StringFromFormat(vertex_program, prefix, prefix).c_str(), StringFromFormat(depth_matrix_program, depth_layer).c_str(), geo_program);
s_ColorMatrixUniform = glGetUniformLocation(s_ColorMatrixProgram.glprogid, "colmat");
s_DepthMatrixUniform = glGetUniformLocation(s_DepthMatrixProgram.glprogid, "colmat");
@ -515,27 +515,27 @@ void TextureCache::CompileShaders()
{
ProgramShaderCache::CompileShader(
s_palette_pixel_shader[GX_TL_IA8],
StringFromFormat(VProgram, prefix, prefix).c_str(),
("#define DECODE DecodePixel_IA8" + palette_shader).c_str(),
GProgram);
StringFromFormat(vertex_program, prefix, prefix),
"#define DECODE DecodePixel_IA8" + palette_shader,
geo_program);
s_palette_buffer_offset_uniform[GX_TL_IA8] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_IA8].glprogid, "texture_buffer_offset");
s_palette_multiplier_uniform[GX_TL_IA8] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_IA8].glprogid, "multiplier");
s_palette_copy_position_uniform[GX_TL_IA8] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_IA8].glprogid, "copy_position");
ProgramShaderCache::CompileShader(
s_palette_pixel_shader[GX_TL_RGB565],
StringFromFormat(VProgram, prefix, prefix).c_str(),
("#define DECODE DecodePixel_RGB565" + palette_shader).c_str(),
GProgram);
StringFromFormat(vertex_program, prefix, prefix),
"#define DECODE DecodePixel_RGB565" + palette_shader,
geo_program);
s_palette_buffer_offset_uniform[GX_TL_RGB565] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB565].glprogid, "texture_buffer_offset");
s_palette_multiplier_uniform[GX_TL_RGB565] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB565].glprogid, "multiplier");
s_palette_copy_position_uniform[GX_TL_RGB565] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB565].glprogid, "copy_position");
ProgramShaderCache::CompileShader(
s_palette_pixel_shader[GX_TL_RGB5A3],
StringFromFormat(VProgram, prefix, prefix).c_str(),
("#define DECODE DecodePixel_RGB5A3" + palette_shader).c_str(),
GProgram);
StringFromFormat(vertex_program, prefix, prefix),
"#define DECODE DecodePixel_RGB5A3" + palette_shader,
geo_program);
s_palette_buffer_offset_uniform[GX_TL_RGB5A3] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB5A3].glprogid, "texture_buffer_offset");
s_palette_multiplier_uniform[GX_TL_RGB5A3] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB5A3].glprogid, "multiplier");
s_palette_copy_position_uniform[GX_TL_RGB5A3] = glGetUniformLocation(s_palette_pixel_shader[GX_TL_RGB5A3].glprogid, "copy_position");

View File

@ -7,10 +7,8 @@
#include "VideoCommon/BPMemory.h"
#include "VideoCommon/GeometryShaderGen.h"
#include "VideoCommon/LightingShaderGen.h"
#include "VideoCommon/VertexShaderGen.h"
#include "VideoCommon/VideoConfig.h"
static char text[16384];
static const char* primitives_ogl[] =
{
@ -39,12 +37,6 @@ static inline T GenerateGeometryShader(u32 primitive_type, API_TYPE ApiType)
if (uid_data == nullptr)
uid_data = &dummy_data;
out.SetBuffer(text);
const bool is_writing_shadercode = (out.GetBuffer() != nullptr);
if (is_writing_shadercode)
text[sizeof(text) - 1] = 0x7C; // canary
uid_data->primitive_type = primitive_type;
const unsigned int vertex_in = primitive_type + 1;
unsigned int vertex_out = primitive_type == PRIMITIVE_TRIANGLES ? 3 : 4;
@ -288,12 +280,6 @@ static inline T GenerateGeometryShader(u32 primitive_type, API_TYPE ApiType)
out.Write("}\n");
if (is_writing_shadercode)
{
if (text[sizeof(text) - 1] != 0x7C)
PanicAlert("GeometryShader generator - buffer too small, canary has been eaten!");
}
return out;
}

View File

@ -12,7 +12,7 @@
#include "Common/Logging/Log.h"
#include "VideoCommon/ImageWrite.h"
bool SaveData(const std::string& filename, const char* data)
bool SaveData(const std::string& filename, const std::string& data)
{
std::ofstream f;
OpenFStream(f, filename, std::ios::binary);

View File

@ -7,5 +7,5 @@
#include <string>
#include "Common/CommonTypes.h"
bool SaveData(const std::string& filename, const char* pdata);
bool SaveData(const std::string& filename, const std::string& data);
bool TextureToPng(u8* data, int row_stride, const std::string& filename, int width, int height, bool saveAlpha = true);

View File

@ -9,13 +9,10 @@
#include "Common/Common.h"
#include "VideoCommon/BoundingBox.h"
#include "VideoCommon/BPMemory.h"
#include "VideoCommon/ConstantManager.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/LightingShaderGen.h"
#include "VideoCommon/NativeVertexFormat.h"
#include "VideoCommon/PixelShaderGen.h"
#include "VideoCommon/VertexLoaderManager.h"
#include "VideoCommon/VertexShaderGen.h"
#include "VideoCommon/VideoConfig.h"
#include "VideoCommon/XFMemory.h" // for texture projection mode
@ -157,8 +154,6 @@ static const char *tevRasTable[] =
static const char *tevCOutputTable[] = { "prev.rgb", "c0.rgb", "c1.rgb", "c2.rgb" };
static const char *tevAOutputTable[] = { "prev.a", "c0.a", "c1.a", "c2.a" };
static char text[32768];
template<class T> static inline void WriteStage(T& out, pixel_shader_uid_data* uid_data, int n, API_TYPE ApiType, const char swapModeTable[4][5]);
template<class T> static inline void WriteTevRegular(T& out, const char* components, int bias, int op, int clamp, int shift);
template<class T> static inline void SampleTexture(T& out, const char *texcoords, const char *texswap, int texmap, API_TYPE ApiType);
@ -176,12 +171,6 @@ static inline T GeneratePixelShader(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
if (uid_data == nullptr)
uid_data = &dummy_data;
out.SetBuffer(text);
const bool is_writing_shadercode = (out.GetBuffer() != nullptr);
if (is_writing_shadercode)
text[sizeof(text) - 1] = 0x7C; // canary
unsigned int numStages = bpmem.genMode.numtevstages + 1;
unsigned int numTexgen = bpmem.genMode.numtexgens;
@ -339,7 +328,7 @@ static inline T GeneratePixelShader(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
out.Write("[earlydepthstencil]\n");
}
}
else if (bpmem.UseEarlyDepthTest() && (g_ActiveConfig.bFastDepthCalc || bpmem.alpha_test.TestResult() == AlphaTest::UNDETERMINED) && is_writing_shadercode)
else if (bpmem.UseEarlyDepthTest() && (g_ActiveConfig.bFastDepthCalc || bpmem.alpha_test.TestResult() == AlphaTest::UNDETERMINED))
{
static bool warn_once = true;
if (warn_once)
@ -663,12 +652,6 @@ static inline T GeneratePixelShader(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
out.Write("}\n");
if (is_writing_shadercode)
{
if (text[sizeof(text) - 1] != 0x7C)
PanicAlert("PixelShader generator - buffer too small, canary has been eaten!");
}
return out;
}
@ -1165,7 +1148,7 @@ static inline void WriteFog(T& out, pixel_shader_uid_data* uid_data)
}
else
{
if (bpmem.fog.c_proj_fsel.fsel != 2 && out.GetBuffer() != nullptr)
if (bpmem.fog.c_proj_fsel.fsel != 2)
WARN_LOG(VIDEO, "Unknown Fog Type! %08x", bpmem.fog.c_proj_fsel.fsel);
}

View File

@ -27,34 +27,31 @@
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.
*/
void Write(const char*, ...)
virtual void Write(const char*, ...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
{}
/*
* 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().
*/
const char* GetBuffer() { return nullptr; }
/*
* 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
*/
void SetBuffer(char* buffer) { }
{
}
/*
* Tells us that a specific constant range (including last_index) is being used by the shader
*/
inline void SetConstantsUsed(unsigned int first_index, unsigned int last_index) {}
virtual void SetConstantsUsed(unsigned int first_index, unsigned int last_index) {}
/*
* Returns a pointer to an internally stored object of the uid_data type.
@ -62,6 +59,9 @@ public:
*/
template<class uid_data>
uid_data* GetUidData() { return nullptr; }
protected:
std::string m_buffer;
};
/**
@ -114,27 +114,21 @@ private:
class ShaderCode : public ShaderGeneratorInterface
{
public:
ShaderCode() : buf(nullptr), write_ptr(nullptr)
ShaderCode()
{
m_buffer.reserve(16384);
}
void Write(const char* fmt, ...)
void Write(const char* fmt, ...) override
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
{
va_list arglist;
va_start(arglist, fmt);
write_ptr += vsprintf(write_ptr, fmt, arglist);
m_buffer += StringFromFormatV(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;
};
/**
@ -145,13 +139,13 @@ class ShaderConstantProfile : public ShaderGeneratorInterface
public:
ShaderConstantProfile(int num_constants) { constant_usage.resize(num_constants); }
inline void SetConstantsUsed(unsigned int first_index, unsigned int last_index)
void SetConstantsUsed(unsigned int first_index, unsigned int last_index) override
{
for (unsigned int i = first_index; i < last_index + 1; ++i)
constant_usage[i] = true;
}
inline bool ConstantIsUsed(unsigned int index)
bool ConstantIsUsed(unsigned int index) const
{
// TODO: Not ready for usage yet
return true;
@ -185,7 +179,7 @@ public:
{
// 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)
if (old_code != new_code.GetBuffer())
{
static int num_failures = 0;

View File

@ -5,16 +5,12 @@
#include <cmath>
#include "VideoCommon/BPMemory.h"
#include "VideoCommon/CPMemory.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/LightingShaderGen.h"
#include "VideoCommon/NativeVertexFormat.h"
#include "VideoCommon/VertexLoaderManager.h"
#include "VideoCommon/VertexShaderGen.h"
#include "VideoCommon/VideoConfig.h"
static char text[16768];
template<class T>
static inline T GenerateVertexShader(API_TYPE api_type)
{
@ -26,12 +22,6 @@ static inline T GenerateVertexShader(API_TYPE api_type)
if (uid_data == nullptr)
uid_data = &dummy_data;
out.SetBuffer(text);
const bool is_writing_shadercode = (out.GetBuffer() != nullptr);
if (is_writing_shadercode)
text[sizeof(text) - 1] = 0x7C; // canary
_assert_(bpmem.genMode.numtexgens == xfmem.numTexGen.numTexGens);
_assert_(bpmem.genMode.numcolchans == xfmem.numChan.numColorChans);
@ -390,12 +380,6 @@ static inline T GenerateVertexShader(API_TYPE api_type)
}
out.Write("}\n");
if (is_writing_shadercode)
{
if (text[sizeof(text) - 1] != 0x7C)
PanicAlert("VertexShader generator - buffer too small, canary has been eaten!");
}
return out;
}