file_environment.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <cstdio>
  2. #include "exception.h"
  3. #include "file_environment.h"
  4. namespace Shader {
  5. FileEnvironment::FileEnvironment(const char* path) {
  6. std::FILE* const file{std::fopen(path, "rb")};
  7. if (!file) {
  8. throw RuntimeError("Failed to open file='{}'", path);
  9. }
  10. std::fseek(file, 0, SEEK_END);
  11. const long size{std::ftell(file)};
  12. std::rewind(file);
  13. if (size % 8 != 0) {
  14. std::fclose(file);
  15. throw RuntimeError("File size={} is not aligned to 8", size);
  16. }
  17. // TODO: Use a unique_ptr to avoid zero-initializing this
  18. const size_t num_inst{static_cast<size_t>(size) / 8};
  19. data.resize(num_inst);
  20. if (std::fread(data.data(), 8, num_inst, file) != num_inst) {
  21. std::fclose(file);
  22. throw RuntimeError("Failed to read instructions={} from file='{}'", num_inst, path);
  23. }
  24. std::fclose(file);
  25. }
  26. FileEnvironment::~FileEnvironment() = default;
  27. u64 FileEnvironment::ReadInstruction(u32 offset) {
  28. if (offset % 8 != 0) {
  29. throw InvalidArgument("offset={} is not aligned to 8", offset);
  30. }
  31. if (offset / 8 >= static_cast<u32>(data.size())) {
  32. throw InvalidArgument("offset={} is out of bounds", offset);
  33. }
  34. return data[offset / 8];
  35. }
  36. u32 FileEnvironment::TextureBoundBuffer() const {
  37. throw NotImplementedException("Texture bound buffer serialization");
  38. }
  39. std::array<u32, 3> FileEnvironment::WorkgroupSize() const {
  40. return {1, 1, 1};
  41. }
  42. } // namespace Shader