2022-02-18 00:38:31 +00:00
|
|
|
#pragma once
|
|
|
|
#include <aurora/gfx.hpp>
|
|
|
|
|
2022-05-02 23:42:59 +00:00
|
|
|
#include <type_traits>
|
2022-02-18 00:38:31 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <dawn/webgpu_cpp.h>
|
2022-02-27 16:05:48 +00:00
|
|
|
#include <xxhash_impl.h>
|
2022-02-18 00:38:31 +00:00
|
|
|
|
2022-02-19 05:33:56 +00:00
|
|
|
#ifndef ALIGN
|
|
|
|
#define ALIGN(x, a) (((x) + ((a)-1)) & ~((a)-1))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace aurora {
|
2022-05-02 23:42:59 +00:00
|
|
|
static inline XXH64_hash_t xxh3_hash_s(const void* input, size_t len, XXH64_hash_t seed = 0) {
|
2022-02-19 05:33:56 +00:00
|
|
|
return XXH3_64bits_withSeed(input, len, seed);
|
|
|
|
}
|
2022-02-18 00:38:31 +00:00
|
|
|
template <typename T>
|
2022-02-19 05:33:56 +00:00
|
|
|
static inline XXH64_hash_t xxh3_hash(const T& input, XXH64_hash_t seed = 0) {
|
2022-05-02 23:42:59 +00:00
|
|
|
// Validate that the type has no padding bytes, which can easily cause
|
|
|
|
// hash mismatches. This also disallows floats, but that's okay for us.
|
|
|
|
static_assert(std::has_unique_object_representations_v<T>);
|
|
|
|
return xxh3_hash_s(&input, sizeof(T), seed);
|
2022-02-18 00:38:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 05:33:56 +00:00
|
|
|
class ByteBuffer {
|
|
|
|
public:
|
2022-03-04 07:47:21 +00:00
|
|
|
ByteBuffer() noexcept = default;
|
|
|
|
explicit ByteBuffer(size_t size) noexcept
|
|
|
|
: m_data(static_cast<uint8_t*>(calloc(1, size))), m_length(size), m_capacity(size) {}
|
2022-03-08 22:19:02 +00:00
|
|
|
explicit ByteBuffer(uint8_t* data, size_t size) noexcept
|
|
|
|
: m_data(data), m_length(0), m_capacity(size), m_owned(false) {}
|
2022-03-04 07:47:21 +00:00
|
|
|
~ByteBuffer() noexcept {
|
2022-03-08 22:19:02 +00:00
|
|
|
if (m_data != nullptr && m_owned) {
|
2022-02-19 05:33:56 +00:00
|
|
|
free(m_data);
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 22:19:02 +00:00
|
|
|
ByteBuffer(ByteBuffer&& rhs) noexcept
|
|
|
|
: m_data(rhs.m_data), m_length(rhs.m_length), m_capacity(rhs.m_capacity), m_owned(rhs.m_owned) {
|
2022-03-04 07:47:21 +00:00
|
|
|
rhs.m_data = nullptr;
|
|
|
|
rhs.m_length = 0;
|
|
|
|
rhs.m_capacity = 0;
|
2022-03-08 22:19:02 +00:00
|
|
|
rhs.m_owned = true;
|
2022-03-04 07:47:21 +00:00
|
|
|
}
|
|
|
|
ByteBuffer& operator=(ByteBuffer&& rhs) noexcept {
|
2022-03-08 22:19:02 +00:00
|
|
|
if (m_data != nullptr && m_owned) {
|
2022-03-04 07:47:21 +00:00
|
|
|
free(m_data);
|
|
|
|
}
|
|
|
|
m_data = rhs.m_data;
|
|
|
|
m_length = rhs.m_length;
|
|
|
|
m_capacity = rhs.m_capacity;
|
2022-03-08 22:19:02 +00:00
|
|
|
m_owned = rhs.m_owned;
|
2022-03-04 07:47:21 +00:00
|
|
|
rhs.m_data = nullptr;
|
|
|
|
rhs.m_length = 0;
|
|
|
|
rhs.m_capacity = 0;
|
2022-03-08 22:19:02 +00:00
|
|
|
rhs.m_owned = true;
|
2022-03-04 07:47:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ByteBuffer(ByteBuffer const&) = delete;
|
|
|
|
ByteBuffer& operator=(ByteBuffer const&) = delete;
|
2022-02-19 05:33:56 +00:00
|
|
|
|
2022-03-04 07:47:21 +00:00
|
|
|
[[nodiscard]] uint8_t* data() noexcept { return m_data; }
|
|
|
|
[[nodiscard]] const uint8_t* data() const noexcept { return m_data; }
|
|
|
|
[[nodiscard]] size_t size() const noexcept { return m_length; }
|
|
|
|
[[nodiscard]] bool empty() const noexcept { return m_length == 0; }
|
2022-02-19 05:33:56 +00:00
|
|
|
|
|
|
|
void append(const void* data, size_t size) {
|
2022-03-08 22:19:02 +00:00
|
|
|
resize(m_length + size, false);
|
2022-02-19 05:33:56 +00:00
|
|
|
memcpy(m_data + m_length, data, size);
|
|
|
|
m_length += size;
|
|
|
|
}
|
2022-02-19 08:28:44 +00:00
|
|
|
|
2022-02-19 05:33:56 +00:00
|
|
|
void append_zeroes(size_t size) {
|
2022-03-08 22:19:02 +00:00
|
|
|
resize(m_length + size, true);
|
2022-02-19 05:33:56 +00:00
|
|
|
m_length += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2022-03-08 22:19:02 +00:00
|
|
|
if (m_data != nullptr && m_owned) {
|
2022-02-19 05:33:56 +00:00
|
|
|
free(m_data);
|
|
|
|
}
|
|
|
|
m_data = nullptr;
|
|
|
|
m_length = 0;
|
|
|
|
m_capacity = 0;
|
2022-03-08 22:19:02 +00:00
|
|
|
m_owned = true;
|
2022-02-19 05:33:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 22:19:02 +00:00
|
|
|
void reserve_extra(size_t size) { resize(m_length + size, true); }
|
|
|
|
|
2022-02-19 05:33:56 +00:00
|
|
|
private:
|
|
|
|
uint8_t* m_data = nullptr;
|
|
|
|
size_t m_length = 0;
|
|
|
|
size_t m_capacity = 0;
|
2022-03-08 22:19:02 +00:00
|
|
|
bool m_owned = true;
|
2022-02-19 08:28:44 +00:00
|
|
|
|
2022-03-08 22:19:02 +00:00
|
|
|
void resize(size_t size, bool zeroed) {
|
2022-02-19 08:28:44 +00:00
|
|
|
if (size == 0) {
|
|
|
|
clear();
|
|
|
|
} else if (m_data == nullptr) {
|
2022-03-08 22:19:02 +00:00
|
|
|
if (zeroed) {
|
|
|
|
m_data = static_cast<uint8_t*>(calloc(1, size));
|
|
|
|
} else {
|
|
|
|
m_data = static_cast<uint8_t*>(malloc(size));
|
|
|
|
}
|
|
|
|
m_owned = true;
|
2022-02-19 08:28:44 +00:00
|
|
|
} else if (size > m_capacity) {
|
2022-03-08 22:19:02 +00:00
|
|
|
if (!m_owned) {
|
|
|
|
abort();
|
|
|
|
}
|
2022-02-19 08:28:44 +00:00
|
|
|
m_data = static_cast<uint8_t*>(realloc(m_data, size));
|
2022-03-08 22:19:02 +00:00
|
|
|
if (zeroed) {
|
|
|
|
memset(m_data + m_capacity, 0, size - m_capacity);
|
|
|
|
}
|
2022-02-19 08:28:44 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_capacity = size;
|
|
|
|
}
|
2022-02-19 05:33:56 +00:00
|
|
|
};
|
2022-02-18 00:38:31 +00:00
|
|
|
} // namespace aurora
|
|
|
|
|
|
|
|
namespace aurora::gfx {
|
2022-02-19 05:33:56 +00:00
|
|
|
extern wgpu::Buffer g_vertexBuffer;
|
|
|
|
extern wgpu::Buffer g_uniformBuffer;
|
|
|
|
extern wgpu::Buffer g_indexBuffer;
|
2022-03-08 05:28:31 +00:00
|
|
|
extern wgpu::Buffer g_storageBuffer;
|
2022-03-15 06:18:45 +00:00
|
|
|
extern size_t g_staticStorageLastSize;
|
2022-05-24 00:26:35 +00:00
|
|
|
struct TextureUpload {
|
|
|
|
wgpu::TextureDataLayout layout;
|
|
|
|
wgpu::ImageCopyTexture tex;
|
|
|
|
wgpu::Extent3D size;
|
|
|
|
|
|
|
|
TextureUpload(wgpu::TextureDataLayout layout, wgpu::ImageCopyTexture tex, wgpu::Extent3D size) noexcept
|
|
|
|
: layout(std::move(layout)), tex(std::move(tex)), size(std::move(size)) {}
|
|
|
|
};
|
|
|
|
extern std::vector<TextureUpload> g_textureUploads;
|
2022-05-09 21:13:38 +00:00
|
|
|
// TODO this is a bad place for this...
|
|
|
|
extern std::vector<TextureHandle> g_resolvedTextures;
|
2022-02-19 05:33:56 +00:00
|
|
|
|
2022-05-13 23:40:19 +00:00
|
|
|
constexpr GX::TextureFormat InvalidTextureFormat = static_cast<GX::TextureFormat>(-1);
|
2022-02-18 00:38:31 +00:00
|
|
|
struct TextureRef {
|
|
|
|
wgpu::Texture texture;
|
|
|
|
wgpu::TextureView view;
|
|
|
|
wgpu::Extent3D size;
|
|
|
|
wgpu::TextureFormat format;
|
2022-03-04 07:47:21 +00:00
|
|
|
uint32_t mipCount;
|
2022-05-13 23:40:19 +00:00
|
|
|
GX::TextureFormat gxFormat;
|
|
|
|
bool isRenderTexture; // :shrug: for now
|
2022-03-04 07:47:21 +00:00
|
|
|
|
|
|
|
TextureRef(wgpu::Texture&& texture, wgpu::TextureView&& view, wgpu::Extent3D size, wgpu::TextureFormat format,
|
2022-05-13 23:40:19 +00:00
|
|
|
uint32_t mipCount, GX::TextureFormat gxFormat, bool isRenderTexture)
|
2022-03-04 07:47:21 +00:00
|
|
|
: texture(std::move(texture))
|
|
|
|
, view(std::move(view))
|
|
|
|
, size(size)
|
|
|
|
, format(format)
|
|
|
|
, mipCount(mipCount)
|
2022-05-13 23:40:19 +00:00
|
|
|
, gxFormat(gxFormat)
|
|
|
|
, isRenderTexture(isRenderTexture) {}
|
2022-02-18 00:38:31 +00:00
|
|
|
};
|
|
|
|
|
2022-02-19 05:33:56 +00:00
|
|
|
using BindGroupRef = uint64_t;
|
2022-03-05 03:36:54 +00:00
|
|
|
using PipelineRef = uint64_t;
|
|
|
|
using SamplerRef = uint64_t;
|
|
|
|
using ShaderRef = uint64_t;
|
2022-03-15 06:18:45 +00:00
|
|
|
struct Range {
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t size;
|
|
|
|
bool isStatic;
|
|
|
|
};
|
|
|
|
static inline uint32_t storage_offset(Range range) {
|
|
|
|
return range.isStatic ? range.offset : range.offset + g_staticStorageLastSize;
|
|
|
|
}
|
2022-02-18 00:38:31 +00:00
|
|
|
|
|
|
|
enum class ShaderType {
|
|
|
|
MoviePlayer,
|
2022-03-05 03:36:54 +00:00
|
|
|
Stream,
|
2022-03-08 05:28:31 +00:00
|
|
|
Model,
|
2022-02-18 00:38:31 +00:00
|
|
|
};
|
2022-02-19 05:33:56 +00:00
|
|
|
|
2022-02-19 06:41:21 +00:00
|
|
|
void initialize();
|
|
|
|
void shutdown();
|
2022-02-19 05:33:56 +00:00
|
|
|
|
2022-03-16 02:38:55 +00:00
|
|
|
void begin_frame();
|
2022-03-16 03:04:43 +00:00
|
|
|
void end_frame(const wgpu::CommandEncoder& cmd);
|
2022-05-09 21:13:38 +00:00
|
|
|
void render(wgpu::CommandEncoder& cmd);
|
|
|
|
void render_pass(const wgpu::RenderPassEncoder& pass, u32 idx);
|
2022-03-16 06:05:56 +00:00
|
|
|
void map_staging_buffer();
|
2022-02-19 05:33:56 +00:00
|
|
|
|
|
|
|
Range push_verts(const uint8_t* data, size_t length);
|
|
|
|
template <typename T>
|
|
|
|
static inline Range push_verts(ArrayRef<T> data) {
|
|
|
|
return push_verts(reinterpret_cast<const uint8_t*>(data.data()), data.size() * sizeof(T));
|
|
|
|
}
|
|
|
|
Range push_indices(const uint8_t* data, size_t length);
|
|
|
|
template <typename T>
|
|
|
|
static inline Range push_indices(ArrayRef<T> data) {
|
|
|
|
return push_indices(reinterpret_cast<const uint8_t*>(data.data()), data.size() * sizeof(T));
|
|
|
|
}
|
|
|
|
Range push_uniform(const uint8_t* data, size_t length);
|
|
|
|
template <typename T>
|
|
|
|
static inline Range push_uniform(const T& data) {
|
|
|
|
return push_uniform(reinterpret_cast<const uint8_t*>(&data), sizeof(T));
|
|
|
|
}
|
2022-03-08 05:28:31 +00:00
|
|
|
Range push_storage(const uint8_t* data, size_t length);
|
|
|
|
template <typename T>
|
2022-03-15 06:18:45 +00:00
|
|
|
static inline Range push_storage(ArrayRef<T> data) {
|
|
|
|
return push_storage(reinterpret_cast<const uint8_t*>(data.data()), data.size() * sizeof(T));
|
|
|
|
}
|
|
|
|
template <typename T>
|
2022-03-08 05:28:31 +00:00
|
|
|
static inline Range push_storage(const T& data) {
|
|
|
|
return push_storage(reinterpret_cast<const uint8_t*>(&data), sizeof(T));
|
|
|
|
}
|
2022-03-15 06:18:45 +00:00
|
|
|
Range push_static_storage(const uint8_t* data, size_t length);
|
|
|
|
template <typename T>
|
|
|
|
static inline Range push_static_storage(ArrayRef<T> data) {
|
|
|
|
return push_static_storage(reinterpret_cast<const uint8_t*>(data.data()), data.size() * sizeof(T));
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
static inline Range push_static_storage(const T& data) {
|
|
|
|
return push_static_storage(reinterpret_cast<const uint8_t*>(&data), sizeof(T));
|
|
|
|
}
|
2022-05-24 00:26:35 +00:00
|
|
|
Range push_texture_data(const uint8_t* data, size_t length, u32 bytesPerRow, u32 rowsPerImage);
|
2022-03-08 22:19:02 +00:00
|
|
|
std::pair<ByteBuffer, Range> map_verts(size_t length);
|
|
|
|
std::pair<ByteBuffer, Range> map_indices(size_t length);
|
|
|
|
std::pair<ByteBuffer, Range> map_uniform(size_t length);
|
|
|
|
std::pair<ByteBuffer, Range> map_storage(size_t length);
|
2022-02-19 05:33:56 +00:00
|
|
|
|
2022-03-05 03:36:54 +00:00
|
|
|
template <typename State>
|
|
|
|
const State& get_state();
|
|
|
|
template <typename DrawData>
|
|
|
|
void push_draw_command(DrawData data);
|
|
|
|
|
2022-02-19 05:33:56 +00:00
|
|
|
template <typename PipelineConfig>
|
|
|
|
PipelineRef pipeline_ref(PipelineConfig config);
|
|
|
|
bool bind_pipeline(PipelineRef ref, const wgpu::RenderPassEncoder& pass);
|
|
|
|
|
|
|
|
BindGroupRef bind_group_ref(const wgpu::BindGroupDescriptor& descriptor);
|
|
|
|
const wgpu::BindGroup& find_bind_group(BindGroupRef id);
|
|
|
|
|
2022-03-05 03:36:54 +00:00
|
|
|
const wgpu::Sampler& sampler_ref(const wgpu::SamplerDescriptor& descriptor);
|
|
|
|
|
|
|
|
uint32_t align_uniform(uint32_t value);
|
2022-02-18 00:38:31 +00:00
|
|
|
} // namespace aurora::gfx
|