1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // CLProgramCL.cpp: Implements the class methods for CLProgramCL.
7*8975f5c5SAndroid Build Coastguard Worker
8*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/cl/CLProgramCL.h"
9*8975f5c5SAndroid Build Coastguard Worker
10*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/cl/CLDeviceCL.h"
11*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/cl/CLKernelCL.h"
12*8975f5c5SAndroid Build Coastguard Worker
13*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLDevice.h"
14*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/CLProgram.h"
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/cl_utils.h"
16*8975f5c5SAndroid Build Coastguard Worker
17*8975f5c5SAndroid Build Coastguard Worker namespace rx
18*8975f5c5SAndroid Build Coastguard Worker {
19*8975f5c5SAndroid Build Coastguard Worker
CLProgramCL(const cl::Program & program,cl_program native)20*8975f5c5SAndroid Build Coastguard Worker CLProgramCL::CLProgramCL(const cl::Program &program, cl_program native)
21*8975f5c5SAndroid Build Coastguard Worker : CLProgramImpl(program), mNative(native)
22*8975f5c5SAndroid Build Coastguard Worker {}
23*8975f5c5SAndroid Build Coastguard Worker
~CLProgramCL()24*8975f5c5SAndroid Build Coastguard Worker CLProgramCL::~CLProgramCL()
25*8975f5c5SAndroid Build Coastguard Worker {
26*8975f5c5SAndroid Build Coastguard Worker if (mNative->getDispatch().clReleaseProgram(mNative) != CL_SUCCESS)
27*8975f5c5SAndroid Build Coastguard Worker {
28*8975f5c5SAndroid Build Coastguard Worker ERR() << "Error while releasing CL program";
29*8975f5c5SAndroid Build Coastguard Worker }
30*8975f5c5SAndroid Build Coastguard Worker }
31*8975f5c5SAndroid Build Coastguard Worker
build(const cl::DevicePtrs & devices,const char * options,cl::Program * notify)32*8975f5c5SAndroid Build Coastguard Worker angle::Result CLProgramCL::build(const cl::DevicePtrs &devices,
33*8975f5c5SAndroid Build Coastguard Worker const char *options,
34*8975f5c5SAndroid Build Coastguard Worker cl::Program *notify)
35*8975f5c5SAndroid Build Coastguard Worker {
36*8975f5c5SAndroid Build Coastguard Worker std::vector<cl_device_id> nativeDevices;
37*8975f5c5SAndroid Build Coastguard Worker for (const cl::DevicePtr &device : devices)
38*8975f5c5SAndroid Build Coastguard Worker {
39*8975f5c5SAndroid Build Coastguard Worker nativeDevices.emplace_back(device->getImpl<CLDeviceCL>().getNative());
40*8975f5c5SAndroid Build Coastguard Worker }
41*8975f5c5SAndroid Build Coastguard Worker const cl_uint numDevices = static_cast<cl_uint>(nativeDevices.size());
42*8975f5c5SAndroid Build Coastguard Worker const cl_device_id *const nativeDevicesPtr =
43*8975f5c5SAndroid Build Coastguard Worker !nativeDevices.empty() ? nativeDevices.data() : nullptr;
44*8975f5c5SAndroid Build Coastguard Worker const cl::ProgramCB callback = notify != nullptr ? Callback : nullptr;
45*8975f5c5SAndroid Build Coastguard Worker ANGLE_CL_TRY(mNative->getDispatch().clBuildProgram(mNative, numDevices, nativeDevicesPtr,
46*8975f5c5SAndroid Build Coastguard Worker options, callback, notify));
47*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
48*8975f5c5SAndroid Build Coastguard Worker }
49*8975f5c5SAndroid Build Coastguard Worker
compile(const cl::DevicePtrs & devices,const char * options,const cl::ProgramPtrs & inputHeaders,const char ** headerIncludeNames,cl::Program * notify)50*8975f5c5SAndroid Build Coastguard Worker angle::Result CLProgramCL::compile(const cl::DevicePtrs &devices,
51*8975f5c5SAndroid Build Coastguard Worker const char *options,
52*8975f5c5SAndroid Build Coastguard Worker const cl::ProgramPtrs &inputHeaders,
53*8975f5c5SAndroid Build Coastguard Worker const char **headerIncludeNames,
54*8975f5c5SAndroid Build Coastguard Worker cl::Program *notify)
55*8975f5c5SAndroid Build Coastguard Worker {
56*8975f5c5SAndroid Build Coastguard Worker std::vector<cl_device_id> nativeDevices;
57*8975f5c5SAndroid Build Coastguard Worker for (const cl::DevicePtr &device : devices)
58*8975f5c5SAndroid Build Coastguard Worker {
59*8975f5c5SAndroid Build Coastguard Worker nativeDevices.emplace_back(device->getImpl<CLDeviceCL>().getNative());
60*8975f5c5SAndroid Build Coastguard Worker }
61*8975f5c5SAndroid Build Coastguard Worker const cl_uint numDevices = static_cast<cl_uint>(nativeDevices.size());
62*8975f5c5SAndroid Build Coastguard Worker const cl_device_id *const nativeDevicesPtr =
63*8975f5c5SAndroid Build Coastguard Worker !nativeDevices.empty() ? nativeDevices.data() : nullptr;
64*8975f5c5SAndroid Build Coastguard Worker
65*8975f5c5SAndroid Build Coastguard Worker std::vector<cl_program> nativePrograms;
66*8975f5c5SAndroid Build Coastguard Worker for (const cl::ProgramPtr &program : inputHeaders)
67*8975f5c5SAndroid Build Coastguard Worker {
68*8975f5c5SAndroid Build Coastguard Worker nativePrograms.emplace_back(program->getImpl<CLProgramCL>().getNative());
69*8975f5c5SAndroid Build Coastguard Worker }
70*8975f5c5SAndroid Build Coastguard Worker const cl_uint numInputHeaders = static_cast<cl_uint>(nativePrograms.size());
71*8975f5c5SAndroid Build Coastguard Worker const cl_program *const inputHeadersPtr =
72*8975f5c5SAndroid Build Coastguard Worker !nativePrograms.empty() ? nativePrograms.data() : nullptr;
73*8975f5c5SAndroid Build Coastguard Worker
74*8975f5c5SAndroid Build Coastguard Worker const cl::ProgramCB callback = notify != nullptr ? Callback : nullptr;
75*8975f5c5SAndroid Build Coastguard Worker ANGLE_CL_TRY(mNative->getDispatch().clCompileProgram(mNative, numDevices, nativeDevicesPtr,
76*8975f5c5SAndroid Build Coastguard Worker options, numInputHeaders, inputHeadersPtr,
77*8975f5c5SAndroid Build Coastguard Worker headerIncludeNames, callback, notify));
78*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
79*8975f5c5SAndroid Build Coastguard Worker }
80*8975f5c5SAndroid Build Coastguard Worker
getInfo(cl::ProgramInfo name,size_t valueSize,void * value,size_t * valueSizeRet) const81*8975f5c5SAndroid Build Coastguard Worker angle::Result CLProgramCL::getInfo(cl::ProgramInfo name,
82*8975f5c5SAndroid Build Coastguard Worker size_t valueSize,
83*8975f5c5SAndroid Build Coastguard Worker void *value,
84*8975f5c5SAndroid Build Coastguard Worker size_t *valueSizeRet) const
85*8975f5c5SAndroid Build Coastguard Worker {
86*8975f5c5SAndroid Build Coastguard Worker ANGLE_CL_TRY(mNative->getDispatch().clGetProgramInfo(mNative, cl::ToCLenum(name), valueSize,
87*8975f5c5SAndroid Build Coastguard Worker value, valueSizeRet));
88*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
89*8975f5c5SAndroid Build Coastguard Worker }
90*8975f5c5SAndroid Build Coastguard Worker
getBuildInfo(const cl::Device & device,cl::ProgramBuildInfo name,size_t valueSize,void * value,size_t * valueSizeRet) const91*8975f5c5SAndroid Build Coastguard Worker angle::Result CLProgramCL::getBuildInfo(const cl::Device &device,
92*8975f5c5SAndroid Build Coastguard Worker cl::ProgramBuildInfo name,
93*8975f5c5SAndroid Build Coastguard Worker size_t valueSize,
94*8975f5c5SAndroid Build Coastguard Worker void *value,
95*8975f5c5SAndroid Build Coastguard Worker size_t *valueSizeRet) const
96*8975f5c5SAndroid Build Coastguard Worker {
97*8975f5c5SAndroid Build Coastguard Worker ANGLE_CL_TRY(mNative->getDispatch().clGetProgramBuildInfo(
98*8975f5c5SAndroid Build Coastguard Worker mNative, device.getImpl<CLDeviceCL>().getNative(), cl::ToCLenum(name), valueSize, value,
99*8975f5c5SAndroid Build Coastguard Worker valueSizeRet));
100*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
101*8975f5c5SAndroid Build Coastguard Worker }
102*8975f5c5SAndroid Build Coastguard Worker
createKernel(const cl::Kernel & kernel,const char * name,CLKernelImpl::Ptr * kernelOut)103*8975f5c5SAndroid Build Coastguard Worker angle::Result CLProgramCL::createKernel(const cl::Kernel &kernel,
104*8975f5c5SAndroid Build Coastguard Worker const char *name,
105*8975f5c5SAndroid Build Coastguard Worker CLKernelImpl::Ptr *kernelOut)
106*8975f5c5SAndroid Build Coastguard Worker {
107*8975f5c5SAndroid Build Coastguard Worker cl_int errorCode = CL_SUCCESS;
108*8975f5c5SAndroid Build Coastguard Worker
109*8975f5c5SAndroid Build Coastguard Worker const cl_kernel nativeKernel = mNative->getDispatch().clCreateKernel(mNative, name, &errorCode);
110*8975f5c5SAndroid Build Coastguard Worker ANGLE_CL_TRY(errorCode);
111*8975f5c5SAndroid Build Coastguard Worker
112*8975f5c5SAndroid Build Coastguard Worker *kernelOut =
113*8975f5c5SAndroid Build Coastguard Worker CLKernelImpl::Ptr(nativeKernel != nullptr ? new CLKernelCL(kernel, nativeKernel) : nullptr);
114*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
115*8975f5c5SAndroid Build Coastguard Worker }
116*8975f5c5SAndroid Build Coastguard Worker
createKernels(cl_uint numKernels,CLKernelImpl::CreateFuncs & createFuncs,cl_uint * numKernelsRet)117*8975f5c5SAndroid Build Coastguard Worker angle::Result CLProgramCL::createKernels(cl_uint numKernels,
118*8975f5c5SAndroid Build Coastguard Worker CLKernelImpl::CreateFuncs &createFuncs,
119*8975f5c5SAndroid Build Coastguard Worker cl_uint *numKernelsRet)
120*8975f5c5SAndroid Build Coastguard Worker {
121*8975f5c5SAndroid Build Coastguard Worker if (numKernels == 0u)
122*8975f5c5SAndroid Build Coastguard Worker {
123*8975f5c5SAndroid Build Coastguard Worker ANGLE_CL_TRY(
124*8975f5c5SAndroid Build Coastguard Worker mNative->getDispatch().clCreateKernelsInProgram(mNative, 0u, nullptr, numKernelsRet));
125*8975f5c5SAndroid Build Coastguard Worker }
126*8975f5c5SAndroid Build Coastguard Worker
127*8975f5c5SAndroid Build Coastguard Worker std::vector<cl_kernel> nativeKernels(numKernels, nullptr);
128*8975f5c5SAndroid Build Coastguard Worker ANGLE_CL_TRY(mNative->getDispatch().clCreateKernelsInProgram(
129*8975f5c5SAndroid Build Coastguard Worker mNative, numKernels, nativeKernels.data(), numKernelsRet));
130*8975f5c5SAndroid Build Coastguard Worker for (cl_kernel nativeKernel : nativeKernels)
131*8975f5c5SAndroid Build Coastguard Worker {
132*8975f5c5SAndroid Build Coastguard Worker createFuncs.emplace_back([nativeKernel](const cl::Kernel &kernel) {
133*8975f5c5SAndroid Build Coastguard Worker return CLKernelImpl::Ptr(new CLKernelCL(kernel, nativeKernel));
134*8975f5c5SAndroid Build Coastguard Worker });
135*8975f5c5SAndroid Build Coastguard Worker }
136*8975f5c5SAndroid Build Coastguard Worker return angle::Result::Continue;
137*8975f5c5SAndroid Build Coastguard Worker }
138*8975f5c5SAndroid Build Coastguard Worker
Callback(cl_program program,void * userData)139*8975f5c5SAndroid Build Coastguard Worker void CLProgramCL::Callback(cl_program program, void *userData)
140*8975f5c5SAndroid Build Coastguard Worker {
141*8975f5c5SAndroid Build Coastguard Worker static_cast<cl::Program *>(userData)->callback();
142*8975f5c5SAndroid Build Coastguard Worker }
143*8975f5c5SAndroid Build Coastguard Worker
144*8975f5c5SAndroid Build Coastguard Worker } // namespace rx
145