Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Dec 13, 2023
1 parent 2df83c1 commit 7436a6a
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 152 deletions.
25 changes: 22 additions & 3 deletions ashura/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ constexpr u32 MAX_COMPUTE_GROUP_COUNT_Y = 1024;
constexpr u32 MAX_COMPUTE_GROUP_COUNT_Z = 1024;
constexpr u32 MAX_SWAPCHAIN_IMAGES = 8;

typedef Vec2U Offset;
typedef Vec2U Extent;
typedef Vec3U Offset3D;
typedef Vec3U Extent3D;
typedef u64 FrameId;
typedef u64 Generation;
typedef struct Buffer_T *Buffer;
typedef struct BufferView_T *BufferView;
typedef struct Image_T *Image;
Expand Down Expand Up @@ -761,7 +766,8 @@ struct MemoryRange

struct Viewport
{
Rect area;
Vec2 offset;
Vec2 extent;
f32 min_depth = 0;
f32 max_depth = 0;
};
Expand Down Expand Up @@ -1224,7 +1230,7 @@ struct SwapchainDesc
/// avoid storing pointers to its data members.
struct SwapchainInfo
{
u64 generation = 0;
Generation generation = 0;
Extent extent = {};
SurfaceFormat format = {};
Span<Image const> images = {};
Expand Down Expand Up @@ -1497,7 +1503,9 @@ struct DeviceInterface
Result<u32, Status> (*get_surface_formats)(
Device self, Surface surface, Span<SurfaceFormat> formats) = nullptr;
Result<u32, Status> (*get_surface_present_modes)(
Device self, Surface surface, Span<PresentMode> modes) = nullptr;
Device self, Surface surface, Span<PresentMode> modes) = nullptr;
Result<ImageUsage, Status> (*get_surface_usage)(Device self,
Surface surface) = nullptr;
Result<SwapchainInfo, Status> (*get_swapchain_info)(
Device self, Swapchain swapchain) = nullptr;
Result<Void, Status> (*invalidate_swapchain)(
Expand Down Expand Up @@ -1708,6 +1716,9 @@ struct DeviceImpl
//
//

//
//
//
struct InstanceInterface
{
Result<Instance, Status> (*create)(AllocatorImpl allocator) = nullptr;
Expand All @@ -1719,6 +1730,14 @@ struct InstanceInterface
AllocatorImpl allocator) = nullptr;
Result<Surface, Status> (*create_headless_surface)(Instance instance) =
nullptr;
void create_win32_surface();
void create_x11_surface();
void create_wayland_surface();
void create_xlib_surface();
void create_metal_surface();
void create_macos_surface();
void create_ios_surface();
void create_android_surface();
};

} // namespace gfx
Expand Down
12 changes: 12 additions & 0 deletions ashura/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "types.h"
#include <cmath>

namespace ash
{
namespace math
{

}
} // namespace ash
13 changes: 1 addition & 12 deletions ashura/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
namespace ash
{

template <typename T>
constexpr bool has_bits(T src, T cmp)
{
return (src & cmp) == cmp;
}

template <typename T>
constexpr bool has_any_bit(T src, T cmp)
{
return (src & cmp) != (T) 0;
}

using Clock = std::chrono::steady_clock; // monotonic system clock
using timepoint = Clock::time_point;
using nanoseconds = std::chrono::nanoseconds;
Expand Down Expand Up @@ -297,6 +285,7 @@ struct tri
};

