Texture2D.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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)
  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. // Somebody passed us in nullptr...
  18. // Apparently this allocates memory on the GPU (according to Khronos docs..)?
  19. if (pixels == nullptr) {
  20. dbgln("LibGL: pixels == nullptr when uploading texture data.");
  21. VERIFY_NOT_REACHED();
  22. }
  23. m_internal_format = internal_format;
  24. // Get reference to the mip
  25. auto& mip = m_mipmaps[lod];
  26. const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
  27. // Copy pixel data to storage
  28. // Pixels are already 32-bits wide
  29. if (format == GL_RGBA || format == GL_BGRA) {
  30. mip.pixel_data().resize(width * height * sizeof(u32));
  31. memcpy(mip.pixel_data().data(), pixels, width * height * sizeof(u32));
  32. } else {
  33. mip.pixel_data().resize(width * height * 3);
  34. // Copy RGB or BGR pixel data
  35. for (auto i = 0; i < width * height * 3; i += 3) {
  36. u32 b0 = pixel_byte_array[i]; // B or R
  37. u32 b1 = pixel_byte_array[i + 1]; // G
  38. u32 b2 = pixel_byte_array[i + 2]; // R or B
  39. u32 pixel = ((0xffu << 24) | (b0 << 16) | (b1 << 8) | b2);
  40. mip.pixel_data().append(pixel);
  41. }
  42. }
  43. // Now we need to swizzle the texture data from `format` to `internal_format`
  44. switch (format) {
  45. case GL_BGR: {
  46. if (internal_format == GL_RGB) {
  47. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  48. u8 r = pixel & 0xff;
  49. u8 g = (pixel >> 8) & 0xff;
  50. u8 b = (pixel >> 16) & 0xff;
  51. return (0xff << 24) | (r << 16) | (g << 8) | b;
  52. });
  53. } else if (internal_format == GL_RGBA) {
  54. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  55. u8 r = pixel & 0xff;
  56. u8 g = (pixel >> 8) & 0xff;
  57. u8 b = (pixel >> 16) & 0xff;
  58. return (r << 24) | (g << 16) | (b << 8) | 0xff;
  59. });
  60. }
  61. } break;
  62. case GL_BGRA: {
  63. if (internal_format == GL_RGB) {
  64. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  65. u8 r = (pixel >> 8) & 0xff;
  66. u8 g = (pixel >> 16) & 0xff;
  67. u8 b = (pixel >> 24) & 0xff;
  68. return (0xff << 24) | (r << 16) | (g << 8) | b;
  69. });
  70. } else if (internal_format == GL_RGBA) {
  71. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  72. u8 a = pixel & 0xff;
  73. u8 r = (pixel >> 8) & 0xff;
  74. u8 g = (pixel >> 16) & 0xff;
  75. u8 b = (pixel >> 24) & 0xff;
  76. return (r << 24) | (g << 16) | (b << 8) | a;
  77. });
  78. }
  79. } break;
  80. case GL_RGB: {
  81. if (internal_format == GL_RGBA) {
  82. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  83. u8 r = pixel & 0xff;
  84. u8 g = (pixel >> 8) & 0xff;
  85. u8 b = (pixel >> 16) & 0xff;
  86. return (r << 24) | (g << 16) | (b << 8) | 0xff;
  87. });
  88. }
  89. } break;
  90. case GL_RGBA:
  91. break;
  92. default:
  93. // Let's crash for now so we can implement format by format
  94. VERIFY_NOT_REACHED();
  95. }
  96. mip.set_width(width);
  97. mip.set_height(height);
  98. }
  99. MipMap const& Texture2D::mipmap(unsigned lod) const
  100. {
  101. if (lod >= m_mipmaps.size())
  102. return m_mipmaps.at(m_mipmaps.size() - 1);
  103. return m_mipmaps.at(lod);
  104. }
  105. }