def test_arb_example_std140(self): layout = Layout.STD430 struct_a = Struct( [ Scalar.int(), Vector.uvec2() # actually bvec2 ], layout, type_name="structA") struct_b = Struct([ Vector.uvec3(), Vector.vec2(), Array(Scalar.float(), 2, layout), Vector.vec2(), Array(Matrix(3, 3, DataType.FLOAT, layout), 2, layout) ], layout, type_name="structB") container = Struct([ Scalar.float(), Vector.vec2(), Vector.vec3(), struct_a, Scalar.float(), Array(Scalar.float(), 2, layout), Matrix(2, 3, DataType.FLOAT, layout), Array(struct_b, 2, layout) ], layout) self.run_test(container, [struct_a, struct_b], BufferUsage.STORAGE_BUFFER)
def test_scalars_and_vectors(self): rng = np.random.RandomState(123) variables = [ Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double() ] variables += [ Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL) ] containers_std140 = [Struct(variables, Layout.STD140)] containers_std430 = [Struct(variables, Layout.STD430)] for _ in range(5): containers_std140.append( Struct(rng.permutation(variables), Layout.STD140)) containers_std430.append( Struct(rng.permutation(variables), Layout.STD430)) for container in containers_std140: self.run_test(container, [], BufferUsage.STORAGE_BUFFER) self.run_test(container, [], BufferUsage.UNIFORM_BUFFER) for container in containers_std430: self.run_test(container, [], BufferUsage.STORAGE_BUFFER)
def test_detection_type_nested_with_structs(self): rng = np.random.RandomState(321) simple = [Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double()] simple += [Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL)] for layout, _ in itertools.product([Layout.STD140, Layout.STD430], range(5)): matrices = [Matrix(n, m, dtype, layout) for n, m, dtype in itertools.product(range(2, 5), range(2, 5), [DataType.FLOAT, DataType.DOUBLE])] simple_and_matrices = simple + matrices struct = Struct(rng.choice(simple_and_matrices, size=3, replace=False), layout, type_name="SomeStruct") structs = [struct] for _ in range(4): members = [structs[-1]] + rng.choice(simple_and_matrices, size=2, replace=False).tolist() structs.append(Struct(rng.permutation(members), layout, type_name="SomeStruct{}".format(len(structs)))) container = structs[-1] structs = structs[:-1] glsl = self.build_glsl_program(((container, 0, BufferUsage.STORAGE_BUFFER),), structs) shader = self.shader_from_txt(glsl, verbose=False) shader.inspect() definition, _ = shader.code.get_block(0) self.assertTrue(container.compare(definition, quiet=True))
def test_detection_layout_stdxxx_ubo(self): variables = [ Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double() ] variables += [ Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL) ] binding = 0 usage = BufferUsage.UNIFORM_BUFFER glsl = self.build_glsl_program( ((Struct(variables, Layout.STD140), binding, usage), )) shader = self.shader_from_txt(glsl, verbose=False) shader.inspect() definition, detected_usage = shader.get_block(binding) self.assertEqual(detected_usage, usage) self.assertEqual( definition.layout, Layout.STD140) # uniform buffer objects can not use std430
def test_detection_layout_stdxxx_ssbo(self): variables = [ Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double() ] variables += [ Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL) ] binding = 0 usage = BufferUsage.STORAGE_BUFFER glsl_std140 = self.build_glsl_program( ((Struct(variables, Layout.STD140), binding, usage), )) glsl_std430 = self.build_glsl_program( ((Struct(variables, Layout.STD430), binding, usage), )) glsls = [glsl_std140, glsl_std430] for glsl in glsls: shader = self.shader_from_txt(glsl, verbose=False) shader.inspect() definition, detected_usage = shader.get_block(binding) self.assertEqual(detected_usage, usage) self.assertEqual(definition.layout, Layout.STDXXX)
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_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_array_of_scalars(self): rng = np.random.RandomState(123) scalar_types = [ Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double() ] for definition, layout, _ in itertools.product( scalar_types, [Layout.STD140, Layout.STD430], range(5)): container = Struct( [Array(definition, Random.shape(rng, 5, 7), layout)], layout) self.run_test(container, [], BufferUsage.STORAGE_BUFFER) if layout == Layout.STD140: self.run_test(container, [], BufferUsage.UNIFORM_BUFFER)
def test_array_of_structs(self): rng = np.random.RandomState(123) simple = [Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double()] simple += [ Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL) ] for layout, _ in itertools.product([Layout.STD140, Layout.STD430], range(5)): struct = Struct(rng.choice(simple, size=3, replace=False), layout, type_name="SomeStruct") array = Array(struct, Random.shape(rng, 3, 5), layout) container = Struct([array], layout) self.run_test(container, [struct], BufferUsage.STORAGE_BUFFER) if layout == Layout.STD140: self.run_test(container, [struct], BufferUsage.UNIFORM_BUFFER)
def test_nested_with_arrays_of_structs(self): rng = np.random.RandomState(123) simple = [Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double()] simple += [ Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL) ] for layout, _ in itertools.product([Layout.STD140, Layout.STD430], range(10)): matrices = [ Matrix(n, m, dtype, layout) for n, m, dtype in itertools.product(range(2, 5), range( 2, 5), [DataType.FLOAT, DataType.DOUBLE]) ] simple_and_matrices = simple + matrices struct = Struct(rng.choice(simple_and_matrices, size=4, replace=False), layout, type_name="SomeStruct") structs = [struct] arrays = [Array(struct, Random.shape(rng, 2, 3), layout)] for _ in range(2): members = [arrays[-1]] + rng.choice( simple_and_matrices, size=3, replace=False).tolist() structs.append( Struct(rng.permutation(members), layout, type_name="SomeStruct{}".format(len(structs)))) arrays.append( Array(structs[-1], Random.shape(rng, 2, 3), layout)) container = structs[-1] structs = structs[:-1] self.run_test(container, structs, BufferUsage.STORAGE_BUFFER) if layout == Layout.STD140: self.run_test(container, structs, BufferUsage.UNIFORM_BUFFER)
def test_manually(self): # byte cache test buffer_usage = BufferUsage.STORAGE_BUFFER buffer_layout = Layout.STD430 buffer_order = Order.ROW_MAJOR struct1 = Struct([Vector.vec3(), Vector.ivec2()], buffer_layout, member_names=["a", "b"], type_name="structB") struct2 = Struct( [Scalar.double(), Scalar.double(), struct1], buffer_layout, type_name="structC") structs = [struct1, struct2] variables = [ Scalar.uint(), Array(Vector.vec2(), (5, 2, 3), buffer_layout), Array(Scalar.float(), 5, buffer_layout), struct2, # this struct needs padding at the end Scalar.uint(), Array(struct1, (2, 3), buffer_layout) ] container = Struct(variables, buffer_layout, type_name="block") cache = ByteCache(container) print("") print("") pprint.pprint(cache.values) print(cache[-1][0][0]["a"]) print("") print("") pprint.pprint(cache) print(cache[-1][0][0]) print("") print("") pprint.pprint(cache.get_as_dict())
def test_scalars_and_vectors_and_matrices(self): rng = np.random.RandomState(123) variables = [ Scalar.uint(), Scalar.int(), Scalar.float(), Scalar.double() ] variables += [ Vector(n, dtype) for n, dtype in itertools.product(range(2, 5), DataType.ALL) ] matrix_combinations = itertools.product(range(2, 5), range( 2, 5), [DataType.FLOAT, DataType.DOUBLE]) variables_std140 = variables + [ Matrix(n, m, dtype, Layout.STD140) for n, m, dtype in matrix_combinations ] variables_std430 = variables + [ Matrix(n, m, dtype, Layout.STD430) for n, m, dtype in matrix_combinations ] containers_std140 = [Struct(variables_std140, Layout.STD140)] containers_std430 = [Struct(variables_std430, Layout.STD430)] for _ in range(5): containers_std140.append( Struct(rng.permutation(variables_std140), Layout.STD140)) containers_std430.append( Struct(rng.permutation(variables_std430), Layout.STD430)) for container in containers_std140 + containers_std430: self.run_test(container, [], BufferUsage.STORAGE_BUFFER)