diff options
-rw-r--r-- | src/citra/default_ini.h | 6 | ||||
-rw-r--r-- | src/core/hle/service/ldr_ro/ldr_ro.cpp | 331 | ||||
-rw-r--r-- | src/input_common/sdl/sdl.cpp | 57 | ||||
-rw-r--r-- | src/video_core/renderer_opengl/gl_rasterizer.cpp | 3 | ||||
-rw-r--r-- | src/video_core/renderer_opengl/gl_rasterizer.h | 201 | ||||
-rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.cpp | 92 | ||||
-rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.h | 122 | ||||
-rw-r--r-- | src/video_core/renderer_opengl/gl_shader_util.h | 11 |
8 files changed, 412 insertions, 411 deletions
diff --git a/src/citra/default_ini.h b/src/citra/default_ini.h index 084372df4..d8a8fe44f 100644 --- a/src/citra/default_ini.h +++ b/src/citra/default_ini.h @@ -19,7 +19,13 @@ const char* sdl2_config_file = R"( # - "joystick": the index of the joystick to bind # - "button"(optional): the index of the button to bind # - "hat"(optional): the index of the hat to bind as direction buttons +# - "axis"(optional): the index of the axis to bind # - "direction"(only used for hat): the direction name of the hat to bind. Can be "up", "down", "left" or "right" +# - "threshould"(only used for axis): a float value in (-1.0, 1.0) which the button is +# triggered if the axis value crosses +# - "direction"(only used for axis): "+" means the button is triggered when the axis value +# is greater than the threshold; "-" means the button is triggered when the axis value +# is smaller than the threshold button_a= button_b= button_x= diff --git a/src/core/hle/service/ldr_ro/ldr_ro.cpp b/src/core/hle/service/ldr_ro/ldr_ro.cpp index 7af76676b..d1e6d869f 100644 --- a/src/core/hle/service/ldr_ro/ldr_ro.cpp +++ b/src/core/hle/service/ldr_ro/ldr_ro.cpp @@ -40,9 +40,6 @@ static const ResultCode ERROR_INVALID_MEMORY_STATE = // 0xD8A12C08 static const ResultCode ERROR_NOT_LOADED = // 0xD8A12C0D ResultCode(static_cast<ErrorDescription>(13), ErrorModule::RO, ErrorSummary::InvalidState, ErrorLevel::Permanent); -static const ResultCode ERROR_INVALID_DESCRIPTOR = // 0xD9001830 - ResultCode(ErrorDescription::OS_InvalidBufferDescriptor, ErrorModule::OS, - ErrorSummary::WrongArgument, ErrorLevel::Permanent); static MemorySynchronizer memory_synchronizer; @@ -71,66 +68,61 @@ static bool VerifyBufferState(VAddr buffer_ptr, u32 size) { * 1 : Result of function, 0 on success, otherwise error code */ static void Initialize(Interface* self) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - VAddr crs_buffer_ptr = cmd_buff[1]; - u32 crs_size = cmd_buff[2]; - VAddr crs_address = cmd_buff[3]; - u32 descriptor = cmd_buff[4]; - u32 process = cmd_buff[5]; - - LOG_DEBUG(Service_LDR, "called, crs_buffer_ptr=0x%08X, crs_address=0x%08X, crs_size=0x%X, " - "descriptor=0x%08X, process=0x%08X", - crs_buffer_ptr, crs_address, crs_size, descriptor, process); - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x01, 3, 2); + VAddr crs_buffer_ptr = rp.Pop<u32>(); + u32 crs_size = rp.Pop<u32>(); + VAddr crs_address = rp.Pop<u32>(); + // TODO (wwylele): RO service checks the descriptor here and return error 0xD9001830 for + // incorrect descriptor. This error return should be probably built in IPC::RequestParser. + // All other service functions below have the same issue. + Kernel::Handle process = rp.PopHandle(); + + LOG_DEBUG(Service_LDR, + "called, crs_buffer_ptr=0x%08X, crs_address=0x%08X, crs_size=0x%X, process=0x%08X", + crs_buffer_ptr, crs_address, crs_size, process); - cmd_buff[0] = IPC::MakeHeader(1, 1, 0); + IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); if (loaded_crs != 0) { LOG_ERROR(Service_LDR, "Already initialized"); - cmd_buff[1] = ERROR_ALREADY_INITIALIZED.raw; + rb.Push(ERROR_ALREADY_INITIALIZED); return; } if (crs_size < CRO_HEADER_SIZE) { LOG_ERROR(Service_LDR, "CRS is too small"); - cmd_buff[1] = ERROR_BUFFER_TOO_SMALL.raw; + rb.Push(ERROR_BUFFER_TOO_SMALL); return; } if (crs_buffer_ptr & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRS original address is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_ADDRESS.raw; + rb.Push(ERROR_MISALIGNED_ADDRESS); return; } if (crs_address & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRS mapping address is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_ADDRESS.raw; + rb.Push(ERROR_MISALIGNED_ADDRESS); return; } if (crs_size & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRS size is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_SIZE.raw; + rb.Push(ERROR_MISALIGNED_SIZE); return; } if (!VerifyBufferState(crs_buffer_ptr, crs_size)) { LOG_ERROR(Service_LDR, "CRS original buffer is in invalid state"); - cmd_buff[1] = ERROR_INVALID_MEMORY_STATE.raw; + rb.Push(ERROR_INVALID_MEMORY_STATE); return; } if (crs_address < Memory::PROCESS_IMAGE_VADDR || crs_address + crs_size > Memory::PROCESS_IMAGE_VADDR_END) { LOG_ERROR(Service_LDR, "CRS mapping address is not in the process image region"); - cmd_buff[1] = ERROR_ILLEGAL_ADDRESS.raw; + rb.Push(ERROR_ILLEGAL_ADDRESS); return; } @@ -145,7 +137,7 @@ static void Initialize(Interface* self) { .Code(); if (result.IsError()) { LOG_ERROR(Service_LDR, "Error mapping memory block %08X", result.raw); - cmd_buff[1] = result.raw; + rb.Push(result); return; } @@ -153,7 +145,7 @@ static void Initialize(Interface* self) { Kernel::VMAPermission::Read); if (result.IsError()) { LOG_ERROR(Service_LDR, "Error reprotecting memory block %08X", result.raw); - cmd_buff[1] = result.raw; + rb.Push(result); return; } @@ -172,7 +164,7 @@ static void Initialize(Interface* self) { result = crs.Rebase(0, crs_size, 0, 0, 0, 0, true); if (result.IsError()) { LOG_ERROR(Service_LDR, "Error rebasing CRS 0x%08X", result.raw); - cmd_buff[1] = result.raw; + rb.Push(result); return; } @@ -180,7 +172,7 @@ static void Initialize(Interface* self) { loaded_crs = crs_address; - cmd_buff[1] = RESULT_SUCCESS.raw; + rb.Push(RESULT_SUCCESS); } /** @@ -196,25 +188,17 @@ static void Initialize(Interface* self) { * 1 : Result of function, 0 on success, otherwise error code */ static void LoadCRR(Interface* self) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - u32 crr_buffer_ptr = cmd_buff[1]; - u32 crr_size = cmd_buff[2]; - u32 descriptor = cmd_buff[3]; - u32 process = cmd_buff[4]; - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x02, 2, 2); + VAddr crr_buffer_ptr = rp.Pop<u32>(); + u32 crr_size = rp.Pop<u32>(); + Kernel::Handle process = rp.PopHandle(); - cmd_buff[0] = IPC::MakeHeader(2, 1, 0); - cmd_buff[1] = RESULT_SUCCESS.raw; // No error + IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); + rb.Push(RESULT_SUCCESS); - LOG_WARNING(Service_LDR, "(STUBBED) called, crr_buffer_ptr=0x%08X, crr_size=0x%08X, " - "descriptor=0x%08X, process=0x%08X", - crr_buffer_ptr, crr_size, descriptor, process); + LOG_WARNING(Service_LDR, + "(STUBBED) called, crr_buffer_ptr=0x%08X, crr_size=0x%08X, process=0x%08X", + crr_buffer_ptr, crr_size, process); } /** @@ -229,24 +213,15 @@ static void LoadCRR(Interface* self) { * 1 : Result of function, 0 on success, otherwise error code */ static void UnloadCRR(Interface* self) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - u32 crr_buffer_ptr = cmd_buff[1]; - u32 descriptor = cmd_buff[2]; - u32 process = cmd_buff[3]; - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x03, 1, 2); + u32 crr_buffer_ptr = rp.Pop<u32>(); + Kernel::Handle process = rp.PopHandle(); - cmd_buff[0] = IPC::MakeHeader(3, 1, 0); - cmd_buff[1] = RESULT_SUCCESS.raw; // No error + IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); + rb.Push(RESULT_SUCCESS); - LOG_WARNING(Service_LDR, - "(STUBBED) called, crr_buffer_ptr=0x%08X, descriptor=0x%08X, process=0x%08X", - crr_buffer_ptr, descriptor, process); + LOG_WARNING(Service_LDR, "(STUBBED) called, crr_buffer_ptr=0x%08X, process=0x%08X", + crr_buffer_ptr, process); } /** @@ -276,87 +251,85 @@ static void UnloadCRR(Interface* self) { * There is a dispatcher template below. */ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - VAddr cro_buffer_ptr = cmd_buff[1]; - VAddr cro_address = cmd_buff[2]; - u32 cro_size = cmd_buff[3]; - VAddr data_segment_address = cmd_buff[4]; - u32 zero = cmd_buff[5]; - u32 data_segment_size = cmd_buff[6]; - u32 bss_segment_address = cmd_buff[7]; - u32 bss_segment_size = cmd_buff[8]; - bool auto_link = (cmd_buff[9] & 0xFF) != 0; - u32 fix_level = cmd_buff[10]; - VAddr crr_address = cmd_buff[11]; - u32 descriptor = cmd_buff[12]; - u32 process = cmd_buff[13]; - - LOG_DEBUG(Service_LDR, - "called (%s), cro_buffer_ptr=0x%08X, cro_address=0x%08X, cro_size=0x%X, " - "data_segment_address=0x%08X, zero=%d, data_segment_size=0x%X, " - "bss_segment_address=0x%08X, bss_segment_size=0x%X, " - "auto_link=%s, fix_level=%d, crr_address=0x%08X, descriptor=0x%08X, process=0x%08X", + IPC::RequestParser rp(Kernel::GetCommandBuffer(), link_on_load_bug_fix ? 0x09 : 0x04, 11, 2); + VAddr cro_buffer_ptr = rp.Pop<u32>(); + VAddr cro_address = rp.Pop<u32>(); + u32 cro_size = rp.Pop<u32>(); + VAddr data_segment_address = rp.Pop<u32>(); + u32 zero = rp.Pop<u32>(); + u32 data_segment_size = rp.Pop<u32>(); + u32 bss_segment_address = rp.Pop<u32>(); + u32 bss_segment_size = rp.Pop<u32>(); + bool auto_link = rp.Pop<bool>(); + u32 fix_level = rp.Pop<u32>(); + VAddr crr_address = rp.Pop<u32>(); + Kernel::Handle process = rp.PopHandle(); + + LOG_DEBUG(Service_LDR, "called (%s), cro_buffer_ptr=0x%08X, cro_address=0x%08X, cro_size=0x%X, " + "data_segment_address=0x%08X, zero=%d, data_segment_size=0x%X, " + "bss_segment_address=0x%08X, bss_segment_size=0x%X, auto_link=%s, " + "fix_level=%d, crr_address=0x%08X, process=0x%08X", link_on_load_bug_fix ? "new" : "old", cro_buffer_ptr, cro_address, cro_size, data_segment_address, zero, data_segment_size, bss_segment_address, bss_segment_size, - auto_link ? "true" : "false", fix_level, crr_address, descriptor, process); - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + auto_link ? "true" : "false", fix_level, crr_address, process); - cmd_buff[0] = IPC::MakeHeader(link_on_load_bug_fix ? 9 : 4, 2, 0); + IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); if (loaded_crs == 0) { LOG_ERROR(Service_LDR, "Not initialized"); - cmd_buff[1] = ERROR_NOT_INITIALIZED.raw; + rb.Push(ERROR_NOT_INITIALIZED); + rb.Push<u32>(0); return; } if (cro_size < CRO_HEADER_SIZE) { LOG_ERROR(Service_LDR, "CRO too small"); - cmd_buff[1] = ERROR_BUFFER_TOO_SMALL.raw; + rb.Push(ERROR_BUFFER_TOO_SMALL); + rb.Push<u32>(0); return; } if (cro_buffer_ptr & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRO original address is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_ADDRESS.raw; + rb.Push(ERROR_MISALIGNED_ADDRESS); + rb.Push<u32>(0); return; } if (cro_address & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRO mapping address is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_ADDRESS.raw; + rb.Push(ERROR_MISALIGNED_ADDRESS); + rb.Push<u32>(0); return; } if (cro_size & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRO size is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_SIZE.raw; + rb.Push(ERROR_MISALIGNED_SIZE); + rb.Push<u32>(0); return; } if (!VerifyBufferState(cro_buffer_ptr, cro_size)) { LOG_ERROR(Service_LDR, "CRO original buffer is in invalid state"); - cmd_buff[1] = ERROR_INVALID_MEMORY_STATE.raw; + rb.Push(ERROR_INVALID_MEMORY_STATE); + rb.Push<u32>(0); return; } if (cro_address < Memory::PROCESS_IMAGE_VADDR || cro_address + cro_size > Memory::PROCESS_IMAGE_VADDR_END) { LOG_ERROR(Service_LDR, "CRO mapping address is not in the process image region"); - cmd_buff[1] = ERROR_ILLEGAL_ADDRESS.raw; + rb.Push(ERROR_ILLEGAL_ADDRESS); + rb.Push<u32>(0); return; } if (zero) { LOG_ERROR(Service_LDR, "Zero is not zero %d", zero); - cmd_buff[1] = ResultCode(static_cast<ErrorDescription>(29), ErrorModule::RO, - ErrorSummary::Internal, ErrorLevel::Usage) - .raw; + rb.Push(ResultCode(static_cast<ErrorDescription>(29), ErrorModule::RO, + ErrorSummary::Internal, ErrorLevel::Usage)); + rb.Push<u32>(0); return; } @@ -371,7 +344,8 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { .Code(); if (result.IsError()) { LOG_ERROR(Service_LDR, "Error mapping memory block %08X", result.raw); - cmd_buff[1] = result.raw; + rb.Push(result); + rb.Push<u32>(0); return; } @@ -380,7 +354,8 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { if (result.IsError()) { LOG_ERROR(Service_LDR, "Error reprotecting memory block %08X", result.raw); Kernel::g_current_process->vm_manager.UnmapRange(cro_address, cro_size); - cmd_buff[1] = result.raw; + rb.Push(result); + rb.Push<u32>(0); return; } @@ -400,7 +375,8 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { if (result.IsError()) { LOG_ERROR(Service_LDR, "Error verifying CRO in CRR %08X", result.raw); Kernel::g_current_process->vm_manager.UnmapRange(cro_address, cro_size); - cmd_buff[1] = result.raw; + rb.Push(result); + rb.Push<u32>(0); return; } @@ -409,7 +385,8 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { if (result.IsError()) { LOG_ERROR(Service_LDR, "Error rebasing CRO %08X", result.raw); Kernel::g_current_process->vm_manager.UnmapRange(cro_address, cro_size); - cmd_buff[1] = result.raw; + rb.Push(result); + rb.Push<u32>(0); return; } @@ -417,7 +394,8 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { if (result.IsError()) { LOG_ERROR(Service_LDR, "Error linking CRO %08X", result.raw); Kernel::g_current_process->vm_manager.UnmapRange(cro_address, cro_size); - cmd_buff[1] = result.raw; + rb.Push(result); + rb.Push<u32>(0); return; } @@ -435,7 +413,8 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { if (result.IsError()) { LOG_ERROR(Service_LDR, "Error unmapping memory block %08X", result.raw); Kernel::g_current_process->vm_manager.UnmapRange(cro_address, cro_size); - cmd_buff[1] = result.raw; + rb.Push(result); + rb.Push<u32>(0); return; } } @@ -453,7 +432,8 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { if (result.IsError()) { LOG_ERROR(Service_LDR, "Error reprotecting memory block %08X", result.raw); Kernel::g_current_process->vm_manager.UnmapRange(cro_address, fix_size); - cmd_buff[1] = result.raw; + rb.Push(result); + rb.Push<u32>(0); return; } } @@ -463,8 +443,7 @@ static void LoadCRO(Interface* self, bool link_on_load_bug_fix) { LOG_INFO(Service_LDR, "CRO \"%s\" loaded at 0x%08X, fixed_end=0x%08X", cro.ModuleName().data(), cro_address, cro_address + fix_size); - cmd_buff[1] = RESULT_SUCCESS.raw; - cmd_buff[2] = fix_size; + rb.Push(RESULT_SUCCESS, fix_size); } template <bool link_on_load_bug_fix> @@ -486,43 +465,35 @@ static void LoadCRO(Interface* self) { * 1 : Result of function, 0 on success, otherwise error code */ static void UnloadCRO(Interface* self) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - VAddr cro_address = cmd_buff[1]; - u32 zero = cmd_buff[2]; - VAddr cro_buffer_ptr = cmd_buff[3]; - u32 descriptor = cmd_buff[4]; - u32 process = cmd_buff[5]; - - LOG_DEBUG(Service_LDR, "called, cro_address=0x%08X, zero=%d, cro_buffer_ptr=0x%08X, " - "descriptor=0x%08X, process=0x%08X", - cro_address, zero, cro_buffer_ptr, descriptor, process); - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x05, 3, 2); + VAddr cro_address = rp.Pop<u32>(); + u32 zero = rp.Pop<u32>(); + VAddr cro_buffer_ptr = rp.Pop<u32>(); + Kernel::Handle process = rp.PopHandle(); + + LOG_DEBUG(Service_LDR, + "called, cro_address=0x%08X, zero=%d, cro_buffer_ptr=0x%08X, process=0x%08X", + cro_address, zero, cro_buffer_ptr, process); CROHelper cro(cro_address); - cmd_buff[0] = IPC::MakeHeader(5, 1, 0); + IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); if (loaded_crs == 0) { LOG_ERROR(Service_LDR, "Not initialized"); - cmd_buff[1] = ERROR_NOT_INITIALIZED.raw; + rb.Push(ERROR_NOT_INITIALIZED); return; } if (cro_address & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRO address is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_ADDRESS.raw; + rb.Push(ERROR_MISALIGNED_ADDRESS); return; } if (!cro.IsLoaded()) { LOG_ERROR(Service_LDR, "Invalid or not loaded CRO"); - cmd_buff[1] = ERROR_NOT_LOADED.raw; + rb.Push(ERROR_NOT_LOADED); return; } @@ -535,7 +506,7 @@ static void UnloadCRO(Interface* self) { ResultCode result = cro.Unlink(loaded_crs); if (result.IsError()) { LOG_ERROR(Service_LDR, "Error unlinking CRO %08X", result.raw); - cmd_buff[1] = result.raw; + rb.Push(result); return; } @@ -545,7 +516,7 @@ static void UnloadCRO(Interface* self) { result = cro.ClearRelocations(); if (result.IsError()) { LOG_ERROR(Service_LDR, "Error clearing relocations %08X", result.raw); - cmd_buff[1] = result.raw; + rb.Push(result); return; } } @@ -565,7 +536,7 @@ static void UnloadCRO(Interface* self) { Core::CPU().ClearInstructionCache(); - cmd_buff[1] = result.raw; + rb.Push(result); } /** @@ -580,40 +551,31 @@ static void UnloadCRO(Interface* self) { * 1 : Result of function, 0 on success, otherwise error code */ static void LinkCRO(Interface* self) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - VAddr cro_address = cmd_buff[1]; - u32 descriptor = cmd_buff[2]; - u32 process = cmd_buff[3]; - - LOG_DEBUG(Service_LDR, "called, cro_address=0x%08X, descriptor=0x%08X, process=0x%08X", - cro_address, descriptor, process); - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x06, 1, 2); + VAddr cro_address = rp.Pop<u32>(); + Kernel::Handle process = rp.PopHandle(); + + LOG_DEBUG(Service_LDR, "called, cro_address=0x%08X, process=0x%08X", cro_address, process); CROHelper cro(cro_address); - cmd_buff[0] = IPC::MakeHeader(6, 1, 0); + IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); if (loaded_crs == 0) { LOG_ERROR(Service_LDR, "Not initialized"); - cmd_buff[1] = ERROR_NOT_INITIALIZED.raw; + rb.Push(ERROR_NOT_INITIALIZED); return; } if (cro_address & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRO address is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_ADDRESS.raw; + rb.Push(ERROR_MISALIGNED_ADDRESS); return; } if (!cro.IsLoaded()) { LOG_ERROR(Service_LDR, "Invalid or not loaded CRO"); - cmd_buff[1] = ERROR_NOT_LOADED.raw; + rb.Push(ERROR_NOT_LOADED); return; } @@ -627,7 +589,7 @@ static void LinkCRO(Interface* self) { memory_synchronizer.SynchronizeOriginalMemory(); Core::CPU().ClearInstructionCache(); - cmd_buff[1] = result.raw; + rb.Push(result); } /** @@ -642,40 +604,31 @@ static void LinkCRO(Interface* self) { * 1 : Result of function, 0 on success, otherwise error code */ static void UnlinkCRO(Interface* self) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - VAddr cro_address = cmd_buff[1]; - u32 descriptor = cmd_buff[2]; - u32 process = cmd_buff[3]; - - LOG_DEBUG(Service_LDR, "called, cro_address=0x%08X, descriptor=0x%08X, process=0x%08X", - cro_address, descriptor, process); - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x07, 1, 2); + VAddr cro_address = rp.Pop<u32>(); + Kernel::Handle process = rp.PopHandle(); + + LOG_DEBUG(Service_LDR, "called, cro_address=0x%08X, process=0x%08X", cro_address, process); CROHelper cro(cro_address); - cmd_buff[0] = IPC::MakeHeader(7, 1, 0); + IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); if (loaded_crs == 0) { LOG_ERROR(Service_LDR, "Not initialized"); - cmd_buff[1] = ERROR_NOT_INITIALIZED.raw; + rb.Push(ERROR_NOT_INITIALIZED); return; } if (cro_address & Memory::PAGE_MASK) { LOG_ERROR(Service_LDR, "CRO address is not aligned"); - cmd_buff[1] = ERROR_MISALIGNED_ADDRESS.raw; + rb.Push(ERROR_MISALIGNED_ADDRESS); return; } if (!cro.IsLoaded()) { LOG_ERROR(Service_LDR, "Invalid or not loaded CRO"); - cmd_buff[1] = ERROR_NOT_LOADED.raw; + rb.Push(ERROR_NOT_LOADED); return; } @@ -689,7 +642,7 @@ static void UnlinkCRO(Interface* self) { memory_synchronizer.SynchronizeOriginalMemory(); Core::CPU().ClearInstructionCache(); - cmd_buff[1] = result.raw; + rb.Push(result); } /** @@ -704,29 +657,21 @@ static void UnlinkCRO(Interface* self) { * 1 : Result of function, 0 on success, otherwise error code */ static void Shutdown(Interface* self) { - u32* cmd_buff = Kernel::GetCommandBuffer(); - VAddr crs_buffer_ptr = cmd_buff[1]; - u32 descriptor = cmd_buff[2]; - u32 process = cmd_buff[3]; - - LOG_DEBUG(Service_LDR, "called, crs_buffer_ptr=0x%08X, descriptor=0x%08X, process=0x%08X", - crs_buffer_ptr, descriptor, process); - - if (descriptor != 0) { - LOG_ERROR(Service_LDR, "IPC handle descriptor failed validation (0x%X)", descriptor); - cmd_buff[0] = IPC::MakeHeader(0, 1, 0); - cmd_buff[1] = ERROR_INVALID_DESCRIPTOR.raw; - return; - } + IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x08, 1, 2); + VAddr crs_buffer_ptr = rp.Pop<u32>(); + Kernel::Handle process = rp.PopHandle(); + + LOG_DEBUG(Service_LDR, "called, crs_buffer_ptr=0x%08X, process=0x%08X", crs_buffer_ptr, + process); + + IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); if (loaded_crs == 0) { LOG_ERROR(Service_LDR, "Not initialized"); - cmd_buff[1] = ERROR_NOT_INITIALIZED.raw; + rb.Push(ERROR_NOT_INITIALIZED); return; } - cmd_buff[0] = IPC::MakeHeader(8, 1, 0); - CROHelper crs(loaded_crs); crs.Unrebase(true); @@ -744,7 +689,7 @@ static void Shutdown(Interface* self) { } loaded_crs = 0; - cmd_buff[1] = result.raw; + rb.Push(result); } const Interface::FunctionInfo FunctionTable[] = { diff --git a/src/input_common/sdl/sdl.cpp b/src/input_common/sdl/sdl.cpp index ae0206909..756ee58b7 100644 --- a/src/input_common/sdl/sdl.cpp +++ b/src/input_common/sdl/sdl.cpp @@ -8,6 +8,7 @@ #include <tuple> #include <unordered_map> #include <SDL.h> +#include "common/logging/log.h" #include "common/math_util.h" #include "input_common/sdl/sdl.h" @@ -40,12 +41,16 @@ public: return SDL_JoystickGetButton(joystick.get(), button) == 1; } - std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const { + float GetAxis(int axis) const { if (!joystick) return {}; SDL_JoystickUpdate(); - float x = SDL_JoystickGetAxis(joystick.get(), axis_x) / 32767.0f; - float y = SDL_JoystickGetAxis(joystick.get(), axis_y) / 32767.0f; + return SDL_JoystickGetAxis(joystick.get(), axis) / 32767.0f; + } + + std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const { + float x = GetAxis(axis_x); + float y = GetAxis(axis_y); y = -y; // 3DS uses an y-axis inverse from SDL // Make sure the coordinates are in the unit circle, @@ -97,6 +102,27 @@ private: Uint8 direction; }; +class SDLAxisButton final : public Input::ButtonDevice { +public: + explicit SDLAxisButton(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_, + bool trigger_if_greater_) + : joystick(joystick_), axis(axis_), threshold(threshold_), + trigger_if_greater(trigger_if_greater_) {} + + bool GetStatus() const override { + float axis_value = joystick->GetAxis(axis); + if (trigger_if_greater) + return axis_value > threshold; + return axis_value < threshold; + } + +private: + std::shared_ptr<SDLJoystick> joystick; + int axis; + float threshold; + bool trigger_if_greater; +}; + class SDLAnalog final : public Input::AnalogDevice { public: SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_) @@ -130,8 +156,14 @@ public: * - "joystick": the index of the joystick to bind * - "button"(optional): the index of the button to bind * - "hat"(optional): the index of the hat to bind as direction buttons + * - "axis"(optional): the index of the axis to bind * - "direction"(only used for hat): the direction name of the hat to bind. Can be "up", - * "down", "left" or "right" + * "down", "left" or "right" + * - "threshould"(only used for axis): a float value in (-1.0, 1.0) which the button is + * triggered if the axis value crosses + * - "direction"(only used for axis): "+" means the button is triggered when the axis value + * is greater than the threshold; "-" means the button is triggered when the axis value + * is smaller than the threshold */ std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override { const int joystick_index = params.Get("joystick", 0); @@ -155,6 +187,23 @@ public: direction); } + if (params.Has("axis")) { + const int axis = params.Get("axis", 0); + const float threshold = params.Get("threshold", 0.5f); + const std::string direction_name = params.Get("direction", ""); + bool trigger_if_greater; + if (direction_name == "+") { + trigger_if_greater = true; + } else if (direction_name == "-") { + trigger_if_greater = false; + } else { + trigger_if_greater = true; + LOG_ERROR(Input, "Unknown direction %s", direction_name.c_str()); + } + return std::make_unique<SDLAxisButton>(GetJoystick(joystick_index), axis, threshold, + trigger_if_greater); + } + const int button = params.Get("button", 0); return std::make_unique<SDLButton>(GetJoystick(joystick_index), button); } diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index de1d5eba7..a47307099 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -20,7 +20,6 @@ #include "video_core/regs_texturing.h" #include "video_core/renderer_opengl/gl_rasterizer.h" #include "video_core/renderer_opengl/gl_shader_gen.h" -#include "video_core/renderer_opengl/gl_shader_util.h" #include "video_core/renderer_opengl/pica_to_gl.h" #include "video_core/renderer_opengl/renderer_opengl.h" @@ -1005,7 +1004,7 @@ void RasterizerOpenGL::SamplerInfo::SyncWithConfig( } void RasterizerOpenGL::SetShader() { - PicaShaderConfig config = PicaShaderConfig::CurrentConfig(); + auto config = GLShader::PicaShaderConfig::BuildFromRegs(Pica::g_state.regs); std::unique_ptr<PicaShader> shader = std::make_unique<PicaShader>(); // Find (or generate) the GLSL shader for the current TEV state diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index ecf737438..3e1770d77 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -25,210 +25,13 @@ #include "video_core/regs_texturing.h" #include "video_core/renderer_opengl/gl_rasterizer_cache.h" #include "video_core/renderer_opengl/gl_resource_manager.h" +#include "video_core/renderer_opengl/gl_shader_gen.h" #include "video_core/renderer_opengl/gl_state.h" #include "video_core/renderer_opengl/pica_to_gl.h" #include "video_core/shader/shader.h" struct ScreenInfo; -/** - * This struct contains all state used to generate the GLSL shader program that emulates the current - * Pica register configuration. This struct is used as a cache key for generated GLSL shader - * programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by - * directly accessing Pica registers. This should reduce the risk of bugs in shader generation where - * Pica state is not being captured in the shader cache key, thereby resulting in (what should be) - * two separate shaders sharing the same key. - * - * We use a union because "implicitly-defined copy/move constructor for a union X copies the object - * representation of X." and "implicitly-defined copy assignment operator for a union X copies the - * object representation (3.9) of X." = Bytewise copy instead of memberwise copy. This is important - * because the padding bytes are included in the hash and comparison between objects. - */ -union PicaShaderConfig { - - /// Construct a PicaShaderConfig with the current Pica register configuration. - static PicaShaderConfig CurrentConfig() { - PicaShaderConfig res; - - auto& state = res.state; - std::memset(&state, 0, sizeof(PicaShaderConfig::State)); - - const auto& regs = Pica::g_state.regs; - - state.scissor_test_mode = regs.rasterizer.scissor_test.mode; - - state.depthmap_enable = regs.rasterizer.depthmap_enable; - - state.alpha_test_func = regs.framebuffer.output_merger.alpha_test.enable - ? regs.framebuffer.output_merger.alpha_test.func.Value() - : Pica::FramebufferRegs::CompareFunc::Always; - - state.texture0_type = regs.texturing.texture0.type; - - // Copy relevant tev stages fields. - // We don't sync const_color here because of the high variance, it is a - // shader uniform instead. - const auto& tev_stages = regs.texturing.GetTevStages(); - DEBUG_ASSERT(state.tev_stages.size() == tev_stages.size()); - for (size_t i = 0; i < tev_stages.size(); i++) { - const auto& tev_stage = tev_stages[i]; - state.tev_stages[i].sources_raw = tev_stage.sources_raw; - state.tev_stages[i].modifiers_raw = tev_stage.modifiers_raw; - state.tev_stages[i].ops_raw = tev_stage.ops_raw; - state.tev_stages[i].scales_raw = tev_stage.scales_raw; - } - - state.fog_mode = regs.texturing.fog_mode; - state.fog_flip = regs.texturing.fog_flip != 0; - - state.combiner_buffer_input = - regs.texturing.tev_combiner_buffer_input.update_mask_rgb.Value() | - regs.texturing.tev_combiner_buffer_input.update_mask_a.Value() << 4; - - // Fragment lighting - - state.lighting.enable = !regs.lighting.disable; - state.lighting.src_num = regs.lighting.max_light_index + 1; - - for (unsigned light_index = 0; light_index < state.lighting.src_num; ++light_index) { - unsigned num = regs.lighting.light_enable.GetNum(light_index); - const auto& light = regs.lighting.light[num]; - state.lighting.light[light_index].num = num; - state.lighting.light[light_index].directional = light.config.directional != 0; - state.lighting.light[light_index].two_sided_diffuse = - light.config.two_sided_diffuse != 0; - state.lighting.light[light_index].dist_atten_enable = - !regs.lighting.IsDistAttenDisabled(num); - } - - state.lighting.lut_d0.enable = regs.lighting.config1.disable_lut_d0 == 0; - state.lighting.lut_d0.abs_input = regs.lighting.abs_lut_input.disable_d0 == 0; - state.lighting.lut_d0.type = regs.lighting.lut_input.d0.Value(); - state.lighting.lut_d0.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d0); - - state.lighting.lut_d1.enable = regs.lighting.config1.disable_lut_d1 == 0; - state.lighting.lut_d1.abs_input = regs.lighting.abs_lut_input.disable_d1 == 0; - state.lighting.lut_d1.type = regs.lighting.lut_input.d1.Value(); - state.lighting.lut_d1.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d1); - - state.lighting.lut_fr.enable = regs.lighting.config1.disable_lut_fr == 0; - state.lighting.lut_fr.abs_input = regs.lighting.abs_lut_input.disable_fr == 0; - state.lighting.lut_fr.type = regs.lighting.lut_input.fr.Value(); - state.lighting.lut_fr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.fr); - - state.lighting.lut_rr.enable = regs.lighting.config1.disable_lut_rr == 0; - state.lighting.lut_rr.abs_input = regs.lighting.abs_lut_input.disable_rr == 0; - state.lighting.lut_rr.type = regs.lighting.lut_input.rr.Value(); - state.lighting.lut_rr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rr); - - state.lighting.lut_rg.enable = regs.lighting.config1.disable_lut_rg == 0; - state.lighting.lut_rg.abs_input = regs.lighting.abs_lut_input.disable_rg == 0; - state.lighting.lut_rg.type = regs.lighting.lut_input.rg.Value(); - state.lighting.lut_rg.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rg); - - state.lighting.lut_rb.enable = regs.lighting.config1.disable_lut_rb == 0; - state.lighting.lut_rb.abs_input = regs.lighting.abs_lut_input.disable_rb == 0; - state.lighting.lut_rb.type = regs.lighting.lut_input.rb.Value(); - state.lighting.lut_rb.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rb); - - state.lighting.config = regs.lighting.config0.config; - state.lighting.fresnel_selector = regs.lighting.config0.fresnel_selector; - state.lighting.bump_mode = regs.lighting.config0.bump_mode; - state.lighting.bump_selector = regs.lighting.config0.bump_selector; - state.lighting.bump_renorm = regs.lighting.config0.disable_bump_renorm == 0; - state.lighting.clamp_highlights = regs.lighting.config0.clamp_highlights != 0; - - return res; - } - - bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const { - return (stage_index < 4) && (state.combiner_buffer_input & (1 << stage_index)); - } - - bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const { - return (stage_index < 4) && ((state.combiner_buffer_input >> 4) & (1 << stage_index)); - } - - bool operator==(const PicaShaderConfig& o) const { - return std::memcmp(&state, &o.state, sizeof(PicaShaderConfig::State)) == 0; - }; - - // NOTE: MSVC15 (Update 2) doesn't think `delete`'d constructors and operators are TC. - // This makes BitField not TC when used in a union or struct so we have to resort - // to this ugly hack. - // Once that bug is fixed we can use Pica::Regs::TevStageConfig here. - // Doesn't include const_color because we don't sync it, see comment in CurrentConfig() - struct TevStageConfigRaw { - u32 sources_raw; - u32 modifiers_raw; - u32 ops_raw; - u32 scales_raw; - explicit operator Pica::TexturingRegs::TevStageConfig() const noexcept { - Pica::TexturingRegs::TevStageConfig stage; - stage.sources_raw = sources_raw; - stage.modifiers_raw = modifiers_raw; - stage.ops_raw = ops_raw; - stage.const_color = 0; - stage.scales_raw = scales_raw; - return stage; - } - }; - - struct State { - Pica::FramebufferRegs::CompareFunc alpha_test_func; - Pica::RasterizerRegs::ScissorMode scissor_test_mode; - Pica::TexturingRegs::TextureConfig::TextureType texture0_type; - std::array<TevStageConfigRaw, 6> tev_stages; - u8 combiner_buffer_input; - - Pica::RasterizerRegs::DepthBuffering depthmap_enable; - Pica::TexturingRegs::FogMode fog_mode; - bool fog_flip; - - struct { - struct { - unsigned num; - bool directional; - bool two_sided_diffuse; - bool dist_atten_enable; - } light[8]; - - bool enable; - unsigned src_num; - Pica::LightingRegs::LightingBumpMode bump_mode; - unsigned bump_selector; - bool bump_renorm; - bool clamp_highlights; - - Pica::LightingRegs::LightingConfig config; - Pica::LightingRegs::LightingFresnelSelector fresnel_selector; - - struct { - bool enable; - bool abs_input; - Pica::LightingRegs::LightingLutInput type; - float scale; - } lut_d0, lut_d1, lut_fr, lut_rr, lut_rg, lut_rb; - } lighting; - - } state; -}; -#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) -static_assert(std::is_trivially_copyable<PicaShaderConfig::State>::value, - "PicaShaderConfig::State must be trivially copyable"); -#endif - -namespace std { - -template <> -struct hash<PicaShaderConfig> { - size_t operator()(const PicaShaderConfig& k) const { - return Common::ComputeHash64(&k.state, sizeof(PicaShaderConfig::State)); - } -}; - -} // namespace std - class RasterizerOpenGL : public VideoCore::RasterizerInterface { public: RasterizerOpenGL(); @@ -437,7 +240,7 @@ private: std::vector<HardwareVertex> vertex_batch; - std::unordered_map<PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache; + std::unordered_map<GLShader::PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache; const PicaShader* current_shader = nullptr; bool shader_dirty; diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 193cb13bd..0f889b172 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp @@ -4,6 +4,7 @@ #include <array> #include <cstddef> +#include <cstring> #include "common/assert.h" #include "common/bit_field.h" #include "common/logging/log.h" @@ -23,6 +24,97 @@ using TevStageConfig = TexturingRegs::TevStageConfig; namespace GLShader { +PicaShaderConfig PicaShaderConfig::BuildFromRegs(const Pica::Regs& regs) { + PicaShaderConfig res; + + auto& state = res.state; + std::memset(&state, 0, sizeof(PicaShaderConfig::State)); + + state.scissor_test_mode = regs.rasterizer.scissor_test.mode; + + state.depthmap_enable = regs.rasterizer.depthmap_enable; + + state.alpha_test_func = regs.framebuffer.output_merger.alpha_test.enable + ? regs.framebuffer.output_merger.alpha_test.func.Value() + : Pica::FramebufferRegs::CompareFunc::Always; + + state.texture0_type = regs.texturing.texture0.type; + + // Copy relevant tev stages fields. + // We don't sync const_color here because of the high variance, it is a + // shader uniform instead. + const auto& tev_stages = regs.texturing.GetTevStages(); + DEBUG_ASSERT(state.tev_stages.size() == tev_stages.size()); + for (size_t i = 0; i < tev_stages.size(); i++) { + const auto& tev_stage = tev_stages[i]; + state.tev_stages[i].sources_raw = tev_stage.sources_raw; + state.tev_stages[i].modifiers_raw = tev_stage.modifiers_raw; + state.tev_stages[i].ops_raw = tev_stage.ops_raw; + state.tev_stages[i].scales_raw = tev_stage.scales_raw; + } + + state.fog_mode = regs.texturing.fog_mode; + state.fog_flip = regs.texturing.fog_flip != 0; + + state.combiner_buffer_input = regs.texturing.tev_combiner_buffer_input.update_mask_rgb.Value() | + regs.texturing.tev_combiner_buffer_input.update_mask_a.Value() + << 4; + + // Fragment lighting + + state.lighting.enable = !regs.lighting.disable; + state.lighting.src_num = regs.lighting.max_light_index + 1; + + for (unsigned light_index = 0; light_index < state.lighting.src_num; ++light_index) { + unsigned num = regs.lighting.light_enable.GetNum(light_index); + const auto& light = regs.lighting.light[num]; + state.lighting.light[light_index].num = num; + state.lighting.light[light_index].directional = light.config.directional != 0; + state.lighting.light[light_index].two_sided_diffuse = light.config.two_sided_diffuse != 0; + state.lighting.light[light_index].dist_atten_enable = + !regs.lighting.IsDistAttenDisabled(num); + } + + state.lighting.lut_d0.enable = regs.lighting.config1.disable_lut_d0 == 0; + state.lighting.lut_d0.abs_input = regs.lighting.abs_lut_input.disable_d0 == 0; + state.lighting.lut_d0.type = regs.lighting.lut_input.d0.Value(); + state.lighting.lut_d0.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d0); + + state.lighting.lut_d1.enable = regs.lighting.config1.disable_lut_d1 == 0; + state.lighting.lut_d1.abs_input = regs.lighting.abs_lut_input.disable_d1 == 0; + state.lighting.lut_d1.type = regs.lighting.lut_input.d1.Value(); + state.lighting.lut_d1.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d1); + + state.lighting.lut_fr.enable = regs.lighting.config1.disable_lut_fr == 0; + state.lighting.lut_fr.abs_input = regs.lighting.abs_lut_input.disable_fr == 0; + state.lighting.lut_fr.type = regs.lighting.lut_input.fr.Value(); + state.lighting.lut_fr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.fr); + + state.lighting.lut_rr.enable = regs.lighting.config1.disable_lut_rr == 0; + state.lighting.lut_rr.abs_input = regs.lighting.abs_lut_input.disable_rr == 0; + state.lighting.lut_rr.type = regs.lighting.lut_input.rr.Value(); + state.lighting.lut_rr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rr); + + state.lighting.lut_rg.enable = regs.lighting.config1.disable_lut_rg == 0; + state.lighting.lut_rg.abs_input = regs.lighting.abs_lut_input.disable_rg == 0; + state.lighting.lut_rg.type = regs.lighting.lut_input.rg.Value(); + state.lighting.lut_rg.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rg); + + state.lighting.lut_rb.enable = regs.lighting.config1.disable_lut_rb == 0; + state.lighting.lut_rb.abs_input = regs.lighting.abs_lut_input.disable_rb == 0; + state.lighting.lut_rb.type = regs.lighting.lut_input.rb.Value(); + state.lighting.lut_rb.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rb); + + state.lighting.config = regs.lighting.config0.config; + state.lighting.fresnel_selector = regs.lighting.config0.fresnel_selector; + state.lighting.bump_mode = regs.lighting.config0.bump_mode; + state.lighting.bump_selector = regs.lighting.config0.bump_selector; + state.lighting.bump_renorm = regs.lighting.config0.disable_bump_renorm == 0; + state.lighting.clamp_highlights = regs.lighting.config0.clamp_highlights != 0; + + return res; +} + /// Detects if a TEV stage is configured to be skipped (to avoid generating unnecessary code) static bool IsPassThroughTevStage(const TevStageConfig& stage) { return (stage.color_op == TevStageConfig::Operation::Replace && diff --git a/src/video_core/renderer_opengl/gl_shader_gen.h b/src/video_core/renderer_opengl/gl_shader_gen.h index bef3249cf..921d976a1 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.h +++ b/src/video_core/renderer_opengl/gl_shader_gen.h @@ -4,12 +4,121 @@ #pragma once +#include <array> +#include <cstring> +#include <functional> #include <string> - -union PicaShaderConfig; +#include <type_traits> +#include "video_core/regs.h" namespace GLShader { +enum Attributes { + ATTRIBUTE_POSITION, + ATTRIBUTE_COLOR, + ATTRIBUTE_TEXCOORD0, + ATTRIBUTE_TEXCOORD1, + ATTRIBUTE_TEXCOORD2, + ATTRIBUTE_TEXCOORD0_W, + ATTRIBUTE_NORMQUAT, + ATTRIBUTE_VIEW, +}; + +/** + * This struct contains all state used to generate the GLSL shader program that emulates the current + * Pica register configuration. This struct is used as a cache key for generated GLSL shader + * programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by + * directly accessing Pica registers. This should reduce the risk of bugs in shader generation where + * Pica state is not being captured in the shader cache key, thereby resulting in (what should be) + * two separate shaders sharing the same key. + * + * We use a union because "implicitly-defined copy/move constructor for a union X copies the object + * representation of X." and "implicitly-defined copy assignment operator for a union X copies the + * object representation (3.9) of X." = Bytewise copy instead of memberwise copy. This is important + * because the padding bytes are included in the hash and comparison between objects. + */ +union PicaShaderConfig { + + /// Construct a PicaShaderConfig with the given Pica register configuration. + static PicaShaderConfig BuildFromRegs(const Pica::Regs& regs); + + bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const { + return (stage_index < 4) && (state.combiner_buffer_input & (1 << stage_index)); + } + + bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const { + return (stage_index < 4) && ((state.combiner_buffer_input >> 4) & (1 << stage_index)); + } + + bool operator==(const PicaShaderConfig& o) const { + return std::memcmp(&state, &o.state, sizeof(PicaShaderConfig::State)) == 0; + }; + + // NOTE: MSVC15 (Update 2) doesn't think `delete`'d constructors and operators are TC. + // This makes BitField not TC when used in a union or struct so we have to resort + // to this ugly hack. + // Once that bug is fixed we can use Pica::Regs::TevStageConfig here. + // Doesn't include const_color because we don't sync it, see comment in BuildFromRegs() + struct TevStageConfigRaw { + u32 sources_raw; + u32 modifiers_raw; + u32 ops_raw; + u32 scales_raw; + explicit operator Pica::TexturingRegs::TevStageConfig() const noexcept { + Pica::TexturingRegs::TevStageConfig stage; + stage.sources_raw = sources_raw; + stage.modifiers_raw = modifiers_raw; + stage.ops_raw = ops_raw; + stage.const_color = 0; + stage.scales_raw = scales_raw; + return stage; + } + }; + + struct State { + Pica::FramebufferRegs::CompareFunc alpha_test_func; + Pica::RasterizerRegs::ScissorMode scissor_test_mode; + Pica::TexturingRegs::TextureConfig::TextureType texture0_type; + std::array<TevStageConfigRaw, 6> tev_stages; + u8 combiner_buffer_input; + + Pica::RasterizerRegs::DepthBuffering depthmap_enable; + Pica::TexturingRegs::FogMode fog_mode; + bool fog_flip; + + struct { + struct { + unsigned num; + bool directional; + bool two_sided_diffuse; + bool dist_atten_enable; + } light[8]; + + bool enable; + unsigned src_num; + Pica::LightingRegs::LightingBumpMode bump_mode; + unsigned bump_selector; + bool bump_renorm; + bool clamp_highlights; + + Pica::LightingRegs::LightingConfig config; + Pica::LightingRegs::LightingFresnelSelector fresnel_selector; + + struct { + bool enable; + bool abs_input; + Pica::LightingRegs::LightingLutInput type; + float scale; + } lut_d0, lut_d1, lut_fr, lut_rr, lut_rg, lut_rb; + } lighting; + + } state; +}; +#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) +static_assert(std::is_trivially_copyable<PicaShaderConfig::State>::value, + "PicaShaderConfig::State must be trivially copyable"); +#endif + /** * Generates the GLSL vertex shader program source code for the current Pica state * @returns String of the shader source code @@ -25,3 +134,12 @@ std::string GenerateVertexShader(); std::string GenerateFragmentShader(const PicaShaderConfig& config); } // namespace GLShader + +namespace std { +template <> +struct hash<GLShader::PicaShaderConfig> { + size_t operator()(const GLShader::PicaShaderConfig& k) const { + return Common::ComputeHash64(&k.state, sizeof(GLShader::PicaShaderConfig::State)); + } +}; +} // namespace std diff --git a/src/video_core/renderer_opengl/gl_shader_util.h b/src/video_core/renderer_opengl/gl_shader_util.h index f59912f79..c66e8acd3 100644 --- a/src/video_core/renderer_opengl/gl_shader_util.h +++ b/src/video_core/renderer_opengl/gl_shader_util.h @@ -8,17 +8,6 @@ namespace GLShader { -enum Attributes { - ATTRIBUTE_POSITION, - ATTRIBUTE_COLOR, - ATTRIBUTE_TEXCOORD0, - ATTRIBUTE_TEXCOORD1, - ATTRIBUTE_TEXCOORD2, - ATTRIBUTE_TEXCOORD0_W, - ATTRIBUTE_NORMQUAT, - ATTRIBUTE_VIEW, -}; - /** * Utility function to create and compile an OpenGL GLSL shader program (vertex + fragment shader) * @param vertex_shader String of the GLSL vertex shader program |