aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/images/kernel_read_write/test_cl_ext_image_buffer.hpp
blob: c6646330b85e3fe963c62ab4e076c650fcfc2f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Copyright (c) 2022 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef _TEST_CL_EXT_IMAGE_BUFFER
#define _TEST_CL_EXT_IMAGE_BUFFER

#define TEST_IMAGE_SIZE 20

#define GET_EXTENSION_FUNC(platform, function_name)                            \
    function_name##_fn function_name = reinterpret_cast<function_name##_fn>(   \
        clGetExtensionFunctionAddressForPlatform(platform, #function_name));   \
    if (function_name == nullptr)                                              \
    {                                                                          \
        return TEST_FAIL;                                                      \
    }                                                                          \
    do                                                                         \
    {                                                                          \
    } while (false)

static inline size_t aligned_size(size_t size, size_t alignment)
{
    return (size + alignment - 1) & ~(alignment - 1);
}

static inline void* aligned_ptr(void* ptr, size_t alignment)
{
    return (void*)(((uintptr_t)ptr + alignment - 1) & ~(alignment - 1));
}

static inline size_t get_format_size(cl_context context,
                                     cl_image_format* format,
                                     cl_mem_object_type imageType,
                                     cl_mem_flags flags)
{
    cl_image_desc image_desc = { 0 };
    image_desc.image_type = imageType;

    /* Size 1 only to query element size */
    image_desc.image_width = 1;
    if (CL_MEM_OBJECT_IMAGE1D_BUFFER != imageType
        && CL_MEM_OBJECT_IMAGE1D != imageType)
    {
        image_desc.image_height = 1;
    }
    if (CL_MEM_OBJECT_IMAGE3D == imageType
        || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
    {
        image_desc.image_depth = 1;
    }
    if (CL_MEM_OBJECT_IMAGE1D_ARRAY == imageType
        || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
    {
        image_desc.image_array_size = 1;
    }

    cl_int error = 0;
    cl_mem buffer;
    if (imageType == CL_MEM_OBJECT_IMAGE1D_BUFFER)
    {
        buffer = clCreateBuffer(context, flags,
                                get_pixel_size(format) * image_desc.image_width,
                                NULL, &error);
        test_error(error, "Unable to create buffer");

        image_desc.buffer = buffer;
    }

    cl_mem image =
        clCreateImage(context, flags, format, &image_desc, nullptr, &error);
    test_error(error, "Unable to create image");

    size_t element_size = 0;
    error = clGetImageInfo(image, CL_IMAGE_ELEMENT_SIZE, sizeof(element_size),
                           &element_size, nullptr);
    test_error(error, "Error clGetImageInfo");

    error = clReleaseMemObject(image);
    test_error(error, "Unable to release image");

    if (imageType == CL_MEM_OBJECT_IMAGE1D_BUFFER)
    {
        error = clReleaseMemObject(buffer);
        test_error(error, "Unable to release buffer");
    }

    return element_size;
}

static inline void image_desc_init(cl_image_desc* desc,
                                   cl_mem_object_type imageType)
{
    desc->image_type = imageType;
    desc->image_width = TEST_IMAGE_SIZE;
    if (CL_MEM_OBJECT_IMAGE1D_BUFFER != imageType
        && CL_MEM_OBJECT_IMAGE1D != imageType)
    {
        desc->image_height = TEST_IMAGE_SIZE;
    }
    if (CL_MEM_OBJECT_IMAGE3D == imageType
        || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
    {
        desc->image_depth = TEST_IMAGE_SIZE;
    }
    if (CL_MEM_OBJECT_IMAGE1D_ARRAY == imageType
        || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
    {
        desc->image_array_size = TEST_IMAGE_SIZE;
    }
}

#endif /* _TEST_CL_EXT_IMAGE_BUFFER */