Skip to content

Commit 2d1cbed

Browse files
committed
asset preview small change
1 parent 9bc39bb commit 2d1cbed

7 files changed

Lines changed: 56 additions & 58 deletions

File tree

sources/HAL/HAL.CommandList.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,8 @@ namespace HAL
891891
auto transition_one = [&](UINT subres) {
892892

893893
auto& subres_cpu = cpu_state.get_subres_state(subres);
894+
895+
if (!subres_cpu.used) return;
894896
ASSERT(subres_cpu.used);
895897

896898

sources/RenderSystem/Assets/Asset.cpp

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CEREAL_FORCE_DYNAMIC_INIT(BinaryAsset);
1818

1919
AssetManager::AssetManager()
2020
{
21-
has_worker = false;
21+
//has_worker = false;
2222

2323
tree_folders.reset(new folder_item(std::wstring(L"All assets")));
2424
std::function<void(std::filesystem::path, folder_item::ptr)> iter;
@@ -54,6 +54,7 @@ AssetManager::AssetManager()
5454

5555
AssetManager::~AssetManager()
5656
{
57+
preview_executor.stop_and_wait();
5758
// std::lock_guard<std::mutex> g(m);
5859
tree_folders = nullptr;
5960
/* while (assets.size())
@@ -75,10 +76,7 @@ void AssetManager::add_preview(Asset::ptr asset)
7576
{
7677
if (!AssetRenderer::is_good()) return; // no preview renderer on Vulkan
7778

78-
std::lock_guard<std::mutex> g(update_preview_mutex);
79-
80-
81-
auto my_task = [this, asset]() {
79+
preview_executor.enqueue([this, asset]() {
8280
try {
8381
auto& preview = asset->holder->get_preview();
8482

@@ -90,50 +88,15 @@ void AssetManager::add_preview(Asset::ptr asset)
9088
}
9189

9290
asset->update_preview(asset->holder->get_preview());
93-
asset->holder->on_preview(asset->holder->get_preview());
91+
RenderSystem::get().device().get_queue(HAL::CommandListType::DIRECT)->signal_and_wait();
9492
}
95-
catch (const std::system_error& e) { Log::get() << Log::LEVEL_ERROR << e.what() << Log::endl; }
96-
};
97-
98-
if (task_inited)
99-
last_update_task = last_update_task.then(my_task);
100-
else
101-
last_update_task = create_task(my_task);
102-
103-
task_inited = true;
104-
/*update_preview.push(a);
105-
106-
if (!has_worker)
107-
{
108-
has_worker = true;
109-
auto& t = thread_pool::get().enqueue([this]()
110-
{
111-
while (true)
112-
{
113-
Asset::ptr asset;
114-
{
115-
std::lock_guard<std::mutex> g(update_preview_mutex);
116-
117-
if (update_preview.empty()) { has_worker = false; break; }
118-
119-
asset = update_preview.front();
120-
update_preview.pop();
121-
}
122-
auto& preview = asset->holder->get_preview();
123-
124-
if (!preview || !preview->is_rt())
125-
{
126-
HAL::Texture::ptr new_preview;
127-
new_preview.reset(new HAL::Texture(CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R16G16B16A16_FLOAT, 256, 256, 1, 0, 1, 0, D3D12_RESOURCE_FLAGS::D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAGS::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)));
128-
asset->holder->editor->preview = new_preview;
129-
}
130-
131-
asset->update_preview(asset->holder->get_preview());
132-
asset->holder->on_preview(asset->holder->get_preview());
133-
}
93+
catch (const std::exception& e) { Log::get() << Log::LEVEL_ERROR << e.what() << Log::endl; }
94+
// Fire on_preview on the render thread so resource state tracking is coherent
95+
auto preview = asset->holder->get_preview();
96+
add_func([asset, preview]() {
97+
asset->holder->on_preview(preview);
13498
});
135-
// t.wait();
136-
}*/
99+
});
137100
}
138101

139102
AssetStorage::ptr AssetManager::get_storage(Guid id)

sources/RenderSystem/Assets/Asset.ixx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,8 @@ class AssetManager : public Singleton<AssetManager>, public EditContainer, publi
397397

398398
void add_func(std::function<void()> f);
399399

400-
std::mutex update_preview_mutex;
401-
std::queue<Asset::ptr> update_preview;
402-
std::atomic_bool has_worker;
403-
404400
void add_preview(Asset::ptr a);
405-
concurrency::task<void> last_update_task;
406-
bool task_inited = false;
401+
SingleThreadExecutor preview_executor;
407402
public:
408403

409404
using Singleton<AssetManager>::get;

sources/RenderSystem/Assets/TextureAsset.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@ TextureAssetRenderer::~TextureAssetRenderer() {}
2020

2121
void TextureAssetRenderer::render(TextureAsset* asset, HAL::Texture::ptr target, HAL::CommandList::ptr c)
2222
{
23+
// DEBUG: GREEN = render entered with valid source
24+
{
25+
RT::SingleColor rt;
26+
rt.GetColor() = target->texture_2d().renderTarget;
27+
c->get_graphics().set_rtv(rt, RTOptions::ClearAll, 0, 0, vec4(0, 1, 0, 1));
28+
}
29+
2330
if (!asset->get_texture()->texture_2d()) return;
31+
32+
// DEBUG: BLUE = source texture is valid, proceeding with copy
33+
{
34+
RT::SingleColor rt;
35+
rt.GetColor() = target->texture_2d().renderTarget;
36+
c->get_graphics().set_rtv(rt, RTOptions::ClearAll, 0, 0, vec4(0, 0, 1, 1));
37+
}
38+
2439
MipMapGenerator::get().copy_texture_2d_slow(c->get_graphics(), target, asset->get_texture()->texture_2d());
2540
MipMapGenerator::get().generate(c->get_compute(), target->texture_2d());
2641
}
@@ -70,6 +85,13 @@ void TextureAsset::update_preview(HAL::Texture::ptr preview)
7085

