void CXZip::PreloadData() { Assert( IsValid() ); // Ensure it isn't already preloaded if( m_pPreloadedData ) return; // If I don't have a preloaded section, ignore the request. if( !m_Header.PreloadBytes || !m_Header.PreloadDirectoryEntries ) return; // Allocate and read the data block in: #ifndef _X360 MEM_ALLOC_CREDIT_( "xZip" ); m_pPreloadedData = malloc( m_Header.PreloadBytes ); // Just drop out if allocation fails; if ( !m_pPreloadedData ) return; m_pRead( m_pPreloadedData, m_nPreloadStart, -1, m_Header.PreloadBytes, m_hUser ); #else int nAlignedStart = AlignValue( ( m_nPreloadStart - XBOX_HDD_SECTORSIZE ) + 1, XBOX_HDD_SECTORSIZE ); int nBytesToRead = AlignValue( ( m_nPreloadStart - nAlignedStart ) + m_Header.PreloadBytes, XBOX_HDD_SECTORSIZE ); int nBytesBuffer = AlignValue( nBytesToRead, XBOX_HDD_SECTORSIZE ); byte *pReadData = (byte *)malloc( nBytesBuffer ); // Just drop out if allocation fails; if ( !pReadData ) return; MEM_ALLOC_CREDIT_( "xZip" ); m_pRead( pReadData, nAlignedStart, nBytesBuffer,nBytesToRead, m_hUser ); m_pPreloadedData = pReadData + ( m_nPreloadStart - nAlignedStart ); #endif // Set up the preload directory: m_pPreloadDirectory = (xZipDirectoryEntry_t*)m_pPreloadedData; // Swap the preload directory: if ( m_bByteSwapped ) { for ( unsigned nDirectoryEntry = 0; nDirectoryEntry < m_Header.PreloadDirectoryEntries; nDirectoryEntry++ ) { m_Swap.SwapFieldsToTargetEndian<xZipDirectoryEntry_t>( &( m_pPreloadDirectory[nDirectoryEntry] ) ); } } // Set up the regular 2 preload mapping section: m_nRegular2PreloadEntryMapping = (unsigned short*)(((unsigned char*)m_pPreloadDirectory) + ( sizeof(xZipDirectoryEntry_t) * m_Header.PreloadDirectoryEntries )); // Swap the regular to preload mapping if ( m_bByteSwapped ) { m_Swap.SwapBufferToTargetEndian<short>( (short *)m_nRegular2PreloadEntryMapping, (short *)m_nRegular2PreloadEntryMapping, m_Header.DirectoryEntries ); } }
bool CMemoryStack::InitPhysical( unsigned size, unsigned alignment ) { m_bPhysical = true; m_maxSize = m_commitSize = size; m_alignment = AlignValue( alignment, 4 ); int flags = PAGE_READWRITE; if ( size >= 16*1024*1024 ) { flags |= MEM_16MB_PAGES; } else { flags |= MEM_LARGE_PAGES; } m_pBase = (unsigned char *)XPhysicalAlloc( m_maxSize, MAXULONG_PTR, 4096, flags ); Assert( m_pBase ); m_pNextAlloc = m_pBase; m_pCommitLimit = m_pBase + m_maxSize; m_pAllocLimit = m_pBase + m_maxSize; MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() ); return ( m_pBase != NULL ); }
void AddSurfacepropFile( const char *pFileName, IPhysicsSurfaceProps *pProps, IFileSystem *pFileSystem ) { // Load file into memory FileHandle_t file = pFileSystem->Open( pFileName, "rb", "GAME" ); if ( file ) { int len = pFileSystem->Size( file ); // read the file int nBufSize = len+1; if ( IsXbox() ) { nBufSize = AlignValue( nBufSize , 512 ); } char *buffer = (char *)stackalloc( nBufSize ); pFileSystem->ReadEx( buffer, nBufSize, len, file ); pFileSystem->Close( file ); buffer[len] = 0; pProps->ParseSurfaceData( pFileName, buffer ); // buffer is on the stack, no need to free } else { Error( "Unable to load surface prop file '%s' (referenced by manifest file '%s')\n", pFileName, SURFACEPROP_MANIFEST_FILE ); } }
void RosUmdResource::CalculateMemoryLayout( void) { switch (m_resourceDimension) { case D3D10DDIRESOURCE_BUFFER: { m_hwLayout = RosHwLayout::Linear; // TODO(bhouse) Need mapping code from resource DXGI format to hw format m_hwWidthPixels = m_mip0Info.TexelWidth; m_hwHeightPixels = m_mip0Info.TexelHeight; m_hwSizeBytes = m_mip0Info.TexelWidth * CPixel::BytesPerPixel(m_format); NT_ASSERT(this->Pitch() == m_hwSizeBytes); } break; case D3D10DDIRESOURCE_TEXTURE2D: { // get layout and alignment requirement from binding and format const auto reqs = Get2dTextureLayoutRequirements(m_bindFlags, m_format); const UINT unalignedPitch = m_mip0Info.TexelWidth * CPixel::BytesPerPixel(m_format); const UINT alignedPitch = AlignValue(unalignedPitch, reqs.PitchAlign); const UINT unalignedHeight = m_mip0Info.TexelHeight; const UINT alignedHeight = AlignValue(unalignedHeight, reqs.HeightAlign); m_hwLayout = reqs.Layout; m_hwWidthPixels = alignedPitch / CPixel::BytesPerPixel(m_format); m_hwHeightPixels = alignedHeight; m_hwSizeBytes = alignedPitch * alignedHeight; } break; case D3D10DDIRESOURCE_TEXTURE1D: case D3D10DDIRESOURCE_TEXTURE3D: case D3D10DDIRESOURCE_TEXTURECUBE: default: throw RosUmdException(DXGI_DDI_ERR_UNSUPPORTED); } }
//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CMemoryPool::AddNewBlob() { MEM_ALLOC_CREDIT_(m_pszAllocOwner); int sizeMultiplier; if( m_GrowMode == GROW_SLOW ) { sizeMultiplier = 1; } else { if ( m_GrowMode == GROW_NONE ) { // Can only have one allocation when we're in this mode if( m_NumBlobs != 0 ) { Assert( !"CMemoryPool::AddNewBlob: mode == GROW_NONE" ); return; } } // GROW_FAST and GROW_NONE use this. sizeMultiplier = m_NumBlobs + 1; } // maybe use something other than malloc? int nElements = m_BlocksPerBlob * sizeMultiplier; int blobSize = m_BlockSize * nElements; CBlob *pBlob = (CBlob*)malloc( sizeof(CBlob) - 1 + blobSize + ( m_nAlignment - 1 ) ); Assert( pBlob ); // Link it in at the end of the blob list. pBlob->m_NumBytes = blobSize; pBlob->m_pNext = &m_BlobHead; pBlob->m_pPrev = pBlob->m_pNext->m_pPrev; pBlob->m_pNext->m_pPrev = pBlob->m_pPrev->m_pNext = pBlob; // setup the free list m_pHeadOfFreeList = AlignValue( pBlob->m_Data, m_nAlignment ); Assert (m_pHeadOfFreeList); void **newBlob = (void**)m_pHeadOfFreeList; for (int j = 0; j < nElements-1; j++) { newBlob[0] = (char*)newBlob + m_BlockSize; newBlob = (void**)newBlob[0]; } // null terminate list newBlob[0] = NULL; m_NumBlobs++; }
void CXZip::DiscardPreloadedData() { if ( m_pPreloadedData ) { #ifndef _X360 free( m_pPreloadedData ); #else int nAlignedStart = AlignValue( ( m_nPreloadStart - XBOX_HDD_SECTORSIZE ) + 1, XBOX_HDD_SECTORSIZE ); byte *pReadData = (byte *)m_pPreloadedData - ( m_nPreloadStart - nAlignedStart ); free( pReadData ); #endif m_pPreloadedData = NULL; m_pPreloadDirectory = NULL; m_nRegular2PreloadEntryMapping = NULL; } }
void ReplaceRGBATexture2D(ID3D12Resource* texture12, const u8* buffer, unsigned int width, unsigned int height, unsigned int src_pitch, unsigned int level, D3D12_RESOURCE_STATES current_resource_state) { const unsigned int upload_size = AlignValue(src_pitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * height; if (!s_texture_upload_stream_buffer) { s_texture_upload_stream_buffer = new D3DStreamBuffer(4 * 1024 * 1024, 64 * 1024 * 1024, nullptr); } bool current_command_list_executed = s_texture_upload_stream_buffer->AllocateSpaceInBuffer(upload_size, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); if (current_command_list_executed) { g_renderer->SetViewport(); D3D::current_command_list->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV12(), FALSE, &FramebufferManager::GetEFBDepthTexture()->GetDSV12()); } ResourceBarrier(current_command_list, texture12, current_resource_state, D3D12_RESOURCE_STATE_COPY_DEST, level); D3D12_PLACED_SUBRESOURCE_FOOTPRINT upload_footprint = {}; u32 upload_rows = 0; u64 upload_row_size_in_bytes = 0; u64 upload_total_bytes = 0; D3D::device12->GetCopyableFootprints(&texture12->GetDesc(), level, 1, s_texture_upload_stream_buffer->GetOffsetOfCurrentAllocation(), &upload_footprint, &upload_rows, &upload_row_size_in_bytes, &upload_total_bytes); u8* dest_data = reinterpret_cast<u8*>(s_texture_upload_stream_buffer->GetCPUAddressOfCurrentAllocation()); const u8* src_data = reinterpret_cast<const u8*>(buffer); for (u32 y = 0; y < upload_rows; ++y) { memcpy( dest_data + upload_footprint.Footprint.RowPitch * y, src_data + src_pitch * y, upload_row_size_in_bytes ); } D3D::current_command_list->CopyTextureRegion(&CD3DX12_TEXTURE_COPY_LOCATION(texture12, level), 0, 0, 0, &CD3DX12_TEXTURE_COPY_LOCATION(s_texture_upload_stream_buffer->GetBuffer(), upload_footprint), nullptr); ResourceBarrier(D3D::current_command_list, texture12, D3D12_RESOURCE_STATE_COPY_DEST, current_resource_state, level); }
void CMemoryStack::FreeToAllocPoint( MemoryStackMark_t mark, bool bDecommit ) { void *pAllocPoint = m_pBase + mark; Assert( pAllocPoint >= m_pBase && pAllocPoint <= m_pNextAlloc ); if ( pAllocPoint >= m_pBase && pAllocPoint < m_pNextAlloc ) { if ( bDecommit ) { #if defined(_WIN32) unsigned char *pDecommitPoint = AlignValue( (unsigned char *)pAllocPoint, m_commitSize ); if ( pDecommitPoint < m_pBase + m_minCommit ) { pDecommitPoint = m_pBase + m_minCommit; } unsigned decommitSize = m_pCommitLimit - pDecommitPoint; if ( decommitSize > 0 ) { MemAlloc_RegisterExternalDeallocation( CMemoryStack, GetBase(), GetSize() ); VirtualFree( pDecommitPoint, decommitSize, MEM_DECOMMIT ); m_pCommitLimit = pDecommitPoint; if ( mark > 0 ) { MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() ); } } #endif } m_pNextAlloc = (unsigned char *)pAllocPoint; } }
//----------------------------------------------------------------------------- // Purpose: Constructor //----------------------------------------------------------------------------- CMemoryPool::CMemoryPool( int blockSize, int numElements, int growMode, const char *pszAllocOwner, int nAlignment ) { #ifdef _X360 if( numElements > 0 && growMode != GROW_NONE ) { numElements = 1; } #endif m_nAlignment = ( nAlignment != 0 ) ? nAlignment : 1; Assert( IsPowerOfTwo( m_nAlignment ) ); m_BlockSize = blockSize < sizeof(void*) ? sizeof(void*) : blockSize; m_BlockSize = AlignValue( m_BlockSize, m_nAlignment ); m_BlocksPerBlob = numElements; m_PeakAlloc = 0; m_GrowMode = growMode; if ( !pszAllocOwner ) { pszAllocOwner = __FILE__; } m_pszAllocOwner = pszAllocOwner; Init(); AddNewBlob(); }
bool CMemoryStack::Init( unsigned maxSize, unsigned commitSize, unsigned initialCommit, unsigned alignment ) { Assert( !m_pBase ); #ifdef _X360 m_bPhysical = false; #endif m_maxSize = maxSize; m_alignment = AlignValue( alignment, 4 ); Assert( m_alignment == alignment ); Assert( m_maxSize > 0 ); #if defined(_WIN32) if ( commitSize != 0 ) { m_commitSize = commitSize; } unsigned pageSize; #ifndef _X360 SYSTEM_INFO sysInfo; GetSystemInfo( &sysInfo ); Assert( !( sysInfo.dwPageSize & (sysInfo.dwPageSize-1)) ); pageSize = sysInfo.dwPageSize; #else pageSize = 64*1024; #endif if ( m_commitSize == 0 ) { m_commitSize = pageSize; } else { m_commitSize = AlignValue( m_commitSize, pageSize ); } m_maxSize = AlignValue( m_maxSize, m_commitSize ); Assert( m_maxSize % pageSize == 0 && m_commitSize % pageSize == 0 && m_commitSize <= m_maxSize ); m_pBase = (unsigned char *)VirtualAlloc( NULL, m_maxSize, VA_RESERVE_FLAGS, PAGE_NOACCESS ); Assert( m_pBase ); m_pCommitLimit = m_pNextAlloc = m_pBase; if ( initialCommit ) { initialCommit = AlignValue( initialCommit, m_commitSize ); Assert( initialCommit < m_maxSize ); if ( !VirtualAlloc( m_pCommitLimit, initialCommit, VA_COMMIT_FLAGS, PAGE_READWRITE ) ) return false; m_minCommit = initialCommit; m_pCommitLimit += initialCommit; MemAlloc_RegisterExternalAllocation( CMemoryStack, GetBase(), GetSize() ); } #else m_pBase = (byte *)MemAlloc_AllocAligned( m_maxSize, alignment ? alignment : 1 ); m_pNextAlloc = m_pBase; m_pCommitLimit = m_pBase + m_maxSize; #endif m_pAllocLimit = m_pBase + m_maxSize; return ( m_pBase != NULL ); }
void CViewRender::Render( vrect_t *rect ) { VPROF_BUDGET( "CViewRender::Render", "CViewRender::Render" ); m_bAllowViewAccess = true; CUtlVector< vgui::Panel * > roots; VGui_GetPanelList( roots ); // Stub out the material system if necessary. CMatStubHandler matStub; engine->EngineStats_BeginFrame(); // Assume normal vis m_bForceNoVis = false; float flViewportScale = mat_viewportscale.GetFloat(); vrect_t engineRect = *rect; // The tool framework wants to adjust the entire 3d viewport, not the per-split screen one from below ToolFramework_AdjustEngineViewport( engineRect.x, engineRect.y, engineRect.width, engineRect.height ); IterateRemoteSplitScreenViewSlots_Push( true ); FOR_EACH_VALID_SPLITSCREEN_PLAYER( hh ) { ACTIVE_SPLITSCREEN_PLAYER_GUARD_VGUI( hh ); CViewSetup &view = GetView( hh ); float engineAspectRatio = engine->GetScreenAspectRatio( view.width, view.height ); Assert( s_DbgSetupOrigin[ hh ] == view.origin ); Assert( s_DbgSetupAngles[ hh ] == view.angles ); // Using this API gives us a chance to "inset" the 3d views as needed for splitscreen int insetX, insetY; VGui_GetEngineRenderBounds( hh, view.x, view.y, view.width, view.height, insetX, insetY ); float aspectRatio = engineAspectRatio * 0.75f; // / (4/3) view.fov = ScaleFOVByWidthRatio( view.fov, aspectRatio ); view.fovViewmodel = ScaleFOVByWidthRatio( view.fovViewmodel, aspectRatio ); // Let the client mode hook stuff. GetClientMode()->PreRender( &view ); GetClientMode()->AdjustEngineViewport( view.x, view.y, view.width, view.height ); view.width *= flViewportScale; view.height *= flViewportScale; if ( IsX360() ) { // view must be compliant to resolve restrictions view.width = AlignValue( view.width, GPU_RESOLVE_ALIGNMENT ); view.height = AlignValue( view.height, GPU_RESOLVE_ALIGNMENT ); } view.m_flAspectRatio = ( engineAspectRatio > 0.0f ) ? engineAspectRatio : ( (float)view.width / (float)view.height ); int nClearFlags = VIEW_CLEAR_DEPTH | VIEW_CLEAR_STENCIL; if ( gl_clear_randomcolor.GetBool() ) { CMatRenderContextPtr pRenderContext( materials ); pRenderContext->ClearColor3ub( rand()%256, rand()%256, rand()%256 ); pRenderContext->ClearBuffers( true, false, false ); pRenderContext->Release(); } else if ( gl_clear.GetBool() ) { nClearFlags |= VIEW_CLEAR_COLOR; } // Determine if we should draw view model ( client mode override ) bool drawViewModel = GetClientMode()->ShouldDrawViewModel(); // Apply any player specific overrides C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer(); if ( pPlayer ) { // Override view model if necessary if ( !pPlayer->m_Local.m_bDrawViewmodel ) { drawViewModel = false; } } if ( cl_leveloverview.GetFloat() > 0 ) { SetUpOverView(); nClearFlags |= VIEW_CLEAR_COLOR; drawViewModel = false; } render->SetMainView( view.origin, view.angles ); int flags = (pPlayer == NULL) ? 0 : RENDERVIEW_DRAWHUD; if ( drawViewModel ) { flags |= RENDERVIEW_DRAWVIEWMODEL; } // This is the hook for per-split screen player views C_BaseEntity::PreRenderEntities( hh ); if ( ( ss_debug_draw_player.GetInt() < 0 ) || ( hh == ss_debug_draw_player.GetInt() ) ) { CViewSetup hudViewSetup; VGui_GetHudBounds( hh, hudViewSetup.x, hudViewSetup.y, hudViewSetup.width, hudViewSetup.height ); RenderView( view, hudViewSetup, nClearFlags, flags ); } GetClientMode()->PostRender(); } IterateRemoteSplitScreenViewSlots_Pop(); engine->EngineStats_EndFrame(); #if !defined( _X360 ) // Stop stubbing the material system so we can see the budget panel matStub.End(); #endif // Render the new-style embedded UI // TODO: when embedded UI will be used for HUD, we will need it to maintain // a separate screen for HUD and a separate screen stack for pause menu & main menu. // for now only render embedded UI in pause menu & main menu #if defined( GAMEUI_UISYSTEM2_ENABLED ) && 0 BaseModUI::CBaseModPanel *pBaseModPanel = BaseModUI::CBaseModPanel::GetSingletonPtr(); // render the new-style embedded UI only if base mod panel is not visible (game-hud) // otherwise base mod panel will render the embedded UI on top of video/productscreen if ( !pBaseModPanel || !pBaseModPanel->IsVisible() ) { Rect_t uiViewport; uiViewport.x = rect->x; uiViewport.y = rect->y; uiViewport.width = rect->width; uiViewport.height = rect->height; g_pGameUIGameSystem->Render( uiViewport, gpGlobals->curtime ); } #endif // Draw all of the UI stuff "fullscreen" if ( true ) // For PIXEVENT { #if PIX_ENABLE { CMatRenderContextPtr pRenderContext( materials ); PIXEVENT( pRenderContext, "VGui UI" ); } #endif CViewSetup view2d; view2d.x = rect->x; view2d.y = rect->y; view2d.width = rect->width; view2d.height = rect->height; render->Push2DView( view2d, 0, NULL, GetFrustum() ); render->VGui_Paint( PAINT_UIPANELS ); { // The engine here is trying to access CurrentView() etc. which is bogus ACTIVE_SPLITSCREEN_PLAYER_GUARD( 0 ); render->PopView( GetFrustum() ); } } m_bAllowViewAccess = false; }
unsigned __stdcall CThread::ThreadProc(LPVOID pv) { #ifdef _LINUX ThreadInit_t *pInit = (ThreadInit_t *)pv; #else std::auto_ptr<ThreadInit_t> pInit((ThreadInit_t *)pv); #endif #ifdef _X360 // Make sure all threads are consistent w.r.t floating-point math SetupFPUControlWord(); #endif CThread *pThread = pInit->pThread; g_pCurThread = pThread; g_pCurThread->m_pStackBase = AlignValue( &pThread, 4096 ); pInit->pThread->m_result = -1; try { *(pInit->pfInitSuccess) = pInit->pThread->Init(); } catch (...) { *(pInit->pfInitSuccess) = false; #ifdef _WIN32 pInit->pInitCompleteEvent->Set(); #endif throw; } bool bInitSuccess = *(pInit->pfInitSuccess); #ifdef _WIN32 pInit->pInitCompleteEvent->Set(); #endif if (!bInitSuccess) return 0; if ( !Plat_IsInDebugSession() && (pInit->pThread->m_flags & SUPPORT_STOP_PROTOCOL) ) { try { pInit->pThread->m_result = pInit->pThread->Run(); } catch (...) { } } else { pInit->pThread->m_result = pInit->pThread->Run(); } pInit->pThread->OnExit(); g_pCurThread = NULL; #ifdef _WIN32 AUTO_LOCK( pThread->m_Lock ); CloseHandle( pThread->m_hThread ); pThread->m_hThread = NULL; #endif pThread->m_threadId = 0; return pInit->pThread->m_result; }