|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import base64 |
| 7 | + |
| 8 | +import pytest |
| 9 | +from executorch.backends.arm.vgf.shaders.grid_sampler import ( |
| 10 | + build_grid_sampler_2d_payload, |
| 11 | + decode_payload, |
| 12 | + encode_payload, |
| 13 | + GRID_SAMPLER_2D_SHADER_BINARY, |
| 14 | + GRID_SAMPLER_2D_SHADER_ENTRY_POINT, |
| 15 | + GRID_SAMPLER_2D_SHADER_LANGUAGE, |
| 16 | + GRID_SAMPLER_2D_SHADER_SOURCE, |
| 17 | + GRID_SAMPLER_2D_VK_FORMAT, |
| 18 | + GRID_SAMPLER_2D_WORKGROUP_SIZES, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def test_grid_sampler_2d_custom_shader_payload_no_target_round_trip(): |
| 23 | + payload = build_grid_sampler_2d_payload( |
| 24 | + interpolation_mode=0, |
| 25 | + padding_mode=2, |
| 26 | + align_corners=True, |
| 27 | + ) |
| 28 | + decoded = decode_payload(encode_payload(payload)) |
| 29 | + |
| 30 | + assert decoded["entry_point"] == GRID_SAMPLER_2D_SHADER_ENTRY_POINT |
| 31 | + assert decoded["workgroup_sizes"] == GRID_SAMPLER_2D_WORKGROUP_SIZES |
| 32 | + assert decoded["shader_language"] == GRID_SAMPLER_2D_SHADER_LANGUAGE |
| 33 | + assert base64.b64decode(decoded["shader_code"])[:4] == b"\x03\x02\x23\x07" |
| 34 | + assert decoded["input_0_type"] == "Tensor" |
| 35 | + assert decoded["input_0_vkformat"] == GRID_SAMPLER_2D_VK_FORMAT |
| 36 | + assert decoded["input_0_vkdescriptortype"] == "VK_DESCRIPTOR_TYPE_STORAGE_BUFFER" |
| 37 | + assert decoded["input_0_binding"] == 0 |
| 38 | + assert decoded["input_1_type"] == "Tensor" |
| 39 | + assert decoded["input_1_vkformat"] == GRID_SAMPLER_2D_VK_FORMAT |
| 40 | + assert decoded["input_1_vkdescriptortype"] == "VK_DESCRIPTOR_TYPE_STORAGE_BUFFER" |
| 41 | + assert decoded["input_1_binding"] == 1 |
| 42 | + assert decoded["output_0_type"] == "Tensor" |
| 43 | + assert decoded["output_0_vkformat"] == GRID_SAMPLER_2D_VK_FORMAT |
| 44 | + assert decoded["output_0_vkdescriptortype"] == "VK_DESCRIPTOR_TYPE_STORAGE_BUFFER" |
| 45 | + assert decoded["output_0_binding"] == 2 |
| 46 | + |
| 47 | + |
| 48 | +def test_grid_sampler_2d_custom_shader_payload_no_target_uses_spirv(): |
| 49 | + payload = build_grid_sampler_2d_payload( |
| 50 | + interpolation_mode=0, |
| 51 | + padding_mode=0, |
| 52 | + align_corners=False, |
| 53 | + ) |
| 54 | + |
| 55 | + shader_binary = base64.b64decode(payload["shader_code"]) |
| 56 | + |
| 57 | + assert payload["shader_language"] == "SPIR-V" |
| 58 | + assert shader_binary[:4] == b"\x03\x02\x23\x07" |
| 59 | + |
| 60 | + |
| 61 | +def test_grid_sampler_2d_custom_shader_payload_no_target_has_shader_resources(): |
| 62 | + assert GRID_SAMPLER_2D_SHADER_SOURCE == "grid_sampler.glsl" |
| 63 | + assert GRID_SAMPLER_2D_SHADER_BINARY == "grid_sampler.spirv.b64" |
| 64 | + |
| 65 | + |
| 66 | +def test_grid_sampler_2d_custom_shader_payload_no_target_rejects_bad_modes(): |
| 67 | + with pytest.raises(ValueError, match="Unsupported interpolation_mode"): |
| 68 | + build_grid_sampler_2d_payload( |
| 69 | + interpolation_mode=99, |
| 70 | + padding_mode=0, |
| 71 | + align_corners=False, |
| 72 | + ) |
| 73 | + |
| 74 | + with pytest.raises(ValueError, match="Unsupported padding_mode"): |
| 75 | + build_grid_sampler_2d_payload( |
| 76 | + interpolation_mode=0, |
| 77 | + padding_mode=99, |
| 78 | + align_corners=False, |
| 79 | + ) |
0 commit comments