llcubemaparray.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file llcubemaparray.cpp
  3. * @brief LLCubeMapArray class implementation
  4. *
  5. * $LicenseInfo:firstyear=2022&license=viewergpl$
  6. *
  7. * Copyright (c) 2022, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llcubemaparray.h"
  34. #include "llrender.h"
  35. #include "llvector3.h"
  36. // Defined in llimagegl.cpp
  37. extern void image_bound(U32 width, U32 height, U32 pixformat, U32 count = 1,
  38. U32 texname = 0);
  39. extern void image_unbound(U32 tex_name);
  40. // MUST match order of OpenGL face-layers
  41. GLenum LLCubeMapArray::sTargets[6] =
  42. {
  43. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  44. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  45. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  46. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  47. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  48. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
  49. };
  50. LLVector3 LLCubeMapArray::sLookVecs[6] =
  51. {
  52. LLVector3(1.f, 0.f, 0.f),
  53. LLVector3(-1.f, 0.f, 0.f),
  54. LLVector3(0.f, 1.f, 0.f),
  55. LLVector3(0.f, -1.f, 0.f),
  56. LLVector3(0.f, 0.f, 1.f),
  57. LLVector3(0.f, 0.f, -1.f)
  58. };
  59. LLVector3 LLCubeMapArray::sUpVecs[6] =
  60. {
  61. LLVector3(0.f, -1.f, 0.f),
  62. LLVector3(0.f, -1.f, 0.f),
  63. LLVector3(0.f, 0.f, 1.f),
  64. LLVector3(0.f, 0.f, -1.f),
  65. LLVector3(0.f, -1.f, 0.f),
  66. LLVector3(0.f, -1.f, 0.f)
  67. };
  68. LLVector3 LLCubeMapArray::sClipToCubeLookVecs[6] =
  69. {
  70. LLVector3(0.f, 0.f, -1.f),
  71. LLVector3(0.f, 0.f, 1.f),
  72. LLVector3(1.f, 0.f, 0.f),
  73. LLVector3(1.f, 0.f, 0.f),
  74. LLVector3(1.f, 0.f, 0.f),
  75. LLVector3(-1.f, 0.f, 0.f)
  76. };
  77. LLVector3 LLCubeMapArray::sClipToCubeUpVecs[6] =
  78. {
  79. LLVector3(-1.f, 0.f, 0.f),
  80. LLVector3(1.f, 0.f, 0.f),
  81. LLVector3(0.f, 1.f, 0.f),
  82. LLVector3(0.f, -1.f, 0.f),
  83. LLVector3(0.f, 0.f, -1.f),
  84. LLVector3(0.f, 0.f, 1.f)
  85. };
  86. void LLCubeMapArray::allocate(U32 resolution, U32 components, U32 count,
  87. bool use_mips)
  88. {
  89. mResolution = resolution;
  90. mCount = count;
  91. LLImageGL::generateTextures(1, &mTexName);
  92. mImage = new LLImageGL(resolution, resolution, components, use_mips);
  93. mImage->setTexName(mTexName);
  94. mImage->setTarget(sTargets[0], LLTexUnit::TT_CUBE_MAP_ARRAY);
  95. mImage->setUseMipMaps(use_mips);
  96. mImage->setHasMipMaps(use_mips);
  97. bind(0);
  98. U32 format = components == 4 ? GL_RGBA16F : GL_RGB16F;
  99. // Account for the actual cube map array size.
  100. image_unbound(mTexName);
  101. image_bound(resolution, resolution, format, 6 * count, mTexName);
  102. U32 mip = 0;
  103. while (resolution >= 1)
  104. {
  105. glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, mip, format, resolution,
  106. resolution, count * 6, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  107. NULL);
  108. if (!use_mips)
  109. {
  110. break;
  111. }
  112. resolution /= 2;
  113. ++mip;
  114. }
  115. mImage->setAddressMode(LLTexUnit::TAM_CLAMP);
  116. if (use_mips)
  117. {
  118. mImage->setFilteringOption(LLTexUnit::TFO_ANISOTROPIC);
  119. #if 0 // Latest AMD drivers do not appreciate this method of allocating
  120. // mipmaps
  121. glGenerateMipmap(GL_TEXTURE_CUBE_MAP_ARRAY);
  122. #endif
  123. }
  124. else
  125. {
  126. mImage->setFilteringOption(LLTexUnit::TFO_BILINEAR);
  127. }
  128. unbind();
  129. }
  130. void LLCubeMapArray::destroyGL()
  131. {
  132. image_unbound(mTexName);
  133. mTexName = 0;
  134. mImage = NULL;
  135. }
  136. void LLCubeMapArray::bind(S32 stage)
  137. {
  138. mTextureStage = stage;
  139. gGL.getTexUnit(stage)->bindManual(LLTexUnit::TT_CUBE_MAP_ARRAY,
  140. getGLName(), mImage->getUseMipMaps());
  141. }
  142. void LLCubeMapArray::unbind()
  143. {
  144. gGL.getTexUnit(mTextureStage)->unbind(LLTexUnit::TT_CUBE_MAP_ARRAY);
  145. mTextureStage = -1;
  146. }