1 //
2 // Copyright (c) 2022 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef CL_KHR_BASIC_COMMAND_BUFFER_H
17 #define CL_KHR_BASIC_COMMAND_BUFFER_H
18 
19 #include "command_buffer_test_base.h"
20 #include "harness/typeWrappers.h"
21 
22 #define ADD_PROP(prop)                                                         \
23     {                                                                          \
24         prop, #prop                                                            \
25     }
26 
27 #define CHECK_VERIFICATION_ERROR(reference, result, index)                     \
28     {                                                                          \
29         if (reference != result)                                               \
30         {                                                                      \
31             log_error("Expected %d was %d at index %zu\n", reference, result,  \
32                       index);                                                  \
33             return TEST_FAIL;                                                  \
34         }                                                                      \
35     }
36 
37 // If it is supported get the addresses of all the APIs here.
38 #define GET_EXTENSION_ADDRESS(FUNC)                                            \
39     FUNC = reinterpret_cast<FUNC##_fn>(                                        \
40         clGetExtensionFunctionAddressForPlatform(platform, #FUNC));            \
41     if (FUNC == nullptr)                                                       \
42     {                                                                          \
43         log_error("ERROR: clGetExtensionFunctionAddressForPlatform failed"     \
44                   " with " #FUNC "\n");                                        \
45         return TEST_FAIL;                                                      \
46     }
47 
48 
49 // Helper test fixture for constructing OpenCL objects used in testing
50 // a variety of simple command-buffer enqueue scenarios.
51 struct BasicCommandBufferTest : CommandBufferTestBase
52 {
53 
54     BasicCommandBufferTest(cl_device_id device, cl_context context,
55                            cl_command_queue queue);
56 
57     virtual bool Skip();
58     virtual cl_int SetUpKernel(void);
59     virtual cl_int SetUpKernelArgs(void);
60     virtual cl_int SetUp(int elements);
61 
62     // Test body returning an OpenCL error code
63     virtual cl_int Run() = 0;
64 
65 protected:
data_sizeBasicCommandBufferTest66     virtual size_t data_size() const { return num_elements * sizeof(cl_int); }
67 
68     cl_context context;
69     clCommandQueueWrapper queue;
70     clProgramWrapper program;
71     clKernelWrapper kernel;
72     clMemWrapper in_mem, out_mem, off_mem;
73     size_t num_elements;
74 
75     // Device support query results
76     bool simultaneous_use_support;
77     bool out_of_order_support;
78 
79     // user request for simultaneous use
80     bool simultaneous_use_requested;
81     unsigned buffer_size_multiplier;
82     clCommandBufferWrapper command_buffer;
83 };
84 
85 
86 template <class T>
MakeAndRunTest(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)87 int MakeAndRunTest(cl_device_id device, cl_context context,
88                    cl_command_queue queue, int num_elements)
89 {
90     CHECK_COMMAND_BUFFER_EXTENSION_AVAILABLE(device);
91 
92     try
93     {
94         auto test_fixture = T(device, context, queue);
95 
96         if (test_fixture.Skip())
97         {
98             return TEST_SKIPPED_ITSELF;
99         }
100 
101         cl_int error = test_fixture.SetUp(num_elements);
102         test_error_ret(error, "Error in test initialization", TEST_FAIL);
103 
104         error = test_fixture.Run();
105         test_error_ret(error, "Test Failed", TEST_FAIL);
106     } catch (const std::runtime_error &e)
107     {
108         log_error("%s", e.what());
109         return TEST_FAIL;
110     }
111 
112     return TEST_PASS;
113 }
114 
115 #endif // CL_KHR_BASIC_COMMAND_BUFFER_H
116