void ARM_Dynarmic::ExecuteInstructions(int num_instructions) { MICROPROFILE_SCOPE(ARM_Jit); unsigned ticks_executed = jit->Run(static_cast<unsigned>(num_instructions)); AddTicks(ticks_executed); }
int main(int argc, char* argv[]) { MicroProfileOnThreadCreate("Main"); printf("press ctrl-c to quit\n"); //turn on profiling MicroProfileSetForceEnable(true); MicroProfileSetEnableAllGroups(true); MicroProfileSetForceMetaCounters(true); MicroProfileStartContextSwitchTrace(); StartFakeWork(); while(!g_nQuit) { MICROPROFILE_SCOPE(MAIN); { usleep(16000); } MicroProfileFlip(); static bool once = false; if(!once) { once = 1; printf("open localhost:%d in chrome to capture profile data\n", MicroProfileWebServerPort()); } } StopFakeWork(); MicroProfileShutdown(); return 0; }
void ShaderSetup::Run(UnitState& state, const InputVertex& input, int num_attributes) { auto& config = g_state.regs.vs; auto& setup = g_state.vs; MICROPROFILE_SCOPE(GPU_Shader); // Setup input register table const auto& attribute_register_map = config.input_register_map; for (unsigned i = 0; i < num_attributes; i++) state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i]; state.conditional_code[0] = false; state.conditional_code[1] = false; #ifdef ARCHITECTURE_x86_64 if (VideoCore::g_shader_jit_enabled) { jit_shader->Run(setup, state, config.main_offset); } else { DebugData<false> dummy_debug_data; RunInterpreter(setup, state, dummy_debug_data, config.main_offset); } #else DebugData<false> dummy_debug_data; RunInterpreter(setup, state, dummy_debug_data, config.main_offset); #endif // ARCHITECTURE_x86_64 }
void CompositorBase::SubmitFovLayer(ovrRecti viewport[ovrEye_Count], ovrFovPort fov[ovrEye_Count], ovrTextureSwapChain swapChain[ovrEye_Count], unsigned int flags) { MICROPROFILE_SCOPE(SubmitFovLayer); // If the right eye isn't set used the left eye for both if (!swapChain[ovrEye_Right]) swapChain[ovrEye_Right] = swapChain[ovrEye_Left]; MICROPROFILE_META_CPU("SwapChain Right", swapChain[ovrEye_Right]->Identifier); MICROPROFILE_META_CPU("SwapChain Left", swapChain[ovrEye_Left]->Identifier); // Render the scene layer for (int i = 0; i < ovrEye_Count; i++) { // Get the scene fov ovrFovPort sceneFov; if (m_SceneLayer->Type == ovrLayerType_EyeFov) sceneFov = ((ovrLayerEyeFov*)m_SceneLayer)->Fov[i]; else if (m_SceneLayer->Type == ovrLayerType_EyeMatrix) sceneFov = MatrixToFovPort(((ovrLayerEyeMatrix*)m_SceneLayer)->Matrix[i]); // Calculate the fov quad vr::HmdVector4_t quad; quad.v[0] = fov[i].LeftTan / -sceneFov.LeftTan; quad.v[1] = fov[i].RightTan / sceneFov.RightTan; quad.v[2] = fov[i].UpTan / sceneFov.UpTan; quad.v[3] = fov[i].DownTan / -sceneFov.DownTan; // Calculate the texture bounds vr::VRTextureBounds_t bounds = ViewportToTextureBounds(viewport[i], swapChain[i], flags); // Composit the layer if (m_SceneLayer->Type == ovrLayerType_EyeFov) { ovrLayerEyeFov* layer = (ovrLayerEyeFov*)m_SceneLayer; RenderTextureSwapChain((vr::EVREye)i, swapChain[i], layer->ColorTexture[i], layer->Viewport[i], bounds, quad); } else if (m_SceneLayer->Type == ovrLayerType_EyeMatrix) { ovrLayerEyeMatrix* layer = (ovrLayerEyeMatrix*)m_SceneLayer; RenderTextureSwapChain((vr::EVREye)i, swapChain[i], layer->ColorTexture[i], layer->Viewport[i], bounds, quad); } } swapChain[ovrEye_Left]->Submit(); if (swapChain[ovrEye_Left] != swapChain[ovrEye_Right]) swapChain[ovrEye_Right]->Submit(); }
vr::VRCompositorError CompositorBase::SubmitSceneLayer(ovrRecti viewport[ovrEye_Count], ovrFovPort fov[ovrEye_Count], ovrTextureSwapChain swapChain[ovrEye_Count], unsigned int flags) { MICROPROFILE_SCOPE(SubmitSceneLayer); // If the right eye isn't set used the left eye for both if (!swapChain[ovrEye_Right]) swapChain[ovrEye_Right] = swapChain[ovrEye_Left]; MICROPROFILE_META_CPU("SwapChain Right", swapChain[ovrEye_Right]->Identifier); MICROPROFILE_META_CPU("SwapChain Left", swapChain[ovrEye_Left]->Identifier); // Submit the scene layer. vr::VRCompositorError err; for (int i = 0; i < ovrEye_Count; i++) { ovrTextureSwapChain chain = swapChain[i]; vr::VRTextureBounds_t bounds = ViewportToTextureBounds(viewport[i], swapChain[i], flags); // Shrink the bounds to account for the overlapping fov vr::VRTextureBounds_t fovBounds = FovPortToTextureBounds((ovrEyeType)i, fov[i]); // Combine the fov bounds with the viewport bounds bounds.uMin += fovBounds.uMin * bounds.uMax; bounds.uMax *= fovBounds.uMax; bounds.vMin += fovBounds.vMin * bounds.vMax; bounds.vMax *= fovBounds.vMax; vr::Texture_t texture = chain->Textures[chain->SubmitIndex]->ToVRTexture(); err = vr::VRCompositor()->Submit((vr::EVREye)i, &texture, &bounds); if (err != vr::VRCompositorError_None) break; } swapChain[ovrEye_Left]->Submit(); if (swapChain[ovrEye_Left] != swapChain[ovrEye_Right]) swapChain[ovrEye_Right]->Submit(); return err; }
void RasterizerCacheOpenGL::LoadAndBindTexture(OpenGLState &state, unsigned texture_unit, const Pica::DebugUtils::TextureInfo& info) { const auto cached_texture = texture_cache.find(info.physical_address); if (cached_texture != texture_cache.end()) { state.texture_units[texture_unit].texture_2d = cached_texture->second->texture.handle; state.Apply(); } else { MICROPROFILE_SCOPE(OpenGL_TextureUpload); std::unique_ptr<CachedTexture> new_texture = Common::make_unique<CachedTexture>(); new_texture->texture.Create(); state.texture_units[texture_unit].texture_2d = new_texture->texture.handle; state.Apply(); glActiveTexture(GL_TEXTURE0 + texture_unit); u8* texture_src_data = Memory::GetPhysicalPointer(info.physical_address); new_texture->width = info.width; new_texture->height = info.height; new_texture->size = info.stride * info.height; new_texture->addr = info.physical_address; new_texture->hash = Common::ComputeHash64(texture_src_data, new_texture->size); std::unique_ptr<Math::Vec4<u8>[]> temp_texture_buffer_rgba(new Math::Vec4<u8>[info.width * info.height]); for (int y = 0; y < info.height; ++y) { for (int x = 0; x < info.width; ++x) { temp_texture_buffer_rgba[x + info.width * y] = Pica::DebugUtils::LookupTexture(texture_src_data, x, info.height - 1 - y, info); } } glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp_texture_buffer_rgba.get()); texture_cache.emplace(info.physical_address, std::move(new_texture)); } }
void ARM_Dynarmic::Run() { ASSERT(Memory::GetCurrentPageTable() == current_page_table); MICROPROFILE_SCOPE(ARM_Jit); jit->Run(); }
inline void Write(u32 addr, const T data) { addr -= HW::VADDR_GPU; u32 index = addr / 4; // Writes other than u32 are untested, so I'd rather have them abort than silently fail if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) { LOG_ERROR(HW_GPU, "unknown Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32)data, addr); return; } g_regs[index] = static_cast<u32>(data); switch (index) { // Memory fills are triggered once the fill value is written. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[0].trigger, 0x00004 + 0x3): case GPU_REG_INDEX_WORKAROUND(memory_fill_config[1].trigger, 0x00008 + 0x3): { const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].trigger)); auto& config = g_regs.memory_fill_config[is_second_filler]; if (config.trigger) { MemoryFill(config); LOG_TRACE(HW_GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress()); // It seems that it won't signal interrupt if "address_start" is zero. // TODO: hwtest this if (config.GetStartAddress() != 0) { if (!is_second_filler) { GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC0); } else { GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC1); } } // Reset "trigger" flag and set the "finish" flag // NOTE: This was confirmed to happen on hardware even if "address_start" is zero. config.trigger.Assign(0); config.finished.Assign(1); } break; } case GPU_REG_INDEX(display_transfer_config.trigger): { MICROPROFILE_SCOPE(GPU_DisplayTransfer); const auto& config = g_regs.display_transfer_config; if (config.trigger & 1) { if (Pica::g_debug_context) Pica::g_debug_context->OnEvent(Pica::DebugContext::Event::IncomingDisplayTransfer, nullptr); if (config.is_texture_copy) { TextureCopy(config); LOG_TRACE(HW_GPU, "TextureCopy: 0x%X bytes from 0x%08X(%u+%u)-> " "0x%08X(%u+%u), flags 0x%08X", config.texture_copy.size, config.GetPhysicalInputAddress(), config.texture_copy.input_width * 16, config.texture_copy.input_gap * 16, config.GetPhysicalOutputAddress(), config.texture_copy.output_width * 16, config.texture_copy.output_gap * 16, config.flags); } else { DisplayTransfer(config); LOG_TRACE(HW_GPU, "DisplayTransfer: 0x%08x(%ux%u)-> " "0x%08x(%ux%u), dst format %x, flags 0x%08X", config.GetPhysicalInputAddress(), config.input_width.Value(), config.input_height.Value(), config.GetPhysicalOutputAddress(), config.output_width.Value(), config.output_height.Value(), config.output_format.Value(), config.flags); } g_regs.display_transfer_config.trigger = 0; GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF); } break; } // Seems like writing to this register triggers processing case GPU_REG_INDEX(command_processor_config.trigger): { const auto& config = g_regs.command_processor_config; if (config.trigger & 1) { MICROPROFILE_SCOPE(GPU_CmdlistProcessing); u32* buffer = (u32*)Memory::GetPhysicalPointer(config.GetPhysicalAddress()); if (Pica::g_debug_context && Pica::g_debug_context->recorder) { Pica::g_debug_context->recorder->MemoryAccessed( (u8*)buffer, config.size * sizeof(u32), config.GetPhysicalAddress()); } Pica::CommandProcessor::ProcessCommandList(buffer, config.size); g_regs.command_processor_config.trigger = 0; } break; } default: break; } // Notify tracer about the register write // This is happening *after* handling the write to make sure we properly catch all memory reads. if (Pica::g_debug_context && Pica::g_debug_context->recorder) { // addr + GPU VBase - IO VBase + IO PBase Pica::g_debug_context->recorder->RegisterWritten<T>( addr + 0x1EF00000 - 0x1EC00000 + 0x10100000, data); } }
int main(int argc, char* argv[]) { MicroProfileOnThreadCreate("AA_Main"); if(SDL_Init(SDL_INIT_VIDEO) < 0) { return 1; } SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetSwapInterval(1); SDL_Window * pWindow = SDL_CreateWindow("microprofiledemo", 10, 10, WIDTH, HEIGHT, SDL_WINDOW_OPENGL); if(!pWindow) return 1; SDL_GLContext glcontext = SDL_GL_CreateContext(pWindow); glewExperimental=1; GLenum err=glewInit(); printf("ERROR IS %d\n",err); if(err!=GLEW_OK) { __BREAK(); } glGetError(); //glew generates an error InitGLBuffers(); #if MICROPROFILE_ENABLED MicroProfileGpuInitGL(); #endif SDL_GL_SetSwapInterval(1); while(!g_nQuit) { MICROPROFILE_SCOPE(MAIN); MICROPROFILE_COUNTER_ADD("engine/frames", 1); SDL_Event Evt; while(SDL_PollEvent(&Evt)) { MICROPROFILE_COUNTER_LOCAL_ADD_ATOMIC(SDLFrameEvents, 1); HandleEvent(&Evt); } MICROPROFILE_COUNTER_LOCAL_UPDATE_SET_ATOMIC(SDLFrameEvents); glClearColor(0.3f,0.4f,0.6f,0.f); glViewport(0, 0, WIDTH, HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); DrawGLStuff(); MicroProfileFlip(0); MICROPROFILE_SCOPEI("MAIN", "Flip", 0xffee00); SDL_GL_SwapWindow(pWindow); static bool bOnce = false; if(!bOnce) { bOnce = true; printf("open localhost:%d in chrome to capture profile data\n", MicroProfileWebServerPort()); } } MicroProfileShutdown(); SDL_GL_DeleteContext(glcontext); SDL_DestroyWindow(pWindow); SDL_Quit(); return 0; }
vr::EVRCompositorError CompositorBase::SubmitFrame(ovrLayerHeader const * const * layerPtrList, unsigned int layerCount) { MICROPROFILE_SCOPE(SubmitFrame); // Other layers are interpreted as overlays. std::vector<vr::VROverlayHandle_t> activeOverlays; for (uint32_t i = 0; i < layerCount; i++) { if (layerPtrList[i] == nullptr) continue; if (layerPtrList[i]->Type == ovrLayerType_Quad) { ovrLayerQuad* layer = (ovrLayerQuad*)layerPtrList[i]; ovrTextureSwapChain chain = layer->ColorTexture; // Every overlay is associated with a swapchain. // This is necessary because the position of the layer may change in the array, // which would otherwise cause flickering between overlays. // TODO: Support multiple overlays using the same texture. vr::VROverlayHandle_t overlay = layer->ColorTexture->Overlay; if (overlay == vr::k_ulOverlayHandleInvalid) { overlay = CreateOverlay(); layer->ColorTexture->Overlay = overlay; } activeOverlays.push_back(overlay); // Set the layer rendering order. vr::VROverlay()->SetOverlaySortOrder(overlay, i); // Transform the overlay. vr::HmdMatrix34_t transform = REV::Matrix4f(layer->QuadPoseCenter); vr::VROverlay()->SetOverlayWidthInMeters(overlay, layer->QuadSize.x); if (layer->Header.Flags & ovrLayerFlag_HeadLocked) vr::VROverlay()->SetOverlayTransformTrackedDeviceRelative(overlay, vr::k_unTrackedDeviceIndex_Hmd, &transform); else vr::VROverlay()->SetOverlayTransformAbsolute(overlay, vr::VRCompositor()->GetTrackingSpace(), &transform); // Set the texture and show the overlay. vr::VRTextureBounds_t bounds = ViewportToTextureBounds(layer->Viewport, layer->ColorTexture, layer->Header.Flags); vr::Texture_t texture = chain->Textures[chain->SubmitIndex]->ToVRTexture(); vr::VROverlay()->SetOverlayTextureBounds(overlay, &bounds); vr::VROverlay()->SetOverlayTexture(overlay, &texture); chain->Submit(); // Show the overlay, unfortunately we have no control over the order in which // overlays are drawn. // TODO: Support ovrLayerFlag_HighQuality for overlays with anisotropic sampling. // TODO: Handle overlay errors. vr::VROverlay()->ShowOverlay(overlay); } else if (layerPtrList[i]->Type == ovrLayerType_EyeFov) { ovrLayerEyeFov* sceneLayer = (ovrLayerEyeFov*)layerPtrList[i]; if (!m_SceneLayer) m_SceneLayer = layerPtrList[i]; else SubmitFovLayer(sceneLayer->Viewport, sceneLayer->Fov, sceneLayer->ColorTexture, sceneLayer->Header.Flags); } else if (layerPtrList[i]->Type == ovrLayerType_EyeMatrix) { ovrLayerEyeMatrix* sceneLayer = (ovrLayerEyeMatrix*)layerPtrList[i]; ovrFovPort fov[ovrEye_Count] = { MatrixToFovPort(sceneLayer->Matrix[ovrEye_Left]), MatrixToFovPort(sceneLayer->Matrix[ovrEye_Right]) }; if (!m_SceneLayer) m_SceneLayer = layerPtrList[i]; else SubmitFovLayer(sceneLayer->Viewport, fov, sceneLayer->ColorTexture, sceneLayer->Header.Flags); } } // Hide previous overlays that are not part of the current layers. for (vr::VROverlayHandle_t overlay : m_ActiveOverlays) { // Find the overlay in the current active overlays, if it was not found then hide it. // TODO: Handle overlay errors. if (std::find(activeOverlays.begin(), activeOverlays.end(), overlay) == activeOverlays.end()) vr::VROverlay()->HideOverlay(overlay); } m_ActiveOverlays = activeOverlays; vr::EVRCompositorError error = vr::VRCompositorError_None; if (m_SceneLayer && m_SceneLayer->Type == ovrLayerType_EyeFov) { ovrLayerEyeFov* sceneLayer = (ovrLayerEyeFov*)m_SceneLayer; error = SubmitSceneLayer(sceneLayer->Viewport, sceneLayer->Fov, sceneLayer->ColorTexture, sceneLayer->Header.Flags); if (m_MirrorTexture && error == vr::VRCompositorError_None) RenderMirrorTexture(m_MirrorTexture, sceneLayer->ColorTexture); } else if (m_SceneLayer && m_SceneLayer->Type == ovrLayerType_EyeMatrix) { ovrLayerEyeMatrix* sceneLayer = (ovrLayerEyeMatrix*)m_SceneLayer; ovrFovPort fov[ovrEye_Count] = { MatrixToFovPort(sceneLayer->Matrix[ovrEye_Left]), MatrixToFovPort(sceneLayer->Matrix[ovrEye_Right]) }; error = SubmitSceneLayer(sceneLayer->Viewport, fov, sceneLayer->ColorTexture, sceneLayer->Header.Flags); if (m_MirrorTexture && error == vr::VRCompositorError_None) RenderMirrorTexture(m_MirrorTexture, sceneLayer->ColorTexture); } m_SceneLayer = nullptr; return error; }
OutputVertex Run(UnitState<false>& state, const InputVertex& input, int num_attributes) { auto& config = g_state.regs.vs; Common::Profiling::ScopeTimer timer(shader_category); MICROPROFILE_SCOPE(GPU_VertexShader); state.program_counter = config.main_offset; state.debug.max_offset = 0; state.debug.max_opdesc_id = 0; // Setup input register table const auto& attribute_register_map = config.input_register_map; // TODO: Instead of this cumbersome logic, just load the input data directly like // for (int attr = 0; attr < num_attributes; ++attr) { input_attr[0] = state.registers.input[attribute_register_map.attribute0_register]; } if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = input.attr[0]; if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = input.attr[1]; if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = input.attr[2]; if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = input.attr[3]; if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = input.attr[4]; if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = input.attr[5]; if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = input.attr[6]; if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = input.attr[7]; if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = input.attr[8]; if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = input.attr[9]; if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = input.attr[10]; if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = input.attr[11]; if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = input.attr[12]; if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = input.attr[13]; if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = input.attr[14]; if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = input.attr[15]; state.conditional_code[0] = false; state.conditional_code[1] = false; #ifdef ARCHITECTURE_x86_64 if (VideoCore::g_shader_jit_enabled) jit_shader(&state.registers); else RunInterpreter(state); #else RunInterpreter(state); #endif // ARCHITECTURE_x86_64 // Setup output data OutputVertex ret; // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to // figure out what those circumstances are and enable the remaining outputs then. for (int i = 0; i < 7; ++i) { const auto& output_register_map = g_state.regs.vs_output_attributes[i]; // TODO: Don't hardcode VS here u32 semantics[4] = { output_register_map.map_x, output_register_map.map_y, output_register_map.map_z, output_register_map.map_w }; for (int comp = 0; comp < 4; ++comp) { float24* out = ((float24*)&ret) + semantics[comp]; if (semantics[comp] != Regs::VSOutputAttributes::INVALID) { *out = state.registers.output[i][comp]; } else { // Zero output so that attributes which aren't output won't have denormals in them, // which would slow us down later. memset(out, 0, sizeof(*out)); } } } // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation for (int i = 0; i < 4; ++i) { ret.color[i] = float24::FromFloat32( std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f)); } LOG_TRACE(Render_Software, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), " "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)", ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(), ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(), ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(), ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(), ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(), ret.view.x.ToFloat32(), ret.view.y.ToFloat32(), ret.view.z.ToFloat32()); return ret; }
inline void Write(u32 addr, const T data) { addr -= HW::VADDR_GPU; u32 index = addr / 4; // Writes other than u32 are untested, so I'd rather have them abort than silently fail if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) { LOG_ERROR(HW_GPU, "unknown Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32)data, addr); return; } g_regs[index] = static_cast<u32>(data); switch (index) { // Memory fills are triggered once the fill value is written. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[0].trigger, 0x00004 + 0x3): case GPU_REG_INDEX_WORKAROUND(memory_fill_config[1].trigger, 0x00008 + 0x3): { const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].trigger)); auto& config = g_regs.memory_fill_config[is_second_filler]; if (config.trigger) { if (config.address_start) { // Some games pass invalid values here u8* start = Memory::GetPhysicalPointer(config.GetStartAddress()); u8* end = Memory::GetPhysicalPointer(config.GetEndAddress()); if (config.fill_24bit) { // fill with 24-bit values for (u8* ptr = start; ptr < end; ptr += 3) { ptr[0] = config.value_24bit_r; ptr[1] = config.value_24bit_g; ptr[2] = config.value_24bit_b; } } else if (config.fill_32bit) { // fill with 32-bit values for (u32* ptr = (u32*)start; ptr < (u32*)end; ++ptr) *ptr = config.value_32bit; } else { // fill with 16-bit values for (u16* ptr = (u16*)start; ptr < (u16*)end; ++ptr) *ptr = config.value_16bit; } LOG_TRACE(HW_GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress()); if (!is_second_filler) { GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC0); } else { GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC1); } VideoCore::g_renderer->rasterizer->InvalidateRegion(config.GetStartAddress(), config.GetEndAddress() - config.GetStartAddress()); } // Reset "trigger" flag and set the "finish" flag // NOTE: This was confirmed to happen on hardware even if "address_start" is zero. config.trigger = 0; config.finished = 1; } break; } case GPU_REG_INDEX(display_transfer_config.trigger): { MICROPROFILE_SCOPE(GPU_DisplayTransfer); const auto& config = g_regs.display_transfer_config; if (config.trigger & 1) { if (Pica::g_debug_context) Pica::g_debug_context->OnEvent(Pica::DebugContext::Event::IncomingDisplayTransfer, nullptr); u8* src_pointer = Memory::GetPhysicalPointer(config.GetPhysicalInputAddress()); u8* dst_pointer = Memory::GetPhysicalPointer(config.GetPhysicalOutputAddress()); if (config.is_texture_copy) { u32 input_width = config.texture_copy.input_width * 16; u32 input_gap = config.texture_copy.input_gap * 16; u32 output_width = config.texture_copy.output_width * 16; u32 output_gap = config.texture_copy.output_gap * 16; size_t contiguous_input_size = config.texture_copy.size / input_width * (input_width + input_gap); VideoCore::g_renderer->rasterizer->FlushRegion(config.GetPhysicalInputAddress(), contiguous_input_size); u32 remaining_size = config.texture_copy.size; u32 remaining_input = input_width; u32 remaining_output = output_width; while (remaining_size > 0) { u32 copy_size = std::min({ remaining_input, remaining_output, remaining_size }); std::memcpy(dst_pointer, src_pointer, copy_size); src_pointer += copy_size; dst_pointer += copy_size; remaining_input -= copy_size; remaining_output -= copy_size; remaining_size -= copy_size; if (remaining_input == 0) { remaining_input = input_width; src_pointer += input_gap; } if (remaining_output == 0) { remaining_output = output_width; dst_pointer += output_gap; } } LOG_TRACE(HW_GPU, "TextureCopy: 0x%X bytes from 0x%08X(%u+%u)-> 0x%08X(%u+%u), flags 0x%08X", config.texture_copy.size, config.GetPhysicalInputAddress(), input_width, input_gap, config.GetPhysicalOutputAddress(), output_width, output_gap, config.flags); size_t contiguous_output_size = config.texture_copy.size / output_width * (output_width + output_gap); VideoCore::g_renderer->rasterizer->InvalidateRegion(config.GetPhysicalOutputAddress(), contiguous_output_size); GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF); break; } if (config.scaling > config.ScaleXY) { LOG_CRITICAL(HW_GPU, "Unimplemented display transfer scaling mode %u", config.scaling.Value()); UNIMPLEMENTED(); break; } if (config.input_linear && config.scaling != config.NoScale) { LOG_CRITICAL(HW_GPU, "Scaling is only implemented on tiled input"); UNIMPLEMENTED(); break; } bool horizontal_scale = config.scaling != config.NoScale; bool vertical_scale = config.scaling == config.ScaleXY; u32 output_width = config.output_width >> horizontal_scale; u32 output_height = config.output_height >> vertical_scale; u32 input_size = config.input_width * config.input_height * GPU::Regs::BytesPerPixel(config.input_format); u32 output_size = output_width * output_height * GPU::Regs::BytesPerPixel(config.output_format); VideoCore::g_renderer->rasterizer->FlushRegion(config.GetPhysicalInputAddress(), input_size); for (u32 y = 0; y < output_height; ++y) { for (u32 x = 0; x < output_width; ++x) { Math::Vec4<u8> src_color; // Calculate the [x,y] position of the input image // based on the current output position and the scale u32 input_x = x << horizontal_scale; u32 input_y = y << vertical_scale; if (config.flip_vertically) { // Flip the y value of the output data, // we do this after calculating the [x,y] position of the input image // to account for the scaling options. y = output_height - y - 1; } u32 dst_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.output_format); u32 src_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.input_format); u32 src_offset; u32 dst_offset; if (config.input_linear) { if (!config.dont_swizzle) { // Interpret the input as linear and the output as tiled u32 coarse_y = y & ~7; u32 stride = output_width * dst_bytes_per_pixel; src_offset = (input_x + input_y * config.input_width) * src_bytes_per_pixel; dst_offset = VideoCore::GetMortonOffset(x, y, dst_bytes_per_pixel) + coarse_y * stride; } else { // Both input and output are linear src_offset = (input_x + input_y * config.input_width) * src_bytes_per_pixel; dst_offset = (x + y * output_width) * dst_bytes_per_pixel; } } else { if (!config.dont_swizzle) { // Interpret the input as tiled and the output as linear u32 coarse_y = input_y & ~7; u32 stride = config.input_width * src_bytes_per_pixel; src_offset = VideoCore::GetMortonOffset(input_x, input_y, src_bytes_per_pixel) + coarse_y * stride; dst_offset = (x + y * output_width) * dst_bytes_per_pixel; } else { // Both input and output are tiled u32 out_coarse_y = y & ~7; u32 out_stride = output_width * dst_bytes_per_pixel; u32 in_coarse_y = input_y & ~7; u32 in_stride = config.input_width * src_bytes_per_pixel; src_offset = VideoCore::GetMortonOffset(input_x, input_y, src_bytes_per_pixel) + in_coarse_y * in_stride; dst_offset = VideoCore::GetMortonOffset(x, y, dst_bytes_per_pixel) + out_coarse_y * out_stride; } } const u8* src_pixel = src_pointer + src_offset; src_color = DecodePixel(config.input_format, src_pixel); if (config.scaling == config.ScaleX) { Math::Vec4<u8> pixel = DecodePixel(config.input_format, src_pixel + src_bytes_per_pixel); src_color = ((src_color + pixel) / 2).Cast<u8>(); } else if (config.scaling == config.ScaleXY) { Math::Vec4<u8> pixel1 = DecodePixel(config.input_format, src_pixel + 1 * src_bytes_per_pixel); Math::Vec4<u8> pixel2 = DecodePixel(config.input_format, src_pixel + 2 * src_bytes_per_pixel); Math::Vec4<u8> pixel3 = DecodePixel(config.input_format, src_pixel + 3 * src_bytes_per_pixel); src_color = (((src_color + pixel1) + (pixel2 + pixel3)) / 4).Cast<u8>(); } u8* dst_pixel = dst_pointer + dst_offset; switch (config.output_format) { case Regs::PixelFormat::RGBA8: Color::EncodeRGBA8(src_color, dst_pixel); break; case Regs::PixelFormat::RGB8: Color::EncodeRGB8(src_color, dst_pixel); break; case Regs::PixelFormat::RGB565: Color::EncodeRGB565(src_color, dst_pixel); break; case Regs::PixelFormat::RGB5A1: Color::EncodeRGB5A1(src_color, dst_pixel); break; case Regs::PixelFormat::RGBA4: Color::EncodeRGBA4(src_color, dst_pixel); break; default: LOG_ERROR(HW_GPU, "Unknown destination framebuffer format %x", config.output_format.Value()); break; } } } LOG_TRACE(HW_GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%ux%u)-> 0x%08x(%ux%u), dst format %x, flags 0x%08X", config.output_height * output_width * GPU::Regs::BytesPerPixel(config.output_format), config.GetPhysicalInputAddress(), config.input_width.Value(), config.input_height.Value(), config.GetPhysicalOutputAddress(), output_width, output_height, config.output_format.Value(), config.flags); g_regs.display_transfer_config.trigger = 0; GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF); VideoCore::g_renderer->rasterizer->InvalidateRegion(config.GetPhysicalOutputAddress(), output_size); } break; } // Seems like writing to this register triggers processing case GPU_REG_INDEX(command_processor_config.trigger): { const auto& config = g_regs.command_processor_config; if (config.trigger & 1) { MICROPROFILE_SCOPE(GPU_CmdlistProcessing); u32* buffer = (u32*)Memory::GetPhysicalPointer(config.GetPhysicalAddress()); if (Pica::g_debug_context && Pica::g_debug_context->recorder) { Pica::g_debug_context->recorder->MemoryAccessed((u8*)buffer, config.size * sizeof(u32), config.GetPhysicalAddress()); } Pica::CommandProcessor::ProcessCommandList(buffer, config.size); g_regs.command_processor_config.trigger = 0; } break; } default: break; } // Notify tracer about the register write // This is happening *after* handling the write to make sure we properly catch all memory reads. if (Pica::g_debug_context && Pica::g_debug_context->recorder) { // addr + GPU VBase - IO VBase + IO PBase Pica::g_debug_context->recorder->RegisterWritten<T>(addr + 0x1EF00000 - 0x1EC00000 + 0x10100000, data); } }
/// Executes the next GSP command static void ExecuteCommand(const Command& command, u32 thread_id) { // Utility function to convert register ID to address static auto WriteGPURegister = [](u32 id, u32 data) { GPU::Write<u32>(0x1EF00000 + 4 * id, data); }; switch (command.id) { // GX request DMA - typically used for copying memory from GSP heap to VRAM case CommandId::REQUEST_DMA: { MICROPROFILE_SCOPE(GPU_GSP_DMA); Memory::MemorySystem& memory = Core::System::GetInstance().Memory(); // TODO: Consider attempting rasterizer-accelerated surface blit if that usage is ever // possible/likely Memory::RasterizerFlushVirtualRegion(command.dma_request.source_address, command.dma_request.size, Memory::FlushMode::Flush); Memory::RasterizerFlushVirtualRegion(command.dma_request.dest_address, command.dma_request.size, Memory::FlushMode::Invalidate); // TODO(Subv): These memory accesses should not go through the application's memory mapping. // They should go through the GSP module's memory mapping. memory.CopyBlock(*Core::System::GetInstance().Kernel().GetCurrentProcess(), command.dma_request.dest_address, command.dma_request.source_address, command.dma_request.size); SignalInterrupt(InterruptId::DMA); break; } // TODO: This will need some rework in the future. (why?) case CommandId::SUBMIT_GPU_CMDLIST: { auto& params = command.submit_gpu_cmdlist; if (params.do_flush) { // This flag flushes the command list (params.address, params.size) from the cache. // Command lists are not processed by the hardware renderer, so we don't need to // actually flush them in Citra. } WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(command_processor_config.address)), VirtualToPhysicalAddress(params.address) >> 3); WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(command_processor_config.size)), params.size); // TODO: Not sure if we are supposed to always write this .. seems to trigger processing // though WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(command_processor_config.trigger)), 1); // TODO(yuriks): Figure out the meaning of the `flags` field. break; } // It's assumed that the two "blocks" behave equivalently. // Presumably this is done simply to allow two memory fills to run in parallel. case CommandId::SET_MEMORY_FILL: { auto& params = command.memory_fill; if (params.start1 != 0) { WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].address_start)), VirtualToPhysicalAddress(params.start1) >> 3); WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].address_end)), VirtualToPhysicalAddress(params.end1) >> 3); WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].value_32bit)), params.value1); WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].control)), params.control1); } if (params.start2 != 0) { WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].address_start)), VirtualToPhysicalAddress(params.start2) >> 3); WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].address_end)), VirtualToPhysicalAddress(params.end2) >> 3); WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].value_32bit)), params.value2); WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].control)), params.control2); } break; }
OutputVertex ShaderSetup::Run(UnitState<false>& state, const InputVertex& input, int num_attributes) { auto& config = g_state.regs.vs; MICROPROFILE_SCOPE(GPU_Shader); state.program_counter = config.main_offset; state.debug.max_offset = 0; state.debug.max_opdesc_id = 0; // Setup input register table const auto& attribute_register_map = config.input_register_map; for (unsigned i = 0; i < num_attributes; i++) state.registers.input[attribute_register_map.GetRegisterForAttribute(i)] = input.attr[i]; state.conditional_code[0] = false; state.conditional_code[1] = false; #ifdef ARCHITECTURE_x86_64 if (VideoCore::g_shader_jit_enabled) jit_shader->Run(&state.registers, g_state.regs.vs.main_offset); else RunInterpreter(state); #else RunInterpreter(state); #endif // ARCHITECTURE_x86_64 // Setup output data OutputVertex ret; // TODO(neobrain): Under some circumstances, up to 16 attributes may be output. We need to // figure out what those circumstances are and enable the remaining outputs then. unsigned index = 0; for (unsigned i = 0; i < 7; ++i) { if (index >= g_state.regs.vs_output_total) break; if ((g_state.regs.vs.output_mask & (1 << i)) == 0) continue; const auto& output_register_map = g_state.regs.vs_output_attributes[index]; // TODO: Don't hardcode VS here u32 semantics[4] = { output_register_map.map_x, output_register_map.map_y, output_register_map.map_z, output_register_map.map_w }; for (unsigned comp = 0; comp < 4; ++comp) { float24* out = ((float24*)&ret) + semantics[comp]; if (semantics[comp] != Regs::VSOutputAttributes::INVALID) { *out = state.registers.output[i][comp]; } else { // Zero output so that attributes which aren't output won't have denormals in them, // which would slow us down later. memset(out, 0, sizeof(*out)); } } index++; } // The hardware takes the absolute and saturates vertex colors like this, *before* doing interpolation for (unsigned i = 0; i < 4; ++i) { ret.color[i] = float24::FromFloat32( std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f)); } LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), " "col(%.2f, %.2f, %.2f, %.2f), tc0(%.2f, %.2f), view(%.2f, %.2f, %.2f)", ret.pos.x.ToFloat32(), ret.pos.y.ToFloat32(), ret.pos.z.ToFloat32(), ret.pos.w.ToFloat32(), ret.quat.x.ToFloat32(), ret.quat.y.ToFloat32(), ret.quat.z.ToFloat32(), ret.quat.w.ToFloat32(), ret.color.x.ToFloat32(), ret.color.y.ToFloat32(), ret.color.z.ToFloat32(), ret.color.w.ToFloat32(), ret.tc0.u().ToFloat32(), ret.tc0.v().ToFloat32(), ret.view.x.ToFloat32(), ret.view.y.ToFloat32(), ret.view.z.ToFloat32()); return ret; }
int main(int argc, char* argv[]) { MICROPROFILE_REGISTER_GROUP("Thread0", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("Thread1", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("Thread2", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("Thread2xx", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("MAIN", "main", 0x88fff00f); g_QueueGraphics = MICROPROFILE_GPU_INIT_QUEUE("GPU-Graphics-Queue"); printf("press 'z' to toggle microprofile drawing\n"); printf("press 'right shift' to pause microprofile update\n"); printf("press 'x' to toggle profiling\n"); printf("press 'c' to toggle enable of all profiler groups\n"); MicroProfileOnThreadCreate("AA_Main"); if(SDL_Init(SDL_INIT_VIDEO) < 0) { return 1; } SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetSwapInterval(1); SDL_Window * pWindow = SDL_CreateWindow("microprofiledemo", 10, 10, WIDTH, HEIGHT, SDL_WINDOW_OPENGL); if(!pWindow) return 1; SDL_GLContext glcontext = SDL_GL_CreateContext(pWindow); glewExperimental=1; GLenum err=glewInit(); if(err!=GLEW_OK) { __BREAK(); } glGetError(); //glew generates an error #if MICROPROFILE_ENABLED MicroProfileGpuInitGL(); MicroProfileDrawInit(); MP_ASSERT(glGetError() == 0); MicroProfileToggleDisplayMode(); MicroProfileInitUI(); MicroProfileCustomGroup("Custom1", 2, 47, 2.f, MICROPROFILE_CUSTOM_BARS); MicroProfileCustomGroupAddTimer("Custom1", "MicroProfile", "Draw"); MicroProfileCustomGroupAddTimer("Custom1", "MicroProfile", "Detailed View"); MicroProfileCustomGroup("Custom2", 2, 100, 20.f, MICROPROFILE_CUSTOM_BARS|MICROPROFILE_CUSTOM_BAR_SOURCE_MAX); MicroProfileCustomGroupAddTimer("Custom2", "MicroProfile", "Draw"); MicroProfileCustomGroupAddTimer("Custom2", "MicroProfile", "Detailed View"); MicroProfileCustomGroup("Custom3", 2, 0, 5.f, MICROPROFILE_CUSTOM_STACK|MICROPROFILE_CUSTOM_STACK_SOURCE_MAX); MicroProfileCustomGroupAddTimer("Custom3", "MicroProfile", "Draw"); MicroProfileCustomGroupAddTimer("Custom3", "MicroProfile", "Detailed View"); MicroProfileCustomGroup("ThreadSafe", 6, 10, 600.f, MICROPROFILE_CUSTOM_BARS | MICROPROFILE_CUSTOM_STACK); MicroProfileCustomGroupAddTimer("ThreadSafe", "ThreadSafe", "main"); MicroProfileCustomGroupAddTimer("ThreadSafe", "ThreadSafe", "inner0"); MicroProfileCustomGroupAddTimer("ThreadSafe", "ThreadSafe", "inner1"); MicroProfileCustomGroupAddTimer("ThreadSafe", "ThreadSafe", "inner2"); MicroProfileCustomGroupAddTimer("ThreadSafe", "ThreadSafe", "inner3"); MicroProfileCustomGroupAddTimer("ThreadSafe", "ThreadSafe", "inner4"); #endif MICROPROFILE_COUNTER_CONFIG("memory/main", MICROPROFILE_COUNTER_FORMAT_BYTES, 10ll<<30ll, 0); MICROPROFILE_COUNTER_CONFIG("memory/gpu/indexbuffers", MICROPROFILE_COUNTER_FORMAT_BYTES, 0, 0); MICROPROFILE_COUNTER_CONFIG("memory/gpu/vertexbuffers", MICROPROFILE_COUNTER_FORMAT_BYTES, 0, 0); MICROPROFILE_COUNTER_CONFIG("memory/mainx", MICROPROFILE_COUNTER_FORMAT_BYTES, 10000, 0); MICROPROFILE_COUNTER_ADD("memory/main", 1000); MICROPROFILE_COUNTER_ADD("memory/gpu/vertexbuffers", 1000); MICROPROFILE_COUNTER_ADD("memory/gpu/indexbuffers", 200); MICROPROFILE_COUNTER_ADD("memory//", 10<<10); MICROPROFILE_COUNTER_ADD("memory//main", (32ll<<30ll) + (1ll <<29ll)); MICROPROFILE_COUNTER_ADD("//memory//mainx/\\//", 1000); MICROPROFILE_COUNTER_ADD("//memoryx//mainx/", 1000); MICROPROFILE_COUNTER_ADD("//memoryy//main/", -1000000); MICROPROFILE_COUNTER_ADD("//\\\\///lala////lelel", 1000); MICROPROFILE_COUNTER_CONFIG("engine/frames", MICROPROFILE_COUNTER_FORMAT_DEFAULT, 1000, 0); MICROPROFILE_COUNTER_SET("fisk/geder/", 42); MICROPROFILE_COUNTER_SET("fisk/aborre/", -2002); MICROPROFILE_COUNTER_SET_LIMIT("fisk/aborre/", 120); static int Frames = 0; static uint64_t FramesX = 0; MICROPROFILE_COUNTER_SET_INT64_PTR("frames/int64", &FramesX); MICROPROFILE_COUNTER_SET_INT32_PTR("frames/int32", &Frames); MICROPROFILE_COUNTER_CONFIG("/test/sinus", MICROPROFILE_COUNTER_FORMAT_BYTES, 0, MICROPROFILE_COUNTER_FLAG_DETAILED); MICROPROFILE_COUNTER_CONFIG("/test/cosinus", MICROPROFILE_COUNTER_FORMAT_DEFAULT, 0, MICROPROFILE_COUNTER_FLAG_DETAILED); MICROPROFILE_COUNTER_CONFIG("/runtime/sdl_frame_events", MICROPROFILE_COUNTER_FORMAT_DEFAULT, 0, MICROPROFILE_COUNTER_FLAG_DETAILED); StartFakeWork(); while(!g_nQuit) { Frames++; FramesX += 1024*1024; MICROPROFILE_SCOPE(MAIN); MICROPROFILE_COUNTER_ADD("engine/frames", 1); SDL_Event Evt; while(SDL_PollEvent(&Evt)) { MICROPROFILE_COUNTER_LOCAL_ADD(SDLFrameEvents, 1); HandleEvent(&Evt); } MICROPROFILE_COUNTER_LOCAL_UPDATE_SET(SDLFrameEvents); glClearColor(0.3f,0.4f,0.6f,0.f); glViewport(0, 0, WIDTH, HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); #if 1||FAKE_WORK { MICROPROFILE_SCOPEI("BMain", "Dummy", 0xff3399ff); for(uint32_t i = 0; i < 14; ++i) { MICROPROFILE_SCOPEI("BMain", "1ms", 0xff3399ff); MICROPROFILE_META_CPU("Sleep",1); usleep(1000); } } #endif MicroProfileMouseButton(g_MouseDown0, g_MouseDown1); MicroProfileMousePosition(g_MouseX, g_MouseY, g_MouseDelta); g_MouseDelta = 0; MicroProfileFlip(0); static float f = 0; f += 0.1f; int sinus = (int)(10000000 * (sinf(f))); int cosinus = int(cosf(f*1.3f) * 100000 + 50000); MICROPROFILE_COUNTER_SET("/test/sinus", sinus); MICROPROFILE_COUNTER_SET("/test/cosinus", cosinus); { MICROPROFILE_SCOPEGPUI("MicroProfileDraw", 0x88dd44); float projection[16]; float left = 0.f; float right = WIDTH; float bottom = HEIGHT; float top = 0.f; float near = -1.f; float far = 1.f; memset(&projection[0], 0, sizeof(projection)); projection[0] = 2.0f / (right - left); projection[5] = 2.0f / (top - bottom); projection[10] = -2.0f / (far - near); projection[12] = - (right + left) / (right - left); projection[13] = - (top + bottom) / (top - bottom); projection[14] = - (far + near) / (far - near); projection[15] = 1.f; glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); #if MICROPROFILE_ENABLED MicroProfileBeginDraw(WIDTH, HEIGHT, &projection[0]); MicroProfileDraw(WIDTH, HEIGHT); MicroProfileEndDraw(); #endif glDisable(GL_BLEND); } MICROPROFILE_SCOPEI("MAIN", "Flip", 0xffee00); SDL_GL_SwapWindow(pWindow); } StopFakeWork(); MicroProfileShutdown(); SDL_GL_DeleteContext(glcontext); SDL_DestroyWindow(pWindow); SDL_Quit(); return 0; }
void WorkerThread(int threadId) { char name[100]; snprintf(name, 99, "Worker%d", threadId); MicroProfileOnThreadCreate(&name[0]); uint32_t c0 = 0xff3399ff; uint32_t c1 = 0xffff99ff; uint32_t c2 = 0xff33ff00; uint32_t c3 = 0xff3399ff; uint32_t c4 = 0xff33ff33; while(!g_nQuit) { switch(threadId) { case 0: { usleep(100); { MICROPROFILE_SCOPEI("Thread0", "Work Thread0", c4); MICROPROFILE_META_CPU("Sleep",10); usleep(200); { MICROPROFILE_SCOPEI("Thread0", "Work Thread1", c3); MICROPROFILE_META_CPU("DrawCalls", 1); MICROPROFILE_META_CPU("DrawCalls", 1); MICROPROFILE_META_CPU("DrawCalls", 1); usleep(200); { MICROPROFILE_SCOPEI("Thread0", "Work Thread2", c2); MICROPROFILE_META_CPU("DrawCalls", 1); usleep(200); { MICROPROFILE_SCOPEI("Thread0", "Work Thread3", c1); MICROPROFILE_META_CPU("DrawCalls", 4); MICROPROFILE_META_CPU("Triangles",1000); usleep(200); } } } } } break; case 1: { usleep(100); MICROPROFILE_SCOPEI("Thread1", "Work Thread 1", c1); usleep(2000); } break; case 2: { usleep(1000); { MICROPROFILE_SCOPEI("Thread2", "Worker2", c0); spinsleep(100000); { MICROPROFILE_SCOPEI("Thread2", "InnerWork0", c1); spinsleep(100); { MICROPROFILE_SCOPEI("Thread2", "InnerWork1", c2); usleep(100); { MICROPROFILE_SCOPEI("Thread2", "InnerWork2", c3); usleep(100); { // for(uint32_t i = 0; i < 1000; ++i) // { // MICROPROFILE_SCOPEI("Thread2", "InnerWork3", c4); // spinsleep(10); // } } } } } } } break; case 3: { MICROPROFILE_SCOPEI("ThreadWork", "MAIN", c0); usleep(1000);; for(uint32_t i = 0; i < 10; ++i) { MICROPROFILE_SCOPEI("ThreadWork", "Inner0", c1); usleep(100); for(uint32_t j = 0; j < 4; ++j) { MICROPROFILE_SCOPEI("ThreadWork", "Inner1", c4); usleep(50); MICROPROFILE_SCOPEI("ThreadWork", "Inner1", c4); usleep(50); MICROPROFILE_SCOPEI("ThreadWork", "Inner2", c2); usleep(50); MICROPROFILE_SCOPEI("ThreadWork", "Inner3", c3); usleep(50); MICROPROFILE_SCOPEI("ThreadWork", "Inner4", c3); usleep(50); } } } break; default: MICROPROFILE_SCOPE(ThreadSafeMain); usleep(1000);; for(uint32_t i = 0; i < 5; ++i) { MICROPROFILE_SCOPE(ThreadSafeInner0); usleep(1000); for(uint32_t j = 0; j < 4; ++j) { MICROPROFILE_META_CPU("custom_very_long_meta", 1); MICROPROFILE_SCOPE(ThreadSafeInner1); usleep(500); MICROPROFILE_SCOPE(ThreadSafeInner2); usleep(150); MICROPROFILE_SCOPE(ThreadSafeInner3); usleep(150); MICROPROFILE_SCOPE(ThreadSafeInner4); usleep(150); } } break; } } }
int main(int argc, char* argv[]) { MICROPROFILE_REGISTER_GROUP("Thread0", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("Thread1", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("Thread2", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("Thread2xx", "Threads", 0x88008800); MICROPROFILE_REGISTER_GROUP("GPU", "main", 0x88fff00f); MICROPROFILE_REGISTER_GROUP("MAIN", "main", 0x88fff00f); printf("press 'z' to toggle microprofile drawing\n"); printf("press 'right shift' to pause microprofile update\n"); printf("press 'x' to toggle profiling\n"); printf("press 'c' to toggle enable of all profiler groups\n"); MicroProfileOnThreadCreate("AA_Main"); if(SDL_Init(SDL_INIT_VIDEO) < 0) { return 1; } SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetSwapInterval(1); SDL_Window * pWindow = SDL_CreateWindow("microprofiledemo", 10, 10, WIDTH, HEIGHT, SDL_WINDOW_OPENGL); if(!pWindow) return 1; SDL_GLContext glcontext = SDL_GL_CreateContext(pWindow); glewExperimental=1; GLenum err=glewInit(); if(err!=GLEW_OK) { __BREAK(); } glGetError(); //glew generates an error #if MICROPROFILE_ENABLED MicroProfileGpuInitGL(); MicroProfileDrawInit(); MP_ASSERT(glGetError() == 0); MicroProfileToggleDisplayMode(); #endif StartFakeWork(); while(!g_nQuit) { MICROPROFILE_SCOPE(MAIN); SDL_Event Evt; while(SDL_PollEvent(&Evt)) { HandleEvent(&Evt); } glClearColor(0.3f,0.4f,0.6f,0.f); glViewport(0, 0, WIDTH, HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); #if 1||FAKE_WORK { MICROPROFILE_SCOPEI("BMain", "Dummy", 0xff3399ff); for(uint32_t i = 0; i < 14; ++i) { MICROPROFILE_SCOPEI("BMain", "1ms", 0xff3399ff); MICROPROFILE_META_CPU("Sleep",1); usleep(1000); } } #endif MicroProfileMouseButton(g_MouseDown0, g_MouseDown1); MicroProfileMousePosition(g_MouseX, g_MouseY, g_MouseDelta); g_MouseDelta = 0; MicroProfileFlip(); { MICROPROFILE_SCOPEGPUI("MicroProfileDraw", 0x88dd44); float projection[16]; float left = 0.f; float right = WIDTH; float bottom = HEIGHT; float top = 0.f; float near = -1.f; float far = 1.f; memset(&projection[0], 0, sizeof(projection)); projection[0] = 2.0f / (right - left); projection[5] = 2.0f / (top - bottom); projection[10] = -2.0f / (far - near); projection[12] = - (right + left) / (right - left); projection[13] = - (top + bottom) / (top - bottom); projection[14] = - (far + near) / (far - near); projection[15] = 1.f; glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); #if MICROPROFILE_ENABLED MicroProfileBeginDraw(WIDTH, HEIGHT, &projection[0]); MicroProfileDraw(WIDTH, HEIGHT); MicroProfileEndDraw(); #endif glDisable(GL_BLEND); } MICROPROFILE_SCOPEI("MAIN", "Flip", 0xffee00); SDL_GL_SwapWindow(pWindow); } StopFakeWork(); MicroProfileShutdown(); SDL_GL_DeleteContext(glcontext); SDL_DestroyWindow(pWindow); SDL_Quit(); return 0; }