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:
61
cpp/5_Domain_Specific/simpleD3D11Texture/CMakeLists.txt
Normal file
61
cpp/5_Domain_Specific/simpleD3D11Texture/CMakeLists.txt
Normal file
@@ -0,0 +1,61 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(simpleD3D11Texture 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)
|
||||
# Source file
|
||||
# Add target for simpleD3D11Texture
|
||||
add_executable(simpleD3D11Texture
|
||||
simpleD3D11Texture.cpp
|
||||
../../../Common/rendercheck_d3d11.cpp
|
||||
texture_2d.cu
|
||||
texture_3d.cu
|
||||
texture_cube.cu
|
||||
)
|
||||
|
||||
target_compile_options(simpleD3D11Texture PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(simpleD3D11Texture PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(simpleD3D11Texture PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_include_directories(simpleD3D11Texture PRIVATE
|
||||
${CUDAToolkit_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_link_libraries(simpleD3D11Texture PRIVATE
|
||||
d3d11
|
||||
dxgi
|
||||
dxguid
|
||||
d3dcompiler
|
||||
)
|
||||
|
||||
add_custom_command(TARGET simpleD3D11Texture POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/data
|
||||
${CMAKE_CURRENT_BINARY_DIR}/data
|
||||
)
|
||||
else()
|
||||
message(STATUS "Sample 'simpleD3D11Texture' is Windows-only - skipping")
|
||||
endif()
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
36
cpp/5_Domain_Specific/simpleD3D11Texture/README.md
Normal file
36
cpp/5_Domain_Specific/simpleD3D11Texture/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# simpleD3D11Texture - Simple D3D11 Texture
|
||||
|
||||
## Description
|
||||
|
||||
Simple program which demonstrates Direct3D11 Texture interoperability with CUDA. The program creates a number of D3D11 Textures (2D, 3D, and CubeMap) which are written to from CUDA kernels. Direct3D then renders the results on the screen. A Direct3D Capable device is required.
|
||||
|
||||
## 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
|
||||
|
||||
Windows
|
||||
|
||||
## Supported CPU Architecture
|
||||
|
||||
x86_64
|
||||
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaGraphicsUnmapResources, cudaMalloc, cudaMallocPitch, cudaGetErrorString, cudaFree, cudaGetLastError, cudaGraphicsMapResources, cudaGetDeviceCount, cudaMemset, cudaGraphicsUnregisterResource, cudaGraphicsSubResourceGetMappedArray, cudaGetDeviceProperties
|
||||
|
||||
## Dependencies needed to build/run
|
||||
[DirectX](../../../README.md#directx)
|
||||
|
||||
## 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)
|
||||
1728
cpp/5_Domain_Specific/simpleD3D11Texture/d3dx11effect/d3dx11effect.h
Normal file
1728
cpp/5_Domain_Specific/simpleD3D11Texture/d3dx11effect/d3dx11effect.h
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
1268
cpp/5_Domain_Specific/simpleD3D11Texture/simpleD3D11Texture.cpp
Normal file
1268
cpp/5_Domain_Specific/simpleD3D11Texture/simpleD3D11Texture.cpp
Normal file
File diff suppressed because it is too large
Load Diff
78
cpp/5_Domain_Specific/simpleD3D11Texture/texture_2d.cu
Normal file
78
cpp/5_Domain_Specific/simpleD3D11Texture/texture_2d.cu
Normal file
@@ -0,0 +1,78 @@
|
||||
/* 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PI 3.1415926536f
|
||||
|
||||
/*
|
||||
* Paint a 2D texture with a moving red/green hatch pattern on a
|
||||
* strobing blue background. Note that this kernel reads to and
|
||||
* writes from the texture, hence why this texture was not mapped
|
||||
* as WriteDiscard.
|
||||
*/
|
||||
__global__ void cuda_kernel_texture_2d(unsigned char *surface, int width, int height, size_t pitch, float t)
|
||||
{
|
||||
int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
float *pixel;
|
||||
|
||||
// in the case where, due to quantization into grids, we have
|
||||
// more threads than pixels, skip the threads which don't
|
||||
// correspond to valid pixels
|
||||
if (x >= width || y >= height)
|
||||
return;
|
||||
|
||||
// get a pointer to the pixel at (x,y)
|
||||
pixel = (float *)(surface + y * pitch) + 4 * x;
|
||||
|
||||
// populate it
|
||||
float value_x = 0.5f + 0.5f * cos(t + 10.0f * ((2.0f * x) / width - 1.0f));
|
||||
float value_y = 0.5f + 0.5f * cos(t + 10.0f * ((2.0f * y) / height - 1.0f));
|
||||
pixel[0] = 0.5 * pixel[0] + 0.5 * pow(value_x, 3.0f); // red
|
||||
pixel[1] = 0.5 * pixel[1] + 0.5 * pow(value_y, 3.0f); // green
|
||||
pixel[2] = 0.5f + 0.5f * cos(t); // blue
|
||||
pixel[3] = 1; // alpha
|
||||
}
|
||||
|
||||
extern "C" void cuda_texture_2d(void *surface, int width, int height, size_t pitch, float t)
|
||||
{
|
||||
cudaError_t error = cudaSuccess;
|
||||
|
||||
dim3 Db = dim3(16, 16); // block dimensions are fixed to be 256 threads
|
||||
dim3 Dg = dim3((width + Db.x - 1) / Db.x, (height + Db.y - 1) / Db.y);
|
||||
|
||||
cuda_kernel_texture_2d<<<Dg, Db>>>((unsigned char *)surface, width, height, pitch, t);
|
||||
|
||||
error = cudaGetLastError();
|
||||
|
||||
if (error != cudaSuccess) {
|
||||
printf("cuda_kernel_texture_2d() failed to launch error = %d\n", error);
|
||||
}
|
||||
}
|
||||
80
cpp/5_Domain_Specific/simpleD3D11Texture/texture_3d.cu
Normal file
80
cpp/5_Domain_Specific/simpleD3D11Texture/texture_3d.cu
Normal file
@@ -0,0 +1,80 @@
|
||||
/* 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* Paint a 3D texture with a gradient in X (blue) and Z (green), and have every
|
||||
* other Z slice have full red.
|
||||
*/
|
||||
__global__ void cuda_kernel_texture_3d(unsigned char *surface,
|
||||
int width,
|
||||
int height,
|
||||
int depth,
|
||||
size_t pitch,
|
||||
size_t pitchSlice,
|
||||
float t)
|
||||
{
|
||||
int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
|
||||
// in the case where, due to quantization into grids, we have
|
||||
// more threads than pixels, skip the threads which don't
|
||||
// correspond to valid pixels
|
||||
if (x >= width || y >= height)
|
||||
return;
|
||||
|
||||
// walk across the Z slices of this texture. it should be noted that
|
||||
// this is far from optimal data access.
|
||||
for (int z = 0; z < depth; ++z) {
|
||||
// get a pointer to this pixel
|
||||
unsigned char *pixel = surface + z * pitchSlice + y * pitch + 4 * x;
|
||||
pixel[0] = (unsigned char)(255.f * (0.5f + 0.5f * cos(t + (x * x + y * y + z * z) * 0.0001f * 3.14f))); // red
|
||||
pixel[1] = (unsigned char)(255.f * (0.5f + 0.5f * sin(t + (x * x + y * y + z * z) * 0.0001f * 3.14f))); // green
|
||||
pixel[2] = (unsigned char)0; // blue
|
||||
pixel[3] = 255; // alpha
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void
|
||||
cuda_texture_3d(void *surface, int width, int height, int depth, size_t pitch, size_t pitchSlice, float t)
|
||||
{
|
||||
cudaError_t error = cudaSuccess;
|
||||
|
||||
dim3 Db = dim3(16, 16); // block dimensions are fixed to be 256 threads
|
||||
dim3 Dg = dim3((width + Db.x - 1) / Db.x, (height + Db.y - 1) / Db.y);
|
||||
|
||||
cuda_kernel_texture_3d<<<Dg, Db>>>((unsigned char *)surface, width, height, depth, pitch, pitchSlice, t);
|
||||
|
||||
error = cudaGetLastError();
|
||||
|
||||
if (error != cudaSuccess) {
|
||||
printf("cuda_kernel_texture_3d() failed to launch error = %d\n", error);
|
||||
}
|
||||
}
|
||||
92
cpp/5_Domain_Specific/simpleD3D11Texture/texture_cube.cu
Normal file
92
cpp/5_Domain_Specific/simpleD3D11Texture/texture_cube.cu
Normal file
@@ -0,0 +1,92 @@
|
||||
/* 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PI 3.1415926536f
|
||||
|
||||
/*
|
||||
* Paint a 2D surface with a moving bulls-eye pattern. The "face" parameter
|
||||
* selects
|
||||
* between 6 different colors to use. We will use a different color on each
|
||||
* face of a
|
||||
* cube map.
|
||||
*/
|
||||
__global__ void cuda_kernel_texture_cube(char *surface, int width, int height, size_t pitch, int face, float t)
|
||||
{
|
||||
int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
unsigned char *pixel;
|
||||
|
||||
// in the case where, due to quantization into grids, we have
|
||||
// more threads than pixels, skip the threads which don't
|
||||
// correspond to valid pixels
|
||||
if (x >= width || y >= height)
|
||||
return;
|
||||
|
||||
// get a pointer to this pixel
|
||||
pixel = (unsigned char *)(surface + y * pitch) + 4 * x;
|
||||
|
||||
// populate it
|
||||
float theta_x = (2.0f * x) / width - 1.0f;
|
||||
float theta_y = (2.0f * y) / height - 1.0f;
|
||||
float theta = 2.0f * PI * sqrt(theta_x * theta_x + theta_y * theta_y);
|
||||
unsigned char value = 255 * (0.6f + 0.4f * cos(theta + t));
|
||||
|
||||
pixel[3] = 255; // alpha
|
||||
|
||||
if (face % 2) {
|
||||
pixel[0] = // blue
|
||||
pixel[1] = // green
|
||||
pixel[2] = 0.5; // red
|
||||
pixel[face / 2] = value;
|
||||
}
|
||||
else {
|
||||
pixel[0] = // blue
|
||||
pixel[1] = // green
|
||||
pixel[2] = value; // red
|
||||
pixel[face / 2] = 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void cuda_texture_cube(void *surface, int width, int height, size_t pitch, int face, float t)
|
||||
{
|
||||
cudaError_t error = cudaSuccess;
|
||||
|
||||
dim3 Db = dim3(16, 16); // block dimensions are fixed to be 256 threads
|
||||
dim3 Dg = dim3((width + Db.x - 1) / Db.x, (height + Db.y - 1) / Db.y);
|
||||
|
||||
cuda_kernel_texture_cube<<<Dg, Db>>>((char *)surface, width, height, pitch, face, t);
|
||||
|
||||
error = cudaGetLastError();
|
||||
|
||||
if (error != cudaSuccess) {
|
||||
printf("cuda_kernel_texture_cube() failed to launch error = %d\n", error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user