Texture2D.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Texture.h"
  8. #include <AK/Array.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/Vector.h>
  11. #include <LibGL/GL/gl.h>
  12. #include <LibGfx/Vector2.h>
  13. #include <LibGfx/Vector4.h>
  14. namespace GL {
  15. class Texture2D final : public Texture {
  16. public:
  17. // FIXME: These shouldn't really belong here, they're context specific.
  18. static constexpr u16 MAX_TEXTURE_SIZE = 2048;
  19. static constexpr u8 LOG2_MAX_TEXTURE_SIZE = 11;
  20. class MipMap {
  21. public:
  22. MipMap() = default;
  23. ~MipMap() = default;
  24. void set_width(GLsizei width) { m_width = width; }
  25. void set_height(GLsizei height) { m_height = height; }
  26. GLsizei width() const { return m_width; }
  27. GLsizei height() const { return m_height; }
  28. Vector<u32>& pixel_data() { return m_pixel_data; }
  29. const Vector<u32>& pixel_data() const { return m_pixel_data; }
  30. private:
  31. GLsizei m_width;
  32. GLsizei m_height;
  33. Vector<u32> m_pixel_data;
  34. };
  35. // To quote the Khronos documentation:
  36. // "You could say that a texture object contains a sampler object, which you access through the texture interface."
  37. // FIXME: Better name?
  38. struct TextureSamplerParamaters {
  39. GLint m_min_filter { GL_NEAREST_MIPMAP_LINEAR };
  40. GLint m_mag_filter { GL_LINEAR };
  41. GLint m_wrap_s_mode { GL_REPEAT };
  42. GLint m_wrap_t_mode { GL_REPEAT };
  43. };
  44. public:
  45. Texture2D() = default;
  46. ~Texture2D() { }
  47. virtual bool is_texture_2d() const override { return true; }
  48. void upload_texture_data(GLenum target, GLint lod, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* pixels);
  49. void replace_sub_texture_data(GLint lod, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* data);
  50. FloatVector4 sample_texel(const FloatVector2& uv) const;
  51. GLenum internal_format() const { return m_internal_format; }
  52. private:
  53. template<typename TCallback>
  54. void swizzle(Vector<u32>& pixels, TCallback&& callback)
  55. {
  56. for (auto& pixel : pixels)
  57. pixel = callback(pixel);
  58. }
  59. private:
  60. // FIXME: Mipmaps are currently unused, but we have the plumbing for it at least
  61. Array<MipMap, LOG2_MAX_TEXTURE_SIZE> m_mipmaps;
  62. GLenum m_internal_format;
  63. TextureSamplerParamaters m_sampler_params;
  64. };
  65. }