Skip to content

Commit

Permalink
Expose subresource range + tests for texture (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccummingsNV authored Oct 22, 2024
1 parent 3e8f73c commit 2515418
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 15 deletions.
21 changes: 20 additions & 1 deletion src/sgl/device/python/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,28 @@ SGL_PY_EXPORT(device_resource)

nb::sgl_enum<ResourceViewType>(m, "ResourceViewType");

nb::sgl_enum<TextureAspect>(m, "TextureAspect");

nb::class_<BufferRange>(m, "BufferRange", D(SubresourceRange))
.def(nb::init<>())
.def_ro("offset", &BufferRange::offset, D_NA(BufferRange, offset))
.def_ro("size", &BufferRange::size, D_NA(BufferRange, size))
.def("__repr__", &BufferRange::to_string, D_NA(BufferRange, to_string));

nb::class_<SubresourceRange>(m, "SubresourceRange", D(SubresourceRange))
.def(nb::init<>())
.def_ro("texture_aspect", &SubresourceRange::texture_aspect, D_NA(SubresourceRange, texture_aspect))
.def_ro("mip_level", &SubresourceRange::mip_level, D_NA(SubresourceRange, mip_level))
.def_ro("mip_count", &SubresourceRange::mip_count, D_NA(SubresourceRange, mip_count))
.def_ro("base_array_layer", &SubresourceRange::base_array_layer, D_NA(SubresourceRange, base_array_layer))
.def_ro("layer_count", &SubresourceRange::layer_count, D_NA(SubresourceRange, layer_count))
.def("__repr__", &SubresourceRange::to_string, D_NA(SubresourceRange, to_string));

nb::class_<ResourceView, Object>(m, "ResourceView", D(ResourceView))
.def_prop_ro("type", &ResourceView::type, D(ResourceView, type))
.def_prop_ro("resource", &ResourceView::resource, D(ResourceView, resource));
.def_prop_ro("resource", &ResourceView::resource, D(ResourceView, resource))
.def_prop_ro("buffer_range", &ResourceView::buffer_range, D_NA(ResourceView, buffer_range))
.def_prop_ro("subresource_range", &ResourceView::subresource_range, D_NA(ResourceView, subresource_range));

nb::class_<BufferDesc>(m, "BufferDesc", D(BufferDesc))
.def(nb::init<>())
Expand Down
9 changes: 4 additions & 5 deletions src/sgl/device/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,14 @@ std::string ResourceView::to_string() const
"ResourceView(\n"
" type = {},\n"
" format = {},\n"
" buffer_range = (offset={}, size={}),\n"
// " subresource_range = {},\n"
" buffer_range = {},\n"
" subresource_range = {},\n"
" resource = {}\n"
")",
m_desc.type,
m_desc.format,
m_desc.buffer_range.offset,
m_desc.buffer_range.size,
// m_desc.subresource_range,
m_desc.buffer_range.to_string(),
m_desc.subresource_range.to_string(),
string::indent(m_resource ? m_resource->to_string() : "null")
);
}
Expand Down
30 changes: 30 additions & 0 deletions src/sgl/device/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ struct BufferRange {
uint64_t size{ALL};

auto operator<=>(const BufferRange&) const = default;

std::string to_string() const { return fmt::format("BufferRange(offset={}, size={}", offset, size); }
};

enum class TextureAspect : uint32_t {
Expand All @@ -205,6 +207,22 @@ enum class TextureAspect : uint32_t {
plane2 = static_cast<uint32_t>(gfx::TextureAspect::Plane2),
depth_stencil = depth | stencil,
};
SGL_ENUM_INFO(
TextureAspect,
{
{TextureAspect::default_, "default_"},
{TextureAspect::color, "color"},
{TextureAspect::depth, "depth"},
{TextureAspect::stencil, "stencil"},
{TextureAspect::meta_data, "meta_data"},
{TextureAspect::plane0, "plane0"},
{TextureAspect::plane1, "plane1"},
{TextureAspect::plane2, "plane2"},
{TextureAspect::depth_stencil, "depth_stencil"},

}
);
SGL_ENUM_REGISTER(TextureAspect);

struct SubresourceRange {
static constexpr uint32_t ALL = std::numeric_limits<uint32_t>::max();
Expand All @@ -220,6 +238,18 @@ struct SubresourceRange {
uint32_t layer_count{ALL}; // For cube maps, this is a multiple of 6.

auto operator<=>(const SubresourceRange&) const = default;

std::string to_string() const
{
return fmt::format(
"SubresourceRange(texture_aspect={}, mip_level={}, mip_count={}, base_array_layer={}, layer_count={}",
texture_aspect,
mip_level,
mip_count,
base_array_layer,
layer_count
);
}
};

struct ResourceViewDesc {
Expand Down
38 changes: 29 additions & 9 deletions src/sgl/device/tests/test_texture_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,22 @@ def test_shader_read_write_texture(
device.link_program([module], [module.entry_point("copy_color")])
)

srv = src_tex.get_srv(mip)
assert srv.subresource_range.base_array_layer == 0
assert srv.subresource_range.layer_count == 1
assert srv.subresource_range.mip_level == mip
assert srv.subresource_range.mip_count == src_tex.mip_count - mip

uav = dest_tex.get_uav(mip)
assert uav.subresource_range.base_array_layer == 0
assert uav.subresource_range.layer_count == 1
assert uav.subresource_range.mip_level == mip
assert uav.subresource_range.mip_count == dest_tex.mip_count - mip

copy_kernel.dispatch(
[src_tex.width, src_tex.height, src_tex.depth],
src=src_tex.get_srv(mip),
dest=dest_tex.get_uav(mip),
src=srv,
dest=uav,
)
else:

Expand All @@ -171,11 +183,10 @@ def test_shader_read_write_texture(
void copy_color(
uint{dims} tid: SV_DispatchThreadID,
Texture{dims}DArray<float4> src,
RWTexture{dims}DArray<float4> dest,
uniform uint slice
RWTexture{dims}DArray<float4> dest
)
{{
uint{dims+1} idx = uint{dims+1}(tid, slice);
uint{dims+1} idx = uint{dims+1}(tid, 0);
dest[idx] = src[idx];
}}
"""
Expand All @@ -187,11 +198,20 @@ def test_shader_read_write_texture(
device.link_program([module], [module.entry_point("copy_color")])
)
for i in range(0, slices):
srv = src_tex.get_srv(mip_level=mip, base_array_layer=i, layer_count=1)
assert srv.subresource_range.base_array_layer == i
assert srv.subresource_range.layer_count == 1
assert srv.subresource_range.mip_level == mip
assert srv.subresource_range.mip_count == src_tex.mip_count - mip

uav = dest_tex.get_uav(mip_level=mip, base_array_layer=i, layer_count=1)
assert uav.subresource_range.base_array_layer == i
assert uav.subresource_range.layer_count == 1
assert uav.subresource_range.mip_level == mip
assert uav.subresource_range.mip_count == dest_tex.mip_count - mip

copy_kernel.dispatch(
[src_tex.width, src_tex.height, src_tex.depth],
src=src_tex.get_srv(mip),
dest=dest_tex.get_uav(mip),
slice=i,
[src_tex.width, src_tex.height, src_tex.depth], src=srv, dest=uav
)

# Read back data and compare
Expand Down

0 comments on commit 2515418

Please sign in to comment.