1 | // Copyright (C) 2015-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/engine/material_manager.hh"
|
---|
8 |
|
---|
9 | #include "nv/io/c_file_system.hh"
|
---|
10 | #include "nv/image/png_loader.hh"
|
---|
11 |
|
---|
12 | using namespace nv;
|
---|
13 |
|
---|
14 |
|
---|
15 | nv::gpu_material_manager::gpu_material_manager( context* context, material_manager* matmgr, image_manager* imgmgr )
|
---|
16 | : m_context( context )
|
---|
17 | , m_material_manager( matmgr )
|
---|
18 | , m_image_manager( imgmgr )
|
---|
19 | {
|
---|
20 | uint8 data[2 * 2 * 3];
|
---|
21 | nv::raw_fill_n( data, 2 * 2 * 3, 0 );
|
---|
22 | m_default = m_context->get_device()->create_texture( ivec2(2,2), nv::image_format( nv::RGB ), nv::sampler(), data );
|
---|
23 | }
|
---|
24 |
|
---|
25 | bool gpu_material_manager::load_resource( const string_view& id )
|
---|
26 | {
|
---|
27 | if ( auto mat = m_material_manager->get( id ).lock() )
|
---|
28 | {
|
---|
29 | gpu_material* result = new gpu_material;
|
---|
30 | sampler smp( sampler::LINEAR_MIPMAP_LINEAR, sampler::REPEAT );
|
---|
31 | for ( uint32 i = 0; i < size( mat->paths ); ++i )
|
---|
32 | if ( !mat->paths[i].empty() )
|
---|
33 | {
|
---|
34 | if ( auto data = m_image_manager->get( mat->paths[i] ).lock() )
|
---|
35 | {
|
---|
36 | result->textures[i] = m_context->get_device()->create_texture( &*data, smp );
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | // HACK
|
---|
41 | for ( uint32 i = 0; i < 8; ++i )
|
---|
42 | if ( result->textures[i].is_nil() )
|
---|
43 | result->textures[i] = m_default;
|
---|
44 |
|
---|
45 | add( id, result );
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 | return false;
|
---|
49 | }
|
---|
50 |
|
---|
51 | void gpu_material_manager::release( gpu_material* m )
|
---|
52 | {
|
---|
53 | for ( const texture& t : m->textures )
|
---|
54 | {
|
---|
55 | m_context->get_device()->release( t );
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | bool material_manager::load_resource( nv::lua::table_guard& table, nv::shash64 id )
|
---|
60 | {
|
---|
61 | c_file_system fs;
|
---|
62 | material* m = new material;
|
---|
63 |
|
---|
64 | if ( table.is_string( "path" ) )
|
---|
65 | {
|
---|
66 | string128 path = table.get_string128( "path" );
|
---|
67 | for ( uint32 i = 0; i < 5; ++i )
|
---|
68 | m->paths[i] = path;
|
---|
69 | m->paths[TEX_DIFFUSE].append( "_diffuse.png" );
|
---|
70 | m->paths[TEX_NORMAL].append( "_normal.png" );
|
---|
71 | m->paths[TEX_METALLIC].append( "_metallic.png" );
|
---|
72 | m->paths[TEX_ROUGHNESS].append( "_roughness.png" );
|
---|
73 | m->paths[TEX_EMISSIVE].append( "_emissive.png" );
|
---|
74 | for ( uint32 i = 0; i < 5; ++i )
|
---|
75 | {
|
---|
76 | if ( !fs.exists( m->paths[i] ) )
|
---|
77 | {
|
---|
78 | if ( i != TEX_EMISSIVE )
|
---|
79 | NV_LOG_ERROR( "Texture file not found! : ", m->paths[i] );
|
---|
80 | m->paths[i].clear();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | m->paths[ TEX_DIFFUSE ] = table.get_string128( "diffuse" );
|
---|
87 | m->paths[ TEX_NORMAL ] = table.get_string128( "normal" );
|
---|
88 | m->paths[ TEX_METALLIC] = table.get_string128( "metallic" );
|
---|
89 | m->paths[ TEX_ROUGHNESS] = table.get_string128( "roughness" );
|
---|
90 | m->paths[ TEX_EMISSIVE ] = table.get_string128( "emissive" );
|
---|
91 | }
|
---|
92 | add( id, m );
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|