util_shaders.cpp 16 KB

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