[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;
|
---|
| 30 | sampler smp( sampler::LINEAR, sampler::REPEAT );
|
---|
| 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
|
---|
| 41 | for ( uint32 i = 0; i < 5; ++i )
|
---|
| 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 | {
|
---|
| 61 | material* m = new material;
|
---|
| 62 | m->paths[ TEX_DIFFUSE ] = table.get_string128( "diffuse" );
|
---|
| 63 | m->paths[ TEX_SPECULAR ] = table.get_string128( "specular" );
|
---|
| 64 | m->paths[ TEX_NORMAL ] = table.get_string128( "normal" );
|
---|
[486] | 65 | m->paths[ TEX_EMISSIVE ] = table.get_string128( "emissive" );
|
---|
[484] | 66 | m->paths[ TEX_GLOSS ] = table.get_string128( "gloss" );
|
---|
| 67 | add( id, m );
|
---|
| 68 | return true;
|
---|
| 69 | }
|
---|