Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions slangpy/builtin/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ def resolve_types(self, context: BindContext, bound_type: refl.SlangType):
# Otherwise, use default behaviour from marshall
return None

def resolve_dimensionality(
self,
context: BindContext,
binding: "BoundVariable",
vector_target_type: refl.SlangType,
):
# If target type is a texture, the texture is passed directly (broadcast as-is)
if isinstance(vector_target_type, refl.TextureType):
return 0

# the texture is being used for per-pixel operations
return self.texture_dims

# Texture is writable if it has unordered access view.
@property
def is_writable(self):
Expand Down
78 changes: 78 additions & 0 deletions slangpy/tests/slangpy_tests/test_textures.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,5 +526,83 @@ def test_texture_return_value_str(
texture_return_value_impl(device_type, texel_name, dims, channels, "texture")


@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
def test_texture_1d_broadcast(device_type: DeviceType):
if device_type == DeviceType.cuda:
pytest.skip("1D texture read returns zero on CUDA backend")
module = load_test_module(device_type)

tex_data = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32).reshape((4, 1))
tex = module.device.create_texture(
type=TextureType.texture_1d,
width=4,
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
format=Format.r32_float,
data=tex_data,
)

result = module.sample_texture_1d_broadcast(tex)
assert result == pytest.approx(1.0)


@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
def test_texture_2d_broadcast(device_type: DeviceType):
module = load_test_module(device_type)

tex_data = np.full((4, 4, 1), 2.5, dtype=np.float32)
tex_data[0, 0, 0] = 7.0 # Value at position (0,0)
tex = module.device.create_texture(
type=TextureType.texture_2d,
width=4,
height=4,
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
format=Format.r32_float,
data=tex_data,
)

result = module.sample_texture_2d_broadcast(tex)
assert result == pytest.approx(7.0)


@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
def test_texture_3d_broadcast(device_type: DeviceType):
module = load_test_module(device_type)

tex_data = np.full((4, 4, 4, 1), 1.0, dtype=np.float32)
tex_data[0, 0, 0, 0] = 3.14 # Value at position (0,0,0)
tex = module.device.create_texture(
type=TextureType.texture_3d,
width=4,
height=4,
depth=4,
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
format=Format.r32_float,
data=tex_data,
)

result = module.sample_texture_3d_broadcast(tex)
assert result == pytest.approx(3.14)


@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
def test_texture_3d_broadcast_with_scalar(device_type: DeviceType):
module = load_test_module(device_type)

tex_data = np.full((4, 4, 4, 1), 2.0, dtype=np.float32)
tex = module.device.create_texture(
type=TextureType.texture_3d,
width=4,
height=4,
depth=4,
usage=TextureUsage.shader_resource | TextureUsage.unordered_access,
format=Format.r32_float,
data=tex_data,
)

# tex[0,0,0] = 2.0, value = 3.0, so result should be 6.0
result = module.sample_texture_3d_with_scalar(3.0, tex)
assert result == pytest.approx(6.0)


if __name__ == "__main__":
pytest.main([__file__, "-v", "-s"])
20 changes: 20 additions & 0 deletions slangpy/tests/slangpy_tests/test_textures.slang
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ T passthru<T>(T t)
{
return t;
}

float sample_texture_1d_broadcast(Texture1D<float> tex)
{
return tex[uint(0)];
}

float sample_texture_2d_broadcast(Texture2D<float> tex)
{
return tex[uint2(0, 0)];
}

float sample_texture_3d_broadcast(Texture3D<float> tex)
{
return tex[uint3(0, 0, 0)];
}

float sample_texture_3d_with_scalar(float value, Texture3D<float> tex)
{
return tex[uint3(0, 0, 0)] * value;
}