SWTexture.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/Software/SWTexture.h"
  4. #include <cstring>
  5. #include "Common/Assert.h"
  6. #include "VideoBackends/Software/CopyRegion.h"
  7. #include "VideoBackends/Software/SWGfx.h"
  8. namespace SW
  9. {
  10. namespace
  11. {
  12. #pragma pack(push, 1)
  13. struct Pixel
  14. {
  15. u8 r;
  16. u8 g;
  17. u8 b;
  18. u8 a;
  19. };
  20. #pragma pack(pop)
  21. void CopyTextureData(const TextureConfig& src_config, const u8* src_ptr, u32 src_x, u32 src_y,
  22. u32 width, u32 height, u32 src_level, const TextureConfig& dst_config,
  23. u8* dst_ptr, u32 dst_x, u32 dst_y, u32 dst_level)
  24. {
  25. const size_t texel_size = AbstractTexture::GetTexelSizeForFormat(src_config.format);
  26. const size_t src_stride = src_config.GetMipStride(src_level);
  27. const size_t src_offset =
  28. static_cast<size_t>(src_y) * src_stride + static_cast<size_t>(src_x) * texel_size;
  29. const size_t dst_stride = dst_config.GetMipStride(dst_level);
  30. const size_t dst_offset =
  31. static_cast<size_t>(dst_y) * dst_stride + static_cast<size_t>(dst_x) * texel_size;
  32. const size_t copy_len = static_cast<size_t>(width) * texel_size;
  33. src_ptr += src_offset;
  34. dst_ptr += dst_offset;
  35. for (u32 i = 0; i < height; i++)
  36. {
  37. std::memcpy(dst_ptr, src_ptr, copy_len);
  38. src_ptr += src_stride;
  39. dst_ptr += dst_stride;
  40. }
  41. }
  42. } // namespace
  43. void SWGfx::ScaleTexture(AbstractFramebuffer* dst_framebuffer,
  44. const MathUtil::Rectangle<int>& dst_rect,
  45. const AbstractTexture* src_texture,
  46. const MathUtil::Rectangle<int>& src_rect)
  47. {
  48. const SWTexture* software_source_texture = static_cast<const SWTexture*>(src_texture);
  49. SWTexture* software_dest_texture = static_cast<SWTexture*>(dst_framebuffer->GetColorAttachment());
  50. CopyRegion(reinterpret_cast<const Pixel*>(software_source_texture->GetData(0, 0)), src_rect,
  51. src_texture->GetWidth(), src_texture->GetHeight(),
  52. reinterpret_cast<Pixel*>(software_dest_texture->GetData(0, 0)), dst_rect,
  53. dst_framebuffer->GetWidth(), dst_framebuffer->GetHeight());
  54. }
  55. SWTexture::SWTexture(const TextureConfig& tex_config) : AbstractTexture(tex_config)
  56. {
  57. m_data.resize(tex_config.layers);
  58. for (u32 layer = 0; layer < tex_config.layers; layer++)
  59. {
  60. m_data[layer].resize(tex_config.levels);
  61. for (u32 level = 0; level < tex_config.levels; level++)
  62. {
  63. m_data[layer][level].resize(std::max(tex_config.width >> level, 1u) *
  64. std::max(tex_config.height >> level, 1u) * sizeof(Pixel));
  65. }
  66. }
  67. }
  68. void SWTexture::CopyRectangleFromTexture(const AbstractTexture* src,
  69. const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
  70. u32 src_level, const MathUtil::Rectangle<int>& dst_rect,
  71. u32 dst_layer, u32 dst_level)
  72. {
  73. CopyTextureData(src->GetConfig(),
  74. static_cast<const SWTexture*>(src)->GetData(src_layer, src_level), src_rect.left,
  75. src_rect.top, src_rect.GetWidth(), src_rect.GetHeight(), src_level, m_config,
  76. GetData(dst_layer, dst_level), dst_rect.left, dst_rect.top, dst_level);
  77. }
  78. void SWTexture::ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect,
  79. u32 layer, u32 level)
  80. {
  81. }
  82. void SWTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer,
  83. size_t buffer_size, u32 layer)
  84. {
  85. u8* data = GetData(layer, level);
  86. for (u32 y = 0; y < height; y++)
  87. {
  88. memcpy(&data[width * y * sizeof(Pixel)], &buffer[y * row_length * sizeof(Pixel)],
  89. width * sizeof(Pixel));
  90. }
  91. }
  92. const u8* SWTexture::GetData(u32 layer, u32 level) const
  93. {
  94. return m_data[layer][level].data();
  95. }
  96. u8* SWTexture::GetData(u32 layer, u32 level)
  97. {
  98. return m_data[layer][level].data();
  99. }
  100. SWStagingTexture::SWStagingTexture(StagingTextureType type, const TextureConfig& config)
  101. : AbstractStagingTexture(type, config)
  102. {
  103. m_data.resize(m_texel_size * config.width * config.height);
  104. m_map_pointer = reinterpret_cast<char*>(m_data.data());
  105. m_map_stride = m_texel_size * config.width;
  106. }
  107. SWStagingTexture::~SWStagingTexture() = default;
  108. void SWStagingTexture::CopyFromTexture(const AbstractTexture* src,
  109. const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
  110. u32 src_level, const MathUtil::Rectangle<int>& dst_rect)
  111. {
  112. CopyTextureData(src->GetConfig(),
  113. static_cast<const SWTexture*>(src)->GetData(src_layer, src_level), src_rect.left,
  114. src_rect.top, src_rect.GetWidth(), src_rect.GetHeight(), src_level, m_config,
  115. m_data.data(), dst_rect.left, dst_rect.top, 0);
  116. m_needs_flush = true;
  117. }
  118. void SWStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
  119. const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer,
  120. u32 dst_level)
  121. {
  122. CopyTextureData(m_config, m_data.data(), src_rect.left, src_rect.top, src_rect.GetWidth(),
  123. src_rect.GetHeight(), 0, dst->GetConfig(),
  124. static_cast<SWTexture*>(dst)->GetData(dst_layer, dst_level), dst_rect.left,
  125. dst_rect.top, dst_level);
  126. m_needs_flush = true;
  127. }
  128. bool SWStagingTexture::Map()
  129. {
  130. return true;
  131. }
  132. void SWStagingTexture::Unmap()
  133. {
  134. }
  135. void SWStagingTexture::Flush()
  136. {
  137. m_needs_flush = false;
  138. }
  139. SWFramebuffer::SWFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
  140. std::vector<AbstractTexture*> additional_color_attachments,
  141. AbstractTextureFormat color_format, AbstractTextureFormat depth_format,
  142. u32 width, u32 height, u32 layers, u32 samples)
  143. : AbstractFramebuffer(color_attachment, depth_attachment,
  144. std::move(additional_color_attachments), color_format, depth_format,
  145. width, height, layers, samples)
  146. {
  147. }
  148. std::unique_ptr<SWFramebuffer>
  149. SWFramebuffer::Create(SWTexture* color_attachment, SWTexture* depth_attachment,
  150. std::vector<AbstractTexture*> additional_color_attachments)
  151. {
  152. if (!ValidateConfig(color_attachment, depth_attachment, additional_color_attachments))
  153. return nullptr;
  154. const AbstractTextureFormat color_format =
  155. color_attachment ? color_attachment->GetFormat() : AbstractTextureFormat::Undefined;
  156. const AbstractTextureFormat depth_format =
  157. depth_attachment ? depth_attachment->GetFormat() : AbstractTextureFormat::Undefined;
  158. const SWTexture* either_attachment = color_attachment ? color_attachment : depth_attachment;
  159. const u32 width = either_attachment->GetWidth();
  160. const u32 height = either_attachment->GetHeight();
  161. const u32 layers = either_attachment->GetLayers();
  162. const u32 samples = either_attachment->GetSamples();
  163. return std::make_unique<SWFramebuffer>(color_attachment, depth_attachment,
  164. std::move(additional_color_attachments), color_format,
  165. depth_format, width, height, layers, samples);
  166. }
  167. } // namespace SW