Image.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibSoftGPU/Image.h>
  8. namespace SoftGPU {
  9. static constexpr FloatVector4 unpack_color(void const* ptr, GPU::ImageFormat format)
  10. {
  11. constexpr auto one_over_255 = 1.0f / 255;
  12. switch (format) {
  13. case GPU::ImageFormat::RGB888: {
  14. auto rgb = reinterpret_cast<u8 const*>(ptr);
  15. return {
  16. rgb[0] * one_over_255,
  17. rgb[1] * one_over_255,
  18. rgb[2] * one_over_255,
  19. 1.0f,
  20. };
  21. }
  22. case GPU::ImageFormat::BGR888: {
  23. auto bgr = reinterpret_cast<u8 const*>(ptr);
  24. return {
  25. bgr[2] * one_over_255,
  26. bgr[1] * one_over_255,
  27. bgr[0] * one_over_255,
  28. 1.0f,
  29. };
  30. }
  31. case GPU::ImageFormat::RGBA8888: {
  32. auto rgba = *reinterpret_cast<u32 const*>(ptr);
  33. return {
  34. (rgba & 0xff) * one_over_255,
  35. ((rgba >> 8) & 0xff) * one_over_255,
  36. ((rgba >> 16) & 0xff) * one_over_255,
  37. ((rgba >> 24) & 0xff) * one_over_255,
  38. };
  39. }
  40. case GPU::ImageFormat::BGRA8888: {
  41. auto bgra = *reinterpret_cast<u32 const*>(ptr);
  42. return {
  43. ((bgra >> 16) & 0xff) * one_over_255,
  44. ((bgra >> 8) & 0xff) * one_over_255,
  45. (bgra & 0xff) * one_over_255,
  46. ((bgra >> 24) & 0xff) * one_over_255,
  47. };
  48. }
  49. case GPU::ImageFormat::RGB565: {
  50. auto rgb = *reinterpret_cast<u16 const*>(ptr);
  51. return {
  52. ((rgb >> 11) & 0x1f) / 31.f,
  53. ((rgb >> 5) & 0x3f) / 63.f,
  54. (rgb & 0x1f) / 31.f,
  55. 1.0f
  56. };
  57. }
  58. case GPU::ImageFormat::L8: {
  59. auto luminance = *reinterpret_cast<u8 const*>(ptr);
  60. auto clamped_luminance = luminance * one_over_255;
  61. return {
  62. clamped_luminance,
  63. clamped_luminance,
  64. clamped_luminance,
  65. 1.0f,
  66. };
  67. }
  68. case GPU::ImageFormat::L8A8: {
  69. auto luminance_and_alpha = reinterpret_cast<u8 const*>(ptr);
  70. auto clamped_luminance = luminance_and_alpha[0] * one_over_255;
  71. return {
  72. clamped_luminance,
  73. clamped_luminance,
  74. clamped_luminance,
  75. luminance_and_alpha[1] * one_over_255,
  76. };
  77. }
  78. default:
  79. VERIFY_NOT_REACHED();
  80. }
  81. }
  82. static constexpr void pack_color(FloatVector4 const& color, void* ptr, GPU::ImageFormat format)
  83. {
  84. auto r = static_cast<u8>(clamp(color.x(), 0.0f, 1.0f) * 255);
  85. auto g = static_cast<u8>(clamp(color.y(), 0.0f, 1.0f) * 255);
  86. auto b = static_cast<u8>(clamp(color.z(), 0.0f, 1.0f) * 255);
  87. auto a = static_cast<u8>(clamp(color.w(), 0.0f, 1.0f) * 255);
  88. switch (format) {
  89. case GPU::ImageFormat::RGB888:
  90. reinterpret_cast<u8*>(ptr)[0] = r;
  91. reinterpret_cast<u8*>(ptr)[1] = g;
  92. reinterpret_cast<u8*>(ptr)[2] = b;
  93. return;
  94. case GPU::ImageFormat::BGR888:
  95. reinterpret_cast<u8*>(ptr)[2] = b;
  96. reinterpret_cast<u8*>(ptr)[1] = g;
  97. reinterpret_cast<u8*>(ptr)[0] = r;
  98. return;
  99. case GPU::ImageFormat::RGBA8888:
  100. *reinterpret_cast<u32*>(ptr) = r | (g << 8) | (b << 16) | (a << 24);
  101. return;
  102. case GPU::ImageFormat::BGRA8888:
  103. *reinterpret_cast<u32*>(ptr) = b | (g << 8) | (r << 16) | (a << 24);
  104. return;
  105. case GPU::ImageFormat::RGB565:
  106. *reinterpret_cast<u16*>(ptr) = (r & 0x1f) | ((g & 0x3f) << 5) | ((b & 0x1f) << 11);
  107. return;
  108. case GPU::ImageFormat::L8:
  109. *reinterpret_cast<u8*>(ptr) = r;
  110. return;
  111. case GPU::ImageFormat::L8A8:
  112. reinterpret_cast<u8*>(ptr)[0] = r;
  113. reinterpret_cast<u8*>(ptr)[1] = a;
  114. return;
  115. default:
  116. VERIFY_NOT_REACHED();
  117. }
  118. }
  119. Image::Image(void* const ownership_token, unsigned width, unsigned height, unsigned depth, unsigned max_levels, unsigned layers)
  120. : GPU::Image(ownership_token)
  121. , m_num_layers(layers)
  122. , m_mipmap_buffers(FixedArray<RefPtr<Typed3DBuffer<FloatVector4>>>::must_create_but_fixme_should_propagate_errors(layers * max_levels))
  123. {
  124. VERIFY(width > 0);
  125. VERIFY(height > 0);
  126. VERIFY(depth > 0);
  127. VERIFY(max_levels > 0);
  128. VERIFY(layers > 0);
  129. m_width_is_power_of_two = is_power_of_two(width);
  130. m_height_is_power_of_two = is_power_of_two(height);
  131. m_depth_is_power_of_two = is_power_of_two(depth);
  132. unsigned level;
  133. for (level = 0; level < max_levels; ++level) {
  134. for (unsigned layer = 0; layer < layers; ++layer)
  135. m_mipmap_buffers[layer * layers + level] = MUST(Typed3DBuffer<FloatVector4>::try_create(width, height, depth));
  136. if (width <= 1 && height <= 1 && depth <= 1)
  137. break;
  138. width = max(width / 2, 1);
  139. height = max(height / 2, 1);
  140. depth = max(depth / 2, 1);
  141. }
  142. m_num_levels = level + 1;
  143. }
  144. void Image::write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, GPU::ImageDataLayout const& layout)
  145. {
  146. VERIFY(layer < num_layers());
  147. VERIFY(level < num_levels());
  148. VERIFY(offset.x() + size.x() <= level_width(level));
  149. VERIFY(offset.y() + size.y() <= level_height(level));
  150. VERIFY(offset.z() + size.z() <= level_depth(level));
  151. for (unsigned z = 0; z < size.z(); ++z) {
  152. for (unsigned y = 0; y < size.y(); ++y) {
  153. for (unsigned x = 0; x < size.x(); ++x) {
  154. auto ptr = reinterpret_cast<u8 const*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x;
  155. auto color = unpack_color(ptr, layout.format);
  156. set_texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z, color);
  157. }
  158. }
  159. }
  160. }
  161. void Image::read_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void* data, GPU::ImageDataLayout const& layout) const
  162. {
  163. VERIFY(layer < num_layers());
  164. VERIFY(level < num_levels());
  165. VERIFY(offset.x() + size.x() <= level_width(level));
  166. VERIFY(offset.y() + size.y() <= level_height(level));
  167. VERIFY(offset.z() + size.z() <= level_depth(level));
  168. for (unsigned z = 0; z < size.z(); ++z) {
  169. for (unsigned y = 0; y < size.y(); ++y) {
  170. for (unsigned x = 0; x < size.x(); ++x) {
  171. auto color = texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z);
  172. auto ptr = reinterpret_cast<u8*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x;
  173. pack_color(color, ptr, layout.format);
  174. }
  175. }
  176. }
  177. }
  178. void Image::copy_texels(GPU::Image const& source, unsigned source_layer, unsigned source_level, Vector3<unsigned> const& source_offset, Vector3<unsigned> const& size, unsigned destination_layer, unsigned destination_level, Vector3<unsigned> const& destination_offset)
  179. {
  180. VERIFY(source.has_same_ownership_token(*this));
  181. auto const& src_image = static_cast<Image const&>(source);
  182. VERIFY(source_layer < src_image.num_layers());
  183. VERIFY(source_level < src_image.num_levels());
  184. VERIFY(source_offset.x() + size.x() <= src_image.level_width(source_level));
  185. VERIFY(source_offset.y() + size.y() <= src_image.level_height(source_level));
  186. VERIFY(source_offset.z() + size.z() <= src_image.level_depth(source_level));
  187. VERIFY(destination_layer < num_layers());
  188. VERIFY(destination_level < num_levels());
  189. VERIFY(destination_offset.x() + size.x() <= level_width(destination_level));
  190. VERIFY(destination_offset.y() + size.y() <= level_height(destination_level));
  191. VERIFY(destination_offset.z() + size.z() <= level_depth(destination_level));
  192. for (unsigned z = 0; z < size.z(); ++z) {
  193. for (unsigned y = 0; y < size.y(); ++y) {
  194. for (unsigned x = 0; x < size.x(); ++x) {
  195. auto color = src_image.texel(source_layer, source_level, source_offset.x() + x, source_offset.y() + y, source_offset.z() + z);
  196. set_texel(destination_layer, destination_level, destination_offset.x() + x, destination_offset.y() + y, destination_offset.z() + z, color);
  197. }
  198. }
  199. }
  200. }
  201. }