Skip to content

Commit 247539d

Browse files
wlewNVccummingsNV
andauthored
Fix Texture3D parameters failing with "invalid dimensionality 1" (#754)
* Fixed Texture3D parameters failing * Address none-square textures comment --------- Co-authored-by: ccummingsNV <ccummings@nvidia.com>
1 parent 4b643e1 commit 247539d

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

slangpy/builtin/texture.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ def resolve_types(self, context: BindContext, bound_type: refl.SlangType):
176176
# Otherwise, use default behaviour from marshall
177177
return None
178178

179+
def resolve_dimensionality(
180+
self,
181+
context: BindContext,
182+
binding: "BoundVariable",
183+
vector_target_type: refl.SlangType,
184+
):
185+
# If target type is a texture, the texture is passed directly (broadcast as-is)
186+
if isinstance(vector_target_type, refl.TextureType):
187+
return 0
188+
189+
# the texture is being used for per-pixel operations
190+
return self.texture_dims
191+
179192
# Texture is writable if it has unordered access view.
180193
@property
181194
def is_writable(self):

slangpy/tests/slangpy_tests/test_textures.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,5 +526,89 @@ def test_texture_return_value_str(
526526
texture_return_value_impl(device_type, texel_name, dims, channels, "texture")
527527

528528

529+
@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
530+
def test_texture_1d_broadcast(device_type: DeviceType):
531+
if device_type == DeviceType.cuda:
532+
pytest.skip("1D texture read returns zero on CUDA backend")
533+
module = load_test_module(device_type)
534+
535+
# Non-square: width=8, sample at position 2
536+
tex_data = np.zeros((8, 1), dtype=np.float32)
537+
tex_data[2, 0] = 5.5 # Value at x=2
538+
tex = module.device.create_texture(
539+
type=TextureType.texture_1d,
540+
width=8,
541+
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
542+
format=Format.r32_float,
543+
data=tex_data,
544+
)
545+
546+
result = module.sample_texture_1d_broadcast(tex)
547+
assert result == pytest.approx(5.5)
548+
549+
550+
@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
551+
def test_texture_2d_broadcast(device_type: DeviceType):
552+
module = load_test_module(device_type)
553+
554+
# Non-square: width=8, height=4, sample at position (3, 1)
555+
tex_data = np.zeros((4, 8, 1), dtype=np.float32)
556+
tex_data[1, 3, 0] = 7.25 # Value at x=3, y=1 -> numpy[y, x, channel]
557+
tex = module.device.create_texture(
558+
type=TextureType.texture_2d,
559+
width=8,
560+
height=4,
561+
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
562+
format=Format.r32_float,
563+
data=tex_data,
564+
)
565+
566+
result = module.sample_texture_2d_broadcast(tex)
567+
assert result == pytest.approx(7.25)
568+
569+
570+
@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
571+
def test_texture_3d_broadcast(device_type: DeviceType):
572+
module = load_test_module(device_type)
573+
574+
# Non-square: width=8, height=6, depth=4, sample at position (2, 1, 3)
575+
tex_data = np.zeros((4, 6, 8, 1), dtype=np.float32)
576+
tex_data[3, 1, 2, 0] = 3.14 # Value at x=2, y=1, z=3 -> numpy[z, y, x, channel]
577+
tex = module.device.create_texture(
578+
type=TextureType.texture_3d,
579+
width=8,
580+
height=6,
581+
depth=4,
582+
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
583+
format=Format.r32_float,
584+
data=tex_data,
585+
)
586+
587+
result = module.sample_texture_3d_broadcast(tex)
588+
assert result == pytest.approx(3.14)
589+
590+
591+
@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
592+
def test_texture_3d_broadcast_with_scalar(device_type: DeviceType):
593+
module = load_test_module(device_type)
594+
595+
# Non-square: width=8, height=6, depth=4, sample at position (2, 1, 3)
596+
tex_data = np.zeros((4, 6, 8, 1), dtype=np.float32)
597+
tex_data[3, 1, 2, 0] = 2.0 # Value at x=2, y=1, z=3 -> numpy[z, y, x, channel]
598+
tex = module.device.create_texture(
599+
type=TextureType.texture_3d,
600+
width=8,
601+
height=6,
602+
depth=4,
603+
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
604+
format=Format.r32_float,
605+
data=tex_data,
606+
)
607+
608+
# tex[2,1,3] = 2.0, value = 3.0, so result should be 6.0
609+
result = module.sample_texture_3d_with_scalar(3.0, tex)
610+
assert result == pytest.approx(6.0)
611+
612+
529613
if __name__ == "__main__":
530614
pytest.main([__file__, "-v", "-s"])

slangpy/tests/slangpy_tests/test_textures.slang

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,23 @@ T passthru<T>(T t)
4242
{
4343
return t;
4444
}
45+
46+
float sample_texture_1d_broadcast(Texture1D<float> tex)
47+
{
48+
return tex[uint(2)];
49+
}
50+
51+
float sample_texture_2d_broadcast(Texture2D<float> tex)
52+
{
53+
return tex[uint2(3, 1)];
54+
}
55+
56+
float sample_texture_3d_broadcast(Texture3D<float> tex)
57+
{
58+
return tex[uint3(2, 1, 3)];
59+
}
60+
61+
float sample_texture_3d_with_scalar(float value, Texture3D<float> tex)
62+
{
63+
return tex[uint3(2, 1, 3)] * value;
64+
}

0 commit comments

Comments
 (0)