il_nvidia.cpp
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 6k
Development Platform:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Sources
  4. // Copyright (C) 2000-2009 by Denton Woods
  5. // Last modified: 01/19/2009
  6. //
  7. // Filename: src-IL/src/il_nvidia.cpp
  8. //
  9. // Description: Implements access to the nVidia Texture Tools library.
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "il_internal.h"
  13. #include "il_dds.h"
  14. #include "il_manip.h"
  15. #include <limits.h>
  16. #ifdef IL_USE_DXTC_NVIDIA
  17. #include <nvtt/nvtt.h>
  18. #include <nvcore/Memory.h>
  19. using namespace nvtt;
  20. #if defined(_WIN32) && defined(IL_USE_PRAGMA_LIBS)
  21. #if defined(_MSC_VER) || defined(__BORLANDC__)
  22. #ifndef _DEBUG
  23. #pragma comment(lib, "nvcore.lib")
  24. #pragma comment(lib, "nvtt.lib")
  25. #else
  26. #pragma comment(lib, "nvcore-d.lib")
  27. #pragma comment(lib, "nvtt-d.lib")
  28. #endif
  29. #endif
  30. #endif
  31. struct ilOutputHandlerMem : public nvtt::OutputHandler
  32. {
  33. ilOutputHandlerMem(ILuint Width, ILuint Height, ILenum DxtType)
  34. {
  35. Width = Width + (4 - (Width % 4)) % 4;    // Operates on 4x4 blocks,
  36. Height = Height + (4 - (Height % 4)) % 4; //  so gives extra room.
  37. switch (DxtType)
  38. {
  39. case IL_DXT1:
  40. case IL_DXT1A:
  41. Size = Width * Height / 2;
  42. break;
  43. case IL_DXT3:
  44. case IL_DXT5:
  45. Size = Width * Height;
  46. break;
  47. default:  // NVTT does not accept DXT2 or DXT4.
  48. // Should error somehow...
  49. break;
  50. }
  51. NewData = (ILubyte*)ialloc(Size);
  52. if (NewData == NULL)
  53. return;
  54. Temp = NewData;
  55. }
  56. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  57. {
  58. // ignore.
  59. }
  60. virtual bool writeData(const void *data, int size)
  61. {
  62. memcpy(Temp, data, size);
  63. Temp += size;
  64. return true;
  65. }
  66. ILubyte *NewData, *Temp;
  67. ILuint Size;
  68. };
  69. //! Compresses data to a DXT format using nVidia's Texture Tools library.
  70. //  The data must be in unsigned byte RGBA format.  The alpha channel will be ignored if DxtType is IL_DXT1.
  71. //  DxtSize is used to return the size in bytes of the DXTC data returned.
  72. ILAPI ILubyte* ILAPIENTRY ilNVidiaCompressDXT(ILubyte *Data, ILuint Width, ILuint Height, ILuint Depth, ILenum DxtFormat, ILuint *DxtSize)
  73. {
  74. if (Data == NULL) {  // We cannot operate on a null pointer.
  75. ilSetError(IL_INVALID_PARAM);
  76. return NULL;
  77. }
  78. // The nVidia Texture Tools library does not support volume textures yet.
  79. if (Depth != 1) {
  80. ilSetError(IL_INVALID_PARAM);
  81. return NULL;
  82. }
  83. InputOptions inputOptions;
  84. inputOptions.setTextureLayout(TextureType_2D, Width, Height);
  85. inputOptions.setMipmapData(Data, Width, Height);
  86. inputOptions.setMipmapGeneration(false, -1);  //@TODO: Use this in certain cases.
  87. OutputOptions outputOptions;
  88. ilOutputHandlerMem outputHandler(Width, Height, DxtFormat);
  89. outputOptions.setOutputHeader(false);
  90. outputOptions.setOutputHandler(&outputHandler);
  91. if (outputHandler.NewData == NULL)
  92. return NULL;
  93. CompressionOptions compressionOptions;
  94. switch (DxtFormat)
  95. {
  96. case IL_DXT1:
  97. compressionOptions.setFormat(Format_DXT1);
  98. break;
  99. case IL_DXT1A:
  100. compressionOptions.setFormat(Format_DXT1a);
  101. break;
  102. case IL_DXT3:
  103. compressionOptions.setFormat(Format_DXT1);
  104. break;
  105. case IL_DXT5:
  106. compressionOptions.setFormat(Format_DXT5);
  107. break;
  108. default:  // Does not support DXT2 or DXT4.
  109. ilSetError(IL_INVALID_PARAM);
  110. break;
  111. }
  112. Compressor compressor;
  113. compressor.process(inputOptions, compressionOptions, outputOptions);
  114. *DxtSize = outputHandler.Size;
  115. return outputHandler.NewData;
  116. }
  117. //
  118. //
  119. // The following is just a repeat of above, but it works generically on file streams or lumps.
  120. //  @TODO: Merge these two together.
  121. //
  122. //
  123. struct ilOutputHandlerFile : public nvtt::OutputHandler
  124. {
  125. ilOutputHandlerFile(ILuint Width, ILuint Height, ILenum DxtType)
  126. {
  127. return;
  128. }
  129. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  130. {
  131. // ignore.
  132. }
  133. virtual bool writeData(const void *data, int size)
  134. {
  135. if (iwrite(data, 1, size) == size)
  136. return true;
  137. return false;
  138. }
  139. };
  140. //! Compresses data to a DXT format using nVidia's Texture Tools library.
  141. //  This version is supposed to be completely internal to DevIL.
  142. //  The data must be in unsigned byte RGBA format.  The alpha channel will be ignored if DxtType is IL_DXT1.
  143. ILuint ilNVidiaCompressDXTFile(ILubyte *Data, ILuint Width, ILuint Height, ILuint Depth, ILenum DxtFormat)
  144. {
  145. ILuint FilePos = itellw();
  146. // The nVidia Texture Tools library does not support volume textures yet.
  147. if (Depth != 1) {
  148. ilSetError(IL_INVALID_PARAM);
  149. return 0;
  150. }
  151. InputOptions inputOptions;
  152. inputOptions.setTextureLayout(TextureType_2D, Width, Height);
  153. inputOptions.setMipmapData(Data, Width, Height);
  154. inputOptions.setMipmapGeneration(false, -1);  //@TODO: Use this in certain cases.
  155. OutputOptions outputOptions;
  156. ilOutputHandlerFile outputHandler(Width, Height, DxtFormat);
  157. outputOptions.setOutputHeader(false);
  158. outputOptions.setOutputHandler(&outputHandler);
  159. CompressionOptions compressionOptions;
  160. switch (DxtFormat)
  161. {
  162. case IL_DXT1:
  163. compressionOptions.setFormat(Format_DXT1);
  164. break;
  165. case IL_DXT1A:
  166. compressionOptions.setFormat(Format_DXT1a);
  167. break;
  168. case IL_DXT3:
  169. compressionOptions.setFormat(Format_DXT1);
  170. break;
  171. case IL_DXT5:
  172. compressionOptions.setFormat(Format_DXT5);
  173. break;
  174. default:  // Does not support DXT2 or DXT4.
  175. ilSetError(IL_INVALID_PARAM);
  176. break;
  177. }
  178. Compressor compressor;
  179. compressor.process(inputOptions, compressionOptions, outputOptions);
  180. return itellw() - FilePos;  // Return the number of characters written.
  181. }
  182. #else
  183. // Let's have this so that the function is always created and exported, even if it does nothing.
  184. ILAPI ILubyte* ILAPIENTRY ilNVidiaCompressDXT(ILubyte *Data, ILuint Width, ILuint Height, ILuint Depth, ILenum DxtFormat, ILuint *DxtSize)
  185. {
  186. //@TODO: Do we need to set an error message?
  187. return NULL;
  188. }
  189. #endif//IL_NO_DXTC_NVIDIA