11#include " stdafx.h"
2- #include " dxtlib.h"
32#include < nvtt/nvtt.h>
43#include " Layers/xrRender/ETextureParams.h"
5- // #include "dds.h"
6-
7- // BOOL APIENTRY DllMain(HANDLE hModule, u32 ul_reason_for_call, LPVOID lpReserved) { return TRUE; }
8- static HFILE gFileOut ;
9- static HFILE gFileIn ;
10-
11- /* const u32 fcc_DXT1 = MAKEFOURCC('D', 'X', 'T', '1');
12- const u32 fcc_DXT2 = MAKEFOURCC('D', 'X', 'T', '2');
13- const u32 fcc_DXT3 = MAKEFOURCC('D', 'X', 'T', '3');
14- const u32 fcc_DXT4 = MAKEFOURCC('D', 'X', 'T', '4');
15- const u32 fcc_DXT5 = MAKEFOURCC('D', 'X', 'T', '5');*/
16-
17- /* void __cdecl WriteDTXnFile(DWORD count, void* buffer, void* userData)
18- {
19- if (count == sizeof(DDS_HEADER))
20- {
21- DDS_HEADER* hdr = (DDS_HEADER*)buffer;
22- if (hdr->dwSize == count)
23- {
24- switch (hdr->ddspf.dwFourCC)
25- {
26- case fcc_DXT1:
27- case fcc_DXT2:
28- case fcc_DXT3:
29- case fcc_DXT4:
30- case fcc_DXT5: hdr->ddspf.dwRGBBitCount = 0; break;
31- }
32- }
33- }
34- _write(gFileOut, buffer, count);
35- }*/
36-
37- class DDSWriter : public nvtt ::OutputHandler
38- {
39- public:
40- HFILE& file;
41-
42- DDSWriter (HFILE& file);
43- virtual void beginImage (int size, int width, int height, int depth, int face, int miplevel) override ;
44- virtual bool writeData (const void * data, int size) override ;
45- };
46-
47- DDSWriter::DDSWriter (HFILE& file) : file(file) {}
48- void DDSWriter::beginImage (int size, int width, int height, int depth, int face, int miplevel) {}
49- bool DDSWriter::writeData (const void * data, int size)
50- {
51- /* if (size == sizeof(DDS_HEADER))
52- {
53- DDS_HEADER* hdr = (DDS_HEADER*)data;
54- if (hdr->dwSize == size)
55- {
56- switch (hdr->ddspf.dwFourCC)
57- {
58- case fcc_DXT1:
59- case fcc_DXT2:
60- case fcc_DXT3:
61- case fcc_DXT4:
62- case fcc_DXT5: hdr->ddspf.dwRGBBitCount = 0; break;
63- }
64- }
65- }*/
66- _write (file, data, size);
67- return true ;
68- }
694
705class DDSErrorHandler : public nvtt ::ErrorHandler
716{
@@ -75,29 +10,10 @@ class DDSErrorHandler : public nvtt::ErrorHandler
7510
7611void DDSErrorHandler::error (nvtt::Error e)
7712{
78- const char * msg;
79- switch (e)
80- {
81- default :
82- case nvtt::Error_Unknown: msg = " Unknown error" ; break ;
83- case nvtt::Error_InvalidInput: msg = " Invalid input" ; break ;
84- case nvtt::Error_UnsupportedFeature: msg = " Unsupported feature" ; break ;
85- case nvtt::Error_CudaError: msg = " CUDA error" ; break ;
86- case nvtt::Error_FileOpen: msg = " File open error" ; break ;
87- case nvtt::Error_FileWrite: msg = " File write error" ; break ;
88- }
89- MessageBox (0 , msg, " DXT compress error" , MB_ICONERROR | MB_OK);
13+ MessageBox (0 , nvtt::errorString (e), " DXT compress error" , MB_ICONERROR | MB_OK);
9014}
9115
92- /* void __cdecl ReadDTXnFile(DWORD count, void* buffer, void* userData) { _read(gFileIn, buffer, count); }
93- HRESULT WriteCompressedData(void* data, int miplevel, u32 size)
94- {
95- _write(gFileOut, data, size);
96- FillMemory(data, size, 0xff);
97- return 0;
98- }*/
99-
100- /* ENGINE_API*/ u32 * Build32MipLevel (u32 & _w, u32 & _h, u32 & _p, u32 * pdwPixelSrc, STextureParams* fmt, float blend)
16+ u32 * Build32MipLevel (u32 & _w, u32 & _h, u32 & _p, u32 * pdwPixelSrc, STextureParams* fmt, float blend)
10117{
10218 R_ASSERT (pdwPixelSrc);
10319 R_ASSERT (_w % 2 == 0 );
@@ -184,57 +100,45 @@ void FillRect(u8* data, u8* new_data, u32 offs, u32 pitch, u32 h, u32 full_pitch
184100int DXTCompressImage (LPCSTR out_name, u8 * raw_data, u32 w, u32 h, u32 pitch, STextureParams* fmt, u32 depth)
185101{
186102 R_ASSERT (0 != w && 0 != h);
187- gFileOut = _open (out_name, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IWRITE);
188- if (gFileOut == -1 )
189- {
190- fprintf (stderr, " Can't open output file %s\n " , out_name);
191- return 0 ;
192- }
193103 bool result = false ;
194104 nvtt::InputOptions inOpt;
195105 auto layout = fmt->type == STextureParams::ttCubeMap ? nvtt::TextureType_Cube : nvtt::TextureType_2D;
196106 inOpt.setTextureLayout (layout, w, h);
197- if (fmt->flags .is (STextureParams::flGenerateMipMaps))
198- inOpt.setMipmapGeneration (true );
199- else
200- inOpt.setMipmapGeneration (false );
107+ inOpt.setMipmapGeneration (fmt->flags .is (STextureParams::flGenerateMipMaps));
201108 inOpt.setWrapMode (nvtt::WrapMode_Clamp);
202109 inOpt.setNormalMap (false );
203110 inOpt.setConvertToNormalMap (false );
204111 inOpt.setGamma (2 .2f , 2 .2f );
205112 inOpt.setNormalizeMipmaps (false );
206113 nvtt::CompressionOptions compOpt;
207114 compOpt.setQuality (nvtt::Quality_Highest);
208- compOpt.setQuantization (
209- fmt->flags .is (STextureParams::flDitherColor), false , fmt->flags .is (STextureParams::flBinaryAlpha));
115+ compOpt.setQuantization (fmt->flags .is (STextureParams::flDitherColor), false , fmt->flags .is (STextureParams::flBinaryAlpha));
210116 switch (fmt->fmt )
211117 {
212- case STextureParams::tfDXT1: compOpt.setFormat (nvtt::Format_DXT1); break ;
118+ case STextureParams::tfDXT1: compOpt.setFormat (nvtt::Format_DXT1 ); break ;
213119 case STextureParams::tfADXT1: compOpt.setFormat (nvtt::Format_DXT1a); break ;
214- case STextureParams::tfDXT3: compOpt.setFormat (nvtt::Format_DXT3); break ;
215- case STextureParams::tfDXT5: compOpt.setFormat (nvtt::Format_DXT5); break ;
216- case STextureParams::tfRGB: compOpt.setFormat (nvtt::Format_RGB); break ;
217- case STextureParams::tfRGBA: compOpt.setFormat (nvtt::Format_RGBA); break ;
120+ case STextureParams::tfDXT3: compOpt.setFormat (nvtt::Format_DXT3 ); break ;
121+ case STextureParams::tfDXT5: compOpt.setFormat (nvtt::Format_DXT5 ); break ;
122+ case STextureParams::tfRGB: compOpt.setFormat (nvtt::Format_RGB ); break ;
123+ case STextureParams::tfRGBA: compOpt.setFormat (nvtt::Format_RGBA ); break ;
218124 }
219125 switch (fmt->mip_filter )
220126 {
221127 case STextureParams::kMIPFilterAdvanced : break ;
222- case STextureParams::kMIPFilterBox : inOpt.setMipmapFilter (nvtt::MipmapFilter_Box); break ;
128+ case STextureParams::kMIPFilterBox : inOpt.setMipmapFilter (nvtt::MipmapFilter_Box ); break ;
223129 case STextureParams::kMIPFilterTriangle : inOpt.setMipmapFilter (nvtt::MipmapFilter_Triangle); break ;
224- case STextureParams::kMIPFilterKaiser : inOpt.setMipmapFilter (nvtt::MipmapFilter_Kaiser); break ;
130+ case STextureParams::kMIPFilterKaiser : inOpt.setMipmapFilter (nvtt::MipmapFilter_Kaiser ); break ;
225131 }
226132 nvtt::OutputOptions outOpt;
227- DDSWriter writer (gFileOut );
228- outOpt.setOutputHandler (&writer);
133+ outOpt.setFileName (out_name);
229134 DDSErrorHandler handler;
230135 outOpt.setErrorHandler (&handler);
231136 if (fmt->flags .is (STextureParams::flGenerateMipMaps) && STextureParams::kMIPFilterAdvanced == fmt->mip_filter )
232137 {
233138 inOpt.setMipmapGeneration (false );
234- u8 * pImagePixels = 0 ;
235139 int numMipmaps = GetPowerOf2Plus1 (__min (w, h));
236140 u32 line_pitch = w * 2 * 4 ;
237- pImagePixels = xr_alloc<u8 >(line_pitch * h);
141+ u8 * pImagePixels = xr_alloc<u8 >(line_pitch * h);
238142 u32 w_offs = 0 ;
239143 u32 dwW = w;
240144 u32 dwH = h;
@@ -255,28 +159,15 @@ int DXTCompressImage(LPCSTR out_name, u8* raw_data, u32 w, u32 h, u32 pitch, STe
255159 w_offs += dwP;
256160 }
257161 xr_free (pLastMip);
258- RGBAImage pImage (w * 2 , h);
259- rgba_t * pixels = pImage.pixels ();
260- u8 * pixel = pImagePixels;
261- for (u32 k = 0 ; k < w * 2 * h; k++, pixel += 4 )
262- {
263- pixels[k].set (pixel[0 ], pixel[1 ], pixel[2 ], pixel[3 ]);
264- }
265- inOpt.setMipmapData (pixels, w, h);
162+ inOpt.setMipmapData (pImagePixels, w, h);
266163 result = nvtt::Compressor ().process (inOpt, compOpt, outOpt);
267164 xr_free (pImagePixels);
268165 }
269166 else
270167 {
271- RGBAImage pImage (w, h);
272- rgba_t * pixels = pImage.pixels ();
273- u8 * pixel = raw_data;
274- for (u32 k = 0 ; k < w * h; k++, pixel += 4 )
275- pixels[k].set (pixel[0 ], pixel[1 ], pixel[2 ], pixel[3 ]);
276- inOpt.setMipmapData (pixels, w, h);
168+ inOpt.setMipmapData (raw_data, w, h);
277169 result = nvtt::Compressor ().process (inOpt, compOpt, outOpt);
278170 }
279- _close (gFileOut );
280171 if (!result)
281172 {
282173 _unlink (out_name);
@@ -285,21 +176,17 @@ int DXTCompressImage(LPCSTR out_name, u8* raw_data, u32 w, u32 h, u32 pitch, STe
285176 return 1 ;
286177}
287178
288- extern int DXTCompressBump (
289- LPCSTR out_name, u8 * raw_data, u8 * normal_map, u32 w, u32 h, u32 pitch, STextureParams* fmt, u32 depth);
179+ extern int DXTCompressBump (LPCSTR out_name, u8 * raw_data, u8 * normal_map, u32 w, u32 h, u32 pitch, STextureParams* fmt, u32 depth);
290180
291- extern " C" __declspec(dllexport) int __stdcall DXTCompress (
292- LPCSTR out_name, u8 * raw_data, u8 * normal_map, u32 w, u32 h, u32 pitch, STextureParams* fmt, u32 depth)
181+ extern " C" __declspec(dllexport) int __stdcall DXTCompress (LPCSTR out_name, u8 * raw_data, u8 * normal_map, u32 w, u32 h, u32 pitch, STextureParams* fmt, u32 depth)
293182{
294183 switch (fmt->type )
295184 {
296185 case STextureParams::ttImage:
297186 case STextureParams::ttCubeMap:
298187 case STextureParams::ttNormalMap:
299188 case STextureParams::ttTerrain: return DXTCompressImage (out_name, raw_data, w, h, pitch, fmt, depth); break ;
300- case STextureParams::ttBumpMap:
301- return DXTCompressBump (out_name, raw_data, normal_map, w, h, pitch, fmt, depth);
302- break ;
189+ case STextureParams::ttBumpMap: return DXTCompressBump (out_name, raw_data, normal_map, w, h, pitch, fmt, depth); break ;
303190 default : NODEFAULT;
304191 }
305192 return -1 ;
0 commit comments