void RemoteReleaseControlImpl::AsyncRelease() { // if another transfer is in progress, wait for completion if (m_evtReleaseImageTransferInProgress.Wait(0)) { m_evtReleaseImageTransferDone.Wait(); } m_evtReleaseImageTransferInProgress.Set(); // set save target ShutterReleaseSettings::T_enSaveTarget enSaveTarget; { LightweightMutex::LockType lock(m_mtxShutterReleaseSettings); enSaveTarget = m_shutterReleaseSettings.SaveTarget(); } PropertyAccess pa(m_hCamera); Variant val; // note: PSREC doesn't have a value for saveToBoth val.Set<prUInt16>( enSaveTarget == ShutterReleaseSettings::saveToHost ? 0x0002 : enSaveTarget == ShutterReleaseSettings::saveToCamera ? 0x0008 : 0x000a ); val.SetType(Variant::typeUInt16); pa.Set(prPTP_DEV_PROP_CAPTURE_TRANSFER_MODE, val); Timer releaseTimer; releaseTimer.Start(); // may return prWAIT_TIMEOUT_ERROR?, prINVALID_FN_CALL, prINVALID_HANDLE, prMEM_ALLOC_FAILED or @ERR prResponse err = PR_RC_Release(m_hCamera); LOG_TRACE(_T("PR_RC_Release(%08x) returned %08x\n"), m_hCamera, err); if (err != prOK) m_subjectStateEvent.Call(RemoteReleaseControl::stateEventReleaseError, 0); CheckError(_T("PR_RC_Release"), err, __FILE__, __LINE__); releaseTimer.Stop(); LOG_TRACE(_T("PR_RC_Release took %u ms\n"), unsigned(releaseTimer.Elapsed() * 1000)); // wait for event m_evtReleaseImageReady.Wait(); try { StartImageDownload(m_hReleaseImage, true); } catch (const CameraException&) { m_subjectStateEvent.Call(RemoteReleaseControl::stateEventReleaseError, 0); } }
int main() { int n_start = 25; int end_n = 1600; int n_step = 2; //Timer First Time is Wrong timer.Start(); Sleep(1000); timer.Stop(); double t = timer.Elapsed(); if (pick_accelerator()) { cout << "This Program compare GPU and CPU performance for 'N'*'N' matrix multiplication." << endl; cout << endl << "______________________________________________________" << endl << "N:\tCPU Time:\tGPU Time:\t\t" << endl << "______________________________________________________" << endl; for (int n = n_start; n <= end_n; n *= n_step) { matrix mat1(n, n), mat2(n, n); mat1.random(1, 100); mat2.random(1, 100); //GPU Time Calc timer.Start(); matrix mc_g = mat1.gpu_multiply<32>(&mat2); timer.Stop(); double gpuTime = timer.Elapsed(); //CPU Time Calc timer.Start(); matrix mc_c = mat1.cpu_multiply(&mat2); timer.Stop(); double cpuTime = timer.Elapsed(); std::cout << n<< "\t" << cpuTime << " ms\t" << gpuTime << " ms\t" << endl << endl; } } getch(); return 0; }
int testAlgo(bool (*algoFunc)(std::vector<int>&, int), std::vector<double>& prevTimes, int algoIndex, const std::vector<int>& v, int x) { Timer t; std::vector<int> vTmp(v); t.Reset(); bool res = algoFunc(vTmp, x); int time = t.Elapsed().count(); // if(prevTimes[algoIndex] == 0) { // prevTimes[algoIndex] = time; // } return time; }
void test2(void* scratch, uint scratchSize) { StackAlloc alloc(scratch, ((u8*)scratch)+scratchSize); Scope a(alloc, ""); Timer* timer = eiNewInterface(a, Timer)(); eiInfo(AllocProfile, "########Standard List########"); for(int itrSample = 0; itrSample < 3; itrSample++) { double begin = timer->Elapsed(); for(int itrItr = 0; itrItr < 10; itrItr++) { std::list<int> lst; for(int itr = 0; itr < 50; itr++) { lst.push_front(itr); } } double end = timer->Elapsed(); eiInfo(AllocProfile, "Sample %d Time: %f ms", itrSample, float(end - begin)*1000.0f); } struct ListItem { int data; ListItem* next; }; eiInfo(AllocProfile, "########Malloc/Free########"); for(int itrSample = 0; itrSample < 3; itrSample++) { double begin = timer->Elapsed(); for(int itrItr = 0; itrItr < 10; itrItr++) { ListItem* lst = 0; for(int itr = 0; itr < 50; itr++) { ListItem* push = (ListItem*)Malloc(sizeof(ListItem)); push->next = lst; push->data = itr; lst = push; } while(lst) { ListItem* dead = lst; lst = dead->next; Free(dead); } } double end = timer->Elapsed(); eiInfo(AllocProfile, "Sample %d Time: %f ms", itrSample, float(end - begin)*1000.0f); } eiInfo(AllocProfile, "########New/Delete########"); for(int itrSample = 0; itrSample < 3; itrSample++) { double begin = timer->Elapsed(); for(int itrItr = 0; itrItr < 10; itrItr++) { ListItem* lst = 0; for(int itr = 0; itr < 50; itr++) { ListItem* push = new ListItem; push->next = lst; push->data = itr; lst = push; } while(lst) { ListItem* dead = lst; lst = dead->next; delete dead; } } double end = timer->Elapsed(); eiInfo(AllocProfile, "Sample %d Time: %f ms", itrSample, float(end - begin)*1000.0f); } eiInfo(AllocProfile, "########Pool########"); Pool<ListItem, false> pool(a,10000); for(int itrSample = 0; itrSample < 3; itrSample++) { double begin = timer->Elapsed(); for(int itrItr = 0; itrItr < 10; itrItr++) { ListItem* lst = 0; for(int itr = 0; itr < 50; itr++) { ListItem* push = pool.Alloc();//(ListItem*)lca.newObject(sizeof(ListItem)); push->next = lst; push->data = itr; lst = push; } while(lst) { ListItem* dead = lst; lst = dead->next; pool.Release(dead);//lca.deleteObject(dead); } } double end = timer->Elapsed(); eiInfo(AllocProfile, "Sample %d Time: %f ms", itrSample, float(end - begin)*1000.0f); } eiInfo(AllocProfile, "########eiNew########"); for(int itrSample = 0; itrSample < 3; itrSample++) { double begin = timer->Elapsed(); for(int itrItr = 0; itrItr < 10; itrItr++) { Scope t(a,""); ListItem* lst = 0; for(int itr = 0; itr < 50; itr++) { ListItem* push = eiNew(t, ListItem); push->next = lst; push->data = itr; lst = push; } } double end = timer->Elapsed(); eiInfo(AllocProfile, "Sample %d Time: %f ms", itrSample, float(end - begin)*1000.0f); } eiInfo(AllocProfile, "########eiAlloc########"); for(int itrSample = 0; itrSample < 3; itrSample++) { double begin = timer->Elapsed(); for(int itrItr = 0; itrItr < 10; itrItr++) { Scope t(a,""); ListItem* lst = 0; for(int itr = 0; itr < 50; itr++) { ListItem* push = eiAlloc(t, ListItem); push->next = lst; push->data = itr; lst = push; } } double end = timer->Elapsed(); eiInfo(AllocProfile, "Sample %d Time: %f ms", itrSample, float(end - begin)*1000.0f); char* data = (char*)Malloc(640*1024);//640KiB should be enough for anybody int& fooVec = *new(data) int; data += sizeof(fooVec); } }
int main(int argc, char* argv[]) { // Initialize logger logFile.open("engine_test.log"); logFilePath = std::experimental::filesystem::current_path(); logFilePath /= "engine_test.log"; cout << "Log files can be found at:\n "; cout << " " << logFilePath << endl; if (logFile.is_open()) { logger.OpenStream(&logFile); } else { logger.OpenStream(&std::cout); } // Set exception terminate handler std::set_terminate(OnTerminate); // Create the window itself Window window; window.SetTitle("QC Simulator"); window.SetSize({ 960, 640 }); // Create GraphicsEngine systemLogStream.Event("Initializing Graphics Engine..."); std::unique_ptr<IGxapiManager> gxapiMgr; std::unique_ptr<IGraphicsApi, ReportDeleter> gxapi; std::unique_ptr<GraphicsEngine> engine; std::unique_ptr<QCWorld> qcWorld; std::unique_ptr<InputHandler> inputHandler; std::unique_ptr<Input> joyInput; std::unique_ptr<Input> keyboardInput; try { // Create manager systemLogStream.Event("Creating GxApi Manager..."); gxapiMgr.reset(new GxapiManager()); auto adapters = gxapiMgr->EnumerateAdapters(); std::string cardList; for (auto adapter : adapters) { cardList += "\n"; cardList += adapter.name; } systemLogStream.Event("Available graphics cards:" + cardList); // Create graphics api int device = 0; if (argc == 3 && argv[1] == std::string("--device") && isdigit(argv[2][0])) { device = argv[2][0] - '0'; // works for single digits, good enough, lol } systemLogStream.Event("Creating GraphicsApi..."); gxapi.reset(gxapiMgr->CreateGraphicsApi(adapters[device].adapterId)); std::stringstream ss; ss << "Using graphics card: " << adapters[device].name; systemLogStream.Event(ss.str()); // Create graphics engine systemLogStream.Event("Creating Graphics Engine..."); GraphicsEngineDesc desc; desc.fullScreen = false; desc.graphicsApi = gxapi.get(); desc.gxapiManager = gxapiMgr.get(); desc.width = window.GetClientSize().x; desc.height = window.GetClientSize().y; desc.targetWindow = window.GetNativeHandle(); desc.logger = &logger; engine.reset(new GraphicsEngine(desc)); // Load graphics pipeline std::string pipelineFileName = SelectPipeline(gxapi.get()); std::string exeDir = System::GetExecutableDir(); std::ifstream pipelineFile(INL_PIPELINE_DIRECTORY "\\" + pipelineFileName); if (!pipelineFile.is_open()) { throw FileNotFoundException("Failed to open pipeline JSON."); } std::string pipelineDesc((std::istreambuf_iterator<char>(pipelineFile)), std::istreambuf_iterator<char>()); engine->LoadPipeline(pipelineDesc); // Create mini world qcWorld.reset(new QCWorld(engine.get())); // Create input handling inputHandler = std::make_unique<InputHandler>(qcWorld.get()); window.OnResize += Delegate<void(ResizeEvent)>{ &InputHandler::OnResize, inputHandler.get() }; auto joysticks = Input::GetDeviceList(eInputSourceType::JOYSTICK); if (!joysticks.empty()) { joyInput = std::make_unique<Input>(joysticks.front().id); joyInput->SetQueueMode(eInputQueueMode::QUEUED); joyInput->OnJoystickMove += Delegate<void(JoystickMoveEvent)>{ &InputHandler::OnJoystickMove, inputHandler.get() }; } auto keyboards = Input::GetDeviceList(eInputSourceType::KEYBOARD); if (!keyboards.empty()) { keyboardInput = std::make_unique<Input>(keyboards.front().id); keyboardInput->SetQueueMode(eInputQueueMode::QUEUED); keyboardInput->OnKeyboard += Delegate<void(KeyboardEvent)>{ &InputHandler::OnKey, inputHandler.get() }; } window.OnResize += [&engine, &qcWorld](ResizeEvent evt) { engine->SetScreenSize(evt.clientSize.x, evt.clientSize.y); qcWorld->ScreenSizeChanged(evt.clientSize.x, evt.clientSize.y); }; logger.Flush(); } catch (Exception& ex) { errorMessage = std::string("Error creating GraphicsEngine: ") + ex.what() + "\n" + ex.StackTraceStr(); systemLogStream.Event(errorMessage); logger.Flush(); } catch (std::exception& ex) { errorMessage = std::string("Error creating GraphicsEngine: ") + ex.what(); systemLogStream.Event(errorMessage); logger.Flush(); } if (!qcWorld) { return 0; } // Main rendering loop Timer timer; timer.Start(); double frameTime = 0.05, frameRateUpdate = 0; std::vector<double> frameTimeHistory; float avgFps = 0; auto CaptionHandler = [&window](Vec2i cursorPos) { Vec2i size = window.GetSize(); RectI rc; rc.top = 5; rc.bottom = 50; rc.right = size.x - 5; rc.left = size.x - 50; if (rc.IsPointInside(cursorPos)) return eWindowCaptionButton::CLOSE; rc.Move({ -50, 0 }); if (rc.IsPointInside(cursorPos)) return eWindowCaptionButton::MAXIMIZE; rc.Move({ -50, 0 }); if (rc.IsPointInside(cursorPos)) return eWindowCaptionButton::MINIMIZE; if (cursorPos.y < 55) { return eWindowCaptionButton::BAR; } return eWindowCaptionButton::NONE; }; //window.SetBorderless(true); //window.SetCaptionButtonHandler(CaptionHandler); while (!window.IsClosed()) { inputHandler->SetFocused(window.IsFocused()); window.CallEvents(); if (joyInput) { joyInput->CallEvents(); } if (keyboardInput) { keyboardInput->CallEvents(); } try { // Update world qcWorld->UpdateWorld(frameTime); qcWorld->RenderWorld(frameTime); // Calculate elapsed time for frame. frameTime = timer.Elapsed(); timer.Reset(); // Calculate average framerate frameRateUpdate += frameTime; if (frameRateUpdate > 0.5) { frameRateUpdate = 0; double avgFrameTime = 0.0; for (auto v : frameTimeHistory) { avgFrameTime += v; } avgFrameTime /= frameTimeHistory.size(); avgFps = 1 / avgFrameTime; frameTimeHistory.clear(); } frameTimeHistory.push_back(frameTime); // Set info text as window title unsigned width, height; engine->GetScreenSize(width, height); std::string title = "Graphics Engine Test | " + std::to_string(width) + "x" + std::to_string(height) + " | FPS=" + std::to_string((int)avgFps); window.SetTitle(title); } catch (Exception& ex) { std::stringstream trace; trace << "Graphics engine error:" << ex.what() << "\n"; ex.PrintStackTrace(trace); systemLogStream.Event(trace.str()); PostQuitMessage(0); } catch (std::exception& ex) { systemLogStream.Event(std::string("Graphics engine error: ") + ex.what()); logger.Flush(); PostQuitMessage(0); } } cout << "Shutting down." << endl; return 0; }
void GameLoop(HWND hwnd) { render_context->ClearFrameBuffer(ARGB_BLACK); //************ if (timer.Elapsed() - updateTimer > updateTick) { int index; // looping var static int curr_texture = 7; // currently active texture static int curr_lightmap = 1; static float scalef = 0.5; // texture scaling factor // copy texture into temp display texture for rendering and scaling //temp_text.CopyBitmap(0, 0, textures[curr_texture], 0, 0, TEXTSIZE, TEXTSIZE); /////////////////////////////////////////// // our little image processing algorithm :) //Cmodulated = s*C1 = (s*r1, s*g1, s*b1) //////////////////////////////////////// // draw texture //temp_text.DrawBitmap24(render_context, WINDOW_WIDTH, 0); //DrawBottomTri(render_context, 200, 0, 100, 400, 400, 400, ARGB_RED, WINDOW_WIDTH); //DrawTopTri(render_context, 0, 200, 400, 200, 200, 400, ARGB_RED, WINDOW_WIDTH); DrawTriangle(render_context, 0, 200, 400, 250, 200, 400, ARGB_RED, WINDOW_WIDTH); //textures[curr_texture].DrawBitmap24(render_context, WINDOW_WIDTH, 0); //lightmaps[curr_lightmap].DrawBitmap24(render_context, WINDOW_WIDTH, 0); // test if user wants to change texture if (KEY_DOWN(VK_RIGHT)) { if (++curr_texture > NUM_TEXT - 1) curr_texture = NUM_TEXT - 1; } // end if if (KEY_DOWN(VK_LEFT)) { if (--curr_texture < 0) curr_texture = 0; } // end if // is user changing scaling factor if (keyboard_state[DIK_UP]) { scalef += .01; if (scalef > 10) scalef = 10; } // end if if (keyboard_state[DIK_DOWN]) { scalef -= .01; if (scalef < 0) scalef = 0; } // end if /* for (int x = 0; x < bitmap24bit.bitmapinfoheader.biWidth; x++) { for (int y = 0; y < bitmap24bit.bitmapinfoheader.biHeight; y++) { unsigned char B = *(bitmap24bit.buffer + y * 640 * 3 + x * 3 + 0); unsigned char G = *(bitmap24bit.buffer + y * 640 * 3 + x * 3 + 1); unsigned char R = *(bitmap24bit.buffer + y * 640 * 3 + x * 3 + 2); render_context->frame_buffer[x + y * 640] = ARGB(0, (int)R, (int)G, (int)B); } } */ window_hdc = GetDC(hwnd); SelectObject(buffer_hdc, now_bitmap); BitBlt(window_hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, buffer_hdc, 0, 0, SRCCOPY); ReleaseDC(hwnd, window_hdc); } frames++; if (timer.Elapsed() - elapsed > 1.0f) { elapsed += 1.0f; cout << frames << "fps" << endl; frames = 0; } // ************** }
void GameLoop(HWND hwnd) { render_context->ClearFrameBuffer(); static Mat4f mrot; // general rotation matrix static float x_ang = 0.0f, y_ang = 0.2f, z_ang = 0.0f; //每秒执行60次部分 if (timer.Elapsed() - updateTimer > updateTick) { updateTimer = updateTimer + updateTick; // reset angles //x_ang = 0.0f; if (KEY_DOWN(VK_UP)) obj.world_pos.z = obj.world_pos.z - 0.5f; if (KEY_DOWN(VK_DOWN)) obj.world_pos.z = obj.world_pos.z + 0.5f; /*if (KEY_DOWN(VK_LEFT)) x_ang = 0.2f; if (KEY_DOWN(VK_RIGHT)) x_ang = -0.2f;*/ } //尽快执行部分 { // 重设剔除、裁剪、隐面标志位 obj.Reset(); // 产生绕三轴旋转矩阵 mrot = Mat4f::Rotation(x_ang, y_ang, z_ang); // 直接对局部坐标变换,存入局部坐标 obj.Transform(mrot, TRANSFORM_LOCAL_ONLY, 1); // 模型坐标到世界坐标变换 obj.ModelToWorld(); // 建立相机变换矩阵,此例中一般为单位矩阵 //cam.BuildMatrixEuler(CAM_ROT_SEQ_ZYX); // 执行隐面剔除 obj.RemoveBackFaces(cam); // 世界坐标到相机坐标变换 obj.WorldToCamera(cam); //获得三角形的法向量 obj.GetTriangleNormal(); // 相机坐标投影变换 obj.CameraToPerspective(cam); obj.ClipUDLR(); // 投影坐标到屏幕坐标 obj.PerspectiveToScreen(cam); // 光照纹理方式绘制 obj.DrawPlane(render_context, WINDOW_WIDTH); window_hdc = GetDC(hwnd); SelectObject(buffer_hdc, now_bitmap); BitBlt(window_hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, buffer_hdc, 0, 0, SRCCOPY); ReleaseDC(hwnd, window_hdc); } //每秒执行1次部分 frames++; if (timer.Elapsed() - elapsed > 1.0f) { elapsed += 1.0f; cout << frames << "fps" << endl; frames = 0; } }