Skip to content

Commit f78631b

Browse files
committed
Some vk ext
1 parent e7b4d9d commit f78631b

File tree

10 files changed

+202
-17
lines changed

10 files changed

+202
-17
lines changed

include/luisa/backends/ext/native_resource_ext_interface.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ class Swapchain;
1919

2020
class NativeResourceExt : public DeviceExtension {
2121

22-
private:
23-
DeviceInterface *_device;
24-
2522
protected:
23+
DeviceInterface *_device;
2624
~NativeResourceExt() noexcept = default;
2725

2826
public:

src/backends/dx/DXApi/ext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ BufferCreationInfo DxNativeResourceExt::register_external_buffer(
8888
auto res = static_cast<Buffer *>(new ExternalBuffer(
8989
dx_device,
9090
reinterpret_cast<ID3D12Resource *>(external_ptr),
91-
*reinterpret_cast<D3D12_RESOURCE_STATES const *>(custom_data)));
91+
custom_data ? *reinterpret_cast<D3D12_RESOURCE_STATES const *>(custom_data) : D3D12_RESOURCE_STATE_COMMON));
9292
BufferCreationInfo info;
9393
info.handle = resource_to_handle(res);
9494
info.native_handle = res->GetResource();
@@ -104,22 +104,22 @@ ResourceCreationInfo DxNativeResourceExt::register_external_image(
104104
void *custom_data) noexcept {
105105
auto desc = reinterpret_cast<NativeTextureDesc const *>(custom_data);
106106
GFXFormat gfxFormat;
107-
if (desc->custom_format == DXGI_FORMAT_UNKNOWN) {
107+
if (!desc || desc->custom_format == DXGI_FORMAT_UNKNOWN) {
108108
gfxFormat = TextureBase::ToGFXFormat(format);
109109
} else {
110110
gfxFormat = static_cast<GFXFormat>(desc->custom_format);
111111
}
112112
auto res = static_cast<TextureBase *>(new ExternalTexture(
113113
dx_device,
114114
reinterpret_cast<ID3D12Resource *>(external_ptr),
115-
desc->initState,
115+
desc ? desc->initState : D3D12_RESOURCE_STATE_COMMON,
116116
width,
117117
height,
118118
gfxFormat,
119119
(TextureDimension)dimension,
120120
depth,
121121
mipmap_levels,
122-
desc->allowUav));
122+
desc ? desc->allowUav : true));
123123
return {
124124
reinterpret_cast<uint64_t>(res),
125125
external_ptr};

