basisu_opencl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // basisu_opencl.h
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  3. //
  4. // Note: Undefine or set BASISU_SUPPORT_OPENCL to 0 to completely OpenCL support.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #pragma once
  18. #include "../transcoder/basisu.h"
  19. #include "basisu_enc.h"
  20. #include "basisu_etc.h"
  21. namespace basisu
  22. {
  23. bool opencl_init(bool force_serialization);
  24. void opencl_deinit();
  25. bool opencl_is_available();
  26. struct opencl_context;
  27. // Each thread calling OpenCL should have its own opencl_context_ptr. This corresponds to a OpenCL command queue. (Confusingly, we only use a single OpenCL device "context".)
  28. typedef opencl_context* opencl_context_ptr;
  29. opencl_context_ptr opencl_create_context();
  30. void opencl_destroy_context(opencl_context_ptr context);
  31. #pragma pack(push, 1)
  32. struct cl_pixel_block
  33. {
  34. color_rgba m_pixels[16]; // [y*4+x]
  35. };
  36. #pragma pack(pop)
  37. // Must match BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE
  38. const uint32_t OPENCL_ENCODE_ETC1S_MAX_PERMS = 165;
  39. bool opencl_set_pixel_blocks(opencl_context_ptr pContext, uint32_t total_blocks, const cl_pixel_block* pPixel_blocks);
  40. bool opencl_encode_etc1s_blocks(opencl_context_ptr pContext, etc_block* pOutput_blocks, bool perceptual, uint32_t total_perms);
  41. // opencl_encode_etc1s_pixel_clusters
  42. #pragma pack(push, 1)
  43. struct cl_pixel_cluster
  44. {
  45. uint64_t m_total_pixels;
  46. uint64_t m_first_pixel_index;
  47. };
  48. #pragma pack(pop)
  49. bool opencl_encode_etc1s_pixel_clusters(
  50. opencl_context_ptr pContext,
  51. etc_block* pOutput_blocks,
  52. uint32_t total_clusters,
  53. const cl_pixel_cluster *pClusters,
  54. uint64_t total_pixels,
  55. const color_rgba *pPixels,
  56. const uint32_t *pPixel_weights,
  57. bool perceptual, uint32_t total_perms);
  58. // opencl_refine_endpoint_clusterization
  59. #pragma pack(push, 1)
  60. struct cl_block_info_struct
  61. {
  62. uint16_t m_first_cluster_ofs;
  63. uint16_t m_num_clusters;
  64. uint16_t m_cur_cluster_index;
  65. uint8_t m_cur_cluster_etc_inten;
  66. };
  67. struct cl_endpoint_cluster_struct
  68. {
  69. color_rgba m_unscaled_color;
  70. uint8_t m_etc_inten;
  71. uint16_t m_cluster_index;
  72. };
  73. #pragma pack(pop)
  74. bool opencl_refine_endpoint_clusterization(
  75. opencl_context_ptr pContext,
  76. const cl_block_info_struct *pPixel_block_info,
  77. uint32_t total_clusters,
  78. const cl_endpoint_cluster_struct *pCluster_info,
  79. const uint32_t *pSorted_block_indices,
  80. uint32_t* pOutput_cluster_indices,
  81. bool perceptual);
  82. // opencl_find_optimal_selector_clusters_for_each_block
  83. #pragma pack(push, 1)
  84. struct fosc_selector_struct
  85. {
  86. uint32_t m_packed_selectors; // 4x4 grid of 2-bit selectors
  87. };
  88. struct fosc_block_struct
  89. {
  90. color_rgba m_etc_color5_inten; // unscaled 5-bit block color in RGB, alpha has block's intensity index
  91. uint32_t m_first_selector; // offset into selector table
  92. uint32_t m_num_selectors; // number of selectors to check
  93. };
  94. struct fosc_param_struct
  95. {
  96. uint32_t m_total_blocks;
  97. int m_perceptual;
  98. };
  99. #pragma pack(pop)
  100. bool opencl_find_optimal_selector_clusters_for_each_block(
  101. opencl_context_ptr pContext,
  102. const fosc_block_struct* pInput_block_info, // one per block
  103. uint32_t total_input_selectors,
  104. const fosc_selector_struct* pInput_selectors,
  105. const uint32_t* pSelector_cluster_indices,
  106. uint32_t* pOutput_selector_cluster_indices, // one per block
  107. bool perceptual);
  108. #pragma pack(push, 1)
  109. struct ds_param_struct
  110. {
  111. uint32_t m_total_blocks;
  112. int m_perceptual;
  113. };
  114. #pragma pack(pop)
  115. bool opencl_determine_selectors(
  116. opencl_context_ptr pContext,
  117. const color_rgba* pInput_etc_color5_and_inten,
  118. etc_block* pOutput_blocks,
  119. bool perceptual);
  120. } // namespace basisu