void Initialize(int nthreads) { BASIS_ASSERT(ThreadCount == 0); ThreadCount = (nthreads <= 0) ? std::thread::hardware_concurrency() : nthreads; GlobalSharedTaskCount = 0; SchedulerList = new scheduler_data[ThreadCount]; for (unsigned i=0; i<ThreadCount; i++) { SchedulerList[i].exitRequested = false; SchedulerList[i].threadId = i; SchedulerList[i].isActive = false; SchedulerList[i].isSignaled = false; } for (unsigned i=1; i<ThreadCount; i++) { SchedulerList[i].thread = std::thread([=]() -> void { Scheduler = SchedulerList + i; TACO_PROFILER_EMIT(profiler::event_object::thread, profiler::event_action::start); FiberInitializeThread(); fiber * f = FiberCreate(&WorkerLoop); Scheduler->isActive = true; FiberInvoke(f); Scheduler->isActive = false; TACO_PROFILER_EMIT(profiler::event_object::thread, profiler::event_action::complete); ShutdownScheduler(); FiberShutdownThread(); }); } Scheduler = SchedulerList; FiberInitializeThread(); }
void Shutdown() { // Must be called from main thread and main thread must not // be in the scheduler loop BASIS_ASSERT(Scheduler == SchedulerList && !Scheduler->isActive); for (unsigned i=1; i<ThreadCount; i++) { SchedulerList[i].exitRequested = true; SignalScheduler(SchedulerList + i); } for (unsigned i=1; i<ThreadCount; i++) { SchedulerList[i].thread.join(); } blocking_thread * thread; while (BlockingThreads.pop_front(thread)) { { std::unique_lock<std::mutex> lock(thread->workMutex); thread->exitRequested = true; } thread->workCondition.notify_one(); thread->thread.join(); delete thread; } ShutdownScheduler(); delete [] SchedulerList; SchedulerList = nullptr; ThreadCount = 0; BlockingThreadCount = 0; FiberShutdownThread(); }
i32 RunApplicationMainLoop() { SDL_Event event; bool active = true; while (active) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: active = false; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) { active = false; break; } else { GApplicationKeyDownFunction(event.key.keysym.sym); } break; case SDL_TEXTINPUT: { GApplicationTextInputFunction((wchar_t*)event.text.text); } break; case SDL_MOUSEWHEEL: { GApplicationMouseWheelFunction(event.wheel.y); } break; case SDL_DROPFILE: { const char* path = event.drop.file; GApplicationFileDropFunction(path); SDL_free((void*)path); } break; case SDL_WINDOWEVENT: { switch (event.window.event) { case SDL_WINDOWEVENT_RESIZED: { GDisplaySettings.resolution.x = event.window.data1; GDisplaySettings.resolution.y = event.window.data2; WaitForCompletion(); ResizeSwapChain(GDisplaySettings.resolution.x, GDisplaySettings.resolution.y); GApplicationWindowResizeFunction(); } break; } } break; } } static INT64 PreviousTime = 0; static INT64 TicksPerSecond = 0; INT64 currentTime; Verify(QueryPerformanceCounter((LARGE_INTEGER *)¤tTime)); double DeltaTime = (double)(currentTime - PreviousTime) / (double)TicksPerSecond; if (TicksPerSecond == 0) { Verify(QueryPerformanceFrequency((LARGE_INTEGER *)&TicksPerSecond)); DeltaTime = 1. / 60.; } PreviousTime = currentTime; float fDeltaTime = (float)DeltaTime; ImGuiIO& io = ImGui::GetIO(); RECT rect; GetClientRect(GDisplaySettings.hwnd, &rect); io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); io.DeltaTime = (float)DeltaTime; io.MouseDrawCursor = true; SDL_ShowCursor(SDL_DISABLE); io.KeyShift = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_LSHIFT]; io.KeyCtrl = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_LCTRL]; io.KeyAlt = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_LALT]; io.KeysDown[SDL_SCANCODE_TAB] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_TAB]; io.KeysDown[SDL_SCANCODE_LEFT] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_LEFT]; io.KeysDown[SDL_SCANCODE_RIGHT] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_RIGHT]; io.KeysDown[SDL_SCANCODE_UP] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_UP]; io.KeysDown[SDL_SCANCODE_DOWN] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_DOWN]; io.KeysDown[SDL_SCANCODE_HOME] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_HOME]; io.KeysDown[SDL_SCANCODE_END] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_END]; io.KeysDown[SDL_SCANCODE_DELETE] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_DELETE]; io.KeysDown[SDL_SCANCODE_RETURN] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_RETURN]; io.KeysDown[SDL_SCANCODE_ESCAPE] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_ESCAPE]; io.KeysDown[SDL_SCANCODE_BACKSPACE] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_BACKSPACE]; io.KeysDown[SDL_SCANCODE_A] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_A]; io.KeysDown[SDL_SCANCODE_C] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_C]; io.KeysDown[SDL_SCANCODE_V] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_V]; io.KeysDown[SDL_SCANCODE_X] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_X]; io.KeysDown[SDL_SCANCODE_Y] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_Y]; io.KeysDown[SDL_SCANCODE_Z] = !!SDL_GetKeyboardState(NULL)[SDL_SCANCODE_Z]; int x, y; auto buttonState = SDL_GetMouseState(&x, &y); io.MousePos = ImVec2((float)x, (float)y); io.MouseDown[0] = !!(buttonState & SDL_BUTTON(SDL_BUTTON_LEFT)); io.MouseDown[1] = !!(buttonState & SDL_BUTTON(SDL_BUTTON_RIGHT)); io.MouseDown[2] = !!(buttonState & SDL_BUTTON(SDL_BUTTON_MIDDLE)); ImGui::NewFrame(); GApplicationTickFunction(fDeltaTime); EndCommandsFrame(GGPUMainQueue); } GApplicationShutdownFunction(); WaitForCompletion(); FreeModelsMemory(); ImGui::Shutdown(); ShutdownRenderingEngines(); ShutdownResources(); ShutdownDevice(); FreeShadersMemory(); ShutdownSDL(SDLWindow); ShutdownScheduler(); ShutdownProfiler(); ShutdownMainThread(); return 0; }