HRESULT CDirectxWaveform::preDrawFrame() { // ASSUMES m_refLock IS HELD BY CALLER if(!m_dataTex || !m_dataTexData || !m_pPSGlobals) { return E_POINTER; } // push the radio input to the video buffers D3D10_MAPPED_TEXTURE2D mappedDataTex; memset(&mappedDataTex, 0, sizeof(mappedDataTex)); UINT subr = D3D10CalcSubresource(0, 0, 0); HRESULT hR = m_dataTex->Map(subr, D3D10_MAP_WRITE_DISCARD, 0, &mappedDataTex); if(FAILED(hR)) return hR; if(m_bIsComplexInput) { UINT halfWidth = m_dataTexWidth / 2; memcpy(((char*)mappedDataTex.pData) + halfWidth*m_dataTexElemSize, m_dataTexData, m_dataTexElemSize*halfWidth); memcpy(mappedDataTex.pData, ((char*)m_dataTexData) + halfWidth*m_dataTexElemSize, m_dataTexElemSize*halfWidth); } else { memcpy(mappedDataTex.pData, m_dataTexData, m_dataTexElemSize*m_dataTexWidth); } m_dataTex->Unmap(subr); return S_OK; }
bool ds::Texture::Map( BYTE* Pixels ) { D3D10_MAPPED_TEXTURE2D l_Mapped; if( FAILED( Texture2D->Map( D3D10CalcSubresource( 0, 0, 1 ), D3D10_MAP_WRITE_DISCARD, 0, &l_Mapped ) ) ) { Pixels = nullptr; dsPushError( ErrTextureMap ); return false; } Pixels = ( BYTE* )l_Mapped.pData; return true; }
//-------------------------------------------------------------------------------------- HRESULT LoadAudioIntoBuffer( ID3D10Device* pd3dDevice, UINT uiTexSizeX, LPCTSTR szFileName ) { HRESULT hr = S_OK; // Load the wave file CAudioData audioData; if( !audioData.LoadWaveFile( ( TCHAR* )szFileName ) ) return E_FAIL; // Normalize the data audioData.NormalizeData(); // If we have data, get the number of samples unsigned long ulNumSamples = audioData.GetNumSamples(); // Find out how much Y space (time) our spectogram will need g_uiTexY = ( ulNumSamples / uiTexSizeX ) - 1; // Create a texture large enough to hold our data hr = CreateBuffers( pd3dDevice, uiTexSizeX, g_uiTexY ); if( FAILED( hr ) ) return hr; // Create temp storage with space for imaginary data unsigned long size = uiTexSizeX * g_uiTexY; D3DXVECTOR2* pvData = new D3DXVECTOR2[ size ]; if( !pvData ) return E_OUTOFMEMORY; float* pDataPtr = audioData.GetChannelPtr( 0 ); for( unsigned long s = 0; s < size; s++ ) { if( s < ulNumSamples ) { pvData[s].x = pDataPtr[ s ]; pvData[s].y = 0.0f; } else { pvData[s] = D3DXVECTOR2( 0.0f, 0.0f ); } } // Update the texture with this information pd3dDevice->UpdateSubresource( g_pSourceTexture, D3D10CalcSubresource( 0, 0, 1 ), NULL, pvData, uiTexSizeX * sizeof( D3DXVECTOR2 ), 0 ); SAFE_DELETE_ARRAY( pvData ); return hr; }
void D10State::blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h) { HRESULT hr; ods("D3D10: Blit %d %d %d %d", x, y, w, h); if (! pTexture || ! pSRView || uiLeft == uiRight) return; D3D10_MAPPED_TEXTURE2D mappedTex; hr = pTexture->Map(D3D10CalcSubresource(0, 0, 1), D3D10_MAP_WRITE_DISCARD, 0, &mappedTex); if (FAILED(hr)) { ods("D3D10: Failed map"); } UCHAR* pTexels = (UCHAR*)mappedTex.pData; for (unsigned int r=0;r< uiHeight; ++r) { unsigned char *sptr = a_ucTexture + r * uiWidth * 4; unsigned char *dptr = reinterpret_cast<unsigned char *>(pTexels) + r * mappedTex.RowPitch; memcpy(dptr, sptr, uiWidth * 4); } pTexture->Unmap(D3D10CalcSubresource(0, 0, 1)); }
bool xD11UnkwonTexture::_update(ID3D11Resource* pLockTexture , void* data , int dateLen , int rowPitch , int depthPicth , int mipmapLevel , int arraySlice) { if(pLockTexture == NULL) return false; ID3D11DeviceContext* pDeviceContext = m_pD11RenderApi->d11DeviceContext(); UINT lockResource = D3D10CalcSubresource((UINT)mipmapLevel , (UINT)arraySlice, (UINT)m_TexInfo.m_MipmapLevel); if(m_TexInfo.m_Usage == D3D11_USAGE_DEFAULT) { if(rowPitch == 0 ) rowPitch = dateLen; if(depthPicth == 0) depthPicth = dateLen; pDeviceContext->UpdateSubresource(pLockTexture ,lockResource , NULL , data ,rowPitch , depthPicth); } else //只有非DEFAULT的纹理才是可以Map的。 { char* pMapedData = NULL; int Pitch = 0; int SlicePith = 0; UINT mapFlag = 0; if(pMapedData == NULL) return false; D3D11_MAPPED_SUBRESOURCE mappedTex; mappedTex.pData = NULL; mappedTex.RowPitch = 0; mappedTex.DepthPitch = 0; pDeviceContext->Map(pLockTexture , lockResource , D3D11_MAP_WRITE_DISCARD , mapFlag , &mappedTex ); pMapedData = (char*)mappedTex.pData; Pitch = mappedTex.RowPitch; SlicePith = mappedTex.DepthPitch; memcpy(pMapedData , data , dateLen); pDeviceContext->Unmap(pLockTexture , lockResource); } return true; }
void D3D10Grab(ID3D10Texture2D* pBackBuffer) { D3D10_TEXTURE2D_DESC tex_desc; pBackBuffer->GetDesc(&tex_desc); ID3D10Device *pDev; pBackBuffer->GetDevice(&pDev); ID3D10Texture2D * pTexture; D3D10_MAPPED_TEXTURE2D mappedTexture; tex_desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; tex_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; tex_desc.ArraySize = 1; tex_desc.MipLevels = 1; tex_desc.BindFlags = 0; tex_desc.SampleDesc.Count = 1; tex_desc.SampleDesc.Quality = 0; tex_desc.Usage = D3D10_USAGE_STAGING; tex_desc.MiscFlags = 0; HRESULT hr = pDev->CreateTexture2D(&tex_desc, NULL, &pTexture); #ifdef DEBUG reportLog(EVENTLOG_INFORMATION_TYPE, L"pDev->CreateTexture2D 0x%x", hr); #endif pDev->CopyResource(pTexture, pBackBuffer); D3D10_BOX box = {0, 0, tex_desc.Width, tex_desc.Height, 0, 1}; pDev->CopySubresourceRegion(pTexture, 0, 0, 0, 0, pBackBuffer, 0, &box); DxgiFrameGrabber *dxgiFrameGrabber = DxgiFrameGrabber::getInstance(); IPCContext *ipcContext = dxgiFrameGrabber->m_ipcContext; Logger *logger = dxgiFrameGrabber->m_logger; if (S_OK != (hr = pTexture->Map(D3D10CalcSubresource(0, 0, 1), D3D10_MAP_READ, 0, &mappedTexture))) { logger->reportLogError(L"d3d10 couldn't map texture, hresult = 0x%x", hr); goto end; } ipcContext->m_memDesc.width = tex_desc.Width; ipcContext->m_memDesc.height = tex_desc.Height; ipcContext->m_memDesc.rowPitch = mappedTexture.RowPitch; ipcContext->m_memDesc.format = BufferFormatAbgr; ipcContext->m_memDesc.frameId++; // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d10 texture description. width: %u, height: %u, pitch: %u", tex_desc.Width, tex_desc.Height, mappedTexture.RowPitch); DWORD errorcode; if (WAIT_OBJECT_0 == (errorcode = WaitForSingleObject(ipcContext->m_hMutex, 0))) { // __asm__("int $3"); // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d10 writing description to mem mapped file"); memcpy(ipcContext->m_pMemMap, &ipcContext->m_memDesc, sizeof (ipcContext->m_memDesc)); // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d10 writing data to mem mapped file"); PVOID pMemDataMap = incPtr(ipcContext->m_pMemMap, sizeof (ipcContext->m_memDesc)); if (mappedTexture.RowPitch == tex_desc.Width * 4) { memcpy(pMemDataMap, mappedTexture.pData, tex_desc.Width * tex_desc.Height * 4); } else { UINT cleanOffset = 0, pitchOffset = 0, i = 0; while (i < tex_desc.Height) { memcpy(incPtr(pMemDataMap, cleanOffset), incPtr(mappedTexture.pData, pitchOffset), tex_desc.Width * 4); cleanOffset += tex_desc.Width * 4; pitchOffset += mappedTexture.RowPitch; i++; } } ReleaseMutex(ipcContext->m_hMutex); SetEvent(ipcContext->m_hFrameGrabbedEvent); } else { logger->reportLogError(L"d3d10 couldn't wait mutex. errocode = 0x%x", errorcode); } pTexture->Unmap(D3D10CalcSubresource(0, 0, 1)); end: pTexture->Release(); pDev->Release(); }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { float ClearColor[4] = { 1.0f, 1.0f, 1.0f, 0.0f }; // R, G, B, A ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); // If the settings dialog is being shown, then render it instead of rendering the app's scene if( g_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } // Set our render target since we can't present multisampled ref ID3D10RenderTargetView* pOrigRT; ID3D10DepthStencilView* pOrigDS; pd3dDevice->OMGetRenderTargets( 1, &pOrigRT, &pOrigDS ); ID3D10RenderTargetView* aRTViews[ 1 ] = { g_pRTRV }; pd3dDevice->OMSetRenderTargets( 1, aRTViews, g_pDSRV ); pd3dDevice->ClearRenderTargetView( g_pRTRV, ClearColor ); pd3dDevice->ClearDepthStencilView( g_pDSRV, D3D10_CLEAR_DEPTH, 1.0, 0 ); // Set Matrices D3DXMATRIX mWorldViewProj; D3DXMATRIX mWorldView; D3DXMATRIX mViewProj; D3DXMATRIX mWorld; D3DXMATRIX mView; D3DXMATRIX mProj; // Fix the camera motion for now D3DXMATRIX mBlurViewProj[MAX_TIME_STEPS]; for( int i = 0; i < MAX_TIME_STEPS; i++ ) { mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mBlurViewProj[i] = mView * mProj; } g_pmBlurViewProj->SetMatrixArray( ( float* )mBlurViewProj, 0, MAX_TIME_STEPS ); D3DXMatrixIdentity( &mWorld ); mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mViewProj = mView * mProj; mWorldViewProj = mWorld * mViewProj; mWorldView = mWorld * mView; g_pmWorldViewProj->SetMatrix( ( float* )&mWorldViewProj ); g_pmViewProj->SetMatrix( ( float* )&mViewProj ); g_pmWorldView->SetMatrix( ( float* )&mWorldView ); RenderSceneMesh( pd3dDevice, &g_SceneMesh, false ); RenderFanMesh( pd3dDevice, &g_FanMesh, fTime, false ); if( g_bRenderOgre ) RenderSkinnedMesh( pd3dDevice, &g_AnimMesh, fTime, false ); for( UINT iMesh = 0; iMesh < g_NumLinkedMeshes; iMesh++ ) RenderLinkedMesh( pd3dDevice, &g_pLinkedMeshes[iMesh], &g_AnimMesh, g_MeshLinkages[iMesh].iBone, fTime, false ); if( g_bUseMotionBlur ) { RenderFanMesh( pd3dDevice, &g_FanMesh, fTime, true ); if( g_bRenderOgre ) RenderSkinnedMesh( pd3dDevice, &g_AnimMesh, fTime, true ); for( UINT iMesh = 0; iMesh < g_NumLinkedMeshes; iMesh++ ) RenderLinkedMesh( pd3dDevice, &g_pLinkedMeshes[iMesh], &g_AnimMesh, g_MeshLinkages[iMesh].iBone, fTime, true ); } //MSAA resolve ID3D10Resource* pRT; pOrigRT->GetResource( &pRT ); D3D10_RENDER_TARGET_VIEW_DESC rtDesc; pOrigRT->GetDesc( &rtDesc ); pd3dDevice->ResolveSubresource( pRT, D3D10CalcSubresource( 0, 0, 1 ), g_pRenderTarget, D3D10CalcSubresource( 0, 0, 1 ), rtDesc.Format ); SAFE_RELEASE( pRT ); // Use our Old RT again aRTViews[0] = pOrigRT; pd3dDevice->OMSetRenderTargets( 1, aRTViews, pOrigDS ); SAFE_RELEASE( pOrigRT ); SAFE_RELEASE( pOrigDS ); DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); RenderText(); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); DXUT_EndPerfEvent(); }
void ds::Texture::UnMap( ) { Texture2D->Unmap( D3D10CalcSubresource( 0, 0, 1 ) ); }