2022-12-03 11:54:34 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett Terpstra 6920201 on 03/12/22.
|
|
|
|
* Copyright (c) 2022 Brett Terpstra. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEP_3_MEMORY_UTIL_H
|
|
|
|
#define STEP_3_MEMORY_UTIL_H
|
|
|
|
|
|
|
|
#include <engine/util/std.h>
|
|
|
|
|
|
|
|
#define ENDIAN_FLIP
|
|
|
|
|
|
|
|
namespace Raytracing {
|
|
|
|
|
|
|
|
class MemoryConvert {
|
|
|
|
private:
|
|
|
|
public:
|
|
|
|
/**
|
2022-12-10 14:25:09 -05:00
|
|
|
* returns the bytes from the type T ordered based on if ENDIAN_FLIP is defined or not
|
2022-12-03 11:54:34 -05:00
|
|
|
*/
|
2022-12-10 14:25:09 -05:00
|
|
|
template<typename T>
|
|
|
|
inline static std::vector<unsigned char> getBytes(T t) {
|
2022-12-03 11:54:34 -05:00
|
|
|
std::vector<unsigned char> bytes;
|
2022-12-10 14:25:09 -05:00
|
|
|
auto* asBytes = reinterpret_cast<unsigned char*>(&t);
|
2022-12-03 11:54:34 -05:00
|
|
|
#ifdef ENDIAN_FLIP
|
2022-12-10 14:25:09 -05:00
|
|
|
for (int i = 0; i < sizeof(T); i++)
|
|
|
|
bytes.push_back(asBytes[i]);
|
2022-12-03 11:54:34 -05:00
|
|
|
#else
|
2022-12-10 14:25:09 -05:00
|
|
|
for (int i = 0; i < sizeof(T); i++)
|
|
|
|
bytes.push_back(asBytes[sizeof(T) - i]);
|
2022-12-03 11:54:34 -05:00
|
|
|
#endif
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-10 14:25:09 -05:00
|
|
|
* Note: this is used for the GPU
|
|
|
|
* Converts the vector to single floating point format.
|
2022-12-03 11:54:34 -05:00
|
|
|
* @param array array to write into
|
|
|
|
* @param offset offset of where to start writing into. Will update this value as it moves through the vector
|
|
|
|
* @param vec the vector to write
|
|
|
|
*/
|
|
|
|
inline static void writeVectorBytes(unsigned char* array, size_t& offset, const Vec4& vec) {
|
2022-12-10 14:25:09 -05:00
|
|
|
auto x = getBytes((float) vec.x());
|
|
|
|
auto y = getBytes((float) vec.y());
|
|
|
|
auto z = getBytes((float) vec.z());
|
2022-12-12 02:07:59 -05:00
|
|
|
// we don't really use the w vector but this is a TODO
|
|
|
|
//auto w = getBytes((float) vec.w());
|
2022-12-03 11:54:34 -05:00
|
|
|
// write the bytes as a packed vector.
|
|
|
|
for (auto b: x)
|
|
|
|
array[offset++] = b;
|
|
|
|
for (auto b: y)
|
|
|
|
array[offset++] = b;
|
|
|
|
for (auto b: z)
|
|
|
|
array[offset++] = b;
|
2022-12-12 02:07:59 -05:00
|
|
|
//for (auto b: w)
|
|
|
|
// array[offset++] = b;
|
2022-12-03 11:54:34 -05:00
|
|
|
}
|
2022-12-10 14:25:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: this is used for the GPU.
|
|
|
|
* @param array array to write into
|
|
|
|
* @param offset offset of where to start writing into. Will update this value as it moves through the vector
|
|
|
|
* @param integer the int to write
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
inline static void writeBytes(unsigned char* array, size_t& offset, const T t) {
|
|
|
|
auto x = getBytes(t);
|
|
|
|
for (auto b: x)
|
|
|
|
array[offset++] = b;
|
|
|
|
}
|
2022-12-03 11:54:34 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //STEP_3_MEMORY_UTIL_H
|