// *****************************************************************************
// Copyright (c) 2002-2013
// Programming by Dr.Neeton
//
//
// メッシュ　頂点宣言関連
//
// *****************************************************************************

#include "CPL_CDI_MeshData.h"

namespace CPL {
namespace CDI {


//typedef SmartPtr<DX::VertexElement>::Linked        SP_VTX_ELM;
//typedef Array::Dynamic< DX::VertexElement >        VTX_ELM_TABLE;
//typedef SmartPtr< VTX_ELM_TABLE >::Linked          SP_VTX_ELM_TABLE;
//typedef SmartPtr<DX::VertexStreamLayout>::Linked SP_VTX_STM_ELEMENTS;
//typedef DX::VertexStreamLayout::SpLinked SpVtxStmElms;
//typedef Array::Dynamic< SpVtxStmElms >   VtxStmElmsSpTbl;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ボーン　関連付けデータ設定
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void
  MeshData::LayoutBuffer::zzf_setupData_by_File_
    ( pin<MeshFileInfo::Chunk> in_ptr_chunk
    , _in<DX::DXDevice*      > in_ptr_d3d_dev
    , rio<Array_DataSPtr     > io_table_for_data_pointer
    , rio<Array_ChunkPtr     > io_table_for_chunk_pointer
    )
{
  if( !m_sp_layout_buf )
  {
    return;
  }
  //
  const MeshFileInfo::Chunk**
    pp_chunk = io_table_for_chunk_pointer.Find( this );
  if( !pp_chunk  ||  *pp_chunk )
  {
    return;
  }

  //
  MFI_BufferLayout const*
    ptr_buf_layout_chunk =
      static_cast<MFI_BufferLayout const*>( in_ptr_chunk );

  // データポインタ登録
  io_table_for_data_pointer.Add( ptr_buf_layout_chunk->m_GID, this->GetLinkMyself() );
  // チャンクポインタ登録
  io_table_for_chunk_pointer.Add( this, in_ptr_chunk );

  // 頂点要素テーブルの作成
  const int table_num = ptr_buf_layout_chunk->m_element_num;
  Core::Array::Dynamic< DX::Layout::Element > element_table;
  element_table.Reserve( table_num );

  for( int i = 0; i < table_num; ++i )
  {
    DX::Layout::Element element = ptr_buf_layout_chunk->m_element[ i ];
    element_table.AddEnd( element );
  }
  //
  m_sp_layout_buf =
    CPL_MAKE_SHARED( CLayoutBuffer, element_table.GetTop(), element_table.GetNum() );

  //// ストリーム毎の頂点要素テーブル
  //Array::Dynamic< DX::VertexStreamLayout::SpLinked > vtx_stm_elms_sp_tbl;
  //vtx_stm_elms_sp_tbl.Reserve( DX::STREAM_MAX );
  //int stream_to_index[ DX::STREAM_MAX ];
  //for( int i = 0; i < DX::STREAM_MAX; ++i )
  //{
  //  stream_to_index[ i ] = -1;
  //}

  //int element_num = ptr_buf_layout_chunk->m_element_num;
  //for( int i = 0; i < element_num; ++i )
  //{
  //  DX::VertexElementData const&
  //    elm = ptr_buf_layout_chunk->m_element[ i ];
  //  const int stream = elm.stream;
  //  if( stream_to_index[ stream ] < 0 )
  //  {
  //    stream_to_index[ stream ] = vtx_stm_elms_sp_tbl.GetNum();
  //    DX::VertexStreamLayout::SpLinked
  //      sp_vsl( new DX::VertexStreamLayout( stream ) );
  //    vtx_stm_elms_sp_tbl.AddEnd( sp_vsl );
  //  }
  //  const int buf_idx = stream_to_index[ stream ];
  //  DX::VertexStreamLayout& vsl = *vtx_stm_elms_sp_tbl[ buf_idx ];

  //  switch( elm.format )
  //  {
  //  case DX::VF_XYZ:           vsl.AddElement( DX::VtxElmXYZ()        ); break;
  //  case DX::VF_XYZW:          vsl.AddElement( DX::VtxElmXYZW()       ); break;
  //  case DX::VF_BLEND_WEIGHT1: vsl.AddElement( DX::VtxElmBWeight1()   ); break;
  //  case DX::VF_BLEND_WEIGHT2: vsl.AddElement( DX::VtxElmBWeight2()   ); break;
  //  case DX::VF_BLEND_WEIGHT3: vsl.AddElement( DX::VtxElmBWeight3()   ); break;
  //  case DX::VF_BLEND_INDEX:   vsl.AddElement( DX::VtxElmBIndex()     ); break;
  //  case DX::VF_NORMAL:        vsl.AddElement( DX::VtxElmNormal()     ); break;
  //  case DX::VF_PSIZE:         vsl.AddElement( DX::VtxElmPSize()      ); break;
  //  case DX::VF_TANGENT:       vsl.AddElement( DX::VtxElmTangent()    ); break;
  //  case DX::VF_BINORMAL:      vsl.AddElement( DX::VtxElmBINormal()   ); break;
  //  case DX::VF_TESSFACTOR:    vsl.AddElement( DX::VtxElmTessFactor() ); break;
  //  case DX::VF_FOG:           vsl.AddElement( DX::VtxElmFog()        ); break;
  //  case DX::VF_DEPTH:         vsl.AddElement( DX::VtxElmDepth()      ); break;
  //  case DX::VF_TEXCOORD0:     vsl.AddElement( DX::VtxElmTex0()       ); break;
  //  case DX::VF_TEXCOORD1:     vsl.AddElement( DX::VtxElmTex1()       ); break;
  //  case DX::VF_TEXCOORD2:     vsl.AddElement( DX::VtxElmTex2()       ); break;
  //  case DX::VF_TEXCOORD3:     vsl.AddElement( DX::VtxElmTex3()       ); break;
  //  case DX::VF_TEXCOORD4:     vsl.AddElement( DX::VtxElmTex4()       ); break;
  //  case DX::VF_TEXCOORD5:     vsl.AddElement( DX::VtxElmTex5()       ); break;
  //  case DX::VF_TEXCOORD6:     vsl.AddElement( DX::VtxElmTex6()       ); break;
  //  case DX::VF_TEXCOORD7:     vsl.AddElement( DX::VtxElmTex7()       ); break;
  //  case DX::VF_COLOR0:        vsl.AddElement( DX::VtxElmColor0()     ); break;
  //  case DX::VF_COLOR1:        vsl.AddElement( DX::VtxElmColor1()     ); break;
  //  case DX::VF_COLOR2:        vsl.AddElement( DX::VtxElmColor2()     ); break;
  //  case DX::VF_COLOR3:        vsl.AddElement( DX::VtxElmColor3()     ); break;
  //  case DX::VF_COLOR4:        vsl.AddElement( DX::VtxElmColor4()     ); break;
  //  case DX::VF_COLOR5:        vsl.AddElement( DX::VtxElmColor5()     ); break;
  //  case DX::VF_COLOR6:        vsl.AddElement( DX::VtxElmColor6()     ); break;
  //  case DX::VF_COLOR7:        vsl.AddElement( DX::VtxElmColor7()     ); break;
  //  }
  //}

  //// 頂点レイアウトに、頂点ストリームレイアウトを追加
  //for( int stream_idx = 0; stream_idx < DX::STREAM_MAX; ++stream_idx )
  //{
  //  m_sp_vtx_layout->Add
  //  if( vtx_stm_elms_tbl[ stream_idx ].GetElementNum() > 0 )
  //  {
  //    // 終端記号を追加
  //    vtx_stm_elms_tbl[ stream_idx ].AddElement( DX::VtxElmEnd() );
  //  }
  //}
  //// エレメントポインタテーブルを作成
  //Array::Dynamic< DX::BaseElement* > elm_ptr_tbl;
  //int table_num = vtx_stm_elms_tbl.GetNum();
  //elm_ptr_tbl.Resize( table_num );
  //for( int i = 0; i < table_num; ++i )
  //{
  //  elm_ptr_tbl[i] = &vtx_stm_elms_tbl[i];
  //}

  ////
  //zzf_createDeclaration_by_Elements( in_ptr_d3d_dev, &elm_ptr_tbl[0] );
}




} // namespace CDI
} // namespace CPL