File: postProcessGL.cu

package info (click to toggle)
nvidia-cuda-samples 12.4.1~dfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 313,216 kB
  • sloc: cpp: 82,042; makefile: 53,971; xml: 15,381; ansic: 8,630; sh: 91; python: 74
file content (256 lines) | stat: -rw-r--r-- 7,774 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of NVIDIA CORPORATION nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// Utilities and system includes

#include <cooperative_groups.h>

namespace cg = cooperative_groups;

#include <helper_cuda.h>

cudaTextureObject_t inTexObject;

// clamp x to range [a, b]
__device__ float clamp(float x, float a, float b) { return max(a, min(b, x)); }

__device__ int clamp(int x, int a, int b) { return max(a, min(b, x)); }

// convert floating point rgb color to 8-bit integer
__device__ int rgbToInt(float r, float g, float b) {
  r = clamp(r, 0.0f, 255.0f);
  g = clamp(g, 0.0f, 255.0f);
  b = clamp(b, 0.0f, 255.0f);
  return (int(b) << 16) | (int(g) << 8) | int(r);
}

// get pixel from 2D image, with clamping to border
__device__ uchar4 getPixel(int x, int y, cudaTextureObject_t inTex) {
#ifndef USE_TEXTURE_RGBA8UI
  float4 res = tex2D<float4>(inTex, x, y);
  uchar4 ucres = make_uchar4(res.x * 255.0f, res.y * 255.0f, res.z * 255.0f,
                             res.w * 255.0f);
#else
  uchar4 ucres = tex2D<uchar4>(inTex, x, y);
#endif
  return ucres;
}

// macros to make indexing shared memory easier
#define SMEM(X, Y) sdata[(Y)*tilew + (X)]

/*
    2D convolution using shared memory
    - operates on 8-bit RGB data stored in 32-bit int
    - assumes kernel radius is less than or equal to block size
    - not optimized for performance
     _____________
    |   :     :   |
    |_ _:_____:_ _|
    |   |     |   |
    |   |     |   |
    |_ _|_____|_ _|
  r |   :     :   |
    |___:_____:___|
      r    bw   r
    <----tilew---->
*/

__global__ void cudaProcess(unsigned int *g_odata, int imgw, int imgh,
                            int tilew, int r, float threshold, float highlight,
                            cudaTextureObject_t inTex) {
  // Handle to thread block group
  cg::thread_block cta = cg::this_thread_block();
  extern __shared__ uchar4 sdata[];

  int tx = threadIdx.x;
  int ty = threadIdx.y;
  int bw = blockDim.x;
  int bh = blockDim.y;
  int x = blockIdx.x * bw + tx;
  int y = blockIdx.y * bh + ty;

#if 0
    uchar4 c4 = getPixel(x, y);
    g_odata[y*imgw+x] = rgbToInt(c4.z, c4.y, c4.x);
#else
  // copy tile to shared memory
  // center region
  SMEM(r + tx, r + ty) = getPixel(x, y, inTex);

  // borders
  if (threadIdx.x < r) {
    // left
    SMEM(tx, r + ty) = getPixel(x - r, y, inTex);
    // right
    SMEM(r + bw + tx, r + ty) = getPixel(x + bw, y, inTex);
  }

  if (threadIdx.y < r) {
    // top
    SMEM(r + tx, ty) = getPixel(x, y - r, inTex);
    // bottom
    SMEM(r + tx, r + bh + ty) = getPixel(x, y + bh, inTex);
  }

  // load corners
  if ((threadIdx.x < r) && (threadIdx.y < r)) {
    // tl
    SMEM(tx, ty) = getPixel(x - r, y - r, inTex);
    // bl
    SMEM(tx, r + bh + ty) = getPixel(x - r, y + bh, inTex);
    // tr
    SMEM(r + bw + tx, ty) = getPixel(x + bh, y - r, inTex);
    // br
    SMEM(r + bw + tx, r + bh + ty) = getPixel(x + bw, y + bh, inTex);
  }

  // wait for loads to complete
  cg::sync(cta);

  // perform convolution
  float rsum = 0.0f;
  float gsum = 0.0f;
  float bsum = 0.0f;
  float samples = 0.0f;

  for (int dy = -r; dy <= r; dy++) {
    for (int dx = -r; dx <= r; dx++) {
#if 0
            // try this to see the benefit of using shared memory
            uchar4 pixel = getPixel(x+dx, y+dy);
#else
      uchar4 pixel = SMEM(r + tx + dx, r + ty + dy);
#endif

      // only sum pixels within disc-shaped kernel
      float l = dx * dx + dy * dy;

      if (l <= r * r) {
        float r = float(pixel.x);
        float g = float(pixel.y);
        float b = float(pixel.z);
#if 1
        // brighten highlights
        float lum = (r + g + b) / (255 * 3);

        if (lum > threshold) {
          r *= highlight;
          g *= highlight;
          b *= highlight;
        }

#endif
        rsum += r;
        gsum += g;
        bsum += b;
        samples += 1.0f;
      }
    }
  }

  rsum /= samples;
  gsum /= samples;
  bsum /= samples;
  // ABGR
  g_odata[y * imgw + x] = rgbToInt(rsum, gsum, bsum);
// g_odata[y*imgw+x] = rgbToInt(x,y,0);
#endif
}