7186
auto list = (RenderSystem::get().device().get_frame_manager().begin_frame()->start_list(L"TextureAsset"));
7287

88+
// DEBUG: RED = update_preview entered, before render
89+
{
90+
RT::SingleColor rt;
91+
rt.GetColor() = preview->texture_2d().renderTarget;
92+
list->get_graphics().set_rtv(rt, RTOptions::ClearAll, 0, 0, vec4(1, 0, 0, 1));
93+
}
94+
7395
TextureAssetRenderer::get().render(this, preview, list);
7496
list->execute();
7597
//

sources/RenderSystem/Font/TextSystem.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,12 @@ namespace
279279
// ---------------------------------------------------------------------------
280280
// FreeType library singleton (created lazily, cleaned up by FontSystem dtor)
281281
// ---------------------------------------------------------------------------
282-
static FT_Library s_ft_lib = nullptr;
282+
static FT_Library s_ft_lib = nullptr;
283+
static std::mutex s_ft_lib_mtx;
283284

284285
static FT_Library get_ft_library()
285286
{
287+
std::lock_guard<std::mutex> lk(s_ft_lib_mtx);
286288
if (!s_ft_lib) FT_Init_FreeType(&s_ft_lib);
287289
return s_ft_lib;
288290
}
@@ -580,7 +582,8 @@ namespace Fonts
580582

581583
struct Font::FTData
582584
{
583-
FT_Face face = nullptr;
585+
FT_Face face = nullptr;
586+
std::mutex face_mtx;
584587
};
585588

586589
// ============================================================================
@@ -647,7 +650,11 @@ void Font::draw(HAL::CommandList::ptr& list,
647650
if (!m_data || !m_data->face) return;
648651

649652
FontAtlas& atlas = FontSystem::get_atlas();
650-
auto verts = layout_text(m_data->face, str, size, area, color, flags, atlas);
653+
std::vector<GlyphVtx> verts;
654+
{
655+
std::lock_guard<std::mutex> face_lk(m_data->face_mtx);
656+
verts = layout_text(m_data->face, str, size, area, color, flags, atlas);
657+
}
651658
draw_vertices(list, verts, &clip_rect, nullptr, flags | FW1_CLIPRECT, atlas);
652659
}
653660

@@ -656,6 +663,7 @@ vec2 Font::measure(std::string str, float size, unsigned int flags)
656663
if (!m_data || !m_data->face || str.empty()) return {0.f, 0.f};
657664

658665
std::wstring wstr = convert(str);
666+
std::lock_guard<std::mutex> face_lk(m_data->face_mtx);
659667
FT_Face face = m_data->face;
660668
uint32_t px = static_cast<uint32_t>(std::ceil(size));
661669
FT_Set_Pixel_Sizes(face, 0, px);
@@ -757,6 +765,7 @@ HAL::TextureResource* FontSystem::get_atlas_texture()
757765

758766
Font::ptr FontSystem::get_font(std::string font_name)
759767
{
768+
std::lock_guard<std::mutex> lk(m_fonts_mtx);
760769
return fonts[font_name];
761770
}
762771

@@ -799,6 +808,7 @@ void FontGeometry::set(HAL::CommandList::ptr& /*list*/,
799808

800809
if (!font || !font->m_data || !font->m_data->face) return;
801810

811+
std::lock_guard<std::mutex> face_lk(font->m_data->face_mtx);
802812
m_impl->verts = layout_text(font->m_data->face, str, size,
803813
area, color, flags, FontSystem::get_atlas());
804814
}
@@ -814,8 +824,12 @@ sizer FontGeometry::add(HAL::CommandList::ptr& /*list*/,
814824

815825
if (!font || !font->m_data || !font->m_data->face) return area;
816826

817-
auto new_verts = layout_text(font->m_data->face, str, size,
818-
area, color, flags, FontSystem::get_atlas());
827+
std::vector<GlyphVtx> new_verts;
828+
{
829+
std::lock_guard<std::mutex> face_lk(font->m_data->face_mtx);
830+
new_verts = layout_text(font->m_data->face, str, size,
831+
area, color, flags, FontSystem::get_atlas());
832+
}
819833
for (auto& v : new_verts)
820834
m_impl->verts.push_back(v);
821835

sources/RenderSystem/Font/TextSystem.ixx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export namespace Fonts
8080
friend class FontGeometry;
8181

8282
Cache<std::string, Font::ptr> fonts;
83+
std::mutex m_fonts_mtx;
8384
FontAtlas* m_atlas = nullptr;
8485

8586
FontSystem();

sources/Spectrum/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ class GraphRender : public Window, public GUI::user_interface
536536
}
537537

538538

539+
AssetManager::get().tact();
539540
process_ui((float)main_timer.tick());
540541

541542

0 commit comments

Comments
 (0)