basisu_opencl.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. // basisu_opencl.cpp
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  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. #include "basisu_opencl.h"
  16. // If 1, the kernel source code will come from encoders/ocl_kernels.h. Otherwise, it will be read from the "ocl_kernels.cl" file in the current directory (for development).
  17. #define BASISU_USE_OCL_KERNELS_HEADER (1)
  18. #define BASISU_OCL_KERNELS_FILENAME "ocl_kernels.cl"
  19. #if BASISU_SUPPORT_OPENCL
  20. #include "basisu_enc.h"
  21. // We only use OpenCL v1.2 or less.
  22. #define CL_TARGET_OPENCL_VERSION 120
  23. #ifdef __APPLE__
  24. #include <OpenCL/opencl.h>
  25. #else
  26. #include <CL/cl.h>
  27. #endif
  28. #define BASISU_OPENCL_ASSERT_ON_ANY_ERRORS (1)
  29. namespace basisu
  30. {
  31. #if BASISU_USE_OCL_KERNELS_HEADER
  32. #include "basisu_ocl_kernels.h"
  33. #endif
  34. static void ocl_error_printf(const char* pFmt, ...)
  35. {
  36. va_list args;
  37. va_start(args, pFmt);
  38. error_vprintf(pFmt, args);
  39. va_end(args);
  40. #if BASISU_OPENCL_ASSERT_ON_ANY_ERRORS
  41. assert(0);
  42. #endif
  43. }
  44. class ocl
  45. {
  46. public:
  47. ocl()
  48. {
  49. memset(&m_dev_fp_config, 0, sizeof(m_dev_fp_config));
  50. m_ocl_mutex.lock();
  51. m_ocl_mutex.unlock();
  52. }
  53. ~ocl()
  54. {
  55. }
  56. bool is_initialized() const { return m_device_id != nullptr; }
  57. cl_device_id get_device_id() const { return m_device_id; }
  58. cl_context get_context() const { return m_context; }
  59. cl_command_queue get_command_queue() { return m_command_queue; }
  60. cl_program get_program() const { return m_program; }
  61. bool init(bool force_serialization)
  62. {
  63. deinit();
  64. interval_timer tm;
  65. tm.start();
  66. cl_uint num_platforms = 0;
  67. cl_int ret = clGetPlatformIDs(0, NULL, &num_platforms);
  68. if (ret != CL_SUCCESS)
  69. {
  70. ocl_error_printf("ocl::init: clGetPlatformIDs() failed with %i\n", ret);
  71. return false;
  72. }
  73. if ((!num_platforms) || (num_platforms > INT_MAX))
  74. {
  75. ocl_error_printf("ocl::init: clGetPlatformIDs() returned an invalid number of num_platforms\n");
  76. return false;
  77. }
  78. std::vector<cl_platform_id> platforms(num_platforms);
  79. ret = clGetPlatformIDs(num_platforms, platforms.data(), NULL);
  80. if (ret != CL_SUCCESS)
  81. {
  82. ocl_error_printf("ocl::init: clGetPlatformIDs() failed\n");
  83. return false;
  84. }
  85. cl_uint num_devices = 0;
  86. ret = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, 1, &m_device_id, &num_devices);
  87. if (ret == CL_DEVICE_NOT_FOUND)
  88. {
  89. ocl_error_printf("ocl::init: Couldn't get any GPU device ID's, trying CL_DEVICE_TYPE_CPU\n");
  90. ret = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_CPU, 1, &m_device_id, &num_devices);
  91. }
  92. if (ret != CL_SUCCESS)
  93. {
  94. ocl_error_printf("ocl::init: Unable to get any device ID's\n");
  95. m_device_id = nullptr;
  96. return false;
  97. }
  98. ret = clGetDeviceInfo(m_device_id,
  99. CL_DEVICE_SINGLE_FP_CONFIG,
  100. sizeof(m_dev_fp_config),
  101. &m_dev_fp_config,
  102. nullptr);
  103. if (ret != CL_SUCCESS)
  104. {
  105. ocl_error_printf("ocl::init: clGetDeviceInfo() failed\n");
  106. return false;
  107. }
  108. char plat_vers[256];
  109. size_t rv = 0;
  110. ret = clGetPlatformInfo(platforms[0], CL_PLATFORM_VERSION, sizeof(plat_vers), plat_vers, &rv);
  111. if (ret == CL_SUCCESS)
  112. printf("OpenCL platform version: \"%s\"\n", plat_vers);
  113. // Serialize CL calls with the AMD driver to avoid lockups when multiple command queues per thread are used. This sucks, but what can we do?
  114. m_use_mutex = (strstr(plat_vers, "AMD") != nullptr) || force_serialization;
  115. printf("Serializing OpenCL calls across threads: %u\n", (uint32_t)m_use_mutex);
  116. m_context = clCreateContext(nullptr, 1, &m_device_id, nullptr, nullptr, &ret);
  117. if (ret != CL_SUCCESS)
  118. {
  119. ocl_error_printf("ocl::init: clCreateContext() failed\n");
  120. m_device_id = nullptr;
  121. m_context = nullptr;
  122. return false;
  123. }
  124. m_command_queue = clCreateCommandQueue(m_context, m_device_id, 0, &ret);
  125. if (ret != CL_SUCCESS)
  126. {
  127. ocl_error_printf("ocl::init: clCreateCommandQueue() failed\n");
  128. deinit();
  129. return false;
  130. }
  131. printf("OpenCL init time: %3.3f secs\n", tm.get_elapsed_secs());
  132. return true;
  133. }
  134. bool deinit()
  135. {
  136. if (m_program)
  137. {
  138. clReleaseProgram(m_program);
  139. m_program = nullptr;
  140. }
  141. if (m_command_queue)
  142. {
  143. clReleaseCommandQueue(m_command_queue);
  144. m_command_queue = nullptr;
  145. }
  146. if (m_context)
  147. {
  148. clReleaseContext(m_context);
  149. m_context = nullptr;
  150. }
  151. m_device_id = nullptr;
  152. return true;
  153. }
  154. cl_command_queue create_command_queue()
  155. {
  156. cl_serializer serializer(this);
  157. cl_int ret = 0;
  158. cl_command_queue p = clCreateCommandQueue(m_context, m_device_id, 0, &ret);
  159. if (ret != CL_SUCCESS)
  160. return nullptr;
  161. return p;
  162. }
  163. void destroy_command_queue(cl_command_queue p)
  164. {
  165. if (p)
  166. {
  167. cl_serializer serializer(this);
  168. clReleaseCommandQueue(p);
  169. }
  170. }
  171. bool init_program(const char* pSrc, size_t src_size)
  172. {
  173. cl_int ret;
  174. if (m_program != nullptr)
  175. {
  176. clReleaseProgram(m_program);
  177. m_program = nullptr;
  178. }
  179. m_program = clCreateProgramWithSource(m_context, 1, (const char**)&pSrc, (const size_t*)&src_size, &ret);
  180. if (ret != CL_SUCCESS)
  181. {
  182. ocl_error_printf("ocl::init_program: clCreateProgramWithSource() failed!\n");
  183. return false;
  184. }
  185. std::string options;
  186. if (m_dev_fp_config & CL_FP_CORRECTLY_ROUNDED_DIVIDE_SQRT)
  187. {
  188. options += "-cl-fp32-correctly-rounded-divide-sqrt";
  189. }
  190. options += " -cl-std=CL1.2";
  191. //options += " -cl-opt-disable";
  192. //options += " -cl-mad-enable";
  193. //options += " -cl-fast-relaxed-math";
  194. ret = clBuildProgram(m_program, 1, &m_device_id,
  195. options.size() ? options.c_str() : nullptr, // options
  196. nullptr, // notify
  197. nullptr); // user_data
  198. if (ret != CL_SUCCESS)
  199. {
  200. const cl_int build_program_result = ret;
  201. size_t ret_val_size;
  202. ret = clGetProgramBuildInfo(m_program, m_device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
  203. if (ret != CL_SUCCESS)
  204. {
  205. ocl_error_printf("ocl::init_program: clGetProgramBuildInfo() failed!\n");
  206. return false;
  207. }
  208. std::vector<char> build_log(ret_val_size + 1);
  209. ret = clGetProgramBuildInfo(m_program, m_device_id, CL_PROGRAM_BUILD_LOG, ret_val_size, build_log.data(), NULL);
  210. ocl_error_printf("\nclBuildProgram() failed with error %i:\n%s", build_program_result, build_log.data());
  211. return false;
  212. }
  213. return true;
  214. }
  215. cl_kernel create_kernel(const char* pName)
  216. {
  217. if (!m_program)
  218. return nullptr;
  219. cl_serializer serializer(this);
  220. cl_int ret;
  221. cl_kernel kernel = clCreateKernel(m_program, pName, &ret);
  222. if (ret != CL_SUCCESS)
  223. {
  224. ocl_error_printf("ocl::create_kernel: clCreateKernel() failed!\n");
  225. return nullptr;
  226. }
  227. return kernel;
  228. }
  229. bool destroy_kernel(cl_kernel k)
  230. {
  231. if (k)
  232. {
  233. cl_serializer serializer(this);
  234. cl_int ret = clReleaseKernel(k);
  235. if (ret != CL_SUCCESS)
  236. {
  237. ocl_error_printf("ocl::destroy_kernel: clReleaseKernel() failed!\n");
  238. return false;
  239. }
  240. }
  241. return true;
  242. }
  243. cl_mem alloc_read_buffer(size_t size)
  244. {
  245. cl_serializer serializer(this);
  246. cl_int ret;
  247. cl_mem obj = clCreateBuffer(m_context, CL_MEM_READ_ONLY, size, NULL, &ret);
  248. if (ret != CL_SUCCESS)
  249. {
  250. ocl_error_printf("ocl::alloc_read_buffer: clCreateBuffer() failed!\n");
  251. return nullptr;
  252. }
  253. return obj;
  254. }
  255. cl_mem alloc_and_init_read_buffer(cl_command_queue command_queue, const void *pInit, size_t size)
  256. {
  257. cl_serializer serializer(this);
  258. cl_int ret;
  259. cl_mem obj = clCreateBuffer(m_context, CL_MEM_READ_ONLY, size, NULL, &ret);
  260. if (ret != CL_SUCCESS)
  261. {
  262. ocl_error_printf("ocl::alloc_and_init_read_buffer: clCreateBuffer() failed!\n");
  263. return nullptr;
  264. }
  265. #if 0
  266. if (!write_to_buffer(command_queue, obj, pInit, size))
  267. {
  268. destroy_buffer(obj);
  269. return nullptr;
  270. }
  271. #else
  272. ret = clEnqueueWriteBuffer(command_queue, obj, CL_TRUE, 0, size, pInit, 0, NULL, NULL);
  273. if (ret != CL_SUCCESS)
  274. {
  275. ocl_error_printf("ocl::alloc_and_init_read_buffer: clEnqueueWriteBuffer() failed!\n");
  276. return nullptr;
  277. }
  278. #endif
  279. return obj;
  280. }
  281. cl_mem alloc_write_buffer(size_t size)
  282. {
  283. cl_serializer serializer(this);
  284. cl_int ret;
  285. cl_mem obj = clCreateBuffer(m_context, CL_MEM_WRITE_ONLY, size, NULL, &ret);
  286. if (ret != CL_SUCCESS)
  287. {
  288. ocl_error_printf("ocl::alloc_write_buffer: clCreateBuffer() failed!\n");
  289. return nullptr;
  290. }
  291. return obj;
  292. }
  293. bool destroy_buffer(cl_mem buf)
  294. {
  295. if (buf)
  296. {
  297. cl_serializer serializer(this);
  298. cl_int ret = clReleaseMemObject(buf);
  299. if (ret != CL_SUCCESS)
  300. {
  301. ocl_error_printf("ocl::destroy_buffer: clReleaseMemObject() failed!\n");
  302. return false;
  303. }
  304. }
  305. return true;
  306. }
  307. bool write_to_buffer(cl_command_queue command_queue, cl_mem clmem, const void* d, const size_t m)
  308. {
  309. cl_serializer serializer(this);
  310. cl_int ret = clEnqueueWriteBuffer(command_queue, clmem, CL_TRUE, 0, m, d, 0, NULL, NULL);
  311. if (ret != CL_SUCCESS)
  312. {
  313. ocl_error_printf("ocl::write_to_buffer: clEnqueueWriteBuffer() failed!\n");
  314. return false;
  315. }
  316. return true;
  317. }
  318. bool read_from_buffer(cl_command_queue command_queue, const cl_mem clmem, void* d, size_t m)
  319. {
  320. cl_serializer serializer(this);
  321. cl_int ret = clEnqueueReadBuffer(command_queue, clmem, CL_TRUE, 0, m, d, 0, NULL, NULL);
  322. if (ret != CL_SUCCESS)
  323. {
  324. ocl_error_printf("ocl::read_from_buffer: clEnqueueReadBuffer() failed!\n");
  325. return false;
  326. }
  327. return true;
  328. }
  329. cl_mem create_read_image_u8(uint32_t width, uint32_t height, const void* pPixels, uint32_t bytes_per_pixel, bool normalized)
  330. {
  331. cl_image_format fmt = get_image_format(bytes_per_pixel, normalized);
  332. cl_image_desc desc;
  333. memset(&desc, 0, sizeof(desc));
  334. desc.image_type = CL_MEM_OBJECT_IMAGE2D;
  335. desc.image_width = width;
  336. desc.image_height = height;
  337. desc.image_row_pitch = width * bytes_per_pixel;
  338. cl_serializer serializer(this);
  339. cl_int ret;
  340. cl_mem img = clCreateImage(m_context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, &fmt, &desc, (void*)pPixels, &ret);
  341. if (ret != CL_SUCCESS)
  342. {
  343. ocl_error_printf("ocl::create_read_image_u8: clCreateImage() failed!\n");
  344. return nullptr;
  345. }
  346. return img;
  347. }
  348. cl_mem create_write_image_u8(uint32_t width, uint32_t height, uint32_t bytes_per_pixel, bool normalized)
  349. {
  350. cl_image_format fmt = get_image_format(bytes_per_pixel, normalized);
  351. cl_image_desc desc;
  352. memset(&desc, 0, sizeof(desc));
  353. desc.image_type = CL_MEM_OBJECT_IMAGE2D;
  354. desc.image_width = width;
  355. desc.image_height = height;
  356. cl_serializer serializer(this);
  357. cl_int ret;
  358. cl_mem img = clCreateImage(m_context, CL_MEM_WRITE_ONLY, &fmt, &desc, nullptr, &ret);
  359. if (ret != CL_SUCCESS)
  360. {
  361. ocl_error_printf("ocl::create_write_image_u8: clCreateImage() failed!\n");
  362. return nullptr;
  363. }
  364. return img;
  365. }
  366. bool read_from_image(cl_command_queue command_queue, cl_mem img, void* pPixels, uint32_t ofs_x, uint32_t ofs_y, uint32_t width, uint32_t height)
  367. {
  368. cl_serializer serializer(this);
  369. size_t origin[3] = { ofs_x, ofs_y, 0 }, region[3] = { width, height, 1 };
  370. cl_int err = clEnqueueReadImage(command_queue, img, CL_TRUE, origin, region, 0, 0, pPixels, 0, NULL, NULL);
  371. if (err != CL_SUCCESS)
  372. {
  373. ocl_error_printf("ocl::read_from_image: clEnqueueReadImage() failed!\n");
  374. return false;
  375. }
  376. return true;
  377. }
  378. bool run_1D(cl_command_queue command_queue, const cl_kernel kernel, size_t num_items)
  379. {
  380. cl_serializer serializer(this);
  381. cl_int ret = clEnqueueNDRangeKernel(command_queue, kernel,
  382. 1, // work_dim
  383. nullptr, // global_work_offset
  384. &num_items, // global_work_size
  385. nullptr, // local_work_size
  386. 0, // num_events_in_wait_list
  387. nullptr, // event_wait_list
  388. nullptr // event
  389. );
  390. if (ret != CL_SUCCESS)
  391. {
  392. ocl_error_printf("ocl::run_1D: clEnqueueNDRangeKernel() failed!\n");
  393. return false;
  394. }
  395. return true;
  396. }
  397. bool run_2D(cl_command_queue command_queue, const cl_kernel kernel, size_t width, size_t height)
  398. {
  399. cl_serializer serializer(this);
  400. size_t num_global_items[2] = { width, height };
  401. //size_t num_local_items[2] = { 1, 1 };
  402. cl_int ret = clEnqueueNDRangeKernel(command_queue, kernel,
  403. 2, // work_dim
  404. nullptr, // global_work_offset
  405. num_global_items, // global_work_size
  406. nullptr, // local_work_size
  407. 0, // num_events_in_wait_list
  408. nullptr, // event_wait_list
  409. nullptr // event
  410. );
  411. if (ret != CL_SUCCESS)
  412. {
  413. ocl_error_printf("ocl::run_2D: clEnqueueNDRangeKernel() failed!\n");
  414. return false;
  415. }
  416. return true;
  417. }
  418. bool run_2D(cl_command_queue command_queue, const cl_kernel kernel, size_t ofs_x, size_t ofs_y, size_t width, size_t height)
  419. {
  420. cl_serializer serializer(this);
  421. size_t global_ofs[2] = { ofs_x, ofs_y };
  422. size_t num_global_items[2] = { width, height };
  423. //size_t num_local_items[2] = { 1, 1 };
  424. cl_int ret = clEnqueueNDRangeKernel(command_queue, kernel,
  425. 2, // work_dim
  426. global_ofs, // global_work_offset
  427. num_global_items, // global_work_size
  428. nullptr, // local_work_size
  429. 0, // num_events_in_wait_list
  430. nullptr, // event_wait_list
  431. nullptr // event
  432. );
  433. if (ret != CL_SUCCESS)
  434. {
  435. ocl_error_printf("ocl::run_2D: clEnqueueNDRangeKernel() failed!\n");
  436. return false;
  437. }
  438. return true;
  439. }
  440. void flush(cl_command_queue command_queue)
  441. {
  442. cl_serializer serializer(this);
  443. clFlush(command_queue);
  444. clFinish(command_queue);
  445. }
  446. template<typename T>
  447. bool set_kernel_arg(cl_kernel kernel, uint32_t index, const T& obj)
  448. {
  449. cl_serializer serializer(this);
  450. cl_int ret = clSetKernelArg(kernel, index, sizeof(T), (void*)&obj);
  451. if (ret != CL_SUCCESS)
  452. {
  453. ocl_error_printf("ocl::set_kernel_arg: clSetKernelArg() failed!\n");
  454. return false;
  455. }
  456. return true;
  457. }
  458. template<typename T>
  459. bool set_kernel_args(cl_kernel kernel, const T& obj1)
  460. {
  461. cl_serializer serializer(this);
  462. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1);
  463. if (ret != CL_SUCCESS)
  464. {
  465. ocl_error_printf("ocl::set_kernel_arg: clSetKernelArg() failed!\n");
  466. return false;
  467. }
  468. return true;
  469. }
  470. #define BASISU_CHECK_ERR if (ret != CL_SUCCESS) { ocl_error_printf("ocl::set_kernel_args: clSetKernelArg() failed!\n"); return false; }
  471. template<typename T, typename U>
  472. bool set_kernel_args(cl_kernel kernel, const T& obj1, const U& obj2)
  473. {
  474. cl_serializer serializer(this);
  475. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1); BASISU_CHECK_ERR
  476. ret = clSetKernelArg(kernel, 1, sizeof(U), (void*)&obj2); BASISU_CHECK_ERR
  477. return true;
  478. }
  479. template<typename T, typename U, typename V>
  480. bool set_kernel_args(cl_kernel kernel, const T& obj1, const U& obj2, const V& obj3)
  481. {
  482. cl_serializer serializer(this);
  483. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1); BASISU_CHECK_ERR
  484. ret = clSetKernelArg(kernel, 1, sizeof(U), (void*)&obj2); BASISU_CHECK_ERR
  485. ret = clSetKernelArg(kernel, 2, sizeof(V), (void*)&obj3); BASISU_CHECK_ERR
  486. return true;
  487. }
  488. template<typename T, typename U, typename V, typename W>
  489. bool set_kernel_args(cl_kernel kernel, const T& obj1, const U& obj2, const V& obj3, const W& obj4)
  490. {
  491. cl_serializer serializer(this);
  492. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1); BASISU_CHECK_ERR
  493. ret = clSetKernelArg(kernel, 1, sizeof(U), (void*)&obj2); BASISU_CHECK_ERR
  494. ret = clSetKernelArg(kernel, 2, sizeof(V), (void*)&obj3); BASISU_CHECK_ERR
  495. ret = clSetKernelArg(kernel, 3, sizeof(W), (void*)&obj4); BASISU_CHECK_ERR
  496. return true;
  497. }
  498. template<typename T, typename U, typename V, typename W, typename X>
  499. bool set_kernel_args(cl_kernel kernel, const T& obj1, const U& obj2, const V& obj3, const W& obj4, const X& obj5)
  500. {
  501. cl_serializer serializer(this);
  502. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1); BASISU_CHECK_ERR
  503. ret = clSetKernelArg(kernel, 1, sizeof(U), (void*)&obj2); BASISU_CHECK_ERR
  504. ret = clSetKernelArg(kernel, 2, sizeof(V), (void*)&obj3); BASISU_CHECK_ERR
  505. ret = clSetKernelArg(kernel, 3, sizeof(W), (void*)&obj4); BASISU_CHECK_ERR
  506. ret = clSetKernelArg(kernel, 4, sizeof(X), (void*)&obj5); BASISU_CHECK_ERR
  507. return true;
  508. }
  509. template<typename T, typename U, typename V, typename W, typename X, typename Y>
  510. bool set_kernel_args(cl_kernel kernel, const T& obj1, const U& obj2, const V& obj3, const W& obj4, const X& obj5, const Y& obj6)
  511. {
  512. cl_serializer serializer(this);
  513. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1); BASISU_CHECK_ERR
  514. ret = clSetKernelArg(kernel, 1, sizeof(U), (void*)&obj2); BASISU_CHECK_ERR
  515. ret = clSetKernelArg(kernel, 2, sizeof(V), (void*)&obj3); BASISU_CHECK_ERR
  516. ret = clSetKernelArg(kernel, 3, sizeof(W), (void*)&obj4); BASISU_CHECK_ERR
  517. ret = clSetKernelArg(kernel, 4, sizeof(X), (void*)&obj5); BASISU_CHECK_ERR
  518. ret = clSetKernelArg(kernel, 5, sizeof(Y), (void*)&obj6); BASISU_CHECK_ERR
  519. return true;
  520. }
  521. template<typename T, typename U, typename V, typename W, typename X, typename Y, typename Z>
  522. bool set_kernel_args(cl_kernel kernel, const T& obj1, const U& obj2, const V& obj3, const W& obj4, const X& obj5, const Y& obj6, const Z& obj7)
  523. {
  524. cl_serializer serializer(this);
  525. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1); BASISU_CHECK_ERR
  526. ret = clSetKernelArg(kernel, 1, sizeof(U), (void*)&obj2); BASISU_CHECK_ERR
  527. ret = clSetKernelArg(kernel, 2, sizeof(V), (void*)&obj3); BASISU_CHECK_ERR
  528. ret = clSetKernelArg(kernel, 3, sizeof(W), (void*)&obj4); BASISU_CHECK_ERR
  529. ret = clSetKernelArg(kernel, 4, sizeof(X), (void*)&obj5); BASISU_CHECK_ERR
  530. ret = clSetKernelArg(kernel, 5, sizeof(Y), (void*)&obj6); BASISU_CHECK_ERR
  531. ret = clSetKernelArg(kernel, 6, sizeof(Z), (void*)&obj7); BASISU_CHECK_ERR
  532. return true;
  533. }
  534. template<typename T, typename U, typename V, typename W, typename X, typename Y, typename Z, typename A>
  535. bool set_kernel_args(cl_kernel kernel, const T& obj1, const U& obj2, const V& obj3, const W& obj4, const X& obj5, const Y& obj6, const Z& obj7, const A& obj8)
  536. {
  537. cl_serializer serializer(this);
  538. cl_int ret = clSetKernelArg(kernel, 0, sizeof(T), (void*)&obj1); BASISU_CHECK_ERR
  539. ret = clSetKernelArg(kernel, 1, sizeof(U), (void*)&obj2); BASISU_CHECK_ERR
  540. ret = clSetKernelArg(kernel, 2, sizeof(V), (void*)&obj3); BASISU_CHECK_ERR
  541. ret = clSetKernelArg(kernel, 3, sizeof(W), (void*)&obj4); BASISU_CHECK_ERR
  542. ret = clSetKernelArg(kernel, 4, sizeof(X), (void*)&obj5); BASISU_CHECK_ERR
  543. ret = clSetKernelArg(kernel, 5, sizeof(Y), (void*)&obj6); BASISU_CHECK_ERR
  544. ret = clSetKernelArg(kernel, 6, sizeof(Z), (void*)&obj7); BASISU_CHECK_ERR
  545. ret = clSetKernelArg(kernel, 7, sizeof(A), (void*)&obj8); BASISU_CHECK_ERR
  546. return true;
  547. }
  548. #undef BASISU_CHECK_ERR
  549. private:
  550. cl_device_id m_device_id = nullptr;
  551. cl_context m_context = nullptr;
  552. cl_command_queue m_command_queue = nullptr;
  553. cl_program m_program = nullptr;
  554. cl_device_fp_config m_dev_fp_config;
  555. bool m_use_mutex = false;
  556. std::mutex m_ocl_mutex;
  557. // This helper object is used to optionally serialize all calls to the CL driver after initialization.
  558. // Currently this is only used to work around race conditions in the Windows AMD driver.
  559. struct cl_serializer
  560. {
  561. inline cl_serializer(const cl_serializer&);
  562. cl_serializer& operator= (const cl_serializer&);
  563. inline cl_serializer(ocl *p) : m_p(p)
  564. {
  565. if (m_p->m_use_mutex)
  566. m_p->m_ocl_mutex.lock();
  567. }
  568. inline ~cl_serializer()
  569. {
  570. if (m_p->m_use_mutex)
  571. m_p->m_ocl_mutex.unlock();
  572. }
  573. private:
  574. ocl* m_p;
  575. };
  576. cl_image_format get_image_format(uint32_t bytes_per_pixel, bool normalized)
  577. {
  578. cl_image_format fmt;
  579. switch (bytes_per_pixel)
  580. {
  581. case 1: fmt.image_channel_order = CL_LUMINANCE; break;
  582. case 2: fmt.image_channel_order = CL_RG; break;
  583. case 3: fmt.image_channel_order = CL_RGB; break;
  584. case 4: fmt.image_channel_order = CL_RGBA; break;
  585. default: assert(0); fmt.image_channel_order = CL_LUMINANCE; break;
  586. }
  587. fmt.image_channel_data_type = normalized ? CL_UNORM_INT8 : CL_UNSIGNED_INT8;
  588. return fmt;
  589. }
  590. };
  591. // Library blobal state
  592. ocl g_ocl;
  593. bool opencl_init(bool force_serialization)
  594. {
  595. if (g_ocl.is_initialized())
  596. {
  597. assert(0);
  598. return false;
  599. }
  600. if (!g_ocl.init(force_serialization))
  601. {
  602. ocl_error_printf("opencl_init: Failed initializing OpenCL\n");
  603. return false;
  604. }
  605. const char* pKernel_src = nullptr;
  606. size_t kernel_src_size = 0;
  607. uint8_vec kernel_src;
  608. #if BASISU_USE_OCL_KERNELS_HEADER
  609. pKernel_src = reinterpret_cast<const char*>(ocl_kernels_cl);
  610. kernel_src_size = ocl_kernels_cl_len;
  611. #else
  612. if (!read_file_to_vec(BASISU_OCL_KERNELS_FILENAME, kernel_src))
  613. {
  614. ocl_error_printf("opencl_init: Cannot read OpenCL kernel source file \"%s\"\n", BASISU_OCL_KERNELS_FILENAME);
  615. g_ocl.deinit();
  616. return false;
  617. }
  618. pKernel_src = (char*)kernel_src.data();
  619. kernel_src_size = kernel_src.size();
  620. #endif
  621. if (!kernel_src_size)
  622. {
  623. ocl_error_printf("opencl_init: Invalid OpenCL kernel source file \"%s\"\n", BASISU_OCL_KERNELS_FILENAME);
  624. g_ocl.deinit();
  625. return false;
  626. }
  627. if (!g_ocl.init_program(pKernel_src, kernel_src_size))
  628. {
  629. ocl_error_printf("opencl_init: Failed compiling OpenCL program\n");
  630. g_ocl.deinit();
  631. return false;
  632. }
  633. printf("OpenCL support initialized successfully\n");
  634. return true;
  635. }
  636. void opencl_deinit()
  637. {
  638. g_ocl.deinit();
  639. }
  640. bool opencl_is_available()
  641. {
  642. return g_ocl.is_initialized();
  643. }
  644. struct opencl_context
  645. {
  646. uint32_t m_ocl_total_pixel_blocks;
  647. cl_mem m_ocl_pixel_blocks;
  648. cl_command_queue m_command_queue;
  649. cl_kernel m_ocl_encode_etc1s_blocks_kernel;
  650. cl_kernel m_ocl_refine_endpoint_clusterization_kernel;
  651. cl_kernel m_ocl_encode_etc1s_from_pixel_cluster_kernel;
  652. cl_kernel m_ocl_find_optimal_selector_clusters_for_each_block_kernel;
  653. cl_kernel m_ocl_determine_selectors_kernel;
  654. };
  655. opencl_context_ptr opencl_create_context()
  656. {
  657. if (!opencl_is_available())
  658. {
  659. ocl_error_printf("opencl_create_context: OpenCL not initialized\n");
  660. assert(0);
  661. return nullptr;
  662. }
  663. interval_timer tm;
  664. tm.start();
  665. opencl_context* pContext = static_cast<opencl_context * >(calloc(sizeof(opencl_context), 1));
  666. if (!pContext)
  667. return nullptr;
  668. // To avoid driver bugs in some drivers - serialize this. Likely not necessary, we don't know.
  669. // https://community.intel.com/t5/OpenCL-for-CPU/Bug-report-clCreateKernelsInProgram-is-not-thread-safe/td-p/1159771
  670. pContext->m_command_queue = g_ocl.create_command_queue();
  671. if (!pContext->m_command_queue)
  672. {
  673. ocl_error_printf("opencl_create_context: Failed creating OpenCL command queue!\n");
  674. opencl_destroy_context(pContext);
  675. return nullptr;
  676. }
  677. pContext->m_ocl_encode_etc1s_blocks_kernel = g_ocl.create_kernel("encode_etc1s_blocks");
  678. if (!pContext->m_ocl_encode_etc1s_blocks_kernel)
  679. {
  680. ocl_error_printf("opencl_create_context: Failed creating OpenCL kernel encode_etc1s_block\n");
  681. opencl_destroy_context(pContext);
  682. return nullptr;
  683. }
  684. pContext->m_ocl_refine_endpoint_clusterization_kernel = g_ocl.create_kernel("refine_endpoint_clusterization");
  685. if (!pContext->m_ocl_refine_endpoint_clusterization_kernel)
  686. {
  687. ocl_error_printf("opencl_create_context: Failed creating OpenCL kernel refine_endpoint_clusterization\n");
  688. opencl_destroy_context(pContext);
  689. return nullptr;
  690. }
  691. pContext->m_ocl_encode_etc1s_from_pixel_cluster_kernel = g_ocl.create_kernel("encode_etc1s_from_pixel_cluster");
  692. if (!pContext->m_ocl_encode_etc1s_from_pixel_cluster_kernel)
  693. {
  694. ocl_error_printf("opencl_create_context: Failed creating OpenCL kernel encode_etc1s_from_pixel_cluster\n");
  695. opencl_destroy_context(pContext);
  696. return nullptr;
  697. }
  698. pContext->m_ocl_find_optimal_selector_clusters_for_each_block_kernel = g_ocl.create_kernel("find_optimal_selector_clusters_for_each_block");
  699. if (!pContext->m_ocl_find_optimal_selector_clusters_for_each_block_kernel)
  700. {
  701. ocl_error_printf("opencl_create_context: Failed creating OpenCL kernel find_optimal_selector_clusters_for_each_block\n");
  702. opencl_destroy_context(pContext);
  703. return nullptr;
  704. }
  705. pContext->m_ocl_determine_selectors_kernel = g_ocl.create_kernel("determine_selectors");
  706. if (!pContext->m_ocl_determine_selectors_kernel)
  707. {
  708. ocl_error_printf("opencl_create_context: Failed creating OpenCL kernel determine_selectors\n");
  709. opencl_destroy_context(pContext);
  710. return nullptr;
  711. }
  712. debug_printf("opencl_create_context: Elapsed time: %f secs\n", tm.get_elapsed_secs());
  713. return pContext;
  714. }
  715. void opencl_destroy_context(opencl_context_ptr pContext)
  716. {
  717. if (!pContext)
  718. return;
  719. interval_timer tm;
  720. tm.start();
  721. g_ocl.destroy_buffer(pContext->m_ocl_pixel_blocks);
  722. g_ocl.destroy_kernel(pContext->m_ocl_determine_selectors_kernel);
  723. g_ocl.destroy_kernel(pContext->m_ocl_find_optimal_selector_clusters_for_each_block_kernel);
  724. g_ocl.destroy_kernel(pContext->m_ocl_encode_etc1s_from_pixel_cluster_kernel);
  725. g_ocl.destroy_kernel(pContext->m_ocl_encode_etc1s_blocks_kernel);
  726. g_ocl.destroy_kernel(pContext->m_ocl_refine_endpoint_clusterization_kernel);
  727. g_ocl.destroy_command_queue(pContext->m_command_queue);
  728. memset(pContext, 0, sizeof(opencl_context));
  729. free(pContext);
  730. debug_printf("opencl_destroy_context: Elapsed time: %f secs\n", tm.get_elapsed_secs());
  731. }
  732. #pragma pack(push, 1)
  733. struct cl_encode_etc1s_param_struct
  734. {
  735. int m_total_blocks;
  736. int m_perceptual;
  737. int m_total_perms;
  738. };
  739. #pragma pack(pop)
  740. bool opencl_set_pixel_blocks(opencl_context_ptr pContext, uint32_t total_blocks, const cl_pixel_block* pPixel_blocks)
  741. {
  742. if (!opencl_is_available())
  743. return false;
  744. if (pContext->m_ocl_pixel_blocks)
  745. {
  746. g_ocl.destroy_buffer(pContext->m_ocl_pixel_blocks);
  747. pContext->m_ocl_pixel_blocks = nullptr;
  748. }
  749. pContext->m_ocl_pixel_blocks = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pPixel_blocks, sizeof(cl_pixel_block) * total_blocks);
  750. if (!pContext->m_ocl_pixel_blocks)
  751. return false;
  752. pContext->m_ocl_total_pixel_blocks = total_blocks;
  753. return true;
  754. }
  755. bool opencl_encode_etc1s_blocks(opencl_context_ptr pContext, etc_block* pOutput_blocks, bool perceptual, uint32_t total_perms)
  756. {
  757. if (!opencl_is_available())
  758. return false;
  759. interval_timer tm;
  760. tm.start();
  761. assert(pContext->m_ocl_pixel_blocks);
  762. if (!pContext->m_ocl_pixel_blocks)
  763. return false;
  764. cl_encode_etc1s_param_struct ps;
  765. ps.m_total_blocks = pContext->m_ocl_total_pixel_blocks;
  766. ps.m_perceptual = perceptual;
  767. ps.m_total_perms = total_perms;
  768. bool status = false;
  769. cl_mem vars = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue , &ps, sizeof(ps));
  770. cl_mem block_buf = g_ocl.alloc_write_buffer(sizeof(etc_block) * pContext->m_ocl_total_pixel_blocks);
  771. if (!vars || !block_buf)
  772. goto exit;
  773. if (!g_ocl.set_kernel_args(pContext->m_ocl_encode_etc1s_blocks_kernel, vars, pContext->m_ocl_pixel_blocks, block_buf))
  774. goto exit;
  775. if (!g_ocl.run_2D(pContext->m_command_queue, pContext->m_ocl_encode_etc1s_blocks_kernel, pContext->m_ocl_total_pixel_blocks, 1))
  776. goto exit;
  777. if (!g_ocl.read_from_buffer(pContext->m_command_queue, block_buf, pOutput_blocks, pContext->m_ocl_total_pixel_blocks * sizeof(etc_block)))
  778. goto exit;
  779. status = true;
  780. debug_printf("opencl_encode_etc1s_blocks: Elapsed time: %3.3f secs\n", tm.get_elapsed_secs());
  781. exit:
  782. g_ocl.destroy_buffer(block_buf);
  783. g_ocl.destroy_buffer(vars);
  784. return status;
  785. }
  786. bool opencl_encode_etc1s_pixel_clusters(
  787. opencl_context_ptr pContext,
  788. etc_block* pOutput_blocks,
  789. uint32_t total_clusters,
  790. const cl_pixel_cluster* pClusters,
  791. uint64_t total_pixels,
  792. const color_rgba* pPixels, const uint32_t* pPixel_weights,
  793. bool perceptual, uint32_t total_perms)
  794. {
  795. if (!opencl_is_available())
  796. return false;
  797. interval_timer tm;
  798. tm.start();
  799. cl_encode_etc1s_param_struct ps;
  800. ps.m_total_blocks = total_clusters;
  801. ps.m_perceptual = perceptual;
  802. ps.m_total_perms = total_perms;
  803. bool status = false;
  804. if (sizeof(size_t) == sizeof(uint32_t))
  805. {
  806. if ( ((sizeof(cl_pixel_cluster) * total_clusters) > UINT32_MAX) ||
  807. ((sizeof(color_rgba) * total_pixels) > UINT32_MAX) ||
  808. ((sizeof(uint32_t) * total_pixels) > UINT32_MAX) )
  809. {
  810. return false;
  811. }
  812. }
  813. cl_mem vars = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue , &ps, sizeof(ps));
  814. cl_mem input_clusters = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pClusters, (size_t)(sizeof(cl_pixel_cluster) * total_clusters));
  815. cl_mem input_pixels = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pPixels, (size_t)(sizeof(color_rgba) * total_pixels));
  816. cl_mem weights_buf = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pPixel_weights, (size_t)(sizeof(uint32_t) * total_pixels));
  817. cl_mem block_buf = g_ocl.alloc_write_buffer(sizeof(etc_block) * total_clusters);
  818. if (!vars || !input_clusters || !input_pixels || !weights_buf || !block_buf)
  819. goto exit;
  820. if (!g_ocl.set_kernel_args(pContext->m_ocl_encode_etc1s_from_pixel_cluster_kernel, vars, input_clusters, input_pixels, weights_buf, block_buf))
  821. goto exit;
  822. if (!g_ocl.run_2D(pContext->m_command_queue, pContext->m_ocl_encode_etc1s_from_pixel_cluster_kernel, total_clusters, 1))
  823. goto exit;
  824. if (!g_ocl.read_from_buffer(pContext->m_command_queue, block_buf, pOutput_blocks, sizeof(etc_block) * total_clusters))
  825. goto exit;
  826. status = true;
  827. debug_printf("opencl_encode_etc1s_pixel_clusters: Elapsed time: %3.3f secs\n", tm.get_elapsed_secs());
  828. exit:
  829. g_ocl.destroy_buffer(block_buf);
  830. g_ocl.destroy_buffer(weights_buf);
  831. g_ocl.destroy_buffer(input_pixels);
  832. g_ocl.destroy_buffer(input_clusters);
  833. g_ocl.destroy_buffer(vars);
  834. return status;
  835. }
  836. #pragma pack(push, 1)
  837. struct cl_rec_param_struct
  838. {
  839. int m_total_blocks;
  840. int m_perceptual;
  841. };
  842. #pragma pack(pop)
  843. bool opencl_refine_endpoint_clusterization(
  844. opencl_context_ptr pContext,
  845. const cl_block_info_struct* pPixel_block_info,
  846. uint32_t total_clusters,
  847. const cl_endpoint_cluster_struct* pCluster_info,
  848. const uint32_t* pSorted_block_indices,
  849. uint32_t* pOutput_cluster_indices,
  850. bool perceptual)
  851. {
  852. if (!opencl_is_available())
  853. return false;
  854. interval_timer tm;
  855. tm.start();
  856. assert(pContext->m_ocl_pixel_blocks);
  857. if (!pContext->m_ocl_pixel_blocks)
  858. return false;
  859. cl_rec_param_struct ps;
  860. ps.m_total_blocks = pContext->m_ocl_total_pixel_blocks;
  861. ps.m_perceptual = perceptual;
  862. bool status = false;
  863. cl_mem pixel_block_info = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pPixel_block_info, sizeof(cl_block_info_struct) * pContext->m_ocl_total_pixel_blocks);
  864. cl_mem cluster_info = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pCluster_info, sizeof(cl_endpoint_cluster_struct) * total_clusters);
  865. cl_mem sorted_block_indices = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pSorted_block_indices, sizeof(uint32_t) * pContext->m_ocl_total_pixel_blocks);
  866. cl_mem output_buf = g_ocl.alloc_write_buffer(sizeof(uint32_t) * pContext->m_ocl_total_pixel_blocks);
  867. if (!pixel_block_info || !cluster_info || !sorted_block_indices || !output_buf)
  868. goto exit;
  869. if (!g_ocl.set_kernel_args(pContext->m_ocl_refine_endpoint_clusterization_kernel, ps, pContext->m_ocl_pixel_blocks, pixel_block_info, cluster_info, sorted_block_indices, output_buf))
  870. goto exit;
  871. if (!g_ocl.run_2D(pContext->m_command_queue, pContext->m_ocl_refine_endpoint_clusterization_kernel, pContext->m_ocl_total_pixel_blocks, 1))
  872. goto exit;
  873. if (!g_ocl.read_from_buffer(pContext->m_command_queue, output_buf, pOutput_cluster_indices, pContext->m_ocl_total_pixel_blocks * sizeof(uint32_t)))
  874. goto exit;
  875. debug_printf("opencl_refine_endpoint_clusterization: Elapsed time: %3.3f secs\n", tm.get_elapsed_secs());
  876. status = true;
  877. exit:
  878. g_ocl.destroy_buffer(pixel_block_info);
  879. g_ocl.destroy_buffer(cluster_info);
  880. g_ocl.destroy_buffer(sorted_block_indices);
  881. g_ocl.destroy_buffer(output_buf);
  882. return status;
  883. }
  884. bool opencl_find_optimal_selector_clusters_for_each_block(
  885. opencl_context_ptr pContext,
  886. const fosc_block_struct* pInput_block_info, // one per block
  887. uint32_t total_input_selectors,
  888. const fosc_selector_struct* pInput_selectors,
  889. const uint32_t* pSelector_cluster_indices,
  890. uint32_t* pOutput_selector_cluster_indices, // one per block
  891. bool perceptual)
  892. {
  893. if (!opencl_is_available())
  894. return false;
  895. interval_timer tm;
  896. tm.start();
  897. assert(pContext->m_ocl_pixel_blocks);
  898. if (!pContext->m_ocl_pixel_blocks)
  899. return false;
  900. fosc_param_struct ps;
  901. ps.m_total_blocks = pContext->m_ocl_total_pixel_blocks;
  902. ps.m_perceptual = perceptual;
  903. bool status = false;
  904. cl_mem input_block_info = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pInput_block_info, sizeof(fosc_block_struct) * pContext->m_ocl_total_pixel_blocks);
  905. cl_mem input_selectors = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pInput_selectors, sizeof(fosc_selector_struct) * total_input_selectors);
  906. cl_mem selector_cluster_indices = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pSelector_cluster_indices, sizeof(uint32_t) * total_input_selectors);
  907. cl_mem output_selector_cluster_indices = g_ocl.alloc_write_buffer(sizeof(uint32_t) * pContext->m_ocl_total_pixel_blocks);
  908. if (!input_block_info || !input_selectors || !selector_cluster_indices || !output_selector_cluster_indices)
  909. goto exit;
  910. if (!g_ocl.set_kernel_args(pContext->m_ocl_find_optimal_selector_clusters_for_each_block_kernel, ps, pContext->m_ocl_pixel_blocks, input_block_info, input_selectors, selector_cluster_indices, output_selector_cluster_indices))
  911. goto exit;
  912. if (!g_ocl.run_2D(pContext->m_command_queue, pContext->m_ocl_find_optimal_selector_clusters_for_each_block_kernel, pContext->m_ocl_total_pixel_blocks, 1))
  913. goto exit;
  914. if (!g_ocl.read_from_buffer(pContext->m_command_queue, output_selector_cluster_indices, pOutput_selector_cluster_indices, pContext->m_ocl_total_pixel_blocks * sizeof(uint32_t)))
  915. goto exit;
  916. debug_printf("opencl_find_optimal_selector_clusters_for_each_block: Elapsed time: %3.3f secs\n", tm.get_elapsed_secs());
  917. status = true;
  918. exit:
  919. g_ocl.destroy_buffer(input_block_info);
  920. g_ocl.destroy_buffer(input_selectors);
  921. g_ocl.destroy_buffer(selector_cluster_indices);
  922. g_ocl.destroy_buffer(output_selector_cluster_indices);
  923. return status;
  924. }
  925. bool opencl_determine_selectors(
  926. opencl_context_ptr pContext,
  927. const color_rgba* pInput_etc_color5_and_inten,
  928. etc_block* pOutput_blocks,
  929. bool perceptual)
  930. {
  931. if (!opencl_is_available())
  932. return false;
  933. interval_timer tm;
  934. tm.start();
  935. assert(pContext->m_ocl_pixel_blocks);
  936. if (!pContext->m_ocl_pixel_blocks)
  937. return false;
  938. ds_param_struct ps;
  939. ps.m_total_blocks = pContext->m_ocl_total_pixel_blocks;
  940. ps.m_perceptual = perceptual;
  941. bool status = false;
  942. cl_mem input_etc_color5_intens = g_ocl.alloc_and_init_read_buffer(pContext->m_command_queue, pInput_etc_color5_and_inten, sizeof(color_rgba) * pContext->m_ocl_total_pixel_blocks);
  943. cl_mem output_blocks = g_ocl.alloc_write_buffer(sizeof(etc_block) * pContext->m_ocl_total_pixel_blocks);
  944. if (!input_etc_color5_intens || !output_blocks)
  945. goto exit;
  946. if (!g_ocl.set_kernel_args(pContext->m_ocl_determine_selectors_kernel, ps, pContext->m_ocl_pixel_blocks, input_etc_color5_intens, output_blocks))
  947. goto exit;
  948. if (!g_ocl.run_2D(pContext->m_command_queue, pContext->m_ocl_determine_selectors_kernel, pContext->m_ocl_total_pixel_blocks, 1))
  949. goto exit;
  950. if (!g_ocl.read_from_buffer(pContext->m_command_queue, output_blocks, pOutput_blocks, pContext->m_ocl_total_pixel_blocks * sizeof(etc_block)))
  951. goto exit;
  952. debug_printf("opencl_determine_selectors: Elapsed time: %3.3f secs\n", tm.get_elapsed_secs());
  953. status = true;
  954. exit:
  955. g_ocl.destroy_buffer(input_etc_color5_intens);
  956. g_ocl.destroy_buffer(output_blocks);
  957. return status;
  958. }
  959. #else
  960. namespace basisu
  961. {
  962. // No OpenCL support - all dummy functions that return false;
  963. bool opencl_init(bool force_serialization)
  964. {
  965. BASISU_NOTE_UNUSED(force_serialization);
  966. return false;
  967. }
  968. void opencl_deinit()
  969. {
  970. }
  971. bool opencl_is_available()
  972. {
  973. return false;
  974. }
  975. opencl_context_ptr opencl_create_context()
  976. {
  977. return nullptr;
  978. }
  979. void opencl_destroy_context(opencl_context_ptr context)
  980. {
  981. BASISU_NOTE_UNUSED(context);
  982. }
  983. bool opencl_set_pixel_blocks(opencl_context_ptr pContext, uint32_t total_blocks, const cl_pixel_block* pPixel_blocks)
  984. {
  985. BASISU_NOTE_UNUSED(pContext);
  986. BASISU_NOTE_UNUSED(total_blocks);
  987. BASISU_NOTE_UNUSED(pPixel_blocks);
  988. return false;
  989. }
  990. bool opencl_encode_etc1s_blocks(opencl_context_ptr pContext, etc_block* pOutput_blocks, bool perceptual, uint32_t total_perms)
  991. {
  992. BASISU_NOTE_UNUSED(pContext);
  993. BASISU_NOTE_UNUSED(pOutput_blocks);
  994. BASISU_NOTE_UNUSED(perceptual);
  995. BASISU_NOTE_UNUSED(total_perms);
  996. return false;
  997. }
  998. bool opencl_encode_etc1s_pixel_clusters(
  999. opencl_context_ptr pContext,
  1000. etc_block* pOutput_blocks,
  1001. uint32_t total_clusters,
  1002. const cl_pixel_cluster* pClusters,
  1003. uint64_t total_pixels,
  1004. const color_rgba* pPixels, const uint32_t *pPixel_weights,
  1005. bool perceptual, uint32_t total_perms)
  1006. {
  1007. BASISU_NOTE_UNUSED(pContext);
  1008. BASISU_NOTE_UNUSED(pOutput_blocks);
  1009. BASISU_NOTE_UNUSED(total_clusters);
  1010. BASISU_NOTE_UNUSED(pClusters);
  1011. BASISU_NOTE_UNUSED(total_pixels);
  1012. BASISU_NOTE_UNUSED(pPixels);
  1013. BASISU_NOTE_UNUSED(pPixel_weights);
  1014. BASISU_NOTE_UNUSED(perceptual);
  1015. BASISU_NOTE_UNUSED(total_perms);
  1016. return false;
  1017. }
  1018. bool opencl_refine_endpoint_clusterization(
  1019. opencl_context_ptr pContext,
  1020. const cl_block_info_struct* pPixel_block_info,
  1021. uint32_t total_clusters,
  1022. const cl_endpoint_cluster_struct* pCluster_info,
  1023. const uint32_t* pSorted_block_indices,
  1024. uint32_t* pOutput_cluster_indices,
  1025. bool perceptual)
  1026. {
  1027. BASISU_NOTE_UNUSED(pContext);
  1028. BASISU_NOTE_UNUSED(pPixel_block_info);
  1029. BASISU_NOTE_UNUSED(total_clusters);
  1030. BASISU_NOTE_UNUSED(pCluster_info);
  1031. BASISU_NOTE_UNUSED(pSorted_block_indices);
  1032. BASISU_NOTE_UNUSED(pOutput_cluster_indices);
  1033. BASISU_NOTE_UNUSED(perceptual);
  1034. return false;
  1035. }
  1036. bool opencl_find_optimal_selector_clusters_for_each_block(
  1037. opencl_context_ptr pContext,
  1038. const fosc_block_struct* pInput_block_info, // one per block
  1039. uint32_t total_input_selectors,
  1040. const fosc_selector_struct* pInput_selectors,
  1041. const uint32_t* pSelector_cluster_indices,
  1042. uint32_t* pOutput_selector_cluster_indices, // one per block
  1043. bool perceptual)
  1044. {
  1045. BASISU_NOTE_UNUSED(pContext);
  1046. BASISU_NOTE_UNUSED(pInput_block_info);
  1047. BASISU_NOTE_UNUSED(total_input_selectors);
  1048. BASISU_NOTE_UNUSED(pInput_selectors);
  1049. BASISU_NOTE_UNUSED(pSelector_cluster_indices);
  1050. BASISU_NOTE_UNUSED(pOutput_selector_cluster_indices);
  1051. BASISU_NOTE_UNUSED(perceptual);
  1052. return false;
  1053. }
  1054. bool opencl_determine_selectors(
  1055. opencl_context_ptr pContext,
  1056. const color_rgba* pInput_etc_color5_and_inten,
  1057. etc_block* pOutput_blocks,
  1058. bool perceptual)
  1059. {
  1060. BASISU_NOTE_UNUSED(pContext);
  1061. BASISU_NOTE_UNUSED(pInput_etc_color5_and_inten);
  1062. BASISU_NOTE_UNUSED(pOutput_blocks);
  1063. BASISU_NOTE_UNUSED(perceptual);
  1064. return false;
  1065. }
  1066. #endif // BASISU_SUPPORT_OPENCL
  1067. } // namespace basisu