extern "C" void launch_cudaProcess(dim3 grid, dim3 block, int sbytes,
                                   cudaArray *g_data_array,
                                   unsigned int *g_odata, int imgw, int imgh,
                                   int tilew, int radius, float threshold,
                                   float highlight) {
  struct cudaChannelFormatDesc desc;
  checkCudaErrors(cudaGetChannelDesc(&desc, g_data_array));

  cudaResourceDesc texRes;
  memset(&texRes, 0, sizeof(cudaResourceDesc));

  texRes.resType = cudaResourceTypeArray;
  texRes.res.array.array = g_data_array;

  cudaTextureDesc texDescr;
  memset(&texDescr, 0, sizeof(cudaTextureDesc));

  texDescr.normalizedCoords = false;
  texDescr.filterMode = cudaFilterModePoint;
  texDescr.addressMode[0] = cudaAddressModeWrap;
  texDescr.readMode = cudaReadModeElementType;

  checkCudaErrors(
      cudaCreateTextureObject(&inTexObject, &texRes, &texDescr, NULL));

#if 0
    printf("CUDA Array channel descriptor, bits per component:\n");
    printf("X %d Y %d Z %d W %d, kind %d\n",
           desc.x,desc.y,desc.z,desc.w,desc.f);

    printf("Possible values for channel format kind: i %d, u%d, f%d:\n",
           cudaChannelFormatKindSigned, cudaChannelFormatKindUnsigned,
           cudaChannelFormatKindFloat);
#endif

// printf("\n");
#ifdef GPU_PROFILING
  StopWatchInterface *timer = 0;
  sdkCreateTimer(&timer);

  int nIter = 30;

  for (int i = -1; i < nIter; ++i) {
    if (i == 0) {
      sdkStartTimer(&timer);
    }

#endif

    cudaProcess<<<grid, block, sbytes>>>(g_odata, imgw, imgh,
                                         block.x + (2 * radius), radius, 0.8f,
                                         4.0f, inTexObject);

#ifdef GPU_PROFILING
  }

  cudaDeviceSynchronize();
  sdkStopTimer(&timer);
  double dSeconds = sdkGetTimerValue(&timer) / ((double)nIter * 1000.0);
  double dNumTexels = (double)imgw * (double)imgh;
  double mtexps = 1.0e-6 * dNumTexels / dSeconds;

  if (radius == 4) {
    printf("\n");
    printf(
        "postprocessGL, Throughput = %.4f MTexels/s, Time = %.5f s, Size = "
        "%.0f Texels, NumDevsUsed = %d, Workgroup = %u\n",
        mtexps, dSeconds, dNumTexels, 1, block.x * block.y);
  }

#endif
}