gpu绘制波纹
每个线程绘制一个像素点,可以显示高清的动画
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include "common/book.h" #include "common/cpu_bitmap.h" #include "common/cpu_anim.h" const int DIM_WIDTH = 1920; const int DIM_HEIGHT = 1080; __global__ void rippleKernel(unsigned char* ptr, int ticks) { // mapping threadIdx/BlockIdx to cell pos int x = threadIdx.x + blockIdx.x * blockDim.x; int y = threadIdx.y + blockIdx.y * blockDim.y; int offset = x + y * blockDim.x * gridDim.x; //calculate the value on the cell float fx = x - DIM_WIDTH / 2; float fy = y - DIM_HEIGHT / 2; float d = sqrtf(fx * fx + fy * fy); unsigned char grey = (unsigned char)(128.0f + 127.0f * cos(d / 10.0f - ticks / 7.0f) / (d / 10.0f + 1.0f)); ptr[offset * 4 + 0] = grey; ptr[offset * 4 + 1] = grey; ptr[offset * 4 + 2] = grey; ptr[offset * 4 + 3] = 255; } struct DataBlock { unsigned char* dev_bitmap; CPUAnimBitmap* bitmap; }; void cleanup(DataBlock* d) { cudaFree(d->dev_bitmap); } void generate_frame(DataBlock* d, int ticks) { dim3 blocks(DIM_WIDTH / 16, DIM_HEIGHT / 16); dim3 threads(16, 16); //dim3 threads(DIM_WIDTH, DIM_HEIGHT); rippleKernel << <blocks, threads >> > (d->dev_bitmap, ticks); HANDLE_ERROR(cudaMemcpy (d->bitmap->get_ptr(), d->dev_bitmap, d->bitmap->image_size(), cudaMemcpyDeviceToHost)); } void testGPURipple() { DataBlock data; CPUAnimBitmap bitmap(DIM_WIDTH, DIM_HEIGHT, &data); data.bitmap = &bitmap; HANDLE_ERROR(cudaMalloc((void**)&data.dev_bitmap,bitmap.image_size())); bitmap.anim_and_exit((void(*)(void*, int))generate_frame, (void(*)(void*))cleanup); } int main() { //testSaclarAdd(); //getDeviceProp(); //testJuliaSet(); //testGPUJuliaSet(); testGPURipple(); return 0; }

浙公网安备 33010602011771号