Image.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/RefPtr.h>
  10. #include <LibGPU/Image.h>
  11. #include <LibGPU/ImageDataLayout.h>
  12. #include <LibGPU/ImageFormat.h>
  13. #include <LibGfx/Vector3.h>
  14. #include <LibGfx/Vector4.h>
  15. #include <LibSoftGPU/Buffer/Typed3DBuffer.h>
  16. namespace SoftGPU {
  17. class Image final : public GPU::Image {
  18. public:
  19. Image(void const* ownership_token, GPU::PixelFormat const&, u32 width, u32 height, u32 depth, u32 max_levels);
  20. u32 level_width(u32 level) const { return m_mipmap_buffers[level]->width(); }
  21. u32 level_height(u32 level) const { return m_mipmap_buffers[level]->height(); }
  22. u32 level_depth(u32 level) const { return m_mipmap_buffers[level]->depth(); }
  23. u32 num_levels() const { return m_num_levels; }
  24. bool width_is_power_of_two() const { return m_width_is_power_of_two; }
  25. bool height_is_power_of_two() const { return m_height_is_power_of_two; }
  26. bool depth_is_power_of_two() const { return m_depth_is_power_of_two; }
  27. GPU::ImageDataLayout image_data_layout(u32 level, Vector3<i32> offset) const;
  28. FloatVector4 texel(u32 level, int x, int y, int z) const
  29. {
  30. return *texel_pointer(level, x, y, z);
  31. }
  32. void set_texel(u32 level, int x, int y, int z, FloatVector4 const& color)
  33. {
  34. *texel_pointer(level, x, y, z) = color;
  35. }
  36. virtual void write_texels(u32 level, Vector3<i32> const& output_offset, void const* input_data, GPU::ImageDataLayout const&) override;
  37. virtual void read_texels(u32 level, Vector3<i32> const& input_offset, void* output_data, GPU::ImageDataLayout const&) const override;
  38. virtual void copy_texels(GPU::Image const& source, u32 source_level, Vector3<u32> const& source_offset, Vector3<u32> const& size, u32 destination_level, Vector3<u32> const& destination_offset) override;
  39. FloatVector4 const* texel_pointer(u32 level, int x, int y, int z) const
  40. {
  41. return m_mipmap_buffers[level]->buffer_pointer(x, y, z);
  42. }
  43. FloatVector4* texel_pointer(u32 level, int x, int y, int z)
  44. {
  45. return m_mipmap_buffers[level]->buffer_pointer(x, y, z);
  46. }
  47. private:
  48. u32 m_num_levels { 0 };
  49. GPU::PixelFormat m_pixel_format;
  50. FixedArray<RefPtr<Typed3DBuffer<FloatVector4>>> m_mipmap_buffers;
  51. bool m_width_is_power_of_two { false };
  52. bool m_height_is_power_of_two { false };
  53. bool m_depth_is_power_of_two { false };
  54. };
  55. }