summaryrefslogtreecommitdiffstats
path: root/src/shader_recompiler/file_environment.cpp
diff options
context:
space:
mode:
authorReinUsesLisp <reinuseslisp@airmail.cc>2021-01-09 07:30:07 +0100
committerameerj <52414509+ameerj@users.noreply.github.com>2021-07-23 03:51:21 +0200
commit2d48a7b4d0666ad16d03a22d85712617a0849046 (patch)
treedd1069afca86f66e77e3438da77421a43adf5091 /src/shader_recompiler/file_environment.cpp
parentthread_worker: Fix compile time error (diff)
downloadyuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar
yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.gz
yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.bz2
yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.lz
yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.xz
yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.zst
yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.zip
Diffstat (limited to 'src/shader_recompiler/file_environment.cpp')
-rw-r--r--src/shader_recompiler/file_environment.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/shader_recompiler/file_environment.cpp b/src/shader_recompiler/file_environment.cpp
new file mode 100644
index 000000000..b34bf462b
--- /dev/null
+++ b/src/shader_recompiler/file_environment.cpp
@@ -0,0 +1,42 @@
+#include <cstdio>
+
+#include "exception.h"
+#include "file_environment.h"
+
+namespace Shader {
+
+FileEnvironment::FileEnvironment(const char* path) {
+ std::FILE* const file{std::fopen(path, "rb")};
+ if (!file) {
+ throw RuntimeError("Failed to open file='{}'", path);
+ }
+ std::fseek(file, 0, SEEK_END);
+ const long size{std::ftell(file)};
+ std::rewind(file);
+ if (size % 8 != 0) {
+ std::fclose(file);
+ throw RuntimeError("File size={} is not aligned to 8", size);
+ }
+ // TODO: Use a unique_ptr to avoid zero-initializing this
+ const size_t num_inst{static_cast<size_t>(size) / 8};
+ data.resize(num_inst);
+ if (std::fread(data.data(), 8, num_inst, file) != num_inst) {
+ std::fclose(file);
+ throw RuntimeError("Failed to read instructions={} from file='{}'", num_inst, path);
+ }
+ std::fclose(file);
+}
+
+FileEnvironment::~FileEnvironment() = default;
+
+u64 FileEnvironment::ReadInstruction(u32 offset) const {
+ if (offset % 8 != 0) {
+ throw InvalidArgument("offset={} is not aligned to 8", offset);
+ }
+ if (offset / 8 >= static_cast<u32>(data.size())) {
+ throw InvalidArgument("offset={} is out of bounds", offset);
+ }
+ return data[offset / 8];
+}
+
+} // namespace Shader