Skip to content

Commit

Permalink
wip shaderc
Browse files Browse the repository at this point in the history
  • Loading branch information
yeetari committed Feb 16, 2024
1 parent d10f01a commit 18aa6ab
Show file tree
Hide file tree
Showing 17 changed files with 1,000 additions and 47 deletions.
4 changes: 2 additions & 2 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ add_shader(shaders/depth_reduce.comp)
add_shader(shaders/draw_cull.comp)
add_shader(shaders/fst.vert)
add_shader(shaders/light_cull.comp)
add_shader(shaders/object.vsl)
#add_shader(shaders/object.vsl)
add_shader(shaders/shadow.vert)
add_shader(shaders/skybox.frag)
add_shader(shaders/skybox.vert)
Expand All @@ -40,7 +40,7 @@ function(vull_depend_builtin target)
${CMAKE_BINARY_DIR}/engine/shaders/draw_cull.comp.spv /shaders/draw_cull.comp
${CMAKE_BINARY_DIR}/engine/shaders/fst.vert.spv /shaders/fst.vert
${CMAKE_BINARY_DIR}/engine/shaders/light_cull.comp.spv /shaders/light_cull.comp
${CMAKE_BINARY_DIR}/engine/shaders/object.vsl.spv /shaders/object
# ${CMAKE_BINARY_DIR}/engine/shaders/object.vsl.spv /shaders/object
${CMAKE_BINARY_DIR}/engine/shaders/shadow.vert.spv /shaders/shadow.vert
${CMAKE_BINARY_DIR}/engine/shaders/skybox.frag.spv /shaders/skybox.frag
${CMAKE_BINARY_DIR}/engine/shaders/skybox.vert.spv /shaders/skybox.vert
Expand Down
82 changes: 82 additions & 0 deletions engine/include/vull/shaderc/arena.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma once

#include <vull/container/vector.hh>
#include <vull/support/utility.hh>

#include <stddef.h>
#include <stdint.h>

namespace vull::shaderc {

class ArenaChunk {
static constexpr size_t k_size = 65536;
uint8_t *m_data;
size_t m_head{0};

public:
ArenaChunk() : m_data(new uint8_t[k_size]) {}
ArenaChunk(const ArenaChunk &) = delete;
ArenaChunk(ArenaChunk &&);
~ArenaChunk() { delete[] m_data; }

ArenaChunk &operator=(const ArenaChunk &) = delete;
ArenaChunk &operator=(ArenaChunk &&) = delete;

void *allocate(size_t size, size_t alignment);
};

class Arena {
Vector<ArenaChunk> m_chunks;
ArenaChunk *m_current_chunk;

public:
Arena() : m_current_chunk(&m_chunks.emplace()) {}
Arena(const Arena &) = delete;
Arena(Arena &&);
~Arena() = default;

Arena &operator=(const Arena &) = delete;
Arena &operator=(Arena &&) = delete;

template <typename U, typename... Args>
U *allocate(Args &&...args);
template <typename U>
void destroy(U *ptr);
};

inline ArenaChunk::ArenaChunk(ArenaChunk &&other) {
m_data = vull::exchange(other.m_data, nullptr);
m_head = vull::exchange(other.m_head, 0u);
}

inline void *ArenaChunk::allocate(size_t size, size_t alignment) {
size = (size + alignment - 1) & ~(alignment - 1);
if (m_head + size >= k_size) {
return nullptr;
}
auto *ptr = m_data + m_head;
m_head += size;
return ptr;
}

inline Arena::Arena(Arena &&other) {
m_chunks = vull::move(other.m_chunks);
m_current_chunk = vull::exchange(other.m_current_chunk, nullptr);
}

template <typename U, typename... Args>
U *Arena::allocate(Args &&...args) {
auto *ptr = m_current_chunk->allocate(sizeof(U), alignof(U));
if (ptr == nullptr) {
m_current_chunk = &m_chunks.emplace();
ptr = m_current_chunk->allocate(sizeof(U), alignof(U));
}
return new (ptr) U(vull::forward<Args>(args)...);
}

template <typename U>
void Arena::destroy(U *ptr) {
ptr->~U();
}

} // namespace vull::shaderc
Loading

0 comments on commit 18aa6ab

Please sign in to comment.