I started work on the following shim to export the c++ to c and to build odin bindings with but stopped since sebbbis api was changing every other day. You need to do a bit of work to compile it with the c++ project and make a dll to use in Odin.
I might continue later on with making the bindings with a dll included but I’m going to give it some more time for his codebase to become stable before making them. You are welcome to try it yourself with the following code though.
#include <nographicsapi_odin.h>
#include <NoGraphicsAPI/NoGraphicsAPI.hpp>
#include <string.h>
#include <type_traits>
namespace
{
template<typename Destination, typename Source>
Destination copy_value(const Source& source) noexcept
{
static_assert(sizeof(Destination) == sizeof(Source));
static_assert(alignof(Destination) == alignof(Source));
static_assert(std::is_trivially_copyable_v<Destination>);
static_assert(std::is_trivially_copyable_v<Source>);
static_assert(std::is_standard_layout_v<Destination>);
static_assert(std::is_standard_layout_v<Source>);
Destination destination{};
memcpy(&destination, &source, sizeof(destination));
return destination;
}
template<typename Destination, typename Source>
void store_value(Destination* destination, const Source& source) noexcept
{
static_assert(sizeof(Destination) == sizeof(Source));
static_assert(alignof(Destination) == alignof(Source));
static_assert(std::is_trivially_copyable_v<Destination>);
static_assert(std::is_trivially_copyable_v<Source>);
static_assert(std::is_standard_layout_v<Destination>);
static_assert(std::is_standard_layout_v<Source>);
memcpy(destination, &source, sizeof(*destination));
}
using GpuCpuRangeByte = gpu::GpuCpuRange<gpu::byte>;
#define NG_ASSERT_LAYOUT(C_TYPE, CPP_TYPE, EXPECTED_SIZE, EXPECTED_ALIGNMENT) \
static_assert(sizeof(C_TYPE) == (EXPECTED_SIZE)); \
static_assert(alignof(C_TYPE) == (EXPECTED_ALIGNMENT)); \
static_assert(sizeof(C_TYPE) == sizeof(CPP_TYPE)); \
static_assert(alignof(C_TYPE) == alignof(CPP_TYPE))
NG_ASSERT_LAYOUT(ng_uint32x2, gpu::uint32x2, 8, 4);
NG_ASSERT_LAYOUT(ng_uint32x3, gpu::uint32x3, 12, 4);
NG_ASSERT_LAYOUT(ng_SizeAlign, gpu::SizeAlign, 16, 8);
NG_ASSERT_LAYOUT(ng_GpuRange, gpu::GpuRange, 16, 8);
NG_ASSERT_LAYOUT(ng_GpuCpuRangeByte, GpuCpuRangeByte, 24, 8);
NG_ASSERT_LAYOUT(ng_GpuHeap, gpu::GpuHeap, 32, 8);
NG_ASSERT_LAYOUT(ng_TextureHeap, gpu::TextureHeap, 16, 8);
NG_ASSERT_LAYOUT(ng_TimelinePoint, gpu::TimelinePoint, 16, 8);
NG_ASSERT_LAYOUT(ng_CommandBufferSpan, gpu::Span<gpu::CommandBuffer* const>, 16, 8);
NG_ASSERT_LAYOUT(ng_TimelinePointSpan, gpu::Span<const gpu::TimelinePoint>, 16, 8);
NG_ASSERT_LAYOUT(ng_SubmitDesc, gpu::SubmitDesc, 48, 8);
NG_ASSERT_LAYOUT(ng_TextureFormatInfo, gpu::TextureFormatInfo, 16, 4);
NG_ASSERT_LAYOUT(ng_DeviceCaps, gpu::DeviceCaps, 72, 8);
NG_ASSERT_LAYOUT(ng_DeviceDesc, gpu::DeviceDesc, 32, 8);
NG_ASSERT_LAYOUT(ng_DeviceInit, gpu::DeviceInit, 16, 8);
NG_ASSERT_LAYOUT(ng_SwapchainFrame, gpu::SwapchainFrame, 16, 8);
NG_ASSERT_LAYOUT(ng_TextureDesc, gpu::TextureDesc, 32, 4);
NG_ASSERT_LAYOUT(ng_RenderViewDesc, gpu::RenderViewDesc, 8, 4);
NG_ASSERT_LAYOUT(ng_TextureDescriptorDesc, gpu::TextureDescriptorDesc, 20, 4);
NG_ASSERT_LAYOUT(ng_TextureCopyDesc, gpu::TextureCopyDesc, 56, 8);
NG_ASSERT_LAYOUT(ng_SamplerDesc, gpu::SamplerDesc, 9, 1);
NG_ASSERT_LAYOUT(ng_BlendComponentState, gpu::BlendComponentState, 3, 1);
NG_ASSERT_LAYOUT(ng_BlendState, gpu::BlendState, 7, 1);
NG_ASSERT_LAYOUT(ng_ColorTargetDesc, gpu::ColorTargetDesc, 9, 1);
NG_ASSERT_LAYOUT(ng_RasterizationState, gpu::RasterizationState, 16, 4);
NG_ASSERT_LAYOUT(ng_Viewport, gpu::Viewport, 24, 4);
NG_ASSERT_LAYOUT(ng_Scissor, gpu::Scissor, 16, 4);
NG_ASSERT_LAYOUT(ng_StencilFaceState, gpu::StencilFaceState, 5, 1);
NG_ASSERT_LAYOUT(ng_DepthStencilState, gpu::DepthStencilState, 16, 1);
NG_ASSERT_LAYOUT(ng_ByteSpan, gpu::ByteSpan, 16, 8);
NG_ASSERT_LAYOUT(ng_ShaderStage, gpu::ShaderStage, 40, 8);
NG_ASSERT_LAYOUT(ng_ColorTargetDescSpan, gpu::Span<const gpu::ColorTargetDesc>, 16, 8);
NG_ASSERT_LAYOUT(ng_GraphicsPSODesc, gpu::GraphicsPSODesc, 120, 8);
NG_ASSERT_LAYOUT(ng_MeshPSODesc, gpu::MeshPSODesc, 160, 8);
NG_ASSERT_LAYOUT(ng_ClearColor, gpu::ClearColor, 16, 4);
NG_ASSERT_LAYOUT(ng_ColorAttachment, gpu::ColorAttachment, 32, 8);
NG_ASSERT_LAYOUT(ng_DepthAttachment, gpu::DepthAttachment, 16, 8);
NG_ASSERT_LAYOUT(ng_StencilAttachment, gpu::StencilAttachment, 16, 8);
NG_ASSERT_LAYOUT(ng_ColorAttachmentSpan, gpu::Span<const gpu::ColorAttachment>, 16, 8);
NG_ASSERT_LAYOUT(ng_RenderingDesc, gpu::RenderingDesc, 48, 8);
static_assert(offsetof(ng_CommandBufferSpan, size) == 8);
static_assert(offsetof(ng_TimelinePointSpan, size) == 8);
static_assert(offsetof(ng_DeviceCaps, max_push_data_size) == 40);
static_assert(offsetof(ng_DeviceCaps, texture_compression_bc) == 64);
static_assert(offsetof(ng_DeviceDesc, swapchain_format) == 8);
static_assert(offsetof(ng_DeviceDesc, desired_swapchain_image_count) == 12);
static_assert(offsetof(ng_TextureDesc, extent) == 4);
static_assert(offsetof(ng_TextureDesc, mip_levels) == 16);
static_assert(offsetof(ng_TextureDesc, format) == 24);
static_assert(offsetof(ng_TextureDesc, mutable_format) == 25);
static_assert(offsetof(ng_TextureDesc, usage) == 28);
static_assert(offsetof(ng_TextureCopyDesc, offset) == 12);
static_assert(offsetof(ng_TextureCopyDesc, extent) == 24);
static_assert(offsetof(ng_TextureCopyDesc, row_pitch_bytes) == 40);
static_assert(offsetof(ng_TextureCopyDesc, slice_pitch_bytes) == 48);
static_assert(offsetof(ng_ShaderStage, entry_point) == 16);
static_assert(offsetof(ng_ShaderStage, threadgroup_size) == 24);
static_assert(offsetof(ng_GraphicsPSODesc, rasterization) == 100);
static_assert(offsetof(ng_MeshPSODesc, rasterization) == 140);
static_assert(offsetof(ng_ColorAttachment, clear) == 12);
static_assert(static_cast<unsigned>(NG_ERROR_NONE) == static_cast<unsigned>(gpu::Error::none));
static_assert(static_cast<unsigned>(NG_ERROR_UNSUPPORTED) == static_cast<unsigned>(gpu::Error::unsupported));
static_assert(static_cast<unsigned>(NG_ERROR_DEVICE_LOST) == static_cast<unsigned>(gpu::Error::device_lost));
static_assert(static_cast<unsigned>(NG_ERROR_DRIVER_ERROR) == static_cast<unsigned>(gpu::Error::driver_error));
static_assert(static_cast<unsigned>(NG_FORMAT_UNDEFINED) == static_cast<unsigned>(gpu::Format::undefined));
static_assert(static_cast<unsigned>(NG_TEXTURE_USAGE_TRANSFER_DESTINATION) ==
static_cast<unsigned>(gpu::TextureUsage::transfer_destination));
static_assert(static_cast<unsigned>(NG_STAGE_ALL_COMMANDS) == static_cast<unsigned>(gpu::Stage::all_commands));
static_assert(static_cast<unsigned>(NG_ACCESS_DESCRIPTOR_READ) == static_cast<unsigned>(gpu::Access::descriptor_read));
static_assert(static_cast<unsigned>(NG_RENDERING_FLAGS_SUSPENDING) ==
static_cast<unsigned>(gpu::RenderingFlags::suspending));
static_assert(static_cast<unsigned>(NG_RENDERING_FLAGS_RESUMING) ==
static_cast<unsigned>(gpu::RenderingFlags::resuming));
#undef NG_ASSERT_LAYOUT
} // namespace
extern "C"
{
void ng_get_texture_format_info(ng_Format format, ng_TextureFormatInfo* out_info) noexcept
{
store_value(out_info, gpu::get_texture_format_info(static_cast<gpu::Format>(format)));
}
void ng_create_device(const ng_DeviceDesc* desc, ng_DeviceInit* out_init) noexcept
{
store_value(out_init, gpu::create_device(copy_value<gpu::DeviceDesc>(*desc)));
}
void ng_destroy_device(ng_Device* device) noexcept
{
gpu::destroy_device(device);
}
void ng_get_device_caps(const ng_Device* device, ng_DeviceCaps* out_caps) noexcept
{
store_value(out_caps, gpu::get_device_caps(device));
}
uint8_t ng_supports_texture_format(const ng_Device* device, ng_Format format, ng_TextureUsage usage) noexcept
{
return static_cast<uint8_t>(gpu::supports_texture_format(device, static_cast<gpu::Format>(format), static_cast<gpu::TextureUsage>(usage)));
}
void ng_get_drawable_extent(ng_Device* device, ng_uint32x2* out_extent) noexcept
{
store_value(out_extent, gpu::get_drawable_extent(device));
}
ng_TimelineSemaphore* ng_create_timeline_semaphore(ng_Device* device, uint64_t initial_value) noexcept
{
return gpu::create_timeline_semaphore(device, initial_value);
}
void ng_destroy_timeline_semaphore(ng_TimelineSemaphore* semaphore) noexcept
{
gpu::destroy_timeline_semaphore(semaphore);
}
uint64_t ng_timeline_completed_value(const ng_TimelineSemaphore* semaphore) noexcept
{
return gpu::timeline_completed_value(semaphore);
}
void ng_wait_timeline(const ng_TimelinePoint* point) noexcept
{
gpu::wait_timeline(copy_value<gpu::TimelinePoint>(*point));
}
void ng_wait_idle(ng_Device* device) noexcept
{
gpu::wait_idle(device);
}
void ng_acquire(ng_CommandBuffer* commands, ng_SwapchainFrame* out_frame) noexcept
{
store_value(out_frame, gpu::acquire(commands));
}
void ng_submit_and_present(ng_Device* device, const ng_SubmitDesc* desc) noexcept
{
gpu::submit_and_present(device, copy_value<gpu::SubmitDesc>(*desc));
}
void ng_create_gpu_heap(ng_Device* device, uint64_t byte_count, ng_MemoryType memory, ng_GpuHeap* out_heap) noexcept
{
store_value(out_heap, gpu::create_gpu_heap(device, byte_count, static_cast<gpu::MemoryType>(memory)));
}
void ng_destroy_gpu_heap(const ng_GpuHeap* heap) noexcept
{
gpu::destroy_gpu_heap(copy_value<gpu::GpuHeap>(*heap));
}
void ng_gpu_heap_range(const ng_GpuHeap* heap, ng_GpuRange* out_range) noexcept
{
store_value(out_range, gpu::gpu_range(copy_value<gpu::GpuHeap>(*heap)));
}
void ng_create_texture_heap(ng_Device* device, uint64_t byte_count, ng_TextureHeap* out_heap) noexcept
{
store_value(out_heap, gpu::create_texture_heap(device, byte_count));
}
void ng_destroy_texture_heap(const ng_TextureHeap* heap) noexcept
{
gpu::destroy_texture_heap(copy_value<gpu::TextureHeap>(*heap));
}
void ng_get_texture_size_align(ng_Device* device, const ng_TextureDesc* desc, ng_SizeAlign* out_size_align) noexcept
{
store_value(out_size_align, gpu::get_texture_size_align(device, copy_value<gpu::TextureDesc>(*desc)));
}
ng_Texture* ng_create_texture(ng_CommandBuffer* commands, const ng_TextureDesc* desc, const ng_TextureHeap* heap, uint64_t offset) noexcept
{
return gpu::create_texture(commands, copy_value<gpu::TextureDesc>(*desc), copy_value<gpu::TextureHeap>(*heap), offset);
}
void ng_destroy_texture(ng_Texture* texture) noexcept
{
gpu::destroy_texture(texture);
}
ng_RenderView* ng_create_render_view(ng_Texture* texture, const ng_RenderViewDesc* desc) noexcept
{
return gpu::create_render_view(texture, copy_value<gpu::RenderViewDesc>(*desc));
}
void ng_destroy_render_view(ng_RenderView* render_view) noexcept
{
gpu::destroy_render_view(render_view);
}
ng_TextureDescriptorHeap* ng_create_texture_descriptor_heap(ng_Device* device, uint32_t capacity) noexcept
{
return gpu::create_texture_descriptor_heap(device, capacity);
}
void ng_destroy_texture_descriptor_heap(ng_TextureDescriptorHeap* heap) noexcept
{
gpu::destroy_texture_descriptor_heap(heap);
}
void ng_write_texture_descriptor(ng_TextureDescriptorHeap* heap, uint32_t index, const ng_Texture* texture,
ng_TextureDescriptorType type, const ng_TextureDescriptorDesc* desc) noexcept
{
gpu::write_texture_descriptor(heap, index, texture, static_cast<gpu::TextureDescriptorType>(type),
copy_value<gpu::TextureDescriptorDesc>(*desc));
}
void ng_copy_texture_descriptors(const ng_TextureDescriptorHeap* source, uint32_t source_index,
ng_TextureDescriptorHeap* destination, uint32_t destination_index, uint32_t count) noexcept
{
gpu::copy_texture_descriptors(source, source_index, destination, destination_index, count);
}
ng_SamplerDescriptorHeap* ng_create_sampler_descriptor_heap(ng_Device* device, uint32_t capacity) noexcept
{
return gpu::create_sampler_descriptor_heap(device, capacity);
}
void ng_destroy_sampler_descriptor_heap(ng_SamplerDescriptorHeap* heap) noexcept
{
gpu::destroy_sampler_descriptor_heap(heap);
}
void ng_write_sampler_descriptor(ng_SamplerDescriptorHeap* heap, uint32_t index, const ng_SamplerDesc* desc) noexcept
{
gpu::write_sampler_descriptor(heap, index, copy_value<gpu::SamplerDesc>(*desc));
}
void ng_copy_sampler_descriptors(const ng_SamplerDescriptorHeap* source, uint32_t source_index,
ng_SamplerDescriptorHeap* destination, uint32_t destination_index, uint32_t count) noexcept
{
gpu::copy_sampler_descriptors(source, source_index, destination, destination_index, count);
}
ng_PSO* ng_create_graphics_pso(ng_Device* device, const ng_GraphicsPSODesc* desc) noexcept
{
return gpu::create_graphics_pso(device, copy_value<gpu::GraphicsPSODesc>(*desc));
}
ng_PSO* ng_create_mesh_pso(ng_Device* device, const ng_MeshPSODesc* desc) noexcept
{
return gpu::create_mesh_pso(device, copy_value<gpu::MeshPSODesc>(*desc));
}
ng_PSO* ng_create_compute_pso(ng_Device* device, const ng_ShaderStage* compute) noexcept
{
return gpu::create_compute_pso(device, copy_value<gpu::ShaderStage>(*compute));
}
void ng_destroy_pso(ng_PSO* pso) noexcept
{
gpu::destroy_pso(pso);
}
ng_CommandPool* ng_create_command_pool(ng_Device* device, uint32_t queue_index) noexcept
{
return gpu::create_command_pool(device, queue_index);
}
void ng_destroy_command_pool(ng_CommandPool* pool) noexcept
{
gpu::destroy_command_pool(pool);
}
void ng_reset_command_pool(ng_CommandPool* pool) noexcept
{
gpu::reset_command_pool(pool);
}
ng_CommandBuffer* ng_begin_commands(ng_CommandPool* pool) noexcept
{
return gpu::begin_commands(pool);
}
void ng_end_commands(ng_CommandBuffer* commands) noexcept
{
gpu::end_commands(commands);
}
void ng_submit(ng_Device* device, const ng_SubmitDesc* desc, uint32_t queue_index) noexcept
{
gpu::submit(device, copy_value<gpu::SubmitDesc>(*desc), queue_index);
}
void ng_set_texture_descriptor_heap(ng_CommandBuffer* commands, ng_TextureDescriptorHeap* heap) noexcept
{
gpu::set_texture_descriptor_heap(commands, heap);
}
void ng_set_sampler_descriptor_heap(ng_CommandBuffer* commands, ng_SamplerDescriptorHeap* heap) noexcept
{
gpu::set_sampler_descriptor_heap(commands, heap);
}
void ng_copy_memory(ng_CommandBuffer* commands, const ng_GpuRange* source, const ng_GpuRange* destination) noexcept
{
gpu::copy_memory(commands, copy_value<gpu::GpuRange>(*source), copy_value<gpu::GpuRange>(*destination));
}
void ng_copy_memory_to_texture(ng_CommandBuffer* commands, const ng_GpuRange* source, ng_Texture* destination,
const ng_TextureCopyDesc* copy) noexcept
{
gpu::copy_memory_to_texture(commands, copy_value<gpu::GpuRange>(*source), destination, copy_value<gpu::TextureCopyDesc>(*copy));
}
void ng_copy_texture_to_memory(ng_CommandBuffer* commands, ng_Texture* source, const ng_GpuRange* destination,
const ng_TextureCopyDesc* copy) noexcept
{
gpu::copy_texture_to_memory(commands, source, copy_value<gpu::GpuRange>(*destination), copy_value<gpu::TextureCopyDesc>(*copy));
}
void ng_barrier(ng_CommandBuffer* commands, ng_Stage before, ng_Access before_access, ng_Stage after, ng_Access after_access) noexcept
{
gpu::barrier(commands, static_cast<gpu::Stage>(before), static_cast<gpu::Access>(before_access),
static_cast<gpu::Stage>(after), static_cast<gpu::Access>(after_access));
}
void ng_write_timestamp(ng_CommandBuffer* commands, uint64_t* cpu_destination, ng_Stage stage) noexcept
{
gpu::write_timestamp(commands, cpu_destination, static_cast<gpu::Stage>(stage));
}
void ng_read_timestamps(ng_CommandPool* pool) noexcept
{
gpu::read_timestamps(pool);
}
void ng_begin_render_pass(ng_CommandBuffer* commands, const ng_RenderingDesc* desc, ng_RenderingFlags flags) noexcept
{
gpu::begin_render_pass(commands, copy_value<gpu::RenderingDesc>(*desc), static_cast<gpu::RenderingFlags>(flags));
}
void ng_end_render_pass(ng_CommandBuffer* commands) noexcept
{
gpu::end_render_pass(commands);
}
void ng_set_viewport(ng_CommandBuffer* commands, const ng_Viewport* viewport) noexcept
{
gpu::set_viewport(commands, copy_value<gpu::Viewport>(*viewport));
}
void ng_set_scissor(ng_CommandBuffer* commands, const ng_Scissor* scissor) noexcept
{
gpu::set_scissor(commands, copy_value<gpu::Scissor>(*scissor));
}
void ng_set_depth_stencil(ng_CommandBuffer* commands, const ng_DepthStencilState* state) noexcept
{
gpu::set_depth_stencil(commands, copy_value<gpu::DepthStencilState>(*state));
}
void ng_bind_pso(ng_CommandBuffer* commands, const ng_PSO* pso) noexcept
{
gpu::bind_pso(commands, pso);
}
void ng_draw(ng_CommandBuffer* commands, const void* root, uint32_t vertex_count, uint32_t instance_count,
uint32_t first_vertex, uint32_t first_instance) noexcept
{
gpu::draw(commands, root, vertex_count, instance_count, first_vertex, first_instance);
}
void ng_draw_indexed(ng_CommandBuffer* commands, const void* root, const ng_GpuRange* indices, ng_IndexType type,
uint32_t index_count, uint32_t instance_count, uint32_t first_index, int32_t vertex_offset,
uint32_t first_instance) noexcept
{
gpu::draw_indexed(commands, root, copy_value<gpu::GpuRange>(*indices), static_cast<gpu::IndexType>(type), index_count,
instance_count, first_index, vertex_offset, first_instance);
}
void ng_draw_indirect(ng_CommandBuffer* commands, const void* root, const ng_GpuRange* arguments,
uint32_t draw_count, uint32_t stride) noexcept
{
gpu::draw_indirect(commands, root, copy_value<gpu::GpuRange>(*arguments), draw_count, stride);
}
void ng_draw_indexed_indirect(ng_CommandBuffer* commands, const void* root, const ng_GpuRange* indices,
ng_IndexType type, const ng_GpuRange* arguments, uint32_t draw_count, uint32_t stride) noexcept
{
gpu::draw_indexed_indirect(commands, root, copy_value<gpu::GpuRange>(*indices), static_cast<gpu::IndexType>(type),
copy_value<gpu::GpuRange>(*arguments), draw_count, stride);
}
void ng_dispatch(ng_CommandBuffer* commands, const void* root, const ng_uint32x3* group_count) noexcept
{
gpu::dispatch(commands, root, copy_value<gpu::uint32x3>(*group_count));
}
void ng_dispatch_indirect(ng_CommandBuffer* commands, const void* root, const ng_GpuRange* arguments) noexcept
{
gpu::dispatch_indirect(commands, root, copy_value<gpu::GpuRange>(*arguments));
}
void ng_draw_meshlets(ng_CommandBuffer* commands, const void* root, const ng_uint32x3* group_count) noexcept
{
gpu::draw_meshlets(commands, root, copy_value<gpu::uint32x3>(*group_count));
}
void ng_draw_meshlets_indirect(ng_CommandBuffer* commands, const void* root, const ng_GpuRange* arguments,
uint32_t draw_count, uint32_t stride) noexcept
{
gpu::draw_meshlets_indirect(commands, root, copy_value<gpu::GpuRange>(*arguments), draw_count, stride);
}
} // extern "C"