[484] | 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 |
|
---|
[486] | 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 |
|
---|
[484] | 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;
|
---|
[498] | 30 | sampler smp( sampler::LINEAR_MIPMAP_LINEAR, sampler::REPEAT );
|
---|
[484] | 31 | for ( uint32 i = 0; i < size( mat->paths ); ++i )
|
---|
| 32 | if ( !mat->paths[i].empty() )
|
---|
[486] | 33 | {
|
---|
[484] | 34 | if ( auto data = m_image_manager->get( mat->paths[i] ).lock() )
|
---|
[485] | 35 | {
|
---|
[484] | 36 | result->textures[i] = m_context->get_device()->create_texture( &*data, smp );
|
---|
[485] | 37 | }
|
---|
[486] | 38 | }
|
---|
| 39 |
|
---|
| 40 | // HACK
|
---|
[491] | 41 | for ( uint32 i = 0; i < 8; ++i )
|
---|
[486] | 42 | if ( result->textures[i].is_nil() )
|
---|
| 43 | result->textures[i] = m_default;
|
---|
| 44 |
|
---|
[484] | 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 | {
|
---|
[491] | 61 | c_file_system fs;
|
---|
[484] | 62 | material* m = new material;
|
---|
[491] | 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 | }
|
---|
[484] | 92 | add( id, m );
|
---|
| 93 | return true;
|
---|
| 94 | }
|
---|
[491] | 95 |
|
---|