v 0. Pasted by Anonymous as cpp at 2014-10-31 19:13:07 MSK and set expiration to never.

Paste will expire never.

  1. #include "nu_StdHeader.h"
  2.  
  3. #include "nu_Texture.h"
  4. #include "nu_Scene.h"
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <time.h>
  8. #include "nu_ProjectFiles.h"
  9. #include "nu_TextureSetting.h"
  10. #include "nu_SceneStatisticFrames.h"
  11. #include "nu_FileStream.h"
  12. #include "nu_EngineRender.h"
  13. #include "nu_RenderSetting.h"
  14.  
  15. using namespace Nuligine;
  16.  
  17. bool Texture::GetIsLoadFromPak(ProjectFiles *project_files, Str real_file, Str pak_file){
  18.     nu_assert_ptr(project_files);
  19.     Pak *pak = project_files->pak;
  20.     if (pak){
  21.         uint32 pak_time = pak->GetTimeChange(pak_file);
  22.         if (!pak_time)
  23.             return false;
  24.         uint32 real_time = project_files->GetTimeChange(real_file);
  25.         //if not found src file, load from PAK
  26.         if (!real_time)
  27.             return true;
  28.         // if source file was changed, need load source file
  29.         if (pak_time != real_time)
  30.             return false;
  31.         Str tex_setting = real_file + ".setting";
  32.         uint32 pak_setting_time = pak->GetTimeChange(tex_setting);
  33.         uint32 source_setting_time = project_files->GetTimeChange(tex_setting);
  34.         // if cur setting time != source setting time, need load source file
  35.         if (source_setting_time != pak_setting_time)
  36.             return false;
  37.         // if all comparable, we can load from pak file!
  38.         return true;
  39.     }
  40.     return false;
  41. }
  42.  
  43. TextureDesc::TextureDesc(){
  44.     Reset();
  45. }
  46. void TextureDesc::Reset(){
  47.     is_cube = false;
  48.     width = height = 0;
  49.     mip_count = 1;
  50.     format = TF_RGBA_8_UNORM;
  51.     group = TG_NO_SET;
  52.     is_from_file = false;
  53.     is_render_target = true;
  54.     is_can_write = false;
  55.     is_can_read = false;
  56.     init_data = UI_Image();
  57. }
  58.  
  59. TextureGpuRaw::TextureGpuRaw( Scene *set_scene ) : GpuLoad( set_scene ){
  60.     is_set_desc = false;
  61.     file_size = 0;
  62.     use_setting = nullptr;
  63.     readed_data = nullptr;
  64.     readed_data_size = 0;
  65.     for( int i=0; i < 6; i++ ){
  66.         file_streams[i] = nullptr;
  67.         load_format[i] = tlf_Uncnown;
  68.         loaded[i] = false;
  69.     }
  70. }
  71. TextureGpuRaw::~TextureGpuRaw(){
  72. }
  73. void TextureGpuRaw::RefCoundIsZero(){
  74.     nu_assert_ptr(scene);
  75.     nu_assert_ptr(scene->render);
  76.     scene->render->AddToGpuDelete(this);
  77. }
  78. u32 TextureGpuRaw::GetFileSize(){
  79.     return file_size;
  80. }
  81. const TextureDesc &TextureGpuRaw::Desc() const{
  82.     return desc;
  83. }
  84. bool TextureGpuRaw::GetIsDepthFormat(){
  85.     return desc.format >= TF_D_16 && desc.format <= TF_D_32;
  86. }
  87. bool TextureGpuRaw::GetCanReadTextureData(){
  88.     LOCKER( readed_data_mutex );
  89.     return readed_data != 0;
  90. }
  91. void *TextureGpuRaw::GetReadTextureData(){
  92.     LOCKER( readed_data_mutex );
  93.     return readed_data;
  94. }
  95. u32  TextureGpuRaw::GetReadTextureDataSize(){
  96.     LOCKER( readed_data_mutex );
  97.     return readed_data_size;
  98. }
  99. void TextureGpuRaw::ClearReadedTextureData(){
  100.     LOCKER( readed_data_mutex );
  101.     NU_FREE( readed_data );
  102.     readed_data_size = 0;
  103. }
  104.  
  105. void TextureGpuRaw::FileStreamLoadComplite( FileStream *fs ){
  106.     if( desc.is_cube ){
  107.         int count_load = 0;
  108.         for( int i=0; i < 6; i++ ){
  109.             if( file_streams[i] == fs )
  110.                 loaded[i] = true;
  111.             count_load += loaded[i] ? 1 : 0;
  112.         }
  113.         if( count_load == 6 )
  114.             AddToLoad();
  115.     }else{
  116.         loaded[0] = true;
  117.         AddToLoad();
  118.     }
  119. }
  120.  
  121.  
  122. bool TextureGpuRaw::GetCanLoadInThreadGPU(){
  123.     // Texture added to load after call back of FileStream it means what
  124.     // we no need check for load     All files ready to load!
  125.     if( !desc.is_render_target ){
  126.         if( desc.is_cube ){
  127.             for( int i=0; i < 6; i++ ){
  128.                 if( file_streams[i] )
  129.                     nu_assert( file_streams[i]->GetCanRead() );
  130.             }
  131.         }
  132.         else if( file_streams[0] ){
  133.             nu_assert( file_streams[0]->GetCanRead() );
  134.         }
  135.     }
  136.     return  true;
  137. }
  138. void TextureGpuRaw::DoLoadInThreadGPU( ){
  139.     size_t f_size = 0;
  140.     CreateTextureAsunc(f_size);
  141.     file_size += f_size;
  142.     for( int i=0; i < 6; i++ )
  143.         SafeRelease( file_streams[i] )
  144. }
  145. void TextureGpuRaw::SetLoadFromDesc( TextureDesc &set_desc ){
  146.     nu_assert( !is_set_desc );
  147.     desc = set_desc;
  148.     if( desc.is_render_target ){
  149.         AddToLoad();
  150.     }else{
  151.         if( desc.is_cube )
  152.             CreateFileStreamsCube();
  153.         else
  154.             CreateFileStream2D();
  155.     }
  156. }
  157. void TextureGpuRaw::CreateFileStreamsCube(){
  158.     use_setting = scene->GetTextureSettings()->Get( desc.name );
  159.     Str real_name = GetRealNameCubeMap( desc.name );
  160.     if( real_name.empty() ){
  161.         GpuLoad::SetGpuLoadState( gls_CompliteLoad );
  162.         return;
  163.     }
  164.     int p = desc.name.GetLastIndexOf( "." );
  165.     Str name_format = desc.name.substr( p, desc.name.size() );
  166.     Str prefix = GetTexPrefixForCurRender();
  167.     Str src_tex_names[6];
  168.     for( int i =0; i < 6; i++ ){
  169.         nu_assert( !file_streams[i] );
  170.         Str load_tex_file = real_name;
  171.         if( i == 0 ) load_tex_file += "_Left";
  172.         if( i == 1 ) load_tex_file += "_Right";
  173.         if( i == 2 ) load_tex_file += "_Top";
  174.         if( i == 3 ) load_tex_file += "_Bottom";
  175.         if( i == 4 ) load_tex_file += "_Front";
  176.         if( i == 5 ) load_tex_file += "_Back";
  177.         load_tex_file += name_format;
  178.         src_tex_names[i] = load_tex_file;
  179.         Str prefix_load_tex_file = prefix + load_tex_file;
  180.         // If we can load from pak file, or not...
  181.         if( Texture::GetIsLoadFromPak( scene->GetProjectFiles(), load_tex_file, prefix_load_tex_file ) ){
  182.             NU_NEW( file_streams[i], FileStream, ( prefix_load_tex_file, fsp_TopPriority, this ) );
  183.             load_format[i] = prefix.GetIndexOf( "dds" ) != -1 ? tlf_DDS : tlf_PVR;
  184.         }
  185.         else{
  186.             NU_NEW( file_streams[i], FileStream, ( load_tex_file, fsp_TopPriority, this ) );
  187.             load_format[i] = tlf_Source;
  188.         }
  189.     }
  190.     for( size_t i=0; i < 6; i++ ){
  191.         nu_assert_ptr( file_streams[i] );
  192.         file_streams[i]->StartLoad(scene->GetProjectFiles(), &src_tex_names[i]);
  193.     }
  194. }
  195. void TextureGpuRaw::CreateFileStream2D(){
  196.     for( int i=0; i < 6; i++ )
  197.         nu_assert( !file_streams[i] );
  198.     nu_assert( scene );
  199.     Str prefix = GetTexPrefixForCurRender();
  200.     Str prefix_name = prefix + desc.name;
  201.     FileStreamPriority priority = fsp_Texture;
  202.     use_setting = scene->GetTextureSettings()->Get( desc.name );
  203.     if( use_setting && use_setting->no_stream )
  204.         priority  = fsp_TopPriority;
  205.     if( Texture::GetIsLoadFromPak( scene->GetProjectFiles(), desc.name, prefix_name ) ){
  206.         NU_NEW( file_streams[0], FileStream, ( prefix_name, priority, this ) );
  207.         load_format[0] = prefix.GetIndexOf( "dds" ) != -1 ? tlf_DDS : tlf_PVR;
  208.     }
  209.     else{
  210.         NU_NEW( file_streams[0], FileStream, ( desc.name, priority, this ) );
  211.         load_format[0] = tlf_Source;
  212.     }
  213.     nu_assert_ptr( file_streams[0] );
  214.     file_streams[0]->StartLoad(scene->GetProjectFiles(), &desc.name);
  215.     if( !scene->can_stream ){
  216.         // wite while file strem != nullptr;
  217.         THREAD_WAIT( file_streams[0] );
  218.     }
  219. }
  220. Str TextureGpuRaw::GetTexPrefixForCurRender(){
  221.     return isPlatformIPhone() ? "pvr_" : "dds_";
  222. }
  223. Str TextureGpuRaw::GetRealNameCubeMap( Str _name )
  224. {
  225.     int p = 0;
  226.     if( (p = _name.GetIndexOf( "_Left." ))  != -1 ) return _name.substr( 0, p );
  227.     if( (p = _name.GetIndexOf( "_Right." )) != -1 ) return _name.substr( 0, p );
  228.     if( (p = _name.GetIndexOf( "_Top." ))   != -1 ) return _name.substr( 0, p );
  229.     if( (p = _name.GetIndexOf( "_Bottom." ))!= -1 ) return _name.substr( 0, p );
  230.     if( (p = _name.GetIndexOf( "_Front." )) != -1 ) return _name.substr( 0, p );
  231.     if( (p = _name.GetIndexOf( "_Back." ))  != -1 ) return _name.substr( 0, p );
  232.     return Str();
  233. }
  234. int TextureGpuRaw::GetCountSckipMipLevels(int mip_map_count){
  235.     nu_assert_ptr(scene);
  236.     nu_assert_ptr(scene->render);
  237.     int skip_count = GetQualityAccept() ? scene->render_setting->GetTextureQuality() : 0;
  238.     return nu_min(skip_count, mip_map_count - 1);   
  239. }
  240. bool TextureGpuRaw::GetUseMipLevels(){
  241.     return use_setting ? use_setting->use_mip_levels : true;
  242. }
  243. bool TextureGpuRaw::GetQualityAccept(){
  244.     return use_setting ? use_setting->quality_accept : true;
  245. }
  246. bool TextureGpuRaw::isCanRepeat(){
  247.     if( desc.width && desc.height && desc.width == desc.height ){
  248.         int val = 1;
  249.         bool w_ok = false;
  250.         for( size_t i=0; i < 15; i++ ){
  251.             if( desc.width == val )
  252.                 w_ok = true;
  253.             val *= 2;
  254.         }
  255.         if( !w_ok )
  256.             return false;
  257.         val = 1;
  258.         for( size_t i=0; i < 15; i++ ){
  259.             if( desc.height == val )
  260.                 return true;
  261.             val *= 2;
  262.         }
  263.     }
  264.     return false;
  265. }
  266.  
  267. Texture::Texture( Scene *_scene ): SceneResource( _scene ){
  268.     AddToScene(Texture::GetStaticRTTI());
  269. }
  270. Texture::~Texture(){
  271.     RemoveOfScene(Texture::GetStaticRTTI());
  272.     Unload();
  273. }
  274. TextureGpuPtr &Texture::GetTexureGpu(){
  275.     if( !tex_gpu ){
  276.         GpuLoad *gpu_load = scene->render->CreateGpuLoad( FROM, glt_Texture );
  277.         TextureGpuRaw *gpu_raw = static_cast<TextureGpuRaw*>(gpu_load);
  278.         nu_assert_ptr(gpu_raw);
  279.         tex_gpu = TextureGpuPtr(gpu_raw);
  280.         tex_gpu->SetLoadFromDesc(desc);
  281.         desc.init_data = UI_Image();
  282.     }
  283.     return tex_gpu;
  284. }
  285. void Texture::FileChange( Str file_name ){
  286.    
  287.     for( size_t s=0; s < Scene::scenes->Size(); s++ ){
  288.         Texture *tex = Scene::scenes->Get(s)->Get<Texture>( file_name );
  289.         if( tex )
  290.             tex->Unload();
  291.     }
  292. }
  293. void Texture::SetName( Str _name ){
  294.     nu_assert( desc.is_render_target );
  295.     desc.name = _name;
  296. }
  297. void Texture::SetSize( u32 _w, u32 _h ){
  298.     nu_assert( desc.is_render_target );
  299.     if( _w != desc.width || _h != desc.height ){
  300.         desc.width = _w;
  301.         desc.height = _h;
  302.         Unload();
  303.     }
  304. }
  305. void Texture::SetImageData(const UI_Image &image){
  306.     Unload();
  307.     SetSize(image.GetW(), image.GetH());
  308.     nu_assert(desc.is_render_target);
  309.     desc.init_data = image;
  310. }
  311. void Texture::SetFormat( TexFormat _format ){
  312.     nu_assert( desc.is_render_target );
  313.     if( desc.format != _format ){
  314.         desc.format = _format;
  315.         Unload();
  316.     }
  317. }
  318.  
  319. void Texture::Unload(){
  320.     tex_gpu.Reset();
  321. //  SafeRelease( tex_gpu );
  322. }
  323. bool Texture::GetResourceByName(Str &name){
  324.     return desc.name == name;
  325. }
  326. void Texture::SetWritable(){
  327.     desc.is_can_write = true;
  328. }
  329. void Texture::SetReadeble(){
  330.     desc.is_can_read = true;
  331. }
  332. void Texture::SetFileName(Str set_name, bool set_is_cube ){
  333.     desc.name = set_name;
  334.     desc.is_cube = set_is_cube;
  335.     desc.is_from_file = true;
  336.     desc.is_render_target = false;
  337.     need_cached = true;
  338. }
  339. bool Texture::GetIsLoadFromFile(){
  340.     return desc.is_from_file;
  341. }
  342. Str &Texture::GetName(){
  343.     return desc.name;
  344. }
  345. int Texture::GetWidth() const{
  346.     return desc.width;
  347. }
  348. int Texture::GetHeight() const{
  349.     return desc.height;
  350. }
  351. int Texture::GetMipCount(){
  352.     return desc.mip_count;
  353. }
  354. TexFormat Texture::GetFormat(){
  355.     return desc.format;
  356. }
  357. void Texture::SetGroup(TexGroup set_group){
  358.     desc.group = set_group;
  359. }
  360. TexGroup Texture::GetGroup(){
  361.     return desc.group;
  362. }
  363. bool Texture::GetIsCube(){
  364.     return desc.is_cube;
  365. }
  366. void Texture::SetIsCube(bool set_is_cube){
  367.     desc.is_cube = set_is_cube;
  368. }
  369. u32 Texture::GetFileLoadSize(){
  370.     if (tex_gpu)
  371.         return tex_gpu->GetFileSize();
  372.     return 0;
  373. }