case Regs::PixelFormat::RGB565: return Color::DecodeRGB565(src_pixel); case Regs::PixelFormat::RGB5A1: return Color::DecodeRGB5A1(src_pixel); case Regs::PixelFormat::RGBA4: return Color::DecodeRGBA4(src_pixel); default: LOG_ERROR(HW_GPU, "Unknown source framebuffer format %x", input_format); return {0, 0, 0, 0}; } } MICROPROFILE_DEFINE(GPU_DisplayTransfer, "GPU", "DisplayTransfer", MP_RGB(100, 100, 255)); MICROPROFILE_DEFINE(GPU_CmdlistProcessing, "GPU", "Cmdlist Processing", MP_RGB(100, 255, 100)); static void MemoryFill(const Regs::MemoryFillConfig& config) { const PAddr start_addr = config.GetStartAddress(); const PAddr end_addr = config.GetEndAddress(); // TODO: do hwtest with these cases if (!Memory::IsValidPhysicalAddress(start_addr)) { LOG_CRITICAL(HW_GPU, "invalid start address 0x%08X", start_addr); return; } if (!Memory::IsValidPhysicalAddress(end_addr)) { LOG_CRITICAL(HW_GPU, "invalid end address 0x%08X", end_addr); return;
s64 ticks = CoreTiming::GetDowncount(); return static_cast<u64>(ticks <= 0 ? 0 : ticks); } ARM_Dynarmic& parent; }; ARM_Dynarmic::ARM_Dynarmic(PrivilegeMode initial_mode) : cb(std::make_unique<DynarmicUserCallbacks>(*this)) { interpreter_state = std::make_shared<ARMul_State>(initial_mode); PageTableChanged(); } ARM_Dynarmic::~ARM_Dynarmic() = default; MICROPROFILE_DEFINE(ARM_Jit, "ARM JIT", "ARM JIT", MP_RGB(255, 64, 64)); void ARM_Dynarmic::Run() { ASSERT(Memory::GetCurrentPageTable() == current_page_table); MICROPROFILE_SCOPE(ARM_Jit); jit->Run(); } void ARM_Dynarmic::Step() { cb->InterpreterFallback(jit->Regs()[15], 1); } void ARM_Dynarmic::SetPC(u32 pc) { jit->Regs()[15] = pc; }
} } } GSUnitState::GSUnitState() : UnitState(&emitter) {} void GSUnitState::SetVertexHandler(VertexHandler vertex_handler, WindingSetter winding_setter) { emitter.handlers->vertex_handler = std::move(vertex_handler); emitter.handlers->winding_setter = std::move(winding_setter); } void GSUnitState::ConfigOutput(const ShaderRegs& config) { emitter.output_mask = config.output_mask; } MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240)); #ifdef ARCHITECTURE_x86_64 static std::unique_ptr<JitX64Engine> jit_engine; #endif // ARCHITECTURE_x86_64 static InterpreterEngine interpreter_engine; ShaderEngine* GetEngine() { #ifdef ARCHITECTURE_x86_64 // TODO(yuriks): Re-initialize on each change rather than being persistent if (VideoCore::g_shader_jit_enabled) { if (jit_engine == nullptr) { jit_engine = std::make_unique<JitX64Engine>(); } return jit_engine.get(); }
#include "common/make_unique.h" #include "common/math_util.h" #include "common/microprofile.h" #include "common/vector_math.h" #include "core/memory.h" #include "video_core/debug_utils/debug_utils.h" #include "video_core/renderer_opengl/gl_rasterizer_cache.h" #include "video_core/renderer_opengl/pica_to_gl.h" RasterizerCacheOpenGL::~RasterizerCacheOpenGL() { InvalidateAll(); } MICROPROFILE_DEFINE(OpenGL_TextureUpload, "OpenGL", "Texture Upload", MP_RGB(128, 64, 192)); 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();
// right), but the PDC0/1 interrupts are signaled for every registered thread. if (interrupt_id == InterruptId::PDC0 || interrupt_id == InterruptId::PDC1) { for (u32 thread_id = 0; thread_id < MaxGSPThreads; ++thread_id) { SignalInterruptForThread(interrupt_id, thread_id); } return; } // For normal interrupts, don't do anything if no process has acquired the GPU right. if (active_thread_id == -1) return; SignalInterruptForThread(interrupt_id, active_thread_id); } MICROPROFILE_DEFINE(GPU_GSP_DMA, "GPU", "GSP DMA", MP_RGB(100, 0, 255)); /// 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();
namespace Shader { #ifdef ARCHITECTURE_x86_64 static std::unordered_map<u64, std::unique_ptr<JitShader>> shader_map; static const JitShader* jit_shader; #endif // ARCHITECTURE_x86_64 void ClearCache() { #ifdef ARCHITECTURE_x86_64 shader_map.clear(); #endif // ARCHITECTURE_x86_64 } void ShaderSetup::Setup() { #ifdef ARCHITECTURE_x86_64 if (VideoCore::g_shader_jit_enabled) { u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^ Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data))); auto iter = shader_map.find(cache_key); if (iter != shader_map.end()) { jit_shader = iter->second.get(); } else { auto shader = std::make_unique<JitShader>(); shader->Compile(); jit_shader = shader.get(); shader_map[cache_key] = std::move(shader); } } #endif // ARCHITECTURE_x86_64 } MICROPROFILE_DEFINE(GPU_Shader, "GPU", "Shader", MP_RGB(50, 50, 240)); 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; } DebugData<true> ShaderSetup::ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const ShaderSetup& setup) { UnitState<true> state; 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; float24 dummy_register; boost::fill(state.registers.input, &dummy_register); 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; RunInterpreter(state); return state.debug; } } // namespace Shader
namespace Shader { #ifdef ARCHITECTURE_x86_64 static std::unordered_map<u64, CompiledShader*> shader_map; static JitCompiler jit; static CompiledShader* jit_shader; #endif // ARCHITECTURE_x86_64 void Setup(UnitState<false>& state) { #ifdef ARCHITECTURE_x86_64 if (VideoCore::g_shader_jit_enabled) { u64 cache_key = (Common::ComputeHash64(&g_state.vs.program_code, sizeof(g_state.vs.program_code)) ^ Common::ComputeHash64(&g_state.vs.swizzle_data, sizeof(g_state.vs.swizzle_data)) ^ g_state.regs.vs.main_offset); auto iter = shader_map.find(cache_key); if (iter != shader_map.end()) { jit_shader = iter->second; } else { jit_shader = jit.Compile(); shader_map.emplace(cache_key, jit_shader); } } #endif // ARCHITECTURE_x86_64 } void Shutdown() { #ifdef ARCHITECTURE_x86_64 shader_map.clear(); #endif // ARCHITECTURE_x86_64 } static Common::Profiling::TimingCategory shader_category("Vertex Shader"); MICROPROFILE_DEFINE(GPU_VertexShader, "GPU", "Vertex Shader", MP_RGB(50, 50, 240)); 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; } DebugData<true> ProduceDebugInfo(const InputVertex& input, int num_attributes, const Regs::ShaderConfig& config, const ShaderSetup& setup) { UnitState<true> state; 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; float24 dummy_register; boost::fill(state.registers.input, &dummy_register); if (num_attributes > 0) state.registers.input[attribute_register_map.attribute0_register] = &input.attr[0].x; if (num_attributes > 1) state.registers.input[attribute_register_map.attribute1_register] = &input.attr[1].x; if (num_attributes > 2) state.registers.input[attribute_register_map.attribute2_register] = &input.attr[2].x; if (num_attributes > 3) state.registers.input[attribute_register_map.attribute3_register] = &input.attr[3].x; if (num_attributes > 4) state.registers.input[attribute_register_map.attribute4_register] = &input.attr[4].x; if (num_attributes > 5) state.registers.input[attribute_register_map.attribute5_register] = &input.attr[5].x; if (num_attributes > 6) state.registers.input[attribute_register_map.attribute6_register] = &input.attr[6].x; if (num_attributes > 7) state.registers.input[attribute_register_map.attribute7_register] = &input.attr[7].x; if (num_attributes > 8) state.registers.input[attribute_register_map.attribute8_register] = &input.attr[8].x; if (num_attributes > 9) state.registers.input[attribute_register_map.attribute9_register] = &input.attr[9].x; if (num_attributes > 10) state.registers.input[attribute_register_map.attribute10_register] = &input.attr[10].x; if (num_attributes > 11) state.registers.input[attribute_register_map.attribute11_register] = &input.attr[11].x; if (num_attributes > 12) state.registers.input[attribute_register_map.attribute12_register] = &input.attr[12].x; if (num_attributes > 13) state.registers.input[attribute_register_map.attribute13_register] = &input.attr[13].x; if (num_attributes > 14) state.registers.input[attribute_register_map.attribute14_register] = &input.attr[14].x; if (num_attributes > 15) state.registers.input[attribute_register_map.attribute15_register] = &input.attr[15].x; state.conditional_code[0] = false; state.conditional_code[1] = false; RunInterpreter(state); return state.debug; } } // namespace Shader