// each coordinate is an edge of the quad
// TODO(lamarrr): rename this
struct Quad
{
Vec2 p0, p1, p2, p3;
Expand Down
175 changes: 175 additions & 0 deletions ashura/span.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#pragma once
#include "ashura/types.h"

namespace ash
{

template <typename T>
struct Span
{
T *data = nullptr;
usize size = 0;

constexpr usize size_bytes() const
{
return sizeof(T) * size;
}

constexpr bool is_empty() const
{
return size == 0;
}

constexpr T *begin() const
{
return data;
}

constexpr T *end() const
{
return data + size;
}

constexpr T &operator[](usize index) const
{
return data[index];
}

constexpr Span<T> operator[](Slice slice) const
{
// written such that overflow will not occur even if both offset and size
// are set to USIZE_MAX
slice.offset = slice.offset > size ? size : slice.offset;
slice.size =
(size - slice.offset) > slice.size ? slice.size : (size - slice.offset);
return Span<T>{data + slice.offset, slice.size};
}

constexpr operator Span<T const>() const
{
return Span<T const>{data, size};
}
};

// A span with bit access semantics
template <typename UnsignedInteger>
struct BitSpan
{
static constexpr u32 NUM_BITS_PER_PACK = sizeof(UnsignedInteger) << 3;

Span<UnsignedInteger> body = {};
Slice current_slice = {};

constexpr bool is_empty() const
{
return current_slice.size == 0;
}

constexpr bool operator[](usize offset) const
{
offset = current_slice.offset + offset;
bool bit = (body.data[offset / NUM_BITS_PER_PACK] >>
(offset % NUM_BITS_PER_PACK)) &
1U;
return bit;
}

constexpr void set(usize offset, bool bit) const
{
offset = current_slice.offset + offset;
UnsignedInteger &out = body.data[offset / NUM_BITS_PER_PACK];
usize bit_offset = offset % NUM_BITS_PER_PACK;
out = (out & ~((UnsignedInteger) 1 << bit_offset)) |
((UnsignedInteger) bit << bit_offset);
}

constexpr void toggle(usize offset) const
{
offset = current_slice.offset + offset;
UnsignedInteger &out = body.data[offset / NUM_BITS_PER_PACK];
out = out ^ (1ULL << (offset % NUM_BITS_PER_PACK));
}

constexpr BitSpan operator[](Slice slice) const
{
slice.offset += current_slice.offset;
slice.offset =
slice.offset > current_slice.size ? current_slice.size : slice.offset;
slice.size = (current_slice.size - slice.offset) > slice.size ?
slice.size :
(current_slice.size - slice.offset);
return BitSpan{body, slice};
}
};

namespace span
{
template <typename T>
constexpr Span<T> slice(Span<T> self, usize offset)
{
return self[Slice{offset, USIZE_MAX}];
}

template <typename T>
constexpr Span<T> slice(Span<T> self, usize offset, usize size)
{
return self[Slice{offset, size}];
}

template <typename T>
constexpr BitSpan<T> slice(BitSpan<T> span, usize offset)
{
return span[Slice{offset, USIZE_MAX}];
}

template <typename T>
constexpr BitSpan<T> slice(BitSpan<T> span, usize bit_offset, usize bit_size)
{
return span[Slice{bit_offset, bit_size}];
}

template <typename T>
constexpr Span<T const> as_const(Span<T> self)
{
return Span<T const>{self.data, self.size};
}

template <typename T>
constexpr Span<u8> as_u8(Span<T> self)
{
return Span<u8>{reinterpret_cast<u8 *>(self.data), self.size_bytes()};
}

template <typename T>
constexpr Span<u8 const> as_u8(Span<T const> self)
{
return Span<u8 const>{reinterpret_cast<u8 const *>(self.data),
self.size_bytes()};
}

template <typename T>
constexpr Span<char> as_char(Span<T> self)
{
return Span<char>{reinterpret_cast<char *>(self.data), self.size_bytes()};
}

template <typename T>
constexpr Span<char const> as_char(Span<T const> self)
{
return Span<char const>{reinterpret_cast<char const *>(self.data),
self.size_bytes()};
}

template <typename T, usize N>
constexpr Span<T> from_array(T (&array)[N])
{
return Span<T>{array, N};
}

template <typename StdContainer>
constexpr auto from_std_container(StdContainer &container)
{
return Span{container.data(), container.size()};
}
} // namespace span
} // namespace ash
Loading

0 comments on commit 7436a6a

Please sign in to comment.