Image.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSoftGPU/Image.h>
  7. namespace SoftGPU {
  8. Image::Image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers)
  9. : m_format(format)
  10. , m_width(width)
  11. , m_height(height)
  12. , m_depth(depth)
  13. , m_num_layers(layers)
  14. {
  15. VERIFY(width > 0);
  16. VERIFY(height > 0);
  17. VERIFY(depth > 0);
  18. VERIFY(levels > 0);
  19. VERIFY(layers > 0);
  20. if ((width & (width - 1)) == 0)
  21. m_width_is_power_of_two = true;
  22. if ((height & (height - 1)) == 0)
  23. m_height_is_power_of_two = true;
  24. if ((depth & (depth - 1)) == 0)
  25. m_depth_is_power_of_two = true;
  26. m_mipmap_sizes.append({ width, height, depth });
  27. m_mipmap_offsets.append(0);
  28. m_mipchain_size += width * height * depth * element_size(format);
  29. while (--levels && (width > 1 || height > 1 || depth > 1)) {
  30. width = max(width / 2, 1);
  31. height = max(height / 2, 1);
  32. depth = max(depth / 2, 1);
  33. m_mipmap_sizes.append({ width, height, depth });
  34. m_mipmap_offsets.append(m_mipchain_size);
  35. m_mipchain_size += width * height * depth * element_size(format);
  36. }
  37. m_num_levels = m_mipmap_sizes.size();
  38. m_data.resize(m_mipchain_size * m_num_layers);
  39. }
  40. void Image::write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, ImageDataLayout const& layout)
  41. {
  42. VERIFY(layer < num_layers());
  43. VERIFY(level < num_levels());
  44. VERIFY(offset.x() + size.x() <= level_width(level));
  45. VERIFY(offset.y() + size.y() <= level_height(level));
  46. VERIFY(offset.z() + size.z() <= level_depth(level));
  47. for (unsigned z = 0; z < size.z(); ++z) {
  48. for (unsigned y = 0; y < size.y(); ++y) {
  49. for (unsigned x = 0; x < size.x(); ++x) {
  50. auto ptr = reinterpret_cast<u8 const*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x;
  51. auto color = unpack_color(ptr, layout.format);
  52. set_texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z, color);
  53. }
  54. }
  55. }
  56. }
  57. void Image::read_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void* data, ImageDataLayout const& layout) const
  58. {
  59. VERIFY(layer < num_layers());
  60. VERIFY(level < num_levels());
  61. VERIFY(offset.x() + size.x() <= level_width(level));
  62. VERIFY(offset.y() + size.y() <= level_height(level));
  63. VERIFY(offset.z() + size.z() <= level_depth(level));
  64. for (unsigned z = 0; z < size.z(); ++z) {
  65. for (unsigned y = 0; y < size.y(); ++y) {
  66. for (unsigned x = 0; x < size.x(); ++x) {
  67. auto color = texel(layer, level, offset.x() + x, offset.y() + y, offset.z() + z);
  68. auto ptr = reinterpret_cast<u8*>(data) + layout.depth_stride * z + layout.row_stride * y + layout.column_stride * x;
  69. pack_color(color, ptr, layout.format);
  70. }
  71. }
  72. }
  73. }
  74. 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)
  75. {
  76. VERIFY(source_layer < source.num_layers());
  77. VERIFY(source_level < source.num_levels());
  78. VERIFY(source_offset.x() + size.x() <= source.level_width(source_level));
  79. VERIFY(source_offset.y() + size.y() <= source.level_height(source_level));
  80. VERIFY(source_offset.z() + size.z() <= source.level_depth(source_level));
  81. VERIFY(destination_layer < num_layers());
  82. VERIFY(destination_level < num_levels());
  83. VERIFY(destination_offset.x() + size.x() <= level_width(destination_level));
  84. VERIFY(destination_offset.y() + size.y() <= level_height(destination_level));
  85. VERIFY(destination_offset.z() + size.z() <= level_depth(destination_level));
  86. for (unsigned z = 0; z < size.z(); ++z) {
  87. for (unsigned y = 0; y < size.y(); ++y) {
  88. for (unsigned x = 0; x < size.x(); ++x) {
  89. auto color = source.texel(source_layer, source_level, source_offset.x() + x, source_offset.y() + y, source_offset.z() + z);
  90. set_texel(destination_layer, destination_level, destination_offset.x() + x, destination_offset.y() + y, destination_offset.z() + z, color);
  91. }
  92. }
  93. }
  94. }
  95. }