Ejemplo n.º 1
0
    def test_pass_through_array_of_scalars(self):
        glsl_template = """
            #version 450
            #extension GL_ARB_separate_shader_objects : enable

            layout(local_size_x=1, local_size_y=1, local_size_z=1) in;

            layout({layout_in}, binding = 0) buffer BufferA {{
                {dtype}[720][1280][3] imageIn;
            }};

            layout({layout_out}, binding = 1) buffer BufferB {{
                {dtype}[720][1280][3] imageOut;
            }};

            void main() {{
                vec3 pixel = gl_GlobalInvocationID;
                int h = int(pixel.x);
                int w = int(pixel.y);
                int c = int(pixel.z);

                imageOut[h][w][c] = imageIn[h][w][c];
            }}
            """

        rng = np.random.RandomState(123)
        w = 1280
        h = 720

        for layout_in, layout_out, dtype in itertools.product(
                self.LAYOUTS, self.LAYOUTS, DataType.ALL):
            scalar = Scalar.of(dtype)
            im = rng.randint(0, 255,
                             size=(h, w, 3)).astype(scalar.numpy_dtype())

            glsl = glsl_template.format(
                **{
                    "layout_in": self.LAYOUT_MAP[layout_in],
                    "layout_out": self.LAYOUT_MAP[layout_out],
                    "dtype": scalar.glsl_dtype()
                })

            shader = self.shader_from_txt(glsl, verbose=False)
            shader.inspect()

            cache_in = ByteCache(shader.code.get_block_definition(0))
            cache_in["imageIn"] = im
            bytez_in = cache_in.definition.to_bytes(cache_in.get_as_dict())

            cache_out = ByteCache(shader.code.get_block_definition(1))
            bytez_out_count = cache_out.definition.size()
            bytez_out = self.run_compiled_program(shader,
                                                  bytez_in,
                                                  bytez_out_count,
                                                  groups=im.shape)
            cache_out.set_from_dict(cache_out.definition.from_bytes(bytez_out))

            self.assertTrue((cache_out["imageOut"] == im).all())
Ejemplo n.º 2
0
    def inspect(self):
        self.block_data = self.byte_code.find_blocks()
        self.definitions_block = {}

        defs_scalar = {
            index: Scalar.of(dtype)
            for index, dtype in self.byte_code.types_scalar.items()
        }
        defs_vector = {
            index: Vector(n, dtype)
            for index, (dtype, n) in self.byte_code.types_vector.items()
        }
        defs_array = {}
        defs_struct = {}

        for binding in self.get_bindings():
            index, _ = self.get_block_index(binding)
            self.deduce_definition(index, defs_scalar, defs_vector, defs_array,
                                   defs_struct, index)
            self.definitions_block[index] = defs_struct[index]
            self.deduce_layout(binding)
Ejemplo n.º 3
0
    def test_pass_through_struct(self):
        glsl_template = """
            #version 450
            #extension GL_ARB_separate_shader_objects : enable

            layout(local_size_x=1, local_size_y=1, local_size_z=1) in;

            {struct_glsl}

            layout({layout_in}, binding = 0) buffer BufferA {{
                {dtype} structIn;
            }};

            layout({layout_out}, binding = 1) buffer BufferB {{
                {dtype} structOut;
            }};

            void main() {{
                structOut = structIn;
            }}
            """

        rng = np.random.RandomState(123)

        scalars = [Scalar.of(dtype) for dtype in DataType.ALL]
        vectors = [
            Vector(n, dtype)
            for n, dtype in itertools.product(range(2, 5), DataType.ALL)
        ]

        type_name = "SomeStruct"

        for layout_in, layout_out in itertools.product(self.LAYOUTS,
                                                       self.LAYOUTS):
            members = rng.permutation(scalars + scalars + vectors + vectors)

            glsl_struct = ["struct {} {{".format(type_name)]
            for i, member in enumerate(members):
                glsl_struct.append("{} member{};".format(
                    member.glsl_dtype(), i))
            glsl_struct.append("};")

            glsl = glsl_template.format(
                **{
                    "layout_in": self.LAYOUT_MAP[layout_in],
                    "layout_out": self.LAYOUT_MAP[layout_out],
                    "struct_glsl": "\n".join(glsl_struct),
                    "dtype": type_name
                })

            shader = self.shader_from_txt(glsl, verbose=False)
            shader.inspect()

            cache_in = ByteCache(shader.code.get_block_definition(0))

            for i, member in enumerate(members):
                if isinstance(member, Scalar):
                    value = member.numpy_dtype()(1.)
                elif isinstance(member, Vector):
                    value = np.ones(member.length(),
                                    member.scalar.numpy_dtype())
                else:
                    value = None

                cache_in["structIn"][i] = value

            bytez_in = cache_in.definition.to_bytes(cache_in.get_as_dict())

            cache_out = ByteCache(shader.code.get_block_definition(1))
            bytez_out_count = cache_out.definition.size()
            bytez_out = self.run_compiled_program(shader, bytez_in,
                                                  bytez_out_count)
            cache_out.set_from_dict(cache_out.definition.from_bytes(bytez_out))

            for i, member in enumerate(members):
                a = cache_in["structIn"][i]
                b = cache_out["structOut"][i]
                if isinstance(member, Vector):
                    a = a.tolist()
                    b = b.tolist()
                self.assertEqual(a, b)