util_shaders.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <bit>
  5. #include <span>
  6. #include <string_view>
  7. #include <glad/glad.h>
  8. #include "common/assert.h"
  9. #include "common/common_types.h"
  10. #include "common/div_ceil.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/pitch_unswizzle_comp.h"
  15. #include "video_core/renderer_opengl/gl_resource_manager.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/surface.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/decoders.h"
  24. namespace OpenGL {
  25. using namespace HostShaders;
  26. using VideoCommon::Extent3D;
  27. using VideoCommon::ImageCopy;
  28. using VideoCommon::ImageType;
  29. using VideoCommon::SwizzleParameters;
  30. using VideoCommon::Accelerated::MakeBlockLinearSwizzle2DParams;
  31. using VideoCommon::Accelerated::MakeBlockLinearSwizzle3DParams;
  32. using VideoCore::Surface::BytesPerBlock;
  33. namespace {
  34. OGLProgram MakeProgram(std::string_view source) {
  35. OGLShader shader;
  36. shader.Create(source, GL_COMPUTE_SHADER);
  37. OGLProgram program;
  38. program.Create(true, false, shader.handle);
  39. return program;
  40. }
  41. } // Anonymous namespace
  42. UtilShaders::UtilShaders(ProgramManager& program_manager_)
  43. : program_manager{program_manager_},
  44. block_linear_unswizzle_2d_program(MakeProgram(BLOCK_LINEAR_UNSWIZZLE_2D_COMP)),
  45. block_linear_unswizzle_3d_program(MakeProgram(BLOCK_LINEAR_UNSWIZZLE_3D_COMP)),
  46. pitch_unswizzle_program(MakeProgram(PITCH_UNSWIZZLE_COMP)),
  47. copy_bc4_program(MakeProgram(OPENGL_COPY_BC4_COMP)) {
  48. const auto swizzle_table = Tegra::Texture::MakeSwizzleTable();
  49. swizzle_table_buffer.Create();
  50. glNamedBufferStorage(swizzle_table_buffer.handle, sizeof(swizzle_table), &swizzle_table, 0);
  51. }
  52. UtilShaders::~UtilShaders() = default;
  53. void UtilShaders::BlockLinearUpload2D(Image& image, const ImageBufferMap& map,
  54. std::span<const SwizzleParameters> swizzles) {
  55. static constexpr Extent3D WORKGROUP_SIZE{32, 32, 1};
  56. static constexpr GLuint BINDING_SWIZZLE_BUFFER = 0;
  57. static constexpr GLuint BINDING_INPUT_BUFFER = 1;
  58. static constexpr GLuint BINDING_OUTPUT_IMAGE = 0;
  59. program_manager.BindHostCompute(block_linear_unswizzle_2d_program.handle);
  60. glFlushMappedNamedBufferRange(map.buffer, map.offset, image.guest_size_bytes);
  61. glBindBufferBase(GL_SHADER_STORAGE_BUFFER, BINDING_SWIZZLE_BUFFER, swizzle_table_buffer.handle);
  62. const GLenum store_format = StoreFormat(BytesPerBlock(image.info.format));
  63. for (const SwizzleParameters& swizzle : swizzles) {
  64. const Extent3D num_tiles = swizzle.num_tiles;
  65. const size_t input_offset = swizzle.buffer_offset + map.offset;
  66. const u32 num_dispatches_x = Common::DivCeil(num_tiles.width, WORKGROUP_SIZE.width);
  67. const u32 num_dispatches_y = Common::DivCeil(num_tiles.height, WORKGROUP_SIZE.height);
  68. const auto params = MakeBlockLinearSwizzle2DParams(swizzle, image.info);
  69. glUniform3uiv(0, 1, params.origin.data());
  70. glUniform3iv(1, 1, params.destination.data());
  71. glUniform1ui(2, params.bytes_per_block_log2);
  72. glUniform1ui(3, params.layer_stride);
  73. glUniform1ui(4, params.block_size);
  74. glUniform1ui(5, params.x_shift);
  75. glUniform1ui(6, params.block_height);
  76. glUniform1ui(7, params.block_height_mask);
  77. glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_BUFFER, map.buffer, input_offset,
  78. image.guest_size_bytes - swizzle.buffer_offset);
  79. glBindImageTexture(BINDING_OUTPUT_IMAGE, image.StorageHandle(), swizzle.level, GL_TRUE, 0,
  80. GL_WRITE_ONLY, store_format);
  81. glDispatchCompute(num_dispatches_x, num_dispatches_y, image.info.resources.layers);
  82. }
  83. program_manager.RestoreGuestCompute();
  84. }
  85. void UtilShaders::BlockLinearUpload3D(Image& image, const ImageBufferMap& map,
  86. std::span<const SwizzleParameters> swizzles) {
  87. static constexpr Extent3D WORKGROUP_SIZE{16, 8, 8};
  88. static constexpr GLuint BINDING_SWIZZLE_BUFFER = 0;
  89. static constexpr GLuint BINDING_INPUT_BUFFER = 1;
  90. static constexpr GLuint BINDING_OUTPUT_IMAGE = 0;
  91. glFlushMappedNamedBufferRange(map.buffer, map.offset, image.guest_size_bytes);
  92. program_manager.BindHostCompute(block_linear_unswizzle_3d_program.handle);
  93. glBindBufferBase(GL_SHADER_STORAGE_BUFFER, BINDING_SWIZZLE_BUFFER, swizzle_table_buffer.handle);
  94. const GLenum store_format = StoreFormat(BytesPerBlock(image.info.format));
  95. for (const SwizzleParameters& swizzle : swizzles) {
  96. const Extent3D num_tiles = swizzle.num_tiles;
  97. const size_t input_offset = swizzle.buffer_offset + map.offset;
  98. const u32 num_dispatches_x = Common::DivCeil(num_tiles.width, WORKGROUP_SIZE.width);
  99. const u32 num_dispatches_y = Common::DivCeil(num_tiles.height, WORKGROUP_SIZE.height);
  100. const u32 num_dispatches_z = Common::DivCeil(num_tiles.depth, WORKGROUP_SIZE.depth);
  101. const auto params = MakeBlockLinearSwizzle3DParams(swizzle, image.info);
  102. glUniform3uiv(0, 1, params.origin.data());
  103. glUniform3iv(1, 1, params.destination.data());
  104. glUniform1ui(2, params.bytes_per_block_log2);
  105. glUniform1ui(3, params.slice_size);
  106. glUniform1ui(4, params.block_size);
  107. glUniform1ui(5, params.x_shift);
  108. glUniform1ui(6, params.block_height);
  109. glUniform1ui(7, params.block_height_mask);
  110. glUniform1ui(8, params.block_depth);
  111. glUniform1ui(9, params.block_depth_mask);
  112. glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_BUFFER, map.buffer, input_offset,
  113. image.guest_size_bytes - swizzle.buffer_offset);
  114. glBindImageTexture(BINDING_OUTPUT_IMAGE, image.StorageHandle(), swizzle.level, GL_TRUE, 0,
  115. GL_WRITE_ONLY, store_format);
  116. glDispatchCompute(num_dispatches_x, num_dispatches_y, num_dispatches_z);
  117. }
  118. program_manager.RestoreGuestCompute();
  119. }
  120. void UtilShaders::PitchUpload(Image& image, const ImageBufferMap& map,
  121. std::span<const SwizzleParameters> swizzles) {
  122. static constexpr Extent3D WORKGROUP_SIZE{32, 32, 1};
  123. static constexpr GLuint BINDING_INPUT_BUFFER = 0;
  124. static constexpr GLuint BINDING_OUTPUT_IMAGE = 0;
  125. static constexpr GLuint LOC_ORIGIN = 0;
  126. static constexpr GLuint LOC_DESTINATION = 1;
  127. static constexpr GLuint LOC_BYTES_PER_BLOCK = 2;
  128. static constexpr GLuint LOC_PITCH = 3;
  129. const u32 bytes_per_block = BytesPerBlock(image.info.format);
  130. const GLenum format = StoreFormat(bytes_per_block);
  131. const u32 pitch = image.info.pitch;
  132. UNIMPLEMENTED_IF_MSG(!std::has_single_bit(bytes_per_block),
  133. "Non-power of two images are not implemented");
  134. program_manager.BindHostCompute(pitch_unswizzle_program.handle);
  135. glFlushMappedNamedBufferRange(map.buffer, map.offset, image.guest_size_bytes);
  136. glUniform2ui(LOC_ORIGIN, 0, 0);
  137. glUniform2i(LOC_DESTINATION, 0, 0);
  138. glUniform1ui(LOC_BYTES_PER_BLOCK, bytes_per_block);
  139. glUniform1ui(LOC_PITCH, pitch);
  140. glBindImageTexture(BINDING_OUTPUT_IMAGE, image.StorageHandle(), 0, GL_FALSE, 0, GL_WRITE_ONLY,
  141. format);
  142. for (const SwizzleParameters& swizzle : swizzles) {
  143. const Extent3D num_tiles = swizzle.num_tiles;
  144. const size_t input_offset = swizzle.buffer_offset + map.offset;
  145. const u32 num_dispatches_x = Common::DivCeil(num_tiles.width, WORKGROUP_SIZE.width);
  146. const u32 num_dispatches_y = Common::DivCeil(num_tiles.height, WORKGROUP_SIZE.height);
  147. glBindBufferRange(GL_SHADER_STORAGE_BUFFER, BINDING_INPUT_BUFFER, map.buffer, input_offset,
  148. image.guest_size_bytes - swizzle.buffer_offset);
  149. glDispatchCompute(num_dispatches_x, num_dispatches_y, 1);
  150. }
  151. program_manager.RestoreGuestCompute();
  152. }
  153. void UtilShaders::CopyBC4(Image& dst_image, Image& src_image, std::span<const ImageCopy> copies) {
  154. static constexpr GLuint BINDING_INPUT_IMAGE = 0;
  155. static constexpr GLuint BINDING_OUTPUT_IMAGE = 1;
  156. static constexpr GLuint LOC_SRC_OFFSET = 0;
  157. static constexpr GLuint LOC_DST_OFFSET = 1;
  158. program_manager.BindHostCompute(copy_bc4_program.handle);
  159. for (const ImageCopy& copy : copies) {
  160. ASSERT(copy.src_subresource.base_layer == 0);
  161. ASSERT(copy.src_subresource.num_layers == 1);
  162. ASSERT(copy.dst_subresource.base_layer == 0);
  163. ASSERT(copy.dst_subresource.num_layers == 1);
  164. glUniform3ui(LOC_SRC_OFFSET, copy.src_offset.x, copy.src_offset.y, copy.src_offset.z);
  165. glUniform3ui(LOC_DST_OFFSET, copy.dst_offset.x, copy.dst_offset.y, copy.dst_offset.z);
  166. glBindImageTexture(BINDING_INPUT_IMAGE, src_image.StorageHandle(),
  167. copy.src_subresource.base_level, GL_FALSE, 0, GL_READ_ONLY, GL_RG32UI);
  168. glBindImageTexture(BINDING_OUTPUT_IMAGE, dst_image.StorageHandle(),
  169. copy.dst_subresource.base_level, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8UI);
  170. glDispatchCompute(copy.extent.width, copy.extent.height, copy.extent.depth);
  171. }
  172. program_manager.RestoreGuestCompute();
  173. }
  174. GLenum StoreFormat(u32 bytes_per_block) {
  175. switch (bytes_per_block) {
  176. case 1:
  177. return GL_R8UI;
  178. case 2:
  179. return GL_R16UI;
  180. case 4:
  181. return GL_R32UI;
  182. case 8:
  183. return GL_RG32UI;
  184. case 16:
  185. return GL_RGBA32UI;
  186. }
  187. UNREACHABLE();
  188. return GL_R8UI;
  189. }
  190. } // namespace OpenGL