2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-12-09 23:30:05 +00:00
|
|
|
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
|
2019-07-26 21:48:33 +00:00
|
|
|
#include <array>
|
2019-02-15 01:59:50 +00:00
|
|
|
#include <atomic>
|
2015-12-21 15:15:17 +00:00
|
|
|
#include <memory>
|
2014-03-30 18:58:05 +00:00
|
|
|
#include <string>
|
|
|
|
|
2022-10-12 02:21:51 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
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"
|
2020-01-24 08:29:38 +00:00
|
|
|
#include "Common/Version.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2016-11-27 21:31:48 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2022-12-27 16:42:02 +00:00
|
|
|
#include "Core/System.h"
|
2016-11-27 21:31:48 +00:00
|
|
|
|
2023-01-27 00:21:09 +00:00
|
|
|
#include "VideoBackends/OGL/OGLConfig.h"
|
|
|
|
#include "VideoBackends/OGL/OGLGfx.h"
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "VideoBackends/OGL/OGLShader.h"
|
2020-09-15 12:00:24 +00:00
|
|
|
#include "VideoBackends/OGL/OGLStreamBuffer.h"
|
|
|
|
#include "VideoBackends/OGL/OGLVertexManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "VideoCommon/AsyncShaderCompiler.h"
|
2014-12-14 20:23:13 +00:00
|
|
|
#include "VideoCommon/GeometryShaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#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"
|
2020-01-24 08:29:38 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2019-12-22 18:40:40 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2011-12-10 21:58:44 +00:00
|
|
|
|
2011-12-01 03:00:21 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2013-01-14 12:58:11 +00:00
|
|
|
u32 ProgramShaderCache::s_ubo_buffer_size;
|
2020-08-27 20:00:36 +00:00
|
|
|
s32 ProgramShaderCache::s_ubo_align = 1;
|
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;
|
2019-03-02 18:42:25 +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;
|
2014-03-30 18:58:05 +00:00
|
|
|
switch (v)
|
|
|
|
{
|
2018-01-05 18:01:18 +00:00
|
|
|
case GlslEs300:
|
2014-03-30 18:58:05 +00:00
|
|
|
return "#version 300 es";
|
2018-01-05 18:01:18 +00:00
|
|
|
case GlslEs310:
|
2014-03-30 18:58:05 +00:00
|
|
|
return "#version 310 es";
|
2018-01-05 18:01:18 +00:00
|
|
|
case GlslEs320:
|
2015-08-26 05:09:32 +00:00
|
|
|
return "#version 320 es";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl130:
|
2014-03-30 18:58:05 +00:00
|
|
|
return "#version 130";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl140:
|
2014-03-30 18:58:05 +00:00
|
|
|
return "#version 140";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl150:
|
2014-03-30 18:58:05 +00:00
|
|
|
return "#version 150";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl330:
|
2015-09-06 11:58:18 +00:00
|
|
|
return "#version 330";
|
2018-01-05 18:01:18 +00:00
|
|
|
case Glsl400:
|
2015-09-06 11:58:18 +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";
|
2023-01-31 17:10:48 +00:00
|
|
|
case Glsl450:
|
|
|
|
return "#version 450";
|
2014-12-03 00:20:52 +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);
|
|
|
|
|
2014-06-07 05:29:21 +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)
|
2023-09-20 00:29:38 +00:00
|
|
|
glUniformBlockBinding(glprogid, GSBlock_id, 4);
|
2018-03-03 09:10:45 +00:00
|
|
|
if (UBERBlock_id != -1)
|
2023-09-20 00:29:38 +00:00
|
|
|
glUniformBlockBinding(glprogid, UBERBlock_id, 5);
|
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
|
2022-10-12 02:21:51 +00:00
|
|
|
int loc = glGetUniformLocation(glprogid, fmt::format("samp[{}]", a).c_str());
|
2019-02-15 01:59:50 +00:00
|
|
|
if (loc < 0)
|
2022-10-12 02:21:51 +00:00
|
|
|
loc = glGetUniformLocation(glprogid, fmt::format("samp{}", a).c_str());
|
2019-02-15 01:59:50 +00:00
|
|
|
if (loc >= 0)
|
2018-03-03 09:10:45 +00:00
|
|
|
glUniform1i(loc, a);
|
2012-12-31 01:34:27 +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
|
|
|
{
|
2023-05-29 01:59:02 +00:00
|
|
|
if (g_ogl_config.bSupportsExplicitLayoutInShader)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-11-27 08:14:56 +00:00
|
|
|
if (!is_compute)
|
2012-12-31 01:34:27 +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
|
2022-10-11 21:23:38 +00:00
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::Position), "rawpos");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::PositionMatrix), "posmtx");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::Color0), "rawcolor0");
|
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::Color1), "rawcolor1");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-10-11 21:23:38 +00:00
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::Normal), "rawnormal");
|
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::Tangent), "rawtangent");
|
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::Binormal), "rawbinormal");
|
2016-11-27 08:14:56 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-15 18:09:53 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
2022-10-12 02:21:51 +00:00
|
|
|
// Per documentation: OpenGL copies the name string when glBindAttribLocation is called, so an
|
|
|
|
// application may free its copy of the name string immediately after the function returns.
|
2022-10-11 21:23:38 +00:00
|
|
|
glBindAttribLocation(glprogid, static_cast<GLuint>(ShaderAttrib::TexCoord0 + i),
|
|
|
|
fmt::format("rawtex{}", i).c_str());
|
2013-01-14 21:59:08 +00:00
|
|
|
}
|
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
|
|
|
{
|
2014-03-10 11:30:55 +00:00
|
|
|
if (CurrentProgram != glprogid)
|
2013-02-13 12:12:19 +00:00
|
|
|
{
|
2019-07-11 03:34:50 +00:00
|
|
|
INCSTAT(g_stats.this_frame.num_shader_changes);
|
2013-02-13 12:12:19 +00:00
|
|
|
glUseProgram(glprogid);
|
|
|
|
CurrentProgram = glprogid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-12-27 16:42:02 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto& pixel_shader_manager = system.GetPixelShaderManager();
|
2022-12-28 14:38:46 +00:00
|
|
|
auto& vertex_shader_manager = system.GetVertexShaderManager();
|
2022-12-29 14:27:48 +00:00
|
|
|
auto& geometry_shader_manager = system.GetGeometryShaderManager();
|
2023-09-20 00:29:38 +00:00
|
|
|
if (pixel_shader_manager.dirty || vertex_shader_manager.dirty || geometry_shader_manager.dirty ||
|
|
|
|
pixel_shader_manager.custom_constants_dirty)
|
2013-10-07 15:19:47 +00:00
|
|
|
{
|
2023-09-20 00:29:38 +00:00
|
|
|
const u32 custom_constants_size = static_cast<u32>(
|
|
|
|
Common::AlignUp(pixel_shader_manager.custom_constants.size(), s_ubo_align));
|
|
|
|
auto buffer = s_buffer->Map(s_ubo_buffer_size + custom_constants_size, s_ubo_align);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-12-27 16:42:02 +00:00
|
|
|
memcpy(buffer.first, &pixel_shader_manager.constants, sizeof(PixelShaderConstants));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-09-20 00:29:38 +00:00
|
|
|
u64 size = Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-09-20 00:29:38 +00:00
|
|
|
memcpy(buffer.first + size, &vertex_shader_manager.constants, sizeof(VertexShaderConstants));
|
|
|
|
size += Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align);
|
|
|
|
|
|
|
|
if (!pixel_shader_manager.custom_constants.empty())
|
|
|
|
{
|
|
|
|
memcpy(buffer.first + size, pixel_shader_manager.custom_constants.data(),
|
|
|
|
pixel_shader_manager.custom_constants.size());
|
|
|
|
size += custom_constants_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer.first + size, &geometry_shader_manager.constants,
|
|
|
|
sizeof(GeometryShaderConstants));
|
|
|
|
|
|
|
|
s_buffer->Unmap(s_ubo_buffer_size + custom_constants_size);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-30 15:38:11 +00:00
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_buffer->m_buffer, buffer.second,
|
|
|
|
sizeof(PixelShaderConstants));
|
2023-09-20 00:29:38 +00:00
|
|
|
size = Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align);
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_buffer->m_buffer, buffer.second + size,
|
2014-01-30 15:38:11 +00:00
|
|
|
sizeof(VertexShaderConstants));
|
2023-09-20 00:29:38 +00:00
|
|
|
size += Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align);
|
|
|
|
|
|
|
|
if (!pixel_shader_manager.custom_constants.empty())
|
|
|
|
{
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 3, s_buffer->m_buffer, buffer.second + size,
|
|
|
|
pixel_shader_manager.custom_constants.size());
|
|
|
|
size += Common::AlignUp(pixel_shader_manager.custom_constants.size(), s_ubo_align);
|
|
|
|
}
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 4, s_buffer->m_buffer, buffer.second + size,
|
2014-12-14 20:23:13 +00:00
|
|
|
sizeof(GeometryShaderConstants));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-12-27 16:42:02 +00:00
|
|
|
pixel_shader_manager.dirty = false;
|
2022-12-28 14:38:46 +00:00
|
|
|
vertex_shader_manager.dirty = false;
|
2022-12-29 14:27:48 +00:00
|
|
|
geometry_shader_manager.dirty = false;
|
2023-09-20 00:29:38 +00:00
|
|
|
pixel_shader_manager.custom_constants_dirty = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2023-09-20 00:29:38 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, s_ubo_buffer_size + custom_constants_size);
|
2011-12-11 10:32:57 +00:00
|
|
|
}
|
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
|
2023-09-20 00:29:38 +00:00
|
|
|
for (u32 index = 1; index <= 4; index++)
|
2018-11-27 07:16:53 +00:00
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, index, s_buffer->m_buffer, buffer.second, data_size);
|
|
|
|
|
2019-07-11 03:34:50 +00:00
|
|
|
ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size);
|
2018-11-27 07:16:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 21:48:33 +00:00
|
|
|
bool ProgramShaderCache::CompileComputeShader(SHADER& shader, std::string_view code)
|
2016-11-27 08:14:56 +00:00
|
|
|
{
|
|
|
|
// We need to enable GL_ARB_compute_shader for drivers that support the extension,
|
|
|
|
// but not GLSL 4.3. Mesa is one example.
|
2019-07-26 21:48:33 +00:00
|
|
|
std::string full_code;
|
2016-11-27 08:14:56 +00:00
|
|
|
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
|
|
|
{
|
2019-07-26 21:48:33 +00:00
|
|
|
full_code = "#extension GL_ARB_compute_shader : enable\n";
|
2016-11-27 08:14:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 21:48:33 +00:00
|
|
|
full_code += code;
|
|
|
|
const 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);
|
|
|
|
|
2019-07-26 21:48:33 +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;
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:48:33 +00:00
|
|
|
GLuint ProgramShaderCache::CompileSingleShader(GLenum type, std::string_view code)
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2019-07-26 21:48:33 +00:00
|
|
|
const GLuint result = glCreateShader(type);
|
|
|
|
|
|
|
|
constexpr GLsizei num_strings = 2;
|
|
|
|
const std::array<const char*, num_strings> src{
|
|
|
|
s_glsl_header.data(),
|
|
|
|
code.data(),
|
|
|
|
};
|
|
|
|
const std::array<GLint, num_strings> src_sizes{
|
|
|
|
static_cast<GLint>(s_glsl_header.size()),
|
|
|
|
static_cast<GLint>(code.size()),
|
|
|
|
};
|
|
|
|
|
|
|
|
glShaderSource(result, num_strings, src.data(), src_sizes.data());
|
2013-02-13 12:12:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:48:33 +00:00
|
|
|
bool ProgramShaderCache::CheckShaderCompileResult(GLuint id, GLenum type, std::string_view code)
|
2017-07-20 05:25:29 +00:00
|
|
|
{
|
2013-03-07 19:26:56 +00:00
|
|
|
GLint compileStatus;
|
2017-07-20 05:25:29 +00:00
|
|
|
glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus);
|
2013-02-13 12:12:19 +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)
|
2013-02-13 12:12:19 +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
|
|
|
{
|
2020-11-09 08:08:00 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "{} failed compilation:\n{}", prefix, info_log);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-01-24 08:29:38 +00:00
|
|
|
std::string filename = VideoBackendBase::BadShaderFilename(prefix, num_failures++);
|
2018-02-25 08:03:58 +00:00
|
|
|
std::ofstream file;
|
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
|
|
|
file << s_glsl_header << code << info_log;
|
2020-01-24 08:29:38 +00:00
|
|
|
file << "\n";
|
2022-01-13 23:04:22 +00:00
|
|
|
file << "Dolphin Version: " + Common::GetScmRevStr() + "\n";
|
2020-01-24 08:29:38 +00:00
|
|
|
file << "Video Backend: " + g_video_backend->GetDisplayName();
|
2018-02-25 08:03:58 +00:00
|
|
|
file.close();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to compile {} shader: {}\n"
|
|
|
|
"Debug info ({}, {}, {}):\n{}",
|
|
|
|
prefix, filename, g_ogl_config.gl_vendor, g_ogl_config.gl_renderer,
|
|
|
|
g_ogl_config.gl_version, info_log);
|
2018-02-25 08:03:58 +00:00
|
|
|
|
|
|
|
return false;
|
2014-08-15 18:09:53 +00:00
|
|
|
}
|
2018-02-25 08:03:58 +00:00
|
|
|
|
2020-11-09 08:08:00 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "{} compiled with warnings:\n{}", prefix, info_log);
|
2013-02-13 12:12:19 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-07-26 21:48:33 +00:00
|
|
|
bool ProgramShaderCache::CheckProgramLinkResult(GLuint id, std::string_view vcode,
|
|
|
|
std::string_view pcode, std::string_view 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
|
|
|
{
|
2020-11-09 08:08:00 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Program failed linking:\n{}", info_log);
|
2020-01-24 08:29:38 +00:00
|
|
|
std::string filename = VideoBackendBase::BadShaderFilename("p", num_failures++);
|
2018-02-25 08:03:58 +00:00
|
|
|
std::ofstream file;
|
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
2019-07-26 21:48:33 +00:00
|
|
|
if (!vcode.empty())
|
|
|
|
file << s_glsl_header << vcode << '\n';
|
|
|
|
if (!gcode.empty())
|
|
|
|
file << s_glsl_header << gcode << '\n';
|
|
|
|
if (!pcode.empty())
|
|
|
|
file << s_glsl_header << pcode << '\n';
|
2019-04-20 13:39:54 +00:00
|
|
|
|
2018-02-25 08:03:58 +00:00
|
|
|
file << info_log;
|
2020-01-24 08:29:38 +00:00
|
|
|
file << "\n";
|
2022-01-13 23:04:22 +00:00
|
|
|
file << "Dolphin Version: " + Common::GetScmRevStr() + "\n";
|
2020-01-24 08:29:38 +00:00
|
|
|
file << "Video Backend: " + g_video_backend->GetDisplayName();
|
2018-02-25 08:03:58 +00:00
|
|
|
file.close();
|
2017-07-20 05:25:29 +00:00
|
|
|
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to link shaders: {}\n"
|
|
|
|
"Debug info ({}, {}, {}):\n{}",
|
|
|
|
filename, g_ogl_config.gl_vendor, g_ogl_config.gl_renderer,
|
|
|
|
g_ogl_config.gl_version, info_log);
|
2017-07-20 05:25:29 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-25 08:03:58 +00:00
|
|
|
|
2020-11-09 08:08:00 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Program linked with warnings:\n{}", info_log);
|
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
|
|
|
{
|
|
|
|
// We have to get the UBO alignment here because
|
|
|
|
// if we generate a buffer that isn't aligned
|
|
|
|
// then the UBO will fail.
|
2014-01-30 15:38:11 +00:00
|
|
|
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &s_ubo_align);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
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
|
|
|
|
2014-01-30 15:38:11 +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
|
2019-03-24 04:24:03 +00:00
|
|
|
s_buffer = StreamBuffer::Create(GL_UNIFORM_BUFFER, VertexManagerBase::UNIFORM_STREAM_BUFFER_SIZE);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-15 06:38:24 +00:00
|
|
|
void ProgramShaderCache::ReBindVertexFormat()
|
|
|
|
{
|
|
|
|
if (s_last_VAO)
|
|
|
|
glBindVertexArray(s_last_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
|
|
|
}
|
|
|
|
|
2019-03-05 13:02:40 +00:00
|
|
|
void ProgramShaderCache::InvalidateVertexFormatIfBound(GLuint vao)
|
|
|
|
{
|
|
|
|
if (s_last_VAO == vao)
|
|
|
|
s_last_VAO = 0;
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
void ProgramShaderCache::InvalidateLastProgram()
|
|
|
|
{
|
|
|
|
CurrentProgram = 0;
|
|
|
|
}
|
|
|
|
|
2019-04-15 13:38:10 +00:00
|
|
|
PipelineProgram* ProgramShaderCache::GetPipelineProgram(const GLVertexFormat* vertex_format,
|
|
|
|
const OGLShader* vertex_shader,
|
|
|
|
const OGLShader* geometry_shader,
|
|
|
|
const OGLShader* pixel_shader,
|
|
|
|
const void* cache_data,
|
|
|
|
size_t cache_data_size)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
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
|
|
|
{
|
2019-07-26 22:22:52 +00:00
|
|
|
std::lock_guard guard{s_pipeline_program_lock};
|
2018-02-25 07:56:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PipelineProgram> prog = std::make_unique<PipelineProgram>();
|
|
|
|
prog->key = key;
|
|
|
|
prog->shader.glprogid = glCreateProgram();
|
2019-04-15 13:38:10 +00:00
|
|
|
|
|
|
|
// Use the cache data, if present. If this fails, we want to return an error, so the shader cache
|
|
|
|
// doesn't attempt to use the same binary data in the future.
|
|
|
|
if (cache_data_size >= sizeof(u32))
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2019-04-15 13:38:10 +00:00
|
|
|
u32 program_binary_type;
|
|
|
|
std::memcpy(&program_binary_type, cache_data, sizeof(u32));
|
|
|
|
glProgramBinary(prog->shader.glprogid, static_cast<GLenum>(program_binary_type),
|
|
|
|
static_cast<const u8*>(cache_data) + sizeof(u32),
|
|
|
|
static_cast<GLsizei>(cache_data_size - sizeof(u32)));
|
|
|
|
|
|
|
|
// Check the link status. If this fails, it means the binary was invalid.
|
|
|
|
GLint link_status;
|
|
|
|
glGetProgramiv(prog->shader.glprogid, GL_LINK_STATUS, &link_status);
|
|
|
|
if (link_status != GL_TRUE)
|
|
|
|
{
|
2020-11-09 08:08:00 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Failed to create GL program from program binary.");
|
2019-04-15 13:38:10 +00:00
|
|
|
prog->shader.Destroy();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to retrieve this binary and duplicate entries in the cache again.
|
|
|
|
// See the explanation in OGLPipeline.cpp.
|
|
|
|
prog->binary_retrieved = true;
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
2019-04-15 13:38:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Attach shaders.
|
|
|
|
ASSERT(vertex_shader && vertex_shader->GetStage() == ShaderStage::Vertex);
|
|
|
|
ASSERT(pixel_shader && pixel_shader->GetStage() == ShaderStage::Pixel);
|
|
|
|
glAttachShader(prog->shader.glprogid, vertex_shader->GetGLShaderID());
|
|
|
|
glAttachShader(prog->shader.glprogid, pixel_shader->GetGLShaderID());
|
|
|
|
if (geometry_shader)
|
|
|
|
{
|
|
|
|
ASSERT(geometry_shader->GetStage() == ShaderStage::Geometry);
|
|
|
|
glAttachShader(prog->shader.glprogid, geometry_shader->GetGLShaderID());
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
|
2019-04-15 13:38:10 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsPipelineCacheData)
|
|
|
|
glProgramParameteri(prog->shader.glprogid, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
|
2018-02-25 07:56:09 +00:00
|
|
|
|
2019-04-15 13:38:10 +00:00
|
|
|
// Link program.
|
|
|
|
prog->shader.SetProgramBindings(false);
|
|
|
|
glLinkProgram(prog->shader.glprogid);
|
2018-02-25 07:56:09 +00:00
|
|
|
|
2019-04-15 13:38:10 +00:00
|
|
|
// Restore VAO binding after linking.
|
|
|
|
if (!s_is_shared_context && vao != s_last_VAO)
|
|
|
|
glBindVertexArray(s_last_VAO);
|
|
|
|
|
2019-07-26 21:48:33 +00:00
|
|
|
if (!CheckProgramLinkResult(prog->shader.glprogid,
|
|
|
|
vertex_shader ? vertex_shader->GetSource() : std::string_view{},
|
|
|
|
geometry_shader ? geometry_shader->GetSource() : std::string_view{},
|
|
|
|
pixel_shader ? pixel_shader->GetSource() : std::string_view{}))
|
2019-04-15 13:38:10 +00:00
|
|
|
{
|
|
|
|
prog->shader.Destroy();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 12:14:31 +00:00
|
|
|
// Lock to insert. A duplicate program may have been created in the meantime.
|
2019-07-26 22:22:52 +00:00
|
|
|
std::lock_guard guard{s_pipeline_program_lock};
|
2018-02-25 07:56:09 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-04-15 13:38:10 +00:00
|
|
|
void ProgramShaderCache::ReleasePipelineProgram(PipelineProgram* prog)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2019-04-15 13:38:10 +00:00
|
|
|
if (--prog->reference_count > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
prog->shader.Destroy();
|
|
|
|
|
2019-07-26 22:22:52 +00:00
|
|
|
std::lock_guard guard{s_pipeline_program_lock};
|
|
|
|
const 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());
|
2019-04-15 13:38:10 +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;
|
2015-08-26 05:09:32 +00:00
|
|
|
std::string SupportedESPointSize;
|
2015-09-01 10:23:43 +00:00
|
|
|
std::string SupportedESTextureBuffer;
|
2015-08-26 05:09:32 +00:00
|
|
|
switch (g_ogl_config.SupportedESPointSize)
|
|
|
|
{
|
2023-03-06 05:50:00 +00:00
|
|
|
case EsPointSizeType::PointSizeOes:
|
2015-08-26 05:09:32 +00:00
|
|
|
SupportedESPointSize = "#extension GL_OES_geometry_point_size : enable";
|
|
|
|
break;
|
2023-03-06 05:50:00 +00:00
|
|
|
case EsPointSizeType::PointSizeExt:
|
2015-08-26 05:09:32 +00:00
|
|
|
SupportedESPointSize = "#extension GL_EXT_geometry_point_size : enable";
|
|
|
|
break;
|
2023-03-06 05:50:00 +00:00
|
|
|
case EsPointSizeType::PointSizeNone:
|
2015-08-26 05:09:32 +00:00
|
|
|
default:
|
|
|
|
SupportedESPointSize = "";
|
2016-06-24 08:43:46 +00:00
|
|
|
break;
|
2015-08-26 05:09:32 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-09-01 10:23:43 +00:00
|
|
|
switch (g_ogl_config.SupportedESTextureBuffer)
|
|
|
|
{
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsTexbufType::TexbufExt:
|
2015-09-01 10:23:43 +00:00
|
|
|
SupportedESTextureBuffer = "#extension GL_EXT_texture_buffer : enable";
|
|
|
|
break;
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsTexbufType::TexbufOes:
|
2015-09-01 10:23:43 +00:00
|
|
|
SupportedESTextureBuffer = "#extension GL_OES_texture_buffer : enable";
|
|
|
|
break;
|
2018-01-05 18:01:18 +00:00
|
|
|
case EsTexbufType::TexbufCore:
|
|
|
|
case EsTexbufType::TexbufNone:
|
2015-09-01 10:23:43 +00:00
|
|
|
SupportedESTextureBuffer = "";
|
|
|
|
break;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-02 18:42:25 +00:00
|
|
|
std::string earlyz_string;
|
2015-10-17 13:37:14 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsEarlyZ)
|
2015-10-17 11:43:22 +00:00
|
|
|
{
|
2016-11-27 08:14:56 +00:00
|
|
|
if (g_ogl_config.bSupportsImageLoadStore)
|
2015-10-17 11:43:22 +00:00
|
|
|
{
|
2015-10-17 13:37:14 +00:00
|
|
|
earlyz_string = "#define FORCE_EARLY_Z layout(early_fragment_tests) in\n";
|
2015-10-17 11:43:22 +00:00
|
|
|
}
|
|
|
|
else if (g_ogl_config.bSupportsConservativeDepth)
|
|
|
|
{
|
|
|
|
// See PixelShaderGen for details about this fallback.
|
2015-10-17 13:37:14 +00:00
|
|
|
earlyz_string = "#define FORCE_EARLY_Z layout(depth_unchanged) out float gl_FragDepth\n";
|
|
|
|
earlyz_string += "#extension GL_ARB_conservative_depth : enable\n";
|
2015-10-17 11:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
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"
|
|
|
|
"#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;
|
|
|
|
}
|
|
|
|
|
2023-03-06 06:03:26 +00:00
|
|
|
// The sampler2DMSArray keyword is reserved in GLSL ES 3.0 and 3.1, but is available in 3.2 and
|
|
|
|
// with GL_OES_texture_storage_multisample_2d_array for 3.1.
|
|
|
|
// See https://bugs.dolphin-emu.org/issues/13198.
|
|
|
|
const bool use_multisample_2d_array_precision =
|
|
|
|
v >= GlslEs320 ||
|
|
|
|
g_ogl_config.SupportedMultisampleTexStorage != MultisampleTexStorageType::TexStorageNone;
|
2023-03-06 06:02:42 +00:00
|
|
|
|
2023-05-29 01:59:02 +00:00
|
|
|
std::string binding_layout;
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsBindingLayout)
|
|
|
|
{
|
|
|
|
if (g_ogl_config.bSupportsExplicitLayoutInShader)
|
|
|
|
{
|
|
|
|
binding_layout =
|
|
|
|
"#extension GL_ARB_explicit_attrib_location : enable\n"
|
|
|
|
"#define ATTRIBUTE_LOCATION(x) layout(location = x)\n"
|
|
|
|
"#define FRAGMENT_OUTPUT_LOCATION(x) layout(location = x)\n"
|
|
|
|
"#define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y) layout(location = x, index = y)\n"
|
|
|
|
"#define UBO_BINDING(packing, x) layout(packing, binding = x)\n"
|
|
|
|
"#define SAMPLER_BINDING(x) layout(binding = x)\n"
|
|
|
|
"#define TEXEL_BUFFER_BINDING(x) layout(binding = x)\n"
|
|
|
|
"#define SSBO_BINDING(x) layout(std430, binding = x)\n"
|
|
|
|
"#define IMAGE_BINDING(format, x) layout(format, binding = x)\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
binding_layout = "#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"
|
|
|
|
"#define TEXEL_BUFFER_BINDING(x) layout(binding = x)\n"
|
|
|
|
"#define SSBO_BINDING(x) layout(std430, binding = x)\n"
|
|
|
|
"#define IMAGE_BINDING(format, x) layout(format, binding = x)\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
binding_layout = "#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"
|
|
|
|
"#define SAMPLER_BINDING(x)\n"
|
|
|
|
"#define TEXEL_BUFFER_BINDING(x)\n"
|
|
|
|
"#define SSBO_BINDING(x) layout(std430)\n"
|
|
|
|
"#define IMAGE_BINDING(format, x) layout(format)\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: actually define this if using 'bSupportsExplicitLayoutInShader'
|
|
|
|
const std::string varying_location = "#define VARYING_LOCATION(x)\n";
|
|
|
|
|
2019-03-22 10:39:11 +00:00
|
|
|
std::string shader_shuffle_string;
|
2023-01-31 17:10:48 +00:00
|
|
|
if (g_ogl_config.bSupportsKHRShaderSubgroup)
|
2019-03-22 10:39:11 +00:00
|
|
|
{
|
|
|
|
shader_shuffle_string = R"(
|
2023-01-31 17:10:48 +00:00
|
|
|
#extension GL_KHR_shader_subgroup_basic : enable
|
|
|
|
#extension GL_KHR_shader_subgroup_arithmetic : enable
|
|
|
|
#extension GL_KHR_shader_subgroup_ballot : enable
|
2019-03-22 10:39:11 +00:00
|
|
|
|
2023-01-31 17:10:48 +00:00
|
|
|
#define SUPPORTS_SUBGROUP_REDUCTION 1
|
|
|
|
#define IS_HELPER_INVOCATION gl_HelperInvocation
|
|
|
|
#define IS_FIRST_ACTIVE_INVOCATION (subgroupElect())
|
|
|
|
#define SUBGROUP_MIN(value) value = subgroupMin(value)
|
|
|
|
#define SUBGROUP_MAX(value) value = subgroupMax(value)
|
2019-03-22 10:39:11 +00:00
|
|
|
)";
|
|
|
|
}
|
|
|
|
|
2022-10-12 02:21:51 +00:00
|
|
|
s_glsl_header = fmt::format(
|
|
|
|
"{}\n"
|
|
|
|
"{}\n" // ubo
|
|
|
|
"{}\n" // early-z
|
|
|
|
"{}\n" // 420pack
|
|
|
|
"{}\n" // msaa
|
|
|
|
"{}\n" // Input/output/sampler binding
|
|
|
|
"{}\n" // Varying location
|
|
|
|
"{}\n" // storage buffer
|
|
|
|
"{}\n" // shader5
|
|
|
|
"{}\n" // SSAA
|
|
|
|
"{}\n" // Geometry point size
|
|
|
|
"{}\n" // AEP
|
|
|
|
"{}\n" // texture buffer
|
|
|
|
"{}\n" // ES texture buffer
|
|
|
|
"{}\n" // ES dual source blend
|
|
|
|
"{}\n" // shader image load store
|
|
|
|
"{}\n" // shader framebuffer fetch
|
|
|
|
"{}\n" // shader thread shuffle
|
|
|
|
"{}\n" // derivative control
|
|
|
|
"{}\n" // query levels
|
2023-03-06 06:03:26 +00:00
|
|
|
"{}\n" // OES multisample texture storage
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-30 18:58:05 +00:00
|
|
|
// Precision defines for GLSL ES
|
2022-10-12 02:21:51 +00:00
|
|
|
"{}\n"
|
|
|
|
"{}\n"
|
|
|
|
"{}\n"
|
|
|
|
"{}\n"
|
|
|
|
"{}\n"
|
|
|
|
"{}\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-02-13 20:34:48 +00:00
|
|
|
// Silly differences
|
2019-02-15 01:59:50 +00:00
|
|
|
"#define API_OPENGL 1\n"
|
2013-02-13 20:34:48 +00:00
|
|
|
"#define float2 vec2\n"
|
|
|
|
"#define float3 vec3\n"
|
|
|
|
"#define float4 vec4\n"
|
2013-08-14 13:06:47 +00:00
|
|
|
"#define uint2 uvec2\n"
|
|
|
|
"#define uint3 uvec3\n"
|
|
|
|
"#define uint4 uvec4\n"
|
2013-11-25 14:49:13 +00:00
|
|
|
"#define int2 ivec2\n"
|
|
|
|
"#define int3 ivec3\n"
|
|
|
|
"#define int4 ivec4\n"
|
2013-06-18 17:42:14 +00:00
|
|
|
"#define frac fract\n"
|
|
|
|
"#define lerp mix\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-30 18:58:05 +00:00
|
|
|
,
|
2022-10-12 02:21:51 +00:00
|
|
|
GetGLSLVersionString(), v < Glsl140 ? "#extension GL_ARB_uniform_buffer_object : enable" : "",
|
|
|
|
earlyz_string,
|
2018-01-05 18:01:18 +00:00
|
|
|
(g_ActiveConfig.backend_info.bSupportsBindingLayout && v < GlslEs310) ?
|
2014-03-30 18:58:05 +00:00
|
|
|
"#extension GL_ARB_shading_language_420pack : enable" :
|
|
|
|
"",
|
2018-01-05 18:01:18 +00:00
|
|
|
(g_ogl_config.bSupportsMSAA && v < Glsl150) ?
|
2014-05-01 10:45:05 +00:00
|
|
|
"#extension GL_ARB_texture_multisample : enable" :
|
|
|
|
"",
|
2023-05-29 01:59:02 +00:00
|
|
|
binding_layout.c_str(), varying_location.c_str(),
|
2017-03-05 23:17:54 +00:00
|
|
|
!is_glsles && g_ActiveConfig.backend_info.bSupportsFragmentStoresAndAtomics ?
|
2015-11-23 20:47:43 +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 ?
|
2015-09-06 11:58:18 +00:00
|
|
|
"#extension GL_ARB_gpu_shader5 : enable" :
|
|
|
|
"",
|
2018-01-05 18:01:18 +00:00
|
|
|
v < Glsl400 && g_ActiveConfig.backend_info.bSupportsSSAA ?
|
2015-09-07 20:21:11 +00:00
|
|
|
"#extension GL_ARB_sample_shading : enable" :
|
|
|
|
"",
|
2022-10-12 02:21:51 +00:00
|
|
|
SupportedESPointSize,
|
2014-12-07 05:29:36 +00:00
|
|
|
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 ?
|
2015-09-06 11:58:18 +00:00
|
|
|
"#extension GL_ARB_texture_buffer_object : enable" :
|
|
|
|
"",
|
2022-10-12 02:21:51 +00:00
|
|
|
SupportedESTextureBuffer,
|
2015-09-05 01:25:59 +00:00
|
|
|
is_glsles && g_ActiveConfig.backend_info.bSupportsDualSourceBlend ?
|
|
|
|
"#extension GL_EXT_blend_func_extended : enable" :
|
|
|
|
""
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-09-05 01:25:59 +00:00
|
|
|
,
|
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" :
|
|
|
|
"",
|
2022-10-12 02:21:51 +00:00
|
|
|
framebuffer_fetch_string, shader_shuffle_string,
|
2021-11-14 04:10:20 +00:00
|
|
|
g_ActiveConfig.backend_info.bSupportsCoarseDerivatives ?
|
|
|
|
"#extension GL_ARB_derivative_control : enable" :
|
|
|
|
"",
|
2021-11-14 04:10:55 +00:00
|
|
|
g_ActiveConfig.backend_info.bSupportsTextureQueryLevels ?
|
|
|
|
"#extension GL_ARB_texture_query_levels : enable" :
|
|
|
|
"",
|
2023-03-06 06:03:26 +00:00
|
|
|
// Note: GL_ARB_texture_storage_multisample doesn't have an #extension, as it doesn't
|
|
|
|
// need to change GLSL, but on GLES 3.1 sampler2DMSArray is a reserved keyword unless
|
|
|
|
// the extension is enabled. Thus, we don't need to check TexStorageCore/have an ARB version.
|
|
|
|
g_ogl_config.SupportedMultisampleTexStorage == MultisampleTexStorageType::TexStorageOes ?
|
|
|
|
"#extension GL_OES_texture_storage_multisample_2d_array : enable" :
|
|
|
|
"",
|
2019-03-22 10:39:11 +00:00
|
|
|
is_glsles ? "precision highp float;" : "", is_glsles ? "precision highp int;" : "",
|
|
|
|
is_glsles ? "precision highp sampler2DArray;" : "",
|
2015-09-01 10:23:43 +00:00
|
|
|
(is_glsles && g_ActiveConfig.backend_info.bSupportsPaletteConversion) ?
|
|
|
|
"precision highp usamplerBuffer;" :
|
|
|
|
"",
|
2023-03-06 06:02:42 +00:00
|
|
|
use_multisample_2d_array_precision ? "precision highp sampler2DMSArray;" : "",
|
2018-01-05 18:01:18 +00:00
|
|
|
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)
|
|
|
|
{
|
2023-01-27 00:21:09 +00:00
|
|
|
std::unique_ptr<GLContext> context = GetOGLGfx()->GetMainGLContext()->CreateSharedContext();
|
2018-02-25 07:56:09 +00:00
|
|
|
if (!context)
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to create shared context for shader compiling.");
|
2018-02-25 07:56:09 +00:00
|
|
|
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;
|
2019-04-21 02:37:22 +00:00
|
|
|
|
|
|
|
// Make the state match the main context to have a better chance of avoiding recompiles.
|
|
|
|
if (!context->IsGLES())
|
|
|
|
glEnable(GL_PROGRAM_POINT_SIZE);
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsClipControl)
|
|
|
|
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsDepthClamp)
|
|
|
|
{
|
|
|
|
glEnable(GL_CLIP_DISTANCE0);
|
|
|
|
glEnable(GL_CLIP_DISTANCE1);
|
|
|
|
glEnable(GL_DEPTH_CLAMP);
|
|
|
|
}
|
2018-02-25 07:56:09 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
} // namespace OGL
|