Texture2D.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGL/GL/gl.h>
  9. #include <LibGL/Tex/Texture2D.h>
  10. namespace GL {
  11. void Texture2D::download_texture_data(GLuint lod, GPU::ImageDataLayout output_layout, GLvoid* pixels)
  12. {
  13. VERIFY(!device_image().is_null());
  14. device_image()->read_texels(lod, { 0, 0, 0 }, pixels, output_layout);
  15. }
  16. void Texture2D::upload_texture_data(GLuint lod, GLenum internal_format, GPU::ImageDataLayout input_layout, GLvoid const* pixels)
  17. {
  18. m_internal_format = internal_format;
  19. // No pixel data was supplied; leave the texture memory uninitialized.
  20. if (pixels == nullptr)
  21. return;
  22. replace_sub_texture_data(lod, input_layout, { 0, 0, 0 }, pixels);
  23. if (lod == 0 && m_generate_mipmaps)
  24. device_image()->regenerate_mipmaps();
  25. }
  26. void Texture2D::replace_sub_texture_data(GLuint lod, GPU::ImageDataLayout input_layout, Vector3<i32> const& output_offset, GLvoid const* pixels)
  27. {
  28. // FIXME: We currently depend on the first glTexImage2D call to attach an image to mipmap level 0, which initializes the GPU image
  29. // Ideally we would create separate GPU images for each level and merge them into a final image
  30. // once used for rendering for the first time.
  31. VERIFY(!device_image().is_null());
  32. device_image()->write_texels(lod, output_offset, pixels, input_layout);
  33. if (lod == 0 && m_generate_mipmaps)
  34. device_image()->regenerate_mipmaps();
  35. }
  36. void Texture2D::set_generate_mipmaps(bool generate_mipmaps)
  37. {
  38. if (m_generate_mipmaps == generate_mipmaps)
  39. return;
  40. m_generate_mipmaps = generate_mipmaps;
  41. if (generate_mipmaps && !device_image().is_null())
  42. device_image()->regenerate_mipmaps();
  43. }
  44. }