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/5_Domain_Specific/bicubicTexture/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/5_Domain_Specific/bicubicTexture/.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/5_Domain_Specific/bicubicTexture/.vscode/extensions.json
vendored
Normal file
7
cpp/5_Domain_Specific/bicubicTexture/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
96
cpp/5_Domain_Specific/bicubicTexture/CMakeLists.txt
Normal file
96
cpp/5_Domain_Specific/bicubicTexture/CMakeLists.txt
Normal file
@@ -0,0 +1,96 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(bicubicTexture 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 bicubicTexture
|
||||
add_executable(bicubicTexture bicubicTexture.cpp bicubicTexture_cuda.cu)
|
||||
|
||||
target_compile_options(bicubicTexture PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(bicubicTexture PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(bicubicTexture PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_include_directories(bicubicTexture PUBLIC
|
||||
${OPENGL_INCLUDE_DIR}
|
||||
${CUDAToolkit_INCLUDE_DIRS}
|
||||
${GLUT_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(bicubicTexture
|
||||
${OPENGL_LIBRARIES}
|
||||
${GLUT_LIBRARIES}
|
||||
)
|
||||
|
||||
# Copy data files to the output directory
|
||||
add_custom_command(TARGET bicubicTexture POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/data
|
||||
${CMAKE_CURRENT_BINARY_DIR}/data
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(bicubicTexture
|
||||
${PC_GLUT_LIBRARY_DIRS}/freeglut.lib
|
||||
${PC_GLUT_LIBRARY_DIRS}/${GLEW_LIB_NAME}.lib
|
||||
)
|
||||
|
||||
add_custom_command(TARGET bicubicTexture
|
||||
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 bicubicTexture
|
||||
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 'bicubicTexture'")
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "OpenGL not found - will not build sample 'bicubicTexture'")
|
||||
endif()
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
36
cpp/5_Domain_Specific/bicubicTexture/README.md
Normal file
36
cpp/5_Domain_Specific/bicubicTexture/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# bicubicTexture - Bicubic B-spline Interoplation
|
||||
|
||||
## Description
|
||||
|
||||
This sample demonstrates how to efficiently implement a Bicubic B-spline interpolation filter with CUDA texture.
|
||||
|
||||
## 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, cudaCreateChannelDesc, cudaMallocArray, cudaFreeArray, cudaFree, cudaMemcpy, cudaGraphicsResourceGetMappedPointer, cudaGraphicsMapResources, cudaDestroyTextureObject, cudaDeviceSynchronize, cudaCreateTextureObject, cudaGraphicsUnregisterResource, cudaMalloc, cudaGraphicsGLRegisterBuffer, cudaGetDeviceProperties
|
||||
|
||||
## 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)
|
||||
764
cpp/5_Domain_Specific/bicubicTexture/bicubicTexture.cpp
Normal file
764
cpp/5_Domain_Specific/bicubicTexture/bicubicTexture.cpp
Normal file
@@ -0,0 +1,764 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
Bicubic texture filtering sample
|
||||
sgreen 6/2008
|
||||
|
||||
This sample demonstrates how to efficiently implement bicubic texture
|
||||
filtering in CUDA.
|
||||
|
||||
Bicubic filtering is a higher order interpolation method that produces
|
||||
smoother results than bilinear interpolation:
|
||||
http://en.wikipedia.org/wiki/Bicubic
|
||||
|
||||
It requires reading a 4 x 4 pixel neighbourhood rather than the
|
||||
2 x 2 area required by bilinear filtering.
|
||||
|
||||
Current graphics hardware doesn't support bicubic filtering natively,
|
||||
but it is possible to compose a bicubic filter using just 4 bilinear
|
||||
lookups by offsetting the sample position within each texel and weighting
|
||||
the samples correctly. The only disadvantage to this method is that the
|
||||
hardware only maintains 9-bits of filtering precision within each texel.
|
||||
|
||||
See "Fast Third-Order Texture Filtering", Sigg & Hadwiger, GPU Gems 2:
|
||||
https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-20-fast-third-order-texture-filtering
|
||||
|
||||
v1.1 - updated to include the brute force method using 16 texture lookups.
|
||||
v1.2 - added Catmull-Rom interpolation
|
||||
|
||||
Example performance results from GeForce 8800 GTS:
|
||||
Bilinear - 5500 MPixels/sec
|
||||
Bicubic - 1400 MPixels/sec
|
||||
Fast Bicubic - 2100 MPixels/sec
|
||||
*/
|
||||
|
||||
// OpenGL Graphics includes
|
||||
#include <helper_gl.h>
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
#include <GLUT/glut.h>
|
||||
#else
|
||||
#include <GL/freeglut.h>
|
||||
#endif
|
||||
|
||||
// Includes
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// CUDA system and GL includes
|
||||
#include <cuda_gl_interop.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
// Helper functions
|
||||
#include <helper_cuda.h> // CUDA device initialization helper functions
|
||||
#include <helper_functions.h> // CUDA SDK Helper functions
|
||||
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned char uchar;
|
||||
|
||||
#define USE_BUFFER_TEX 0
|
||||
#ifndef MAX
|
||||
#define MAX(a, b) ((a < b) ? b : a)
|
||||
#endif
|
||||
|
||||
// Auto-Verification Code
|
||||
const int frameCheckNumber = 4;
|
||||
int fpsCount = 0; // FPS count for averaging
|
||||
int fpsLimit = 4; // FPS limit for sampling
|
||||
int g_Index = 0;
|
||||
unsigned int frameCount = 0;
|
||||
unsigned int g_TotalErrors = 0;
|
||||
StopWatchInterface *timer = 0;
|
||||
bool g_Verify = false;
|
||||
|
||||
int *pArgc = NULL;
|
||||
char **pArgv = NULL;
|
||||
|
||||
#define MAX_EPSILON_ERROR 5.0f
|
||||
#define REFRESH_DELAY 10 // ms
|
||||
|
||||
static const char *sSDKsample = "CUDA BicubicTexture";
|
||||
|
||||
// Define the files that are to be save and the reference images for validation
|
||||
const char *sFilterMode[] = {"Nearest", "Bilinear", "Bicubic", "Fast Bicubic", "Catmull-Rom", NULL};
|
||||
|
||||
const char *sOriginal[] =
|
||||
{"0_nearest.ppm", "1_bilinear.ppm", "2_bicubic.ppm", "3_fastbicubic.ppm", "4_catmull-rom.ppm", NULL};
|
||||
|
||||
const char *sReference[] =
|
||||
{"0_nearest.ppm", "1_bilinear.ppm", "2_bicubic.ppm", "3_fastbicubic.ppm", "4_catmull-rom.ppm", NULL};
|
||||
|
||||
const char *srcImageFilename = "teapot512.pgm";
|
||||
char *dumpFilename = NULL;
|
||||
|
||||
uint width = 512, height = 512;
|
||||
uint imageWidth, imageHeight;
|
||||
dim3 blockSize(16, 16);
|
||||
dim3 gridSize(width / blockSize.x, height / blockSize.y);
|
||||
|
||||
enum eFilterMode { MODE_NEAREST, MODE_BILINEAR, MODE_BICUBIC, MODE_FAST_BICUBIC, MODE_CATMULL_ROM, NUM_MODES };
|
||||
|
||||
eFilterMode g_FilterMode = MODE_FAST_BICUBIC;
|
||||
|
||||
bool drawCurves = false;
|
||||
|
||||
GLuint pbo = 0; // OpenGL pixel buffer object
|
||||
struct cudaGraphicsResource *cuda_pbo_resource; // handles OpenGL-CUDA exchange
|
||||
GLuint displayTex = 0;
|
||||
GLuint bufferTex = 0;
|
||||
GLuint fprog; // fragment program (shader)
|
||||
|
||||
float tx = -27.75f, ty = -189.0f; // image translation
|
||||
float scale = 0.125f; // image scale
|
||||
float cx, cy; // image centre
|
||||
|
||||
void display();
|
||||
void initGLBuffers();
|
||||
void runBenchmark(int iterations);
|
||||
void cleanup();
|
||||
|
||||
#define GL_TEXTURE_TYPE GL_TEXTURE_RECTANGLE_ARB
|
||||
// #define GL_TEXTURE_TYPE GL_TEXTURE_2D
|
||||
|
||||
extern "C" void initGL(int *argc, char **argv);
|
||||
extern "C" void loadImageData(int argc, char **argv);
|
||||
|
||||
extern "C" void initTexture(int imageWidth, int imageHeight, uchar *h_data);
|
||||
extern "C" void freeTexture();
|
||||
extern "C" void render(int width,
|
||||
int height,
|
||||
float tx,
|
||||
float ty,
|
||||
float scale,
|
||||
float cx,
|
||||
float cy,
|
||||
dim3 blockSize,
|
||||
dim3 gridSize,
|
||||
eFilterMode filter_mode,
|
||||
uchar4 *output);
|
||||
|
||||
// w0, w1, w2, and w3 are the four cubic B-spline basis functions
|
||||
float bspline_w0(float a) { return (1.0f / 6.0f) * (-a * a * a + 3.0f * a * a - 3.0f * a + 1.0f); }
|
||||
|
||||
float bspline_w1(float a) { return (1.0f / 6.0f) * (3.0f * a * a * a - 6.0f * a * a + 4.0f); }
|
||||
|
||||
float bspline_w2(float a) { return (1.0f / 6.0f) * (-3.0f * a * a * a + 3.0f * a * a + 3.0f * a + 1.0f); }
|
||||
|
||||
__host__ __device__ float bspline_w3(float a) { return (1.0f / 6.0f) * (a * a * a); }
|
||||
|
||||
void computeFPS()
|
||||
{
|
||||
frameCount++;
|
||||
fpsCount++;
|
||||
|
||||
if (fpsCount == fpsLimit - 1) {
|
||||
g_Verify = true;
|
||||
}
|
||||
|
||||
if (fpsCount == fpsLimit) {
|
||||
char fps[256];
|
||||
float ifps = 1.f / (sdkGetAverageTimerValue(&timer) / 1000.f);
|
||||
sprintf(fps, "%s %s <%s>: %3.1f fps", "", sSDKsample, sFilterMode[g_FilterMode], ifps);
|
||||
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
}
|
||||
|
||||
void plotCurve(float (*func)(float))
|
||||
{
|
||||
const int steps = 100;
|
||||
glBegin(GL_LINE_STRIP);
|
||||
|
||||
for (int i = 0; i < steps; i++) {
|
||||
float x = i / (float)(steps - 1);
|
||||
glVertex2f(x, func(x));
|
||||
}
|
||||
|
||||
glEnd();
|
||||
}
|
||||
|
||||
// display results using OpenGL (called by GLUT)
|
||||
void display()
|
||||
{
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
// map PBO to get CUDA device pointer
|
||||
uchar4 *d_output;
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void **)&d_output, &num_bytes, cuda_pbo_resource));
|
||||
render(imageWidth, imageHeight, tx, ty, scale, cx, cy, blockSize, gridSize, g_FilterMode, d_output);
|
||||
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
|
||||
|
||||
// Common display path
|
||||
{
|
||||
// display results
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
#if USE_BUFFER_TEX
|
||||
// display using buffer texture
|
||||
glBindTexture(GL_TEXTURE_BUFFER_EXT, bufferTex);
|
||||
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fprog);
|
||||
glEnable(GL_FRAGMENT_PROGRAM_ARB);
|
||||
glProgramLocalParameterI4iNV(GL_FRAGMENT_PROGRAM_ARB, 0, width, 0, 0, 0);
|
||||
#else
|
||||
// download image from PBO to OpenGL texture
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
|
||||
glBindTexture(GL_TEXTURE_TYPE, displayTex);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexSubImage2D(GL_TEXTURE_TYPE, 0, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
|
||||
glEnable(GL_TEXTURE_TYPE);
|
||||
#endif
|
||||
|
||||
// draw textured quad
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0.0f, (GLfloat)height);
|
||||
glVertex2f(0.0f, 0.0f);
|
||||
glTexCoord2f((GLfloat)width, (GLfloat)height);
|
||||
glVertex2f(1.0f, 0.0f);
|
||||
glTexCoord2f((GLfloat)width, 0.0f);
|
||||
glVertex2f(1.0f, 1.0f);
|
||||
glTexCoord2f(0.0f, 0.0f);
|
||||
glVertex2f(0.0f, 1.0f);
|
||||
glEnd();
|
||||
glDisable(GL_TEXTURE_TYPE);
|
||||
glDisable(GL_FRAGMENT_PROGRAM_ARB);
|
||||
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
||||
|
||||
if (drawCurves) {
|
||||
// draw spline curves
|
||||
glPushMatrix();
|
||||
glScalef(0.25, 0.25, 1.0);
|
||||
|
||||
glTranslatef(0.0, 2.0, 0.0);
|
||||
glColor3f(1.0, 0.0, 0.0);
|
||||
plotCurve(bspline_w3);
|
||||
|
||||
glTranslatef(1.0, 0.0, 0.0);
|
||||
glColor3f(0.0, 1.0, 0.0);
|
||||
plotCurve(bspline_w2);
|
||||
|
||||
glTranslatef(1.0, 0.0, 0.0);
|
||||
glColor3f(0.0, 0.0, 1.0);
|
||||
plotCurve(bspline_w1);
|
||||
|
||||
glTranslatef(1.0, 0.0, 0.0);
|
||||
glColor3f(1.0, 0.0, 1.0);
|
||||
plotCurve(bspline_w0);
|
||||
|
||||
glPopMatrix();
|
||||
glColor3f(1.0, 1.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
glutSwapBuffers();
|
||||
glutReportErrors();
|
||||
|
||||
sdkStopTimer(&timer);
|
||||
|
||||
computeFPS();
|
||||
}
|
||||
|
||||
// GLUT callback functions
|
||||
void timerEvent(int value)
|
||||
{
|
||||
if (glutGetWindow()) {
|
||||
glutPostRedisplay();
|
||||
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int /*x*/, int /*y*/)
|
||||
{
|
||||
switch (key) {
|
||||
case 27:
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
exit(EXIT_SUCCESS);
|
||||
#else
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
return;
|
||||
#endif
|
||||
|
||||
case '1':
|
||||
g_FilterMode = MODE_NEAREST;
|
||||
break;
|
||||
|
||||
case '2':
|
||||
g_FilterMode = MODE_BILINEAR;
|
||||
break;
|
||||
|
||||
case '3':
|
||||
g_FilterMode = MODE_BICUBIC;
|
||||
break;
|
||||
|
||||
case '4':
|
||||
g_FilterMode = MODE_FAST_BICUBIC;
|
||||
break;
|
||||
|
||||
case '5':
|
||||
g_FilterMode = MODE_CATMULL_ROM;
|
||||
break;
|
||||
|
||||
case '=':
|
||||
case '+':
|
||||
scale *= 0.5f;
|
||||
break;
|
||||
|
||||
case '-':
|
||||
scale *= 2.0f;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
scale = 1.0f;
|
||||
tx = ty = 0.0f;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
printf("%f, %f, %f\n", tx, ty, scale);
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
runBenchmark(500);
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
drawCurves ^= 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (key >= '1' && key <= '5') {
|
||||
printf("> FilterMode[%d] = %s\n", g_FilterMode + 1, sFilterMode[g_FilterMode]);
|
||||
}
|
||||
}
|
||||
|
||||
int ox, oy;
|
||||
int buttonState = 0;
|
||||
|
||||
void mouse(int button, int state, int x, int y)
|
||||
{
|
||||
if (state == GLUT_DOWN) {
|
||||
buttonState |= 1 << button;
|
||||
}
|
||||
else if (state == GLUT_UP) {
|
||||
buttonState = 0;
|
||||
}
|
||||
|
||||
ox = x;
|
||||
oy = y;
|
||||
}
|
||||
|
||||
void motion(int x, int y)
|
||||
{
|
||||
float dx, dy;
|
||||
dx = (float)(x - ox);
|
||||
dy = (float)(y - oy);
|
||||
|
||||
if (buttonState & 1) {
|
||||
// left = translate
|
||||
tx -= dx * scale;
|
||||
ty -= dy * scale;
|
||||
}
|
||||
else if (buttonState & 2) {
|
||||
// middle = zoom
|
||||
scale -= dy / 1000.0f;
|
||||
}
|
||||
|
||||
ox = x;
|
||||
oy = y;
|
||||
}
|
||||
|
||||
void reshape(int x, int y)
|
||||
{
|
||||
width = x;
|
||||
height = y;
|
||||
imageWidth = width;
|
||||
imageHeight = height;
|
||||
|
||||
initGLBuffers();
|
||||
|
||||
glViewport(0, 0, x, y);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
void cleanup()
|
||||
{
|
||||
freeTexture();
|
||||
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_pbo_resource));
|
||||
|
||||
glDeleteBuffers(1, &pbo);
|
||||
|
||||
#if USE_BUFFER_TEX
|
||||
glDeleteTextures(1, &bufferTex);
|
||||
glDeleteProgramsARB(1, &fprog);
|
||||
#else
|
||||
glDeleteTextures(1, &displayTex);
|
||||
#endif
|
||||
|
||||
sdkDeleteTimer(&timer);
|
||||
}
|
||||
|
||||
int iDivUp(int a, int b) { return (a % b != 0) ? (a / b + 1) : (a / b); }
|
||||
|
||||
void initGLBuffers()
|
||||
{
|
||||
if (pbo) {
|
||||
// delete old buffer
|
||||
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_pbo_resource));
|
||||
glDeleteBuffers(1, &pbo);
|
||||
}
|
||||
|
||||
// create pixel buffer object for display
|
||||
glGenBuffers(1, &pbo);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
|
||||
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * sizeof(uchar4), 0, GL_STREAM_DRAW_ARB);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
||||
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(&cuda_pbo_resource, pbo, cudaGraphicsMapFlagsWriteDiscard));
|
||||
|
||||
#if USE_BUFFER_TEX
|
||||
|
||||
// create buffer texture, attach to pbo
|
||||
if (bufferTex) {
|
||||
glDeleteTextures(1, &bufferTex);
|
||||
}
|
||||
|
||||
glGenTextures(1, &bufferTex);
|
||||
glBindTexture(GL_TEXTURE_BUFFER_EXT, bufferTex);
|
||||
glTexBufferEXT(GL_TEXTURE_BUFFER_EXT, GL_RGBA8, pbo);
|
||||
glBindTexture(GL_TEXTURE_BUFFER_EXT, 0);
|
||||
#else
|
||||
|
||||
// create texture for display
|
||||
if (displayTex) {
|
||||
glDeleteTextures(1, &displayTex);
|
||||
}
|
||||
|
||||
glGenTextures(1, &displayTex);
|
||||
glBindTexture(GL_TEXTURE_TYPE, displayTex);
|
||||
glTexImage2D(GL_TEXTURE_TYPE, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
glTexParameteri(GL_TEXTURE_TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glBindTexture(GL_TEXTURE_TYPE, 0);
|
||||
#endif
|
||||
|
||||
// calculate new grid size
|
||||
gridSize = dim3(iDivUp(width, blockSize.x), iDivUp(height, blockSize.y));
|
||||
}
|
||||
|
||||
void mainMenu(int i) { keyboard(i, 0, 0); }
|
||||
|
||||
void initMenus()
|
||||
{
|
||||
glutCreateMenu(mainMenu);
|
||||
glutAddMenuEntry("Nearest [1]", '1');
|
||||
glutAddMenuEntry("Bilinear [2]", '2');
|
||||
glutAddMenuEntry("Bicubic [3]", '3');
|
||||
glutAddMenuEntry("Fast Bicubic [4]", '4');
|
||||
glutAddMenuEntry("Catmull-Rom [5]", '5');
|
||||
glutAddMenuEntry("Zoom in [=]", '=');
|
||||
glutAddMenuEntry("Zoom out [-]", '-');
|
||||
glutAddMenuEntry("Benchmark [b]", 'b');
|
||||
glutAddMenuEntry("DrawCurves [c]", 'c');
|
||||
glutAddMenuEntry("Quit [esc]", 27);
|
||||
glutAttachMenu(GLUT_RIGHT_BUTTON);
|
||||
}
|
||||
|
||||
void runBenchmark(int iterations)
|
||||
{
|
||||
printf("[%s] (Benchmark Mode)\n", sSDKsample);
|
||||
|
||||
sdkCreateTimer(&timer);
|
||||
|
||||
uchar4 *d_output;
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void **)&d_output, &num_bytes, cuda_pbo_resource));
|
||||
|
||||
sdkStartTimer(&timer);
|
||||
|
||||
for (int i = 0; i < iterations; ++i) {
|
||||
render(imageWidth, imageHeight, tx, ty, scale, cx, cy, blockSize, gridSize, g_FilterMode, d_output);
|
||||
}
|
||||
|
||||
cudaDeviceSynchronize();
|
||||
sdkStopTimer(&timer);
|
||||
float time = sdkGetTimerValue(&timer) / (float)iterations;
|
||||
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
|
||||
|
||||
printf("time: %0.3f ms, %f Mpixels/sec\n", time, (width * height / (time * 0.001f)) / 1e6);
|
||||
}
|
||||
|
||||
void runAutoTest(int argc, char **argv, const char *dump_filename, eFilterMode filter_mode)
|
||||
{
|
||||
cudaDeviceProp deviceProps;
|
||||
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
|
||||
|
||||
printf("[%s] (automated testing w/ readback)\n", sSDKsample);
|
||||
printf("CUDA device [%s] has %d Multi-Processors\n", deviceProps.name, deviceProps.multiProcessorCount);
|
||||
|
||||
loadImageData(argc, argv);
|
||||
|
||||
uchar4 *d_output;
|
||||
checkCudaErrors(cudaMalloc((void **)&d_output, imageWidth * imageHeight * 4));
|
||||
unsigned int *h_result = (unsigned int *)malloc(width * height * sizeof(unsigned int));
|
||||
|
||||
printf("AutoTest: %s Filter Mode: <%s>\n", sSDKsample, sFilterMode[g_FilterMode]);
|
||||
|
||||
render(imageWidth, imageHeight, tx, ty, scale, cx, cy, blockSize, gridSize, filter_mode, d_output);
|
||||
|
||||
// check if kernel execution generated an error
|
||||
getLastCudaError("Error: render (bicubicTexture) Kernel execution FAILED");
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
cudaMemcpy(h_result, d_output, imageWidth * imageHeight * 4, cudaMemcpyDeviceToHost);
|
||||
|
||||
sdkSavePPM4ub(dump_filename, (unsigned char *)h_result, imageWidth, imageHeight);
|
||||
|
||||
checkCudaErrors(cudaFree(d_output));
|
||||
free(h_result);
|
||||
}
|
||||
|
||||
#if USE_BUFFER_TEX
|
||||
// fragment program for reading from buffer texture
|
||||
static const char *shaderCode = "!!NVfp4.0\n"
|
||||
"INT PARAM width = program.local[0];\n"
|
||||
"INT TEMP index;\n"
|
||||
"FLR.S index, fragment.texcoord;\n"
|
||||
"MAD.S index.x, index.y, width, index.x;\n" // compute 1D index from 2D
|
||||
// coords
|
||||
"TXF result.color, index.x, texture[0], BUFFER;\n"
|
||||
"END";
|
||||
#endif
|
||||
|
||||
GLuint compileASMShader(GLenum program_type, const char *code)
|
||||
{
|
||||
GLuint program_id;
|
||||
glGenProgramsARB(1, &program_id);
|
||||
glBindProgramARB(program_type, program_id);
|
||||
glProgramStringARB(program_type, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)strlen(code), (GLubyte *)code);
|
||||
|
||||
GLint error_pos;
|
||||
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
|
||||
|
||||
if (error_pos != -1) {
|
||||
const GLubyte *error_string;
|
||||
error_string = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
|
||||
fprintf(stderr, "Program error at position: %d\n%s\n", (int)error_pos, error_string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return program_id;
|
||||
}
|
||||
|
||||
void initialize(int argc, char **argv)
|
||||
{
|
||||
printf("[%s] (OpenGL Mode)\n", sSDKsample);
|
||||
|
||||
initGL(&argc, argv);
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
// get number of SMs on this GPU
|
||||
cudaDeviceProp deviceProps;
|
||||
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
|
||||
printf("CUDA device [%s] has %d Multi-Processors\n", deviceProps.name, deviceProps.multiProcessorCount);
|
||||
|
||||
// Create the timer (for fps measurement)
|
||||
sdkCreateTimer(&timer);
|
||||
|
||||
// load image from disk
|
||||
loadImageData(argc, argv);
|
||||
|
||||
printf("\n"
|
||||
"\tControls\n"
|
||||
"\t=/- : Zoom in/out\n"
|
||||
"\tb : Run Benchmark g_FilterMode\n"
|
||||
"\tc : Draw Bicubic Spline Curve\n"
|
||||
"\t[esc] - Quit\n\n"
|
||||
|
||||
"\tPress number keys to change filtering g_FilterMode:\n\n"
|
||||
"\t1 : nearest filtering\n"
|
||||
"\t2 : bilinear filtering\n"
|
||||
"\t3 : bicubic filtering\n"
|
||||
"\t4 : fast bicubic filtering\n"
|
||||
"\t5 : Catmull-Rom filtering\n\n");
|
||||
|
||||
initGLBuffers();
|
||||
|
||||
#if USE_BUFFER_TEX
|
||||
fprog = compileASMShader(GL_FRAGMENT_PROGRAM_ARB, shaderCode);
|
||||
|
||||
if (!fprog) {
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void initGL(int *argc, char **argv)
|
||||
{
|
||||
// initialize GLUT callback functions
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH);
|
||||
glutInitWindowSize(width, height);
|
||||
glutCreateWindow("CUDA bicubic texture filtering");
|
||||
glutDisplayFunc(display);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutMouseFunc(mouse);
|
||||
glutMotionFunc(motion);
|
||||
glutReshapeFunc(reshape);
|
||||
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
|
||||
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
atexit(cleanup);
|
||||
#else
|
||||
glutCloseFunc(cleanup);
|
||||
#endif
|
||||
|
||||
initMenus();
|
||||
|
||||
if (!isGLVersionSupported(2, 0) || !areGLExtensionsSupported("GL_ARB_pixel_buffer_object")) {
|
||||
fprintf(stderr, "Required OpenGL extensions are missing.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
#if USE_BUFFER_TEX
|
||||
|
||||
if (!areGLExtensionsSupported("GL_EXT_texture_buffer_object")) {
|
||||
fprintf(stderr, "OpenGL extension: GL_EXT_texture_buffer_object missing.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!areGLExtensionsSupported("GL_NV_gpu_program4")) {
|
||||
fprintf(stderr, "OpenGL extension: GL_NV_gpu_program4 missing.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void loadImageData(int argc, char **argv)
|
||||
{
|
||||
// load image from disk
|
||||
uchar *h_data = NULL;
|
||||
char *srcImagePath = NULL;
|
||||
|
||||
if ((srcImagePath = sdkFindFilePath(srcImageFilename, argv[0])) == NULL) {
|
||||
printf("bicubicTexture loadImageData() could not find <%s>\nExiting...\n", srcImageFilename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
sdkLoadPGM<unsigned char>(srcImagePath, &h_data, &imageWidth, &imageHeight);
|
||||
|
||||
printf("Loaded '%s', %d x %d pixels\n", srcImageFilename, imageWidth, imageHeight);
|
||||
|
||||
cx = imageWidth * 0.5f;
|
||||
cy = imageHeight * 0.5f;
|
||||
|
||||
// initialize texture
|
||||
initTexture(imageWidth, imageHeight, h_data);
|
||||
}
|
||||
|
||||
void printHelp()
|
||||
{
|
||||
printf("bicubicTexture Usage:\n");
|
||||
printf("\t-file=output.ppm (output file to save to disk)\n");
|
||||
printf("\t-mode=n (0=Nearest, 1=Bilinear, 2=Bicubic, 3=Fast-Bicubic, "
|
||||
"4=Catmull-Rom\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Program main
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pArgc = &argc;
|
||||
pArgv = argv;
|
||||
|
||||
// parse arguments
|
||||
char *filename;
|
||||
|
||||
#if defined(__linux__)
|
||||
setenv("DISPLAY", ":0", 0);
|
||||
#endif
|
||||
|
||||
printf("Starting bicubicTexture\n");
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help")) {
|
||||
printHelp();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "mode")) {
|
||||
g_FilterMode = (eFilterMode)getCmdLineArgumentInt(argc, (const char **)argv, "mode");
|
||||
|
||||
if (g_FilterMode < 0 || g_FilterMode >= NUM_MODES) {
|
||||
printf("Invalid Mode setting %d\n", g_FilterMode);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (getCmdLineArgumentString(argc, (const char **)argv, "file", &filename)) {
|
||||
dumpFilename = filename;
|
||||
fpsLimit = frameCheckNumber;
|
||||
|
||||
// Running CUDA kernel (bicubicFiltering) without visualization (QA
|
||||
// Testing/Verification)
|
||||
runAutoTest(argc, argv, (const char *)dumpFilename, g_FilterMode);
|
||||
}
|
||||
else {
|
||||
// This runs the CUDA kernel (bicubicFiltering) + OpenGL visualization
|
||||
initialize(argc, argv);
|
||||
glutMainLoop();
|
||||
}
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
134
cpp/5_Domain_Specific/bicubicTexture/bicubicTexture_cuda.cu
Normal file
134
cpp/5_Domain_Specific/bicubicTexture/bicubicTexture_cuda.cu
Normal file
@@ -0,0 +1,134 @@
|
||||
/* 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 _BICUBICTEXTURE_CU_
|
||||
#define _BICUBICTEXTURE_CU_
|
||||
|
||||
#include <helper_math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// includes, cuda
|
||||
#include <helper_cuda.h>
|
||||
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned char uchar;
|
||||
|
||||
#include "bicubicTexture_kernel.cuh"
|
||||
|
||||
cudaArray *d_imageArray = 0;
|
||||
|
||||
extern "C" void initTexture(int imageWidth, int imageHeight, uchar *h_data)
|
||||
{
|
||||
// allocate array and copy image data
|
||||
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0, cudaChannelFormatKindUnsigned);
|
||||
checkCudaErrors(cudaMallocArray(&d_imageArray, &channelDesc, imageWidth, imageHeight));
|
||||
checkCudaErrors(cudaMemcpy2DToArray(d_imageArray,
|
||||
0,
|
||||
0,
|
||||
h_data,
|
||||
imageWidth * sizeof(uchar),
|
||||
imageWidth * sizeof(uchar),
|
||||
imageHeight,
|
||||
cudaMemcpyHostToDevice));
|
||||
free(h_data);
|
||||
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = d_imageArray;
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = false;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeClamp;
|
||||
texDescr.addressMode[1] = cudaAddressModeClamp;
|
||||
texDescr.readMode = cudaReadModeNormalizedFloat;
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&texObjLinear, &texRes, &texDescr, NULL));
|
||||
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
texDescr.normalizedCoords = false;
|
||||
texDescr.filterMode = cudaFilterModePoint;
|
||||
texDescr.addressMode[0] = cudaAddressModeClamp;
|
||||
texDescr.addressMode[1] = cudaAddressModeClamp;
|
||||
texDescr.readMode = cudaReadModeNormalizedFloat;
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&texObjPoint, &texRes, &texDescr, NULL));
|
||||
}
|
||||
|
||||
extern "C" void freeTexture()
|
||||
{
|
||||
checkCudaErrors(cudaDestroyTextureObject(texObjPoint));
|
||||
checkCudaErrors(cudaDestroyTextureObject(texObjLinear));
|
||||
checkCudaErrors(cudaFreeArray(d_imageArray));
|
||||
}
|
||||
|
||||
// render image using CUDA
|
||||
extern "C" void render(int width,
|
||||
int height,
|
||||
float tx,
|
||||
float ty,
|
||||
float scale,
|
||||
float cx,
|
||||
float cy,
|
||||
dim3 blockSize,
|
||||
dim3 gridSize,
|
||||
int filter_mode,
|
||||
uchar4 *output)
|
||||
{
|
||||
// call CUDA kernel, writing results to PBO memory
|
||||
switch (filter_mode) {
|
||||
case MODE_NEAREST:
|
||||
d_render<<<gridSize, blockSize>>>(output, width, height, tx, ty, scale, cx, cy, texObjPoint);
|
||||
break;
|
||||
|
||||
case MODE_BILINEAR:
|
||||
d_render<<<gridSize, blockSize>>>(output, width, height, tx, ty, scale, cx, cy, texObjLinear);
|
||||
break;
|
||||
|
||||
case MODE_BICUBIC:
|
||||
d_renderBicubic<<<gridSize, blockSize>>>(output, width, height, tx, ty, scale, cx, cy, texObjPoint);
|
||||
break;
|
||||
|
||||
case MODE_FAST_BICUBIC:
|
||||
d_renderFastBicubic<<<gridSize, blockSize>>>(output, width, height, tx, ty, scale, cx, cy, texObjLinear);
|
||||
break;
|
||||
|
||||
case MODE_CATROM:
|
||||
d_renderCatRom<<<gridSize, blockSize>>>(output, width, height, tx, ty, scale, cx, cy, texObjPoint);
|
||||
break;
|
||||
}
|
||||
|
||||
getLastCudaError("kernel failed");
|
||||
}
|
||||
|
||||
#endif
|
||||
371
cpp/5_Domain_Specific/bicubicTexture/bicubicTexture_kernel.cuh
Normal file
371
cpp/5_Domain_Specific/bicubicTexture/bicubicTexture_kernel.cuh
Normal file
@@ -0,0 +1,371 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
Bicubic filtering
|
||||
See GPU Gems 2: "Fast Third-Order Texture Filtering", Sigg & Hadwiger
|
||||
https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-20-fast-third-order-texture-filtering
|
||||
|
||||
Reformulation thanks to Keenan Crane
|
||||
*/
|
||||
|
||||
#ifndef _BICUBICTEXTURE_KERNEL_CUH_
|
||||
#define _BICUBICTEXTURE_KERNEL_CUH_
|
||||
|
||||
enum Mode { MODE_NEAREST, MODE_BILINEAR, MODE_BICUBIC, MODE_FAST_BICUBIC, MODE_CATROM };
|
||||
|
||||
cudaTextureObject_t texObjPoint, texObjLinear;
|
||||
|
||||
// w0, w1, w2, and w3 are the four cubic B-spline basis functions
|
||||
__host__ __device__ float w0(float a)
|
||||
{
|
||||
// return (1.0f/6.0f)*(-a*a*a + 3.0f*a*a - 3.0f*a + 1.0f);
|
||||
return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f); // optimized
|
||||
}
|
||||
|
||||
__host__ __device__ float w1(float a)
|
||||
{
|
||||
// return (1.0f/6.0f)*(3.0f*a*a*a - 6.0f*a*a + 4.0f);
|
||||
return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f);
|
||||
}
|
||||
|
||||
__host__ __device__ float w2(float a)
|
||||
{
|
||||
// return (1.0f/6.0f)*(-3.0f*a*a*a + 3.0f*a*a + 3.0f*a + 1.0f);
|
||||
return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f);
|
||||
}
|
||||
|
||||
__host__ __device__ float w3(float a) { return (1.0f / 6.0f) * (a * a * a); }
|
||||
|
||||
// g0 and g1 are the two amplitude functions
|
||||
__device__ float g0(float a) { return w0(a) + w1(a); }
|
||||
|
||||
__device__ float g1(float a) { return w2(a) + w3(a); }
|
||||
|
||||
// h0 and h1 are the two offset functions
|
||||
__device__ float h0(float a)
|
||||
{
|
||||
// note +0.5 offset to compensate for CUDA linear filtering convention
|
||||
return -1.0f + w1(a) / (w0(a) + w1(a)) + 0.5f;
|
||||
}
|
||||
|
||||
__device__ float h1(float a) { return 1.0f + w3(a) / (w2(a) + w3(a)) + 0.5f; }
|
||||
|
||||
// filter 4 values using cubic splines
|
||||
template <class T> __device__ T cubicFilter(float x, T c0, T c1, T c2, T c3)
|
||||
{
|
||||
T r;
|
||||
r = c0 * w0(x);
|
||||
r += c1 * w1(x);
|
||||
r += c2 * w2(x);
|
||||
r += c3 * w3(x);
|
||||
return r;
|
||||
}
|
||||
|
||||
// slow but precise bicubic lookup using 16 texture lookups
|
||||
template <class T, class R> // texture data type, return type
|
||||
__device__ R tex2DBicubic(const cudaTextureObject_t tex, float x, float y)
|
||||
{
|
||||
x -= 0.5f;
|
||||
y -= 0.5f;
|
||||
float px = floorf(x);
|
||||
float py = floorf(y);
|
||||
float fx = x - px;
|
||||
float fy = y - py;
|
||||
|
||||
return cubicFilter<R>(
|
||||
fy,
|
||||
cubicFilter<R>(fx,
|
||||
tex2D<R>(tex, px - 1, py - 1),
|
||||
tex2D<R>(tex, px, py - 1),
|
||||
tex2D<R>(tex, px + 1, py - 1),
|
||||
tex2D<R>(tex, px + 2, py - 1)),
|
||||
cubicFilter<R>(
|
||||
fx, tex2D<R>(tex, px - 1, py), tex2D<R>(tex, px, py), tex2D<R>(tex, px + 1, py), tex2D<R>(tex, px + 2, py)),
|
||||
cubicFilter<R>(fx,
|
||||
tex2D<R>(tex, px - 1, py + 1),
|
||||
tex2D<R>(tex, px, py + 1),
|
||||
tex2D<R>(tex, px + 1, py + 1),
|
||||
tex2D<R>(tex, px + 2, py + 1)),
|
||||
cubicFilter<R>(fx,
|
||||
tex2D<R>(tex, px - 1, py + 2),
|
||||
tex2D<R>(tex, px, py + 2),
|
||||
tex2D<R>(tex, px + 1, py + 2),
|
||||
tex2D<R>(tex, px + 2, py + 2)));
|
||||
}
|
||||
|
||||
// fast bicubic texture lookup using 4 bilinear lookups
|
||||
// assumes texture is set to non-normalized coordinates, point sampling
|
||||
template <class T, class R> // texture data type, return type
|
||||
__device__ R tex2DFastBicubic(const cudaTextureObject_t tex, float x, float y)
|
||||
{
|
||||
x -= 0.5f;
|
||||
y -= 0.5f;
|
||||
float px = floorf(x);
|
||||
float py = floorf(y);
|
||||
float fx = x - px;
|
||||
float fy = y - py;
|
||||
|
||||
// note: we could store these functions in a lookup table texture, but maths
|
||||
// is cheap
|
||||
float g0x = g0(fx);
|
||||
float g1x = g1(fx);
|
||||
float h0x = h0(fx);
|
||||
float h1x = h1(fx);
|
||||
float h0y = h0(fy);
|
||||
float h1y = h1(fy);
|
||||
|
||||
R r = g0(fy) * (g0x * tex2D<R>(tex, px + h0x, py + h0y) + g1x * tex2D<R>(tex, px + h1x, py + h0y))
|
||||
+ g1(fy) * (g0x * tex2D<R>(tex, px + h0x, py + h1y) + g1x * tex2D<R>(tex, px + h1x, py + h1y));
|
||||
return r;
|
||||
}
|
||||
|
||||
// higher-precision 2D bilinear lookup
|
||||
template <class T, class R> // texture data type, return type
|
||||
__device__ R tex2DBilinear(const cudaTextureObject_t tex, float x, float y)
|
||||
{
|
||||
x -= 0.5f;
|
||||
y -= 0.5f;
|
||||
float px = floorf(x); // integer position
|
||||
float py = floorf(y);
|
||||
float fx = x - px; // fractional position
|
||||
float fy = y - py;
|
||||
px += 0.5f;
|
||||
py += 0.5f;
|
||||
|
||||
return lerp(lerp(tex2D<R>(tex, px, py), tex2D<R>(tex, px + 1.0f, py), fx),
|
||||
lerp(tex2D<R>(tex, px, py + 1.0f), tex2D<R>(tex, px + 1.0f, py + 1.0f), fx),
|
||||
fy);
|
||||
}
|
||||
|
||||
#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 200
|
||||
|
||||
/*
|
||||
bilinear 2D texture lookup using tex2Dgather function
|
||||
- tex2Dgather() returns the four neighbouring samples in a single texture
|
||||
lookup
|
||||
- it is only supported on the Fermi architecture
|
||||
- you can select which component to fetch using the "comp" parameter
|
||||
- it can be used to efficiently implement custom texture filters
|
||||
|
||||
The samples are returned in a 4-vector in the following order:
|
||||
x: (0, 1)
|
||||
y: (1, 1)
|
||||
z: (1, 0)
|
||||
w: (0, 0)
|
||||
*/
|
||||
|
||||
template <class T, class R> // texture data type, return type
|
||||
__device__ float tex2DBilinearGather(const cudaTextureObject_t tex, float x, float y, int comp = 0)
|
||||
{
|
||||
x -= 0.5f;
|
||||
y -= 0.5f;
|
||||
float px = floorf(x); // integer position
|
||||
float py = floorf(y);
|
||||
float fx = x - px; // fractional position
|
||||
float fy = y - py;
|
||||
|
||||
R samples = tex2Dgather<R>(tex, px + 0.5f, py + 0.5f, comp);
|
||||
|
||||
return lerp(lerp((float)samples.w, (float)samples.z, fx), lerp((float)samples.x, (float)samples.y, fx), fy);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Catmull-Rom interpolation
|
||||
|
||||
__host__ __device__ float catrom_w0(float a)
|
||||
{
|
||||
// return -0.5f*a + a*a - 0.5f*a*a*a;
|
||||
return a * (-0.5f + a * (1.0f - 0.5f * a));
|
||||
}
|
||||
|
||||
__host__ __device__ float catrom_w1(float a)
|
||||
{
|
||||
// return 1.0f - 2.5f*a*a + 1.5f*a*a*a;
|
||||
return 1.0f + a * a * (-2.5f + 1.5f * a);
|
||||
}
|
||||
|
||||
__host__ __device__ float catrom_w2(float a)
|
||||
{
|
||||
// return 0.5f*a + 2.0f*a*a - 1.5f*a*a*a;
|
||||
return a * (0.5f + a * (2.0f - 1.5f * a));
|
||||
}
|
||||
|
||||
__host__ __device__ float catrom_w3(float a)
|
||||
{
|
||||
// return -0.5f*a*a + 0.5f*a*a*a;
|
||||
return a * a * (-0.5f + 0.5f * a);
|
||||
}
|
||||
|
||||
template <class T> __device__ T catRomFilter(float x, T c0, T c1, T c2, T c3)
|
||||
{
|
||||
T r;
|
||||
r = c0 * catrom_w0(x);
|
||||
r += c1 * catrom_w1(x);
|
||||
r += c2 * catrom_w2(x);
|
||||
r += c3 * catrom_w3(x);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Note - can't use bilinear trick here because of negative lobes
|
||||
template <class T, class R> // texture data type, return type
|
||||
__device__ R tex2DCatRom(const cudaTextureObject_t tex, float x, float y)
|
||||
{
|
||||
x -= 0.5f;
|
||||
y -= 0.5f;
|
||||
float px = floorf(x);
|
||||
float py = floorf(y);
|
||||
float fx = x - px;
|
||||
float fy = y - py;
|
||||
|
||||
return catRomFilter<R>(
|
||||
fy,
|
||||
catRomFilter<R>(fx,
|
||||
tex2D<R>(tex, px - 1, py - 1),
|
||||
tex2D<R>(tex, px, py - 1),
|
||||
tex2D<R>(tex, px + 1, py - 1),
|
||||
tex2D<R>(tex, px + 2, py - 1)),
|
||||
catRomFilter<R>(
|
||||
fx, tex2D<R>(tex, px - 1, py), tex2D<R>(tex, px, py), tex2D<R>(tex, px + 1, py), tex2D<R>(tex, px + 2, py)),
|
||||
catRomFilter<R>(fx,
|
||||
tex2D<R>(tex, px - 1, py + 1),
|
||||
tex2D<R>(tex, px, py + 1),
|
||||
tex2D<R>(tex, px + 1, py + 1),
|
||||
tex2D<R>(tex, px + 2, py + 1)),
|
||||
catRomFilter<R>(fx,
|
||||
tex2D<R>(tex, px - 1, py + 2),
|
||||
tex2D<R>(tex, px, py + 2),
|
||||
tex2D<R>(tex, px + 1, py + 2),
|
||||
tex2D<R>(tex, px + 2, py + 2)));
|
||||
}
|
||||
|
||||
// test functions
|
||||
|
||||
// render image using normal bilinear texture lookup
|
||||
__global__ void d_render(uchar4 *d_output,
|
||||
uint width,
|
||||
uint height,
|
||||
float tx,
|
||||
float ty,
|
||||
float scale,
|
||||
float cx,
|
||||
float cy,
|
||||
cudaTextureObject_t texObj)
|
||||
{
|
||||
uint x = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
uint y = __umul24(blockIdx.y, blockDim.y) + threadIdx.y;
|
||||
uint i = __umul24(y, width) + x;
|
||||
|
||||
float u = (x - cx) * scale + cx + tx;
|
||||
float v = (y - cy) * scale + cy + ty;
|
||||
|
||||
if ((x < width) && (y < height)) {
|
||||
// write output color
|
||||
float c = tex2D<float>(texObj, u, v);
|
||||
// float c = tex2DBilinear<uchar, float>(tex, u, v);
|
||||
// float c = tex2DBilinearGather<uchar, uchar4>(tex2, u, v, 0) / 255.0f;
|
||||
d_output[i] = make_uchar4(c * 0xff, c * 0xff, c * 0xff, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// render image using bicubic texture lookup
|
||||
__global__ void d_renderBicubic(uchar4 *d_output,
|
||||
uint width,
|
||||
uint height,
|
||||
float tx,
|
||||
float ty,
|
||||
float scale,
|
||||
float cx,
|
||||
float cy,
|
||||
cudaTextureObject_t texObj)
|
||||
{
|
||||
uint x = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
uint y = __umul24(blockIdx.y, blockDim.y) + threadIdx.y;
|
||||
uint i = __umul24(y, width) + x;
|
||||
|
||||
float u = (x - cx) * scale + cx + tx;
|
||||
float v = (y - cy) * scale + cy + ty;
|
||||
|
||||
if ((x < width) && (y < height)) {
|
||||
// write output color
|
||||
float c = tex2DBicubic<uchar, float>(texObj, u, v);
|
||||
d_output[i] = make_uchar4(c * 0xff, c * 0xff, c * 0xff, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// render image using fast bicubic texture lookup
|
||||
__global__ void d_renderFastBicubic(uchar4 *d_output,
|
||||
uint width,
|
||||
uint height,
|
||||
float tx,
|
||||
float ty,
|
||||
float scale,
|
||||
float cx,
|
||||
float cy,
|
||||
cudaTextureObject_t texObj)
|
||||
{
|
||||
uint x = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
uint y = __umul24(blockIdx.y, blockDim.y) + threadIdx.y;
|
||||
uint i = __umul24(y, width) + x;
|
||||
|
||||
float u = (x - cx) * scale + cx + tx;
|
||||
float v = (y - cy) * scale + cy + ty;
|
||||
|
||||
if ((x < width) && (y < height)) {
|
||||
// write output color
|
||||
float c = tex2DFastBicubic<uchar, float>(texObj, u, v);
|
||||
d_output[i] = make_uchar4(c * 0xff, c * 0xff, c * 0xff, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// render image using Catmull-Rom texture lookup
|
||||
__global__ void d_renderCatRom(uchar4 *d_output,
|
||||
uint width,
|
||||
uint height,
|
||||
float tx,
|
||||
float ty,
|
||||
float scale,
|
||||
float cx,
|
||||
float cy,
|
||||
cudaTextureObject_t texObj)
|
||||
{
|
||||
uint x = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
uint y = __umul24(blockIdx.y, blockDim.y) + threadIdx.y;
|
||||
uint i = __umul24(y, width) + x;
|
||||
|
||||
float u = (x - cx) * scale + cx + tx;
|
||||
float v = (y - cy) * scale + cy + ty;
|
||||
|
||||
if ((x < width) && (y < height)) {
|
||||
// write output color
|
||||
float c = tex2DCatRom<uchar, float>(texObj, u, v);
|
||||
d_output[i] = make_uchar4(c * 0xff, c * 0xff, c * 0xff, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // _BICUBICTEXTURE_KERNEL_CUH_
|
||||
5
cpp/5_Domain_Specific/bicubicTexture/data/0_nearest.ppm
Normal file
5
cpp/5_Domain_Specific/bicubicTexture/data/0_nearest.ppm
Normal file
File diff suppressed because one or more lines are too long
5
cpp/5_Domain_Specific/bicubicTexture/data/1_bilinear.ppm
Normal file
5
cpp/5_Domain_Specific/bicubicTexture/data/1_bilinear.ppm
Normal file
File diff suppressed because one or more lines are too long
5
cpp/5_Domain_Specific/bicubicTexture/data/2_bicubic.ppm
Normal file
5
cpp/5_Domain_Specific/bicubicTexture/data/2_bicubic.ppm
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
BIN
cpp/5_Domain_Specific/bicubicTexture/data/teapot512.pgm
Normal file
BIN
cpp/5_Domain_Specific/bicubicTexture/data/teapot512.pgm
Normal file
Binary file not shown.
Reference in New Issue
Block a user