util_shaders.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <span>
  5. #include <string_view>
  6. #include <glad/glad.h>
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "common/div_ceil.h"
  10. #include "video_core/host_shaders/astc_decoder_comp.h"
  11. #include "video_core/host_shaders/block_linear_unswizzle_2d_comp.h"
  12. #include "video_core/host_shaders/block_linear_unswizzle_3d_comp.h"
  13. #include "video_core/host_shaders/opengl_copy_bc4_comp.h"
  14. #include "video_core/host_shaders/opengl_copy_bgra_comp.h"
  15. #include "video_core/host_shaders/pitch_unswizzle_comp.h"
  16. #include "video_core/renderer_opengl/gl_shader_manager.h"
  17. #include "video_core/renderer_opengl/gl_shader_util.h"
  18. #include "video_core/renderer_opengl/gl_texture_cache.h"
  19. #include "video_core/renderer_opengl/util_shaders.h"
  20. #include "video_core/texture_cache/accelerated_swizzle.h"
  21. #include "video_core/texture_cache/types.h"
  22. #include "video_core/texture_cache/util.h"
  23. #include "video_core/textures/astc.h"
  24. #include "video_core/textures/decoders.h"
  25. namespace OpenGL {
  26. using namespace HostShaders;
  27. using namespace Tegra::Texture::ASTC;
  28. using VideoCommon::Extent2D;
  29. using VideoCommon::Extent3D;
  30. using VideoCommon::ImageCopy;
  31. using VideoCommon::ImageType;
  32. using VideoCommon::SwizzleParameters;
  33. using VideoCommon::Accelerated::MakeBlockLinearSwizzle2DParams;
  34. using VideoCommon::Accelerated::MakeBlockLinearSwizzle3DParams;
  35. using VideoCore::Surface::BytesPerBlock;
  36. namespace {
  37. OGLProgram MakeProgram(std::string_view source) {
  38. return CreateProgram(source, GL_COMPUTE_SHADER);
  39. }
  40. size_t NumPixelsInCopy(const VideoCommon::ImageCopy& copy) {
  41. return static_cast<size_t>(copy.extent.width * copy.extent.height *
  42. copy.src_subresource.num_layers);
  43. }
  44. } // Anonymous namespace
  45. UtilShaders::UtilShaders(ProgramManager& program_manager_)
  46. : program_manager{program_manager_}, astc_decoder_program(MakeProgram(ASTC_DECODER_COMP)),
  47. block_linear_unswizzle_2d_program(MakeProgram(BLOCK_LINEAR_UNSWIZZLE_2D_COMP)),
  48. block_linear_unswizzle_3d_program(MakeProgram(BLOCK_LINEAR_UNSWIZZLE_3D_COMP)),
  49. pitch_unswizzle_program(MakeProgram(PITCH_UNSWIZZLE_COMP)),
  50. copy_bgra_program(MakeProgram(OPENGL_COPY_BGRA_COMP)),
  51. copy_bc4_program(MakeProgram(OPENGL_COPY_BC4_COMP)) {
  52. const auto swizzle_table = Tegra::Texture::MakeSwizzleTable();
  53. swizzle_table_buffer.Create();
  54. glNamedBufferStorage(swizzle_table_buffer.handle, sizeof(swizzle_table), &swizzle_table, 0);
  55. }
  56. UtilShaders::~UtilShaders() = default;
  57. void UtilShaders::ASTCDecode(Image& image, const ImageBufferMap& map,
  58. std::span<const VideoCommon::SwizzleParameters> swizzles) {
  59. static constexpr GLuint BINDING_INPUT_BUFFER = 0;
  60. static constexpr GLuint BINDING_OUTPUT_IMAGE = 0;
  61. const Extent2D tile_size{
  62. .width = VideoCore::Surface::DefaultBlockWidth(image.info.format),
  63. .height = VideoCore::Surface::DefaultBlockHeight(image.info.format),
  64. };
  65. program_manager.BindComputeProgram(astc_decoder_program.handle);
  66. glFlushMappedNamedBufferRange(map.buffer, map.offset, image.guest_size_bytes);
  67. glUniform2ui(1, tile_size.width, tile_size.height);
  68. // Ensure buffer data is valid before dispatching
  69. glFlush();
  70. for (const SwizzleParameters& swizzle : swizzles) {
  71. const size_t input_offset = swizzle.buffer_offset + map.offset;
  72. const u32 num_dispatches_x = Common::DivCeil(swizzle.num_tiles.width, 32U);
  73. const u32 num_dispatches_y = Common::DivCeil(swizzle.num_tiles.height, 32U);
  74. const auto params = MakeBlockLinearSwizzle2DParams(swizzle, image.info);
  75. ASSERT(params.origin == (std::array<u32, 3>{0, 0, 0}));
  76. ASSERT(params.destination == (std::array<s32, 3>{0, 0, 0}));
  77. ASSERT(params.bytes_per_block_log2 == 4);
  78. glUniform1ui(2, params.layer_stride);
  79. glUniform1ui(3, params.block_size);
  80. glUniform1ui(4, params.x_shift);
  81. glUniform1ui(5, params.block_height);
  82. glUniform1ui(6, params.block_height_mask);
  83. // ASTC texture data
  84. glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_BUFFER, map.buffer, input_offset,
  85. image.guest_size_bytes - swizzle.buffer_offset);
  86. glBindImageTexture(BINDING_OUTPUT_IMAGE, image.StorageHandle(), swizzle.level, GL_TRUE, 0,
  87. GL_WRITE_ONLY, GL_RGBA8);
  88. glDispatchCompute(num_dispatches_x, num_dispatches_y, image.info.resources.layers);
  89. }
  90. // Precautionary barrier to ensure the compute shader is done decoding prior to texture access.
  91. // GL_TEXTURE_FETCH_BARRIER_BIT and GL_SHADER_IMAGE_ACCESS_BARRIER_BIT are used in a separate
  92. // glMemoryBarrier call by the texture cache runtime
  93. glMemoryBarrier(GL_UNIFORM_BARRIER_BIT | GL_COMMAND_BARRIER_BIT | GL_PIXEL_BUFFER_BARRIER_BIT |
  94. GL_TEXTURE_UPDATE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT |
  95. GL_SHADER_STORAGE_BARRIER_BIT | GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
  96. program_manager.RestoreGuestCompute();
  97. }
  98. void UtilShaders::BlockLinearUpload2D(Image& image, const ImageBufferMap& map,
  99. std::span<const SwizzleParameters> swizzles) {
  100. static constexpr Extent3D WORKGROUP_SIZE{32, 32, 1};
  101. static constexpr GLuint BINDING_SWIZZLE_BUFFER = 0;
  102. static constexpr GLuint BINDING_INPUT_BUFFER = 1;
  103. static constexpr GLuint BINDING_OUTPUT_IMAGE = 0;
  104. program_manager.BindComputeProgram(block_linear_unswizzle_2d_program.handle);
  105. glFlushMappedNamedBufferRange(map.buffer, map.offset, image.guest_size_bytes);
  106. glBindBufferBase(GL_SHADER_STORAGE_BUFFER, BINDING_SWIZZLE_BUFFER, swizzle_table_buffer.handle);
  107. const GLenum store_format = StoreFormat(BytesPerBlock(image.info.format));
  108. for (const SwizzleParameters& swizzle : swizzles) {
  109. const Extent3D num_tiles = swizzle.num_tiles;
  110. const size_t input_offset = swizzle.buffer_offset + map.offset;
  111. const u32 num_dispatches_x = Common::DivCeil(num_tiles.width, WORKGROUP_SIZE.width);
  112. const u32 num_dispatches_y = Common::DivCeil(num_tiles.height, WORKGROUP_SIZE.height);
  113. const auto params = MakeBlockLinearSwizzle2DParams(swizzle, image.info);
  114. glUniform3uiv(0, 1, params.origin.data());
  115. glUniform3iv(1, 1, params.destination.data());
  116. glUniform1ui(2, params.bytes_per_block_log2);
  117. glUniform1ui(3, params.layer_stride);
  118. glUniform1ui(4, params.block_size);
  119. glUniform1ui(5, params.x_shift);
  120. glUniform1ui(6, params.block_height);
  121. glUniform1ui(7, params.block_height_mask);
  122. glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_BUFFER, map.buffer, input_offset,
  123. image.guest_size_bytes - swizzle.buffer_offset);
  124. glBindImageTexture(BINDING_OUTPUT_IMAGE, image.StorageHandle(), swizzle.level, GL_TRUE, 0,
  125. GL_WRITE_ONLY, store_format);
  126. glDispatchCompute(num_dispatches_x, num_dispatches_y, image.info.resources.layers);
  127. }
  128. program_manager.RestoreGuestCompute();
  129. }
  130. void UtilShaders::BlockLinearUpload3D(Image& image, const ImageBufferMap& map,
  131. std::span<const SwizzleParameters> swizzles) {
  132. static constexpr Extent3D WORKGROUP_SIZE{16, 8, 8};
  133. static constexpr GLuint BINDING_SWIZZLE_BUFFER = 0;
  134. static constexpr GLuint BINDING_INPUT_BUFFER = 1;
  135. static constexpr GLuint BINDING_OUTPUT_IMAGE = 0;
  136. glFlushMappedNamedBufferRange(map.buffer, map.offset, image.guest_size_bytes);
  137. program_manager.BindComputeProgram(block_linear_unswizzle_3d_program.handle);
  138. glBindBufferBase(GL_SHADER_STORAGE_BUFFER, BINDING_SWIZZLE_BUFFER, swizzle_table_buffer.handle);
  139. const GLenum store_format = StoreFormat(BytesPerBlock(image.info.format));
  140. for (const SwizzleParameters& swizzle : swizzles) {
  141. const Extent3D num_tiles = swizzle.num_tiles;
  142. const size_t input_offset = swizzle.buffer_offset + map.offset;
  143. const u32 num_dispatches_x = Common::DivCeil(num_tiles.width, WORKGROUP_SIZE.width);
  144. const u32 num_dispatches_y = Common::DivCeil(num_tiles.height, WORKGROUP_SIZE.height);
  145. const u32 num_dispatches_z = Common::DivCeil(num_tiles.depth, WORKGROUP_SIZE.depth);
  146. const auto params = MakeBlockLinearSwizzle3DParams(swizzle, image.info);
  147. glUniform3uiv(0, 1, params.origin.data());
  148. glUniform3iv(1, 1, params.destination.data());
  149. glUniform1ui(2, params.bytes_per_block_log2);
  150. glUniform1ui(3, params.slice_size);
  151. glUniform1ui(4, params.block_size);
  152. glUniform1ui(5, params.x_shift);
  153. glUniform1ui(6, params.block_height);
  154. glUniform1ui(7, params.block_height_mask);
  155. glUniform1ui(8, params.block_depth);
  156. glUniform1ui(9, params.block_depth_mask);
  157. glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_BUFFER, map.buffer, input_offset,
  158. image.guest_size_bytes - swizzle.buffer_offset);
  159. glBindImageTexture(BINDING_OUTPUT_IMAGE, image.StorageHandle(), swizzle.level, GL_TRUE, 0,
  160. GL_WRITE_ONLY, store_format);
  161. glDispatchCompute(num_dispatches_x, num_dispatches_y, num_dispatches_z);
  162. }
  163. program_manager.RestoreGuestCompute();
  164. }
  165. void UtilShaders::PitchUpload(Image& image, const ImageBufferMap& map,
  166. std::span<const SwizzleParameters> swizzles) {
  167. static constexpr Extent3D WORKGROUP_SIZE{32, 32, 1};
  168. static constexpr GLuint BINDING_INPUT_BUFFER = 0;
  169. static constexpr GLuint BINDING_OUTPUT_IMAGE = 0;
  170. static constexpr GLuint LOC_ORIGIN = 0;
  171. static constexpr GLuint LOC_DESTINATION = 1;
  172. static constexpr GLuint LOC_BYTES_PER_BLOCK = 2;
  173. static constexpr GLuint LOC_PITCH = 3;
  174. const u32 bytes_per_block = BytesPerBlock(image.info.format);
  175. const GLenum format = StoreFormat(bytes_per_block);
  176. const u32 pitch = image.info.pitch;
  177. UNIMPLEMENTED_IF_MSG(!std::has_single_bit(bytes_per_block),
  178. "Non-power of two images are not implemented");
  179. program_manager.BindComputeProgram(pitch_unswizzle_program.handle);
  180. glFlushMappedNamedBufferRange(map.buffer, map.offset, image.guest_size_bytes);
  181. glUniform2ui(LOC_ORIGIN, 0, 0);
  182. glUniform2i(LOC_DESTINATION, 0, 0);
  183. glUniform1ui(LOC_BYTES_PER_BLOCK, bytes_per_block);
  184. glUniform1ui(LOC_PITCH, pitch);
  185. glBindImageTexture(BINDING_OUTPUT_IMAGE, image.StorageHandle(), 0, GL_FALSE, 0, GL_WRITE_ONLY,
  186. format);
  187. for (const SwizzleParameters& swizzle : swizzles) {
  188. const Extent3D num_tiles = swizzle.num_tiles;
  189. const size_t input_offset = swizzle.buffer_offset + map.offset;
  190. const u32 num_dispatches_x = Common::DivCeil(num_tiles.width, WORKGROUP_SIZE.width);
  191. const u32 num_dispatches_y = Common::DivCeil(num_tiles.height, WORKGROUP_SIZE.height);
  192. glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_BUFFER, map.buffer, input_offset,
  193. image.guest_size_bytes - swizzle.buffer_offset);
  194. glDispatchCompute(num_dispatches_x, num_dispatches_y, 1);
  195. }
  196. program_manager.RestoreGuestCompute();
  197. }
  198. void UtilShaders::CopyBC4(Image& dst_image, Image& src_image, std::span<const ImageCopy> copies) {
  199. static constexpr GLuint BINDING_INPUT_IMAGE = 0;
  200. static constexpr GLuint BINDING_OUTPUT_IMAGE = 1;
  201. static constexpr GLuint LOC_SRC_OFFSET = 0;
  202. static constexpr GLuint LOC_DST_OFFSET = 1;
  203. program_manager.BindComputeProgram(copy_bc4_program.handle);
  204. for (const ImageCopy& copy : copies) {
  205. ASSERT(copy.src_subresource.base_layer == 0);
  206. ASSERT(copy.src_subresource.num_layers == 1);
  207. ASSERT(copy.dst_subresource.base_layer == 0);
  208. ASSERT(copy.dst_subresource.num_layers == 1);
  209. glUniform3ui(LOC_SRC_OFFSET, copy.src_offset.x, copy.src_offset.y, copy.src_offset.z);
  210. glUniform3ui(LOC_DST_OFFSET, copy.dst_offset.x, copy.dst_offset.y, copy.dst_offset.z);
  211. glBindImageTexture(BINDING_INPUT_IMAGE, src_image.StorageHandle(),
  212. copy.src_subresource.base_level, GL_TRUE, 0, GL_READ_ONLY, GL_RG32UI);
  213. glBindImageTexture(BINDING_OUTPUT_IMAGE, dst_image.StorageHandle(),
  214. copy.dst_subresource.base_level, GL_TRUE, 0, GL_WRITE_ONLY, GL_RGBA8UI);
  215. glDispatchCompute(copy.extent.width, copy.extent.height, copy.extent.depth);
  216. }
  217. program_manager.RestoreGuestCompute();
  218. }
  219. void UtilShaders::CopyBGR(Image& dst_image, Image& src_image,
  220. std::span<const VideoCommon::ImageCopy> copies) {
  221. static constexpr GLuint BINDING_INPUT_IMAGE = 0;
  222. static constexpr GLuint BINDING_OUTPUT_IMAGE = 1;
  223. static constexpr VideoCommon::Offset3D zero_offset{0, 0, 0};
  224. const u32 bytes_per_block = BytesPerBlock(dst_image.info.format);
  225. switch (bytes_per_block) {
  226. case 2:
  227. // BGR565 copy
  228. for (const ImageCopy& copy : copies) {
  229. ASSERT(copy.src_offset == zero_offset);
  230. ASSERT(copy.dst_offset == zero_offset);
  231. bgr_copy_pass.Execute(dst_image, src_image, copy);
  232. }
  233. break;
  234. case 4: {
  235. // BGRA8 copy
  236. program_manager.BindComputeProgram(copy_bgra_program.handle);
  237. constexpr GLenum FORMAT = GL_RGBA8;
  238. for (const ImageCopy& copy : copies) {
  239. ASSERT(copy.src_offset == zero_offset);
  240. ASSERT(copy.dst_offset == zero_offset);
  241. glBindImageTexture(BINDING_INPUT_IMAGE, src_image.StorageHandle(),
  242. copy.src_subresource.base_level, GL_FALSE, 0, GL_READ_ONLY, FORMAT);
  243. glBindImageTexture(BINDING_OUTPUT_IMAGE, dst_image.StorageHandle(),
  244. copy.dst_subresource.base_level, GL_FALSE, 0, GL_WRITE_ONLY, FORMAT);
  245. glDispatchCompute(copy.extent.width, copy.extent.height, copy.extent.depth);
  246. }
  247. program_manager.RestoreGuestCompute();
  248. break;
  249. }
  250. default:
  251. UNREACHABLE();
  252. break;
  253. }
  254. }
  255. GLenum StoreFormat(u32 bytes_per_block) {
  256. switch (bytes_per_block) {
  257. case 1:
  258. return GL_R8UI;
  259. case 2:
  260. return GL_R16UI;
  261. case 4:
  262. return GL_R32UI;
  263. case 8:
  264. return GL_RG32UI;
  265. case 16:
  266. return GL_RGBA32UI;
  267. }
  268. UNREACHABLE();
  269. return GL_R8UI;
  270. }
  271. void Bgr565CopyPass::Execute(const Image& dst_image, const Image& src_image,
  272. const ImageCopy& copy) {
  273. if (CopyBufferCreationNeeded(copy)) {
  274. CreateNewCopyBuffer(copy, GL_TEXTURE_2D_ARRAY, GL_RGB565);
  275. }
  276. // Copy from source to PBO
  277. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  278. glPixelStorei(GL_PACK_ROW_LENGTH, copy.extent.width);
  279. glBindBuffer(GL_PIXEL_PACK_BUFFER, bgr16_pbo.handle);
  280. glGetTextureSubImage(src_image.Handle(), 0, 0, 0, 0, copy.extent.width, copy.extent.height,
  281. copy.src_subresource.num_layers, GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
  282. static_cast<GLsizei>(bgr16_pbo_size), nullptr);
  283. // Copy from PBO to destination in reverse order
  284. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  285. glPixelStorei(GL_UNPACK_ROW_LENGTH, copy.extent.width);
  286. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, bgr16_pbo.handle);
  287. glTextureSubImage3D(dst_image.Handle(), 0, 0, 0, 0, copy.extent.width, copy.extent.height,
  288. copy.dst_subresource.num_layers, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV,
  289. nullptr);
  290. }
  291. bool Bgr565CopyPass::CopyBufferCreationNeeded(const ImageCopy& copy) {
  292. return bgr16_pbo_size < NumPixelsInCopy(copy) * sizeof(u16);
  293. }
  294. void Bgr565CopyPass::CreateNewCopyBuffer(const ImageCopy& copy, GLenum target, GLuint format) {
  295. bgr16_pbo.Create();
  296. bgr16_pbo_size = NumPixelsInCopy(copy) * sizeof(u16);
  297. glNamedBufferData(bgr16_pbo.handle, bgr16_pbo_size, nullptr, GL_STREAM_COPY);
  298. }
  299. } // namespace OpenGL