mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-13 00:08:26 +08:00
CUDA 13.2 samples update (#432)
- Added Python samples for CUDA Python 1.0 release - Renamed top-level `Samples` directory to `cpp` to accommodate Python samples.
This commit is contained in:
18
cpp/2_Concepts_and_Techniques/FunctionPointers/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/2_Concepts_and_Techniques/FunctionPointers/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"${workspaceFolder}/../../../Common"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||
"cStandard": "gnu17",
|
||||
"cppStandard": "gnu++14",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"configurationProvider": "ms-vscode.makefile-tools"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
7
cpp/2_Concepts_and_Techniques/FunctionPointers/.vscode/extensions.json
vendored
Normal file
7
cpp/2_Concepts_and_Techniques/FunctionPointers/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(FunctionPointers LANGUAGES C CXX CUDA)
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 89 90 100 110 120)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||
if(ENABLE_CUDA_DEBUG)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (may significantly affect performance on some targets)
|
||||
else()
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo") # add line information to all builds for debug tools (exclusive to -G option)
|
||||
endif()
|
||||
|
||||
# Include directories and libraries
|
||||
include_directories(../../../Common)
|
||||
|
||||
if(WIN32)
|
||||
set(PC_GLUT_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../../Common")
|
||||
set(PC_GLUT_LIBRARY_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../../Common/lib/x64")
|
||||
# The GLEW library built on Windows on Arm system is named as glew32.lib/glew32.dll by default.
|
||||
if(EXISTS "${PC_GLUT_LIBRARY_DIRS}/glew32.lib")
|
||||
set(GLEW_LIB_NAME "glew32")
|
||||
else()
|
||||
set(GLEW_LIB_NAME "glew64")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
find_package(OpenGL)
|
||||
find_package(GLUT)
|
||||
|
||||
# Source file
|
||||
if(${OpenGL_FOUND})
|
||||
if (${GLUT_FOUND})
|
||||
# Add target for FunctionPointers
|
||||
add_executable(FunctionPointers FunctionPointers.cpp FunctionPointers_kernels.cu)
|
||||
|
||||
target_compile_options(FunctionPointers PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(FunctionPointers PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(FunctionPointers PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_include_directories(FunctionPointers PUBLIC
|
||||
${OPENGL_INCLUDE_DIR}
|
||||
${CUDAToolkit_INCLUDE_DIRS}
|
||||
${GLUT_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(FunctionPointers
|
||||
${OPENGL_LIBRARIES}
|
||||
${GLUT_LIBRARIES}
|
||||
)
|
||||
|
||||
# Copy data files to output directory
|
||||
add_custom_command(TARGET FunctionPointers POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/data
|
||||
${CMAKE_CURRENT_BINARY_DIR}/data
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(FunctionPointers
|
||||
${PC_GLUT_LIBRARY_DIRS}/freeglut.lib
|
||||
${PC_GLUT_LIBRARY_DIRS}/${GLEW_LIB_NAME}.lib
|
||||
)
|
||||
|
||||
add_custom_command(TARGET FunctionPointers
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../../bin/win64/$<CONFIGURATION>/freeglut.dll
|
||||
${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>
|
||||
)
|
||||
|
||||
add_custom_command(TARGET FunctionPointers
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../../bin/win64/$<CONFIGURATION>/${GLEW_LIB_NAME}.dll
|
||||
${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>
|
||||
)
|
||||
endif()
|
||||
|
||||
else()
|
||||
message(STATUS "GLUT not found - will not build sample 'FunctionPointers'")
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "OpenGL not found - will not build sample 'FunctionPointers'")
|
||||
endif()
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
@@ -0,0 +1,506 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// OpenGL Graphics includes
|
||||
#include <helper_gl.h>
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
#include <GLUT/glut.h>
|
||||
#ifndef glutCloseFunc
|
||||
#define glutCloseFunc glutWMCloseFunc
|
||||
#endif
|
||||
#else
|
||||
#include <GL/freeglut.h>
|
||||
#endif
|
||||
|
||||
// Includes
|
||||
#include <cuda_gl_interop.h> // CUDA OpenGL interop
|
||||
#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"
|
||||
|
||||
#define EXIT_WAIVED 2
|
||||
|
||||
//
|
||||
// Cuda example code that implements the Sobel edge detection
|
||||
// filter. This code works for 8-bit monochrome images.
|
||||
//
|
||||
// Use the '-' and '=' keys to change the scale factor.
|
||||
//
|
||||
// Other keys:
|
||||
// I: display image
|
||||
// T: display Sobel Edge Detection (computed solely with texture)
|
||||
// S: display Sobel Edge Detection (computed with texture and shared memory)
|
||||
|
||||
void cleanup(void);
|
||||
void initializeData(char *file);
|
||||
|
||||
#define MAX_EPSILON_ERROR 5.0f
|
||||
|
||||
static const char *sSDKsample = "CUDA Function Pointers (SobelFilter)";
|
||||
|
||||
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 imHeight = 0; // Image height
|
||||
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;
|
||||
|
||||
int g_TotalErrors = 0;
|
||||
|
||||
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 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
|
||||
|
||||
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);
|
||||
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
|
||||
fpsLimit = (int)MAX(ifps, 1.f);
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
}
|
||||
|
||||
// This is the normal display path
|
||||
void display(void)
|
||||
{
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
// 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);
|
||||
|
||||
sobelFilter(data, imWidth, imHeight, g_SobelDisplayMode, imageScale, blockOp, pointOp);
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
glutSwapBuffers();
|
||||
|
||||
sdkStopTimer(&timer);
|
||||
computeFPS();
|
||||
}
|
||||
|
||||
void timerEvent(int value)
|
||||
{
|
||||
if (glutGetWindow()) {
|
||||
glutPostRedisplay();
|
||||
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int /*x*/, int /*y*/)
|
||||
{
|
||||
char temp[256];
|
||||
|
||||
switch (key) {
|
||||
case 27:
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
exit(EXIT_SUCCESS);
|
||||
#else
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
return;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case '-':
|
||||
imageScale -= 0.1f;
|
||||
printf("brightness = %4.2f\n", imageScale);
|
||||
break;
|
||||
|
||||
case '=':
|
||||
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;
|
||||
|
||||
case 's':
|
||||
case 'S':
|
||||
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;
|
||||
|
||||
case 'b':
|
||||
case 'B':
|
||||
blockOp = (blockOp + 1) % LAST_BLOCK_FILTER;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
case 'P':
|
||||
pointOp = (pointOp + 1) % LAST_POINT_FILTER;
|
||||
break;
|
||||
|
||||
default:
|
||||
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 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);
|
||||
}
|
||||
|
||||
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 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 (image_path == NULL) {
|
||||
printf("Failed to read image file: <%s>\n", image_filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
initializeData(image_path);
|
||||
free(image_path);
|
||||
}
|
||||
|
||||
void initGL(int *argc, char **argv)
|
||||
{
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
|
||||
glutInitWindowSize(wWidth, wHeight);
|
||||
glutCreateWindow("Function Pointers [CUDA Edge Detection]n");
|
||||
|
||||
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");
|
||||
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
}
|
||||
|
||||
void runAutoTest(int argc, char *argv[])
|
||||
{
|
||||
printf("[%s] (automated testing w/ readback)\n", sSDKsample);
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
loadDefaultImage(argv[0]);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,414 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cooperative_groups.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
|
||||
#include <helper_cuda.h>
|
||||
|
||||
#include "FunctionPointers_kernels.h"
|
||||
|
||||
// Texture object for reading image
|
||||
cudaTextureObject_t tex;
|
||||
extern __shared__ unsigned char LocalBlock[];
|
||||
static cudaArray *array = NULL;
|
||||
|
||||
#define RADIUS 1
|
||||
|
||||
// pixel value used for thresholding function,
|
||||
// works well with sample image 'teapot512'
|
||||
#define THRESHOLD 150.0f
|
||||
|
||||
#ifdef FIXED_BLOCKWIDTH
|
||||
#define BlockWidth 80
|
||||
#define SharedPitch 384
|
||||
#endif
|
||||
|
||||
// A function pointer can be declared explicitly like this line:
|
||||
//__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 (*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));
|
||||
}
|
||||
|
||||
// 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 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Declare function tables, one for the point function chosen, one for the
|
||||
// block function chosen. The number of entries is determined by the
|
||||
// enum in FunctionPointers_kernels.h
|
||||
__device__ blockFunction_t blockFunction_table[LAST_BLOCK_FILTER];
|
||||
__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__ pointFunction_t pComputeThreshold = Threshold;
|
||||
|
||||
// Allocate host side tables to mirror the device side, and later, we
|
||||
// fill these tables with the function pointers. This lets us send
|
||||
// the pointers to the kernel on invocation, as a method of choosing
|
||||
// which function to run.
|
||||
blockFunction_t h_blockFunction_table[2];
|
||||
pointFunction_t h_pointFunction_table[2];
|
||||
|
||||
// Perform a filter operation on the data, using shared memory
|
||||
// The actual operation performed is
|
||||
// determined by the function pointer "blockFunction" and selected
|
||||
// by the integer argument "blockOperation" and has access
|
||||
// to an apron around the current pixel being processed.
|
||||
// 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,
|
||||
#ifndef FIXED_BLOCKWIDTH
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
// 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)));
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
// Wrapper for the __global__ call that sets up the texture and threads
|
||||
// Below two methods for selecting the image processing function to run are
|
||||
// shown.
|
||||
// BlockOperation is an integer kernel argument used as an index into the
|
||||
// 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];
|
||||
|
||||
switch (mode) {
|
||||
case SOBELDISPLAY_IMAGE:
|
||||
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;
|
||||
|
||||
case SOBELDISPLAY_SOBELSHARED: {
|
||||
dim3 threads(16, 4);
|
||||
#ifndef FIXED_BLOCKWIDTH
|
||||
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);
|
||||
|
||||
// for the shared kernel, width must be divisible by 4
|
||||
iw &= ~3;
|
||||
|
||||
SobelShared<<<blocks, threads, sharedMem>>>((uchar4 *)odata,
|
||||
iw,
|
||||
#ifndef FIXED_BLOCKWIDTH
|
||||
BlockWidth,
|
||||
SharedPitch,
|
||||
#endif
|
||||
iw,
|
||||
ih,
|
||||
fScale,
|
||||
blockOperation,
|
||||
pPointOp,
|
||||
tex);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __SOBELFILTER_KERNELS_H_
|
||||
#define __SOBELFILTER_KERNELS_H_
|
||||
|
||||
typedef unsigned char Pixel;
|
||||
|
||||
// global determines which filter to invoke
|
||||
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
|
||||
// that include this header or ensure the .h is in the
|
||||
// dependencies for the related object files
|
||||
enum POINT_ENUM { SOBEL_FILTER = 0, BOX_FILTER, LAST_POINT_FILTER };
|
||||
|
||||
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 setupTexture(int iw, int ih, Pixel *data, int Bpp);
|
||||
extern "C" void deleteTexture(void);
|
||||
extern "C" void initFilter(void);
|
||||
void setupFunctionTables();
|
||||
|
||||
#endif
|
||||
36
cpp/2_Concepts_and_Techniques/FunctionPointers/README.md
Normal file
36
cpp/2_Concepts_and_Techniques/FunctionPointers/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# FunctionPointers - Function Pointers
|
||||
|
||||
## Description
|
||||
|
||||
This sample illustrates how to use function pointers and implements the Sobel Edge Detection filter for 8-bit monochrome images.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Graphics Interop, Image Processing
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||
|
||||
## Supported OSes
|
||||
|
||||
Linux, Windows
|
||||
|
||||
## Supported CPU Architecture
|
||||
|
||||
x86_64, armv7l
|
||||
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaGraphicsUnmapResources, cudaMemcpy, cudaMallocArray, cudaFreeArray, cudaFree, cudaMemcpyFromSymbol, cudaGraphicsResourceGetMappedPointer, cudaGraphicsMapResources, cudaDestroyTextureObject, cudaDeviceSynchronize, cudaCreateTextureObject, cudaMemcpyToSymbol, cudaGraphicsUnregisterResource, cudaMalloc, cudaGraphicsGLRegisterBuffer
|
||||
|
||||
## Dependencies needed to build/run
|
||||
[X11](../../../README.md#x11), [OpenGL](../../../README.md#opengl), [Freeglut](../../../README.md#freeglut), [GLEW](../../../README.md#glew)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||
|
||||
## References (for more details)
|
||||
BIN
cpp/2_Concepts_and_Techniques/FunctionPointers/data/ref_orig.pgm
Normal file
BIN
cpp/2_Concepts_and_Techniques/FunctionPointers/data/ref_orig.pgm
Normal file
Binary file not shown.
Binary file not shown.
BIN
cpp/2_Concepts_and_Techniques/FunctionPointers/data/ref_tex.pgm
Normal file
BIN
cpp/2_Concepts_and_Techniques/FunctionPointers/data/ref_tex.pgm
Normal file
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user