Texture2D.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <AK/Format.h>
  8. #include <LibGL/GL/gl.h>
  9. #include <LibGL/Tex/Texture2D.h>
  10. #include <string.h>
  11. namespace GL {
  12. void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GLsizei width, GLsizei height, GLint, GLenum format, GLenum, const GLvoid* pixels, size_t pixels_per_row)
  13. {
  14. // NOTE: Some target, format, and internal formats are currently unsupported.
  15. // Considering we control this library, and `gl.h` itself, we don't need to add any
  16. // checks here to see if we support them; the program will simply fail to compile..
  17. auto& mip = m_mipmaps[lod];
  18. mip.set_width(width);
  19. mip.set_height(height);
  20. // No pixel data was supplied. Just allocate texture memory and leave it uninitialized.
  21. if (pixels == nullptr) {
  22. mip.pixel_data().resize(width * height);
  23. return;
  24. }
  25. m_internal_format = internal_format;
  26. const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
  27. mip.pixel_data().clear();
  28. if (format == GL_RGBA) {
  29. for (auto y = 0; y < height; y++) {
  30. for (auto x = 0; x < width; x++) {
  31. u32 r = *pixel_byte_array++;
  32. u32 g = *pixel_byte_array++;
  33. u32 b = *pixel_byte_array++;
  34. u32 a = *pixel_byte_array++;
  35. u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
  36. mip.pixel_data().append(pixel);
  37. }
  38. if (pixels_per_row > 0) {
  39. pixel_byte_array += (pixels_per_row - width) * 4;
  40. }
  41. }
  42. } else if (format == GL_BGRA) {
  43. for (auto y = 0; y < height; y++) {
  44. for (auto x = 0; x < width; x++) {
  45. u32 b = *pixel_byte_array++;
  46. u32 g = *pixel_byte_array++;
  47. u32 r = *pixel_byte_array++;
  48. u32 a = *pixel_byte_array++;
  49. u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
  50. mip.pixel_data().append(pixel);
  51. }
  52. if (pixels_per_row > 0) {
  53. pixel_byte_array += (pixels_per_row - width) * 4;
  54. }
  55. }
  56. } else if (format == GL_BGR) {
  57. for (auto y = 0; y < height; y++) {
  58. for (auto x = 0; x < width; x++) {
  59. u32 b = *pixel_byte_array++;
  60. u32 g = *pixel_byte_array++;
  61. u32 r = *pixel_byte_array++;
  62. u32 a = 255;
  63. u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
  64. mip.pixel_data().append(pixel);
  65. }
  66. if (pixels_per_row > 0) {
  67. pixel_byte_array += (pixels_per_row - width) * 3;
  68. }
  69. }
  70. } else if (format == GL_RGB) {
  71. for (auto y = 0; y < height; y++) {
  72. for (auto x = 0; x < width; x++) {
  73. u32 r = *pixel_byte_array++;
  74. u32 g = *pixel_byte_array++;
  75. u32 b = *pixel_byte_array++;
  76. u32 a = 255;
  77. u32 pixel = ((a << 24) | (r << 16) | (g << 8) | b);
  78. mip.pixel_data().append(pixel);
  79. }
  80. if (pixels_per_row > 0) {
  81. pixel_byte_array += (pixels_per_row - width) * 3;
  82. }
  83. }
  84. } else {
  85. VERIFY_NOT_REACHED();
  86. }
  87. }
  88. MipMap const& Texture2D::mipmap(unsigned lod) const
  89. {
  90. if (lod >= m_mipmaps.size())
  91. return m_mipmaps.at(m_mipmaps.size() - 1);
  92. return m_mipmaps.at(lod);
  93. }
  94. }