UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
rgb2dxt6.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include "cuda_dxt.h"
4
5
6static void usage(const char * const name, const char * const message) {
7 printf("ERROR: %s\n", message);
8 printf("Usage: %s width height input.rgb output.yog\n", name);
9}
10
11
12int main(int argc, char** argv) {
13 FILE *in_file, *out_file;
14 size_t in_size, out_size;
15 int size_x, size_y;
16 void *in_host = 0, *out_host = 0, *out_gpu = 0, *in_gpu = 0;
17 unsigned int * header;
18 cudaEvent_t event_begin, event_end;
19 float kernel_time_ms;
20
21 /* check arguments */
22 if(5 != argc) {
23 usage(argv[0], "Incorrect argument count.");
24 return -1;
25 }
26
27 /* get image size */
28 size_x = atoi(argv[1]);
29 size_y = atoi(argv[2]);
30 if(size_x <= 0 || (size_x & 3) || size_y == 0 || (abs(size_y) & 3)) {
31 usage(argv[0], "Image sizes must be positive numbers divisible by 4.");
32 return -1;
33 }
34
35 /* Check CUDA device */
36 if(cudaSuccess != cudaSetDevice(0)) {
37 usage(argv[0], "Cannot set CUDA device #0.");
38 return -1;
39 }
40
41 /* allocate buffers (both GPU and host buffers) */
42 out_size = size_x * abs(size_y);
43 in_size = out_size * 3;
44 cudaMallocHost(&in_host, in_size);
45 cudaMallocHost(&out_host, out_size);
46 cudaMalloc(&in_gpu, in_size);
47 cudaMalloc(&out_gpu, out_size);
48 if(!in_host || !out_host || !out_gpu || !in_gpu) {
49 usage(argv[0], "Cannot allocate buffers.\n");
50 return -1;
51 }
52
53 /* open input file */
54 in_file = fopen(argv[3], "r");
55 if(!in_file) {
56 usage(argv[0], "Could not open input file.");
57 return -1;
58 }
59
60#if 0
61 /* check file size */
62 fseek(in_file, 0, SEEK_END);
63 if(ftell(in_file) != (long int)in_size) {
64 usage(argv[0], "Input file size does not match.");
65 return -1;
66 }
67 fseek(in_file, 0, SEEK_SET);
68#endif
69
70 /* load data */
71 if(1 != fread(in_host, in_size, 1, in_file)) {
72 usage(argv[0], "Could not read from input file.");
73 return -1;
74 }
75 fclose(in_file);
76
77 /* copy data into GPU buffer */
78 if(cudaSuccess != cudaMemcpy(in_gpu, in_host, in_size, cudaMemcpyHostToDevice)) {
79 usage(argv[0], "Could not copy input to GPU.");
80 return -1;
81 }
82
83 /* prepare envents for time emasurement and begin */
84 cudaEventCreate(&event_end);
85 cudaEventCreate(&event_begin);
86 cudaEventRecord(event_begin, 0);
87
88 /* compress */
89 if(cuda_rgb_to_dxt6(in_gpu, out_gpu, size_x, size_y, 0)) {
90 usage(argv[0], "DXT Encoder error.");
91 return -1;
92 }
93
94 /* measure kernel time */
95 cudaEventRecord(event_end, 0);
96
97 /* check kernel call */
98 if(cudaSuccess != cudaDeviceSynchronize()) {
100 return -1;
101 }
102
103 /* print the time */
104 cudaEventElapsedTime(&kernel_time_ms, event_begin, event_end);
105 printf("DXT6 compression time: %.3f ms.\n", kernel_time_ms);
106
107 /* copy back to host memory */
108 if(cudaSuccess != cudaMemcpy((char*)out_host, out_gpu, out_size, cudaMemcpyDeviceToHost)) {
109 usage(argv[0], "Could not copy output to host memory.");
110 return -1;
111 }
112
113 /* open output file */
114 out_file = fopen(argv[4], "w");
115 if(!out_file) {
116 usage(argv[0], "Could not open output file for writing.");
117 return -1;
118 }
119
120 /* write output into the file */
121 if(1 != fwrite(out_host, out_size, 1, out_file)) {
122 usage(argv[0], "Could not write into output file.");
123 return -1;
124 }
125 fclose(out_file);
126
127 /* free buffers */
128 cudaFreeHost(in_host);
129 cudaFreeHost(out_host);
130 cudaFree(in_gpu);
131 cudaFree(out_gpu);
132
133 /* indicate success */
134 return 0;
135}
136
static __device__ vec2 abs(const vec2 &v)
Definition cuda_dxt.cu:118
int cuda_rgb_to_dxt6(const void *src, void *out, int size_x, int size_y, cuda_wrapper_stream_t stream)
Definition cuda_dxt.cu:816
Interface of DXT compression CUDA implementation.
#define cudaMemcpy
#define cudaSuccess
#define cudaFree
#define cudaMemcpyHostToDevice
#define cudaGetLastError
#define cudaMalloc
#define cudaFreeHost
#define cudaGetErrorString
void usage()
Definition main.cpp:28
const char * name
Definition settings.cpp:193