Image.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <AK/Vector.h>
  9. #include <LibGfx/Vector3.h>
  10. #include <LibGfx/Vector4.h>
  11. #include <LibSoftGPU/ImageDataLayout.h>
  12. #include <LibSoftGPU/ImageFormat.h>
  13. namespace SoftGPU {
  14. class Image final : public RefCounted<Image> {
  15. public:
  16. Image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  17. ImageFormat format() const { return m_format; }
  18. unsigned width() const { return m_width; }
  19. unsigned height() const { return m_height; }
  20. unsigned depth() const { return m_depth; }
  21. unsigned level_width(unsigned level) const { return m_mipmap_sizes[level].x(); }
  22. unsigned level_height(unsigned level) const { return m_mipmap_sizes[level].y(); }
  23. unsigned level_depth(unsigned level) const { return m_mipmap_sizes[level].z(); }
  24. unsigned num_levels() const { return m_num_levels; }
  25. unsigned num_layers() const { return m_num_layers; }
  26. bool width_is_power_of_two() const { return m_width_is_power_of_two; }
  27. bool height_is_power_of_two() const { return m_height_is_power_of_two; }
  28. bool depth_is_power_of_two() const { return m_depth_is_power_of_two; }
  29. FloatVector4 texel(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) const
  30. {
  31. return unpack_color(texel_pointer(layer, level, x, y, z), m_format);
  32. }
  33. void set_texel(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z, FloatVector4 const& color)
  34. {
  35. pack_color(color, texel_pointer(layer, level, x, y, z), m_format);
  36. }
  37. void write_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void const* data, ImageDataLayout const& layout);
  38. void read_texels(unsigned layer, unsigned level, Vector3<unsigned> const& offset, Vector3<unsigned> const& size, void* data, ImageDataLayout const& layout) const;
  39. void 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);
  40. private:
  41. void const* texel_pointer(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) const
  42. {
  43. auto size = m_mipmap_sizes[level];
  44. return &m_data[m_mipchain_size * layer + m_mipmap_offsets[level] + (z * size.x() * size.y() + y * size.x() + x) * element_size(m_format)];
  45. }
  46. void* texel_pointer(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z)
  47. {
  48. auto size = m_mipmap_sizes[level];
  49. return &m_data[m_mipchain_size * layer + m_mipmap_offsets[level] + (z * size.x() * size.y() + y * size.x() + x) * element_size(m_format)];
  50. }
  51. private:
  52. ImageFormat m_format { ImageFormat::RGBA8888 };
  53. unsigned m_width { 0 };
  54. unsigned m_height { 0 };
  55. unsigned m_depth { 0 };
  56. unsigned m_num_levels { 0 };
  57. unsigned m_num_layers { 0 };
  58. size_t m_mipchain_size { 0 };
  59. Vector<size_t, 16> m_mipmap_offsets;
  60. Vector<Vector3<unsigned>, 16> m_mipmap_sizes;
  61. Vector<u8> m_data;
  62. bool m_width_is_power_of_two { false };
  63. bool m_height_is_power_of_two { false };
  64. bool m_depth_is_power_of_two { false };
  65. };
  66. }