Texture2D.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGL/GL/gl.h>
  8. #include <LibGL/Tex/Texture2D.h>
  9. namespace GL {
  10. void Texture2D::upload_texture_data(GLuint lod, GLint internal_format, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid const* pixels, GLsizei pixels_per_row, u8 byte_alignment)
  11. {
  12. // NOTE: Some target, format, and internal formats are currently unsupported.
  13. // Considering we control this library, and `gl.h` itself, we don't need to add any
  14. // checks here to see if we support them; the program will simply fail to compile..
  15. auto& mip = m_mipmaps[lod];
  16. mip.set_width(width);
  17. mip.set_height(height);
  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, 0, 0, width, height, format, type, pixels, pixels_per_row, byte_alignment);
  23. }
  24. void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid const* pixels, GLsizei pixels_per_row, u8 byte_alignment)
  25. {
  26. auto& mip = m_mipmaps[lod];
  27. // FIXME: We currently only support GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5 pixel data
  28. VERIFY(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5);
  29. VERIFY(xoffset >= 0 && yoffset >= 0 && xoffset + width <= mip.width() && yoffset + height <= mip.height());
  30. VERIFY(pixels_per_row == 0 || pixels_per_row >= xoffset + width);
  31. // FIXME: We currently depend on the first glTexImage2D call to attach an image to mipmap level 0, which initializes the GPU image
  32. // Ideally we would create separate GPU images for each level and merge them into a final image
  33. // once used for rendering for the first time.
  34. if (device_image().is_null())
  35. return;
  36. u8 pixel_size_bytes;
  37. switch (type) {
  38. case GL_UNSIGNED_BYTE:
  39. pixel_size_bytes = (format == GL_RGBA || format == GL_BGRA) ? 4 : 3;
  40. break;
  41. case GL_UNSIGNED_SHORT_5_6_5:
  42. pixel_size_bytes = sizeof(u16);
  43. break;
  44. default:
  45. VERIFY_NOT_REACHED();
  46. }
  47. // Calculate row offset at end to fit alignment
  48. int const physical_width = pixels_per_row > 0 ? pixels_per_row : width;
  49. size_t const physical_width_bytes = physical_width * pixel_size_bytes;
  50. GPU::ImageDataLayout layout;
  51. layout.column_stride = pixel_size_bytes;
  52. layout.row_stride = physical_width_bytes + (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment;
  53. layout.depth_stride = 0;
  54. if (type == GL_UNSIGNED_SHORT_5_6_5) {
  55. layout.format = GPU::ImageFormat::RGB565;
  56. } else if (type == GL_UNSIGNED_BYTE) {
  57. if (format == GL_RGB)
  58. layout.format = GPU::ImageFormat::RGB888;
  59. else if (format == GL_BGR)
  60. layout.format = GPU::ImageFormat::BGR888;
  61. else if (format == GL_RGBA)
  62. layout.format = GPU::ImageFormat::RGBA8888;
  63. else if (format == GL_BGRA)
  64. layout.format = GPU::ImageFormat::BGRA8888;
  65. }
  66. Vector3<unsigned> offset {
  67. static_cast<unsigned>(xoffset),
  68. static_cast<unsigned>(yoffset),
  69. 0
  70. };
  71. Vector3<unsigned> size {
  72. static_cast<unsigned>(width),
  73. static_cast<unsigned>(height),
  74. 1
  75. };
  76. device_image()->write_texels(0, lod, offset, size, pixels, layout);
  77. }
  78. }