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_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_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_manual(self): buffer_usage = BufferUsage.STORAGE_BUFFER buffer_layout = Layout.STD140 buffer_order = Order.ROW_MAJOR structA = Struct([Vector.ivec2(), Scalar.double()], buffer_layout, member_names=["a", "b"], type_name="structA") structB = Struct([Scalar.uint(), Scalar.double()], buffer_layout, type_name="structB") structC = Struct([structB, Vector.ivec2()], buffer_layout, type_name="structC") structs = [structA, structB, structC] variables = [ Vector.vec3(), Vector.ivec4(), Array(structC, 2, buffer_layout), Vector.ivec4(), Scalar.uint(), Array(Scalar.double(), (5, 2), buffer_layout), Scalar.int(), Array(Vector.vec4(), (2, 3, 4), buffer_layout), Vector.dvec2(), structA ] container = Struct(variables, buffer_layout, type_name="block") print(container) glsl = self.build_glsl_program(container, structs, buffer_usage) # print(glsl) values, array_expected = self.build_values(container.definitions) array_expected = np.array(array_expected, dtype=np.float32) bytez_input = container.to_bytes(values) bytez_output = self.run_program(glsl, bytez_input, array_expected.nbytes, usage_input=buffer_usage) array = np.frombuffer(bytez_output, dtype=array_expected.dtype) print(array_expected) print(array) print("equal", ((array_expected - array) == 0).all())
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_manually(self): buffer_usage = BufferUsage.STORAGE_BUFFER buffer_layout = Layout.STD140 buffer_order = Order.ROW_MAJOR structA = Struct([Vector.ivec2(), Scalar.double()], buffer_layout, member_names=["a", "b"], type_name="structA") structB = Struct([Scalar.uint(), Scalar.double()], buffer_layout, type_name="structB") structC = Struct([structB, Vector.ivec2()], buffer_layout, type_name="structC") structs = [structA, structB, structC] variables = [ Vector.vec3(), Vector.ivec4(), Array(structC, 2, buffer_layout), Vector.ivec4(), Scalar.uint(), Array(Scalar.double(), (5, 2), buffer_layout), Scalar.int(), Array(Vector.vec4(), (2, 3, 4), buffer_layout), Vector.dvec2(), structA ] container = Struct(variables, buffer_layout, type_name="block") print(container) glsl = self.build_glsl_program(container, structs, buffer_usage) # print(glsl) values_expected, array = self.build_values(container.definitions) array = np.array(array, dtype=np.float32) bytez_in = array.tobytes() # std430 bytez_out = self.run_program(glsl, bytez_in, container.size()) values = container.from_bytes(bytez_out) register = {} steps = {Scalar: 0, Vector: 0, Array: 0, Struct: 0} for struct in structs + [container]: self.build_register(register, struct, steps) values_ftd = self.format_values(container, values, register) values_expected_ftd = self.format_values(container, values_expected, register) print("") self.print_formatted_values(values_ftd) print("") self.print_formatted_values(values_expected_ftd) print("") print("") print(values_ftd == values_expected_ftd)