Image.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #pragma once
  8. #include <AK/FixedArray.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <LibGPU/Enums.h>
  12. #include <LibGPU/Image.h>
  13. #include <LibGPU/ImageDataLayout.h>
  14. #include <LibGPU/ImageFormat.h>
  15. #include <LibGfx/Vector3.h>
  16. #include <LibGfx/Vector4.h>
  17. #include <LibSoftGPU/Buffer/Typed3DBuffer.h>
  18. #include <LibSoftGPU/Config.h>
  19. namespace SoftGPU {
  20. inline static FloatVector4 unpack_color(void const* ptr, GPU::ImageFormat format)
  21. {
  22. constexpr auto one_over_255 = 1.0f / 255;
  23. switch (format) {
  24. case GPU::ImageFormat::RGB888: {
  25. auto rgb = reinterpret_cast<u8 const*>(ptr);
  26. return {
  27. rgb[0] * one_over_255,
  28. rgb[1] * one_over_255,
  29. rgb[2] * one_over_255,
  30. 1.0f,
  31. };
  32. }
  33. case GPU::ImageFormat::BGR888: {
  34. auto bgr = reinterpret_cast<u8 const*>(ptr);
  35. return {
  36. bgr[2] * one_over_255,
  37. bgr[1] * one_over_255,
  38. bgr[0] * one_over_255,
  39. 1.0f,
  40. };
  41. }
  42. case GPU::ImageFormat::RGBA8888: {
  43. auto rgba = *reinterpret_cast<u32 const*>(ptr);
  44. return {
  45. (rgba & 0xff) * one_over_255,
  46. ((rgba >> 8) & 0xff) * one_over_255,
  47. ((rgba >> 16) & 0xff) * one_over_255,
  48. ((rgba >> 24) & 0xff) * one_over_255,
  49. };
  50. }
  51. case GPU::ImageFormat::BGRA8888: {
  52. auto bgra = *reinterpret_cast<u32 const*>(ptr);
  53. return {
  54. ((bgra >> 16) & 0xff) * one_over_255,
  55. ((bgra >> 8) & 0xff) * one_over_255,
  56. (bgra & 0xff) * one_over_255,
  57. ((bgra >> 24) & 0xff) * one_over_255,
  58. };
  59. }
  60. case GPU::ImageFormat::RGB565: {
  61. auto rgb = *reinterpret_cast<u16 const*>(ptr);
  62. return {
  63. ((rgb >> 11) & 0x1f) / 31.f,
  64. ((rgb >> 5) & 0x3f) / 63.f,
  65. (rgb & 0x1f) / 31.f,
  66. 1.0f
  67. };
  68. }
  69. case GPU::ImageFormat::L8: {
  70. auto luminance = *reinterpret_cast<u8 const*>(ptr);
  71. auto clamped_luminance = luminance * one_over_255;
  72. return {
  73. clamped_luminance,
  74. clamped_luminance,
  75. clamped_luminance,
  76. 1.0f,
  77. };
  78. }
  79. case GPU::ImageFormat::L8A8: {
  80. auto luminance_and_alpha = reinterpret_cast<u8 const*>(ptr);
  81. auto clamped_luminance = luminance_and_alpha[0] * one_over_255;
  82. return {
  83. clamped_luminance,
  84. clamped_luminance,
  85. clamped_luminance,
  86. luminance_and_alpha[1] * one_over_255,
  87. };
  88. }
  89. default:
  90. VERIFY_NOT_REACHED();
  91. }
  92. }
  93. inline static void pack_color(FloatVector4 const& color, void* ptr, GPU::ImageFormat format)
  94. {
  95. auto r = static_cast<u8>(clamp(color.x(), 0.0f, 1.0f) * 255);
  96. auto g = static_cast<u8>(clamp(color.y(), 0.0f, 1.0f) * 255);
  97. auto b = static_cast<u8>(clamp(color.z(), 0.0f, 1.0f) * 255);
  98. auto a = static_cast<u8>(clamp(color.w(), 0.0f, 1.0f) * 255);
  99. switch (format) {
  100. case GPU::ImageFormat::RGB888:
  101. reinterpret_cast<u8*>(ptr)[0] = r;
  102. reinterpret_cast<u8*>(ptr)[1] = g;
  103. reinterpret_cast<u8*>(ptr)[2] = b;
  104. return;
  105. case GPU::ImageFormat::BGR888:
  106. reinterpret_cast<u8*>(ptr)[2] = b;
  107. reinterpret_cast<u8*>(ptr)[1] = g;
  108. reinterpret_cast<u8*>(ptr)[0] = r;
  109. return;
  110. case GPU::ImageFormat::RGBA8888:
  111. *reinterpret_cast<u32*>(ptr) = r | (g << 8) | (b << 16) | (a << 24);
  112. return;
  113. case GPU::ImageFormat::BGRA8888:
  114. *reinterpret_cast<u32*>(ptr) = b | (g << 8) | (r << 16) | (a << 24);
  115. return;
  116. case GPU::ImageFormat::RGB565:
  117. *reinterpret_cast<u16*>(ptr) = (r & 0x1f) | ((g & 0x3f) << 5) | ((b & 0x1f) << 11);
  118. return;
  119. case GPU::ImageFormat::L8:
  120. *reinterpret_cast<u8*>(ptr) = r;
  121. return;
  122. case GPU::ImageFormat::L8A8:
  123. reinterpret_cast<u8*>(ptr)[0] = r;
  124. reinterpret_cast<u8*>(ptr)[1] = a;
  125. return;
  126. default:
  127. VERIFY_NOT_REACHED();
  128. }
  129. }
  130. class Image final : public GPU::Image {
  131. public:
  132. Image(void* const ownership_token, unsigned width, unsigned height, unsigned depth, unsigned max_levels, unsigned layers);
  133. unsigned level_width(unsigned level) const { return m_mipmap_buffers[level]->width(); }
  134. unsigned level_height(unsigned level) const { return m_mipmap_buffers[level]->height(); }
  135. unsigned level_depth(unsigned level) const { return m_mipmap_buffers[level]->depth(); }
  136. unsigned num_levels() const { return m_num_levels; }
  137. unsigned num_layers() const { return m_num_layers; }
  138. bool width_is_power_of_two() const { return m_width_is_power_of_two; }
  139. bool height_is_power_of_two() const { return m_height_is_power_of_two; }
  140. bool depth_is_power_of_two() const { return m_depth_is_power_of_two; }
  141. FloatVector4 texel(unsigned layer, unsigned level, int x, int y, int z) const
  142. {
  143. return unpack_color(texel_pointer(layer, level, x, y, z), GPU::ImageFormat::BGRA8888);
  144. }
  145. void set_texel(unsigned layer, unsigned level, int x, int y, int z, FloatVector4 const& color)
  146. {
  147. pack_color(color, texel_pointer(layer, level, x, y, z), GPU::ImageFormat::BGRA8888);
  148. }
  149. virtual void write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, GPU::ImageDataLayout const& layout) override;
  150. virtual void read_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void* data, GPU::ImageDataLayout const& layout) const override;
  151. virtual void 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) override;
  152. private:
  153. void const* texel_pointer(unsigned layer, unsigned level, int x, int y, int z) const
  154. {
  155. return m_mipmap_buffers[layer * m_num_layers + level]->buffer_pointer(x, y, z);
  156. }
  157. void* texel_pointer(unsigned layer, unsigned level, int x, int y, int z)
  158. {
  159. return m_mipmap_buffers[layer * m_num_layers + level]->buffer_pointer(x, y, z);
  160. }
  161. private:
  162. unsigned m_num_levels { 0 };
  163. unsigned m_num_layers { 0 };
  164. FixedArray<RefPtr<Typed3DBuffer<GPU::ColorType>>> m_mipmap_buffers;
  165. bool m_width_is_power_of_two { false };
  166. bool m_height_is_power_of_two { false };
  167. bool m_depth_is_power_of_two { false };
  168. };
  169. }