Texture2D.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <LibGL/GL/gl.h>
  8. #include <LibGL/Tex/Texture2D.h>
  9. #include <string.h>
  10. namespace GL {
  11. void Texture2D::upload_texture_data(GLenum, GLint lod, GLint internal_format, GLsizei width, GLsizei height, GLint, GLenum format, GLenum, const GLvoid* pixels)
  12. {
  13. // NOTE: Some target, format, and internal formats are currently unsupported.
  14. // Considering we control this library, and `gl.h` itself, we don't need to add any
  15. // checks here to see if we support them; the program will simply fail to compile..
  16. // Somebody passed us in nullptr...
  17. // Apparently this allocates memory on the GPU (according to Khronos docs..)?
  18. if (pixels == nullptr) {
  19. dbgln("LibGL: pixels == nullptr when uploading texture data.");
  20. VERIFY_NOT_REACHED();
  21. }
  22. m_internal_format = internal_format;
  23. // Get reference to the mip
  24. auto& mip = m_mipmaps[lod];
  25. const u8* pixel_byte_array = reinterpret_cast<const u8*>(pixels);
  26. // Copy pixel data to storage
  27. // Pixels are already 32-bits wide
  28. if (format == GL_RGBA || format == GL_BGRA) {
  29. mip.pixel_data().resize(width * height * sizeof(u32));
  30. memcpy(mip.pixel_data().data(), pixels, width * height * sizeof(u32));
  31. } else {
  32. mip.pixel_data().resize(width * height * 3);
  33. // Copy RGB or BGR pixel data
  34. for (auto i = 0; i < width * height * 3; i += 3) {
  35. u32 b0 = pixel_byte_array[i]; // B or R
  36. u32 b1 = pixel_byte_array[i + 1]; // G
  37. u32 b2 = pixel_byte_array[i + 2]; // R or B
  38. u32 pixel = ((0xffu << 24) | (b0 << 16) | (b1 << 8) | b2);
  39. mip.pixel_data().append(pixel);
  40. }
  41. }
  42. // Now we need to swizzle the texture data from `format` to `internal_format`
  43. switch (format) {
  44. case GL_BGR: {
  45. if (internal_format == GL_RGB) {
  46. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  47. u8 r = pixel & 0xff;
  48. u8 g = (pixel >> 8) & 0xff;
  49. u8 b = (pixel >> 16) & 0xff;
  50. return (0xff << 24) | (r << 16) | (g << 8) | b;
  51. });
  52. } else if (internal_format == GL_RGBA) {
  53. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  54. u8 r = pixel & 0xff;
  55. u8 g = (pixel >> 8) & 0xff;
  56. u8 b = (pixel >> 16) & 0xff;
  57. return (r << 24) | (g << 16) | (b << 8) | 0xff;
  58. });
  59. }
  60. } break;
  61. case GL_BGRA: {
  62. if (internal_format == GL_RGB) {
  63. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  64. u8 r = (pixel >> 8) & 0xff;
  65. u8 g = (pixel >> 16) & 0xff;
  66. u8 b = (pixel >> 24) & 0xff;
  67. return (0xff << 24) | (r << 16) | (g << 8) | b;
  68. });
  69. } else if (internal_format == GL_RGBA) {
  70. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  71. u8 a = pixel & 0xff;
  72. u8 r = (pixel >> 8) & 0xff;
  73. u8 g = (pixel >> 16) & 0xff;
  74. u8 b = (pixel >> 24) & 0xff;
  75. return (r << 24) | (g << 16) | (b << 8) | a;
  76. });
  77. }
  78. } break;
  79. case GL_RGB: {
  80. if (internal_format == GL_RGBA) {
  81. swizzle(mip.pixel_data(), [](u32 pixel) -> u32 {
  82. u8 r = pixel & 0xff;
  83. u8 g = (pixel >> 8) & 0xff;
  84. u8 b = (pixel >> 16) & 0xff;
  85. return (r << 24) | (g << 16) | (b << 8) | 0xff;
  86. });
  87. }
  88. } break;
  89. case GL_RGBA:
  90. break;
  91. default:
  92. // Let's crash for now so we can implement format by format
  93. VERIFY_NOT_REACHED();
  94. }
  95. mip.set_width(width);
  96. mip.set_height(height);
  97. }
  98. FloatVector4 Texture2D::sample_texel(const FloatVector2& uv) const
  99. {
  100. auto& mip = m_mipmaps.at(0);
  101. // FIXME: Remove this to prevent a crash when we have proper texture binding
  102. if (mip.width() == 0 || mip.height() == 0)
  103. return { 1.0f, 1.0f, 1.0f, 1.0f };
  104. u32 u = static_cast<u32>(uv.x() * mip.width());
  105. u32 v = static_cast<u32>(uv.y() * mip.height());
  106. u32 pixel = mip.pixel_data().at(v * mip.width() + u);
  107. float b0 = ((pixel)&0xff) / 255.0f;
  108. float b1 = ((pixel >> 8) & 0xff) / 255.0f;
  109. float b2 = ((pixel >> 16) & 0xff) / 255.0f;
  110. return { b0, b1, b2, 1.0f };
  111. }
  112. }