15int main(
int argc,
char** argv) {
16 FILE *in_file, *out_file;
17 size_t in_size, out_size, dds_size;
19 void *in_host = 0, *out_host = 0, *out_gpu = 0, *in_gpu = 0;
20 unsigned int * header;
21 cudaEvent_t event_begin, event_end;
26 usage(argv[0],
"Incorrect argument count.");
31 size_x = atoi(argv[1]);
32 size_y = atoi(argv[2]);
33 if(size_x <= 0 || (size_x & 3) || size_y == 0 || (
abs(size_y) & 3)) {
34 usage(argv[0],
"Image sizes must be positive numbers divisible by 4.");
40 usage(argv[0],
"Cannot set CUDA device #0.");
45 in_size = size_x *
abs(size_y) * 3;
46 out_size = in_size / 6;
48 cudaMallocHost(&in_host, in_size);
49 cudaMallocHost(&out_host, dds_size);
52 if(!in_host || !out_host || !out_gpu || !in_gpu) {
53 usage(argv[0],
"Cannot allocate buffers.\n");
58 in_file = fopen(argv[3],
"r");
60 usage(argv[0],
"Could not open input file.");
66 fseek(in_file, 0, SEEK_END);
67 if(ftell(in_file) != (
long int)in_size) {
68 usage(argv[0],
"Input file size does not match.");
71 fseek(in_file, 0, SEEK_SET);
75 if(1 != fread(in_host, in_size, 1, in_file)) {
76 usage(argv[0],
"Could not read from input file.");
83 usage(argv[0],
"Could not copy input to GPU.");
88 cudaEventCreate(&event_end);
89 cudaEventCreate(&event_begin);
90 cudaEventRecord(event_begin, 0);
94 usage(argv[0],
"DXT Encoder error.");
99 cudaEventRecord(event_end, 0);
108 cudaEventElapsedTime(&kernel_time_ms, event_begin, event_end);
109 printf(
"DXT1 compression time: %.3f ms.\n", kernel_time_ms);
113 usage(argv[0],
"Could not copy output to host memory.");
122 header = (
unsigned int*)out_host;
123 *(header++) = 0x20534444;
125 *(header++) = 0xA1007;
126 *(header++) =
abs(size_y);
127 *(header++) = size_x;
128 *(header++) = size_x * 2;
144 ((
char*)header)[0] =
'D';
145 ((
char*)header)[1] =
'X';
146 ((
char*)header)[2] =
'T';
147 ((
char*)header)[3] =
'1';
154 *(header++) = 0x1000;
161 out_file = fopen(argv[4],
"w");
163 usage(argv[0],
"Could not open output file for writing.");
168 if(1 != fwrite(out_host, dds_size, 1, out_file)) {
169 usage(argv[0],
"Could not write into output file.");