Debian Codegraph @

Source/Core/VideoBackends/OGL/OGLShader.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/OGL/OGLShader.h"
5
6 #include <algorithm>
7 #include <cctype>
8
9 #include "VideoBackends/OGL/ProgramShaderCache.h"
10
11 #include "VideoCommon/ShaderCompileUtils.h"
12 #include "VideoCommon/ShaderGenCommon.h"
13 #include "VideoCommon/VideoConfig.h"
14
15 namespace OGL
16 {
17 namespace
18 {
19 std::string ResolveIncludeStatements(glslang::TShader::Includer* shader_includer,
20 std::string_view source, const char* includer_name = "",
21 std::size_t depth = 1)
22 {
23 if (!shader_includer)
24 {
25 return std::string{source};
26 }
27
28 std::string source_str(source);
29 std::istringstream iss(source_str);
30
31 ShaderCode output;
32 std::string line;
33 while (std::getline(iss, line))
34 {
35 if (line.empty())
36 {
37 output.Write("{}\n", line);
38 continue;
39 }
40
41 std::string_view include_preprocessor = "#include";
42 if (!line.starts_with(include_preprocessor))
43 {
44 output.Write("{}\n", line);
45 continue;
46 }
47
48 const std::string after_include = line.substr(include_preprocessor.size());
49
50 bool local_include = true;
51 std::string_view filename;
52 // First non-whitespace character after include
53 const std::string::const_iterator non_whitespace_iter = std::find_if_not(
54 after_include.begin(), after_include.end(), [](char ch) { return std::isspace(ch); });
55 if (*non_whitespace_iter == '<')
56 {
57 const auto after_less = std::next(non_whitespace_iter);
58 if (after_less == after_include.end())
59 {
60 // Found less-than at the end, malformed
61 output.Write("{}\n", line);
62 continue;
63 }
64 const auto end_iter = std::find(after_less, after_include.end(), '>');
65 if (end_iter == after_include.end())
66 {
67 // Include spans multiple lines or is malformed, just pass it along
68 output.Write("{}\n", line);
69 continue;
70 }
71 filename = std::string_view(after_less, end_iter);
72 local_include = false;
73 }
74 else if (*non_whitespace_iter == '"')
75 {
76 const auto after_quote = std::next(non_whitespace_iter);
77 if (after_quote == after_include.end())
78 {
79 // Found quote at the end, malformed
80 output.Write("{}\n", line);
81 continue;
82 }
83 const auto end_iter = std::find(after_quote, after_include.end(), '"');
84 if (end_iter == after_include.end())
85 {
86 // Include spans multiple lines or is malformed, just pass it along
87 output.Write("{}\n", line);
88 continue;
89 }
90 filename = std::string_view(after_quote, end_iter);
91 }
92
93 const std::string header_path = std::string{filename};
94 auto include_result =
95 local_include ? shader_includer->includeLocal(header_path.c_str(), includer_name, depth) :
96 shader_includer->includeSystem(header_path.c_str(), includer_name, depth);
97 if (!include_result)
98 {
99 output.Write("{}\n", line);
100 continue;
101 }
102
103 output.Write("{}", ResolveIncludeStatements(shader_includer, include_result->headerData,
104 header_path.c_str(), ++depth));
105 }
106
107 return output.GetBuffer();
108 }
109 } // namespace
110 static GLenum GetGLShaderTypeForStage(ShaderStage stage)
111 {
112 switch (stage)
113 {
114 case ShaderStage::Vertex:
115 return GL_VERTEX_SHADER;
116 case ShaderStage::Geometry:
117 return GL_GEOMETRY_SHADER;
118 case ShaderStage::Pixel:
119 return GL_FRAGMENT_SHADER;
120 case ShaderStage::Compute:
121 return GL_COMPUTE_SHADER;
122 default:
123 return 0;
124 }
125 }
126
127 OGLShader::OGLShader(ShaderStage stage, GLenum gl_type, GLuint gl_id, std::string source,
128 std::string name)
129 : AbstractShader(stage), m_id(ProgramShaderCache::GenerateShaderID()), m_type(gl_type),
130 m_gl_id(gl_id), m_source(std::move(source)), m_name(std::move(name))
131 {
132 if (!m_name.empty() && g_backend_info.bSupportsSettingObjectNames)
133 {
134 glObjectLabel(GL_SHADER, m_gl_id, (GLsizei)m_name.size(), m_name.c_str());
135 }
136 }
137
138 OGLShader::OGLShader(GLuint gl_compute_program_id, std::string source, std::string name)
139 : AbstractShader(ShaderStage::Compute), m_id(ProgramShaderCache::GenerateShaderID()),
140 m_type(GL_COMPUTE_SHADER), m_gl_compute_program_id(gl_compute_program_id),
141 m_source(std::move(source)), m_name(std::move(name))
142 {
143 if (!m_name.empty() && g_backend_info.bSupportsSettingObjectNames)
144 {
145 glObjectLabel(GL_PROGRAM, m_gl_compute_program_id, (GLsizei)m_name.size(), m_name.c_str());
146 }
147 }
148
149 OGLShader::~OGLShader()
150 {
151 if (m_stage != ShaderStage::Compute)
152 glDeleteShader(m_gl_id);
153 else
154 glDeleteProgram(m_gl_compute_program_id);
155 }
156
157 std::unique_ptr<OGLShader> OGLShader::CreateFromSource(ShaderStage stage, std::string_view source,
158 VideoCommon::ShaderIncluder* shader_includer,
159 std::string_view name)
160 {
161 // Note: while the source will all be available, any errors will not
162 // reference the correct paths
163 std::string source_str = ResolveIncludeStatements(shader_includer, source);
164 std::string name_str(name);
165 if (stage != ShaderStage::Compute)
166 {
167 GLenum shader_type = GetGLShaderTypeForStage(stage);
168 GLuint shader_id = ProgramShaderCache::CompileSingleShader(shader_type, source_str);
169 if (!shader_id)
170 return nullptr;
171
172 return std::make_unique<OGLShader>(stage, shader_type, shader_id, std::move(source_str),
173 std::move(name_str));
174 }
175
176 // Compute shaders.
177 SHADER prog;
178 if (!ProgramShaderCache::CompileComputeShader(prog, source_str))
179 return nullptr;
180 return std::make_unique<OGLShader>(prog.glprogid, std::move(source_str), std::move(name_str));
181 }
182
183 } // namespace OGL