Debian Codegraph @

Source/Core/VideoCommon/Resources/TextureAndSamplerResource.cpp

raw symbols SCIP
1 // Copyright 2025 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/Resources/TextureAndSamplerResource.h"
5
6 #include "VideoCommon/Assets/CustomAssetCache.h"
7 #include "VideoCommon/Resources/TexturePool.h"
8
9 namespace VideoCommon
10 {
11 TextureAndSamplerResource::TextureAndSamplerResource(Resource::ResourceContext resource_context)
12 : Resource(std::move(resource_context))
13 {
14 m_texture_and_sampler_asset = m_resource_context.asset_cache->CreateAsset<TextureAndSamplerAsset>(
15 m_resource_context.primary_asset_id, m_resource_context.asset_library, this);
16 }
17
18 void TextureAndSamplerResource::MarkAsActive()
19 {
20 m_resource_context.asset_cache->MarkAssetActive(m_texture_and_sampler_asset);
21 }
22
23 void TextureAndSamplerResource::MarkAsPending()
24 {
25 m_resource_context.asset_cache->MarkAssetPending(m_texture_and_sampler_asset);
26 }
27
28 const std::shared_ptr<TextureAndSamplerResource::Data>& TextureAndSamplerResource::GetData() const
29 {
30 return m_current_data;
31 }
32
33 void TextureAndSamplerResource::ResetData()
34 {
35 m_load_data = std::make_shared<Data>();
36 }
37
38 Resource::TaskComplete TextureAndSamplerResource::CollectPrimaryData()
39 {
40 m_load_data->m_texture_and_sampler_data = m_texture_and_sampler_asset->GetData();
41 if (!m_load_data->m_texture_and_sampler_data)
42 return Resource::TaskComplete::No;
43
44 auto& texture_data = m_load_data->m_texture_and_sampler_data->texture_data;
45 if (texture_data.m_slices.empty())
46 return Resource::TaskComplete::Error;
47
48 if (texture_data.m_slices[0].m_levels.empty())
49 return Resource::TaskComplete::Error;
50
51 const auto& first_level = texture_data.m_slices[0].m_levels[0];
52
53 auto& config = m_load_data->m_config;
54 config.format = first_level.format;
55 config.flags = 0;
56 config.layers = 1;
57 config.levels = 1;
58 config.type = m_load_data->m_texture_and_sampler_data->type;
59 config.samples = 1;
60
61 config.width = first_level.width;
62 config.height = first_level.height;
63
64 return Resource::TaskComplete::Yes;
65 }
66
67 Resource::TaskComplete TextureAndSamplerResource::ProcessData()
68 {
69 auto texture = m_resource_context.texture_pool->AllocateTexture(m_load_data->m_config);
70 if (!texture) [[unlikely]]
71 return Resource::TaskComplete::Error;
72
73 m_load_data->m_texture = std::move(texture);
74
75 auto& texture_data = m_load_data->m_texture_and_sampler_data->texture_data;
76 for (std::size_t slice_index = 0; slice_index < texture_data.m_slices.size(); slice_index++)
77 {
78 auto& slice = texture_data.m_slices[slice_index];
79 for (u32 level_index = 0; level_index < static_cast<u32>(slice.m_levels.size()); ++level_index)
80 {
81 auto& level = slice.m_levels[level_index];
82 m_load_data->m_texture->Load(level_index, level.width, level.height, level.row_length,
83 level.data.data(), level.data.size(),
84 static_cast<u32>(slice_index));
85 }
86 }
87 std::swap(m_current_data, m_load_data);
88
89 // Release old data back to the pool
90 if (m_load_data)
91 m_resource_context.texture_pool->ReleaseTexture(std::move(m_load_data->m_texture));
92
93 return Resource::TaskComplete::Yes;
94 }
95
96 void TextureAndSamplerResource::OnUnloadRequested()
97 {
98 if (!m_current_data)
99 return;
100 m_resource_context.texture_pool->ReleaseTexture(std::move(m_current_data->m_texture));
101 m_current_data = nullptr;
102 }
103 } // namespace VideoCommon