util_shaders.cpp 16 KB

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