def test_bytecode_parsing_without_gpu(self): with self.env_backup(): del os.environ["VULKAN_SDK"] import lava as lv from lava.api.bytecode.logical import ByteCode from lava.api.bytecode.physical import ByteCodeData from lava.api.bytes import Vector, Scalar, Struct from lava.api.constants.spirv import Layout self.assertTrue(not lv.initialized()) glsl = """ #version 450 #extension GL_ARB_separate_shader_objects : enable layout(local_size_x=1, local_size_y=1, local_size_z=1) in; layout(std430, binding = 0) readonly buffer bufIn { vec3 var1; }; layout(std430, binding = 1) writeonly buffer bufOut { float var2; }; void main() { var2 = var1.x + var1.y + var1.z; } """ path_shader = write_to_temp_file(glsl, suffix=".comp") path_shader_spirv = lv.compile_glsl(path_shader, verbose=True) with self.env_backup(): del os.environ["VULKAN_SDK"] self.assertTrue(not lv.initialized()) byte_code_data = ByteCodeData.from_file(path_shader_spirv) byte_code = ByteCode(byte_code_data, None) quiet = True container0 = Struct([Vector.vec3()], Layout.STD430) container1 = Struct([Scalar.float()], Layout.STD430) self.assertTrue( container0.compare(byte_code.get_block_definition(0), quiet=quiet)) self.assertTrue( container1.compare(byte_code.get_block_definition(1), quiet=quiet)) os.remove(path_shader) os.remove(path_shader_spirv)
def test_detection_type_bools(self): glsl = """ #version 450 #extension GL_ARB_separate_shader_objects : enable layout({layout}, binding = 0) buffer Buffer {{ bool var1; bool[720][1280] var2; bvec2 var3; bvec3 var4; bvec4 var5; bvec3[5] var6; }}; void main() {{}} """ for layout in (Layout.STD140, Layout.STD430): # the vulkan spir-v compiler turns bools into uints expected_definition = Struct([ Scalar.uint(), Array(Scalar.uint(), (720, 1280), layout), Vector.uvec2(), Vector.uvec3(), Vector.uvec4(), Array(Vector.uvec3(), 5, layout) ], layout) shader = self.shader_from_txt(glsl.format(layout=layout), verbose=False) shader.inspect() detected_definition, _ = shader.code.get_block(0) equal = expected_definition.compare(detected_definition, quiet=True) self.assertTrue(equal)
def test_detection_type_arrays(self): rng = np.random.RandomState(321) variables = [ Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double() ] variables += [ Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL) ] for definition, layout, _ in itertools.product( variables, [Layout.STD140, Layout.STD430], range(3)): container = Struct( [Array(definition, Random.shape(rng, 3, 5), layout)], layout) glsl = self.build_glsl_program( ((container, 0, BufferUsage.STORAGE_BUFFER), )) shader = self.shader_from_txt(glsl, verbose=False) shader.inspect() detected_definition, _ = shader.get_block(0) self.assertTrue(container.compare(detected_definition, quiet=True)) if isinstance(definition, Vector): if definition.length( ) < 3 and definition.dtype != DataType.DOUBLE: self.assertEqual(detected_definition.layout, layout)
def test_detection_binding(self): container = Struct([Scalar.int(), Vector.vec3()], Layout.STD140) for binding, usage in itertools.product([0, 1, 2, 3, 4, 99, 512], [BufferUsage.UNIFORM_BUFFER, BufferUsage.STORAGE_BUFFER]): glsl = self.build_glsl_program(((container, binding, usage),)) shader = self.shader_from_txt(glsl, verbose=False) shader.inspect() detected_definition, detected_usage = shader.code.get_block(binding) self.assertEqual(detected_usage, usage) equal = container.compare(detected_definition, quiet=True) self.assertTrue(equal)
def test_struct_shared_between_different_layouts(self): glsl = """ #version 450 #extension GL_ARB_separate_shader_objects : enable struct Shared { int var1; double var2; }; layout(std140, binding = 0) buffer BufferA { uvec2 varA1; Shared varA2; // expected offset 16 }; layout(std430, binding = 1) buffer BufferB { uvec2 varB1; Shared varB2; // expected offset 8 }; void main() {} """ shared_std140 = Struct([Scalar.int(), Scalar.double()], Layout.STD140) shared_std430 = Struct([Scalar.int(), Scalar.double()], Layout.STD430) container_std140 = Struct([Vector.uvec2(), shared_std140], Layout.STD140) container_std430 = Struct([Vector.uvec2(), shared_std430], Layout.STD430) shader = self.shader_from_txt(glsl, verbose=False) shader.inspect() definition0, _ = shader.get_block(0) definition1, _ = shader.get_block(1) self.assertTrue(container_std140.compare(definition0, quiet=True)) self.assertFalse(container_std140.compare(definition1, quiet=True)) self.assertFalse(container_std430.compare(definition0, quiet=True)) self.assertTrue(container_std430.compare(definition1, quiet=True))
def test_detection_type_arrays_of_matrices(self): rng = np.random.RandomState(321) matrix_attributes = itertools.product(range(2, 5), range(2, 5), [DataType.FLOAT, DataType.DOUBLE]) for (n, m, dtype), layout, _ in itertools.product(matrix_attributes, [Layout.STD140, Layout.STD430], range(3)): matrix = Matrix(n, m, dtype, layout) container = Struct([Array(matrix, Random.shape(rng, 3, 5), layout)], layout) glsl = self.build_glsl_program(((container, 0, BufferUsage.STORAGE_BUFFER),)) shader = self.shader_from_txt(glsl, verbose=False) shader.inspect() detected_definition, _ = shader.code.get_block(0) self.assertTrue(container.compare(detected_definition, quiet=True))