Texture2D.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, GLint, GLenum format, GLenum type, const GLvoid* 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. mip.pixel_data().resize(width * height);
  19. // No pixel data was supplied leave the texture memory uninitialized.
  20. if (pixels == nullptr)
  21. return;
  22. m_internal_format = internal_format;
  23. replace_sub_texture_data(lod, 0, 0, width, height, format, type, pixels, pixels_per_row, byte_alignment);
  24. }
  25. void Texture2D::replace_sub_texture_data(GLuint lod, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* pixels, GLsizei pixels_per_row, u8 byte_alignment)
  26. {
  27. auto& mip = m_mipmaps[lod];
  28. // FIXME: We currently only support GL_UNSIGNED_BYTE and GL_UNSIGNED_SHORT_5_6_5 pixel data
  29. VERIFY(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_SHORT_5_6_5);
  30. VERIFY(xoffset >= 0 && yoffset >= 0 && xoffset + width <= mip.width() && yoffset + height <= mip.height());
  31. VERIFY(pixels_per_row == 0 || pixels_per_row >= xoffset + width);
  32. u8 pixel_size_bytes;
  33. switch (type) {
  34. case GL_UNSIGNED_BYTE:
  35. pixel_size_bytes = (format == GL_RGBA || format == GL_BGRA) ? 4 : 3;
  36. break;
  37. case GL_UNSIGNED_SHORT_5_6_5:
  38. pixel_size_bytes = sizeof(u16);
  39. break;
  40. default:
  41. VERIFY_NOT_REACHED();
  42. }
  43. // Calculate row offset at end to fit alignment
  44. int const physical_width = pixels_per_row > 0 ? pixels_per_row : width;
  45. size_t const physical_width_bytes = physical_width * pixel_size_bytes;
  46. size_t const row_remainder_bytes = (physical_width - width) * pixel_size_bytes
  47. + (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment;
  48. u8 const* pixel_byte_array = reinterpret_cast<u8 const*>(pixels);
  49. auto get_next_pixel = [type, format](u8 const** pixels) -> u32 {
  50. // Split bytes up into RGBA components
  51. u8 c1, c2, c3, c4;
  52. switch (type) {
  53. case GL_UNSIGNED_BYTE:
  54. c1 = *((*pixels)++);
  55. c2 = *((*pixels)++);
  56. c3 = *((*pixels)++);
  57. if (format == GL_RGBA || format == GL_BGRA) {
  58. c4 = *((*pixels)++);
  59. } else {
  60. c4 = 255;
  61. }
  62. break;
  63. case GL_UNSIGNED_SHORT_5_6_5: {
  64. u16 const s = *reinterpret_cast<u16 const*>((*pixels) += 2);
  65. c1 = (s & 0xf800) >> 8;
  66. c2 = (s & 0x7e0) >> 3;
  67. c3 = (s & 0x1f) << 3;
  68. c4 = 255;
  69. break;
  70. }
  71. default:
  72. VERIFY_NOT_REACHED();
  73. }
  74. // Reorder components into BGRA pixel
  75. switch (format) {
  76. case GL_BGR:
  77. case GL_BGRA:
  78. return ((c4 << 24) | (c3 << 16) | (c2 << 8) | c1);
  79. case GL_RGB:
  80. case GL_RGBA:
  81. return ((c4 << 24) | (c1 << 16) | (c2 << 8) | c3);
  82. default:
  83. VERIFY_NOT_REACHED();
  84. }
  85. };
  86. for (auto y = yoffset; y < yoffset + height; y++) {
  87. for (auto x = xoffset; x < xoffset + width; x++) {
  88. u32 pixel = get_next_pixel(&pixel_byte_array);
  89. mip.pixel_data()[y * mip.width() + x] = pixel;
  90. }
  91. pixel_byte_array += row_remainder_bytes;
  92. }
  93. if (!device_image().is_null()) {
  94. SoftGPU::ImageDataLayout layout;
  95. layout.column_stride = pixel_size_bytes;
  96. layout.row_stride = physical_width_bytes + (byte_alignment - physical_width_bytes % byte_alignment) % byte_alignment;
  97. layout.depth_stride = 0;
  98. if (type == GL_UNSIGNED_SHORT_5_6_5) {
  99. layout.format = SoftGPU::ImageFormat::RGB565;
  100. } else if (type == GL_UNSIGNED_BYTE) {
  101. if (format == GL_RGB)
  102. layout.format = SoftGPU::ImageFormat::RGB888;
  103. else if (format == GL_BGR)
  104. layout.format = SoftGPU::ImageFormat::BGR888;
  105. else if (format == GL_RGBA)
  106. layout.format = SoftGPU::ImageFormat::RGBA8888;
  107. else if (format == GL_BGRA)
  108. layout.format = SoftGPU::ImageFormat::BGRA8888;
  109. }
  110. Vector3<unsigned> offset {
  111. static_cast<unsigned>(xoffset),
  112. static_cast<unsigned>(yoffset),
  113. 0
  114. };
  115. Vector3<unsigned> size {
  116. static_cast<unsigned>(width),
  117. static_cast<unsigned>(height),
  118. 1
  119. };
  120. device_image()->write_texels(0, lod, offset, size, pixels, layout);
  121. }
  122. }
  123. }