Image.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #pragma once
  8. #include <AK/FixedArray.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <LibGfx/Vector3.h>
  12. #include <LibGfx/Vector4.h>
  13. #include <LibSoftGPU/Buffer/Typed3DBuffer.h>
  14. #include <LibSoftGPU/Enums.h>
  15. #include <LibSoftGPU/ImageDataLayout.h>
  16. #include <LibSoftGPU/ImageFormat.h>
  17. namespace SoftGPU {
  18. class Image final : public RefCounted<Image> {
  19. public:
  20. Image(unsigned width, unsigned height, unsigned depth, unsigned max_levels, unsigned layers);
  21. unsigned level_width(unsigned level) const { return m_mipmap_buffers[level]->width(); }
  22. unsigned level_height(unsigned level) const { return m_mipmap_buffers[level]->height(); }
  23. unsigned level_depth(unsigned level) const { return m_mipmap_buffers[level]->depth(); }
  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, int x, int y, int z) const
  30. {
  31. return unpack_color(texel_pointer(layer, level, x, y, z), ImageFormat::BGRA8888);
  32. }
  33. void set_texel(unsigned layer, unsigned level, int x, int y, int z, FloatVector4 const& color)
  34. {
  35. pack_color(color, texel_pointer(layer, level, x, y, z), ImageFormat::BGRA8888);
  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, int x, int y, int z) const
  42. {
  43. return m_mipmap_buffers[layer * m_num_layers + level]->buffer_pointer(x, y, z);
  44. }
  45. void* texel_pointer(unsigned layer, unsigned level, int x, int y, int z)
  46. {
  47. return m_mipmap_buffers[layer * m_num_layers + level]->buffer_pointer(x, y, z);
  48. }
  49. private:
  50. unsigned m_num_levels { 0 };
  51. unsigned m_num_layers { 0 };
  52. FixedArray<RefPtr<Typed3DBuffer<ColorType>>> m_mipmap_buffers;
  53. bool m_width_is_power_of_two { false };
  54. bool m_height_is_power_of_two { false };
  55. bool m_depth_is_power_of_two { false };
  56. };
  57. }