Texture2D.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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(0, 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. // NOTE: Some target, format, and internal formats are currently unsupported.
  19. // Considering we control this library, and `gl.h` itself, we don't need to add any
  20. // checks here to see if we support them; the program will simply fail to compile..
  21. auto& mip = m_mipmaps[lod];
  22. m_internal_format = internal_format;
  23. mip.set_width(input_layout.selection.width);
  24. mip.set_height(input_layout.selection.height);
  25. // No pixel data was supplied; leave the texture memory uninitialized.
  26. if (pixels == nullptr)
  27. return;
  28. replace_sub_texture_data(lod, input_layout, { 0, 0, 0 }, pixels);
  29. }
  30. void Texture2D::replace_sub_texture_data(GLuint lod, GPU::ImageDataLayout input_layout, Vector3<i32> const& output_offset, GLvoid const* pixels)
  31. {
  32. // FIXME: We currently depend on the first glTexImage2D call to attach an image to mipmap level 0, which initializes the GPU image
  33. // Ideally we would create separate GPU images for each level and merge them into a final image
  34. // once used for rendering for the first time.
  35. VERIFY(!device_image().is_null());
  36. device_image()->write_texels(0, lod, output_offset, pixels, input_layout);
  37. }
  38. }