src/backends/vk/buffer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ class Buffer : public Resource {
2424
uint64_t get_device_address() const;
2525
virtual bool flush_host() const { return false; }
2626
};
27+
class ExternalBuffer : public Buffer {
28+
VkBuffer _buffer{};
29+
30+
public:
31+
ExternalBuffer(Device *device, VkBuffer vk_buffer, size_t size_bytes)
32+
: Buffer{device, size_bytes},
33+
_buffer(vk_buffer) {
34+
}
35+
ExternalBuffer(ExternalBuffer &&rhs) = default;
36+
~ExternalBuffer() = default;
37+
VkBuffer vk_buffer() const override { return _buffer; }
38+
};
2739
class BufferView {
2840
public:
2941
Buffer const *buffer;

src/backends/vk/device.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "sparse_buffer.h"
2323
#include "pinned_memory_ext.h"
2424
#include "vk_raster_ext.h"
25+
#include "vk_native_res_ext.h"
2526
#include <luisa/backends/ext/raster_ext_interface.h>
2627
namespace lc::vk {
2728
static constexpr uint k_shader_model = 65u;
@@ -363,6 +364,18 @@ Device::Device(Context &&ctx_arg, DeviceConfig const *configs)
363364
[](DeviceExtension *ext) {
364365
delete static_cast<VkRasterExt *>(ext);
365366
});
367+
exts.try_emplace(
368+
#ifdef LUISA_USE_SYSTEM_STL
369+
luisa::string{NativeResourceExt::name},
370+
#else
371+
NativeResourceExt::name,
372+
#endif
373+
[](Device *device) -> DeviceExtension * {
374+
return new VkNativeResourceExt(device);
375+
},
376+
[](DeviceExtension *ext) {
377+
delete static_cast<VkNativeResourceExt *>(ext);
378+
});
366379
// auto exts = detail::supported_exts(physical_device());
367380
// for(auto&& i : exts){
368381
// LUISA_INFO("{}", i.extensionName);
@@ -728,7 +741,7 @@ BufferCreationInfo Device::create_buffer(const Type *element, size_t elem_count,
728741
}
729742
BufferCreationInfo Device::create_buffer(const ir::CArc<ir::Type> *element, size_t elem_count, void *external_ptr) noexcept { return BufferCreationInfo::make_invalid(); }
730743
void Device::destroy_buffer(uint64_t handle) noexcept {
731-
delete reinterpret_cast<DefaultBuffer *>(handle);
744+
delete reinterpret_cast<Buffer *>(handle);
732745
}
733746

734747
// texture

src/backends/vk/texture.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ using namespace luisa::compute;
66
Texture::Texture(Device *device)
77
: Resource(device) {
88
}
9+
Texture::Texture(
10+
Device *device,
11+
VkImage external_image,
12+
uint dimension,
13+
VkFormat format,
14+
uint3 size,
15+
uint mip,
16+
bool simultaneous_access)
17+
: Resource(device),
18+
_img(external_image),
19+
_format(
20+
static_cast<compute::PixelFormat>(static_cast<uint>(format) | (1u << 31u))),
21+
_size(size),
22+
_mip(mip),
23+
_dimension(dimension),
24+
_contained{false},
25+
_simultaneous_access(simultaneous_access) {}
26+
927
Texture::Texture(
1028
Device *device,
1129
uint dimension,
@@ -43,7 +61,7 @@ Texture::Texture(
4361
Texture::~Texture() {
4462
if (_img.allocation)
4563
device()->allocator().destroy_image(_img);
46-
else
64+
else if (_contained)
4765
vkDestroyImage(device()->logic_device(), _img.image, Device::alloc_callbacks());
4866
}
4967

@@ -122,8 +140,12 @@ uint3 Texture::tex3d_tile_size(luisa::compute::PixelStorage storage) {
122140
}
123141

124142
VkFormat Texture::to_vk_format(PixelFormat format) {
143+
// native format
144+
if ((luisa::to_underlying(format) & (1u << 31u)) != 0) {
145+
return static_cast<VkFormat>(luisa::to_underlying(format) & ((1u << 31u) - 1u));
146+
}
125147
// depth
126-
if (luisa::to_underlying(format) > 65535u) {
148+
else if (luisa::to_underlying(format) > 65535u) {
127149
auto depth_format = static_cast<compute::DepthFormat>(luisa::to_underlying(format) & 65535u);
128150
switch (depth_format) {
129151
case compute::DepthFormat::D16:

src/backends/vk/texture.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ class Texture : public Resource {
1111
uint3 _size;
1212
uint _mip;
1313
uint _dimension;
14-
bool _simultaneous_access;
14+
bool _contained : 1 {true};
15+
bool _simultaneous_access : 1;
1516
mutable luisa::spin_mutex _layout_mtx;
1617
mutable vstd::vector<VkImageLayout> _layouts;
1718
public:
1819
auto simultaneous_access() const { return _simultaneous_access; }
1920
auto dimension() const { return _dimension; }
2021
Texture(Device *device);
22+
// external
23+
Texture(
24+
Device *device,
25+
VkImage external_image,
26+
uint dimension,
27+
VkFormat format,
28+
uint3 size,
29+
uint mip,
30+
bool simultaneous_access);
2131
Texture(
2232
Device *device,
2333
uint dimension,
@@ -36,7 +46,7 @@ class Texture : public Resource {
3646
static uint2 tex2d_tile_size(luisa::compute::PixelStorage storage);
3747
static uint3 tex3d_tile_size(luisa::compute::PixelStorage storage);
3848
uint3 tile_size() const {
39-
if (luisa::to_underlying(_format) > 65535u) return {}; // depth
49+
if (luisa::to_underlying(_format) > 65535u) return {};// depth
4050
if (_dimension <= 2) {
4151
return make_uint3(tex2d_tile_size(luisa::compute::pixel_format_to_storage(_format)), 1);
4252
} else {
@@ -47,8 +57,8 @@ class Texture : public Resource {
4757

4858
auto mip() const { return _mip; }
4959
auto vk_image() const { return _img.image; }
50-
auto format() const {
51-
if (luisa::to_underlying(_format) > 65535u) return static_cast<compute::PixelFormat>(-1); // depth
60+
auto format() const {
61+
if (luisa::to_underlying(_format) > 65535u) return static_cast<compute::PixelFormat>(-1);// depth
5262
return _format;
5363
}
5464
auto depth_format() const {

src/backends/vk/vk_native_res_ext.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "vk_native_res_ext.h"
2+
#include "buffer.h"
3+
#include "texture.h"
4+
#include "device.h"
5+
namespace lc::vk {
6+
VkNativeResourceExt::VkNativeResourceExt(Device *device) : NativeResourceExt(device) {}
7+
BufferCreationInfo VkNativeResourceExt::register_external_buffer(
8+
void *buffer_ptr,
9+
const Type *element,
10+
size_t elem_count,
11+
// custom data see backends' header
12+
void *custom_data) noexcept {
13+
size_t elem_size = (element == Type::of<void>()) ? 1 : element->size();
14+
size_t size = elem_size * elem_count;
15+
BufferCreationInfo info;
16+
info.handle = reinterpret_cast<uint64_t>(new ExternalBuffer(
17+
static_cast<Device *>(_device),
18+
static_cast<VkBuffer>(buffer_ptr),
19+
size));
20+
info.native_handle = buffer_ptr;
21+
info.element_stride = elem_size;
22+
info.total_size_bytes = size;
23+
return info;
24+
}
25+
ResourceCreationInfo VkNativeResourceExt::register_external_image(
26+
void *image_ptr,
27+
PixelFormat format, uint dimension,
28+
uint width, uint height, uint depth,
29+
uint mipmap_levels,
30+
// custom data see backends' header
31+
void *custom_data) noexcept {
32+
VkFormat vk_format = custom_data ? *static_cast<VkFormat *>(custom_data) : Texture::to_vk_format(format);
33+
auto tex = new Texture(
34+
static_cast<Device *>(_device),
35+
static_cast<VkImage>(image_ptr),
36+
dimension,
37+
vk_format,
38+
uint3(width, height, depth),
39+
mipmap_levels,
40+
false);
41+
ResourceCreationInfo info{
42+
.handle = reinterpret_cast<uint64_t>(tex),
43+
.native_handle = image_ptr};
44+
return info;
45+
}
46+
47+
ResourceCreationInfo VkNativeResourceExt::register_external_depth_buffer(
48+
void *depth_buffer_ptr,
49+
DepthFormat format,
50+
uint width,
51+
uint height,
52+
// custom data see backends' header
53+
void *custom_data) noexcept {
54+
// TODO
55+
return ResourceCreationInfo::make_invalid();
56+
}
57+
58+
SwapchainCreationInfo VkNativeResourceExt::register_external_swapchain(
59+
void *swapchain_ptr,
60+
bool vsync) noexcept {
61+
// TODO
62+
SwapchainCreationInfo s;
63+
s.invalidate();
64+
return s;
65+
}
66+
67+
uint64_t VkNativeResourceExt::get_native_resource_device_address(
68+
void *native_handle) noexcept {
69+
VkBufferDeviceAddressInfoKHR buffer_device_address_info{};
70+
buffer_device_address_info.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;
71+
buffer_device_address_info.buffer = static_cast<VkBuffer>(native_handle);
72+
return vkGetBufferDeviceAddress(static_cast<Device *>(_device)->logic_device(), &buffer_device_address_info);
73+
}
74+
75+
VkNativeResourceExt::~VkNativeResourceExt() {}
76+
}// namespace lc::vk

src/backends/vk/vk_native_res_ext.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
#include <luisa/backends/ext/native_resource_ext_interface.h>
3+
namespace lc::vk {
4+
class Device;
5+
using namespace luisa::compute;
6+
class VkNativeResourceExt : public NativeResourceExt {
7+
public:
8+
VkNativeResourceExt(Device *device);
9+
BufferCreationInfo register_external_buffer(
10+
void *buffer_ptr,
11+
const Type *element,
12+
size_t elem_count,
13+
// custom data see backends' header
14+
void *custom_data) noexcept override;
15+
16+
ResourceCreationInfo register_external_image(
17+
void *image_ptr,
18+
PixelFormat format, uint dimension,
19+
uint width, uint height, uint depth,
20+
uint mipmap_levels,
21+
// custom data see backends' header
22+
void *custom_data) noexcept override;
23+
24+
ResourceCreationInfo register_external_depth_buffer(
25+
void *depth_buffer_ptr,
26+
DepthFormat format,
27+
uint width,
28+
uint height,
29+
// custom data see backends' header
30+
void *custom_data) noexcept override;
31+
32+
SwapchainCreationInfo register_external_swapchain(
33+
void *swapchain_ptr,
34+
bool vsync) noexcept override;
35+
36+
uint64_t get_native_resource_device_address(
37+
void *native_handle) noexcept override;
38+
39+
~VkNativeResourceExt();
40+
};
41+
}// namespace lc::vk

src/backends/vk/vk_raster_ext.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../common/hlsl/shader_compiler.h"
44
#include "../common/hlsl/hlsl_codegen.h"
55
#include "shader_serializer.h"
6+
#include "compute_shader.h"
67
namespace lc::vk {
78
static constexpr uint k_shader_model = 65u;
89
ResourceCreationInfo VkRasterExt::create_raster_shader(
@@ -69,15 +70,27 @@ ResourceCreationInfo VkRasterExt::create_raster_shader(
6970
ResourceCreationInfo VkRasterExt::load_raster_shader(
7071
luisa::span<Type const *const> types,
7172
luisa::string_view ser_path) noexcept {
72-
return ResourceCreationInfo::make_invalid();
73+
auto deser_result = ShaderSerializer::try_deser_raster(_device, {}, {}, ser_path, SerdeType::ByteCode, _device->binary_io());
74+
if (!deser_result.shader)
75+
return ResourceCreationInfo::make_invalid();
76+
ResourceCreationInfo info{};
77+
info.handle = reinterpret_cast<uint64_t>(deser_result.shader);
78+
if (!ComputeShader::verify_type_md5(types, deser_result.type_md5)) {
79+
LUISA_WARNING("Shader {} arguments not match.", name);
80+
info.invalidate();
81+
return info;
82+
}
83+
return info;
7384
}
7485

7586
VkRasterExt::VkRasterExt(Device *device) {
7687
_device = device;
7788
}
7889
VkRasterExt::~VkRasterExt() {}
7990

80-
void VkRasterExt::destroy_raster_shader(uint64_t handle) noexcept {}
91+
void VkRasterExt::destroy_raster_shader(uint64_t handle) noexcept {
92+
delete reinterpret_cast<RasterShader*>(handle);
93+
}
8194

8295
// depth buffer
8396
ResourceCreationInfo VkRasterExt::create_depth_buffer(DepthFormat format, uint width, uint height) noexcept {

0 commit comments

Comments
 (0)