summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x.travis-build.sh4
-rwxr-xr-x.travis-upload.sh4
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/file_sys/archive_ncch.cpp29
-rw-r--r--src/core/file_sys/ncch_container.cpp245
-rw-r--r--src/core/file_sys/ncch_container.h30
-rw-r--r--src/core/file_sys/title_metadata.cpp212
-rw-r--r--src/core/file_sys/title_metadata.h125
-rw-r--r--src/core/hle/kernel/shared_memory.cpp27
-rw-r--r--src/core/hle/service/apt/apt.cpp73
-rw-r--r--src/core/hle/service/apt/apt_s.cpp4
-rw-r--r--src/core/loader/ncch.cpp17
-rw-r--r--src/video_core/command_processor.cpp439
13 files changed, 866 insertions, 344 deletions
diff --git a/.travis-build.sh b/.travis-build.sh
index bb4e6fc47..fc5a5f8b2 100755
--- a/.travis-build.sh
+++ b/.travis-build.sh
@@ -52,8 +52,8 @@ elif [ "$TRAVIS_OS_NAME" = "osx" ]; then
52 export Qt5_DIR=$(brew --prefix)/opt/qt5 52 export Qt5_DIR=$(brew --prefix)/opt/qt5
53 53
54 mkdir build && cd build 54 mkdir build && cd build
55 cmake .. -DUSE_SYSTEM_CURL=ON -GXcode 55 cmake .. -DUSE_SYSTEM_CURL=ON -DCMAKE_OSX_ARCHITECTURES="x86_64;x86_64h" -DCMAKE_BUILD_TYPE=Release
56 xcodebuild -configuration Release 56 make -j4
57 57
58 ctest -VV -C Release 58 ctest -VV -C Release
59fi 59fi
diff --git a/.travis-upload.sh b/.travis-upload.sh
index 8c1fa21c5..edf195f7d 100755
--- a/.travis-upload.sh
+++ b/.travis-upload.sh
@@ -16,8 +16,8 @@ elif [ "$TRAVIS_OS_NAME" = "osx" ]; then
16 COMPRESSION_FLAGS="-czvf" 16 COMPRESSION_FLAGS="-czvf"
17 mkdir "$REV_NAME" 17 mkdir "$REV_NAME"
18 18
19 cp build/src/citra/Release/citra "$REV_NAME" 19 cp build/src/citra/citra "$REV_NAME"
20 cp -r build/src/citra_qt/Release/citra-qt.app "$REV_NAME" 20 cp -r build/src/citra_qt/citra-qt.app "$REV_NAME"
21 21
22 # move qt libs into app bundle for deployment 22 # move qt libs into app bundle for deployment
23 $(brew --prefix)/opt/qt5/bin/macdeployqt "${REV_NAME}/citra-qt.app" 23 $(brew --prefix)/opt/qt5/bin/macdeployqt "${REV_NAME}/citra-qt.app"
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 3ed619991..2618da18c 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -29,6 +29,7 @@ set(SRCS
29 file_sys/ncch_container.cpp 29 file_sys/ncch_container.cpp
30 file_sys/path_parser.cpp 30 file_sys/path_parser.cpp
31 file_sys/savedata_archive.cpp 31 file_sys/savedata_archive.cpp
32 file_sys/title_metadata.cpp
32 frontend/camera/blank_camera.cpp 33 frontend/camera/blank_camera.cpp
33 frontend/camera/factory.cpp 34 frontend/camera/factory.cpp
34 frontend/camera/interface.cpp 35 frontend/camera/interface.cpp
diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp
index 6d9007731..e8c5be983 100644
--- a/src/core/file_sys/archive_ncch.cpp
+++ b/src/core/file_sys/archive_ncch.cpp
@@ -13,7 +13,10 @@
13#include "core/file_sys/archive_ncch.h" 13#include "core/file_sys/archive_ncch.h"
14#include "core/file_sys/errors.h" 14#include "core/file_sys/errors.h"
15#include "core/file_sys/ivfc_archive.h" 15#include "core/file_sys/ivfc_archive.h"
16#include "core/file_sys/ncch_container.h"
17#include "core/file_sys/title_metadata.h"
16#include "core/hle/service/fs/archive.h" 18#include "core/hle/service/fs/archive.h"
19#include "core/loader/loader.h"
17 20
18//////////////////////////////////////////////////////////////////////////////////////////////////// 21////////////////////////////////////////////////////////////////////////////////////////////////////
19// FileSys namespace 22// FileSys namespace
@@ -25,8 +28,18 @@ static std::string GetNCCHContainerPath(const std::string& nand_directory) {
25} 28}
26 29
27static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) { 30static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) {
28 return Common::StringFromFormat("%s%08x/%08x/content/00000000.app.romfs", mount_point.c_str(), 31 u32 content_id = 0;
29 high, low); 32
33 // TODO(shinyquagsire23): Title database should be doing this path lookup
34 std::string content_path =
35 Common::StringFromFormat("%s%08x/%08x/content/", mount_point.c_str(), high, low);
36 std::string tmd_path = content_path + "00000000.tmd";
37 TitleMetadata tmd(tmd_path);
38 if (tmd.Load() == Loader::ResultStatus::Success) {
39 content_id = tmd.GetBootContentID();
40 }
41
42 return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id);
30} 43}
31 44
32ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory) 45ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory)
@@ -38,9 +51,14 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path&
38 u32 high = data[1]; 51 u32 high = data[1];
39 u32 low = data[0]; 52 u32 low = data[0];
40 std::string file_path = GetNCCHPath(mount_point, high, low); 53 std::string file_path = GetNCCHPath(mount_point, high, low);
41 auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb");
42 54
43 if (!file->IsOpen()) { 55 std::shared_ptr<FileUtil::IOFile> romfs_file;
56 u64 romfs_offset = 0;
57 u64 romfs_size = 0;
58 auto ncch_container = NCCHContainer(file_path);
59
60 if (ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size) !=
61 Loader::ResultStatus::Success) {
44 // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list). 62 // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
45 constexpr u32 shared_data_archive = 0x0004009B; 63 constexpr u32 shared_data_archive = 0x0004009B;
46 constexpr u32 system_data_archive = 0x000400DB; 64 constexpr u32 system_data_archive = 0x000400DB;
@@ -74,9 +92,8 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path&
74 } 92 }
75 return ERROR_NOT_FOUND; 93 return ERROR_NOT_FOUND;
76 } 94 }
77 auto size = file->GetSize();
78 95
79 auto archive = std::make_unique<IVFCArchive>(file, 0, size); 96 auto archive = std::make_unique<IVFCArchive>(romfs_file, romfs_offset, romfs_size);
80 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); 97 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
81} 98}
82 99
diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp
index 59c72f3e9..b9fb940c7 100644
--- a/src/core/file_sys/ncch_container.cpp
+++ b/src/core/file_sys/ncch_container.cpp
@@ -116,92 +116,143 @@ Loader::ResultStatus NCCHContainer::Load() {
116 if (is_loaded) 116 if (is_loaded)
117 return Loader::ResultStatus::Success; 117 return Loader::ResultStatus::Success;
118 118
119 // Reset read pointer in case this file has been read before. 119 if (file.IsOpen()) {
120 file.Seek(0, SEEK_SET); 120 // Reset read pointer in case this file has been read before.
121 file.Seek(0, SEEK_SET);
121 122
122 if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header)) 123 if (file.ReadBytes(&ncch_header, sizeof(NCCH_Header)) != sizeof(NCCH_Header))
123 return Loader::ResultStatus::Error; 124 return Loader::ResultStatus::Error;
124 125
125 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)... 126 // Skip NCSD header and load first NCCH (NCSD is just a container of NCCH files)...
126 if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) { 127 if (Loader::MakeMagic('N', 'C', 'S', 'D') == ncch_header.magic) {
127 LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!"); 128 LOG_DEBUG(Service_FS, "Only loading the first (bootable) NCCH within the NCSD file!");
128 ncch_offset = 0x4000; 129 ncch_offset = 0x4000;
129 file.Seek(ncch_offset, SEEK_SET); 130 file.Seek(ncch_offset, SEEK_SET);
130 file.ReadBytes(&ncch_header, sizeof(NCCH_Header)); 131 file.ReadBytes(&ncch_header, sizeof(NCCH_Header));
131 } 132 }
132 133
133 // Verify we are loading the correct file type... 134 // Verify we are loading the correct file type...
134 if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic) 135 if (Loader::MakeMagic('N', 'C', 'C', 'H') != ncch_header.magic)
135 return Loader::ResultStatus::ErrorInvalidFormat; 136 return Loader::ResultStatus::ErrorInvalidFormat;
137
138 has_header = true;
139
140 // System archives and DLC don't have an extended header but have RomFS
141 if (ncch_header.extended_header_size) {
142 if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) !=
143 sizeof(ExHeader_Header))
144 return Loader::ResultStatus::Error;
145
146 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1;
147 u32 entry_point = exheader_header.codeset_info.text.address;
148 u32 code_size = exheader_header.codeset_info.text.code_size;
149 u32 stack_size = exheader_header.codeset_info.stack_size;
150 u32 bss_size = exheader_header.codeset_info.bss_size;
151 u32 core_version = exheader_header.arm11_system_local_caps.core_version;
152 u8 priority = exheader_header.arm11_system_local_caps.priority;
153 u8 resource_limit_category =
154 exheader_header.arm11_system_local_caps.resource_limit_category;
155
156 LOG_DEBUG(Service_FS, "Name: %s",
157 exheader_header.codeset_info.name);
158 LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64,
159 ncch_header.program_id);
160 LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no");
161 LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point);
162 LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size);
163 LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size);
164 LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size);
165 LOG_DEBUG(Service_FS, "Core version: %d", core_version);
166 LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority);
167 LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category);
168 LOG_DEBUG(Service_FS, "System Mode: %d",
169 static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
170
171 if (exheader_header.system_info.jump_id != ncch_header.program_id) {
172 LOG_ERROR(Service_FS,
173 "ExHeader Program ID mismatch: the ROM is probably encrypted.");
174 return Loader::ResultStatus::ErrorEncrypted;
175 }
136 176
137 // System archives and DLC don't have an extended header but have RomFS 177 has_exheader = true;
138 if (ncch_header.extended_header_size) { 178 }
139 if (file.ReadBytes(&exheader_header, sizeof(ExHeader_Header)) != sizeof(ExHeader_Header))
140 return Loader::ResultStatus::Error;
141 179
142 is_compressed = (exheader_header.codeset_info.flags.flag & 1) == 1; 180 // DLC can have an ExeFS and a RomFS but no extended header
143 u32 entry_point = exheader_header.codeset_info.text.address; 181 if (ncch_header.exefs_size) {
144 u32 code_size = exheader_header.codeset_info.text.code_size; 182 exefs_offset = ncch_header.exefs_offset * kBlockSize;
145 u32 stack_size = exheader_header.codeset_info.stack_size; 183 u32 exefs_size = ncch_header.exefs_size * kBlockSize;
146 u32 bss_size = exheader_header.codeset_info.bss_size; 184
147 u32 core_version = exheader_header.arm11_system_local_caps.core_version; 185 LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset);
148 u8 priority = exheader_header.arm11_system_local_caps.priority; 186 LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size);
149 u8 resource_limit_category = 187
150 exheader_header.arm11_system_local_caps.resource_limit_category; 188 file.Seek(exefs_offset + ncch_offset, SEEK_SET);
151 189 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header))
152 LOG_DEBUG(Service_FS, "Name: %s", exheader_header.codeset_info.name); 190 return Loader::ResultStatus::Error;
153 LOG_DEBUG(Service_FS, "Program ID: %016" PRIX64, ncch_header.program_id); 191
154 LOG_DEBUG(Service_FS, "Code compressed: %s", is_compressed ? "yes" : "no"); 192 exefs_file = FileUtil::IOFile(filepath, "rb");
155 LOG_DEBUG(Service_FS, "Entry point: 0x%08X", entry_point); 193 has_exefs = true;
156 LOG_DEBUG(Service_FS, "Code size: 0x%08X", code_size);
157 LOG_DEBUG(Service_FS, "Stack size: 0x%08X", stack_size);
158 LOG_DEBUG(Service_FS, "Bss size: 0x%08X", bss_size);
159 LOG_DEBUG(Service_FS, "Core version: %d", core_version);
160 LOG_DEBUG(Service_FS, "Thread priority: 0x%X", priority);
161 LOG_DEBUG(Service_FS, "Resource limit category: %d", resource_limit_category);
162 LOG_DEBUG(Service_FS, "System Mode: %d",
163 static_cast<int>(exheader_header.arm11_system_local_caps.system_mode));
164
165 if (exheader_header.system_info.jump_id != ncch_header.program_id) {
166 LOG_ERROR(Service_FS, "ExHeader Program ID mismatch: the ROM is probably encrypted.");
167 return Loader::ResultStatus::ErrorEncrypted;
168 } 194 }
169 195
170 has_exheader = true; 196 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0)
197 has_romfs = true;
171 } 198 }
172 199
173 // DLC can have an ExeFS and a RomFS but no extended header 200 LoadOverrides();
174 if (ncch_header.exefs_size) {
175 exefs_offset = ncch_header.exefs_offset * kBlockSize;
176 u32 exefs_size = ncch_header.exefs_size * kBlockSize;
177 201
178 LOG_DEBUG(Service_FS, "ExeFS offset: 0x%08X", exefs_offset); 202 // We need at least one of these or overrides, practically
179 LOG_DEBUG(Service_FS, "ExeFS size: 0x%08X", exefs_size); 203 if (!(has_exefs || has_romfs || is_tainted))
204 return Loader::ResultStatus::Error;
180 205
181 file.Seek(exefs_offset + ncch_offset, SEEK_SET); 206 is_loaded = true;
182 if (file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) != sizeof(ExeFs_Header)) 207 return Loader::ResultStatus::Success;
183 return Loader::ResultStatus::Error; 208}
184 209
185 has_exefs = true; 210Loader::ResultStatus NCCHContainer::LoadOverrides() {
211 // Check for split-off files, mark the archive as tainted if we will use them
212 std::string romfs_override = filepath + ".romfs";
213 if (FileUtil::Exists(romfs_override)) {
214 is_tainted = true;
186 } 215 }
187 216
188 if (ncch_header.romfs_offset != 0 && ncch_header.romfs_size != 0) 217 // If we have a split-off exefs file/folder, it takes priority
189 has_romfs = true; 218 std::string exefs_override = filepath + ".exefs";
219 std::string exefsdir_override = filepath + ".exefsdir/";
220 if (FileUtil::Exists(exefs_override)) {
221 exefs_file = FileUtil::IOFile(exefs_override, "rb");
222
223 if (exefs_file.ReadBytes(&exefs_header, sizeof(ExeFs_Header)) == sizeof(ExeFs_Header)) {
224 LOG_DEBUG(Service_FS, "Loading ExeFS section from %s", exefs_override.c_str());
225 exefs_offset = 0;
226 is_tainted = true;
227 has_exefs = true;
228 } else {
229 exefs_file = FileUtil::IOFile(filepath, "rb");
230 }
231 } else if (FileUtil::Exists(exefsdir_override) && FileUtil::IsDirectory(exefsdir_override)) {
232 is_tainted = true;
233 }
234
235 if (is_tainted)
236 LOG_WARNING(Service_FS,
237 "Loaded NCCH %s is tainted, application behavior may not be as expected!",
238 filepath.c_str());
190 239
191 is_loaded = true;
192 return Loader::ResultStatus::Success; 240 return Loader::ResultStatus::Success;
193} 241}
194 242
195Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) { 243Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vector<u8>& buffer) {
196 if (!file.IsOpen())
197 return Loader::ResultStatus::Error;
198
199 Loader::ResultStatus result = Load(); 244 Loader::ResultStatus result = Load();
200 if (result != Loader::ResultStatus::Success) 245 if (result != Loader::ResultStatus::Success)
201 return result; 246 return result;
202 247
203 if (!has_exefs) 248 // Check if we have files that can drop-in and replace
204 return Loader::ResultStatus::ErrorNotUsed; 249 result = LoadOverrideExeFSSection(name, buffer);
250 if (result == Loader::ResultStatus::Success || !has_exefs)
251 return result;
252
253 // If we don't have any separate files, we'll need a full ExeFS
254 if (!exefs_file.IsOpen())
255 return Loader::ResultStatus::Error;
205 256
206 LOG_DEBUG(Service_FS, "%d sections:", kMaxSections); 257 LOG_DEBUG(Service_FS, "%d sections:", kMaxSections);
207 // Iterate through the ExeFs archive until we find a section with the specified name... 258 // Iterate through the ExeFs archive until we find a section with the specified name...
@@ -215,7 +266,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
215 266
216 s64 section_offset = 267 s64 section_offset =
217 (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset); 268 (section.offset + exefs_offset + sizeof(ExeFs_Header) + ncch_offset);
218 file.Seek(section_offset, SEEK_SET); 269 exefs_file.Seek(section_offset, SEEK_SET);
219 270
220 if (strcmp(section.name, ".code") == 0 && is_compressed) { 271 if (strcmp(section.name, ".code") == 0 && is_compressed) {
221 // Section is compressed, read compressed .code section... 272 // Section is compressed, read compressed .code section...
@@ -226,7 +277,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
226 return Loader::ResultStatus::ErrorMemoryAllocationFailed; 277 return Loader::ResultStatus::ErrorMemoryAllocationFailed;
227 } 278 }
228 279
229 if (file.ReadBytes(&temp_buffer[0], section.size) != section.size) 280 if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size)
230 return Loader::ResultStatus::Error; 281 return Loader::ResultStatus::Error;
231 282
232 // Decompress .code section... 283 // Decompress .code section...
@@ -237,7 +288,7 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
237 } else { 288 } else {
238 // Section is uncompressed... 289 // Section is uncompressed...
239 buffer.resize(section.size); 290 buffer.resize(section.size);
240 if (file.ReadBytes(&buffer[0], section.size) != section.size) 291 if (exefs_file.ReadBytes(&buffer[0], section.size) != section.size)
241 return Loader::ResultStatus::Error; 292 return Loader::ResultStatus::Error;
242 } 293 }
243 return Loader::ResultStatus::Success; 294 return Loader::ResultStatus::Success;
@@ -246,20 +297,56 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
246 return Loader::ResultStatus::ErrorNotUsed; 297 return Loader::ResultStatus::ErrorNotUsed;
247} 298}
248 299
249Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file, 300Loader::ResultStatus NCCHContainer::LoadOverrideExeFSSection(const char* name,
250 u64& offset, u64& size) { 301 std::vector<u8>& buffer) {
251 if (!file.IsOpen()) 302 std::string override_name;
303
304 // Map our section name to the extracted equivalent
305 if (!strcmp(name, ".code"))
306 override_name = "code.bin";
307 else if (!strcmp(name, "icon"))
308 override_name = "code.bin";
309 else if (!strcmp(name, "banner"))
310 override_name = "banner.bnr";
311 else if (!strcmp(name, "logo"))
312 override_name = "logo.bcma.lz";
313 else
252 return Loader::ResultStatus::Error; 314 return Loader::ResultStatus::Error;
253 315
316 std::string section_override = filepath + ".exefsdir/" + override_name;
317 FileUtil::IOFile section_file(section_override, "rb");
318
319 if (section_file.IsOpen()) {
320 auto section_size = section_file.GetSize();
321 buffer.resize(section_size);
322
323 section_file.Seek(0, SEEK_SET);
324 if (section_file.ReadBytes(&buffer[0], section_size) == section_size) {
325 LOG_WARNING(Service_FS, "File %s overriding built-in ExeFS file",
326 section_override.c_str());
327 return Loader::ResultStatus::Success;
328 }
329 }
330 return Loader::ResultStatus::ErrorNotUsed;
331}
332
333Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
334 u64& offset, u64& size) {
254 Loader::ResultStatus result = Load(); 335 Loader::ResultStatus result = Load();
255 if (result != Loader::ResultStatus::Success) 336 if (result != Loader::ResultStatus::Success)
256 return result; 337 return result;
257 338
339 if (ReadOverrideRomFS(romfs_file, offset, size) == Loader::ResultStatus::Success)
340 return Loader::ResultStatus::Success;
341
258 if (!has_romfs) { 342 if (!has_romfs) {
259 LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS"); 343 LOG_DEBUG(Service_FS, "RomFS requested from NCCH which has no RomFS");
260 return Loader::ResultStatus::ErrorNotUsed; 344 return Loader::ResultStatus::ErrorNotUsed;
261 } 345 }
262 346
347 if (!file.IsOpen())
348 return Loader::ResultStatus::Error;
349
263 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000; 350 u32 romfs_offset = ncch_offset + (ncch_header.romfs_offset * kBlockSize) + 0x1000;
264 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000; 351 u32 romfs_size = (ncch_header.romfs_size * kBlockSize) - 0x1000;
265 352
@@ -280,11 +367,31 @@ Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<FileUtil::IOFile>&
280 return Loader::ResultStatus::Success; 367 return Loader::ResultStatus::Success;
281} 368}
282 369
370Loader::ResultStatus NCCHContainer::ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
371 u64& offset, u64& size) {
372 // Check for RomFS overrides
373 std::string split_filepath = filepath + ".romfs";
374 if (FileUtil::Exists(split_filepath)) {
375 romfs_file = std::make_shared<FileUtil::IOFile>(split_filepath, "rb");
376 if (romfs_file->IsOpen()) {
377 LOG_WARNING(Service_FS, "File %s overriding built-in RomFS", split_filepath.c_str());
378 offset = 0;
379 size = romfs_file->GetSize();
380 return Loader::ResultStatus::Success;
381 }
382 }
383
384 return Loader::ResultStatus::ErrorNotUsed;
385}
386
283Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) { 387Loader::ResultStatus NCCHContainer::ReadProgramId(u64_le& program_id) {
284 Loader::ResultStatus result = Load(); 388 Loader::ResultStatus result = Load();
285 if (result != Loader::ResultStatus::Success) 389 if (result != Loader::ResultStatus::Success)
286 return result; 390 return result;
287 391
392 if (!has_header)
393 return Loader::ResultStatus::ErrorNotUsed;
394
288 program_id = ncch_header.program_id; 395 program_id = ncch_header.program_id;
289 return Loader::ResultStatus::Success; 396 return Loader::ResultStatus::Success;
290} 397}
diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h
index 8af9032b4..2cc9d13dc 100644
--- a/src/core/file_sys/ncch_container.h
+++ b/src/core/file_sys/ncch_container.h
@@ -180,6 +180,13 @@ public:
180 Loader::ResultStatus Load(); 180 Loader::ResultStatus Load();
181 181
182 /** 182 /**
183 * Attempt to find overridden sections for the NCCH and mark the container as tainted
184 * if any are found.
185 * @return ResultStatus result of function
186 */
187 Loader::ResultStatus LoadOverrides();
188
189 /**
183 * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.) 190 * Reads an application ExeFS section of an NCCH file (e.g. .code, .logo, etc.)
184 * @param name Name of section to read out of NCCH file 191 * @param name Name of section to read out of NCCH file
185 * @param buffer Vector to read data into 192 * @param buffer Vector to read data into
@@ -188,6 +195,15 @@ public:
188 Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer); 195 Loader::ResultStatus LoadSectionExeFS(const char* name, std::vector<u8>& buffer);
189 196
190 /** 197 /**
198 * Reads an application ExeFS section from external files instead of an NCCH file,
199 * (e.g. code.bin, logo.bcma.lz, icon.icn, banner.bnr)
200 * @param name Name of section to read from external files
201 * @param buffer Vector to read data into
202 * @return ResultStatus result of function
203 */
204 Loader::ResultStatus LoadOverrideExeFSSection(const char* name, std::vector<u8>& buffer);
205
206 /**
191 * Get the RomFS of the NCCH container 207 * Get the RomFS of the NCCH container
192 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer 208 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
193 * @param romfs_file The file containing the RomFS 209 * @param romfs_file The file containing the RomFS
@@ -199,6 +215,17 @@ public:
199 u64& size); 215 u64& size);
200 216
201 /** 217 /**
218 * Get the override RomFS of the NCCH container
219 * Since the RomFS can be huge, we return a file reference instead of copying to a buffer
220 * @param romfs_file The file containing the RomFS
221 * @param offset The offset the romfs begins on
222 * @param size The size of the romfs
223 * @return ResultStatus result of function
224 */
225 Loader::ResultStatus ReadOverrideRomFS(std::shared_ptr<FileUtil::IOFile>& romfs_file,
226 u64& offset, u64& size);
227
228 /**
202 * Get the Program ID of the NCCH container 229 * Get the Program ID of the NCCH container
203 * @return ResultStatus result of function 230 * @return ResultStatus result of function
204 */ 231 */
@@ -227,10 +254,12 @@ public:
227 ExHeader_Header exheader_header; 254 ExHeader_Header exheader_header;
228 255
229private: 256private:
257 bool has_header = false;
230 bool has_exheader = false; 258 bool has_exheader = false;
231 bool has_exefs = false; 259 bool has_exefs = false;
232 bool has_romfs = false; 260 bool has_romfs = false;
233 261
262 bool is_tainted = false; // Are there parts of this container being overridden?
234 bool is_loaded = false; 263 bool is_loaded = false;
235 bool is_compressed = false; 264 bool is_compressed = false;
236 265
@@ -239,6 +268,7 @@ private:
239 268
240 std::string filepath; 269 std::string filepath;
241 FileUtil::IOFile file; 270 FileUtil::IOFile file;
271 FileUtil::IOFile exefs_file;
242}; 272};
243 273
244} // namespace FileSys 274} // namespace FileSys
diff --git a/src/core/file_sys/title_metadata.cpp b/src/core/file_sys/title_metadata.cpp
new file mode 100644
index 000000000..1ef8840a0
--- /dev/null
+++ b/src/core/file_sys/title_metadata.cpp
@@ -0,0 +1,212 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cinttypes>
6#include <cryptopp/sha.h>
7#include "common/alignment.h"
8#include "common/file_util.h"
9#include "common/logging/log.h"
10#include "core/file_sys/title_metadata.h"
11#include "core/loader/loader.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// FileSys namespace
15
16namespace FileSys {
17
18static u32 GetSignatureSize(u32 signature_type) {
19 switch (signature_type) {
20 case Rsa4096Sha1:
21 case Rsa4096Sha256:
22 return 0x200;
23
24 case Rsa2048Sha1:
25 case Rsa2048Sha256:
26 return 0x100;
27
28 case EllipticSha1:
29 case EcdsaSha256:
30 return 0x3C;
31 }
32}
33
34Loader::ResultStatus TitleMetadata::Load() {
35 FileUtil::IOFile file(filepath, "rb");
36 if (!file.IsOpen())
37 return Loader::ResultStatus::Error;
38
39 if (!file.ReadBytes(&signature_type, sizeof(u32_be)))
40 return Loader::ResultStatus::Error;
41
42 // Signature lengths are variable, and the body follows the signature
43 u32 signature_size = GetSignatureSize(signature_type);
44
45 tmd_signature.resize(signature_size);
46 if (!file.ReadBytes(&tmd_signature[0], signature_size))
47 return Loader::ResultStatus::Error;
48
49 // The TMD body start position is rounded to the nearest 0x40 after the signature
50 size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
51 file.Seek(body_start, SEEK_SET);
52
53 // Read our TMD body, then load the amount of ContentChunks specified
54 if (file.ReadBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
55 return Loader::ResultStatus::Error;
56
57 for (u16 i = 0; i < tmd_body.content_count; i++) {
58 ContentChunk chunk;
59 if (file.ReadBytes(&chunk, sizeof(ContentChunk)) == sizeof(ContentChunk)) {
60 tmd_chunks.push_back(chunk);
61 } else {
62 LOG_ERROR(Service_FS, "Malformed TMD %s, failed to load content chunk index %u!",
63 filepath.c_str(), i);
64 return Loader::ResultStatus::ErrorInvalidFormat;
65 }
66 }
67
68 return Loader::ResultStatus::Success;
69}
70
71Loader::ResultStatus TitleMetadata::Save() {
72 FileUtil::IOFile file(filepath, "wb");
73 if (!file.IsOpen())
74 return Loader::ResultStatus::Error;
75
76 if (!file.WriteBytes(&signature_type, sizeof(u32_be)))
77 return Loader::ResultStatus::Error;
78
79 // Signature lengths are variable, and the body follows the signature
80 u32 signature_size = GetSignatureSize(signature_type);
81
82 if (!file.WriteBytes(tmd_signature.data(), signature_size))
83 return Loader::ResultStatus::Error;
84
85 // The TMD body start position is rounded to the nearest 0x40 after the signature
86 size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
87 file.Seek(body_start, SEEK_SET);
88
89 // Update our TMD body values and hashes
90 tmd_body.content_count = static_cast<u16>(tmd_chunks.size());
91
92 // TODO(shinyquagsire23): Do TMDs with more than one contentinfo exist?
93 // For now we'll just adjust the first index to hold all content chunks
94 // and ensure that no further content info data exists.
95 tmd_body.contentinfo = {};
96 tmd_body.contentinfo[0].index = 0;
97 tmd_body.contentinfo[0].command_count = static_cast<u16>(tmd_chunks.size());
98
99 CryptoPP::SHA256 chunk_hash;
100 for (u16 i = 0; i < tmd_body.content_count; i++) {
101 chunk_hash.Update(reinterpret_cast<u8*>(&tmd_chunks[i]), sizeof(ContentChunk));
102 }
103 chunk_hash.Final(tmd_body.contentinfo[0].hash.data());
104
105 CryptoPP::SHA256 contentinfo_hash;
106 for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
107 chunk_hash.Update(reinterpret_cast<u8*>(&tmd_body.contentinfo[i]), sizeof(ContentInfo));
108 }
109 chunk_hash.Final(tmd_body.contentinfo_hash.data());
110
111 // Write our TMD body, then write each of our ContentChunks
112 if (file.WriteBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
113 return Loader::ResultStatus::Error;
114
115 for (u16 i = 0; i < tmd_body.content_count; i++) {
116 ContentChunk chunk = tmd_chunks[i];
117 if (file.WriteBytes(&chunk, sizeof(ContentChunk)) != sizeof(ContentChunk))
118 return Loader::ResultStatus::Error;
119 }
120
121 return Loader::ResultStatus::Success;
122}
123
124u64 TitleMetadata::GetTitleID() const {
125 return tmd_body.title_id;
126}
127
128u32 TitleMetadata::GetTitleType() const {
129 return tmd_body.title_type;
130}
131
132u16 TitleMetadata::GetTitleVersion() const {
133 return tmd_body.title_version;
134}
135
136u64 TitleMetadata::GetSystemVersion() const {
137 return tmd_body.system_version;
138}
139
140size_t TitleMetadata::GetContentCount() const {
141 return tmd_chunks.size();
142}
143
144u32 TitleMetadata::GetBootContentID() const {
145 return tmd_chunks[TMDContentIndex::Main].id;
146}
147
148u32 TitleMetadata::GetManualContentID() const {
149 return tmd_chunks[TMDContentIndex::Manual].id;
150}
151
152u32 TitleMetadata::GetDLPContentID() const {
153 return tmd_chunks[TMDContentIndex::DLP].id;
154}
155
156void TitleMetadata::SetTitleID(u64 title_id) {
157 tmd_body.title_id = title_id;
158}
159
160void TitleMetadata::SetTitleType(u32 type) {
161 tmd_body.title_type = type;
162}
163
164void TitleMetadata::SetTitleVersion(u16 version) {
165 tmd_body.title_version = version;
166}
167
168void TitleMetadata::SetSystemVersion(u64 version) {
169 tmd_body.system_version = version;
170}
171
172void TitleMetadata::AddContentChunk(const ContentChunk& chunk) {
173 tmd_chunks.push_back(chunk);
174}
175
176void TitleMetadata::Print() const {
177 LOG_DEBUG(Service_FS, "%s - %u chunks", filepath.c_str(),
178 static_cast<u32>(tmd_body.content_count));
179
180 // Content info describes ranges of content chunks
181 LOG_DEBUG(Service_FS, "Content info:");
182 for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
183 if (tmd_body.contentinfo[i].command_count == 0)
184 break;
185
186 LOG_DEBUG(Service_FS, " Index %04X, Command Count %04X",
187 static_cast<u32>(tmd_body.contentinfo[i].index),
188 static_cast<u32>(tmd_body.contentinfo[i].command_count));
189 }
190
191 // For each content info, print their content chunk range
192 for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
193 u16 index = static_cast<u16>(tmd_body.contentinfo[i].index);
194 u16 count = static_cast<u16>(tmd_body.contentinfo[i].command_count);
195
196 if (count == 0)
197 continue;
198
199 LOG_DEBUG(Service_FS, "Content chunks for content info index %zu:", i);
200 for (u16 j = index; j < index + count; j++) {
201 // Don't attempt to print content we don't have
202 if (j > tmd_body.content_count)
203 break;
204
205 const ContentChunk& chunk = tmd_chunks[j];
206 LOG_DEBUG(Service_FS, " ID %08X, Index %04X, Type %04x, Size %016" PRIX64,
207 static_cast<u32>(chunk.id), static_cast<u32>(chunk.index),
208 static_cast<u32>(chunk.type), static_cast<u64>(chunk.size));
209 }
210 }
211}
212} // namespace FileSys
diff --git a/src/core/file_sys/title_metadata.h b/src/core/file_sys/title_metadata.h
new file mode 100644
index 000000000..1fc157bf3
--- /dev/null
+++ b/src/core/file_sys/title_metadata.h
@@ -0,0 +1,125 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8#include <vector>
9#include "common/common_types.h"
10#include "common/swap.h"
11
12namespace Loader {
13enum class ResultStatus;
14}
15
16////////////////////////////////////////////////////////////////////////////////////////////////////
17// FileSys namespace
18
19namespace FileSys {
20
21enum TMDSignatureType : u32 {
22 Rsa4096Sha1 = 0x10000,
23 Rsa2048Sha1 = 0x10001,
24 EllipticSha1 = 0x10002,
25 Rsa4096Sha256 = 0x10003,
26 Rsa2048Sha256 = 0x10004,
27 EcdsaSha256 = 0x10005
28};
29
30enum TMDContentTypeFlag : u16 {
31 Encrypted = 1 << 1,
32 Disc = 1 << 2,
33 CFM = 1 << 3,
34 Optional = 1 << 14,
35 Shared = 1 << 15
36};
37
38/**
39 * Helper which implements an interface to read and write Title Metadata (TMD) files.
40 * If a file path is provided and the file exists, it can be parsed and used, otherwise
41 * it must be created. The TMD file can then be interpreted, modified and/or saved.
42 */
43class TitleMetadata {
44public:
45 struct ContentChunk {
46 u32_be id;
47 u16_be index;
48 u16_be type;
49 u64_be size;
50 std::array<u8, 0x20> hash;
51 };
52
53 static_assert(sizeof(ContentChunk) == 0x30, "TMD ContentChunk structure size is wrong");
54
55 struct ContentInfo {
56 u16_be index;
57 u16_be command_count;
58 std::array<u8, 0x20> hash;
59 };
60
61 static_assert(sizeof(ContentInfo) == 0x24, "TMD ContentInfo structure size is wrong");
62
63#pragma pack(push, 1)
64
65 struct Body {
66 std::array<u8, 0x40> issuer;
67 u8 version;
68 u8 ca_crl_version;
69 u8 signer_crl_version;
70 u8 reserved;
71 u64_be system_version;
72 u64_be title_id;
73 u32_be title_type;
74 u16_be group_id;
75 u32_be savedata_size;
76 u32_be srl_private_savedata_size;
77 std::array<u8, 4> reserved_2;
78 u8 srl_flag;
79 std::array<u8, 0x31> reserved_3;
80 u32_be access_rights;
81 u16_be title_version;
82 u16_be content_count;
83 u16_be boot_content;
84 std::array<u8, 2> reserved_4;
85 std::array<u8, 0x20> contentinfo_hash;
86 std::array<ContentInfo, 64> contentinfo;
87 };
88
89 static_assert(sizeof(Body) == 0x9C4, "TMD body structure size is wrong");
90
91#pragma pack(pop)
92
93 explicit TitleMetadata(std::string& path) : filepath(std::move(path)) {}
94 Loader::ResultStatus Load();
95 Loader::ResultStatus Save();
96
97 u64 GetTitleID() const;
98 u32 GetTitleType() const;
99 u16 GetTitleVersion() const;
100 u64 GetSystemVersion() const;
101 size_t GetContentCount() const;
102 u32 GetBootContentID() const;
103 u32 GetManualContentID() const;
104 u32 GetDLPContentID() const;
105
106 void SetTitleID(u64 title_id);
107 void SetTitleType(u32 type);
108 void SetTitleVersion(u16 version);
109 void SetSystemVersion(u64 version);
110 void AddContentChunk(const ContentChunk& chunk);
111
112 void Print() const;
113
114private:
115 enum TMDContentIndex { Main = 0, Manual = 1, DLP = 2 };
116
117 Body tmd_body;
118 u32_be signature_type;
119 std::vector<u8> tmd_signature;
120 std::vector<ContentChunk> tmd_chunks;
121
122 std::string filepath;
123};
124
125} // namespace FileSys
diff --git a/src/core/hle/kernel/shared_memory.cpp b/src/core/hle/kernel/shared_memory.cpp
index 02d5a7a36..d45daca35 100644
--- a/src/core/hle/kernel/shared_memory.cpp
+++ b/src/core/hle/kernel/shared_memory.cpp
@@ -55,22 +55,19 @@ SharedPtr<SharedMemory> SharedMemory::Create(SharedPtr<Process> owner_process, u
55 Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get()); 55 Kernel::g_current_process->vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
56 } 56 }
57 } else { 57 } else {
58 // TODO(Subv): What happens if an application tries to create multiple memory blocks
59 // pointing to the same address?
60 auto& vm_manager = shared_memory->owner_process->vm_manager; 58 auto& vm_manager = shared_memory->owner_process->vm_manager;
61 // The memory is already available and mapped in the owner process. 59 // The memory is already available and mapped in the owner process.
62 auto vma = vm_manager.FindVMA(address)->second; 60 auto vma = vm_manager.FindVMA(address);
63 // Copy it over to our own storage 61 ASSERT_MSG(vma != vm_manager.vma_map.end(), "Invalid memory address");
64 shared_memory->backing_block = std::make_shared<std::vector<u8>>( 62 ASSERT_MSG(vma->second.backing_block, "Backing block doesn't exist for address");
65 vma.backing_block->data() + vma.offset, vma.backing_block->data() + vma.offset + size); 63
66 shared_memory->backing_block_offset = 0; 64 // The returned VMA might be a bigger one encompassing the desired address.
67 // Unmap the existing pages 65 auto vma_offset = address - vma->first;
68 vm_manager.UnmapRange(address, size); 66 ASSERT_MSG(vma_offset + size <= vma->second.size,
69 // Map our own block into the address space 67 "Shared memory exceeds bounds of mapped block");
70 vm_manager.MapMemoryBlock(address, shared_memory->backing_block, 0, size, 68
71 MemoryState::Shared); 69 shared_memory->backing_block = vma->second.backing_block;
72 // Reprotect the block with the new permissions 70 shared_memory->backing_block_offset = vma->second.offset + vma_offset;
73 vm_manager.ReprotectRange(address, size, ConvertPermissions(permissions));
74 } 71 }
75 72
76 shared_memory->base_address = address; 73 shared_memory->base_address = address;
@@ -184,4 +181,4 @@ u8* SharedMemory::GetPointer(u32 offset) {
184 return backing_block->data() + backing_block_offset + offset; 181 return backing_block->data() + backing_block_offset + offset;
185} 182}
186 183
187} // namespace 184} // namespace Kernel
diff --git a/src/core/hle/service/apt/apt.cpp b/src/core/hle/service/apt/apt.cpp
index 2f7362748..59ea9823d 100644
--- a/src/core/hle/service/apt/apt.cpp
+++ b/src/core/hle/service/apt/apt.cpp
@@ -171,7 +171,11 @@ void SendParameter(const MessageParameter& parameter) {
171 next_parameter = parameter; 171 next_parameter = parameter;
172 // Signal the event to let the receiver know that a new parameter is ready to be read 172 // Signal the event to let the receiver know that a new parameter is ready to be read
173 auto* const slot_data = GetAppletSlotData(static_cast<AppletId>(parameter.destination_id)); 173 auto* const slot_data = GetAppletSlotData(static_cast<AppletId>(parameter.destination_id));
174 ASSERT(slot_data); 174 if (slot_data == nullptr) {
175 LOG_DEBUG(Service_APT, "No applet was registered with the id %03X",
176 parameter.destination_id);
177 return;
178 }
175 179
176 slot_data->parameter_event->Signal(); 180 slot_data->parameter_event->Signal();
177} 181}
@@ -505,9 +509,6 @@ void SendParameter(Service::Interface* self) {
505 size_t size; 509 size_t size;
506 VAddr buffer = rp.PopStaticBuffer(&size); 510 VAddr buffer = rp.PopStaticBuffer(&size);
507 511
508 std::shared_ptr<HLE::Applets::Applet> dest_applet =
509 HLE::Applets::Applet::Get(static_cast<AppletId>(dst_app_id));
510
511 LOG_DEBUG(Service_APT, 512 LOG_DEBUG(Service_APT,
512 "called src_app_id=0x%08X, dst_app_id=0x%08X, signal_type=0x%08X," 513 "called src_app_id=0x%08X, dst_app_id=0x%08X, signal_type=0x%08X,"
513 "buffer_size=0x%08X, handle=0x%08X, size=0x%08zX, in_param_buffer_ptr=0x%08X", 514 "buffer_size=0x%08X, handle=0x%08X, size=0x%08zX, in_param_buffer_ptr=0x%08X",
@@ -522,12 +523,6 @@ void SendParameter(Service::Interface* self) {
522 return; 523 return;
523 } 524 }
524 525
525 if (dest_applet == nullptr) {
526 LOG_ERROR(Service_APT, "Unknown applet id=0x%08X", dst_app_id);
527 rb.Push<u32>(-1); // TODO(Subv): Find the right error code
528 return;
529 }
530
531 MessageParameter param; 526 MessageParameter param;
532 param.destination_id = dst_app_id; 527 param.destination_id = dst_app_id;
533 param.sender_id = src_app_id; 528 param.sender_id = src_app_id;
@@ -536,7 +531,14 @@ void SendParameter(Service::Interface* self) {
536 param.buffer.resize(buffer_size); 531 param.buffer.resize(buffer_size);
537 Memory::ReadBlock(buffer, param.buffer.data(), param.buffer.size()); 532 Memory::ReadBlock(buffer, param.buffer.data(), param.buffer.size());
538 533
539 rb.Push(dest_applet->ReceiveParameter(param)); 534 SendParameter(param);
535
536 // If the applet is running in HLE mode, use the HLE interface to communicate with it.
537 if (auto dest_applet = HLE::Applets::Applet::Get(static_cast<AppletId>(dst_app_id))) {
538 rb.Push(dest_applet->ReceiveParameter(param));
539 } else {
540 rb.Push(RESULT_SUCCESS);
541 }
540} 542}
541 543
542void ReceiveParameter(Service::Interface* self) { 544void ReceiveParameter(Service::Interface* self) {
@@ -765,7 +767,12 @@ void PrepareToStartLibraryApplet(Service::Interface* self) {
765 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x18, 1, 0); // 0x180040 767 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x18, 1, 0); // 0x180040
766 AppletId applet_id = static_cast<AppletId>(rp.Pop<u32>()); 768 AppletId applet_id = static_cast<AppletId>(rp.Pop<u32>());
767 769
770 LOG_DEBUG(Service_APT, "called applet_id=%08X", applet_id);
771
768 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); 772 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
773
774 // TODO(Subv): Launch the requested applet application.
775
769 auto applet = HLE::Applets::Applet::Get(applet_id); 776 auto applet = HLE::Applets::Applet::Get(applet_id);
770 if (applet) { 777 if (applet) {
771 LOG_WARNING(Service_APT, "applet has already been started id=%08X", applet_id); 778 LOG_WARNING(Service_APT, "applet has already been started id=%08X", applet_id);
@@ -773,7 +780,6 @@ void PrepareToStartLibraryApplet(Service::Interface* self) {
773 } else { 780 } else {
774 rb.Push(HLE::Applets::Applet::Create(applet_id)); 781 rb.Push(HLE::Applets::Applet::Create(applet_id));
775 } 782 }
776 LOG_DEBUG(Service_APT, "called applet_id=%08X", applet_id);
777} 783}
778 784
779void PrepareToStartNewestHomeMenu(Service::Interface* self) { 785void PrepareToStartNewestHomeMenu(Service::Interface* self) {
@@ -794,7 +800,12 @@ void PreloadLibraryApplet(Service::Interface* self) {
794 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x16, 1, 0); // 0x160040 800 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x16, 1, 0); // 0x160040
795 AppletId applet_id = static_cast<AppletId>(rp.Pop<u32>()); 801 AppletId applet_id = static_cast<AppletId>(rp.Pop<u32>());
796 802
803 LOG_DEBUG(Service_APT, "called applet_id=%08X", applet_id);
804
797 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); 805 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
806
807 // TODO(Subv): Launch the requested applet application.
808
798 auto applet = HLE::Applets::Applet::Get(applet_id); 809 auto applet = HLE::Applets::Applet::Get(applet_id);
799 if (applet) { 810 if (applet) {
800 LOG_WARNING(Service_APT, "applet has already been started id=%08X", applet_id); 811 LOG_WARNING(Service_APT, "applet has already been started id=%08X", applet_id);
@@ -802,34 +813,40 @@ void PreloadLibraryApplet(Service::Interface* self) {
802 } else { 813 } else {
803 rb.Push(HLE::Applets::Applet::Create(applet_id)); 814 rb.Push(HLE::Applets::Applet::Create(applet_id));
804 } 815 }
805 LOG_DEBUG(Service_APT, "called applet_id=%08X", applet_id);
806} 816}
807 817
808void StartLibraryApplet(Service::Interface* self) { 818void StartLibraryApplet(Service::Interface* self) {
809 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1E, 2, 4); // 0x1E0084 819 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1E, 2, 4); // 0x1E0084
810 AppletId applet_id = static_cast<AppletId>(rp.Pop<u32>()); 820 AppletId applet_id = static_cast<AppletId>(rp.Pop<u32>());
811 std::shared_ptr<HLE::Applets::Applet> applet = HLE::Applets::Applet::Get(applet_id);
812
813 LOG_DEBUG(Service_APT, "called applet_id=%08X", applet_id);
814
815 if (applet == nullptr) {
816 LOG_ERROR(Service_APT, "unknown applet id=%08X", applet_id);
817 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0, false);
818 rb.Push<u32>(-1); // TODO(Subv): Find the right error code
819 return;
820 }
821 821
822 size_t buffer_size = rp.Pop<u32>(); 822 size_t buffer_size = rp.Pop<u32>();
823 Kernel::Handle handle = rp.PopHandle(); 823 Kernel::Handle handle = rp.PopHandle();
824 VAddr buffer_addr = rp.PopStaticBuffer(); 824 VAddr buffer_addr = rp.PopStaticBuffer();
825 825
826 AppletStartupParameter parameter; 826 LOG_DEBUG(Service_APT, "called applet_id=%08X", applet_id);
827 parameter.object = Kernel::g_handle_table.GetGeneric(handle);
828 parameter.buffer.resize(buffer_size);
829 Memory::ReadBlock(buffer_addr, parameter.buffer.data(), parameter.buffer.size());
830 827
831 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); 828 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
832 rb.Push(applet->Start(parameter)); 829
830 // Send the Wakeup signal to the applet
831 MessageParameter param;
832 param.destination_id = static_cast<u32>(applet_id);
833 param.sender_id = static_cast<u32>(AppletId::Application);
834 param.object = Kernel::g_handle_table.GetGeneric(handle);
835 param.signal = static_cast<u32>(SignalType::Wakeup);
836 param.buffer.resize(buffer_size);
837 Memory::ReadBlock(buffer_addr, param.buffer.data(), param.buffer.size());
838 SendParameter(param);
839
840 // In case the applet is being HLEd, attempt to communicate with it.
841 if (auto applet = HLE::Applets::Applet::Get(applet_id)) {
842 AppletStartupParameter parameter;
843 parameter.object = Kernel::g_handle_table.GetGeneric(handle);
844 parameter.buffer.resize(buffer_size);
845 Memory::ReadBlock(buffer_addr, parameter.buffer.data(), parameter.buffer.size());
846 rb.Push(applet->Start(parameter));
847 } else {
848 rb.Push(RESULT_SUCCESS);
849 }
833} 850}
834 851
835void CancelLibraryApplet(Service::Interface* self) { 852void CancelLibraryApplet(Service::Interface* self) {
diff --git a/src/core/hle/service/apt/apt_s.cpp b/src/core/hle/service/apt/apt_s.cpp
index fe1d21fff..bb78ee7d7 100644
--- a/src/core/hle/service/apt/apt_s.cpp
+++ b/src/core/hle/service/apt/apt_s.cpp
@@ -20,7 +20,7 @@ const Interface::FunctionInfo FunctionTable[] = {
20 {0x00090040, IsRegistered, "IsRegistered"}, 20 {0x00090040, IsRegistered, "IsRegistered"},
21 {0x000A0040, nullptr, "GetAttribute"}, 21 {0x000A0040, nullptr, "GetAttribute"},
22 {0x000B0040, InquireNotification, "InquireNotification"}, 22 {0x000B0040, InquireNotification, "InquireNotification"},
23 {0x000C0104, nullptr, "SendParameter"}, 23 {0x000C0104, SendParameter, "SendParameter"},
24 {0x000D0080, ReceiveParameter, "ReceiveParameter"}, 24 {0x000D0080, ReceiveParameter, "ReceiveParameter"},
25 {0x000E0080, GlanceParameter, "GlanceParameter"}, 25 {0x000E0080, GlanceParameter, "GlanceParameter"},
26 {0x000F0100, nullptr, "CancelParameter"}, 26 {0x000F0100, nullptr, "CancelParameter"},
@@ -38,7 +38,7 @@ const Interface::FunctionInfo FunctionTable[] = {
38 {0x001B00C4, nullptr, "StartApplication"}, 38 {0x001B00C4, nullptr, "StartApplication"},
39 {0x001C0000, nullptr, "WakeupApplication"}, 39 {0x001C0000, nullptr, "WakeupApplication"},
40 {0x001D0000, nullptr, "CancelApplication"}, 40 {0x001D0000, nullptr, "CancelApplication"},
41 {0x001E0084, nullptr, "StartLibraryApplet"}, 41 {0x001E0084, StartLibraryApplet, "StartLibraryApplet"},
42 {0x001F0084, nullptr, "StartSystemApplet"}, 42 {0x001F0084, nullptr, "StartSystemApplet"},
43 {0x00200044, nullptr, "StartNewestHomeMenu"}, 43 {0x00200044, nullptr, "StartNewestHomeMenu"},
44 {0x00210000, nullptr, "OrderToCloseApplication"}, 44 {0x00210000, nullptr, "OrderToCloseApplication"},
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 66bc5823d..52686e364 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -14,6 +14,7 @@
14#include "core/core.h" 14#include "core/core.h"
15#include "core/file_sys/archive_selfncch.h" 15#include "core/file_sys/archive_selfncch.h"
16#include "core/file_sys/ncch_container.h" 16#include "core/file_sys/ncch_container.h"
17#include "core/file_sys/title_metadata.h"
17#include "core/hle/kernel/process.h" 18#include "core/hle/kernel/process.h"
18#include "core/hle/kernel/resource_limit.h" 19#include "core/hle/kernel/resource_limit.h"
19#include "core/hle/service/cfg/cfg.h" 20#include "core/hle/service/cfg/cfg.h"
@@ -49,9 +50,19 @@ static std::string GetUpdateNCCHPath(u64_le program_id) {
49 u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32); 50 u32 high = static_cast<u32>((program_id | UPDATE_MASK) >> 32);
50 u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF); 51 u32 low = static_cast<u32>((program_id | UPDATE_MASK) & 0xFFFFFFFF);
51 52
52 return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/%08x/%08x/content/00000000.app", 53 // TODO(shinyquagsire23): Title database should be doing this path lookup
53 FileUtil::GetUserPath(D_SDMC_IDX).c_str(), SYSTEM_ID, SDCARD_ID, 54 std::string content_path = Common::StringFromFormat(
54 high, low); 55 "%sNintendo 3DS/%s/%s/title/%08x/%08x/content/", FileUtil::GetUserPath(D_SDMC_IDX).c_str(),
56 SYSTEM_ID, SDCARD_ID, high, low);
57 std::string tmd_path = content_path + "00000000.tmd";
58
59 u32 content_id = 0;
60 FileSys::TitleMetadata tmd(tmd_path);
61 if (tmd.Load() == ResultStatus::Success) {
62 content_id = tmd.GetBootContentID();
63 }
64
65 return Common::StringFromFormat("%s%08x.app", content_path.c_str(), content_id);
55} 66}
56 67
57std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() { 68std::pair<boost::optional<u32>, ResultStatus> AppLoader_NCCH::LoadKernelSystemMode() {
diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp
index 3ab4af374..caf9f7a06 100644
--- a/src/video_core/command_processor.cpp
+++ b/src/video_core/command_processor.cpp
@@ -119,6 +119,224 @@ static void WriteUniformFloatReg(ShaderRegs& config, Shader::ShaderSetup& setup,
119 } 119 }
120} 120}
121 121
122static void LoadDefaultVertexAttributes(u32 register_value) {
123 auto& regs = g_state.regs;
124
125 // TODO: Does actual hardware indeed keep an intermediate buffer or does
126 // it directly write the values?
127 default_attr_write_buffer[default_attr_counter++] = register_value;
128
129 // Default attributes are written in a packed format such that four float24 values are encoded
130 // in three 32-bit numbers.
131 // We write to internal memory once a full such vector is written.
132 if (default_attr_counter >= 3) {
133 default_attr_counter = 0;
134
135 auto& setup = regs.pipeline.vs_default_attributes_setup;
136
137 if (setup.index >= 16) {
138 LOG_ERROR(HW_GPU, "Invalid VS default attribute index %d", (int)setup.index);
139 return;
140 }
141
142 Math::Vec4<float24> attribute;
143
144 // NOTE: The destination component order indeed is "backwards"
145 attribute.w = float24::FromRaw(default_attr_write_buffer[0] >> 8);
146 attribute.z = float24::FromRaw(((default_attr_write_buffer[0] & 0xFF) << 16) |
147 ((default_attr_write_buffer[1] >> 16) & 0xFFFF));
148 attribute.y = float24::FromRaw(((default_attr_write_buffer[1] & 0xFFFF) << 8) |
149 ((default_attr_write_buffer[2] >> 24) & 0xFF));
150 attribute.x = float24::FromRaw(default_attr_write_buffer[2] & 0xFFFFFF);
151
152 LOG_TRACE(HW_GPU, "Set default VS attribute %x to (%f %f %f %f)", (int)setup.index,
153 attribute.x.ToFloat32(), attribute.y.ToFloat32(), attribute.z.ToFloat32(),
154 attribute.w.ToFloat32());
155
156 // TODO: Verify that this actually modifies the register!
157 if (setup.index < 15) {
158 g_state.input_default_attributes.attr[setup.index] = attribute;
159 setup.index++;
160 } else {
161 // Put each attribute into an immediate input buffer. When all specified immediate
162 // attributes are present, the Vertex Shader is invoked and everything is sent to
163 // the primitive assembler.
164
165 auto& immediate_input = g_state.immediate.input_vertex;
166 auto& immediate_attribute_id = g_state.immediate.current_attribute;
167
168 immediate_input.attr[immediate_attribute_id] = attribute;
169
170 if (immediate_attribute_id < regs.pipeline.max_input_attrib_index) {
171 immediate_attribute_id += 1;
172 } else {
173 MICROPROFILE_SCOPE(GPU_Drawing);
174 immediate_attribute_id = 0;
175
176 auto* shader_engine = Shader::GetEngine();
177 shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
178
179 // Send to vertex shader
180 if (g_debug_context)
181 g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
182 static_cast<void*>(&immediate_input));
183 Shader::UnitState shader_unit;
184 Shader::AttributeBuffer output{};
185
186 shader_unit.LoadInput(regs.vs, immediate_input);
187 shader_engine->Run(g_state.vs, shader_unit);
188 shader_unit.WriteOutput(regs.vs, output);
189
190 // Send to geometry pipeline
191 if (g_state.immediate.reset_geometry_pipeline) {
192 g_state.geometry_pipeline.Reconfigure();
193 g_state.immediate.reset_geometry_pipeline = false;
194 }
195 ASSERT(!g_state.geometry_pipeline.NeedIndexInput());
196 g_state.geometry_pipeline.Setup(shader_engine);
197 g_state.geometry_pipeline.SubmitVertex(output);
198
199 // TODO: If drawing after every immediate mode triangle kills performance,
200 // change it to flush triangles whenever a drawing config register changes
201 // See: https://github.com/citra-emu/citra/pull/2866#issuecomment-327011550
202 VideoCore::g_renderer->Rasterizer()->DrawTriangles();
203 if (g_debug_context) {
204 g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
205 }
206 }
207 }
208 }
209}
210
211static void Draw(u32 command_id) {
212 MICROPROFILE_SCOPE(GPU_Drawing);
213 auto& regs = g_state.regs;
214
215#if PICA_LOG_TEV
216 DebugUtils::DumpTevStageConfig(regs.GetTevStages());
217#endif
218 if (g_debug_context)
219 g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
220
221 // Processes information about internal vertex attributes to figure out how a vertex is
222 // loaded.
223 // Later, these can be compiled and cached.
224 const u32 base_address = regs.pipeline.vertex_attributes.GetPhysicalBaseAddress();
225 VertexLoader loader(regs.pipeline);
226
227 // Load vertices
228 bool is_indexed = (command_id == PICA_REG_INDEX(pipeline.trigger_draw_indexed));
229
230 const auto& index_info = regs.pipeline.index_array;
231 const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
232 const u16* index_address_16 = reinterpret_cast<const u16*>(index_address_8);
233 bool index_u16 = index_info.format != 0;
234
235 PrimitiveAssembler<Shader::OutputVertex>& primitive_assembler = g_state.primitive_assembler;
236
237 if (g_debug_context && g_debug_context->recorder) {
238 for (int i = 0; i < 3; ++i) {
239 const auto texture = regs.texturing.GetTextures()[i];
240 if (!texture.enabled)
241 continue;
242
243 u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
244 g_debug_context->recorder->MemoryAccessed(
245 texture_data, Pica::TexturingRegs::NibblesPerPixel(texture.format) *
246 texture.config.width / 2 * texture.config.height,
247 texture.config.GetPhysicalAddress());
248 }
249 }
250
251 DebugUtils::MemoryAccessTracker memory_accesses;
252
253 // Simple circular-replacement vertex cache
254 // The size has been tuned for optimal balance between hit-rate and the cost of lookup
255 const size_t VERTEX_CACHE_SIZE = 32;
256 std::array<u16, VERTEX_CACHE_SIZE> vertex_cache_ids;
257 std::array<Shader::AttributeBuffer, VERTEX_CACHE_SIZE> vertex_cache;
258 Shader::AttributeBuffer vs_output;
259
260 unsigned int vertex_cache_pos = 0;
261 vertex_cache_ids.fill(-1);
262
263 auto* shader_engine = Shader::GetEngine();
264 Shader::UnitState shader_unit;
265
266 shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
267
268 g_state.geometry_pipeline.Reconfigure();
269 g_state.geometry_pipeline.Setup(shader_engine);
270 if (g_state.geometry_pipeline.NeedIndexInput())
271 ASSERT(is_indexed);
272
273 for (unsigned int index = 0; index < regs.pipeline.num_vertices; ++index) {
274 // Indexed rendering doesn't use the start offset
275 unsigned int vertex = is_indexed
276 ? (index_u16 ? index_address_16[index] : index_address_8[index])
277 : (index + regs.pipeline.vertex_offset);
278
279 // -1 is a common special value used for primitive restart. Since it's unknown if
280 // the PICA supports it, and it would mess up the caching, guard against it here.
281 ASSERT(vertex != -1);
282
283 bool vertex_cache_hit = false;
284
285 if (is_indexed) {
286 if (g_state.geometry_pipeline.NeedIndexInput()) {
287 g_state.geometry_pipeline.SubmitIndex(vertex);
288 continue;
289 }
290
291 if (g_debug_context && Pica::g_debug_context->recorder) {
292 int size = index_u16 ? 2 : 1;
293 memory_accesses.AddAccess(base_address + index_info.offset + size * index, size);
294 }
295
296 for (unsigned int i = 0; i < VERTEX_CACHE_SIZE; ++i) {
297 if (vertex == vertex_cache_ids[i]) {
298 vs_output = vertex_cache[i];
299 vertex_cache_hit = true;
300 break;
301 }
302 }
303 }
304
305 if (!vertex_cache_hit) {
306 // Initialize data for the current vertex
307 Shader::AttributeBuffer input;
308 loader.LoadVertex(base_address, index, vertex, input, memory_accesses);
309
310 // Send to vertex shader
311 if (g_debug_context)
312 g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
313 (void*)&input);
314 shader_unit.LoadInput(regs.vs, input);
315 shader_engine->Run(g_state.vs, shader_unit);
316 shader_unit.WriteOutput(regs.vs, vs_output);
317
318 if (is_indexed) {
319 vertex_cache[vertex_cache_pos] = vs_output;
320 vertex_cache_ids[vertex_cache_pos] = vertex;
321 vertex_cache_pos = (vertex_cache_pos + 1) % VERTEX_CACHE_SIZE;
322 }
323 }
324
325 // Send to geometry pipeline
326 g_state.geometry_pipeline.SubmitVertex(vs_output);
327 }
328
329 for (auto& range : memory_accesses.ranges) {
330 g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
331 range.second, range.first);
332 }
333
334 VideoCore::g_renderer->Rasterizer()->DrawTriangles();
335 if (g_debug_context) {
336 g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
337 }
338}
339
122static void WritePicaReg(u32 id, u32 value, u32 mask) { 340static void WritePicaReg(u32 id, u32 value, u32 mask) {
123 auto& regs = g_state.regs; 341 auto& regs = g_state.regs;
124 342
@@ -168,95 +386,9 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) {
168 // Load default vertex input attributes 386 // Load default vertex input attributes
169 case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[0], 0x233): 387 case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[0], 0x233):
170 case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[1], 0x234): 388 case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[1], 0x234):
171 case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[2], 0x235): { 389 case PICA_REG_INDEX_WORKAROUND(pipeline.vs_default_attributes_setup.set_value[2], 0x235):
172 // TODO: Does actual hardware indeed keep an intermediate buffer or does 390 LoadDefaultVertexAttributes(value);
173 // it directly write the values?
174 default_attr_write_buffer[default_attr_counter++] = value;
175
176 // Default attributes are written in a packed format such that four float24 values are
177 // encoded in
178 // three 32-bit numbers. We write to internal memory once a full such vector is
179 // written.
180 if (default_attr_counter >= 3) {
181 default_attr_counter = 0;
182
183 auto& setup = regs.pipeline.vs_default_attributes_setup;
184
185 if (setup.index >= 16) {
186 LOG_ERROR(HW_GPU, "Invalid VS default attribute index %d", (int)setup.index);
187 break;
188 }
189
190 Math::Vec4<float24> attribute;
191
192 // NOTE: The destination component order indeed is "backwards"
193 attribute.w = float24::FromRaw(default_attr_write_buffer[0] >> 8);
194 attribute.z = float24::FromRaw(((default_attr_write_buffer[0] & 0xFF) << 16) |
195 ((default_attr_write_buffer[1] >> 16) & 0xFFFF));
196 attribute.y = float24::FromRaw(((default_attr_write_buffer[1] & 0xFFFF) << 8) |
197 ((default_attr_write_buffer[2] >> 24) & 0xFF));
198 attribute.x = float24::FromRaw(default_attr_write_buffer[2] & 0xFFFFFF);
199
200 LOG_TRACE(HW_GPU, "Set default VS attribute %x to (%f %f %f %f)", (int)setup.index,
201 attribute.x.ToFloat32(), attribute.y.ToFloat32(), attribute.z.ToFloat32(),
202 attribute.w.ToFloat32());
203
204 // TODO: Verify that this actually modifies the register!
205 if (setup.index < 15) {
206 g_state.input_default_attributes.attr[setup.index] = attribute;
207 setup.index++;
208 } else {
209 // Put each attribute into an immediate input buffer. When all specified immediate
210 // attributes are present, the Vertex Shader is invoked and everything is sent to
211 // the primitive assembler.
212
213 auto& immediate_input = g_state.immediate.input_vertex;
214 auto& immediate_attribute_id = g_state.immediate.current_attribute;
215
216 immediate_input.attr[immediate_attribute_id] = attribute;
217
218 if (immediate_attribute_id < regs.pipeline.max_input_attrib_index) {
219 immediate_attribute_id += 1;
220 } else {
221 MICROPROFILE_SCOPE(GPU_Drawing);
222 immediate_attribute_id = 0;
223
224 auto* shader_engine = Shader::GetEngine();
225 shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
226
227 // Send to vertex shader
228 if (g_debug_context)
229 g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
230 static_cast<void*>(&immediate_input));
231 Shader::UnitState shader_unit;
232 Shader::AttributeBuffer output{};
233
234 shader_unit.LoadInput(regs.vs, immediate_input);
235 shader_engine->Run(g_state.vs, shader_unit);
236 shader_unit.WriteOutput(regs.vs, output);
237
238 // Send to geometry pipeline
239 if (g_state.immediate.reset_geometry_pipeline) {
240 g_state.geometry_pipeline.Reconfigure();
241 g_state.immediate.reset_geometry_pipeline = false;
242 }
243 ASSERT(!g_state.geometry_pipeline.NeedIndexInput());
244 g_state.geometry_pipeline.Setup(shader_engine);
245 g_state.geometry_pipeline.SubmitVertex(output);
246
247 // TODO: If drawing after every immediate mode triangle kills performance,
248 // change it to flush triangles whenever a drawing config register changes
249 // See: https://github.com/citra-emu/citra/pull/2866#issuecomment-327011550
250 VideoCore::g_renderer->Rasterizer()->DrawTriangles();
251 if (g_debug_context) {
252 g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch,
253 nullptr);
254 }
255 }
256 }
257 }
258 break; 391 break;
259 }
260 392
261 case PICA_REG_INDEX(pipeline.gpu_mode): 393 case PICA_REG_INDEX(pipeline.gpu_mode):
262 // This register likely just enables vertex processing and doesn't need any special handling 394 // This register likely just enables vertex processing and doesn't need any special handling
@@ -275,136 +407,9 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) {
275 407
276 // It seems like these trigger vertex rendering 408 // It seems like these trigger vertex rendering
277 case PICA_REG_INDEX(pipeline.trigger_draw): 409 case PICA_REG_INDEX(pipeline.trigger_draw):
278 case PICA_REG_INDEX(pipeline.trigger_draw_indexed): { 410 case PICA_REG_INDEX(pipeline.trigger_draw_indexed):
279 MICROPROFILE_SCOPE(GPU_Drawing); 411 Draw(id);
280
281#if PICA_LOG_TEV
282 DebugUtils::DumpTevStageConfig(regs.GetTevStages());
283#endif
284 if (g_debug_context)
285 g_debug_context->OnEvent(DebugContext::Event::IncomingPrimitiveBatch, nullptr);
286
287 // Processes information about internal vertex attributes to figure out how a vertex is
288 // loaded.
289 // Later, these can be compiled and cached.
290 const u32 base_address = regs.pipeline.vertex_attributes.GetPhysicalBaseAddress();
291 VertexLoader loader(regs.pipeline);
292
293 // Load vertices
294 bool is_indexed = (id == PICA_REG_INDEX(pipeline.trigger_draw_indexed));
295
296 const auto& index_info = regs.pipeline.index_array;
297 const u8* index_address_8 = Memory::GetPhysicalPointer(base_address + index_info.offset);
298 const u16* index_address_16 = reinterpret_cast<const u16*>(index_address_8);
299 bool index_u16 = index_info.format != 0;
300
301 PrimitiveAssembler<Shader::OutputVertex>& primitive_assembler = g_state.primitive_assembler;
302
303 if (g_debug_context && g_debug_context->recorder) {
304 for (int i = 0; i < 3; ++i) {
305 const auto texture = regs.texturing.GetTextures()[i];
306 if (!texture.enabled)
307 continue;
308
309 u8* texture_data = Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
310 g_debug_context->recorder->MemoryAccessed(
311 texture_data, Pica::TexturingRegs::NibblesPerPixel(texture.format) *
312 texture.config.width / 2 * texture.config.height,
313 texture.config.GetPhysicalAddress());
314 }
315 }
316
317 DebugUtils::MemoryAccessTracker memory_accesses;
318
319 // Simple circular-replacement vertex cache
320 // The size has been tuned for optimal balance between hit-rate and the cost of lookup
321 const size_t VERTEX_CACHE_SIZE = 32;
322 std::array<u16, VERTEX_CACHE_SIZE> vertex_cache_ids;
323 std::array<Shader::AttributeBuffer, VERTEX_CACHE_SIZE> vertex_cache;
324 Shader::AttributeBuffer vs_output;
325
326 unsigned int vertex_cache_pos = 0;
327 vertex_cache_ids.fill(-1);
328
329 auto* shader_engine = Shader::GetEngine();
330 Shader::UnitState shader_unit;
331
332 shader_engine->SetupBatch(g_state.vs, regs.vs.main_offset);
333
334 g_state.geometry_pipeline.Reconfigure();
335 g_state.geometry_pipeline.Setup(shader_engine);
336 if (g_state.geometry_pipeline.NeedIndexInput())
337 ASSERT(is_indexed);
338
339 for (unsigned int index = 0; index < regs.pipeline.num_vertices; ++index) {
340 // Indexed rendering doesn't use the start offset
341 unsigned int vertex =
342 is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index])
343 : (index + regs.pipeline.vertex_offset);
344
345 // -1 is a common special value used for primitive restart. Since it's unknown if
346 // the PICA supports it, and it would mess up the caching, guard against it here.
347 ASSERT(vertex != -1);
348
349 bool vertex_cache_hit = false;
350
351 if (is_indexed) {
352 if (g_state.geometry_pipeline.NeedIndexInput()) {
353 g_state.geometry_pipeline.SubmitIndex(vertex);
354 continue;
355 }
356
357 if (g_debug_context && Pica::g_debug_context->recorder) {
358 int size = index_u16 ? 2 : 1;
359 memory_accesses.AddAccess(base_address + index_info.offset + size * index,
360 size);
361 }
362
363 for (unsigned int i = 0; i < VERTEX_CACHE_SIZE; ++i) {
364 if (vertex == vertex_cache_ids[i]) {
365 vs_output = vertex_cache[i];
366 vertex_cache_hit = true;
367 break;
368 }
369 }
370 }
371
372 if (!vertex_cache_hit) {
373 // Initialize data for the current vertex
374 Shader::AttributeBuffer input;
375 loader.LoadVertex(base_address, index, vertex, input, memory_accesses);
376
377 // Send to vertex shader
378 if (g_debug_context)
379 g_debug_context->OnEvent(DebugContext::Event::VertexShaderInvocation,
380 (void*)&input);
381 shader_unit.LoadInput(regs.vs, input);
382 shader_engine->Run(g_state.vs, shader_unit);
383 shader_unit.WriteOutput(regs.vs, vs_output);
384
385 if (is_indexed) {
386 vertex_cache[vertex_cache_pos] = vs_output;
387 vertex_cache_ids[vertex_cache_pos] = vertex;
388 vertex_cache_pos = (vertex_cache_pos + 1) % VERTEX_CACHE_SIZE;
389 }
390 }
391
392 // Send to geometry pipeline
393 g_state.geometry_pipeline.SubmitVertex(vs_output);
394 }
395
396 for (auto& range : memory_accesses.ranges) {
397 g_debug_context->recorder->MemoryAccessed(Memory::GetPhysicalPointer(range.first),
398 range.second, range.first);
399 }
400
401 VideoCore::g_renderer->Rasterizer()->DrawTriangles();
402 if (g_debug_context) {
403 g_debug_context->OnEvent(DebugContext::Event::FinishedPrimitiveBatch, nullptr);
404 }
405
406 break; 412 break;
407 }
408 413
409 case PICA_REG_INDEX(gs.bool_uniforms): 414 case PICA_REG_INDEX(gs.bool_uniforms):
410 WriteUniformBoolReg(g_state.gs, g_state.regs.gs.bool_uniforms.Value()); 415 WriteUniformBoolReg(g_state.gs, g_state.regs.gs.bool_uniforms.Value());