| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Resources/CustomResourceManager.h" |
| 5 | |
| 6 | #include "VideoCommon/AbstractGfx.h" |
| 7 | #include "VideoCommon/PipelineUtils.h" |
| 8 | #include "VideoCommon/Resources/InvalidTextures.h" |
| 9 | #include "VideoCommon/VideoEvents.h" |
| 10 | |
| 11 | namespace VideoCommon |
| 12 | { |
| 13 | void CustomResourceManager::Initialize() |
| 14 | { |
| 15 | m_asset_cache.Initialize(); |
| 16 | m_worker_thread.Reset("resource-worker"); |
| 17 | m_host_config.bits = ShaderHostConfig::GetCurrent().bits; |
| 18 | m_async_shader_compiler = g_gfx->CreateAsyncShaderCompiler(); |
| 19 | |
| 20 | m_async_shader_compiler->StartWorkerThreads(1); // TODO expose to config |
| 21 | |
| 22 | m_xfb_event = |
| 23 | GetVideoEvents().after_frame_event.Register([this](Core::System&) { XFBTriggered(); }); |
| 24 | |
| 25 | m_invalid_array_texture = CreateInvalidArrayTexture(); |
| 26 | m_invalid_color_texture = CreateInvalidColorTexture(); |
| 27 | m_invalid_cubemap_texture = CreateInvalidCubemapTexture(); |
| 28 | m_invalid_transparent_texture = CreateInvalidTransparentTexture(); |
| 29 | } |
| 30 | |
| 31 | void CustomResourceManager::Shutdown() |
| 32 | { |
| 33 | if (m_async_shader_compiler) |
| 34 | { |
| 35 | m_async_shader_compiler->StopWorkerThreads(); |
| 36 | m_async_shader_compiler->ClearAllWork(); |
| 37 | } |
| 38 | |
| 39 | m_asset_cache.Shutdown(); |
| 40 | m_worker_thread.Shutdown(); |
| 41 | Reset(); |
| 42 | } |
| 43 | |
| 44 | void CustomResourceManager::Reset() |
| 45 | { |
| 46 | m_draw_material_resources.clear(); |
| 47 | m_postprocessing_material_resources.clear(); |
| 48 | m_shader_resources.clear(); |
| 49 | m_texture_data_resources.clear(); |
| 50 | m_texture_sampler_resources.clear(); |
| 51 | |
| 52 | m_invalid_transparent_texture.reset(); |
| 53 | m_invalid_color_texture.reset(); |
| 54 | m_invalid_cubemap_texture.reset(); |
| 55 | m_invalid_array_texture.reset(); |
| 56 | |
| 57 | m_asset_cache.Reset(); |
| 58 | m_texture_pool.Reset(); |
| 59 | m_worker_thread.Reset("resource-worker"); |
| 60 | } |
| 61 | |
| 62 | void CustomResourceManager::MarkAssetDirty(const CustomAssetLibrary::AssetID& asset_id) |
| 63 | { |
| 64 | m_asset_cache.MarkAssetDirty(asset_id); |
| 65 | } |
| 66 | |
| 67 | void CustomResourceManager::XFBTriggered() |
| 68 | { |
| 69 | m_asset_cache.Update(); |
| 70 | } |
| 71 | |
| 72 | void CustomResourceManager::SetHostConfig(const ShaderHostConfig& host_config) |
| 73 | { |
| 74 | for (auto& [id, shader_resources] : m_shader_resources) |
| 75 | { |
| 76 | for (auto& [key, shader_resource] : shader_resources) |
| 77 | { |
| 78 | shader_resource->SetHostConfig(host_config); |
| 79 | |
| 80 | // Hack to get access to resource internals |
| 81 | Resource* resource = shader_resource.get(); |
| 82 | |
| 83 | // Tell shader and references to trigger a reload |
| 84 | // on next usage |
| 85 | resource->NotifyAssetChanged(false); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | m_host_config.bits = host_config.bits; |
| 90 | } |
| 91 | |
| 92 | TextureDataResource* CustomResourceManager::GetTextureDataFromAsset( |
| 93 | const CustomAssetLibrary::AssetID& asset_id, |
| 94 | std::shared_ptr<VideoCommon::CustomAssetLibrary> library) |
| 95 | { |
| 96 | auto& resource = m_texture_data_resources[asset_id]; |
| 97 | if (resource == nullptr) |
| 98 | { |
| 99 | resource = |
| 100 | std::make_unique<TextureDataResource>(CreateResourceContext(asset_id, std::move(library))); |
| 101 | } |
| 102 | resource->Process(); |
| 103 | return resource.get(); |
| 104 | } |
| 105 | |
| 106 | MaterialResource* CustomResourceManager::GetDrawMaterialFromAsset( |
| 107 | const CustomAssetLibrary::AssetID& asset_id, const GXPipelineUid& pipeline_uid, |
| 108 | std::shared_ptr<VideoCommon::CustomAssetLibrary> library) |
| 109 | { |
| 110 | auto& resource = m_draw_material_resources[asset_id][PipelineToHash(pipeline_uid)]; |
| 111 | if (resource == nullptr) |
| 112 | { |
| 113 | resource = std::make_unique<MaterialResource>( |
| 114 | CreateResourceContext(asset_id, std::move(library)), pipeline_uid); |
| 115 | } |
| 116 | resource->Process(); |
| 117 | return resource.get(); |
| 118 | } |
| 119 | |
| 120 | MaterialResource* CustomResourceManager::GetPostProcessingMaterialFromAsset( |
| 121 | const CustomAssetLibrary::AssetID& asset_id, |
| 122 | std::shared_ptr<VideoCommon::CustomAssetLibrary> library) |
| 123 | { |
| 124 | auto& resource = m_postprocessing_material_resources[asset_id]; |
| 125 | if (resource == nullptr) |
| 126 | { |
| 127 | resource = |
| 128 | std::make_unique<MaterialResource>(CreateResourceContext(asset_id, std::move(library))); |
| 129 | } |
| 130 | resource->Process(); |
| 131 | return resource.get(); |
| 132 | } |
| 133 | |
| 134 | ShaderResource* CustomResourceManager::GetShaderFromAsset( |
| 135 | const CustomAssetLibrary::AssetID& asset_id, std::size_t shader_key, |
| 136 | const std::optional<GXPipelineUid>& pipeline_uid, const std::string& preprocessor_settings, |
| 137 | std::shared_ptr<VideoCommon::CustomAssetLibrary> library) |
| 138 | { |
| 139 | auto& resource = m_shader_resources[asset_id][shader_key]; |
| 140 | if (resource == nullptr) |
| 141 | { |
| 142 | resource = std::make_unique<ShaderResource>(CreateResourceContext(asset_id, std::move(library)), |
| 143 | pipeline_uid, preprocessor_settings, m_host_config); |
| 144 | } |
| 145 | resource->Process(); |
| 146 | return resource.get(); |
| 147 | } |
| 148 | |
| 149 | TextureAndSamplerResource* CustomResourceManager::GetTextureAndSamplerFromAsset( |
| 150 | const CustomAssetLibrary::AssetID& asset_id, |
| 151 | std::shared_ptr<VideoCommon::CustomAssetLibrary> library) |
| 152 | { |
| 153 | auto& resource = m_texture_sampler_resources[asset_id]; |
| 154 | if (resource == nullptr) |
| 155 | { |
| 156 | resource = std::make_unique<TextureAndSamplerResource>( |
| 157 | CreateResourceContext(asset_id, std::move(library))); |
| 158 | } |
| 159 | resource->Process(); |
| 160 | return resource.get(); |
| 161 | } |
| 162 | |
| 163 | Resource::ResourceContext CustomResourceManager::CreateResourceContext( |
| 164 | const CustomAssetLibrary::AssetID& asset_id, |
| 165 | std::shared_ptr<VideoCommon::CustomAssetLibrary> library) |
| 166 | { |
| 167 | return Resource::ResourceContext{asset_id, |
| 168 | std::move(library), |
| 169 | &m_asset_cache, |
| 170 | this, |
| 171 | &m_texture_pool, |
| 172 | &m_worker_thread, |
| 173 | m_async_shader_compiler.get(), |
| 174 | m_invalid_array_texture.get(), |
| 175 | m_invalid_color_texture.get(), |
| 176 | m_invalid_cubemap_texture.get(), |
| 177 | m_invalid_transparent_texture.get()}; |
| 178 | } |
| 179 | } // namespace VideoCommon |