diff options
| author | 2021-01-09 03:30:07 -0300 | |
|---|---|---|
| committer | 2021-07-22 21:51:21 -0400 | |
| commit | 2d48a7b4d0666ad16d03a22d85712617a0849046 (patch) | |
| tree | dd1069afca86f66e77e3438da77421a43adf5091 /src/shader_recompiler/file_environment.cpp | |
| parent | thread_worker: Fix compile time error (diff) | |
| download | yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.gz yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.xz yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.zip | |
shader: Initial recompiler work
Diffstat (limited to 'src/shader_recompiler/file_environment.cpp')
| -rw-r--r-- | src/shader_recompiler/file_environment.cpp | 42 |
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 @@ | |||
| 1 | #include <cstdio> | ||
| 2 | |||
| 3 | #include "exception.h" | ||
| 4 | #include "file_environment.h" | ||
| 5 | |||
| 6 | namespace Shader { | ||
| 7 | |||
| 8 | FileEnvironment::FileEnvironment(const char* path) { | ||
| 9 | std::FILE* const file{std::fopen(path, "rb")}; | ||
| 10 | if (!file) { | ||
| 11 | throw RuntimeError("Failed to open file='{}'", path); | ||
| 12 | } | ||
| 13 | std::fseek(file, 0, SEEK_END); | ||
| 14 | const long size{std::ftell(file)}; | ||
| 15 | std::rewind(file); | ||
| 16 | if (size % 8 != 0) { | ||
| 17 | std::fclose(file); | ||
| 18 | throw RuntimeError("File size={} is not aligned to 8", size); | ||
| 19 | } | ||
| 20 | // TODO: Use a unique_ptr to avoid zero-initializing this | ||
| 21 | const size_t num_inst{static_cast<size_t>(size) / 8}; | ||
| 22 | data.resize(num_inst); | ||
| 23 | if (std::fread(data.data(), 8, num_inst, file) != num_inst) { | ||
| 24 | std::fclose(file); | ||
| 25 | throw RuntimeError("Failed to read instructions={} from file='{}'", num_inst, path); | ||
| 26 | } | ||
| 27 | std::fclose(file); | ||
| 28 | } | ||
| 29 | |||
| 30 | FileEnvironment::~FileEnvironment() = default; | ||
| 31 | |||
| 32 | u64 FileEnvironment::ReadInstruction(u32 offset) const { | ||
| 33 | if (offset % 8 != 0) { | ||
| 34 | throw InvalidArgument("offset={} is not aligned to 8", offset); | ||
| 35 | } | ||
| 36 | if (offset / 8 >= static_cast<u32>(data.size())) { | ||
| 37 | throw InvalidArgument("offset={} is out of bounds", offset); | ||
| 38 | } | ||
| 39 | return data[offset / 8]; | ||
| 40 | } | ||
| 41 | |||
| 42 | } // namespace Shader | ||