| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Resources/ShaderResource.h" |
| 5 | |
| 6 | #include <string_view> |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <fmt/format.h> |
| 10 | |
| 11 | #include "VideoCommon/AbstractGfx.h" |
| 12 | #include "VideoCommon/Assets/CustomAssetCache.h" |
| 13 | #include "VideoCommon/AsyncShaderCompiler.h" |
| 14 | #include "VideoCommon/FramebufferShaderGen.h" |
| 15 | #include "VideoCommon/GeometryShaderGen.h" |
| 16 | #include "VideoCommon/PipelineUtils.h" |
| 17 | #include "VideoCommon/PixelShaderGen.h" |
| 18 | #include "VideoCommon/VertexShaderGen.h" |
| 19 | #include "VideoCommon/VideoConfig.h" |
| 20 | |
| 21 | namespace VideoCommon |
| 22 | { |
| 23 | namespace |
| 24 | { |
| 25 | // TODO: the uniform buffer is combined due to the utility path only having a single |
| 26 | // set of constants, this isn't ideal (both for the end user, where it's more readable |
| 27 | // to see 'custom_uniforms' before variables and from Dolphin because the |
| 28 | // standard uniforms have to be packed with the custom ones |
| 29 | void GeneratePostProcessUniformOutput(ShaderCode& shader_source, std::string_view block_name, |
| 30 | std::string_view uniforms) |
| 31 | { |
| 32 | shader_source.Write("UBO_BINDING(std140, 1) uniform {} {{\n", block_name); |
| 33 | shader_source.Write("\tvec4 source_resolution;\n"); |
| 34 | shader_source.Write("\tvec4 target_resolution;\n"); |
| 35 | shader_source.Write("\tvec4 window_resolution;\n"); |
| 36 | shader_source.Write("\tvec4 source_region;\n"); |
| 37 | shader_source.Write("\tint source_layer;\n"); |
| 38 | shader_source.Write("\tint source_layer_pad1;\n"); |
| 39 | shader_source.Write("\tint source_layer_pad2;\n"); |
| 40 | shader_source.Write("\tint source_layer_pad3;\n"); |
| 41 | shader_source.Write("\tuint time;\n"); |
| 42 | shader_source.Write("\tuint time_pad1;\n"); |
| 43 | shader_source.Write("\tuint time_pad2;\n"); |
| 44 | shader_source.Write("\tuint time_pad3;\n"); |
| 45 | shader_source.Write("\tint graphics_api;\n"); |
| 46 | shader_source.Write("\tint graphics_api_pad1;\n"); |
| 47 | shader_source.Write("\tint graphics_api_pad2;\n"); |
| 48 | shader_source.Write("\tint graphics_api_pad3;\n"); |
| 49 | shader_source.Write("\tuint efb_scale;\n"); |
| 50 | shader_source.Write("\tuint efb_scale_pad1;\n"); |
| 51 | shader_source.Write("\tuint efb_scale_pad2;\n"); |
| 52 | shader_source.Write("\tuint efb_scale_pad3;\n"); |
| 53 | if (!uniforms.empty()) |
| 54 | { |
| 55 | shader_source.Write("{}", uniforms); |
| 56 | } |
| 57 | shader_source.Write("}};\n"); |
| 58 | } |
| 59 | |
| 60 | // TODO: move this to a more standard post processing file |
| 61 | // once post processing has been cleaned up |
| 62 | void GeneratePostProcessingVertexShader(ShaderCode& shader_source, |
| 63 | const CustomVertexContents& custom_contents) |
| 64 | { |
| 65 | // Note: if blocks are the same, they need to match for the OpenGL backend |
| 66 | GeneratePostProcessUniformOutput(shader_source, "PSBlock", custom_contents.uniforms); |
| 67 | |
| 68 | // Define some defines that are shared with custom draw shaders, |
| 69 | // so that a common shader code could possibly be used in both |
| 70 | shader_source.Write("#define HAS_COLOR_0 0\n"); |
| 71 | shader_source.Write("#define HAS_COLOR_1 0\n"); |
| 72 | shader_source.Write("#define HAS_NORMAL 0\n"); |
| 73 | shader_source.Write("#define HAS_BINORMAL 0\n"); |
| 74 | shader_source.Write("#define HAS_TANGENT 0\n"); |
| 75 | shader_source.Write("#define HAS_TEXTURE_COORD_0 1\n"); |
| 76 | for (u32 i = 1; i < 8; i++) |
| 77 | { |
| 78 | shader_source.Write("#define HAS_TEXTURE_COORD_{} 0\n", i); |
| 79 | } |
| 80 | |
| 81 | // Write the common structs, might want to consider |
| 82 | // moving these to another location? |
| 83 | shader_source.Write("struct DolphinVertexInput\n"); |
| 84 | shader_source.Write("{{\n"); |
| 85 | shader_source.Write("\tvec4 position;\n"); |
| 86 | shader_source.Write("\tvec3 texture_coord_0;\n"); |
| 87 | shader_source.Write("}};\n\n"); |
| 88 | |
| 89 | shader_source.Write("struct DolphinVertexOutput\n"); |
| 90 | shader_source.Write("{{\n"); |
| 91 | shader_source.Write("\tvec4 position;\n"); |
| 92 | shader_source.Write("\tvec3 texture_coord_0;\n"); |
| 93 | shader_source.Write("}};\n\n"); |
| 94 | |
| 95 | constexpr std::string_view emulated_vertex_definition = |
| 96 | "void dolphin_process_emulated_vertex(in DolphinVertexInput vertex_input, out " |
| 97 | "DolphinVertexOutput vertex_output)"; |
| 98 | shader_source.Write("{}\n", emulated_vertex_definition); |
| 99 | shader_source.Write("{{\n"); |
| 100 | shader_source.Write("\tvertex_output.position = vertex_input.position;\n"); |
| 101 | shader_source.Write("\tvertex_output.texture_coord_0 = vertex_input.texture_coord_0;\n"); |
| 102 | shader_source.Write("}}\n"); |
| 103 | |
| 104 | if (custom_contents.shader.empty()) |
| 105 | { |
| 106 | shader_source.Write( |
| 107 | "void process_vertex(in DolphinVertexInput vertex_input, out DolphinVertexOutput " |
| 108 | "vertex_output)\n"); |
| 109 | shader_source.Write("{{\n"); |
| 110 | |
| 111 | shader_source.Write("\tdolphin_process_emulated_vertex(vertex_input, vertex_output);\n"); |
| 112 | |
| 113 | shader_source.Write("}}\n"); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | shader_source.Write("{}\n", custom_contents.shader); |
| 118 | } |
| 119 | |
| 120 | if (g_backend_info.bSupportsGeometryShaders) |
| 121 | { |
| 122 | shader_source.Write("VARYING_LOCATION(0) out VertexData {{\n"); |
| 123 | shader_source.Write("\tvec3 v_tex0;\n"); |
| 124 | shader_source.Write("}};\n"); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | shader_source.Write("VARYING_LOCATION(0) out vec3 v_tex0;\n"); |
| 129 | } |
| 130 | |
| 131 | shader_source.Write("void main()\n"); |
| 132 | shader_source.Write("{{\n"); |
| 133 | shader_source.Write("\tDolphinVertexInput vertex_input;\n"); |
| 134 | shader_source.Write("\tvec3 vert = vec3(float((gl_VertexID << 1) & 2), " |
| 135 | "float(gl_VertexID & 2), 0.0f);\n"); |
| 136 | shader_source.Write("\tvertex_input.position = vec4(vert.xy * " |
| 137 | "float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);\n"); |
| 138 | shader_source.Write("\tvertex_input.texture_coord_0 = vec3(source_region.xy + " |
| 139 | "(source_region.zw * vert.xy), 0.0f);\n"); |
| 140 | shader_source.Write("\tDolphinVertexOutput vertex_output;\n"); |
| 141 | shader_source.Write("\tprocess_vertex(vertex_input, vertex_output);\n"); |
| 142 | shader_source.Write("\tgl_Position = vertex_output.position;\n"); |
| 143 | shader_source.Write("\tv_tex0 = vertex_output.texture_coord_0;\n"); |
| 144 | |
| 145 | // NDC space is flipped in Vulkan |
| 146 | if (g_backend_info.api_type == APIType::Vulkan) |
| 147 | { |
| 148 | shader_source.Write("\tgl_Position.y = -gl_Position.y;\n"); |
| 149 | } |
| 150 | |
| 151 | shader_source.Write("}}\n"); |
| 152 | } |
| 153 | |
| 154 | void GeneratePostProcessingPixelShader(ShaderCode& shader_source, |
| 155 | const CustomPixelContents& custom_contents) |
| 156 | { |
| 157 | GeneratePostProcessUniformOutput(shader_source, "PSBlock", custom_contents.uniforms); |
| 158 | |
| 159 | shader_source.Write("SAMPLER_BINDING(0) uniform sampler2DArray samp0;\n"); |
| 160 | |
| 161 | if (g_backend_info.bSupportsGeometryShaders) |
| 162 | { |
| 163 | shader_source.Write("VARYING_LOCATION(0) in VertexData {{\n"); |
| 164 | shader_source.Write("\tvec3 v_tex0;\n"); |
| 165 | shader_source.Write("}};\n"); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | shader_source.Write("VARYING_LOCATION(0) in float3 v_tex0;\n"); |
| 170 | } |
| 171 | |
| 172 | shader_source.Write("FRAGMENT_OUTPUT_LOCATION(0) out vec4 ocol0;\n"); |
| 173 | |
| 174 | shader_source.Write("struct DolphinFragmentInput\n"); |
| 175 | shader_source.Write("{{\n"); |
| 176 | for (u32 i = 0; i < 1; i++) |
| 177 | { |
| 178 | shader_source.Write("\tvec3 tex{};\n", i); |
| 179 | } |
| 180 | shader_source.Write("\n"); |
| 181 | |
| 182 | shader_source.Write("}};\n\n"); |
| 183 | |
| 184 | shader_source.Write("struct DolphinFragmentOutput\n"); |
| 185 | shader_source.Write("{{\n"); |
| 186 | shader_source.Write("\tvec4 main;\n"); |
| 187 | shader_source.Write("}};\n\n"); |
| 188 | |
| 189 | constexpr std::string_view emulated_fragment_definition = |
| 190 | "void dolphin_process_emulated_fragment(in DolphinFragmentInput frag_input, out " |
| 191 | "DolphinFragmentOutput frag_output)"; |
| 192 | shader_source.Write("{}\n", emulated_fragment_definition); |
| 193 | shader_source.Write("{{\n"); |
| 194 | shader_source.Write("\tfrag_output.main = texture(samp0, frag_input.tex0);\n"); |
| 195 | shader_source.Write("}}\n"); |
| 196 | |
| 197 | if (custom_contents.shader.empty()) |
| 198 | { |
| 199 | shader_source.Write( |
| 200 | "void process_fragment(in DolphinFragmentInput frag_input, out DolphinFragmentOutput " |
| 201 | "frag_output)\n"); |
| 202 | shader_source.Write("{{\n"); |
| 203 | |
| 204 | shader_source.Write("\tdolphin_process_emulated_fragment(frag_input, frag_output);\n"); |
| 205 | |
| 206 | shader_source.Write("}}\n"); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | shader_source.Write("{}\n", custom_contents.shader); |
| 211 | } |
| 212 | |
| 213 | shader_source.Write("void main()\n"); |
| 214 | shader_source.Write("{{\n"); |
| 215 | shader_source.Write("\tDolphinFragmentInput frag_input;\n"); |
| 216 | shader_source.Write("\tfrag_input.tex0 = v_tex0;\n"); |
| 217 | shader_source.Write("\tDolphinFragmentOutput frag_output;\n"); |
| 218 | shader_source.Write("\tprocess_fragment(frag_input, frag_output);\n"); |
| 219 | shader_source.Write("\tocol0 = frag_output.main;\n"); |
| 220 | shader_source.Write("}}\n"); |
| 221 | } |
| 222 | |
| 223 | std::unique_ptr<AbstractShader> CompileGeometryShader(GeometryShaderUid* uid, APIType api_type, |
| 224 | ShaderHostConfig host_config) |
| 225 | { |
| 226 | if (!uid) |
| 227 | { |
| 228 | return g_gfx->CreateShaderFromSource( |
| 229 | ShaderStage::Geometry, FramebufferShaderGen::GeneratePassthroughGeometryShader(1, 0), |
| 230 | nullptr, "Custom Post Processing Geometry Shader"); |
| 231 | } |
| 232 | |
| 233 | const ShaderCode source_code = |
| 234 | GenerateGeometryShaderCode(api_type, host_config, uid->GetUidData()); |
| 235 | return g_gfx->CreateShaderFromSource(ShaderStage::Geometry, source_code.GetBuffer(), nullptr, |
| 236 | fmt::format("Geometry shader: {}", *uid->GetUidData())); |
| 237 | } |
| 238 | |
| 239 | std::unique_ptr<AbstractShader> |
| 240 | CompilePixelShader(PixelShaderUid* uid, std::string_view preprocessor_settings, APIType api_type, |
| 241 | const ShaderHostConfig& host_config, RasterSurfaceShaderData* shader_data) |
| 242 | { |
| 243 | ShaderCode shader_code; |
| 244 | |
| 245 | // Write any preprocessor values that were passed in |
| 246 | shader_code.Write("{}", preprocessor_settings); |
| 247 | |
| 248 | // TODO: in the future we could dynamically determine the amount of samplers |
| 249 | // available, for now just hardcode to start at 8 (the first non game |
| 250 | // sampler index available) |
| 251 | const std::size_t custom_sampler_index_offset = 8; |
| 252 | for (std::size_t i = 0; i < shader_data->samplers.size(); i++) |
| 253 | { |
| 254 | const auto& sampler = shader_data->samplers[i]; |
| 255 | std::string_view sampler_type; |
| 256 | switch (sampler.type) |
| 257 | { |
| 258 | case AbstractTextureType::Texture_2D: |
| 259 | sampler_type = "sampler2D"; |
| 260 | break; |
| 261 | case AbstractTextureType::Texture_2DArray: |
| 262 | sampler_type = "sampler2DArray"; |
| 263 | break; |
| 264 | case AbstractTextureType::Texture_CubeMap: |
| 265 | sampler_type = "samplerCube"; |
| 266 | break; |
| 267 | }; |
| 268 | shader_code.Write("SAMPLER_BINDING({}) uniform {} samp_{};\n", custom_sampler_index_offset + i, |
| 269 | sampler_type, sampler.name); |
| 270 | |
| 271 | // Sampler usage is passed in from the material |
| 272 | // Write a new preprocessor value with the sampler name |
| 273 | // for easier code in the shader |
| 274 | shader_code.Write("#if HAS_SAMPLER_{} == 1\n", i); |
| 275 | shader_code.Write("#define HAS_{} 1\n", sampler.name); |
| 276 | shader_code.Write("#endif\n"); |
| 277 | |
| 278 | shader_code.Write("\n"); |
| 279 | } |
| 280 | shader_code.Write("\n"); |
| 281 | |
| 282 | // Now write the custom shader |
| 283 | shader_code.Write("{}", ReplaceAll(shader_data->pixel_source, "\r\n", "\n")); |
| 284 | |
| 285 | // Write out the uniform data |
| 286 | ShaderCode uniform_code; |
| 287 | for (const auto& property : shader_data->uniform_properties) |
| 288 | { |
| 289 | VideoCommon::ShaderProperty::WriteAsShaderCode(uniform_code, property); |
| 290 | } |
| 291 | if (!shader_data->uniform_properties.empty()) |
| 292 | uniform_code.Write("\n\n"); |
| 293 | |
| 294 | // Compile the shader |
| 295 | CustomPixelContents contents{.shader = shader_code.GetBuffer(), |
| 296 | .uniforms = uniform_code.GetBuffer()}; |
| 297 | |
| 298 | ShaderCode source_code; |
| 299 | if (uid) |
| 300 | { |
| 301 | source_code = GeneratePixelShaderCode(api_type, host_config, uid->GetUidData(), contents); |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | GeneratePostProcessingPixelShader(source_code, contents); |
| 306 | } |
| 307 | ShaderIncluder* shader_includer = |
| 308 | shader_data->shader_includer ? &*shader_data->shader_includer : nullptr; |
| 309 | return g_gfx->CreateShaderFromSource(ShaderStage::Pixel, source_code.GetBuffer(), shader_includer, |
| 310 | "Custom Pixel Shader"); |
| 311 | } |
| 312 | |
| 313 | std::unique_ptr<AbstractShader> |
| 314 | CompileVertexShader(VertexShaderUid* uid, std::string_view preprocessor_settings, APIType api_type, |
| 315 | const ShaderHostConfig& host_config, const RasterSurfaceShaderData& shader_data) |
| 316 | { |
| 317 | ShaderCode shader_code; |
| 318 | |
| 319 | // Write any preprocessor values that were passed in |
| 320 | shader_code.Write("{}", preprocessor_settings); |
| 321 | |
| 322 | // TODO: in the future we could dynamically determine the amount of samplers |
| 323 | // available, for now just hardcode to start at 8 (the first non game |
| 324 | // sampler index available) |
| 325 | const std::size_t custom_sampler_index_offset = 8; |
| 326 | for (std::size_t i = 0; i < shader_data.samplers.size(); i++) |
| 327 | { |
| 328 | const auto& sampler = shader_data.samplers[i]; |
| 329 | std::string_view sampler_type = ""; |
| 330 | switch (sampler.type) |
| 331 | { |
| 332 | case AbstractTextureType::Texture_2D: |
| 333 | sampler_type = "sampler2D"; |
| 334 | break; |
| 335 | case AbstractTextureType::Texture_2DArray: |
| 336 | sampler_type = "sampler2DArray"; |
| 337 | break; |
| 338 | case AbstractTextureType::Texture_CubeMap: |
| 339 | sampler_type = "samplerCube"; |
| 340 | break; |
| 341 | }; |
| 342 | shader_code.Write("SAMPLER_BINDING({}) uniform {} samp_{};\n", custom_sampler_index_offset + i, |
| 343 | sampler_type, sampler.name); |
| 344 | |
| 345 | // Sampler usage is passed in from the material |
| 346 | // Write a new preprocessor value with the sampler name |
| 347 | // for easier code in the shader |
| 348 | shader_code.Write("#if HAS_SAMPLER_{} == 1\n", i); |
| 349 | shader_code.Write("#define HAS_{} 1\n", sampler.name); |
| 350 | shader_code.Write("#endif\n"); |
| 351 | |
| 352 | shader_code.Write("\n"); |
| 353 | } |
| 354 | shader_code.Write("\n"); |
| 355 | |
| 356 | // Now write the custom shader |
| 357 | shader_code.Write("{}", ReplaceAll(shader_data.vertex_source, "\r\n", "\n")); |
| 358 | |
| 359 | // Write out the uniform data |
| 360 | ShaderCode uniform_code; |
| 361 | for (const auto& property : shader_data.uniform_properties) |
| 362 | { |
| 363 | VideoCommon::ShaderProperty::WriteAsShaderCode(uniform_code, property); |
| 364 | } |
| 365 | if (!shader_data.uniform_properties.empty()) |
| 366 | uniform_code.Write("\n\n"); |
| 367 | |
| 368 | // Compile the shader |
| 369 | CustomVertexContents contents{.shader = shader_code.GetBuffer(), |
| 370 | .uniforms = uniform_code.GetBuffer()}; |
| 371 | |
| 372 | ShaderCode source_code; |
| 373 | if (uid) |
| 374 | { |
| 375 | source_code = GenerateVertexShaderCode(api_type, host_config, uid->GetUidData(), contents); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | GeneratePostProcessingVertexShader(source_code, contents); |
| 380 | } |
| 381 | return g_gfx->CreateShaderFromSource(ShaderStage::Vertex, source_code.GetBuffer(), nullptr, |
| 382 | "Custom Vertex Shader"); |
| 383 | } |
| 384 | } // namespace |
| 385 | ShaderResource::ShaderResource(Resource::ResourceContext resource_context, |
| 386 | std::optional<GXPipelineUid> pipeline_uid, |
| 387 | std::string preprocessor_setting, |
| 388 | const ShaderHostConfig& shader_host_config) |
| 389 | : Resource(std::move(resource_context)), m_shader_host_config{.bits = shader_host_config.bits}, |
| 390 | m_uid(std::move(pipeline_uid)), m_preprocessor_settings(std::move(preprocessor_setting)) |
| 391 | { |
| 392 | m_shader_asset = m_resource_context.asset_cache->CreateAsset<RasterSurfaceShaderAsset>( |
| 393 | m_resource_context.primary_asset_id, m_resource_context.asset_library, this); |
| 394 | } |
| 395 | |
| 396 | void ShaderResource::SetHostConfig(const ShaderHostConfig& host_config) |
| 397 | { |
| 398 | m_shader_host_config.bits = host_config.bits; |
| 399 | } |
| 400 | |
| 401 | void ShaderResource::MarkAsPending() |
| 402 | { |
| 403 | m_resource_context.asset_cache->MarkAssetPending(m_shader_asset); |
| 404 | } |
| 405 | |
| 406 | void ShaderResource::MarkAsActive() |
| 407 | { |
| 408 | m_resource_context.asset_cache->MarkAssetActive(m_shader_asset); |
| 409 | } |
| 410 | |
| 411 | AbstractShader* ShaderResource::Data::GetVertexShader() const |
| 412 | { |
| 413 | if (!m_vertex_shader) |
| 414 | return nullptr; |
| 415 | return m_vertex_shader.get(); |
| 416 | } |
| 417 | |
| 418 | AbstractShader* ShaderResource::Data::GetPixelShader() const |
| 419 | { |
| 420 | if (!m_pixel_shader) |
| 421 | return nullptr; |
| 422 | return m_pixel_shader.get(); |
| 423 | } |
| 424 | |
| 425 | AbstractShader* ShaderResource::Data::GetGeometryShader() const |
| 426 | { |
| 427 | if (!m_geometry_shader) |
| 428 | return nullptr; |
| 429 | return m_geometry_shader.get(); |
| 430 | } |
| 431 | |
| 432 | bool ShaderResource::Data::IsCompiled() const |
| 433 | { |
| 434 | return m_vertex_shader && m_pixel_shader && (!m_needs_geometry_shader || m_geometry_shader); |
| 435 | } |
| 436 | |
| 437 | AbstractTextureType ShaderResource::Data::GetTextureType(std::size_t index) |
| 438 | { |
| 439 | // If the data doesn't exist, just pick one... |
| 440 | if (!m_shader_data || index >= m_shader_data->samplers.size()) [[unlikely]] |
| 441 | return AbstractTextureType::Texture_2D; |
| 442 | |
| 443 | return m_shader_data->samplers[index].type; |
| 444 | } |
| 445 | |
| 446 | void ShaderResource::ResetData() |
| 447 | { |
| 448 | m_load_data = std::make_shared<Data>(); |
| 449 | m_processing_load_data = false; |
| 450 | } |
| 451 | |
| 452 | Resource::TaskComplete ShaderResource::CollectPrimaryData() |
| 453 | { |
| 454 | const auto shader_data = m_shader_asset->GetData(); |
| 455 | if (!shader_data) [[unlikely]] |
| 456 | { |
| 457 | return Resource::TaskComplete::No; |
| 458 | } |
| 459 | m_load_data->m_shader_data = shader_data; |
| 460 | |
| 461 | return Resource::TaskComplete::Yes; |
| 462 | } |
| 463 | |
| 464 | Resource::TaskComplete ShaderResource::ProcessData() |
| 465 | { |
| 466 | class WorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem |
| 467 | { |
| 468 | public: |
| 469 | WorkItem(std::shared_ptr<ShaderResource::Data> resource_data, VideoCommon::GXPipelineUid* uid, |
| 470 | u32 shader_bits, std::string_view preprocessor_settings) |
| 471 | : m_resource_data(std::move(resource_data)), m_uid(uid), m_shader_bits(shader_bits), |
| 472 | m_preprocessor_settings(preprocessor_settings) |
| 473 | { |
| 474 | } |
| 475 | |
| 476 | bool Compile() override |
| 477 | { |
| 478 | const ShaderHostConfig shader_host_config{.bits = m_shader_bits}; |
| 479 | if (m_uid) |
| 480 | { |
| 481 | // Draw based shader |
| 482 | auto actual_uid = ApplyDriverBugs(*m_uid); |
| 483 | |
| 484 | ClearUnusedPixelShaderUidBits(g_backend_info.api_type, shader_host_config, |
| 485 | &actual_uid.ps_uid); |
| 486 | m_resource_data->m_needs_geometry_shader = shader_host_config.backend_geometry_shaders && |
| 487 | !actual_uid.gs_uid.GetUidData()->IsPassthrough(); |
| 488 | |
| 489 | if (m_resource_data->m_needs_geometry_shader) |
| 490 | { |
| 491 | m_resource_data->m_geometry_shader = CompileGeometryShader( |
| 492 | &actual_uid.gs_uid, g_backend_info.api_type, shader_host_config); |
| 493 | } |
| 494 | m_resource_data->m_pixel_shader = |
| 495 | CompilePixelShader(&actual_uid.ps_uid, m_preprocessor_settings, g_backend_info.api_type, |
| 496 | shader_host_config, m_resource_data->m_shader_data.get()); |
| 497 | m_resource_data->m_vertex_shader = CompileVertexShader( |
| 498 | &actual_uid.vs_uid, m_preprocessor_settings, g_backend_info.api_type, |
| 499 | shader_host_config, *m_resource_data->m_shader_data); |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | // Post processing based shader |
| 504 | |
| 505 | m_resource_data->m_needs_geometry_shader = |
| 506 | shader_host_config.backend_geometry_shaders && shader_host_config.stereo; |
| 507 | if (m_resource_data->m_needs_geometry_shader) |
| 508 | { |
| 509 | m_resource_data->m_geometry_shader = |
| 510 | CompileGeometryShader(nullptr, g_backend_info.api_type, shader_host_config); |
| 511 | } |
| 512 | m_resource_data->m_pixel_shader = |
| 513 | CompilePixelShader(nullptr, m_preprocessor_settings, g_backend_info.api_type, |
| 514 | shader_host_config, m_resource_data->m_shader_data.get()); |
| 515 | m_resource_data->m_vertex_shader = |
| 516 | CompileVertexShader(nullptr, m_preprocessor_settings, g_backend_info.api_type, |
| 517 | shader_host_config, *m_resource_data->m_shader_data); |
| 518 | } |
| 519 | m_resource_data->m_processing_finished = true; |
| 520 | return true; |
| 521 | } |
| 522 | void Retrieve() override {} |
| 523 | |
| 524 | private: |
| 525 | std::shared_ptr<ShaderResource::Data> m_resource_data; |
| 526 | VideoCommon::GXPipelineUid* m_uid; |
| 527 | u32 m_shader_bits; |
| 528 | std::string_view m_preprocessor_settings; |
| 529 | }; |
| 530 | |
| 531 | if (!m_processing_load_data) |
| 532 | { |
| 533 | std::string_view preprocessor_settings = m_preprocessor_settings; |
| 534 | auto wi = m_resource_context.shader_compiler->CreateWorkItem<WorkItem>( |
| 535 | m_load_data, m_uid ? &*m_uid : nullptr, m_shader_host_config.bits, preprocessor_settings); |
| 536 | |
| 537 | // We don't need priority, that is already handled by the resource system |
| 538 | m_resource_context.shader_compiler->QueueWorkItem(std::move(wi), 0); |
| 539 | m_processing_load_data = true; |
| 540 | } |
| 541 | |
| 542 | if (!m_load_data->m_processing_finished) |
| 543 | return Resource::TaskComplete::No; |
| 544 | |
| 545 | if (!m_load_data->IsCompiled()) |
| 546 | return Resource::TaskComplete::Error; |
| 547 | |
| 548 | std::swap(m_current_data, m_load_data); |
| 549 | return Resource::TaskComplete::Yes; |
| 550 | } |
| 551 | } // namespace VideoCommon |