mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-26 06:38:16 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -38,15 +38,13 @@
|
||||
#endif
|
||||
|
||||
// Includes
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <helper_functions.h> // helper functions for timing, string parsing
|
||||
|
||||
#include <cuda_runtime.h> // CUDA Runtime
|
||||
#include <cuda_gl_interop.h> // CUDA OpenGL interop
|
||||
|
||||
#include <helper_cuda.h> // includes for CUDA initialization and error checking
|
||||
#include <cuda_runtime.h> // CUDA Runtime
|
||||
#include <helper_cuda.h> // includes for CUDA initialization and error checking
|
||||
#include <helper_functions.h> // helper functions for timing, string parsing
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "FunctionPointers_kernels.h"
|
||||
|
||||
@@ -70,441 +68,439 @@ void initializeData(char *file);
|
||||
|
||||
static const char *sSDKsample = "CUDA Function Pointers (SobelFilter)";
|
||||
|
||||
const char *filterMode[] = {"No Filtering", "Sobel Texture",
|
||||
"Sobel SMEM+Texture", NULL};
|
||||
const char *filterMode[] = {"No Filtering", "Sobel Texture", "Sobel SMEM+Texture", NULL};
|
||||
|
||||
static int wWidth = 512; // Window width
|
||||
static int wHeight = 512; // Window height
|
||||
static int imWidth = 0; // Image width
|
||||
static int wWidth = 512; // Window width
|
||||
static int wHeight = 512; // Window height
|
||||
static int imWidth = 0; // Image width
|
||||
static int imHeight = 0; // Image height
|
||||
static int blockOp = 0;
|
||||
static int pointOp = 1;
|
||||
static int blockOp = 0;
|
||||
static int pointOp = 1;
|
||||
|
||||
// Code to handle Auto verification
|
||||
const int frameCheckNumber = 4;
|
||||
int fpsCount = 0; // FPS count for averaging
|
||||
int fpsLimit = 8; // FPS limit for sampling
|
||||
unsigned int frameCount = 0;
|
||||
StopWatchInterface *timer = NULL;
|
||||
unsigned int g_Bpp;
|
||||
const int frameCheckNumber = 4;
|
||||
int fpsCount = 0; // FPS count for averaging
|
||||
int fpsLimit = 8; // FPS limit for sampling
|
||||
unsigned int frameCount = 0;
|
||||
StopWatchInterface *timer = NULL;
|
||||
unsigned int g_Bpp;
|
||||
|
||||
int g_TotalErrors = 0;
|
||||
|
||||
int *pArgc = NULL;
|
||||
int *pArgc = NULL;
|
||||
char **pArgv = NULL;
|
||||
|
||||
bool g_bQAReadback = false;
|
||||
|
||||
// Display Data
|
||||
static GLuint pbo_buffer = 0; // Front and back CA buffers
|
||||
struct cudaGraphicsResource
|
||||
*cuda_pbo_resource; // CUDA Graphics Resource (to transfer PBO)
|
||||
static GLuint pbo_buffer = 0; // Front and back CA buffers
|
||||
struct cudaGraphicsResource *cuda_pbo_resource; // CUDA Graphics Resource (to transfer PBO)
|
||||
|
||||
static GLuint texid = 0; // Texture for display
|
||||
unsigned char *pixels = NULL; // Image pixel data on the host
|
||||
float imageScale = 1.f; // Image exposure
|
||||
static GLuint texid = 0; // Texture for display
|
||||
unsigned char *pixels = NULL; // Image pixel data on the host
|
||||
float imageScale = 1.f; // Image exposure
|
||||
enum SobelDisplayMode g_SobelDisplayMode;
|
||||
|
||||
#define OFFSET(i) ((char *)NULL + (i))
|
||||
#define MAX(a, b) ((a > b) ? a : b)
|
||||
#define REFRESH_DELAY 10 // ms
|
||||
#define OFFSET(i) ((char *)NULL + (i))
|
||||
#define MAX(a, b) ((a > b) ? a : b)
|
||||
#define REFRESH_DELAY 10 // ms
|
||||
|
||||
void computeFPS() {
|
||||
frameCount++;
|
||||
fpsCount++;
|
||||
void computeFPS()
|
||||
{
|
||||
frameCount++;
|
||||
fpsCount++;
|
||||
|
||||
if (fpsCount == fpsLimit) {
|
||||
char fps[256];
|
||||
float ifps = 1.f / (sdkGetAverageTimerValue(&timer) / 1000.f);
|
||||
sprintf(fps, "FunctionPointers [CUDA Edge Detection] (%s): %3.1f fps",
|
||||
filterMode[g_SobelDisplayMode], ifps);
|
||||
if (fpsCount == fpsLimit) {
|
||||
char fps[256];
|
||||
float ifps = 1.f / (sdkGetAverageTimerValue(&timer) / 1000.f);
|
||||
sprintf(fps, "FunctionPointers [CUDA Edge Detection] (%s): %3.1f fps", filterMode[g_SobelDisplayMode], ifps);
|
||||
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
|
||||
fpsLimit = (int)MAX(ifps, 1.f);
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
fpsLimit = (int)MAX(ifps, 1.f);
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
}
|
||||
|
||||
// This is the normal display path
|
||||
void display(void) {
|
||||
sdkStartTimer(&timer);
|
||||
void display(void)
|
||||
{
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
// Sobel operation
|
||||
Pixel *data = NULL;
|
||||
// Sobel operation
|
||||
Pixel *data = NULL;
|
||||
|
||||
// map PBO to get CUDA device pointer
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
|
||||
(void **)&data, &num_bytes, cuda_pbo_resource));
|
||||
// printf("CUDA mapped PBO: May access %ld bytes\n", num_bytes);
|
||||
// map PBO to get CUDA device pointer
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void **)&data, &num_bytes, cuda_pbo_resource));
|
||||
// printf("CUDA mapped PBO: May access %ld bytes\n", num_bytes);
|
||||
|
||||
sobelFilter(data, imWidth, imHeight, g_SobelDisplayMode, imageScale, blockOp,
|
||||
pointOp);
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
|
||||
sobelFilter(data, imWidth, imHeight, g_SobelDisplayMode, imageScale, blockOp, pointOp);
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, imWidth, imHeight, GL_LUMINANCE,
|
||||
GL_UNSIGNED_BYTE, OFFSET(0));
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, imWidth, imHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, OFFSET(0));
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
glVertex2f(0, 0);
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex2f(0, 1);
|
||||
glTexCoord2f(1, 0);
|
||||
glVertex2f(1, 1);
|
||||
glTexCoord2f(1, 1);
|
||||
glVertex2f(1, 0);
|
||||
glTexCoord2f(0, 1);
|
||||
glEnd();
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBegin(GL_QUADS);
|
||||
glVertex2f(0, 0);
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex2f(0, 1);
|
||||
glTexCoord2f(1, 0);
|
||||
glVertex2f(1, 1);
|
||||
glTexCoord2f(1, 1);
|
||||
glVertex2f(1, 0);
|
||||
glTexCoord2f(0, 1);
|
||||
glEnd();
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
glutSwapBuffers();
|
||||
glutSwapBuffers();
|
||||
|
||||
sdkStopTimer(&timer);
|
||||
computeFPS();
|
||||
sdkStopTimer(&timer);
|
||||
computeFPS();
|
||||
}
|
||||
|
||||
void timerEvent(int value) {
|
||||
if (glutGetWindow()) {
|
||||
glutPostRedisplay();
|
||||
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
|
||||
}
|
||||
void timerEvent(int value)
|
||||
{
|
||||
if (glutGetWindow()) {
|
||||
glutPostRedisplay();
|
||||
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int /*x*/, int /*y*/) {
|
||||
char temp[256];
|
||||
void keyboard(unsigned char key, int /*x*/, int /*y*/)
|
||||
{
|
||||
char temp[256];
|
||||
|
||||
switch (key) {
|
||||
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 '-':
|
||||
imageScale -= 0.1f;
|
||||
printf("brightness = %4.2f\n", imageScale);
|
||||
break;
|
||||
imageScale -= 0.1f;
|
||||
printf("brightness = %4.2f\n", imageScale);
|
||||
break;
|
||||
|
||||
case '=':
|
||||
imageScale += 0.1f;
|
||||
printf("brightness = %4.2f\n", imageScale);
|
||||
break;
|
||||
imageScale += 0.1f;
|
||||
printf("brightness = %4.2f\n", imageScale);
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
case 'I':
|
||||
g_SobelDisplayMode = SOBELDISPLAY_IMAGE;
|
||||
sprintf(temp, "Function Pointers [CUDA Edge Detection] (%s)",
|
||||
filterMode[g_SobelDisplayMode]);
|
||||
glutSetWindowTitle(temp);
|
||||
break;
|
||||
g_SobelDisplayMode = SOBELDISPLAY_IMAGE;
|
||||
sprintf(temp, "Function Pointers [CUDA Edge Detection] (%s)", filterMode[g_SobelDisplayMode]);
|
||||
glutSetWindowTitle(temp);
|
||||
break;
|
||||
|
||||
case 's':
|
||||
case 'S':
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELSHARED;
|
||||
sprintf(temp, "Function Pointers [CUDA Edge Detection] (%s)",
|
||||
filterMode[g_SobelDisplayMode]);
|
||||
glutSetWindowTitle(temp);
|
||||
break;
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELSHARED;
|
||||
sprintf(temp, "Function Pointers [CUDA Edge Detection] (%s)", filterMode[g_SobelDisplayMode]);
|
||||
glutSetWindowTitle(temp);
|
||||
break;
|
||||
|
||||
case 't':
|
||||
case 'T':
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELTEX;
|
||||
sprintf(temp, "Function Pointers [CUDA Edge Detection] (%s)",
|
||||
filterMode[g_SobelDisplayMode]);
|
||||
glutSetWindowTitle(temp);
|
||||
break;
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELTEX;
|
||||
sprintf(temp, "Function Pointers [CUDA Edge Detection] (%s)", filterMode[g_SobelDisplayMode]);
|
||||
glutSetWindowTitle(temp);
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
case 'B':
|
||||
blockOp = (blockOp + 1) % LAST_BLOCK_FILTER;
|
||||
break;
|
||||
blockOp = (blockOp + 1) % LAST_BLOCK_FILTER;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
case 'P':
|
||||
pointOp = (pointOp + 1) % LAST_POINT_FILTER;
|
||||
break;
|
||||
pointOp = (pointOp + 1) % LAST_POINT_FILTER;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void reshape(int x, int y) {
|
||||
glViewport(0, 0, x, y);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0, 1, 0, 1, 0, 1);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
void reshape(int x, int y)
|
||||
{
|
||||
glViewport(0, 0, x, y);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0, 1, 0, 1, 0, 1);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
}
|
||||
|
||||
void cleanup(void) {
|
||||
cudaGraphicsUnregisterResource(cuda_pbo_resource);
|
||||
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||
glDeleteBuffers(1, &pbo_buffer);
|
||||
glDeleteTextures(1, &texid);
|
||||
deleteTexture();
|
||||
|
||||
sdkDeleteTimer(&timer);
|
||||
}
|
||||
|
||||
void initializeData(char *file) {
|
||||
GLint bsize;
|
||||
unsigned int w, h;
|
||||
size_t file_length = strlen(file);
|
||||
|
||||
if (!strcmp(&file[file_length - 3], "pgm")) {
|
||||
if (sdkLoadPGM<unsigned char>(file, &pixels, &w, &h) != true) {
|
||||
printf("Failed to load PGM image file: %s\n", file);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
g_Bpp = 1;
|
||||
} else if (!strcmp(&file[file_length - 3], "ppm")) {
|
||||
if (sdkLoadPPM4(file, &pixels, &w, &h) != true) {
|
||||
printf("Failed to load PPM image file: %s\n", file);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
g_Bpp = 4;
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
imWidth = (int)w;
|
||||
imHeight = (int)h;
|
||||
setupTexture(imWidth, imHeight, pixels, g_Bpp);
|
||||
|
||||
// copy function pointer tables to host side for later use
|
||||
setupFunctionTables();
|
||||
|
||||
memset(pixels, 0x0, g_Bpp * sizeof(Pixel) * imWidth * imHeight);
|
||||
|
||||
if (!g_bQAReadback) {
|
||||
// use OpenGL Path
|
||||
glGenBuffers(1, &pbo_buffer);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer);
|
||||
glBufferData(GL_PIXEL_UNPACK_BUFFER,
|
||||
g_Bpp * sizeof(Pixel) * imWidth * imHeight, pixels,
|
||||
GL_STREAM_DRAW);
|
||||
|
||||
glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER, GL_BUFFER_SIZE, &bsize);
|
||||
|
||||
if ((GLuint)bsize != (g_Bpp * sizeof(Pixel) * imWidth * imHeight)) {
|
||||
printf("Buffer object (%d) has incorrect size (%d).\n",
|
||||
(unsigned)pbo_buffer, (unsigned)bsize);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
void cleanup(void)
|
||||
{
|
||||
cudaGraphicsUnregisterResource(cuda_pbo_resource);
|
||||
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||
glDeleteBuffers(1, &pbo_buffer);
|
||||
glDeleteTextures(1, &texid);
|
||||
deleteTexture();
|
||||
|
||||
// register this buffer object with CUDA
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(
|
||||
&cuda_pbo_resource, pbo_buffer, cudaGraphicsMapFlagsWriteDiscard));
|
||||
|
||||
glGenTextures(1, &texid);
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, ((g_Bpp == 1) ? GL_LUMINANCE : GL_BGRA),
|
||||
imWidth, imHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
}
|
||||
sdkDeleteTimer(&timer);
|
||||
}
|
||||
|
||||
void loadDefaultImage(char *loc_exec) {
|
||||
printf("Reading image: teapot512.pgm\n");
|
||||
const char *image_filename = "teapot512.pgm";
|
||||
char *image_path = sdkFindFilePath(image_filename, loc_exec);
|
||||
void initializeData(char *file)
|
||||
{
|
||||
GLint bsize;
|
||||
unsigned int w, h;
|
||||
size_t file_length = strlen(file);
|
||||
|
||||
if (image_path == NULL) {
|
||||
printf("Failed to read image file: <%s>\n", image_filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!strcmp(&file[file_length - 3], "pgm")) {
|
||||
if (sdkLoadPGM<unsigned char>(file, &pixels, &w, &h) != true) {
|
||||
printf("Failed to load PGM image file: %s\n", file);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
initializeData(image_path);
|
||||
free(image_path);
|
||||
g_Bpp = 1;
|
||||
}
|
||||
else if (!strcmp(&file[file_length - 3], "ppm")) {
|
||||
if (sdkLoadPPM4(file, &pixels, &w, &h) != true) {
|
||||
printf("Failed to load PPM image file: %s\n", file);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
g_Bpp = 4;
|
||||
}
|
||||
else {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
imWidth = (int)w;
|
||||
imHeight = (int)h;
|
||||
setupTexture(imWidth, imHeight, pixels, g_Bpp);
|
||||
|
||||
// copy function pointer tables to host side for later use
|
||||
setupFunctionTables();
|
||||
|
||||
memset(pixels, 0x0, g_Bpp * sizeof(Pixel) * imWidth * imHeight);
|
||||
|
||||
if (!g_bQAReadback) {
|
||||
// use OpenGL Path
|
||||
glGenBuffers(1, &pbo_buffer);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer);
|
||||
glBufferData(GL_PIXEL_UNPACK_BUFFER, g_Bpp * sizeof(Pixel) * imWidth * imHeight, pixels, GL_STREAM_DRAW);
|
||||
|
||||
glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER, GL_BUFFER_SIZE, &bsize);
|
||||
|
||||
if ((GLuint)bsize != (g_Bpp * sizeof(Pixel) * imWidth * imHeight)) {
|
||||
printf("Buffer object (%d) has incorrect size (%d).\n", (unsigned)pbo_buffer, (unsigned)bsize);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||
|
||||
// register this buffer object with CUDA
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(&cuda_pbo_resource, pbo_buffer, cudaGraphicsMapFlagsWriteDiscard));
|
||||
|
||||
glGenTextures(1, &texid);
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glTexImage2D(GL_TEXTURE_2D,
|
||||
0,
|
||||
((g_Bpp == 1) ? GL_LUMINANCE : GL_BGRA),
|
||||
imWidth,
|
||||
imHeight,
|
||||
0,
|
||||
GL_LUMINANCE,
|
||||
GL_UNSIGNED_BYTE,
|
||||
NULL);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void initGL(int *argc, char **argv) {
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
|
||||
glutInitWindowSize(wWidth, wHeight);
|
||||
glutCreateWindow("Function Pointers [CUDA Edge Detection]n");
|
||||
void loadDefaultImage(char *loc_exec)
|
||||
{
|
||||
printf("Reading image: teapot512.pgm\n");
|
||||
const char *image_filename = "teapot512.pgm";
|
||||
char *image_path = sdkFindFilePath(image_filename, loc_exec);
|
||||
|
||||
if (!isGLVersionSupported(1, 5) ||
|
||||
!areGLExtensionsSupported(
|
||||
"GL_ARB_vertex_buffer_object GL_ARB_pixel_buffer_object")) {
|
||||
fprintf(stderr, "Error: failed to get minimal extensions for demo\n");
|
||||
fprintf(stderr, "This sample requires:\n");
|
||||
fprintf(stderr, " OpenGL version 1.5\n");
|
||||
fprintf(stderr, " GL_ARB_vertex_buffer_object\n");
|
||||
fprintf(stderr, " GL_ARB_pixel_buffer_object\n");
|
||||
if (image_path == NULL) {
|
||||
printf("Failed to read image file: <%s>\n", image_filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
initializeData(image_path);
|
||||
free(image_path);
|
||||
}
|
||||
|
||||
void runAutoTest(int argc, char *argv[]) {
|
||||
printf("[%s] (automated testing w/ readback)\n", sSDKsample);
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
void initGL(int *argc, char **argv)
|
||||
{
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
|
||||
glutInitWindowSize(wWidth, wHeight);
|
||||
glutCreateWindow("Function Pointers [CUDA Edge Detection]n");
|
||||
|
||||
loadDefaultImage(argv[0]);
|
||||
if (!isGLVersionSupported(1, 5)
|
||||
|| !areGLExtensionsSupported("GL_ARB_vertex_buffer_object GL_ARB_pixel_buffer_object")) {
|
||||
fprintf(stderr, "Error: failed to get minimal extensions for demo\n");
|
||||
fprintf(stderr, "This sample requires:\n");
|
||||
fprintf(stderr, " OpenGL version 1.5\n");
|
||||
fprintf(stderr, " GL_ARB_vertex_buffer_object\n");
|
||||
fprintf(stderr, " GL_ARB_pixel_buffer_object\n");
|
||||
|
||||
Pixel *d_result;
|
||||
checkCudaErrors(
|
||||
cudaMalloc((void **)&d_result, imWidth * imHeight * sizeof(Pixel)));
|
||||
|
||||
char *ref_file = NULL;
|
||||
char dump_file[256];
|
||||
|
||||
int mode = 0;
|
||||
mode = getCmdLineArgumentInt(argc, (const char **)argv, "mode");
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
|
||||
|
||||
switch (mode) {
|
||||
case 0:
|
||||
g_SobelDisplayMode = SOBELDISPLAY_IMAGE;
|
||||
sprintf(dump_file, "teapot512_orig.pgm");
|
||||
break;
|
||||
|
||||
case 1:
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELTEX;
|
||||
sprintf(dump_file, "teapot512_tex.pgm");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELSHARED;
|
||||
sprintf(dump_file, "teapot512_shared.pgm");
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Invalid Filter Mode File\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("AutoTest: %s <%s>\n", sSDKsample, filterMode[g_SobelDisplayMode]);
|
||||
sobelFilter(d_result, imWidth, imHeight, g_SobelDisplayMode, imageScale,
|
||||
blockOp, pointOp);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
unsigned char *h_result =
|
||||
(unsigned char *)malloc(imWidth * imHeight * sizeof(Pixel));
|
||||
checkCudaErrors(cudaMemcpy(h_result, d_result,
|
||||
imWidth * imHeight * sizeof(Pixel),
|
||||
cudaMemcpyDeviceToHost));
|
||||
sdkSavePGM(dump_file, h_result, imWidth, imHeight);
|
||||
|
||||
if (!sdkComparePGM(dump_file, sdkFindFilePath(ref_file, argv[0]),
|
||||
MAX_EPSILON_ERROR, 0.15f, false)) {
|
||||
g_TotalErrors++;
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaFree(d_result));
|
||||
free(h_result);
|
||||
|
||||
if (g_TotalErrors != 0) {
|
||||
printf("Test failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Test passed!\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
pArgc = &argc;
|
||||
pArgv = argv;
|
||||
|
||||
#if defined(__linux__)
|
||||
setenv("DISPLAY", ":0", 0);
|
||||
#endif
|
||||
|
||||
printf("%s Starting...\n\n", argv[0]);
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
|
||||
printf("\nUsage: FunctionPointers (SobelFilter) <options>\n");
|
||||
printf("\t\t-mode=n (0=original, 1=texture, 2=smem + texture)\n");
|
||||
printf("\t\t-file=ref_orig.pgm (ref_tex.pgm, ref_shared.pgm)\n\n");
|
||||
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
g_bQAReadback = true;
|
||||
runAutoTest(argc, argv);
|
||||
}
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
|
||||
printf(
|
||||
" This SDK does not explicitly support -device=n when running with "
|
||||
"OpenGL.\n");
|
||||
printf(
|
||||
" When specifying -device=n (n=0,1,2,....) the sample must not use "
|
||||
"OpenGL.\n");
|
||||
printf(" See details below to run without OpenGL:\n\n");
|
||||
printf(" > %s -device=n\n\n", argv[0]);
|
||||
printf("exiting...\n");
|
||||
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
if (!g_bQAReadback) {
|
||||
// First initialize OpenGL context, so we can properly set the GL for CUDA.
|
||||
// This is necessary in order to achieve optimal performance with
|
||||
// OpenGL/CUDA interop.
|
||||
initGL(&argc, argv);
|
||||
|
||||
int dev = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
sdkCreateTimer(&timer);
|
||||
sdkResetTimer(&timer);
|
||||
|
||||
glutDisplayFunc(display);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutReshapeFunc(reshape);
|
||||
void runAutoTest(int argc, char *argv[])
|
||||
{
|
||||
printf("[%s] (automated testing w/ readback)\n", sSDKsample);
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
loadDefaultImage(argv[0]);
|
||||
|
||||
// If code is not printing the USage, then we execute this path.
|
||||
printf("I: display Image (no filtering)\n");
|
||||
printf("T: display Sobel Edge Detection (Using Texture)\n");
|
||||
printf("S: display Sobel Edge Detection (Using SMEM+Texture)\n");
|
||||
printf("Use the '-' and '=' keys to change the brightness.\n");
|
||||
printf("b: switch block filter operation (Mean/Sobel)\n");
|
||||
printf("p: switch point filter operation (Threshold ON/OFF)\n");
|
||||
fflush(stdout);
|
||||
Pixel *d_result;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_result, imWidth * imHeight * sizeof(Pixel)));
|
||||
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
atexit(cleanup);
|
||||
#else
|
||||
glutCloseFunc(cleanup);
|
||||
char *ref_file = NULL;
|
||||
char dump_file[256];
|
||||
|
||||
int mode = 0;
|
||||
mode = getCmdLineArgumentInt(argc, (const char **)argv, "mode");
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
|
||||
|
||||
switch (mode) {
|
||||
case 0:
|
||||
g_SobelDisplayMode = SOBELDISPLAY_IMAGE;
|
||||
sprintf(dump_file, "teapot512_orig.pgm");
|
||||
break;
|
||||
|
||||
case 1:
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELTEX;
|
||||
sprintf(dump_file, "teapot512_tex.pgm");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
g_SobelDisplayMode = SOBELDISPLAY_SOBELSHARED;
|
||||
sprintf(dump_file, "teapot512_shared.pgm");
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Invalid Filter Mode File\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("AutoTest: %s <%s>\n", sSDKsample, filterMode[g_SobelDisplayMode]);
|
||||
sobelFilter(d_result, imWidth, imHeight, g_SobelDisplayMode, imageScale, blockOp, pointOp);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
unsigned char *h_result = (unsigned char *)malloc(imWidth * imHeight * sizeof(Pixel));
|
||||
checkCudaErrors(cudaMemcpy(h_result, d_result, imWidth * imHeight * sizeof(Pixel), cudaMemcpyDeviceToHost));
|
||||
sdkSavePGM(dump_file, h_result, imWidth, imHeight);
|
||||
|
||||
if (!sdkComparePGM(dump_file, sdkFindFilePath(ref_file, argv[0]), MAX_EPSILON_ERROR, 0.15f, false)) {
|
||||
g_TotalErrors++;
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaFree(d_result));
|
||||
free(h_result);
|
||||
|
||||
if (g_TotalErrors != 0) {
|
||||
printf("Test failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Test passed!\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pArgc = &argc;
|
||||
pArgv = argv;
|
||||
|
||||
#if defined(__linux__)
|
||||
setenv("DISPLAY", ":0", 0);
|
||||
#endif
|
||||
|
||||
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
|
||||
glutMainLoop();
|
||||
}
|
||||
printf("%s Starting...\n\n", argv[0]);
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
|
||||
printf("\nUsage: FunctionPointers (SobelFilter) <options>\n");
|
||||
printf("\t\t-mode=n (0=original, 1=texture, 2=smem + texture)\n");
|
||||
printf("\t\t-file=ref_orig.pgm (ref_tex.pgm, ref_shared.pgm)\n\n");
|
||||
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
g_bQAReadback = true;
|
||||
runAutoTest(argc, argv);
|
||||
}
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
|
||||
printf(" This SDK does not explicitly support -device=n when running with "
|
||||
"OpenGL.\n");
|
||||
printf(" When specifying -device=n (n=0,1,2,....) the sample must not use "
|
||||
"OpenGL.\n");
|
||||
printf(" See details below to run without OpenGL:\n\n");
|
||||
printf(" > %s -device=n\n\n", argv[0]);
|
||||
printf("exiting...\n");
|
||||
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
if (!g_bQAReadback) {
|
||||
// First initialize OpenGL context, so we can properly set the GL for CUDA.
|
||||
// This is necessary in order to achieve optimal performance with
|
||||
// OpenGL/CUDA interop.
|
||||
initGL(&argc, argv);
|
||||
|
||||
int dev = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
sdkCreateTimer(&timer);
|
||||
sdkResetTimer(&timer);
|
||||
|
||||
glutDisplayFunc(display);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutReshapeFunc(reshape);
|
||||
|
||||
loadDefaultImage(argv[0]);
|
||||
|
||||
// If code is not printing the USage, then we execute this path.
|
||||
printf("I: display Image (no filtering)\n");
|
||||
printf("T: display Sobel Edge Detection (Using Texture)\n");
|
||||
printf("S: display Sobel Edge Detection (Using SMEM+Texture)\n");
|
||||
printf("Use the '-' and '=' keys to change the brightness.\n");
|
||||
printf("b: switch block filter operation (Mean/Sobel)\n");
|
||||
printf("p: switch point filter operation (Threshold ON/OFF)\n");
|
||||
fflush(stdout);
|
||||
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
atexit(cleanup);
|
||||
#else
|
||||
glutCloseFunc(cleanup);
|
||||
#endif
|
||||
|
||||
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
|
||||
glutMainLoop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cooperative_groups.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cooperative_groups.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
|
||||
@@ -36,9 +36,9 @@ namespace cg = cooperative_groups;
|
||||
#include "FunctionPointers_kernels.h"
|
||||
|
||||
// Texture object for reading image
|
||||
cudaTextureObject_t tex;
|
||||
cudaTextureObject_t tex;
|
||||
extern __shared__ unsigned char LocalBlock[];
|
||||
static cudaArray *array = NULL;
|
||||
static cudaArray *array = NULL;
|
||||
|
||||
#define RADIUS 1
|
||||
|
||||
@@ -47,7 +47,7 @@ static cudaArray *array = NULL;
|
||||
#define THRESHOLD 150.0f
|
||||
|
||||
#ifdef FIXED_BLOCKWIDTH
|
||||
#define BlockWidth 80
|
||||
#define BlockWidth 80
|
||||
#define SharedPitch 384
|
||||
#endif
|
||||
|
||||
@@ -55,59 +55,73 @@ static cudaArray *array = NULL;
|
||||
//__device__ unsigned char (*pointFunction)(unsigned char, float ) = NULL;
|
||||
// or by using typedef's like below:
|
||||
|
||||
typedef unsigned char (*blockFunction_t)(unsigned char, unsigned char,
|
||||
unsigned char, unsigned char,
|
||||
unsigned char, unsigned char,
|
||||
unsigned char, unsigned char,
|
||||
unsigned char, float);
|
||||
typedef unsigned char (*blockFunction_t)(unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
float);
|
||||
|
||||
typedef unsigned char (*pointFunction_t)(unsigned char, float);
|
||||
|
||||
__device__ blockFunction_t blockFunction;
|
||||
|
||||
__device__ unsigned char ComputeSobel(unsigned char ul, // upper left
|
||||
unsigned char um, // upper middle
|
||||
unsigned char ur, // upper right
|
||||
unsigned char ml, // middle left
|
||||
unsigned char mm, // middle (unused)
|
||||
unsigned char mr, // middle right
|
||||
unsigned char ll, // lower left
|
||||
unsigned char lm, // lower middle
|
||||
unsigned char lr, // lower right
|
||||
float fScale) {
|
||||
short Horz = ur + 2 * mr + lr - ul - 2 * ml - ll;
|
||||
short Vert = ul + 2 * um + ur - ll - 2 * lm - lr;
|
||||
short Sum = (short)(fScale * (abs((int)Horz) + abs((int)Vert)));
|
||||
return (unsigned char)((Sum < 0) ? 0 : ((Sum > 255) ? 255 : Sum));
|
||||
__device__ unsigned char ComputeSobel(unsigned char ul, // upper left
|
||||
unsigned char um, // upper middle
|
||||
unsigned char ur, // upper right
|
||||
unsigned char ml, // middle left
|
||||
unsigned char mm, // middle (unused)
|
||||
unsigned char mr, // middle right
|
||||
unsigned char ll, // lower left
|
||||
unsigned char lm, // lower middle
|
||||
unsigned char lr, // lower right
|
||||
float fScale)
|
||||
{
|
||||
short Horz = ur + 2 * mr + lr - ul - 2 * ml - ll;
|
||||
short Vert = ul + 2 * um + ur - ll - 2 * lm - lr;
|
||||
short Sum = (short)(fScale * (abs((int)Horz) + abs((int)Vert)));
|
||||
return (unsigned char)((Sum < 0) ? 0 : ((Sum > 255) ? 255 : Sum));
|
||||
}
|
||||
|
||||
// define a function pointer and initialize to NULL
|
||||
__device__ unsigned char (*varFunction)(unsigned char, unsigned char,
|
||||
unsigned char, unsigned char,
|
||||
unsigned char, unsigned char,
|
||||
unsigned char, unsigned char,
|
||||
unsigned char, float x) = NULL;
|
||||
__device__ unsigned char (*varFunction)(unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
unsigned char,
|
||||
float x) = NULL;
|
||||
|
||||
__device__ unsigned char ComputeBox(unsigned char ul, // upper left
|
||||
unsigned char um, // upper middle
|
||||
unsigned char ur, // upper right
|
||||
unsigned char ml, // middle left
|
||||
unsigned char mm, // middle...middle
|
||||
unsigned char mr, // middle right
|
||||
unsigned char ll, // lower left
|
||||
unsigned char lm, // lower middle
|
||||
unsigned char lr, // lower right
|
||||
float fscale) {
|
||||
short Sum = (short)(ul + um + ur + ml + mm + mr + ll + lm + lr) / 9;
|
||||
Sum *= fscale;
|
||||
return (unsigned char)((Sum < 0) ? 0 : ((Sum > 255) ? 255 : Sum));
|
||||
__device__ unsigned char ComputeBox(unsigned char ul, // upper left
|
||||
unsigned char um, // upper middle
|
||||
unsigned char ur, // upper right
|
||||
unsigned char ml, // middle left
|
||||
unsigned char mm, // middle...middle
|
||||
unsigned char mr, // middle right
|
||||
unsigned char ll, // lower left
|
||||
unsigned char lm, // lower middle
|
||||
unsigned char lr, // lower right
|
||||
float fscale)
|
||||
{
|
||||
short Sum = (short)(ul + um + ur + ml + mm + mr + ll + lm + lr) / 9;
|
||||
Sum *= fscale;
|
||||
return (unsigned char)((Sum < 0) ? 0 : ((Sum > 255) ? 255 : Sum));
|
||||
}
|
||||
__device__ unsigned char Threshold(unsigned char in, float thresh) {
|
||||
if (in > thresh) {
|
||||
return 0xFF;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
__device__ unsigned char Threshold(unsigned char in, float thresh)
|
||||
{
|
||||
if (in > thresh) {
|
||||
return 0xFF;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Declare function tables, one for the point function chosen, one for the
|
||||
@@ -119,8 +133,8 @@ __device__ pointFunction_t pointFunction_table[LAST_POINT_FILTER];
|
||||
// Declare device side function pointers. We retrieve them later with
|
||||
// cudaMemcpyFromSymbol to set our function tables above in some
|
||||
// particular order specified at runtime.
|
||||
__device__ blockFunction_t pComputeSobel = ComputeSobel;
|
||||
__device__ blockFunction_t pComputeBox = ComputeBox;
|
||||
__device__ blockFunction_t pComputeSobel = ComputeSobel;
|
||||
__device__ blockFunction_t pComputeBox = ComputeBox;
|
||||
__device__ pointFunction_t pComputeThreshold = Threshold;
|
||||
|
||||
// Allocate host side tables to mirror the device side, and later, we
|
||||
@@ -138,222 +152,210 @@ pointFunction_t h_pointFunction_table[2];
|
||||
// Following the block operation, a per-pixel operation,
|
||||
// pointed to by pPointFunction is performed before the final
|
||||
// pixel is produced.
|
||||
__global__ void SobelShared(uchar4 *pSobelOriginal, unsigned short SobelPitch,
|
||||
__global__ void SobelShared(uchar4 *pSobelOriginal,
|
||||
unsigned short SobelPitch,
|
||||
#ifndef FIXED_BLOCKWIDTH
|
||||
short BlockWidth, short SharedPitch,
|
||||
short BlockWidth,
|
||||
short SharedPitch,
|
||||
#endif
|
||||
short w, short h, float fScale, int blockOperation,
|
||||
pointFunction_t pPointFunction,
|
||||
cudaTextureObject_t tex) {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
short u = 4 * blockIdx.x * BlockWidth;
|
||||
short v = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
short ib;
|
||||
short w,
|
||||
short h,
|
||||
float fScale,
|
||||
int blockOperation,
|
||||
pointFunction_t pPointFunction,
|
||||
cudaTextureObject_t tex)
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
short u = 4 * blockIdx.x * BlockWidth;
|
||||
short v = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
short ib;
|
||||
|
||||
int SharedIdx = threadIdx.y * SharedPitch;
|
||||
|
||||
for (ib = threadIdx.x; ib < BlockWidth + 2 * RADIUS; ib += blockDim.x) {
|
||||
LocalBlock[SharedIdx + 4 * ib + 0] = tex2D<unsigned char>(
|
||||
tex, (float)(u + 4 * ib - RADIUS + 0), (float)(v - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 1] = tex2D<unsigned char>(
|
||||
tex, (float)(u + 4 * ib - RADIUS + 1), (float)(v - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 2] = tex2D<unsigned char>(
|
||||
tex, (float)(u + 4 * ib - RADIUS + 2), (float)(v - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 3] = tex2D<unsigned char>(
|
||||
tex, (float)(u + 4 * ib - RADIUS + 3), (float)(v - RADIUS));
|
||||
}
|
||||
|
||||
if (threadIdx.y < RADIUS * 2) {
|
||||
//
|
||||
// copy trailing RADIUS*2 rows of pixels into shared
|
||||
//
|
||||
SharedIdx = (blockDim.y + threadIdx.y) * SharedPitch;
|
||||
int SharedIdx = threadIdx.y * SharedPitch;
|
||||
|
||||
for (ib = threadIdx.x; ib < BlockWidth + 2 * RADIUS; ib += blockDim.x) {
|
||||
LocalBlock[SharedIdx + 4 * ib + 0] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 0),
|
||||
(float)(v + blockDim.y - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 1] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 1),
|
||||
(float)(v + blockDim.y - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 2] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 2),
|
||||
(float)(v + blockDim.y - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 3] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 3),
|
||||
(float)(v + blockDim.y - RADIUS));
|
||||
}
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
|
||||
u >>= 2; // index as uchar4 from here
|
||||
uchar4 *pSobel = (uchar4 *)(((char *)pSobelOriginal) + v * SobelPitch);
|
||||
SharedIdx = threadIdx.y * SharedPitch;
|
||||
|
||||
blockFunction = blockFunction_table[blockOperation];
|
||||
|
||||
for (ib = threadIdx.x; ib < BlockWidth; ib += blockDim.x) {
|
||||
uchar4 out;
|
||||
|
||||
unsigned char pix00 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 0];
|
||||
unsigned char pix01 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 1];
|
||||
unsigned char pix02 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 2];
|
||||
unsigned char pix10 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 0];
|
||||
unsigned char pix11 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 1];
|
||||
unsigned char pix12 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 2];
|
||||
unsigned char pix20 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 0];
|
||||
unsigned char pix21 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 1];
|
||||
unsigned char pix22 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 2];
|
||||
|
||||
out.x = (*blockFunction)(pix00, pix01, pix02, pix10, pix11, pix12, pix20,
|
||||
pix21, pix22, fScale);
|
||||
|
||||
pix00 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 3];
|
||||
pix10 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 3];
|
||||
pix20 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 3];
|
||||
out.y = (*blockFunction)(pix01, pix02, pix00, pix11, pix12, pix10, pix21,
|
||||
pix22, pix20, fScale);
|
||||
|
||||
pix01 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 4];
|
||||
pix11 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 4];
|
||||
pix21 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 4];
|
||||
out.z = (*blockFunction)(pix02, pix00, pix01, pix12, pix10, pix11, pix22,
|
||||
pix20, pix21, fScale);
|
||||
|
||||
pix02 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 5];
|
||||
pix12 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 5];
|
||||
pix22 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 5];
|
||||
out.w = (*blockFunction)(pix00, pix01, pix02, pix10, pix11, pix12, pix20,
|
||||
pix21, pix22, fScale);
|
||||
|
||||
if (pPointFunction != NULL) {
|
||||
out.x = (*pPointFunction)(out.x, THRESHOLD);
|
||||
out.y = (*pPointFunction)(out.y, THRESHOLD);
|
||||
out.z = (*pPointFunction)(out.z, THRESHOLD);
|
||||
out.w = (*pPointFunction)(out.w, THRESHOLD);
|
||||
LocalBlock[SharedIdx + 4 * ib + 0] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 0), (float)(v - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 1] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 1), (float)(v - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 2] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 2), (float)(v - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 3] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 3), (float)(v - RADIUS));
|
||||
}
|
||||
|
||||
if (u + ib < w / 4 && v < h) {
|
||||
pSobel[u + ib] = out;
|
||||
}
|
||||
}
|
||||
if (threadIdx.y < RADIUS * 2) {
|
||||
//
|
||||
// copy trailing RADIUS*2 rows of pixels into shared
|
||||
//
|
||||
SharedIdx = (blockDim.y + threadIdx.y) * SharedPitch;
|
||||
|
||||
cg::sync(cta);
|
||||
for (ib = threadIdx.x; ib < BlockWidth + 2 * RADIUS; ib += blockDim.x) {
|
||||
LocalBlock[SharedIdx + 4 * ib + 0] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 0), (float)(v + blockDim.y - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 1] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 1), (float)(v + blockDim.y - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 2] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 2), (float)(v + blockDim.y - RADIUS));
|
||||
LocalBlock[SharedIdx + 4 * ib + 3] =
|
||||
tex2D<unsigned char>(tex, (float)(u + 4 * ib - RADIUS + 3), (float)(v + blockDim.y - RADIUS));
|
||||
}
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
|
||||
u >>= 2; // index as uchar4 from here
|
||||
uchar4 *pSobel = (uchar4 *)(((char *)pSobelOriginal) + v * SobelPitch);
|
||||
SharedIdx = threadIdx.y * SharedPitch;
|
||||
|
||||
blockFunction = blockFunction_table[blockOperation];
|
||||
|
||||
for (ib = threadIdx.x; ib < BlockWidth; ib += blockDim.x) {
|
||||
uchar4 out;
|
||||
|
||||
unsigned char pix00 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 0];
|
||||
unsigned char pix01 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 1];
|
||||
unsigned char pix02 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 2];
|
||||
unsigned char pix10 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 0];
|
||||
unsigned char pix11 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 1];
|
||||
unsigned char pix12 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 2];
|
||||
unsigned char pix20 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 0];
|
||||
unsigned char pix21 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 1];
|
||||
unsigned char pix22 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 2];
|
||||
|
||||
out.x = (*blockFunction)(pix00, pix01, pix02, pix10, pix11, pix12, pix20, pix21, pix22, fScale);
|
||||
|
||||
pix00 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 3];
|
||||
pix10 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 3];
|
||||
pix20 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 3];
|
||||
out.y = (*blockFunction)(pix01, pix02, pix00, pix11, pix12, pix10, pix21, pix22, pix20, fScale);
|
||||
|
||||
pix01 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 4];
|
||||
pix11 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 4];
|
||||
pix21 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 4];
|
||||
out.z = (*blockFunction)(pix02, pix00, pix01, pix12, pix10, pix11, pix22, pix20, pix21, fScale);
|
||||
|
||||
pix02 = LocalBlock[SharedIdx + 4 * ib + 0 * SharedPitch + 5];
|
||||
pix12 = LocalBlock[SharedIdx + 4 * ib + 1 * SharedPitch + 5];
|
||||
pix22 = LocalBlock[SharedIdx + 4 * ib + 2 * SharedPitch + 5];
|
||||
out.w = (*blockFunction)(pix00, pix01, pix02, pix10, pix11, pix12, pix20, pix21, pix22, fScale);
|
||||
|
||||
if (pPointFunction != NULL) {
|
||||
out.x = (*pPointFunction)(out.x, THRESHOLD);
|
||||
out.y = (*pPointFunction)(out.y, THRESHOLD);
|
||||
out.z = (*pPointFunction)(out.z, THRESHOLD);
|
||||
out.w = (*pPointFunction)(out.w, THRESHOLD);
|
||||
}
|
||||
|
||||
if (u + ib < w / 4 && v < h) {
|
||||
pSobel[u + ib] = out;
|
||||
}
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
}
|
||||
|
||||
__global__ void SobelCopyImage(Pixel *pSobelOriginal, unsigned int Pitch, int w,
|
||||
int h, float fscale, cudaTextureObject_t tex) {
|
||||
unsigned char *pSobel =
|
||||
(unsigned char *)(((char *)pSobelOriginal) + blockIdx.x * Pitch);
|
||||
__global__ void
|
||||
SobelCopyImage(Pixel *pSobelOriginal, unsigned int Pitch, int w, int h, float fscale, cudaTextureObject_t tex)
|
||||
{
|
||||
unsigned char *pSobel = (unsigned char *)(((char *)pSobelOriginal) + blockIdx.x * Pitch);
|
||||
|
||||
for (int i = threadIdx.x; i < w; i += blockDim.x) {
|
||||
pSobel[i] = min(
|
||||
max((tex2D<unsigned char>(tex, (float)i, (float)blockIdx.x) * fscale),
|
||||
0.f),
|
||||
255.f);
|
||||
}
|
||||
for (int i = threadIdx.x; i < w; i += blockDim.x) {
|
||||
pSobel[i] = min(max((tex2D<unsigned char>(tex, (float)i, (float)blockIdx.x) * fscale), 0.f), 255.f);
|
||||
}
|
||||
}
|
||||
|
||||
// Perform block and pointer filtering using texture lookups.
|
||||
// The block and point operations are determined by the
|
||||
// input argument (see comment above for "SobelShared" function)
|
||||
__global__ void SobelTex(Pixel *pSobelOriginal, unsigned int Pitch, int w,
|
||||
int h, float fScale, int blockOperation,
|
||||
pointFunction_t pPointOperation,
|
||||
cudaTextureObject_t tex) {
|
||||
unsigned char *pSobel =
|
||||
(unsigned char *)(((char *)pSobelOriginal) + blockIdx.x * Pitch);
|
||||
unsigned char tmp = 0;
|
||||
__global__ void SobelTex(Pixel *pSobelOriginal,
|
||||
unsigned int Pitch,
|
||||
int w,
|
||||
int h,
|
||||
float fScale,
|
||||
int blockOperation,
|
||||
pointFunction_t pPointOperation,
|
||||
cudaTextureObject_t tex)
|
||||
{
|
||||
unsigned char *pSobel = (unsigned char *)(((char *)pSobelOriginal) + blockIdx.x * Pitch);
|
||||
unsigned char tmp = 0;
|
||||
|
||||
for (int i = threadIdx.x; i < w; i += blockDim.x) {
|
||||
unsigned char pix00 =
|
||||
tex2D<unsigned char>(tex, (float)i - 1, (float)blockIdx.x - 1);
|
||||
unsigned char pix01 =
|
||||
tex2D<unsigned char>(tex, (float)i + 0, (float)blockIdx.x - 1);
|
||||
unsigned char pix02 =
|
||||
tex2D<unsigned char>(tex, (float)i + 1, (float)blockIdx.x - 1);
|
||||
unsigned char pix10 =
|
||||
tex2D<unsigned char>(tex, (float)i - 1, (float)blockIdx.x + 0);
|
||||
unsigned char pix11 =
|
||||
tex2D<unsigned char>(tex, (float)i + 0, (float)blockIdx.x + 0);
|
||||
unsigned char pix12 =
|
||||
tex2D<unsigned char>(tex, (float)i + 1, (float)blockIdx.x + 0);
|
||||
unsigned char pix20 =
|
||||
tex2D<unsigned char>(tex, (float)i - 1, (float)blockIdx.x + 1);
|
||||
unsigned char pix21 =
|
||||
tex2D<unsigned char>(tex, (float)i + 0, (float)blockIdx.x + 1);
|
||||
unsigned char pix22 =
|
||||
tex2D<unsigned char>(tex, (float)i + 1, (float)blockIdx.x + 1);
|
||||
tmp = (*(blockFunction_table[blockOperation]))(
|
||||
pix00, pix01, pix02, pix10, pix11, pix12, pix20, pix21, pix22, fScale);
|
||||
for (int i = threadIdx.x; i < w; i += blockDim.x) {
|
||||
unsigned char pix00 = tex2D<unsigned char>(tex, (float)i - 1, (float)blockIdx.x - 1);
|
||||
unsigned char pix01 = tex2D<unsigned char>(tex, (float)i + 0, (float)blockIdx.x - 1);
|
||||
unsigned char pix02 = tex2D<unsigned char>(tex, (float)i + 1, (float)blockIdx.x - 1);
|
||||
unsigned char pix10 = tex2D<unsigned char>(tex, (float)i - 1, (float)blockIdx.x + 0);
|
||||
unsigned char pix11 = tex2D<unsigned char>(tex, (float)i + 0, (float)blockIdx.x + 0);
|
||||
unsigned char pix12 = tex2D<unsigned char>(tex, (float)i + 1, (float)blockIdx.x + 0);
|
||||
unsigned char pix20 = tex2D<unsigned char>(tex, (float)i - 1, (float)blockIdx.x + 1);
|
||||
unsigned char pix21 = tex2D<unsigned char>(tex, (float)i + 0, (float)blockIdx.x + 1);
|
||||
unsigned char pix22 = tex2D<unsigned char>(tex, (float)i + 1, (float)blockIdx.x + 1);
|
||||
tmp = (*(blockFunction_table[blockOperation]))(
|
||||
pix00, pix01, pix02, pix10, pix11, pix12, pix20, pix21, pix22, fScale);
|
||||
|
||||
if (pPointOperation != NULL) {
|
||||
tmp = (*pPointOperation)(tmp, 150.0);
|
||||
if (pPointOperation != NULL) {
|
||||
tmp = (*pPointOperation)(tmp, 150.0);
|
||||
}
|
||||
|
||||
pSobel[i] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void setupTexture(int iw, int ih, Pixel *data, int Bpp)
|
||||
{
|
||||
cudaChannelFormatDesc desc;
|
||||
|
||||
if (Bpp == 1) {
|
||||
desc = cudaCreateChannelDesc<unsigned char>();
|
||||
}
|
||||
else {
|
||||
desc = cudaCreateChannelDesc<uchar4>();
|
||||
}
|
||||
|
||||
pSobel[i] = tmp;
|
||||
}
|
||||
checkCudaErrors(cudaMallocArray(&array, &desc, iw, ih));
|
||||
checkCudaErrors(cudaMemcpy2DToArray(
|
||||
array, 0, 0, data, iw * Bpp * sizeof(Pixel), iw * Bpp * sizeof(Pixel), ih, cudaMemcpyHostToDevice));
|
||||
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = array;
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&tex, &texRes, &texDescr, NULL));
|
||||
}
|
||||
|
||||
extern "C" void setupTexture(int iw, int ih, Pixel *data, int Bpp) {
|
||||
cudaChannelFormatDesc desc;
|
||||
|
||||
if (Bpp == 1) {
|
||||
desc = cudaCreateChannelDesc<unsigned char>();
|
||||
} else {
|
||||
desc = cudaCreateChannelDesc<uchar4>();
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaMallocArray(&array, &desc, iw, ih));
|
||||
checkCudaErrors(cudaMemcpy2DToArray(
|
||||
array, 0, 0, data, iw * Bpp * sizeof(Pixel), iw * Bpp * sizeof(Pixel), ih,
|
||||
cudaMemcpyHostToDevice));
|
||||
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = array;
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&tex, &texRes, &texDescr, NULL));
|
||||
}
|
||||
|
||||
extern "C" void deleteTexture(void) {
|
||||
checkCudaErrors(cudaFreeArray(array));
|
||||
checkCudaErrors(cudaDestroyTextureObject(tex));
|
||||
extern "C" void deleteTexture(void)
|
||||
{
|
||||
checkCudaErrors(cudaFreeArray(array));
|
||||
checkCudaErrors(cudaDestroyTextureObject(tex));
|
||||
}
|
||||
|
||||
// Copy the pointers from the function tables to the host side
|
||||
void setupFunctionTables() {
|
||||
// Dynamically assign the function table.
|
||||
// Copy the function pointers to their appropriate locations according to the
|
||||
// enum
|
||||
checkCudaErrors(cudaMemcpyFromSymbol(&h_blockFunction_table[SOBEL_FILTER],
|
||||
pComputeSobel, sizeof(blockFunction_t)));
|
||||
checkCudaErrors(cudaMemcpyFromSymbol(&h_blockFunction_table[BOX_FILTER],
|
||||
pComputeBox, sizeof(blockFunction_t)));
|
||||
void setupFunctionTables()
|
||||
{
|
||||
// Dynamically assign the function table.
|
||||
// Copy the function pointers to their appropriate locations according to the
|
||||
// enum
|
||||
checkCudaErrors(cudaMemcpyFromSymbol(&h_blockFunction_table[SOBEL_FILTER], pComputeSobel, sizeof(blockFunction_t)));
|
||||
checkCudaErrors(cudaMemcpyFromSymbol(&h_blockFunction_table[BOX_FILTER], pComputeBox, sizeof(blockFunction_t)));
|
||||
|
||||
// do the same for the point function, where the 2nd function is NULL ("no-op"
|
||||
// filter, skipped in kernel code)
|
||||
checkCudaErrors(cudaMemcpyFromSymbol(&h_pointFunction_table[THRESHOLD_FILTER],
|
||||
pComputeThreshold,
|
||||
sizeof(pointFunction_t)));
|
||||
h_pointFunction_table[NULL_FILTER] = NULL;
|
||||
// do the same for the point function, where the 2nd function is NULL ("no-op"
|
||||
// filter, skipped in kernel code)
|
||||
checkCudaErrors(
|
||||
cudaMemcpyFromSymbol(&h_pointFunction_table[THRESHOLD_FILTER], pComputeThreshold, sizeof(pointFunction_t)));
|
||||
h_pointFunction_table[NULL_FILTER] = NULL;
|
||||
|
||||
// now copy the function tables back to the device, so if we wish we can use
|
||||
// an index into the table to choose them
|
||||
// We have now set the order in the function table according to our enum.
|
||||
checkCudaErrors(
|
||||
cudaMemcpyToSymbol(blockFunction_table, h_blockFunction_table,
|
||||
sizeof(blockFunction_t) * LAST_BLOCK_FILTER));
|
||||
checkCudaErrors(
|
||||
cudaMemcpyToSymbol(pointFunction_table, h_pointFunction_table,
|
||||
sizeof(pointFunction_t) * LAST_POINT_FILTER));
|
||||
// now copy the function tables back to the device, so if we wish we can use
|
||||
// an index into the table to choose them
|
||||
// We have now set the order in the function table according to our enum.
|
||||
checkCudaErrors(
|
||||
cudaMemcpyToSymbol(blockFunction_table, h_blockFunction_table, sizeof(blockFunction_t) * LAST_BLOCK_FILTER));
|
||||
checkCudaErrors(
|
||||
cudaMemcpyToSymbol(pointFunction_table, h_pointFunction_table, sizeof(pointFunction_t) * LAST_POINT_FILTER));
|
||||
}
|
||||
|
||||
// Wrapper for the __global__ call that sets up the texture and threads
|
||||
@@ -363,40 +365,50 @@ void setupFunctionTables() {
|
||||
// blockFunction_table on the device side
|
||||
// pPointOp is itself a function pointer passed as a kernel argument, retrieved
|
||||
// from a host side copy of the function table
|
||||
extern "C" void sobelFilter(Pixel *odata, int iw, int ih,
|
||||
enum SobelDisplayMode mode, float fScale,
|
||||
int blockOperation, int pointOperation) {
|
||||
pointFunction_t pPointOp = h_pointFunction_table[pointOperation];
|
||||
extern "C" void sobelFilter(Pixel *odata,
|
||||
int iw,
|
||||
int ih,
|
||||
enum SobelDisplayMode mode,
|
||||
float fScale,
|
||||
int blockOperation,
|
||||
int pointOperation)
|
||||
{
|
||||
pointFunction_t pPointOp = h_pointFunction_table[pointOperation];
|
||||
|
||||
switch (mode) {
|
||||
switch (mode) {
|
||||
case SOBELDISPLAY_IMAGE:
|
||||
SobelCopyImage<<<ih, 384>>>(odata, iw, iw, ih, fScale, tex);
|
||||
break;
|
||||
SobelCopyImage<<<ih, 384>>>(odata, iw, iw, ih, fScale, tex);
|
||||
break;
|
||||
|
||||
case SOBELDISPLAY_SOBELTEX:
|
||||
SobelTex<<<ih, 384>>>(odata, iw, iw, ih, fScale, blockOperation, pPointOp,
|
||||
tex);
|
||||
break;
|
||||
SobelTex<<<ih, 384>>>(odata, iw, iw, ih, fScale, blockOperation, pPointOp, tex);
|
||||
break;
|
||||
|
||||
case SOBELDISPLAY_SOBELSHARED: {
|
||||
dim3 threads(16, 4);
|
||||
dim3 threads(16, 4);
|
||||
#ifndef FIXED_BLOCKWIDTH
|
||||
int BlockWidth = 80; // must be divisible by 16 for coalescing
|
||||
int BlockWidth = 80; // must be divisible by 16 for coalescing
|
||||
#endif
|
||||
dim3 blocks = dim3(iw / (4 * BlockWidth) + (0 != iw % (4 * BlockWidth)),
|
||||
ih / threads.y + (0 != ih % threads.y));
|
||||
int SharedPitch = ~0x3f & (4 * (BlockWidth + 2 * RADIUS) + 0x3f);
|
||||
int sharedMem = SharedPitch * (threads.y + 2 * RADIUS);
|
||||
dim3 blocks =
|
||||
dim3(iw / (4 * BlockWidth) + (0 != iw % (4 * BlockWidth)), ih / threads.y + (0 != ih % threads.y));
|
||||
int SharedPitch = ~0x3f & (4 * (BlockWidth + 2 * RADIUS) + 0x3f);
|
||||
int sharedMem = SharedPitch * (threads.y + 2 * RADIUS);
|
||||
|
||||
// for the shared kernel, width must be divisible by 4
|
||||
iw &= ~3;
|
||||
// for the shared kernel, width must be divisible by 4
|
||||
iw &= ~3;
|
||||
|
||||
SobelShared<<<blocks, threads, sharedMem>>>(
|
||||
(uchar4 *)odata, iw,
|
||||
SobelShared<<<blocks, threads, sharedMem>>>((uchar4 *)odata,
|
||||
iw,
|
||||
#ifndef FIXED_BLOCKWIDTH
|
||||
BlockWidth, SharedPitch,
|
||||
BlockWidth,
|
||||
SharedPitch,
|
||||
#endif
|
||||
iw, ih, fScale, blockOperation, pPointOp, tex);
|
||||
iw,
|
||||
ih,
|
||||
fScale,
|
||||
blockOperation,
|
||||
pPointOp,
|
||||
tex);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +31,7 @@
|
||||
typedef unsigned char Pixel;
|
||||
|
||||
// global determines which filter to invoke
|
||||
enum SobelDisplayMode {
|
||||
SOBELDISPLAY_IMAGE = 0,
|
||||
SOBELDISPLAY_SOBELTEX,
|
||||
SOBELDISPLAY_SOBELSHARED
|
||||
};
|
||||
enum SobelDisplayMode { SOBELDISPLAY_IMAGE = 0, SOBELDISPLAY_SOBELTEX, SOBELDISPLAY_SOBELSHARED };
|
||||
|
||||
// Enums to set up the function table
|
||||
// note: if you change these be sure to recompile those files
|
||||
@@ -47,12 +43,16 @@ enum BLOCK_ENUM { THRESHOLD_FILTER = 0, NULL_FILTER, LAST_BLOCK_FILTER };
|
||||
|
||||
extern enum SobelDisplayMode g_SobelDisplayMode;
|
||||
|
||||
extern "C" void sobelFilter(Pixel *odata, int iw, int ih,
|
||||
enum SobelDisplayMode mode, float fScale,
|
||||
int blockOperation, int pointOperation);
|
||||
extern "C" void sobelFilter(Pixel *odata,
|
||||
int iw,
|
||||
int ih,
|
||||
enum SobelDisplayMode mode,
|
||||
float fScale,
|
||||
int blockOperation,
|
||||
int pointOperation);
|
||||
extern "C" void setupTexture(int iw, int ih, Pixel *data, int Bpp);
|
||||
extern "C" void deleteTexture(void);
|
||||
extern "C" void initFilter(void);
|
||||
void setupFunctionTables();
|
||||
void setupFunctionTables();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user