Image.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. Image::Image(unsigned width, unsigned height, unsigned depth, unsigned max_levels, unsigned layers)
  10. : m_num_layers(layers)
  11. , m_mipmap_buffers(FixedArray<RefPtr<Typed3DBuffer<ColorType>>>::must_create_but_fixme_should_propagate_errors(layers * max_levels))
  12. {
  13. VERIFY(width > 0);
  14. VERIFY(height > 0);
  15. VERIFY(depth > 0);
  16. VERIFY(max_levels > 0);
  17. VERIFY(layers > 0);
  18. if ((width & (width - 1)) == 0)
  19. m_width_is_power_of_two = true;
  20. if ((height & (height - 1)) == 0)
  21. m_height_is_power_of_two = true;
  22. if ((depth & (depth - 1)) == 0)
  23. m_depth_is_power_of_two = true;
  24. unsigned level;
  25. for (level = 0; level < max_levels; ++level) {
  26. for (unsigned layer = 0; layer < layers; ++layer)
  27. m_mipmap_buffers[layer * layers + level] = MUST(Typed3DBuffer<ColorType>::try_create(width, height, depth));
  28. if (width <= 1 && height <= 1 && depth <= 1)
  29. break;
  30. width = max(width / 2, 1);
  31. height = max(height / 2, 1);
  32. depth = max(depth / 2, 1);
  33. }
  34. m_num_levels = level + 1;
  35. }
  36. void Image::write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, ImageDataLayout const& layout)
  37. {
  38. VERIFY(layer < num_layers());
  39. VERIFY(level < num_levels());
  40. VERIFY(offset.x() + size.x() <= level_width(level));
  41. VERIFY(offset.y() + size.y() <= level_height(level));
  42. VERIFY(offset.z() + size.z() <= level_depth(level));
  43. for (unsigned z = 0; z < size.z(); ++z) {
  44. for (unsigned y = 0; y < size.y(); ++y) {
  45. for (unsigned x = 0; x < size.x(); ++x) {
  46. auto ptr = reinterpret_cast<u8 const*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x;
  47. auto color = unpack_color(ptr, layout.format);
  48. set_texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z, color);
  49. }
  50. }
  51. }
  52. }
  53. void Image::read_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void* data, ImageDataLayout const& layout) const
  54. {
  55. VERIFY(layer < num_layers());
  56. VERIFY(level < num_levels());
  57. VERIFY(offset.x() + size.x() <= level_width(level));
  58. VERIFY(offset.y() + size.y() <= level_height(level));
  59. VERIFY(offset.z() + size.z() <= level_depth(level));
  60. for (unsigned z = 0; z < size.z(); ++z) {
  61. for (unsigned y = 0; y < size.y(); ++y) {
  62. for (unsigned x = 0; x < size.x(); ++x) {
  63. auto color = texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z);
  64. auto ptr = reinterpret_cast<u8*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x;
  65. pack_color(color, ptr, layout.format);
  66. }
  67. }
  68. }
  69. }
  70. void Image::copy_texels(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)
  71. {
  72. VERIFY(source_layer < source.num_layers());
  73. VERIFY(source_level < source.num_levels());
  74. VERIFY(source_offset.x() + size.x() <= source.level_width(source_level));
  75. VERIFY(source_offset.y() + size.y() <= source.level_height(source_level));
  76. VERIFY(source_offset.z() + size.z() <= source.level_depth(source_level));
  77. VERIFY(destination_layer < num_layers());
  78. VERIFY(destination_level < num_levels());
  79. VERIFY(destination_offset.x() + size.x() <= level_width(destination_level));
  80. VERIFY(destination_offset.y() + size.y() <= level_height(destination_level));
  81. VERIFY(destination_offset.z() + size.z() <= level_depth(destination_level));
  82. for (unsigned z = 0; z < size.z(); ++z) {
  83. for (unsigned y = 0; y < size.y(); ++y) {
  84. for (unsigned x = 0; x < size.x(); ++x) {
  85. auto color = source.texel(source_layer, source_level, source_offset.x() + x, source_offset.y() + y, source_offset.z() + z);
  86. set_texel(destination_layer, destination_level, destination_offset.x() + x, destination_offset.y() + y, destination_offset.z() + z, color);
  87. }
  88. }
  89. }
  90. }
  91. }