mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-25 22:28:17 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -34,4 +34,3 @@ Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-d
|
||||
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||
|
||||
## References (for more details)
|
||||
|
||||
|
||||
@@ -64,472 +64,467 @@
|
||||
#endif
|
||||
|
||||
// CUDA includes and interop headers
|
||||
#include <cuda_runtime.h>
|
||||
#include <cuda_gl_interop.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
// CUDA utilities and system includes
|
||||
#include <helper_cuda.h> // includes cuda.h and cuda_runtime_api.h
|
||||
#include <helper_functions.h>
|
||||
#include <helper_cuda.h> // includes cuda.h and cuda_runtime_api.h
|
||||
|
||||
// Includes
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX(a, b) ((a > b) ? a : b)
|
||||
|
||||
#define USE_SIMPLE_FILTER 0
|
||||
|
||||
#define MAX_EPSILON_ERROR 5.0f
|
||||
#define THRESHOLD 0.15f
|
||||
#define THRESHOLD 0.15f
|
||||
|
||||
// Define the files that are to be save and the reference images for validation
|
||||
const char *sOriginal[] = {"teapot512_10.ppm", "teapot512_14.ppm", "teapot512_18.ppm",
|
||||
"teapot512_22.ppm", NULL};
|
||||
const char *sOriginal[] = {"teapot512_10.ppm", "teapot512_14.ppm", "teapot512_18.ppm", "teapot512_22.ppm", NULL};
|
||||
|
||||
const char *sReference[] = {"ref_10.ppm", "ref_14.ppm", "ref_18.ppm",
|
||||
"ref_22.ppm", NULL};
|
||||
const char *sReference[] = {"ref_10.ppm", "ref_14.ppm", "ref_18.ppm", "ref_22.ppm", NULL};
|
||||
|
||||
const char *image_filename = "teapot512.ppm";
|
||||
float sigma = 10.0f;
|
||||
int order = 0;
|
||||
int nthreads = 64; // number of threads per block
|
||||
float sigma = 10.0f;
|
||||
int order = 0;
|
||||
int nthreads = 64; // number of threads per block
|
||||
|
||||
unsigned int width, height;
|
||||
unsigned int *h_img = NULL;
|
||||
unsigned int *d_img = NULL;
|
||||
unsigned int width, height;
|
||||
unsigned int *h_img = NULL;
|
||||
unsigned int *d_img = NULL;
|
||||
unsigned int *d_temp = NULL;
|
||||
|
||||
GLuint pbo = 0; // OpenGL pixel buffer object
|
||||
GLuint texid = 0; // texture
|
||||
GLuint pbo = 0; // OpenGL pixel buffer object
|
||||
GLuint texid = 0; // texture
|
||||
|
||||
cudaGraphicsResource_t cuda_vbo_resource;
|
||||
|
||||
StopWatchInterface *timer = 0;
|
||||
|
||||
// Auto-Verification Code
|
||||
const int frameCheckNumber = 4;
|
||||
int fpsCount = 0; // FPS count for averaging
|
||||
int fpsLimit = 1; // FPS limit for sampling
|
||||
unsigned int frameCount = 0;
|
||||
const int frameCheckNumber = 4;
|
||||
int fpsCount = 0; // FPS count for averaging
|
||||
int fpsLimit = 1; // FPS limit for sampling
|
||||
unsigned int frameCount = 0;
|
||||
|
||||
int *pArgc = NULL;
|
||||
int *pArgc = NULL;
|
||||
char **pArgv = NULL;
|
||||
|
||||
bool runBenchmark = false;
|
||||
|
||||
const char *sSDKsample = "CUDA Recursive Gaussian";
|
||||
|
||||
extern "C" void transpose(unsigned int *d_src, unsigned int *d_dest,
|
||||
unsigned int width, int height);
|
||||
extern "C" void transpose(unsigned int *d_src, unsigned int *d_dest, unsigned int width, int height);
|
||||
|
||||
extern "C" void gaussianFilterRGBA(unsigned int *d_src, unsigned int *d_dest,
|
||||
unsigned int *d_temp, int width, int height,
|
||||
float sigma, int order, int nthreads);
|
||||
extern "C" void gaussianFilterRGBA(unsigned int *d_src,
|
||||
unsigned int *d_dest,
|
||||
unsigned int *d_temp,
|
||||
int width,
|
||||
int height,
|
||||
float sigma,
|
||||
int order,
|
||||
int nthreads);
|
||||
|
||||
void cleanup();
|
||||
|
||||
void computeFPS() {
|
||||
frameCount++;
|
||||
fpsCount++;
|
||||
void computeFPS()
|
||||
{
|
||||
frameCount++;
|
||||
fpsCount++;
|
||||
|
||||
if (fpsCount == fpsLimit) {
|
||||
char fps[256];
|
||||
float ifps = 1.f / (sdkGetAverageTimerValue(&timer) / 1000.f);
|
||||
sprintf(fps, "%s (sigma=%4.2f): %3.1f fps", sSDKsample, sigma, ifps);
|
||||
if (fpsCount == fpsLimit) {
|
||||
char fps[256];
|
||||
float ifps = 1.f / (sdkGetAverageTimerValue(&timer) / 1000.f);
|
||||
sprintf(fps, "%s (sigma=%4.2f): %3.1f fps", sSDKsample, sigma, ifps);
|
||||
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
|
||||
fpsLimit = ftoi(MAX(ifps, 1.f));
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
fpsLimit = ftoi(MAX(ifps, 1.f));
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
}
|
||||
|
||||
// display results using OpenGL
|
||||
void display() {
|
||||
sdkStartTimer(&timer);
|
||||
void display()
|
||||
{
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
// execute filter, writing results to pbo
|
||||
unsigned int *d_result;
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_vbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
|
||||
(void **)&d_result, &num_bytes, cuda_vbo_resource));
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order,
|
||||
nthreads);
|
||||
// execute filter, writing results to pbo
|
||||
unsigned int *d_result;
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_vbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void **)&d_result, &num_bytes, cuda_vbo_resource));
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order, nthreads);
|
||||
|
||||
// unmap buffer object
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));
|
||||
// unmap buffer object
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));
|
||||
|
||||
// load texture from pbo
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, 0);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
||||
// load texture from pbo
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
||||
|
||||
// display results
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// display results
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0, 1);
|
||||
glVertex2f(0, 0);
|
||||
glTexCoord2f(1, 1);
|
||||
glVertex2f(1, 0);
|
||||
glTexCoord2f(1, 0);
|
||||
glVertex2f(1, 1);
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex2f(0, 1);
|
||||
glEnd();
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0, 1);
|
||||
glVertex2f(0, 0);
|
||||
glTexCoord2f(1, 1);
|
||||
glVertex2f(1, 0);
|
||||
glTexCoord2f(1, 0);
|
||||
glVertex2f(1, 1);
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex2f(0, 1);
|
||||
glEnd();
|
||||
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glutSwapBuffers();
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glutSwapBuffers();
|
||||
|
||||
sdkStopTimer(&timer);
|
||||
sdkStopTimer(&timer);
|
||||
|
||||
computeFPS();
|
||||
computeFPS();
|
||||
}
|
||||
|
||||
void idle() { glutPostRedisplay(); }
|
||||
|
||||
void cleanup() {
|
||||
sdkDeleteTimer(&timer);
|
||||
void cleanup()
|
||||
{
|
||||
sdkDeleteTimer(&timer);
|
||||
|
||||
checkCudaErrors(cudaFree(d_img));
|
||||
checkCudaErrors(cudaFree(d_temp));
|
||||
checkCudaErrors(cudaFree(d_img));
|
||||
checkCudaErrors(cudaFree(d_temp));
|
||||
|
||||
if (!runBenchmark) {
|
||||
if (pbo) {
|
||||
// unregister this buffer object with CUDA
|
||||
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_vbo_resource));
|
||||
glDeleteBuffers(1, &pbo);
|
||||
if (!runBenchmark) {
|
||||
if (pbo) {
|
||||
// unregister this buffer object with CUDA
|
||||
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_vbo_resource));
|
||||
glDeleteBuffers(1, &pbo);
|
||||
}
|
||||
|
||||
if (texid) {
|
||||
glDeleteTextures(1, &texid);
|
||||
}
|
||||
}
|
||||
|
||||
if (texid) {
|
||||
glDeleteTextures(1, &texid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int x, int y) {
|
||||
switch (key) {
|
||||
void keyboard(unsigned char key, int x, int y)
|
||||
{
|
||||
switch (key) {
|
||||
case 27:
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
exit(EXIT_SUCCESS);
|
||||
exit(EXIT_SUCCESS);
|
||||
#else
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
return;
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
return;
|
||||
#endif
|
||||
break;
|
||||
break;
|
||||
|
||||
case '=':
|
||||
sigma += 0.1f;
|
||||
break;
|
||||
sigma += 0.1f;
|
||||
break;
|
||||
|
||||
case '-':
|
||||
sigma -= 0.1f;
|
||||
sigma -= 0.1f;
|
||||
|
||||
if (sigma < 0.0) {
|
||||
sigma = 0.0f;
|
||||
}
|
||||
if (sigma < 0.0) {
|
||||
sigma = 0.0f;
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
|
||||
case '+':
|
||||
sigma += 1.0f;
|
||||
break;
|
||||
sigma += 1.0f;
|
||||
break;
|
||||
|
||||
case '_':
|
||||
sigma -= 1.0f;
|
||||
sigma -= 1.0f;
|
||||
|
||||
if (sigma < 0.0) {
|
||||
sigma = 0.0f;
|
||||
}
|
||||
if (sigma < 0.0) {
|
||||
sigma = 0.0f;
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
|
||||
case '0':
|
||||
order = 0;
|
||||
break;
|
||||
order = 0;
|
||||
break;
|
||||
|
||||
case '1':
|
||||
order = 1;
|
||||
sigma = 0.5f;
|
||||
break;
|
||||
order = 1;
|
||||
sigma = 0.5f;
|
||||
break;
|
||||
|
||||
case '2':
|
||||
order = 2;
|
||||
sigma = 0.5f;
|
||||
break;
|
||||
order = 2;
|
||||
sigma = 0.5f;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
printf("sigma = %f\n", sigma);
|
||||
glutPostRedisplay();
|
||||
printf("sigma = %f\n", sigma);
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
void reshape(int x, int y) {
|
||||
glViewport(0, 0, x, y);
|
||||
void reshape(int x, int y)
|
||||
{
|
||||
glViewport(0, 0, x, y);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
void initCudaBuffers() {
|
||||
unsigned int size = width * height * sizeof(unsigned int);
|
||||
void initCudaBuffers()
|
||||
{
|
||||
unsigned int size = width * height * sizeof(unsigned int);
|
||||
|
||||
// allocate device memory
|
||||
checkCudaErrors(cudaMalloc((void **)&d_img, size));
|
||||
checkCudaErrors(cudaMalloc((void **)&d_temp, size));
|
||||
// allocate device memory
|
||||
checkCudaErrors(cudaMalloc((void **)&d_img, size));
|
||||
checkCudaErrors(cudaMalloc((void **)&d_temp, size));
|
||||
|
||||
checkCudaErrors(cudaMemcpy(d_img, h_img, size, cudaMemcpyHostToDevice));
|
||||
checkCudaErrors(cudaMemcpy(d_img, h_img, size, cudaMemcpyHostToDevice));
|
||||
|
||||
sdkCreateTimer(&timer);
|
||||
sdkCreateTimer(&timer);
|
||||
}
|
||||
|
||||
void initGLBuffers() {
|
||||
// create pixel buffer object to store final image
|
||||
glGenBuffers(1, &pbo);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
|
||||
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * sizeof(GLubyte) * 4,
|
||||
h_img, GL_STREAM_DRAW_ARB);
|
||||
void initGLBuffers()
|
||||
{
|
||||
// create pixel buffer object to store final image
|
||||
glGenBuffers(1, &pbo);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
|
||||
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * sizeof(GLubyte) * 4, h_img, GL_STREAM_DRAW_ARB);
|
||||
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(
|
||||
&cuda_vbo_resource, pbo, cudaGraphicsRegisterFlagsWriteDiscard));
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, pbo, cudaGraphicsRegisterFlagsWriteDiscard));
|
||||
|
||||
// create texture for display
|
||||
glGenTextures(1, &texid);
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
// create texture for display
|
||||
glGenTextures(1, &texid);
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void initGL(int *argc, char **argv) {
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
||||
glutInitWindowSize(width, height);
|
||||
glutCreateWindow(sSDKsample);
|
||||
glutDisplayFunc(display);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutReshapeFunc(reshape);
|
||||
glutIdleFunc(idle);
|
||||
void initGL(int *argc, char **argv)
|
||||
{
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
||||
glutInitWindowSize(width, height);
|
||||
glutCreateWindow(sSDKsample);
|
||||
glutDisplayFunc(display);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutReshapeFunc(reshape);
|
||||
glutIdleFunc(idle);
|
||||
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
atexit(cleanup);
|
||||
atexit(cleanup);
|
||||
#else
|
||||
glutCloseFunc(cleanup);
|
||||
glutCloseFunc(cleanup);
|
||||
#endif
|
||||
|
||||
printf("Press '+' and '-' to change filter width\n");
|
||||
printf("0, 1, 2 - change filter order\n");
|
||||
printf("Press '+' and '-' to change filter width\n");
|
||||
printf("0, 1, 2 - change filter order\n");
|
||||
|
||||
if (!isGLVersionSupported(2, 0) ||
|
||||
!areGLExtensionsSupported(
|
||||
"GL_ARB_vertex_buffer_object GL_ARB_pixel_buffer_object")) {
|
||||
fprintf(stderr, "Required OpenGL extensions missing.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!isGLVersionSupported(2, 0)
|
||||
|| !areGLExtensionsSupported("GL_ARB_vertex_buffer_object GL_ARB_pixel_buffer_object")) {
|
||||
fprintf(stderr, "Required OpenGL extensions missing.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void benchmark(int iterations) {
|
||||
// allocate memory for result
|
||||
unsigned int *d_result;
|
||||
unsigned int size = width * height * sizeof(unsigned int);
|
||||
checkCudaErrors(cudaMalloc((void **)&d_result, size));
|
||||
void benchmark(int iterations)
|
||||
{
|
||||
// allocate memory for result
|
||||
unsigned int *d_result;
|
||||
unsigned int size = width * height * sizeof(unsigned int);
|
||||
checkCudaErrors(cudaMalloc((void **)&d_result, size));
|
||||
|
||||
// warm-up
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order,
|
||||
nthreads);
|
||||
// warm-up
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order, nthreads);
|
||||
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStartTimer(&timer);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
// execute the kernel
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order,
|
||||
nthreads);
|
||||
}
|
||||
// execute the kernel
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order, nthreads);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&timer);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&timer);
|
||||
|
||||
// check if kernel execution generated an error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
// check if kernel execution generated an error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
|
||||
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
|
||||
printf("%.2f Mpixels/sec\n",
|
||||
(width * height * iterations / (sdkGetTimerValue(&timer) / 1000.0f)) /
|
||||
1e6);
|
||||
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
|
||||
printf("%.2f Mpixels/sec\n", (width * height * iterations / (sdkGetTimerValue(&timer) / 1000.0f)) / 1e6);
|
||||
|
||||
checkCudaErrors(cudaFree(d_result));
|
||||
checkCudaErrors(cudaFree(d_result));
|
||||
}
|
||||
|
||||
bool runSingleTest(const char *ref_file, const char *exec_path) {
|
||||
// allocate memory for result
|
||||
int nTotalErrors = 0;
|
||||
unsigned int *d_result;
|
||||
unsigned int size = width * height * sizeof(unsigned int);
|
||||
checkCudaErrors(cudaMalloc((void **)&d_result, size));
|
||||
bool runSingleTest(const char *ref_file, const char *exec_path)
|
||||
{
|
||||
// allocate memory for result
|
||||
int nTotalErrors = 0;
|
||||
unsigned int *d_result;
|
||||
unsigned int size = width * height * sizeof(unsigned int);
|
||||
checkCudaErrors(cudaMalloc((void **)&d_result, size));
|
||||
|
||||
// warm-up
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order,
|
||||
nthreads);
|
||||
// warm-up
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order, nthreads);
|
||||
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStartTimer(&timer);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order,
|
||||
nthreads);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
getLastCudaError("Kernel execution failed");
|
||||
sdkStopTimer(&timer);
|
||||
gaussianFilterRGBA(d_img, d_result, d_temp, width, height, sigma, order, nthreads);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
getLastCudaError("Kernel execution failed");
|
||||
sdkStopTimer(&timer);
|
||||
|
||||
unsigned char *h_result = (unsigned char *)malloc(width * height * 4);
|
||||
checkCudaErrors(cudaMemcpy(h_result, d_result, width * height * 4,
|
||||
cudaMemcpyDeviceToHost));
|
||||
unsigned char *h_result = (unsigned char *)malloc(width * height * 4);
|
||||
checkCudaErrors(cudaMemcpy(h_result, d_result, width * height * 4, cudaMemcpyDeviceToHost));
|
||||
|
||||
char dump_file[1024];
|
||||
sprintf(dump_file, "teapot512_%02d.ppm", (int)sigma);
|
||||
sdkSavePPM4ub(dump_file, h_result, width, height);
|
||||
char dump_file[1024];
|
||||
sprintf(dump_file, "teapot512_%02d.ppm", (int)sigma);
|
||||
sdkSavePPM4ub(dump_file, h_result, width, height);
|
||||
|
||||
if (!sdkComparePPM(dump_file, sdkFindFilePath(ref_file, exec_path),
|
||||
MAX_EPSILON_ERROR, THRESHOLD, false)) {
|
||||
nTotalErrors++;
|
||||
}
|
||||
if (!sdkComparePPM(dump_file, sdkFindFilePath(ref_file, exec_path), MAX_EPSILON_ERROR, THRESHOLD, false)) {
|
||||
nTotalErrors++;
|
||||
}
|
||||
|
||||
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
|
||||
printf("%.2f Mpixels/sec\n",
|
||||
(width * height / (sdkGetTimerValue(&timer) / 1000.0f)) / 1e6);
|
||||
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
|
||||
printf("%.2f Mpixels/sec\n", (width * height / (sdkGetTimerValue(&timer) / 1000.0f)) / 1e6);
|
||||
|
||||
checkCudaErrors(cudaFree(d_result));
|
||||
free(h_result);
|
||||
checkCudaErrors(cudaFree(d_result));
|
||||
free(h_result);
|
||||
|
||||
printf("Summary: %d errors!\n", nTotalErrors);
|
||||
printf("Summary: %d errors!\n", nTotalErrors);
|
||||
|
||||
printf(nTotalErrors == 0 ? "Test passed\n" : "Test failed!\n");
|
||||
return (nTotalErrors == 0);
|
||||
printf(nTotalErrors == 0 ? "Test passed\n" : "Test failed!\n");
|
||||
return (nTotalErrors == 0);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Program main
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv) {
|
||||
pArgc = &argc;
|
||||
pArgv = argv;
|
||||
char *ref_file = NULL;
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pArgc = &argc;
|
||||
pArgv = argv;
|
||||
char *ref_file = NULL;
|
||||
|
||||
#if defined(__linux__)
|
||||
setenv("DISPLAY", ":0", 0);
|
||||
setenv("DISPLAY", ":0", 0);
|
||||
#endif
|
||||
|
||||
printf("%s Starting...\n\n", sSDKsample);
|
||||
printf("%s Starting...\n\n", sSDKsample);
|
||||
|
||||
printf(
|
||||
"NOTE: The CUDA Samples are not meant for performance measurements. "
|
||||
"Results may vary when GPU Boost is enabled.\n\n");
|
||||
printf("NOTE: The CUDA Samples are not meant for performance measurements. "
|
||||
"Results may vary when GPU Boost is enabled.\n\n");
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
if (argc > 1) {
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
|
||||
fpsLimit = frameCheckNumber;
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
if (argc > 1) {
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
|
||||
fpsLimit = frameCheckNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the path of the filename
|
||||
char *filename;
|
||||
// Get the path of the filename
|
||||
char *filename;
|
||||
|
||||
if (getCmdLineArgumentString(argc, (const char **)argv, "image", &filename)) {
|
||||
image_filename = filename;
|
||||
}
|
||||
if (getCmdLineArgumentString(argc, (const char **)argv, "image", &filename)) {
|
||||
image_filename = filename;
|
||||
}
|
||||
|
||||
// load image
|
||||
char *image_path = sdkFindFilePath(image_filename, argv[0]);
|
||||
// load image
|
||||
char *image_path = sdkFindFilePath(image_filename, argv[0]);
|
||||
|
||||
if (image_path == NULL) {
|
||||
fprintf(stderr, "Error unable to find and load image file: '%s'\n",
|
||||
image_filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (image_path == NULL) {
|
||||
fprintf(stderr, "Error unable to find and load image file: '%s'\n", image_filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
sdkLoadPPM4ub(image_path, (unsigned char **)&h_img, &width, &height);
|
||||
sdkLoadPPM4ub(image_path, (unsigned char **)&h_img, &width, &height);
|
||||
|
||||
if (!h_img) {
|
||||
printf("Error unable to load PPM file: '%s'\n", image_path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!h_img) {
|
||||
printf("Error unable to load PPM file: '%s'\n", image_path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Loaded '%s', %d x %d pixels\n", image_path, width, height);
|
||||
printf("Loaded '%s', %d x %d pixels\n", image_path, width, height);
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "threads")) {
|
||||
nthreads = getCmdLineArgumentInt(argc, (const char **)argv, "threads");
|
||||
}
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "threads")) {
|
||||
nthreads = getCmdLineArgumentInt(argc, (const char **)argv, "threads");
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "sigma")) {
|
||||
sigma = getCmdLineArgumentFloat(argc, (const char **)argv, "sigma");
|
||||
}
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "sigma")) {
|
||||
sigma = getCmdLineArgumentFloat(argc, (const char **)argv, "sigma");
|
||||
}
|
||||
|
||||
runBenchmark = checkCmdLineFlag(argc, (const char **)argv, "benchmark");
|
||||
runBenchmark = checkCmdLineFlag(argc, (const char **)argv, "benchmark");
|
||||
|
||||
int device;
|
||||
struct cudaDeviceProp prop;
|
||||
cudaGetDevice(&device);
|
||||
cudaGetDeviceProperties(&prop, device);
|
||||
int device;
|
||||
struct cudaDeviceProp prop;
|
||||
cudaGetDevice(&device);
|
||||
cudaGetDeviceProperties(&prop, device);
|
||||
|
||||
if (!strncmp("Tesla", prop.name, 5)) {
|
||||
printf(
|
||||
"Tesla card detected, running the test in benchmark mode (no OpenGL "
|
||||
"display)\n");
|
||||
// runBenchmark = true;
|
||||
runBenchmark = true;
|
||||
}
|
||||
if (!strncmp("Tesla", prop.name, 5)) {
|
||||
printf("Tesla card detected, running the test in benchmark mode (no OpenGL "
|
||||
"display)\n");
|
||||
// runBenchmark = true;
|
||||
runBenchmark = true;
|
||||
}
|
||||
|
||||
// Benchmark or AutoTest mode detected, no OpenGL
|
||||
if (runBenchmark == true || ref_file != NULL) {
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
} else {
|
||||
// First initialize OpenGL context, and then select CUDA device.
|
||||
initGL(&argc, argv);
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
}
|
||||
// Benchmark or AutoTest mode detected, no OpenGL
|
||||
if (runBenchmark == true || ref_file != NULL) {
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
}
|
||||
else {
|
||||
// First initialize OpenGL context, and then select CUDA device.
|
||||
initGL(&argc, argv);
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
}
|
||||
|
||||
initCudaBuffers();
|
||||
initCudaBuffers();
|
||||
|
||||
if (ref_file) {
|
||||
printf("(Automated Testing)\n");
|
||||
bool testPassed = runSingleTest(ref_file, argv[0]);
|
||||
if (ref_file) {
|
||||
printf("(Automated Testing)\n");
|
||||
bool testPassed = runSingleTest(ref_file, argv[0]);
|
||||
|
||||
cleanup();
|
||||
exit(testPassed ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
cleanup();
|
||||
exit(testPassed ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (runBenchmark) {
|
||||
printf("(Run Benchmark)\n");
|
||||
benchmark(100);
|
||||
if (runBenchmark) {
|
||||
printf("(Run Benchmark)\n");
|
||||
benchmark(100);
|
||||
|
||||
cleanup();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
initGLBuffers();
|
||||
glutMainLoop();
|
||||
|
||||
cleanup();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
initGLBuffers();
|
||||
glutMainLoop();
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -49,13 +49,12 @@
|
||||
Thanks to David Tschumperl<72> and all the CImg contributors!
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "recursiveGaussian_kernel.cuh"
|
||||
|
||||
@@ -67,11 +66,12 @@ int iDivUp(int a, int b) { return (a % b != 0) ? (a / b + 1) : (a / b); }
|
||||
/*
|
||||
Transpose a 2D array (see SDK transpose example)
|
||||
*/
|
||||
extern "C" void transpose(uint *d_src, uint *d_dest, uint width, int height) {
|
||||
dim3 grid(iDivUp(width, BLOCK_DIM), iDivUp(height, BLOCK_DIM), 1);
|
||||
dim3 threads(BLOCK_DIM, BLOCK_DIM, 1);
|
||||
d_transpose<<<grid, threads>>>(d_dest, d_src, width, height);
|
||||
getLastCudaError("Kernel execution failed");
|
||||
extern "C" void transpose(uint *d_src, uint *d_dest, uint width, int height)
|
||||
{
|
||||
dim3 grid(iDivUp(width, BLOCK_DIM), iDivUp(height, BLOCK_DIM), 1);
|
||||
dim3 threads(BLOCK_DIM, BLOCK_DIM, 1);
|
||||
d_transpose<<<grid, threads>>>(d_dest, d_src, width, height);
|
||||
getLastCudaError("Kernel execution failed");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -88,73 +88,68 @@ extern "C" void transpose(uint *d_src, uint *d_dest, uint width, int height) {
|
||||
*/
|
||||
|
||||
// 8-bit RGBA version
|
||||
extern "C" void gaussianFilterRGBA(uint *d_src, uint *d_dest, uint *d_temp,
|
||||
int width, int height, float sigma,
|
||||
int order, int nthreads) {
|
||||
// compute filter coefficients
|
||||
const float nsigma = sigma < 0.1f ? 0.1f : sigma, alpha = 1.695f / nsigma,
|
||||
ema = (float)std::exp(-alpha), ema2 = (float)std::exp(-2 * alpha),
|
||||
b1 = -2 * ema, b2 = ema2;
|
||||
extern "C" void
|
||||
gaussianFilterRGBA(uint *d_src, uint *d_dest, uint *d_temp, int width, int height, float sigma, int order, int nthreads)
|
||||
{
|
||||
// compute filter coefficients
|
||||
const float nsigma = sigma < 0.1f ? 0.1f : sigma, alpha = 1.695f / nsigma, ema = (float)std::exp(-alpha),
|
||||
ema2 = (float)std::exp(-2 * alpha), b1 = -2 * ema, b2 = ema2;
|
||||
|
||||
float a0 = 0, a1 = 0, a2 = 0, a3 = 0, coefp = 0, coefn = 0;
|
||||
float a0 = 0, a1 = 0, a2 = 0, a3 = 0, coefp = 0, coefn = 0;
|
||||
|
||||
switch (order) {
|
||||
switch (order) {
|
||||
case 0: {
|
||||
const float k = (1 - ema) * (1 - ema) / (1 + 2 * alpha * ema - ema2);
|
||||
a0 = k;
|
||||
a1 = k * (alpha - 1) * ema;
|
||||
a2 = k * (alpha + 1) * ema;
|
||||
a3 = -k * ema2;
|
||||
const float k = (1 - ema) * (1 - ema) / (1 + 2 * alpha * ema - ema2);
|
||||
a0 = k;
|
||||
a1 = k * (alpha - 1) * ema;
|
||||
a2 = k * (alpha + 1) * ema;
|
||||
a3 = -k * ema2;
|
||||
} break;
|
||||
|
||||
case 1: {
|
||||
const float k = (1 - ema) * (1 - ema) / ema;
|
||||
a0 = k * ema;
|
||||
a1 = a3 = 0;
|
||||
a2 = -a0;
|
||||
const float k = (1 - ema) * (1 - ema) / ema;
|
||||
a0 = k * ema;
|
||||
a1 = a3 = 0;
|
||||
a2 = -a0;
|
||||
} break;
|
||||
|
||||
case 2: {
|
||||
const float ea = (float)std::exp(-alpha),
|
||||
k = -(ema2 - 1) / (2 * alpha * ema),
|
||||
kn = (-2 * (-1 + 3 * ea - 3 * ea * ea + ea * ea * ea) /
|
||||
(3 * ea + 1 + 3 * ea * ea + ea * ea * ea));
|
||||
a0 = kn;
|
||||
a1 = -kn * (1 + k * alpha) * ema;
|
||||
a2 = kn * (1 - k * alpha) * ema;
|
||||
a3 = -kn * ema2;
|
||||
const float ea = (float)std::exp(-alpha), k = -(ema2 - 1) / (2 * alpha * ema),
|
||||
kn = (-2 * (-1 + 3 * ea - 3 * ea * ea + ea * ea * ea) / (3 * ea + 1 + 3 * ea * ea + ea * ea * ea));
|
||||
a0 = kn;
|
||||
a1 = -kn * (1 + k * alpha) * ema;
|
||||
a2 = kn * (1 - k * alpha) * ema;
|
||||
a3 = -kn * ema2;
|
||||
} break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "gaussianFilter: invalid order parameter!\n");
|
||||
return;
|
||||
}
|
||||
fprintf(stderr, "gaussianFilter: invalid order parameter!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
coefp = (a0 + a1) / (1 + b1 + b2);
|
||||
coefn = (a2 + a3) / (1 + b1 + b2);
|
||||
coefp = (a0 + a1) / (1 + b1 + b2);
|
||||
coefn = (a2 + a3) / (1 + b1 + b2);
|
||||
|
||||
// process columns
|
||||
#if USE_SIMPLE_FILTER
|
||||
d_simpleRecursive_rgba<<<iDivUp(width, nthreads), nthreads>>>(
|
||||
d_src, d_temp, width, height, ema);
|
||||
d_simpleRecursive_rgba<<<iDivUp(width, nthreads), nthreads>>>(d_src, d_temp, width, height, ema);
|
||||
#else
|
||||
d_recursiveGaussian_rgba<<<iDivUp(width, nthreads), nthreads>>>(
|
||||
d_src, d_temp, width, height, a0, a1, a2, a3, b1, b2, coefp, coefn);
|
||||
d_recursiveGaussian_rgba<<<iDivUp(width, nthreads), nthreads>>>(
|
||||
d_src, d_temp, width, height, a0, a1, a2, a3, b1, b2, coefp, coefn);
|
||||
#endif
|
||||
getLastCudaError("Kernel execution failed");
|
||||
getLastCudaError("Kernel execution failed");
|
||||
|
||||
transpose(d_temp, d_dest, width, height);
|
||||
getLastCudaError("transpose: Kernel execution failed");
|
||||
transpose(d_temp, d_dest, width, height);
|
||||
getLastCudaError("transpose: Kernel execution failed");
|
||||
|
||||
// process rows
|
||||
#if USE_SIMPLE_FILTER
|
||||
d_simpleRecursive_rgba<<<iDivUp(height, nthreads), nthreads>>>(
|
||||
d_dest, d_temp, height, width, ema);
|
||||
d_simpleRecursive_rgba<<<iDivUp(height, nthreads), nthreads>>>(d_dest, d_temp, height, width, ema);
|
||||
#else
|
||||
d_recursiveGaussian_rgba<<<iDivUp(height, nthreads), nthreads>>>(
|
||||
d_dest, d_temp, height, width, a0, a1, a2, a3, b1, b2, coefp, coefn);
|
||||
d_recursiveGaussian_rgba<<<iDivUp(height, nthreads), nthreads>>>(
|
||||
d_dest, d_temp, height, width, a0, a1, a2, a3, b1, b2, coefp, coefn);
|
||||
#endif
|
||||
getLastCudaError("Kernel execution failed");
|
||||
getLastCudaError("Kernel execution failed");
|
||||
|
||||
transpose(d_temp, d_dest, height, width);
|
||||
transpose(d_temp, d_dest, height, width);
|
||||
}
|
||||
|
||||
@@ -32,68 +32,70 @@
|
||||
#ifndef _RECURSIVEGAUSSIAN_KERNEL_CU_
|
||||
#define _RECURSIVEGAUSSIAN_KERNEL_CU_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <cooperative_groups.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_math.h>
|
||||
|
||||
#define BLOCK_DIM 16
|
||||
#define BLOCK_DIM 16
|
||||
#define CLAMP_TO_EDGE 1
|
||||
|
||||
// Transpose kernel (see transpose CUDA Sample for details)
|
||||
__global__ void d_transpose(uint *odata, uint *idata, int width, int height) {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__global__ void d_transpose(uint *odata, uint *idata, int width, int height)
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
|
||||
__shared__ uint block[BLOCK_DIM][BLOCK_DIM + 1];
|
||||
__shared__ uint block[BLOCK_DIM][BLOCK_DIM + 1];
|
||||
|
||||
// read the matrix tile into shared memory
|
||||
unsigned int xIndex = blockIdx.x * BLOCK_DIM + threadIdx.x;
|
||||
unsigned int yIndex = blockIdx.y * BLOCK_DIM + threadIdx.y;
|
||||
// read the matrix tile into shared memory
|
||||
unsigned int xIndex = blockIdx.x * BLOCK_DIM + threadIdx.x;
|
||||
unsigned int yIndex = blockIdx.y * BLOCK_DIM + threadIdx.y;
|
||||
|
||||
if ((xIndex < width) && (yIndex < height)) {
|
||||
unsigned int index_in = yIndex * width + xIndex;
|
||||
block[threadIdx.y][threadIdx.x] = idata[index_in];
|
||||
}
|
||||
if ((xIndex < width) && (yIndex < height)) {
|
||||
unsigned int index_in = yIndex * width + xIndex;
|
||||
block[threadIdx.y][threadIdx.x] = idata[index_in];
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
cg::sync(cta);
|
||||
|
||||
// write the transposed matrix tile to global memory
|
||||
xIndex = blockIdx.y * BLOCK_DIM + threadIdx.x;
|
||||
yIndex = blockIdx.x * BLOCK_DIM + threadIdx.y;
|
||||
// write the transposed matrix tile to global memory
|
||||
xIndex = blockIdx.y * BLOCK_DIM + threadIdx.x;
|
||||
yIndex = blockIdx.x * BLOCK_DIM + threadIdx.y;
|
||||
|
||||
if ((xIndex < height) && (yIndex < width)) {
|
||||
unsigned int index_out = yIndex * height + xIndex;
|
||||
odata[index_out] = block[threadIdx.x][threadIdx.y];
|
||||
}
|
||||
if ((xIndex < height) && (yIndex < width)) {
|
||||
unsigned int index_out = yIndex * height + xIndex;
|
||||
odata[index_out] = block[threadIdx.x][threadIdx.y];
|
||||
}
|
||||
}
|
||||
|
||||
// RGBA version
|
||||
// reads from 32-bit uint array holding 8-bit RGBA
|
||||
|
||||
// convert floating point rgba color to 32-bit integer
|
||||
__device__ uint rgbaFloatToInt(float4 rgba) {
|
||||
rgba.x = __saturatef(rgba.x); // clamp to [0.0, 1.0]
|
||||
rgba.y = __saturatef(rgba.y);
|
||||
rgba.z = __saturatef(rgba.z);
|
||||
rgba.w = __saturatef(rgba.w);
|
||||
return (uint(rgba.w * 255) << 24) | (uint(rgba.z * 255) << 16) |
|
||||
(uint(rgba.y * 255) << 8) | uint(rgba.x * 255);
|
||||
__device__ uint rgbaFloatToInt(float4 rgba)
|
||||
{
|
||||
rgba.x = __saturatef(rgba.x); // clamp to [0.0, 1.0]
|
||||
rgba.y = __saturatef(rgba.y);
|
||||
rgba.z = __saturatef(rgba.z);
|
||||
rgba.w = __saturatef(rgba.w);
|
||||
return (uint(rgba.w * 255) << 24) | (uint(rgba.z * 255) << 16) | (uint(rgba.y * 255) << 8) | uint(rgba.x * 255);
|
||||
}
|
||||
|
||||
// convert from 32-bit int to float4
|
||||
__device__ float4 rgbaIntToFloat(uint c) {
|
||||
float4 rgba;
|
||||
rgba.x = (c & 0xff) / 255.0f;
|
||||
rgba.y = ((c >> 8) & 0xff) / 255.0f;
|
||||
rgba.z = ((c >> 16) & 0xff) / 255.0f;
|
||||
rgba.w = ((c >> 24) & 0xff) / 255.0f;
|
||||
return rgba;
|
||||
__device__ float4 rgbaIntToFloat(uint c)
|
||||
{
|
||||
float4 rgba;
|
||||
rgba.x = (c & 0xff) / 255.0f;
|
||||
rgba.y = ((c >> 8) & 0xff) / 255.0f;
|
||||
rgba.z = ((c >> 16) & 0xff) / 255.0f;
|
||||
rgba.w = ((c >> 24) & 0xff) / 255.0f;
|
||||
return rgba;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -108,44 +110,44 @@ __device__ float4 rgbaIntToFloat(uint c) {
|
||||
a - blur parameter
|
||||
*/
|
||||
|
||||
__global__ void d_simpleRecursive_rgba(uint *id, uint *od, int w, int h,
|
||||
float a) {
|
||||
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
__global__ void d_simpleRecursive_rgba(uint *id, uint *od, int w, int h, float a)
|
||||
{
|
||||
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
|
||||
if (x >= w) return;
|
||||
if (x >= w)
|
||||
return;
|
||||
|
||||
id += x; // advance pointers to correct column
|
||||
od += x;
|
||||
id += x; // advance pointers to correct column
|
||||
od += x;
|
||||
|
||||
// forward pass
|
||||
float4 yp = rgbaIntToFloat(*id); // previous output
|
||||
// forward pass
|
||||
float4 yp = rgbaIntToFloat(*id); // previous output
|
||||
|
||||
for (int y = 0; y < h; y++) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc =
|
||||
xc + a * (yp - xc); // simple lerp between current and previous value
|
||||
*od = rgbaFloatToInt(yc);
|
||||
id += w;
|
||||
od += w; // move to next row
|
||||
yp = yc;
|
||||
}
|
||||
for (int y = 0; y < h; y++) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc = xc + a * (yp - xc); // simple lerp between current and previous value
|
||||
*od = rgbaFloatToInt(yc);
|
||||
id += w;
|
||||
od += w; // move to next row
|
||||
yp = yc;
|
||||
}
|
||||
|
||||
// reset pointers to point to last element in column
|
||||
id -= w;
|
||||
od -= w;
|
||||
|
||||
// reverse pass
|
||||
// ensures response is symmetrical
|
||||
yp = rgbaIntToFloat(*id);
|
||||
|
||||
for (int y = h - 1; y >= 0; y--) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc = xc + a * (yp - xc);
|
||||
*od = rgbaFloatToInt((rgbaIntToFloat(*od) + yc) * 0.5f);
|
||||
// reset pointers to point to last element in column
|
||||
id -= w;
|
||||
od -= w; // move to previous row
|
||||
yp = yc;
|
||||
}
|
||||
od -= w;
|
||||
|
||||
// reverse pass
|
||||
// ensures response is symmetrical
|
||||
yp = rgbaIntToFloat(*id);
|
||||
|
||||
for (int y = h - 1; y >= 0; y--) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc = xc + a * (yp - xc);
|
||||
*od = rgbaFloatToInt((rgbaIntToFloat(*od) + yc) * 0.5f);
|
||||
id -= w;
|
||||
od -= w; // move to previous row
|
||||
yp = yc;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -159,65 +161,75 @@ __global__ void d_simpleRecursive_rgba(uint *id, uint *od, int w, int h,
|
||||
a0-a3, b1, b2, coefp, coefn - filter parameters
|
||||
*/
|
||||
|
||||
__global__ void d_recursiveGaussian_rgba(uint *id, uint *od, int w, int h,
|
||||
float a0, float a1, float a2, float a3,
|
||||
float b1, float b2, float coefp,
|
||||
float coefn) {
|
||||
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
__global__ void d_recursiveGaussian_rgba(uint *id,
|
||||
uint *od,
|
||||
int w,
|
||||
int h,
|
||||
float a0,
|
||||
float a1,
|
||||
float a2,
|
||||
float a3,
|
||||
float b1,
|
||||
float b2,
|
||||
float coefp,
|
||||
float coefn)
|
||||
{
|
||||
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
|
||||
if (x >= w) return;
|
||||
if (x >= w)
|
||||
return;
|
||||
|
||||
id += x; // advance pointers to correct column
|
||||
od += x;
|
||||
id += x; // advance pointers to correct column
|
||||
od += x;
|
||||
|
||||
// forward pass
|
||||
float4 xp = make_float4(0.0f); // previous input
|
||||
float4 yp = make_float4(0.0f); // previous output
|
||||
float4 yb = make_float4(0.0f); // previous output by 2
|
||||
// forward pass
|
||||
float4 xp = make_float4(0.0f); // previous input
|
||||
float4 yp = make_float4(0.0f); // previous output
|
||||
float4 yb = make_float4(0.0f); // previous output by 2
|
||||
#if CLAMP_TO_EDGE
|
||||
xp = rgbaIntToFloat(*id);
|
||||
yb = coefp * xp;
|
||||
yp = yb;
|
||||
xp = rgbaIntToFloat(*id);
|
||||
yb = coefp * xp;
|
||||
yp = yb;
|
||||
#endif
|
||||
|
||||
for (int y = 0; y < h; y++) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc = a0 * xc + a1 * xp - b1 * yp - b2 * yb;
|
||||
*od = rgbaFloatToInt(yc);
|
||||
id += w;
|
||||
od += w; // move to next row
|
||||
xp = xc;
|
||||
yb = yp;
|
||||
yp = yc;
|
||||
}
|
||||
for (int y = 0; y < h; y++) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc = a0 * xc + a1 * xp - b1 * yp - b2 * yb;
|
||||
*od = rgbaFloatToInt(yc);
|
||||
id += w;
|
||||
od += w; // move to next row
|
||||
xp = xc;
|
||||
yb = yp;
|
||||
yp = yc;
|
||||
}
|
||||
|
||||
// reset pointers to point to last element in column
|
||||
id -= w;
|
||||
od -= w;
|
||||
|
||||
// reverse pass
|
||||
// ensures response is symmetrical
|
||||
float4 xn = make_float4(0.0f);
|
||||
float4 xa = make_float4(0.0f);
|
||||
float4 yn = make_float4(0.0f);
|
||||
float4 ya = make_float4(0.0f);
|
||||
#if CLAMP_TO_EDGE
|
||||
xn = xa = rgbaIntToFloat(*id);
|
||||
yn = coefn * xn;
|
||||
ya = yn;
|
||||
#endif
|
||||
|
||||
for (int y = h - 1; y >= 0; y--) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc = a2 * xn + a3 * xa - b1 * yn - b2 * ya;
|
||||
xa = xn;
|
||||
xn = xc;
|
||||
ya = yn;
|
||||
yn = yc;
|
||||
*od = rgbaFloatToInt(rgbaIntToFloat(*od) + yc);
|
||||
// reset pointers to point to last element in column
|
||||
id -= w;
|
||||
od -= w; // move to previous row
|
||||
}
|
||||
od -= w;
|
||||
|
||||
// reverse pass
|
||||
// ensures response is symmetrical
|
||||
float4 xn = make_float4(0.0f);
|
||||
float4 xa = make_float4(0.0f);
|
||||
float4 yn = make_float4(0.0f);
|
||||
float4 ya = make_float4(0.0f);
|
||||
#if CLAMP_TO_EDGE
|
||||
xn = xa = rgbaIntToFloat(*id);
|
||||
yn = coefn * xn;
|
||||
ya = yn;
|
||||
#endif
|
||||
|
||||
for (int y = h - 1; y >= 0; y--) {
|
||||
float4 xc = rgbaIntToFloat(*id);
|
||||
float4 yc = a2 * xn + a3 * xa - b1 * yn - b2 * ya;
|
||||
xa = xn;
|
||||
xn = xc;
|
||||
ya = yn;
|
||||
yn = yc;
|
||||
*od = rgbaFloatToInt(rgbaIntToFloat(*od) + yc);
|
||||
id -= w;
|
||||
od -= w; // move to previous row
|
||||
}
|
||||
}
|
||||
|
||||
#endif // #ifndef _GAUSSIAN_KERNEL_H_
|
||||
#endif // #ifndef _GAUSSIAN_KERNEL_H_
|
||||
|
||||
Reference in New Issue
Block a user