2021-01-09 09:30:07 +03:00
|
|
|
#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;
|
|
|
|
|
2021-02-17 06:59:28 +03:00
|
|
|
u64 FileEnvironment::ReadInstruction(u32 offset) {
|
2021-01-09 09:30:07 +03:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2021-03-23 03:03:20 +03:00
|
|
|
u32 FileEnvironment::TextureBoundBuffer() const {
|
2021-03-09 00:31:53 +03:00
|
|
|
throw NotImplementedException("Texture bound buffer serialization");
|
|
|
|
}
|
|
|
|
|
2021-03-23 03:03:20 +03:00
|
|
|
std::array<u32, 3> FileEnvironment::WorkgroupSize() const {
|
2021-02-17 06:59:28 +03:00
|
|
|
return {1, 1, 1};
|
|
|
|
}
|
|
|
|
|
2021-01-09 09:30:07 +03:00
|
|
|
} // namespace Shader
|