2017-09-08 09:42:56 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-09-08 09:42:56 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/OGL/OGLShader.h"
|
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
|
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
static GLenum GetGLShaderTypeForStage(ShaderStage stage)
|
|
|
|
{
|
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case ShaderStage::Vertex:
|
|
|
|
return GL_VERTEX_SHADER;
|
|
|
|
case ShaderStage::Geometry:
|
|
|
|
return GL_GEOMETRY_SHADER;
|
|
|
|
case ShaderStage::Pixel:
|
|
|
|
return GL_FRAGMENT_SHADER;
|
|
|
|
case ShaderStage::Compute:
|
|
|
|
return GL_COMPUTE_SHADER;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
OGLShader::OGLShader(ShaderStage stage, GLenum gl_type, GLuint gl_id, std::string source,
|
|
|
|
std::string name)
|
2019-02-15 01:59:50 +00:00
|
|
|
: AbstractShader(stage), m_id(ProgramShaderCache::GenerateShaderID()), m_type(gl_type),
|
2021-08-28 05:30:05 +00:00
|
|
|
m_gl_id(gl_id), m_source(std::move(source)), m_name(std::move(name))
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
if (!m_name.empty())
|
|
|
|
{
|
|
|
|
glObjectLabel(GetGLShaderTypeForStage(stage), m_gl_id, -1, m_name.c_str());
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
OGLShader::OGLShader(GLuint gl_compute_program_id, std::string source, std::string name)
|
2019-02-15 01:59:50 +00:00
|
|
|
: AbstractShader(ShaderStage::Compute), m_id(ProgramShaderCache::GenerateShaderID()),
|
2019-04-20 13:39:54 +00:00
|
|
|
m_type(GL_COMPUTE_SHADER), m_gl_compute_program_id(gl_compute_program_id),
|
2021-08-28 05:30:05 +00:00
|
|
|
m_source(std::move(source)), m_name(std::move(name))
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
if (!m_name.empty())
|
|
|
|
{
|
|
|
|
glObjectLabel(GL_COMPUTE_SHADER, m_gl_compute_program_id, -1, m_name.c_str());
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OGLShader::~OGLShader()
|
|
|
|
{
|
|
|
|
if (m_stage != ShaderStage::Compute)
|
2019-02-15 01:59:50 +00:00
|
|
|
glDeleteShader(m_gl_id);
|
2017-09-08 09:42:56 +00:00
|
|
|
else
|
2019-02-15 01:59:50 +00:00
|
|
|
glDeleteProgram(m_gl_compute_program_id);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
std::unique_ptr<OGLShader> OGLShader::CreateFromSource(ShaderStage stage, std::string_view source,
|
|
|
|
std::string_view name)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2019-05-30 07:07:40 +00:00
|
|
|
std::string source_str(source);
|
2021-08-28 05:30:05 +00:00
|
|
|
std::string name_str(name);
|
2017-09-08 09:42:56 +00:00
|
|
|
if (stage != ShaderStage::Compute)
|
|
|
|
{
|
|
|
|
GLenum shader_type = GetGLShaderTypeForStage(stage);
|
2019-04-20 13:39:54 +00:00
|
|
|
GLuint shader_id = ProgramShaderCache::CompileSingleShader(shader_type, source_str);
|
2017-09-08 09:42:56 +00:00
|
|
|
if (!shader_id)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
return std::make_unique<OGLShader>(stage, shader_type, shader_id, std::move(source_str),
|
|
|
|
std::move(name_str));
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute shaders.
|
|
|
|
SHADER prog;
|
2019-04-20 13:39:54 +00:00
|
|
|
if (!ProgramShaderCache::CompileComputeShader(prog, source_str))
|
2017-09-08 09:42:56 +00:00
|
|
|
return nullptr;
|
2021-08-28 05:30:05 +00:00
|
|
|
return std::make_unique<OGLShader>(prog.glprogid, std::move(source_str), std::move(name_str));
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace OGL
|