void SkinnedMeshNode::RenderInstancing( int instanceSize ) { m_pRscVetextBuffer->SetStreamSource(0,D3DXGetDeclVertexSize(declBlendInstance,0)); m_pRscVetextBuffer->SetStreamSourceFreq(0,D3DSTREAMSOURCE_INDEXEDDATA | instanceSize); m_pInstanceDataBuffer->SetStreamSource(1,D3DXGetDeclVertexSize(declBlendInstance,1)); m_pInstanceDataBuffer->SetStreamSourceFreq(1,D3DSTREAMSOURCE_INSTANCEDATA|1); m_pRscIndexBuffer->SetIndices(); Graphics::m_pDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,0,0,m_pRscVetextBuffer->GetVertexCount(),m_startIndex,m_primitiveCount ); }
ETOOLS_API UINT WINAPI D3DX_GetDeclVertexSize( CONST D3DVERTEXELEMENT9* pDecl, DWORD Stream) { return D3DXGetDeclVertexSize(pDecl,Stream); }
int RenderSystem::GetSizeFromDeclaration(IDirect3DVertexDeclaration9* declaration) { if(!declaration) return 0; D3DVERTEXELEMENT9 elements[256]; memset(elements, 0, sizeof(elements)); unsigned int elementsCount = 0; declaration->GetDeclaration(elements, &elementsCount); return D3DXGetDeclVertexSize(&elements[0],0); }
SGeometry* CResourceManager::CreateGeom (D3DVERTEXELEMENT9* decl, IDirect3DVertexBuffer9* vb, IDirect3DIndexBuffer9* ib) { R_ASSERT (decl && vb); SDeclaration* dcl = _CreateDecl (decl); u32 vb_stride = D3DXGetDeclVertexSize (decl,0); // ***** first pass - search already loaded shader for (u32 it=0; it<v_geoms.size(); it++) { SGeometry& G = *(v_geoms[it]); if ((G.dcl==dcl) && (G.vb==vb) && (G.ib==ib) && (G.vb_stride==vb_stride)) return v_geoms[it]; } SGeometry *Geom = xr_new<SGeometry> (); Geom->dwFlags |= xr_resource_flagged::RF_REGISTERED; Geom->dcl = dcl; Geom->vb = vb; Geom->vb_stride = vb_stride; Geom->ib = ib; v_geoms.push_back (Geom); return Geom; }
void CRender::LoadBuffers (CStreamReader *base_fs, BOOL _alternative) { R_ASSERT2 (base_fs,"Could not load geometry. File not found."); dxRenderDeviceRender::Instance().Resources->Evict (); // u32 dwUsage = D3DUSAGE_WRITEONLY; xr_vector<VertexDeclarator> &_DC = _alternative?xDC:nDC; xr_vector<ID3DVertexBuffer*> &_VB = _alternative?xVB:nVB; xr_vector<ID3DIndexBuffer*> &_IB = _alternative?xIB:nIB; // Vertex buffers { // Use DX9-style declarators CStreamReader *fs = base_fs->open_chunk(fsL_VB); R_ASSERT2 (fs,"Could not load geometry. File 'level.geom?' corrupted."); u32 count = fs->r_u32(); _DC.resize (count); _VB.resize (count); u32 bufferSize = (MAXD3DDECLLENGTH+1)*sizeof(D3DVERTEXELEMENT9); D3DVERTEXELEMENT9* dcl = (D3DVERTEXELEMENT9*)_alloca(bufferSize); for (u32 i=0; i<count; i++) { // decl // D3DVERTEXELEMENT9* dcl = (D3DVERTEXELEMENT9*) fs().pointer(); fs->r (dcl,bufferSize); fs->advance (-(int)bufferSize); u32 dcl_len = D3DXGetDeclLength (dcl)+1; _DC[i].resize (dcl_len); fs->r (_DC[i].begin(),dcl_len*sizeof(D3DVERTEXELEMENT9)); // count, size u32 vCount = fs->r_u32 (); u32 vSize = D3DXGetDeclVertexSize (dcl,0); Msg ("* [Loading VB] %d verts, %d Kb",vCount,(vCount*vSize)/1024); // Create and fill //BYTE* pData = 0; //R_CHK (HW.pDevice->CreateVertexBuffer(vCount*vSize,dwUsage,0,D3DPOOL_MANAGED,&_VB[i],0)); //R_CHK (_VB[i]->Lock(0,0,(void**)&pData,0)); // CopyMemory (pData,fs().pointer(),vCount*vSize); //fs->r (pData,vCount*vSize); //_VB[i]->Unlock (); // TODO: DX10: Check fragmentation. // Check if buffer is less then 2048 kb BYTE* pData = xr_alloc<BYTE>(vCount*vSize); fs->r (pData,vCount*vSize); dx10BufferUtils::CreateVertexBuffer (&_VB[i], pData, vCount*vSize); HW.stats_manager.increment_stats_vb (_VB[i]); xr_free(pData); // fs->advance (vCount*vSize); } fs->close (); } // Index buffers { CStreamReader *fs = base_fs->open_chunk(fsL_IB); u32 count = fs->r_u32(); _IB.resize (count); for (u32 i=0; i<count; i++) { u32 iCount = fs->r_u32 (); Msg("* [Loading IB] %d indices, %d Kb",iCount,(iCount*2)/1024); // Create and fill //BYTE* pData = 0; //R_CHK (HW.pDevice->CreateIndexBuffer(iCount*2,dwUsage,D3DFMT_INDEX16,D3DPOOL_MANAGED,&_IB[i],0)); //R_CHK (_IB[i]->Lock(0,0,(void**)&pData,0)); // CopyMemory (pData,fs().pointer(),iCount*2); //fs->r (pData,iCount*2); //_IB[i]->Unlock (); // TODO: DX10: Check fragmentation. // Check if buffer is less then 2048 kb BYTE* pData = xr_alloc<BYTE>(iCount*2); fs->r (pData,iCount*2); dx10BufferUtils::CreateIndexBuffer (&_IB[i], pData, iCount*2); HW.stats_manager.increment_stats_ib (_IB[i]); xr_free(pData); // fs().advance (iCount*2); } fs->close (); } }
static void test_get_decl_vertex_size(void) { static const D3DVERTEXELEMENT9 declaration1[] = { {0, 0, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {1, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {2, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {3, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {4, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {5, 0, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {6, 0, D3DDECLTYPE_SHORT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {7, 0, D3DDECLTYPE_SHORT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {8, 0, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {9, 0, D3DDECLTYPE_SHORT2N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {10, 0, D3DDECLTYPE_SHORT4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {11, 0, D3DDECLTYPE_UDEC3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {12, 0, D3DDECLTYPE_DEC3N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {13, 0, D3DDECLTYPE_FLOAT16_2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {14, 0, D3DDECLTYPE_FLOAT16_4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, D3DDECL_END(), }; static const D3DVERTEXELEMENT9 declaration2[] = { {0, 8, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {1, 8, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {2, 8, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {3, 8, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {4, 8, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {5, 8, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {6, 8, D3DDECLTYPE_SHORT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {7, 8, D3DDECLTYPE_SHORT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 8, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {1, 8, D3DDECLTYPE_SHORT2N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {2, 8, D3DDECLTYPE_SHORT4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {3, 8, D3DDECLTYPE_UDEC3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {4, 8, D3DDECLTYPE_DEC3N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {5, 8, D3DDECLTYPE_FLOAT16_2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {6, 8, D3DDECLTYPE_FLOAT16_4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {7, 8, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, D3DDECL_END(), }; static const UINT sizes1[] = { 4, 8, 12, 16, 4, 4, 4, 8, 4, 4, 8, 4, 4, 4, 8, 0, }; static const UINT sizes2[] = { 12, 16, 20, 24, 12, 12, 16, 16, }; unsigned int i; UINT size; size = D3DXGetDeclVertexSize(NULL, 0); ok(size == 0, "Got size %#x, expected 0.\n", size); for (i = 0; i < 16; ++i) { size = D3DXGetDeclVertexSize(declaration1, i); ok(size == sizes1[i], "Got size %u for stream %u, expected %u.\n", size, i, sizes1[i]); } for (i = 0; i < 8; ++i) { size = D3DXGetDeclVertexSize(declaration2, i); ok(size == sizes2[i], "Got size %u for stream %u, expected %u.\n", size, i, sizes2[i]); } }
void Face::Render(FaceController *pController) { if (m_pBaseMesh == NULL || pController == NULL) return; //Set Active Vertex Declaration g_pDevice->SetVertexDeclaration(m_pFaceVertexDecl); //Set streams D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE]; m_pBaseMesh->GetDeclaration(decl); DWORD vSize = D3DXGetDeclVertexSize(decl, 0); IDirect3DVertexBuffer9* baseMeshBuffer = NULL; m_pBaseMesh->GetVertexBuffer(&baseMeshBuffer); g_pDevice->SetStreamSource(0, baseMeshBuffer, 0, vSize); //Set Blink Source vSize = D3DXGetFVFVertexSize(m_pBlinkMesh->GetFVF()); IDirect3DVertexBuffer9* blinkBuffer = NULL; m_pBlinkMesh->GetVertexBuffer(&blinkBuffer); g_pDevice->SetStreamSource(1, blinkBuffer, 0, vSize); //Set Emotion Source IDirect3DVertexBuffer9* emotionBuffer = NULL; m_emotionMeshes[pController->m_emotionIndex]->GetVertexBuffer(&emotionBuffer); g_pDevice->SetStreamSource(2, emotionBuffer, 0, vSize); //Set Speech Sources for (int i=0; i<2; i++) { IDirect3DVertexBuffer9* speechBuffer = NULL; m_speechMeshes[pController->m_speechIndices[i]]->GetVertexBuffer(&speechBuffer); g_pDevice->SetStreamSource(3 + i, speechBuffer, 0, vSize); } //Set Index buffer IDirect3DIndexBuffer9* ib = NULL; m_pBaseMesh->GetIndexBuffer(&ib); g_pDevice->SetIndices(ib); //Set Shader variables D3DXMATRIX view, proj; g_pDevice->GetTransform(D3DTS_VIEW, &view); g_pDevice->GetTransform(D3DTS_PROJECTION, &proj); g_pEffect->SetMatrix("matW", &pController->m_headMatrix); g_pEffect->SetMatrix("matVP", &(view * proj)); g_pEffect->SetTexture("texDiffuse", m_pFaceTexture); g_pEffect->SetTexture("texNormalMap", m_pFaceNormalMap); g_pEffect->SetVector("weights", &pController->m_morphWeights); //Start Technique D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("FaceMorphNormalMap"); g_pEffect->SetTechnique(hTech); g_pEffect->Begin(NULL, NULL); g_pEffect->BeginPass(0); //Draw mesh g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_pBaseMesh->GetNumVertices(), 0, m_pBaseMesh->GetNumFaces()); g_pEffect->EndPass(); g_pEffect->End(); //Restore vertex declaration and stream sources g_pDevice->SetVertexDeclaration(NULL); for (int i=0; i<5; i++) { g_pDevice->SetStreamSource(i, NULL, 0, 0); } }
void Renderer::DrawScene() { D3DCOLOR color = D3DRGBA(0.0f,1.0f,0.0f,1.0f); // Clear the render target and the zbuffer m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, color, 1.0f, 0 ); //Render the scene if( SUCCEEDED( m_pDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class // Update the effect's variables D3DXMATRIXA16 viewProjection = m_View * m_Projection; m_pEffect->SetMatrix( "g_mViewProjection" , &viewProjection); HRESULT hr; UINT iPass, cPasses; m_pDevice->SetVertexDeclaration( m_pVertexDeclHardware ); // Stream zero is our model, and its frequency is how we communicate the number of instances required, // which in this case is the total number of boxes m_pDevice->SetStreamSource( 0, m_pVB, 0, D3DXGetDeclVertexSize( m_VertexDeclaration , 0) ); m_pDevice->SetStreamSourceFreq( 0, D3DSTREAMSOURCE_INDEXEDDATA | m_Size); // Stream one is the instancing buffer, so this advances to the next value // after each box instance has been drawn, so the divider is 1. m_pDevice->SetStreamSource( 1, m_pVBInstanceData, 0, sizeof(InstanceData) ); m_pDevice->SetStreamSourceFreq( 1, D3DSTREAMSOURCE_INSTANCEDATA | 1ul ); m_pDevice->SetIndices( m_pIB ); // Render the scene with this technique // as defined in the .fx file m_pEffect->SetTechnique( "RenderScene" ); m_pEffect->Begin( &cPasses, 0 ); for( iPass = 0; iPass < cPasses; iPass++ ) { m_pEffect->BeginPass( iPass ); // Render the boxes with the applied technique // The effect interface queues up the changes and performs them // with the CommitChanges call. You do not need to call CommitChanges if // you are not setting any parameters between the BeginPass and EndPass. m_pEffect->CommitChanges(); //m_pDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 4 * 6, 0, 6 * 2 ); m_pSphere->DrawSubset(0); m_pEffect->EndPass(); } m_pEffect->End(); m_pDevice->SetStreamSourceFreq( 0, 1 ); m_pDevice->SetStreamSourceFreq( 1, 1 ); m_pDevice->EndScene(); } }
void Model::CreateFromSDKMeshFile(ID3D11Device* device, LPCWSTR fileName) { _ASSERT(FileExists(fileName)); // Use the SDKMesh class to load in the data SDKMesh sdkMesh; sdkMesh.Create(fileName); wstring directory = GetDirectoryFromFileName(fileName); // Make materials UINT numMaterials = sdkMesh.GetNumMaterials(); for (UINT i = 0; i < numMaterials; ++i) { MeshMaterial material; SDKMESH_MATERIAL* mat = sdkMesh.GetMaterial(i); memcpy(&material.AmbientAlbedo, &mat->Ambient, sizeof(D3DXVECTOR4)); memcpy(&material.DiffuseAlbedo, &mat->Diffuse, sizeof(D3DXVECTOR4)); memcpy(&material.SpecularAlbedo, &mat->Specular, sizeof(D3DXVECTOR4)); memcpy(&material.Emissive, &mat->Emissive, sizeof(D3DXVECTOR4)); material.Alpha = mat->Diffuse.w; material.SpecularPower = mat->Power; material.DiffuseMapName = AnsiToWString(mat->DiffuseTexture); material.NormalMapName = AnsiToWString(mat->NormalTexture); LoadMaterialResources(material, directory, device); meshMaterials.push_back(material); } // Make a D3D9 device IDirect3DDevice9Ptr d3d9Device = CreateD3D9Device(); UINT numMeshes = sdkMesh.GetNumMeshes(); for (UINT meshIdx = 0; meshIdx < numMeshes; ++meshIdx) { // Figure out the index type UINT ops = D3DXMESH_MANAGED; UINT indexSize = 2; Mesh::IndexType indexType = Mesh::Index16Bit; if (sdkMesh.GetIndexType(meshIdx) == IT_32BIT) { ops |= D3DXMESH_32BIT; indexSize = 4; indexType = Mesh::Index32Bit; } // Make a D3DX mesh ID3DXMesh* d3dxMesh = NULL; UINT numPrims = static_cast<UINT>(sdkMesh.GetNumIndices(meshIdx) / 3); UINT numVerts = static_cast<UINT>(sdkMesh.GetNumVertices(meshIdx, 0)); UINT vbIndex = sdkMesh.GetMesh(meshIdx)->VertexBuffers[0]; UINT ibIndex = sdkMesh.GetMesh(meshIdx)->IndexBuffer; const D3DVERTEXELEMENT9* vbElements = sdkMesh.VBElements(vbIndex); DXCall(D3DXCreateMesh(numPrims, numVerts, ops, vbElements, d3d9Device, &d3dxMesh)); IUnknownReleaser<ID3DXMesh> meshReleaser(d3dxMesh); // Copy in vertex data BYTE* verts = NULL; BYTE* srcVerts = reinterpret_cast<BYTE*>(sdkMesh.GetRawVerticesAt(vbIndex)); UINT vbStride = sdkMesh.GetVertexStride(meshIdx, 0); UINT declStride = D3DXGetDeclVertexSize(vbElements, 0); DXCall(d3dxMesh->LockVertexBuffer(0, reinterpret_cast<void**>(&verts))); for (UINT vertIdx = 0; vertIdx < numVerts; ++vertIdx) { memcpy(verts, srcVerts, declStride); verts += declStride; srcVerts += vbStride; } DXCall(d3dxMesh->UnlockVertexBuffer()); // Copy in index data void* indices = NULL; void* srcIndices = sdkMesh.GetRawIndicesAt(ibIndex); DXCall(d3dxMesh->LockIndexBuffer(0, &indices)); memcpy(indices, srcIndices, numPrims * 3 * indexSize); DXCall(d3dxMesh->UnlockIndexBuffer()); // Set up the attribute table DWORD* attributeBuffer = NULL; DXCall(d3dxMesh->LockAttributeBuffer(0, &attributeBuffer)); UINT numSubsets = sdkMesh.GetNumSubsets(meshIdx); D3DXATTRIBUTERANGE* attributes = new D3DXATTRIBUTERANGE[numSubsets]; ArrayDeleter<D3DXATTRIBUTERANGE> attributeDeleter(attributes); for (UINT i = 0; i < numSubsets; ++i) { SDKMESH_SUBSET* subset = sdkMesh.GetSubset(meshIdx, i); attributes[i].AttribId = subset->MaterialID; attributes[i].FaceStart = static_cast<DWORD>(subset->IndexStart / 3); attributes[i].FaceCount = static_cast<DWORD>(subset->IndexCount / 3); attributes[i].VertexStart = static_cast<DWORD>(subset->VertexStart); // attributes[i].VertexCount = static_cast<DWORD>(subset->VertexCount); attributes[i].VertexCount = numVerts; for (UINT faceIdx = attributes[i].FaceStart; faceIdx < attributes[i].FaceStart + attributes[i].FaceCount; ++faceIdx) attributeBuffer[faceIdx] = subset->MaterialID; } DXCall(d3dxMesh->UnlockAttributeBuffer()); d3dxMesh->SetAttributeTable(attributes, numSubsets); // Generate initial adjacency vector<DWORD> initialAdjacency; initialAdjacency.resize(d3dxMesh->GetNumFaces() * 3); DXCall(d3dxMesh->GenerateAdjacency(0.0001f, &initialAdjacency[0])); // Make the mesh Mesh mesh; mesh.CreateFromD3DXMesh(directory, device, d3d9Device, d3dxMesh, false, false, &initialAdjacency[0], indexType); meshes.push_back(mesh); } }