2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2011-12-09 23:30:05 +00:00
|
|
|
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
#include <atomic>
|
2017-07-20 05:25:29 +00:00
|
|
|
#include <limits>
|
2015-12-21 15:15:17 +00:00
|
|
|
#include <memory>
|
2014-03-30 18:58:05 +00:00
|
|
|
#include <string>
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "Common/Assert.h"
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 20:46:32 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2018-10-03 13:02:45 +00:00
|
|
|
#include "Common/GL/GLContext.h"
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-06-03 05:08:54 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "Common/Timer.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2016-11-27 21:31:48 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "Core/Host.h"
|
2016-11-27 21:31:48 +00:00
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "VideoBackends/OGL/OGLShader.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/OGL/Render.h"
|
|
|
|
#include "VideoBackends/OGL/StreamBuffer.h"
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "VideoBackends/OGL/VertexManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "VideoCommon/AsyncShaderCompiler.h"
|
|
|
|
#include "VideoCommon/DriverDetails.h"
|
2014-12-14 20:23:13 +00:00
|
|
|
#include "VideoCommon/GeometryShaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/ImageWrite.h"
|
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
2014-02-19 11:14:09 +00:00
|
|
|
#include "VideoCommon/Statistics.h"
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
2016-07-21 23:04:57 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2011-12-10 21:58:44 +00:00
|
|
|
|
2011-12-01 03:00:21 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
static constexpr u32 UBO_LENGTH = 32 * 1024 * 1024;
|
2013-01-25 12:20:42 +00:00
|
|
|
|
2013-01-14 12:58:11 +00:00
|
|
|
u32 ProgramShaderCache::s_ubo_buffer_size;
|
2013-10-07 15:19:47 +00:00
|
|
|
s32 ProgramShaderCache::s_ubo_align;
|
2018-01-20 14:59:10 +00:00
|
|
|
GLuint ProgramShaderCache::s_attributeless_VBO = 0;
|
|
|
|
GLuint ProgramShaderCache::s_attributeless_VAO = 0;
|
|
|
|
GLuint ProgramShaderCache::s_last_VAO = 0;
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2015-12-21 15:15:17 +00:00
|
|
|
static std::unique_ptr<StreamBuffer> s_buffer;
|
2013-05-04 21:30:13 +00:00
|
|
|
static int num_failures = 0;
|
2013-01-31 22:11:53 +00:00
|
|
|
|
2013-02-13 20:34:48 +00:00
|
|
|
static GLuint CurrentProgram = 0;
|
2018-02-25 07:56:09 +00:00
|
|
|
ProgramShaderCache::PipelineProgramMap ProgramShaderCache::s_pipeline_programs;
|
|
|
|
std::mutex ProgramShaderCache::s_pipeline_program_lock;
|
2015-11-04 00:56:02 +00:00
|
|
|
static std::string s_glsl_header = "";
|
2019-02-15 01:59:50 +00:00
|
|
|
static std::atomic<u64> s_shader_counter{0};
|
2018-02-25 07:56:09 +00:00
|
|
|
static thread_local bool s_is_shared_context = false;
|
2013-02-13 20:34:48 +00:00
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static std::string GetGLSLVersionString()
|
2014-03-30 18:58:05 +00:00
|
|
|
{
|
2018-01-05 18:01:18 +00:00
|
|
|
GlslVersion v = g_ogl_config.eSupportedGLSLVersion;
|
2016-06-24 08:43:46 +00:00
|
|
|
switch (v)
|
|
|
|
{
|
2018-01-05 18:01:18 +00:00
|
|
|
case GlslEs300:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 300 es";
|
2018-01-05 18:01:18 +00:00
|
|
|
case GlslEs310:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 310 es";
|
2018-01-05 18:01:18 +00:00
|
|
|
case GlslEs320:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 320 es";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl130:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 130";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl140:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 140";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl150:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 150";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl330:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 330";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl400:
|
2016-06-24 08:43:46 +00:00
|
|
|
return "#version 400";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl430:
|
2016-11-27 08:14:56 +00:00
|
|
|
return "#version 430";
|
2016-06-24 08:43:46 +00:00
|
|
|
default:
|
|
|
|
// Shouldn't ever hit this
|
|
|
|
return "#version ERROR";
|
|
|
|
}
|
2014-03-30 18:58:05 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
void SHADER::SetProgramVariables()
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2018-03-03 09:10:45 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsBindingLayout)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// To set uniform blocks/uniforms, the program must be active. We restore the
|
|
|
|
// current binding at the end of this method to maintain the invariant.
|
|
|
|
glUseProgram(glprogid);
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Bind UBO and texture samplers
|
2018-03-03 09:10:45 +00:00
|
|
|
GLint PSBlock_id = glGetUniformBlockIndex(glprogid, "PSBlock");
|
|
|
|
GLint VSBlock_id = glGetUniformBlockIndex(glprogid, "VSBlock");
|
|
|
|
GLint GSBlock_id = glGetUniformBlockIndex(glprogid, "GSBlock");
|
|
|
|
GLint UBERBlock_id = glGetUniformBlockIndex(glprogid, "UBERBlock");
|
|
|
|
if (PSBlock_id != -1)
|
|
|
|
glUniformBlockBinding(glprogid, PSBlock_id, 1);
|
|
|
|
if (VSBlock_id != -1)
|
|
|
|
glUniformBlockBinding(glprogid, VSBlock_id, 2);
|
|
|
|
if (GSBlock_id != -1)
|
|
|
|
glUniformBlockBinding(glprogid, GSBlock_id, 3);
|
|
|
|
if (UBERBlock_id != -1)
|
|
|
|
glUniformBlockBinding(glprogid, UBERBlock_id, 4);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-03-03 09:10:45 +00:00
|
|
|
// Bind Texture Samplers
|
2019-02-15 01:59:50 +00:00
|
|
|
for (int a = 0; a < 8; ++a)
|
2018-03-03 09:10:45 +00:00
|
|
|
{
|
|
|
|
// Still need to get sampler locations since we aren't binding them statically in the shaders
|
2019-02-15 01:59:50 +00:00
|
|
|
int loc = glGetUniformLocation(glprogid, StringFromFormat("samp[%d]", a).c_str());
|
|
|
|
if (loc < 0)
|
|
|
|
loc = glGetUniformLocation(glprogid, StringFromFormat("samp%d", a).c_str());
|
|
|
|
if (loc >= 0)
|
2018-03-03 09:10:45 +00:00
|
|
|
glUniform1i(loc, a);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2018-03-03 09:10:45 +00:00
|
|
|
|
|
|
|
// Restore previous program binding.
|
|
|
|
glUseProgram(CurrentProgram);
|
2012-12-31 01:34:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 08:14:56 +00:00
|
|
|
void SHADER::SetProgramBindings(bool is_compute)
|
2012-12-31 01:34:27 +00:00
|
|
|
{
|
2016-11-27 08:14:56 +00:00
|
|
|
if (!is_compute)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-11-27 08:14:56 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsDualSourceBlend)
|
|
|
|
{
|
|
|
|
// So we do support extended blending
|
|
|
|
// So we need to set a few more things here.
|
|
|
|
// Bind our out locations
|
|
|
|
glBindFragDataLocationIndexed(glprogid, 0, 0, "ocol0");
|
|
|
|
glBindFragDataLocationIndexed(glprogid, 0, 1, "ocol1");
|
|
|
|
}
|
|
|
|
// Need to set some attribute locations
|
|
|
|
glBindAttribLocation(glprogid, SHADER_POSITION_ATTRIB, "rawpos");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-11-27 08:14:56 +00:00
|
|
|
glBindAttribLocation(glprogid, SHADER_POSMTX_ATTRIB, "posmtx");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-26 15:01:25 +00:00
|
|
|
glBindAttribLocation(glprogid, SHADER_COLOR0_ATTRIB, "rawcolor0");
|
|
|
|
glBindAttribLocation(glprogid, SHADER_COLOR1_ATTRIB, "rawcolor1");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-11-27 08:14:56 +00:00
|
|
|
glBindAttribLocation(glprogid, SHADER_NORM0_ATTRIB, "rawnorm0");
|
|
|
|
glBindAttribLocation(glprogid, SHADER_NORM1_ATTRIB, "rawnorm1");
|
|
|
|
glBindAttribLocation(glprogid, SHADER_NORM2_ATTRIB, "rawnorm2");
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
2017-07-26 15:01:25 +00:00
|
|
|
std::string attrib_name = StringFromFormat("rawtex%d", i);
|
2016-06-24 08:43:46 +00:00
|
|
|
glBindAttribLocation(glprogid, SHADER_TEXTURE0_ATTRIB + i, attrib_name.c_str());
|
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
2011-12-11 10:14:02 +00:00
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
void SHADER::Bind() const
|
2013-02-13 12:12:19 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (CurrentProgram != glprogid)
|
|
|
|
{
|
|
|
|
INCSTAT(stats.thisFrame.numShaderChanges);
|
|
|
|
glUseProgram(glprogid);
|
|
|
|
CurrentProgram = glprogid;
|
|
|
|
}
|
2013-02-13 12:12:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
void SHADER::DestroyShaders()
|
|
|
|
{
|
|
|
|
if (vsid)
|
|
|
|
{
|
|
|
|
glDeleteShader(vsid);
|
|
|
|
vsid = 0;
|
|
|
|
}
|
|
|
|
if (gsid)
|
|
|
|
{
|
|
|
|
glDeleteShader(gsid);
|
|
|
|
gsid = 0;
|
|
|
|
}
|
|
|
|
if (psid)
|
|
|
|
{
|
|
|
|
glDeleteShader(psid);
|
|
|
|
psid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
bool PipelineProgramKey::operator!=(const PipelineProgramKey& rhs) const
|
|
|
|
{
|
|
|
|
return !operator==(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PipelineProgramKey::operator==(const PipelineProgramKey& rhs) const
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
return std::tie(vertex_shader_id, geometry_shader_id, pixel_shader_id) ==
|
|
|
|
std::tie(rhs.vertex_shader_id, rhs.geometry_shader_id, rhs.pixel_shader_id);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PipelineProgramKey::operator<(const PipelineProgramKey& rhs) const
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
return std::tie(vertex_shader_id, geometry_shader_id, pixel_shader_id) <
|
|
|
|
std::tie(rhs.vertex_shader_id, rhs.geometry_shader_id, rhs.pixel_shader_id);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t PipelineProgramKeyHash::operator()(const PipelineProgramKey& key) const
|
|
|
|
{
|
|
|
|
// We would really want std::hash_combine for this..
|
2019-02-15 01:59:50 +00:00
|
|
|
std::hash<u64> hasher;
|
|
|
|
return hasher(key.vertex_shader_id) + hasher(key.geometry_shader_id) +
|
|
|
|
hasher(key.pixel_shader_id);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamBuffer* ProgramShaderCache::GetUniformBuffer()
|
|
|
|
{
|
|
|
|
return s_buffer.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 ProgramShaderCache::GetUniformBufferAlignment()
|
|
|
|
{
|
|
|
|
return s_ubo_align;
|
|
|
|
}
|
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
void ProgramShaderCache::UploadConstants()
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (PixelShaderManager::dirty || VertexShaderManager::dirty || GeometryShaderManager::dirty)
|
|
|
|
{
|
|
|
|
auto buffer = s_buffer->Map(s_ubo_buffer_size, s_ubo_align);
|
|
|
|
|
|
|
|
memcpy(buffer.first, &PixelShaderManager::constants, sizeof(PixelShaderConstants));
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
memcpy(buffer.first + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align),
|
2016-06-24 08:43:46 +00:00
|
|
|
&VertexShaderManager::constants, sizeof(VertexShaderConstants));
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
memcpy(buffer.first + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align) +
|
|
|
|
Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align),
|
2016-06-24 08:43:46 +00:00
|
|
|
&GeometryShaderManager::constants, sizeof(GeometryShaderConstants));
|
|
|
|
|
|
|
|
s_buffer->Unmap(s_ubo_buffer_size);
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_buffer->m_buffer, buffer.second,
|
|
|
|
sizeof(PixelShaderConstants));
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_buffer->m_buffer,
|
2016-11-27 10:56:22 +00:00
|
|
|
buffer.second + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align),
|
2016-06-24 08:43:46 +00:00
|
|
|
sizeof(VertexShaderConstants));
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 3, s_buffer->m_buffer,
|
2016-11-27 10:56:22 +00:00
|
|
|
buffer.second + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align) +
|
|
|
|
Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align),
|
2016-06-24 08:43:46 +00:00
|
|
|
sizeof(GeometryShaderConstants));
|
|
|
|
|
|
|
|
PixelShaderManager::dirty = false;
|
|
|
|
VertexShaderManager::dirty = false;
|
|
|
|
GeometryShaderManager::dirty = false;
|
|
|
|
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, s_ubo_buffer_size);
|
|
|
|
}
|
2013-02-13 12:12:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
void ProgramShaderCache::UploadConstants(const void* data, u32 data_size)
|
|
|
|
{
|
|
|
|
// allocate and copy
|
|
|
|
const u32 alloc_size = Common::AlignUp(data_size, s_ubo_align);
|
|
|
|
auto buffer = s_buffer->Map(alloc_size, s_ubo_align);
|
|
|
|
std::memcpy(buffer.first, data, data_size);
|
|
|
|
s_buffer->Unmap(alloc_size);
|
|
|
|
|
|
|
|
// bind the same sub-buffer to all stages
|
|
|
|
for (u32 index = 1; index <= 3; index++)
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, index, s_buffer->m_buffer, buffer.second, data_size);
|
|
|
|
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, data_size);
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
bool ProgramShaderCache::CompileShader(SHADER& shader, const std::string& vcode,
|
|
|
|
const std::string& pcode, const std::string& gcode)
|
|
|
|
{
|
2013-02-13 12:12:19 +00:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2016-06-24 08:43:46 +00:00
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVESHADERS)
|
|
|
|
{
|
|
|
|
static int counter = 0;
|
|
|
|
std::string filename =
|
|
|
|
StringFromFormat("%svs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
|
2017-07-20 05:25:29 +00:00
|
|
|
SaveData(filename, vcode.c_str());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
filename = StringFromFormat("%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
|
2017-07-20 05:25:29 +00:00
|
|
|
SaveData(filename, pcode.c_str());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
if (!gcode.empty())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
filename =
|
|
|
|
StringFromFormat("%sgs_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
|
2017-07-20 05:25:29 +00:00
|
|
|
SaveData(filename, gcode.c_str());
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-13 12:12:19 +00:00
|
|
|
#endif
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.vsid = CompileSingleShader(GL_VERTEX_SHADER, vcode);
|
|
|
|
shader.psid = CompileSingleShader(GL_FRAGMENT_SHADER, pcode);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Optional geometry shader
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.gsid = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!gcode.empty())
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.gsid = CompileSingleShader(GL_GEOMETRY_SHADER, gcode);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
if (!shader.vsid || !shader.psid || (!gcode.empty() && !shader.gsid))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.Destroy();
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
// Create and link the program.
|
|
|
|
shader.glprogid = glCreateProgram();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
glAttachShader(shader.glprogid, shader.vsid);
|
|
|
|
glAttachShader(shader.glprogid, shader.psid);
|
|
|
|
if (shader.gsid)
|
|
|
|
glAttachShader(shader.glprogid, shader.gsid);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (g_ogl_config.bSupportsGLSLCache)
|
2017-07-20 05:25:29 +00:00
|
|
|
glProgramParameteri(shader.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-11-27 08:14:56 +00:00
|
|
|
shader.SetProgramBindings(false);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
glLinkProgram(shader.glprogid);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
if (!CheckProgramLinkResult(shader.glprogid, vcode, pcode, gcode))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
// Don't try to use this shader
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.Destroy();
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
// For drivers that don't support binding layout, we need to bind it here.
|
2016-06-24 08:43:46 +00:00
|
|
|
shader.SetProgramVariables();
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
// Original shaders aren't needed any more.
|
|
|
|
shader.DestroyShaders();
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 08:14:56 +00:00
|
|
|
bool ProgramShaderCache::CompileComputeShader(SHADER& shader, const std::string& code)
|
|
|
|
{
|
|
|
|
// We need to enable GL_ARB_compute_shader for drivers that support the extension,
|
|
|
|
// but not GLSL 4.3. Mesa is one example.
|
|
|
|
std::string header;
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsComputeShaders &&
|
2018-01-05 18:01:18 +00:00
|
|
|
g_ogl_config.eSupportedGLSLVersion < Glsl430)
|
2016-11-27 08:14:56 +00:00
|
|
|
{
|
|
|
|
header = "#extension GL_ARB_compute_shader : enable\n";
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
std::string full_code = header + code;
|
|
|
|
GLuint shader_id = CompileSingleShader(GL_COMPUTE_SHADER, full_code);
|
2016-11-27 08:14:56 +00:00
|
|
|
if (!shader_id)
|
|
|
|
return false;
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.glprogid = glCreateProgram();
|
|
|
|
glAttachShader(shader.glprogid, shader_id);
|
2016-11-27 08:14:56 +00:00
|
|
|
shader.SetProgramBindings(true);
|
2017-07-20 05:25:29 +00:00
|
|
|
glLinkProgram(shader.glprogid);
|
2016-11-27 08:14:56 +00:00
|
|
|
|
|
|
|
// original shaders aren't needed any more
|
|
|
|
glDeleteShader(shader_id);
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
if (!CheckProgramLinkResult(shader.glprogid, full_code, "", ""))
|
2016-11-27 08:14:56 +00:00
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.Destroy();
|
2016-11-27 08:14:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
shader.SetProgramVariables();
|
2016-11-27 08:14:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
GLuint ProgramShaderCache::CompileSingleShader(GLenum type, const std::string& code)
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
GLuint result = glCreateShader(type);
|
|
|
|
|
|
|
|
const char* src[] = {s_glsl_header.c_str(), code.c_str()};
|
|
|
|
|
|
|
|
glShaderSource(result, 2, src, nullptr);
|
|
|
|
glCompileShader(result);
|
2017-07-20 05:25:29 +00:00
|
|
|
|
|
|
|
if (!CheckShaderCompileResult(result, type, code))
|
|
|
|
{
|
|
|
|
// Don't try to use this shader
|
|
|
|
glDeleteShader(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramShaderCache::CheckShaderCompileResult(GLuint id, GLenum type, const std::string& code)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
GLint compileStatus;
|
2017-07-20 05:25:29 +00:00
|
|
|
glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus);
|
2016-06-24 08:43:46 +00:00
|
|
|
GLsizei length = 0;
|
2017-07-20 05:25:29 +00:00
|
|
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
|
2018-02-09 10:59:56 +00:00
|
|
|
if (compileStatus != GL_TRUE || length > 1)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-11-27 08:14:56 +00:00
|
|
|
std::string info_log;
|
|
|
|
info_log.resize(length);
|
2017-07-20 05:25:29 +00:00
|
|
|
glGetShaderInfoLog(id, length, &length, &info_log[0]);
|
2016-11-27 08:14:56 +00:00
|
|
|
|
|
|
|
const char* prefix = "";
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case GL_VERTEX_SHADER:
|
|
|
|
prefix = "vs";
|
|
|
|
break;
|
|
|
|
case GL_GEOMETRY_SHADER:
|
|
|
|
prefix = "gs";
|
|
|
|
break;
|
|
|
|
case GL_FRAGMENT_SHADER:
|
|
|
|
prefix = "ps";
|
|
|
|
break;
|
|
|
|
case GL_COMPUTE_SHADER:
|
|
|
|
prefix = "cs";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:59:56 +00:00
|
|
|
if (compileStatus != GL_TRUE)
|
2018-02-25 08:03:58 +00:00
|
|
|
{
|
2018-02-09 10:59:56 +00:00
|
|
|
ERROR_LOG(VIDEO, "%s failed compilation:\n%s", prefix, info_log.c_str());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-02-25 08:03:58 +00:00
|
|
|
std::string filename = StringFromFormat(
|
|
|
|
"%sbad_%s_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), prefix, num_failures++);
|
|
|
|
std::ofstream file;
|
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
|
|
|
file << s_glsl_header << code << info_log;
|
|
|
|
file.close();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
PanicAlert("Failed to compile %s shader: %s\n"
|
|
|
|
"Debug info (%s, %s, %s):\n%s",
|
2016-11-27 08:14:56 +00:00
|
|
|
prefix, filename.c_str(), g_ogl_config.gl_vendor, g_ogl_config.gl_renderer,
|
|
|
|
g_ogl_config.gl_version, info_log.c_str());
|
2018-02-25 08:03:58 +00:00
|
|
|
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2018-02-25 08:03:58 +00:00
|
|
|
|
|
|
|
WARN_LOG(VIDEO, "%s compiled with warnings:\n%s", prefix, info_log.c_str());
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
return true;
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, const std::string& vcode,
|
|
|
|
const std::string& pcode, const std::string& gcode)
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
GLint linkStatus;
|
|
|
|
glGetProgramiv(id, GL_LINK_STATUS, &linkStatus);
|
|
|
|
GLsizei length = 0;
|
|
|
|
glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length);
|
2018-02-09 10:59:56 +00:00
|
|
|
if (linkStatus != GL_TRUE || length > 1)
|
2017-07-20 05:25:29 +00:00
|
|
|
{
|
|
|
|
std::string info_log;
|
|
|
|
info_log.resize(length);
|
|
|
|
glGetProgramInfoLog(id, length, &length, &info_log[0]);
|
2018-02-09 10:59:56 +00:00
|
|
|
if (linkStatus != GL_TRUE)
|
2018-02-25 08:03:58 +00:00
|
|
|
{
|
2018-02-09 10:59:56 +00:00
|
|
|
ERROR_LOG(VIDEO, "Program failed linking:\n%s", info_log.c_str());
|
2018-02-25 08:03:58 +00:00
|
|
|
std::string filename =
|
|
|
|
StringFromFormat("%sbad_p_%d.txt", File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
|
|
|
|
std::ofstream file;
|
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
|
|
|
file << s_glsl_header << vcode << s_glsl_header << pcode;
|
|
|
|
if (!gcode.empty())
|
|
|
|
file << s_glsl_header << gcode;
|
|
|
|
file << info_log;
|
|
|
|
file.close();
|
2017-07-20 05:25:29 +00:00
|
|
|
|
|
|
|
PanicAlert("Failed to link shaders: %s\n"
|
|
|
|
"Debug info (%s, %s, %s):\n%s",
|
|
|
|
filename.c_str(), g_ogl_config.gl_vendor, g_ogl_config.gl_renderer,
|
|
|
|
g_ogl_config.gl_version, info_log.c_str());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-25 08:03:58 +00:00
|
|
|
|
|
|
|
WARN_LOG(VIDEO, "Program linked with warnings:\n%s", info_log.c_str());
|
2017-07-20 05:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-03-29 13:54:44 +00:00
|
|
|
}
|
2013-02-13 12:12:19 +00:00
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
void ProgramShaderCache::Init()
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// We have to get the UBO alignment here because
|
|
|
|
// if we generate a buffer that isn't aligned
|
|
|
|
// then the UBO will fail.
|
|
|
|
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &s_ubo_align);
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
s_ubo_buffer_size =
|
|
|
|
static_cast<u32>(Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align) +
|
|
|
|
Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align) +
|
|
|
|
Common::AlignUp(sizeof(GeometryShaderConstants), s_ubo_align));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// We multiply by *4*4 because we need to get down to basic machine units.
|
|
|
|
// So multiply by four to get how many floats we have from vec4s
|
|
|
|
// Then once more to get bytes
|
|
|
|
s_buffer = StreamBuffer::Create(GL_UNIFORM_BUFFER, UBO_LENGTH);
|
|
|
|
|
2017-06-24 09:20:34 +00:00
|
|
|
CreateHeader();
|
2018-01-20 14:59:10 +00:00
|
|
|
CreateAttributelessVAO();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-24 09:20:34 +00:00
|
|
|
CurrentProgram = 0;
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
void ProgramShaderCache::Shutdown()
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2017-06-24 09:20:34 +00:00
|
|
|
s_buffer.reset();
|
2018-01-20 14:59:10 +00:00
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
glDeleteBuffers(1, &s_attributeless_VBO);
|
|
|
|
glDeleteVertexArrays(1, &s_attributeless_VAO);
|
|
|
|
s_attributeless_VBO = 0;
|
|
|
|
s_attributeless_VAO = 0;
|
|
|
|
s_last_VAO = 0;
|
2017-09-08 09:42:56 +00:00
|
|
|
|
|
|
|
// All pipeline programs should have been released.
|
2018-03-16 16:57:36 +00:00
|
|
|
DEBUG_ASSERT(s_pipeline_programs.empty());
|
2018-02-25 07:56:09 +00:00
|
|
|
s_pipeline_programs.clear();
|
2018-01-20 14:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramShaderCache::CreateAttributelessVAO()
|
|
|
|
{
|
|
|
|
glGenVertexArrays(1, &s_attributeless_VAO);
|
|
|
|
|
|
|
|
// In a compatibility context, we require a valid, bound array buffer.
|
|
|
|
glGenBuffers(1, &s_attributeless_VBO);
|
|
|
|
|
|
|
|
// Initialize the buffer with nothing. 16 floats is an arbitrary size that may work around driver
|
|
|
|
// issues.
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, s_attributeless_VBO);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16, nullptr, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// We must also define vertex attribute 0.
|
|
|
|
glBindVertexArray(s_attributeless_VAO);
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
|
|
|
|
glEnableVertexAttribArray(0);
|
2017-06-24 09:20:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
void ProgramShaderCache::BindVertexFormat(const GLVertexFormat* vertex_format)
|
|
|
|
{
|
2018-01-20 14:59:10 +00:00
|
|
|
u32 new_VAO = vertex_format ? vertex_format->VAO : s_attributeless_VAO;
|
2017-07-20 05:25:29 +00:00
|
|
|
if (s_last_VAO == new_VAO)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glBindVertexArray(new_VAO);
|
|
|
|
s_last_VAO = new_VAO;
|
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
bool ProgramShaderCache::IsValidVertexFormatBound()
|
|
|
|
{
|
|
|
|
return s_last_VAO != 0 && s_last_VAO != s_attributeless_VAO;
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
void ProgramShaderCache::InvalidateVertexFormat()
|
|
|
|
{
|
2018-01-20 14:59:10 +00:00
|
|
|
s_last_VAO = 0;
|
2017-07-20 05:25:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
void ProgramShaderCache::InvalidateLastProgram()
|
|
|
|
{
|
|
|
|
CurrentProgram = 0;
|
|
|
|
}
|
|
|
|
|
2018-02-25 07:56:09 +00:00
|
|
|
const PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* vertex_format,
|
|
|
|
const OGLShader* vertex_shader,
|
2017-09-08 09:42:56 +00:00
|
|
|
const OGLShader* geometry_shader,
|
|
|
|
const OGLShader* pixel_shader)
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
PipelineProgramKey key = {vertex_shader ? vertex_shader->GetID() : 0,
|
|
|
|
geometry_shader ? geometry_shader->GetID() : 0,
|
|
|
|
pixel_shader ? pixel_shader->GetID() : 0};
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2018-02-25 07:56:09 +00:00
|
|
|
std::lock_guard<std::mutex> guard(s_pipeline_program_lock);
|
|
|
|
auto iter = s_pipeline_programs.find(key);
|
|
|
|
if (iter != s_pipeline_programs.end())
|
2018-02-24 12:14:31 +00:00
|
|
|
{
|
|
|
|
iter->second->reference_count++;
|
|
|
|
return iter->second.get();
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 07:56:09 +00:00
|
|
|
// We temporarily change the vertex array to the pipeline's vertex format.
|
|
|
|
// This can prevent the NVIDIA OpenGL driver from recompiling on first use.
|
|
|
|
GLuint vao = vertex_format ? vertex_format->VAO : s_attributeless_VAO;
|
|
|
|
if (s_is_shared_context || vao != s_last_VAO)
|
|
|
|
glBindVertexArray(vao);
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
std::unique_ptr<PipelineProgram> prog = std::make_unique<PipelineProgram>();
|
|
|
|
prog->key = key;
|
|
|
|
|
|
|
|
// Attach shaders.
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT(vertex_shader && vertex_shader->GetStage() == ShaderStage::Vertex);
|
|
|
|
ASSERT(pixel_shader && pixel_shader->GetStage() == ShaderStage::Pixel);
|
2017-09-08 09:42:56 +00:00
|
|
|
prog->shader.glprogid = glCreateProgram();
|
|
|
|
glAttachShader(prog->shader.glprogid, vertex_shader->GetGLShaderID());
|
|
|
|
glAttachShader(prog->shader.glprogid, pixel_shader->GetGLShaderID());
|
|
|
|
if (geometry_shader)
|
|
|
|
{
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT(geometry_shader->GetStage() == ShaderStage::Geometry);
|
2017-09-08 09:42:56 +00:00
|
|
|
glAttachShader(prog->shader.glprogid, geometry_shader->GetGLShaderID());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link program.
|
|
|
|
prog->shader.SetProgramBindings(false);
|
|
|
|
glLinkProgram(prog->shader.glprogid);
|
2018-02-25 07:56:09 +00:00
|
|
|
|
|
|
|
// Restore VAO binding after linking.
|
|
|
|
if (!s_is_shared_context && vao != s_last_VAO)
|
|
|
|
glBindVertexArray(s_last_VAO);
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
if (!ProgramShaderCache::CheckProgramLinkResult(prog->shader.glprogid, {}, {}, {}))
|
|
|
|
{
|
|
|
|
prog->shader.Destroy();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-24 12:14:31 +00:00
|
|
|
// Lock to insert. A duplicate program may have been created in the meantime.
|
2018-02-25 07:56:09 +00:00
|
|
|
std::lock_guard<std::mutex> guard(s_pipeline_program_lock);
|
|
|
|
auto iter = s_pipeline_programs.find(key);
|
|
|
|
if (iter != s_pipeline_programs.end())
|
2018-02-24 12:14:31 +00:00
|
|
|
{
|
|
|
|
// Destroy this program, and use the one which was created first.
|
|
|
|
prog->shader.Destroy();
|
|
|
|
iter->second->reference_count++;
|
|
|
|
return iter->second.get();
|
|
|
|
}
|
|
|
|
|
2018-03-03 09:10:45 +00:00
|
|
|
// Set program variables on the shader which will be returned.
|
|
|
|
// This is only needed for drivers which don't support binding layout.
|
|
|
|
prog->shader.SetProgramVariables();
|
2018-02-25 07:56:09 +00:00
|
|
|
|
|
|
|
// If this is a shared context, ensure we sync before we return the program to
|
|
|
|
// the main thread. If we don't do this, some driver can lock up (e.g. AMD).
|
|
|
|
if (s_is_shared_context)
|
|
|
|
glFinish();
|
|
|
|
|
|
|
|
auto ip = s_pipeline_programs.emplace(key, std::move(prog));
|
2017-09-08 09:42:56 +00:00
|
|
|
return ip.first->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramShaderCache::ReleasePipelineProgram(const PipelineProgram* prog)
|
|
|
|
{
|
2018-02-25 07:56:09 +00:00
|
|
|
auto iter = s_pipeline_programs.find(prog->key);
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT(iter != s_pipeline_programs.end() && prog == iter->second.get());
|
2017-09-08 09:42:56 +00:00
|
|
|
|
|
|
|
if (--iter->second->reference_count == 0)
|
|
|
|
{
|
|
|
|
iter->second->shader.Destroy();
|
2018-02-25 07:56:09 +00:00
|
|
|
s_pipeline_programs.erase(iter);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
void ProgramShaderCache::CreateHeader()
|
2013-02-13 20:34:48 +00:00
|
|
|
{
|
2018-01-05 18:01:18 +00:00
|
|
|
GlslVersion v = g_ogl_config.eSupportedGLSLVersion;
|
|
|
|
bool is_glsles = v >= GlslEs300;
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string SupportedESPointSize;
|
|
|
|
std::string SupportedESTextureBuffer;
|
|
|
|
switch (g_ogl_config.SupportedESPointSize)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
SupportedESPointSize = "#extension GL_OES_geometry_point_size : enable";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
SupportedESPointSize = "#extension GL_EXT_geometry_point_size : enable";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SupportedESPointSize = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (g_ogl_config.SupportedESTextureBuffer)
|
|
|
|
{
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsTexbufType::TexbufExt:
|
2016-06-24 08:43:46 +00:00
|
|
|
SupportedESTextureBuffer = "#extension GL_EXT_texture_buffer : enable";
|
|
|
|
break;
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsTexbufType::TexbufOes:
|
2016-06-24 08:43:46 +00:00
|
|
|
SupportedESTextureBuffer = "#extension GL_OES_texture_buffer : enable";
|
|
|
|
break;
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsTexbufType::TexbufCore:
|
|
|
|
case EsTexbufType::TexbufNone:
|
2016-06-24 08:43:46 +00:00
|
|
|
SupportedESTextureBuffer = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string earlyz_string = "";
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsEarlyZ)
|
|
|
|
{
|
2016-11-27 08:14:56 +00:00
|
|
|
if (g_ogl_config.bSupportsImageLoadStore)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
earlyz_string = "#define FORCE_EARLY_Z layout(early_fragment_tests) in\n";
|
|
|
|
}
|
|
|
|
else if (g_ogl_config.bSupportsConservativeDepth)
|
|
|
|
{
|
|
|
|
// See PixelShaderGen for details about this fallback.
|
|
|
|
earlyz_string = "#define FORCE_EARLY_Z layout(depth_unchanged) out float gl_FragDepth\n";
|
|
|
|
earlyz_string += "#extension GL_ARB_conservative_depth : enable\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 05:44:39 +00:00
|
|
|
std::string framebuffer_fetch_string;
|
|
|
|
switch (g_ogl_config.SupportedFramebufferFetch)
|
|
|
|
{
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsFbFetchType::FbFetchExt:
|
2017-10-26 05:44:39 +00:00
|
|
|
framebuffer_fetch_string = "#extension GL_EXT_shader_framebuffer_fetch: enable\n"
|
2017-11-10 01:39:09 +00:00
|
|
|
"#define FB_FETCH_VALUE real_ocol0\n"
|
2017-10-26 05:44:39 +00:00
|
|
|
"#define FRAGMENT_INOUT inout";
|
|
|
|
break;
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsFbFetchType::FbFetchArm:
|
2017-10-26 05:44:39 +00:00
|
|
|
framebuffer_fetch_string = "#extension GL_ARM_shader_framebuffer_fetch: enable\n"
|
|
|
|
"#define FB_FETCH_VALUE gl_LastFragColorARM\n"
|
|
|
|
"#define FRAGMENT_INOUT out";
|
|
|
|
break;
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsFbFetchType::FbFetchNone:
|
2017-10-26 05:44:39 +00:00
|
|
|
framebuffer_fetch_string = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_glsl_header = StringFromFormat(
|
|
|
|
"%s\n"
|
|
|
|
"%s\n" // ubo
|
|
|
|
"%s\n" // early-z
|
|
|
|
"%s\n" // 420pack
|
|
|
|
"%s\n" // msaa
|
2016-08-12 14:40:18 +00:00
|
|
|
"%s\n" // Input/output/sampler binding
|
|
|
|
"%s\n" // Varying location
|
2016-06-24 08:43:46 +00:00
|
|
|
"%s\n" // storage buffer
|
|
|
|
"%s\n" // shader5
|
|
|
|
"%s\n" // SSAA
|
|
|
|
"%s\n" // Geometry point size
|
|
|
|
"%s\n" // AEP
|
|
|
|
"%s\n" // texture buffer
|
|
|
|
"%s\n" // ES texture buffer
|
|
|
|
"%s\n" // ES dual source blend
|
2016-11-27 08:14:56 +00:00
|
|
|
"%s\n" // shader image load store
|
2017-10-26 05:44:39 +00:00
|
|
|
"%s\n" // shader framebuffer fetch
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Precision defines for GLSL ES
|
|
|
|
"%s\n"
|
|
|
|
"%s\n"
|
|
|
|
"%s\n"
|
|
|
|
"%s\n"
|
|
|
|
"%s\n"
|
2016-11-27 08:14:56 +00:00
|
|
|
"%s\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Silly differences
|
2019-02-15 01:59:50 +00:00
|
|
|
"#define API_OPENGL 1\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
"#define float2 vec2\n"
|
|
|
|
"#define float3 vec3\n"
|
|
|
|
"#define float4 vec4\n"
|
|
|
|
"#define uint2 uvec2\n"
|
|
|
|
"#define uint3 uvec3\n"
|
|
|
|
"#define uint4 uvec4\n"
|
|
|
|
"#define int2 ivec2\n"
|
|
|
|
"#define int3 ivec3\n"
|
|
|
|
"#define int4 ivec4\n"
|
|
|
|
"#define frac fract\n"
|
|
|
|
"#define lerp mix\n"
|
|
|
|
|
|
|
|
,
|
|
|
|
GetGLSLVersionString().c_str(),
|
2018-01-05 18:01:18 +00:00
|
|
|
v < Glsl140 ? "#extension GL_ARB_uniform_buffer_object : enable" : "", earlyz_string.c_str(),
|
|
|
|
(g_ActiveConfig.backend_info.bSupportsBindingLayout && v < GlslEs310) ?
|
2016-06-24 08:43:46 +00:00
|
|
|
"#extension GL_ARB_shading_language_420pack : enable" :
|
|
|
|
"",
|
2018-01-05 18:01:18 +00:00
|
|
|
(g_ogl_config.bSupportsMSAA && v < Glsl150) ?
|
2016-06-24 08:43:46 +00:00
|
|
|
"#extension GL_ARB_texture_multisample : enable" :
|
|
|
|
"",
|
2016-08-12 14:40:18 +00:00
|
|
|
// Attribute and fragment output bindings are still done via glBindAttribLocation and
|
|
|
|
// glBindFragDataLocation. In the future this could be moved to the layout qualifier
|
|
|
|
// in GLSL, but requires verification of GL_ARB_explicit_attrib_location.
|
2016-06-24 08:43:46 +00:00
|
|
|
g_ActiveConfig.backend_info.bSupportsBindingLayout ?
|
2016-08-12 14:40:18 +00:00
|
|
|
"#define ATTRIBUTE_LOCATION(x)\n"
|
|
|
|
"#define FRAGMENT_OUTPUT_LOCATION(x)\n"
|
|
|
|
"#define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y)\n"
|
|
|
|
"#define UBO_BINDING(packing, x) layout(packing, binding = x)\n"
|
|
|
|
"#define SAMPLER_BINDING(x) layout(binding = x)\n"
|
2019-02-15 01:59:50 +00:00
|
|
|
"#define TEXEL_BUFFER_BINDING(x) layout(binding = x)\n"
|
|
|
|
"#define SSBO_BINDING(x) layout(binding = x)\n"
|
|
|
|
"#define IMAGE_BINDING(format, x) layout(format, binding = x)\n" :
|
2016-08-12 14:40:18 +00:00
|
|
|
"#define ATTRIBUTE_LOCATION(x)\n"
|
|
|
|
"#define FRAGMENT_OUTPUT_LOCATION(x)\n"
|
|
|
|
"#define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y)\n"
|
|
|
|
"#define UBO_BINDING(packing, x) layout(packing)\n"
|
2019-02-15 01:59:50 +00:00
|
|
|
"#define SAMPLER_BINDING(x)\n"
|
|
|
|
"#define TEXEL_BUFFER_BINDING(x)\n"
|
|
|
|
"#define SSBO_BINDING(x)\n"
|
|
|
|
"#define IMAGE_BINDING(format, x) layout(format)\n",
|
2016-08-12 14:40:18 +00:00
|
|
|
// Input/output blocks are matched by name during program linking
|
|
|
|
"#define VARYING_LOCATION(x)\n",
|
2017-03-05 23:17:54 +00:00
|
|
|
!is_glsles && g_ActiveConfig.backend_info.bSupportsFragmentStoresAndAtomics ?
|
2016-06-24 08:43:46 +00:00
|
|
|
"#extension GL_ARB_shader_storage_buffer_object : enable" :
|
|
|
|
"",
|
2018-01-05 18:01:18 +00:00
|
|
|
v < Glsl400 && g_ActiveConfig.backend_info.bSupportsGSInstancing ?
|
2016-06-24 08:43:46 +00:00
|
|
|
"#extension GL_ARB_gpu_shader5 : enable" :
|
|
|
|
"",
|
2018-01-05 18:01:18 +00:00
|
|
|
v < Glsl400 && g_ActiveConfig.backend_info.bSupportsSSAA ?
|
2016-06-24 08:43:46 +00:00
|
|
|
"#extension GL_ARB_sample_shading : enable" :
|
|
|
|
"",
|
|
|
|
SupportedESPointSize.c_str(),
|
|
|
|
g_ogl_config.bSupportsAEP ? "#extension GL_ANDROID_extension_pack_es31a : enable" : "",
|
2018-01-05 18:01:18 +00:00
|
|
|
v < Glsl140 && g_ActiveConfig.backend_info.bSupportsPaletteConversion ?
|
2016-06-24 08:43:46 +00:00
|
|
|
"#extension GL_ARB_texture_buffer_object : enable" :
|
|
|
|
"",
|
|
|
|
SupportedESTextureBuffer.c_str(),
|
|
|
|
is_glsles && g_ActiveConfig.backend_info.bSupportsDualSourceBlend ?
|
|
|
|
"#extension GL_EXT_blend_func_extended : enable" :
|
|
|
|
""
|
|
|
|
|
|
|
|
,
|
2016-11-27 08:14:56 +00:00
|
|
|
g_ogl_config.bSupportsImageLoadStore &&
|
2018-01-05 18:01:18 +00:00
|
|
|
((!is_glsles && v < Glsl430) || (is_glsles && v < GlslEs310)) ?
|
2016-11-27 08:14:56 +00:00
|
|
|
"#extension GL_ARB_shader_image_load_store : enable" :
|
|
|
|
"",
|
2017-10-26 05:44:39 +00:00
|
|
|
framebuffer_fetch_string.c_str(), is_glsles ? "precision highp float;" : "",
|
|
|
|
is_glsles ? "precision highp int;" : "", is_glsles ? "precision highp sampler2DArray;" : "",
|
2016-06-24 08:43:46 +00:00
|
|
|
(is_glsles && g_ActiveConfig.backend_info.bSupportsPaletteConversion) ?
|
|
|
|
"precision highp usamplerBuffer;" :
|
|
|
|
"",
|
2018-01-05 18:01:18 +00:00
|
|
|
v > GlslEs300 ? "precision highp sampler2DMS;" : "",
|
|
|
|
v >= GlslEs310 ? "precision highp image2DArray;" : "");
|
2013-02-13 20:34:48 +00:00
|
|
|
}
|
2018-02-25 07:56:09 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
u64 ProgramShaderCache::GenerateShaderID()
|
|
|
|
{
|
|
|
|
return s_shader_counter++;
|
|
|
|
}
|
|
|
|
|
2018-02-25 07:56:09 +00:00
|
|
|
bool SharedContextAsyncShaderCompiler::WorkerThreadInitMainThread(void** param)
|
|
|
|
{
|
2018-10-03 13:03:26 +00:00
|
|
|
std::unique_ptr<GLContext> context =
|
|
|
|
static_cast<Renderer*>(g_renderer.get())->GetMainGLContext()->CreateSharedContext();
|
2018-02-25 07:56:09 +00:00
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to create shared context for shader compiling.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*param = context.release();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedContextAsyncShaderCompiler::WorkerThreadInitWorkerThread(void* param)
|
|
|
|
{
|
2018-10-03 13:02:45 +00:00
|
|
|
GLContext* context = static_cast<GLContext*>(param);
|
2018-02-25 07:56:09 +00:00
|
|
|
if (!context->MakeCurrent())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
s_is_shared_context = true;
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPrimitiveRestart)
|
2018-10-03 13:03:26 +00:00
|
|
|
GLUtil::EnablePrimitiveRestart(context);
|
2018-02-25 07:56:09 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedContextAsyncShaderCompiler::WorkerThreadExit(void* param)
|
|
|
|
{
|
2018-10-03 13:02:45 +00:00
|
|
|
GLContext* context = static_cast<GLContext*>(param);
|
2018-02-25 07:56:09 +00:00
|
|
|
context->ClearCurrent();
|
|
|
|
delete context;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
} // namespace OGL
|