void Renderer::Exit() { if (0 != this->mhThread) { mbQuitFlag = true; REPORTERROR("Request thread shutdown"); WaitForSingleObject(this->mhThread, INFINITE); for (;;) { ::DWORD exitCode; ::BOOL result = ::GetExitCodeThread(this->mhThread, &exitCode); if (0 == result) { ::DWORD errorCode = ::GetLastError(); REPORTWIN32ERROR("Unable to exit the rendering thread; error %d, '%s'", errorCode); return; } else if (STILL_ACTIVE == result) { REPORTERROR("Thread is still alive"); continue; } else { REPORTERROR1("Thread exit code is %d", result); ::CloseHandle(this->mhThread); this->mhThread = 0; break; } } } }
Application::Application(int UNUSEDARG(argc), char *UNUSEDARG(argv)[]) : mpRenderer(0), mhWin32Instance(0), mi32ExitCode(0) { if (0 != spInstance) { REPORTERROR("There is already an instance of the application running"); return; } spInstance = this; }
// route to the worker thread void Renderer::threadFunction(void *param) { Renderer *lpRenderer = static_cast<Renderer *>(param); if (!lpRenderer->CreateDevice()) { return; } lpRenderer->runThread(); lpRenderer->DestroyDevice(); // terminate rendering thread _endthread(); REPORTERROR("Render thread has terminated"); }
void flightTask(void) { static time_t last = 100; // Don't begin immediately if ((getSystemTime() - last) >= QUADDELAY) { last = getSystemTime(); Error e = orientationTask(); REPORTERROR(e); if (state & STATE_PID) { pidTask(); } else { pidOutput[0] = pidOutput[1] = 0; } setTask(); motorTask(); uint32_t diff = getSystemTime() - last; if (++countFlightTask >= QUADFREQ) { countFlightTask = 1; sumFlightTask = diff; } else { sumFlightTask += diff; } } }
// rendering loop void Renderer::runThread() { ::HRESULT lResult; ::IDirect3DDevice9 *lpD3DDevice = static_cast<::IDirect3DDevice9 *>(this->mpD3DDevice); ::IDirect3DSurface9 *lpDefaultRenderTarget = 0; lResult = lpD3DDevice->GetRenderTarget(0, &lpDefaultRenderTarget); if (FAILED(lResult)) { REPORTERROR2("GetRenderTarget 0 failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lpDefaultRenderTarget->Release(); ::IDirect3DSurface9 *lpDefaultDepthStencilTarget = 0; lResult = lpD3DDevice->GetDepthStencilSurface(&lpDefaultDepthStencilTarget); if (0 != lpDefaultDepthStencilTarget) { lpDefaultDepthStencilTarget->Release(); } if (!this->CreateImmediateModeVertexBuffer()) { REPORTERROR("Unable to create immediate mode vertex buffer"); return; } ::D3DVERTEXELEMENT9 *laVertexElements = Vertex::GetVertexElements(); ::IDirect3DVertexDeclaration9 *lpVertexDeclaration = 0; lResult = lpD3DDevice->CreateVertexDeclaration(laVertexElements, &lpVertexDeclaration); if (FAILED(lResult)) { REPORTERROR2("CreateVertexDeclaration failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } if (!this->CreateShaders()) { REPORTERROR("Unable to create shaders"); return; } this->CreateTimerQuery(); // rendering loop while(!this->mbQuitFlag) { ::SwitchToThread(); this->StartTimerQuery(); lResult = lpD3DDevice->BeginScene(); if (FAILED(lResult)) { REPORTERROR2("BeginScene failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } ::D3DVIEWPORT9 lViewport; lViewport.X = 0; lViewport.Y = 0; lViewport.Width = 512; lViewport.Height = 512; lViewport.MinZ = 0; lViewport.MaxZ = 1; lResult = lpD3DDevice->SetViewport(&lViewport); if (FAILED(lResult)) { REPORTERROR2("SetViewport failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lResult = lpD3DDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 0, 0); if (FAILED(lResult)) { REPORTERROR2("Clear 0 failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } const unsigned int lu32VertexCount = 3u; unsigned int lu32Offset; Vertex *lpVertexData = static_cast<Vertex *>(this->BeginImmediateMode(lu32VertexCount * sizeof(Vertex), lu32Offset)); lpVertexData->mfPosition[0] = -0.5f; lpVertexData->mfPosition[1] = -0.5f; lpVertexData->mfColor[0] = 1.0f; lpVertexData->mfColor[1] = 0; lpVertexData->mfColor[2] = 0; ++lpVertexData; lpVertexData->mfPosition[0] = 0.5f; lpVertexData->mfPosition[1] = -0.5f; lpVertexData->mfColor[0] = 0; lpVertexData->mfColor[1] = 1.0f; lpVertexData->mfColor[2] = 0; ++lpVertexData; lpVertexData->mfPosition[0] = 0.5f; lpVertexData->mfPosition[1] = 0.5f; lpVertexData->mfColor[0] = 0; lpVertexData->mfColor[1] = 0; lpVertexData->mfColor[2] = 1.0f; ++lpVertexData; this->EndImmediateMode(); lResult = lpD3DDevice->SetVertexDeclaration(lpVertexDeclaration); if (FAILED(lResult)) { REPORTERROR2("SetVertexDeclaration failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } ::IDirect3DVertexBuffer9 *lpVertexBuffer = static_cast<::IDirect3DVertexBuffer9 *>(this->mpImmediateModeVB); lResult = lpD3DDevice->SetStreamSource(0, lpVertexBuffer, lu32Offset, sizeof(Vertex)); if (FAILED(lResult)) { REPORTERROR2("SetStreamSource failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lResult = lpD3DDevice->SetRenderState(::D3DRS_CULLMODE, ::D3DCULL_NONE); if (FAILED(lResult)) { REPORTERROR2("SetRenderState D3DRS_CULLMODE failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lResult = lpD3DDevice->SetRenderState(::D3DRS_ZFUNC, ::D3DCMP_ALWAYS); if (FAILED(lResult)) { REPORTERROR2("SetRenderState D3DRS_ZFUNC failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lResult = lpD3DDevice->SetRenderState(::D3DRS_ZWRITEENABLE, FALSE); if (FAILED(lResult)) { REPORTERROR2("SetRenderState D3DRS_ZWRITEENABLE failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lResult = lpD3DDevice->SetRenderState(::D3DRS_LIGHTING, FALSE); if (FAILED(lResult)) { REPORTERROR2("SetRenderState D3DRS_LIGHTING failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lResult = lpD3DDevice->SetRenderState(::D3DRS_ALPHABLENDENABLE, FALSE); if (FAILED(lResult)) { REPORTERROR2("SetRenderState D3DRS_ALPHABLENDENABLE failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } ::IDirect3DVertexShader9 *lpVertexShader = static_cast<::IDirect3DVertexShader9 *>(this->mpVertexShader); lpD3DDevice->SetVertexShader(lpVertexShader); if (FAILED(lResult)) { REPORTERROR2("SetVertexShader failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } ::IDirect3DPixelShader9 *lpPixelShader = static_cast<::IDirect3DPixelShader9 *>(this->mpPixelShader); lpD3DDevice->SetPixelShader(lpPixelShader); if (FAILED(lResult)) { REPORTERROR2("SetPixelShader failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } const unsigned int luNumTriangles = lu32VertexCount / 3u; lResult = lpD3DDevice->DrawPrimitive(::D3DPT_TRIANGLELIST, 0, luNumTriangles); if (FAILED(lResult)) { REPORTERROR2("DrawPrimitive failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } lResult = lpD3DDevice->EndScene(); if (FAILED(lResult)) { REPORTERROR2("EndScene failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } this->EndTimerQuery(); // Note: this involves a synchronous wait REPORTERROR1("GPU time = %.4f ms", this->GetMillisecondsElapsed()); lResult = lpD3DDevice->Present(0, 0, 0, 0); if (FAILED(lResult)) { REPORTERROR2("Present failed, HR=0x%x '%s'", lResult, getErrorMessage(lResult)); return; } } REPORTERROR("Begin shutting down render thread"); this->DestroyTimerQuery(); this->DestroyShaders(); if (0 != lpVertexDeclaration) { lpVertexDeclaration->Release(); } this->DestroyImmediateModeVertexBuffer(); }
bool Renderer::CreateDevice() { ::IDirect3D9 *lpD3D = ::Direct3DCreate9(D3D_SDK_VERSION); if (0 == lpD3D) { REPORTERROR("Direct3D9 object creation failed"); return false; } ::HRESULT lResult; ::D3DCAPS9 lCaps; lResult = lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, ::D3DDEVTYPE_HAL, &lCaps); if (FAILED(lResult)) { REPORTERROR2("GetDeviceCaps failed: 0x%x '%s'", lResult, getErrorMessage(lResult)); return false; } ::DWORD lu32BehaviourFlags = 0; if (D3DDEVCAPS_HWTRANSFORMANDLIGHT == (lCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)) { lu32BehaviourFlags |= D3DCREATE_PUREDEVICE; lu32BehaviourFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING; } else { lu32BehaviourFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; } ::D3DPRESENT_PARAMETERS lPresentParams; ::ZeroMemory(&lPresentParams, sizeof(lPresentParams)); lPresentParams.hDeviceWindow = static_cast<HWND>(this->mhWindowHandle); lPresentParams.BackBufferCount = 1; lPresentParams.BackBufferWidth = 512; lPresentParams.BackBufferHeight = 512; lPresentParams.BackBufferFormat = ::D3DFMT_X8R8G8B8; lPresentParams.EnableAutoDepthStencil = false; //lPresentParams.AutoDepthStencilFormat = ::D3DFMT_D24S8; lPresentParams.Windowed = true; lPresentParams.SwapEffect = ::D3DSWAPEFFECT_COPY; lPresentParams.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; ::IDirect3DDevice9 *lpDevice = 0; lResult = lpD3D->CreateDevice( D3DADAPTER_DEFAULT, ::D3DDEVTYPE_HAL, 0, lu32BehaviourFlags, &lPresentParams, &lpDevice); if (FAILED(lResult)) { REPORTERROR2("Direct3D9 creation failed: 0x%x '%s'", lResult, getErrorMessage(lResult)); return false; } this->mpD3D = lpD3D; this->mpD3DDevice = lpDevice; return true; }
int spe_record__record(struct spe_record__conf *conf) { signal(SIGCHLD, spe_record__handler); print_spe_record__conf(conf); spe__prepare_workload(conf->wl_args); /* * Set affinity masks */ check_return(sched_setaffinity(0, sizeof(cpu_set_t), &conf->record_aff_mask), "Failed to set record process affinity mask, Error: %d\n", errno); check_return(sched_setaffinity(wl_state.pid, sizeof(cpu_set_t), &conf->load_aff_mask), "Failed to set load process affinity mask, Error: %d\n", errno); sc_init(conf, pe_handler); check_return(initialize_groups(&g_groups, conf, wl_state.pid), "Error initializing groups.\n"); check_return(open_outputs(&g_groups, conf->output_preffix), "Error opening outputs.\n"); check_return(open_pe_group(&g_groups), "Error opening groups.\n"); printf("Ready to go.\n"); check_return(start_pe_group(&g_groups), "Error starting groups.\n"); check_return(spe__start_workload(), "Error start_workload.\n"); int awaken = 0, bytes_curr = 0; int bytes_written = 0; //TODO: How to improve efficiency of polling? int nr_signal_to_wait = 20; int i; while (wl_state.wait_child) { for (i = 0; wl_state.wait_child && i < nr_signal_to_wait; ++i) pause(); ++awaken; bytes_curr = perf_mmap_read_all(&g_groups); if (bytes_curr < 0) { REPORTERROR("Error reading mmap.\n"); } else { bytes_written += bytes_curr; } } perf_mmap_read_all(&g_groups); check_return(stop_pe_group(&g_groups), "Error stopping groups.\n"); printf("End. Written %d bytes. Awaken %d times\n", bytes_written, awaken); check_return(stop_pe_group(&g_groups), "Error stopping groups\n"); check_return(print_summaries(&g_groups, conf->output_preffix), "Error printing outputs.\n"); check_return(close_outputs(&g_groups), "Error closing outputs.\n"); check_return(close_pe_group(&g_groups), "Error closing groups\n"); return 0; }
static ::LRESULT CALLBACK WindowProc(::HWND hWnd, ::UINT Msg, ::WPARAM wParam, ::LPARAM lParam) { Application *lpApplication = Application::GetInstance(); switch (Msg) { case WM_CREATE: { Renderer *renderer = new Renderer(hWnd); renderer->Initialize(); lpApplication->SetRenderer(renderer); int li32SocketVersion = MAKEWORD(1, 1); Networking::Socket::Initialize(li32SocketVersion); } break; case WM_DESTROY: { Networking::Socket::Release(); Renderer *lpRenderer = lpApplication->GetRenderer(); if (lpRenderer != 0) { lpRenderer->Release(); delete lpRenderer; lpApplication->SetRenderer(0); } } break; case WM_CLOSE: { Renderer *lpRenderer = lpApplication->GetRenderer(); if (0 != lpRenderer) { lpRenderer->Exit(); } REPORTERROR("Sending quit message"); ::PostQuitMessage(0); } return 0; case WM_DROPFILES: { ::HDROP hDrop = reinterpret_cast< ::HDROP>(wParam); ::UINT luFilesDropped = ::DragQueryFile(hDrop, 0xFFFFFFFF, 0, 0); REPORTERROR1("There were %d files dropped", luFilesDropped); if (1 == luFilesDropped) { ::UINT luBufferSize = ::DragQueryFile(hDrop, 0, 0, 0); luBufferSize += 1; char *lpBuffer = new char[luBufferSize]; ::UINT luResult = ::DragQueryFile(hDrop, 0, lpBuffer, luBufferSize); if (0 == luResult) { ::DWORD luErrorCode = ::GetLastError(); REPORTWIN32ERROR("DragQueryFile failed", luErrorCode); } struct addrinfo hints; ::ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; #if 1 Networking::Socket mySocket(AF_INET, SOCK_STREAM, IPPROTO_TCP); struct hostent *host = ::gethostbyname("localhost"); ::SOCKADDR_IN SockAddr; SockAddr.sin_port = ::htons(8888); SockAddr.sin_family = AF_INET; SockAddr.sin_addr.s_addr = *((unsigned long *)host->h_addr); bool lbResult = mySocket.Connect(&SockAddr, sizeof(SockAddr)); #else // Resolve the server address and port struct addrinfo *result; int iResult = ::getaddrinfo("127.0.0.1", DEFAULT_PORT, &hints, &result); if ( iResult != 0 ) { REPORTWIN32MODULEERROR(::GetModuleHandle("WS2_32.dll"), "Failed to getaddrinfo, error %d, '%s'", iResult); } //::hostent *lpLocalHost = ::gethostbyname("localhost"); Networking::Socket mySocket(result->ai_family, result->ai_socktype, result->ai_protocol); bool lbResult = mySocket.Connect(result->ai_addr, result->ai_addrlen); ::freeaddrinfo(result); #endif if (lbResult) { lbResult = mySocket.Send(lpBuffer, luBufferSize); } if (lbResult) { TextureHeader textureHeader; int liSize; lbResult = mySocket.Receive(&textureHeader, liSize); if (lbResult) { REPORTERROR3("Texture %dx%d of size %d", textureHeader.mu32Width, textureHeader.mu32Height, textureHeader.mu32TotalTextureDataSize); unsigned char *lpTextureData = new unsigned char[textureHeader.mu32TotalTextureDataSize]; lbResult = mySocket.Receive(lpTextureData, liSize); textureHeader.mpData = lpTextureData; Renderer *lpRenderer = lpApplication->GetRenderer(); if (0 != lpRenderer) { lpRenderer->UploadTexture(textureHeader); } delete [] lpTextureData; } } delete [] lpBuffer; } } break; } return ::DefWindowProc(hWnd, Msg, wParam, lParam); }