util_shaders.cpp 16 KB

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