WebGPU wrapper wip: add texture & texture view methods

This commit is contained in:
Nikita Lisitsa 2023-12-30 20:35:45 +03:00
parent 0555243990
commit 1144d4a46c
8 changed files with 173 additions and 9 deletions

View file

@ -21,7 +21,7 @@ namespace psemek::wgpu
static instance create(descriptor const & desc);
void process_events();
surface create_surface(surface::descriptor const & desc) const;
surface create_surface(surface::descriptor const & desc);
void request_adapter(adapter::request_options const & request_options, adapter::request_callback callback);
static void reference(void * ptr);

View file

@ -81,15 +81,10 @@ namespace psemek::wgpu
};
void configure(configuration const & conf);
capabilities get_capabilities(adapter const & adapter);
current_texture get_current_texture();
wgpu::texture::format get_preferred_format(adapter const & adapter);
void present();
void unconfigure();
struct from_android_native_window

View file

@ -1,8 +1,12 @@
#pragma once
#include <psemek/wgpu/detail/object.hpp>
#include <psemek/wgpu/texture_view.hpp>
#include <psemek/wgpu/chained_struct.hpp>
#include <cstdint>
#include <string>
#include <vector>
namespace psemek::wgpu
{
@ -122,6 +126,32 @@ namespace psemek::wgpu
render_attachment = 0x00000010,
};
enum class dimension : std::uint32_t
{
_1d = 0x00000000,
_2d = 0x00000001,
_3d = 0x00000002,
};
enum class aspect : std::uint32_t
{
all = 0x00000000,
stencil_only = 0x00000001,
depth_only = 0x00000002,
};
// TODO: WGPU_EXPORT WGPUTextureView wgpuTextureCreateView(WGPUTexture texture, WGPU_NULLABLE WGPUTextureViewDescriptor const * descriptor) WGPU_FUNCTION_ATTRIBUTE;
texture_view create_view(texture_view::descriptor const & desc);
void destroy();
std::uint32_t get_width();
std::uint32_t get_height();
std::uint32_t get_depth();
dimension get_dimension();
format get_format();
std::uint32_t get_mip_level_count();
usage get_usage();
void set_label(std::string const & label);
static void reference(void * ptr);
static void release(void * ptr);
@ -134,6 +164,19 @@ namespace psemek::wgpu
friend struct device;
};
struct texture_view::descriptor
{
std::vector<chained_struct> chain = {};
std::string label = {};
texture::format format;
texture_view::dimension dimension;
std::uint32_t base_mip_level = 0;
std::uint32_t mip_level_count = 1;
std::uint32_t base_array_layer = 0;
std::uint32_t array_layer_count = 1;
texture::aspect aspect = texture::aspect::all;
};
inline texture::usage operator | (texture::usage u1, texture::usage u2)
{
return static_cast<texture::usage>(static_cast<unsigned int>(u1) | static_cast<unsigned int>(u2));

View file

@ -0,0 +1,43 @@
#pragma once
#include <psemek/wgpu/detail/object.hpp>
#include <cstdint>
#include <string>
namespace psemek::wgpu
{
struct texture_view
: detail::object<texture_view>
{
using detail::object<texture_view>::object;
enum class dimension : std::uint32_t
{
undefined = 0x00000000,
_1d = 0x00000001,
_2d = 0x00000002,
_2d_array = 0x00000003,
cube = 0x00000004,
cube_array = 0x00000005,
_3d = 0x00000006,
};
// Defined in wgpu/texture.hpp to break circular dependencies
struct descriptor;
void set_label(std::string const & label);
static void reference(void * ptr);
static void release(void * ptr);
private:
explicit texture_view(void * ptr)
: detail::object<texture_view>(ptr)
{}
friend struct texture;
};
}

View file

@ -18,5 +18,5 @@
WGPUSampler
WGPUShaderModule
+ WGPUSurface
WGPUTexture
WGPUTextureView
+ WGPUTexture
+ WGPUTextureView

View file

@ -16,7 +16,7 @@ namespace psemek::wgpu
wgpuInstanceProcessEvents((WGPUInstance)get());
}
surface instance::create_surface(surface::descriptor const & desc) const
surface instance::create_surface(surface::descriptor const & desc)
{
WGPUSurfaceDescriptor descriptor = {};
descriptor.nextInChain = (WGPUChainedStruct const *)detail::fill_chain(desc.chain);

View file

@ -4,6 +4,67 @@
namespace psemek::wgpu
{
texture_view texture::create_view(texture_view::descriptor const & desc)
{
WGPUTextureViewDescriptor descriptor = {};
descriptor.nextInChain = (WGPUChainedStruct const *)detail::fill_chain(desc.chain);
descriptor.label = desc.label.data();
descriptor.format = (WGPUTextureFormat)desc.format;
descriptor.dimension = (WGPUTextureViewDimension)desc.dimension;
descriptor.baseMipLevel = desc.base_mip_level;
descriptor.mipLevelCount = desc.mip_level_count;
descriptor.baseArrayLayer = desc.base_array_layer;
descriptor.arrayLayerCount = desc.array_layer_count;
descriptor.aspect = (WGPUTextureAspect)desc.aspect;
return texture_view(wgpuTextureCreateView((WGPUTexture)get(), &descriptor));
}
void texture::destroy()
{
wgpuTextureDestroy((WGPUTexture)get());
}
std::uint32_t texture::get_width()
{
return wgpuTextureGetWidth((WGPUTexture)get());
}
std::uint32_t texture::get_height()
{
return wgpuTextureGetHeight((WGPUTexture)get());
}
std::uint32_t texture::get_depth()
{
return wgpuTextureGetDepthOrArrayLayers((WGPUTexture)get());
}
texture::dimension texture::get_dimension()
{
return (dimension)wgpuTextureGetDimension((WGPUTexture)get());
}
texture::format texture::get_format()
{
return (format)wgpuTextureGetFormat((WGPUTexture)get());
}
std::uint32_t texture::get_mip_level_count()
{
return wgpuTextureGetMipLevelCount((WGPUTexture)get());
}
texture::usage texture::get_usage()
{
return (usage)wgpuTextureGetUsage((WGPUTexture)get());
}
void texture::set_label(std::string const & label)
{
wgpuTextureSetLabel((WGPUTexture)get(), label.data());
}
void texture::reference(void * ptr)
{
wgpuTextureReference((WGPUTexture)ptr);

View file

@ -0,0 +1,22 @@
#include <psemek/wgpu/texture_view.hpp>
#include <psemek/wgpu/external/webgpu.h>
namespace psemek::wgpu
{
void texture_view::set_label(std::string const & label)
{
wgpuTextureViewSetLabel((WGPUTextureView)get(), label.data());
}
void texture_view::reference(void * ptr)
{
wgpuTextureViewReference((WGPUTextureView)ptr);
}
void texture_view::release(void * ptr)
{
wgpuTextureViewRelease((WGPUTextureView)ptr);
}
}