Image.cpp 4.4 KB

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