Image.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/ImageFormat.h>
  12. namespace SoftGPU {
  13. class Image final : public RefCounted<Image> {
  14. public:
  15. Image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  16. ImageFormat format() const { return m_format; }
  17. unsigned width() const { return m_width; }
  18. unsigned height() const { return m_height; }
  19. unsigned depth() const { return m_depth; }
  20. unsigned level_width(unsigned level) const { return m_mipmap_sizes[level].x(); }
  21. unsigned level_height(unsigned level) const { return m_mipmap_sizes[level].y(); }
  22. unsigned level_depth(unsigned level) const { return m_mipmap_sizes[level].z(); }
  23. unsigned num_levels() const { return m_num_levels; }
  24. unsigned num_layers() const { return m_num_layers; }
  25. FloatVector4 texel(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) const
  26. {
  27. return unpack_color(texel_pointer(layer, level, x, y, z), m_format);
  28. }
  29. void set_texel(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z, FloatVector4 const& color)
  30. {
  31. pack_color(color, texel_pointer(layer, level, x, y, z), m_format);
  32. }
  33. private:
  34. void const* texel_pointer(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z) const
  35. {
  36. auto size = m_mipmap_sizes[level];
  37. return &m_data[m_mipchain_size * layer + m_mipmap_offsets[level] + (z * size.x() * size.y() + y * size.x() + x) * element_size(m_format)];
  38. }
  39. void* texel_pointer(unsigned layer, unsigned level, unsigned x, unsigned y, unsigned z)
  40. {
  41. auto size = m_mipmap_sizes[level];
  42. return &m_data[m_mipchain_size * layer + m_mipmap_offsets[level] + (z * size.x() * size.y() + y * size.x() + x) * element_size(m_format)];
  43. }
  44. private:
  45. ImageFormat m_format { ImageFormat::RGBA8888 };
  46. unsigned m_width { 0 };
  47. unsigned m_height { 0 };
  48. unsigned m_depth { 0 };
  49. unsigned m_num_levels { 0 };
  50. unsigned m_num_layers { 0 };
  51. size_t m_mipchain_size { 0 };
  52. Vector<size_t, 16> m_mipmap_offsets;
  53. Vector<Vector3<unsigned>, 16> m_mipmap_sizes;
  54. Vector<u8> m_data;
  55. };
  56. }