8 #include "RayTracing.h"
10 #include "RayTracingDevice.h"
12 #include "RayTracingCommon.h"
14 RayTracing::RayTracing(int w, int h) :
19 #if CURRENT_MEMORY_MODEL == MEMORY_MODEL_GLOBAL
20 title("Ray Tracing (Global memory)")
21 #elif CURRENT_MEMORY_MODEL == MEMORY_MODEL_CONSTANT
22 title("Ray Tracing (Constant memory)")
23 #elif CURRENT_MEMORY_MODEL == MEMORY_MODEL_SHARED
24 title("Ray Tracing (Shared memory)")
27 Device::assertDim(dg, db);
29 Sphere* spheres = this->createSpheres(NB_SPHERES);
31 #if CURRENT_MEMORY_MODEL == MEMORY_MODEL_GLOBAL or CURRENT_MEMORY_MODEL == MEMORY_MODEL_SHARED
32 // Copie des spheres dans la global memory.
33 const size_t sizeSpheres = NB_SPHERES * sizeof(Sphere);
34 HANDLE_ERROR(cudaMalloc(&this->ptrDevSpheres, sizeSpheres));
35 HANDLE_ERROR(cudaMemcpy(this->ptrDevSpheres, spheres, sizeSpheres, cudaMemcpyHostToDevice));
36 #elif CURRENT_MEMORY_MODEL == MEMORY_MODEL_CONSTANT
37 // Copie des spheres en constant memory.
38 ConstantMemoryLink cmSpheresLink = constantMemorySpheresLink();
39 Sphere* ptrDevConstSperes = (Sphere*)cmSpheresLink.ptrDevTab;
40 size_t sizeALL = cmSpheresLink.sizeAll;
41 HANDLE_ERROR(cudaMemcpy(ptrDevConstSperes, spheres, sizeALL, cudaMemcpyHostToDevice));
44 cout << "sizeof(Sphere): " << sizeof(Sphere) << endl;
47 RayTracing::~RayTracing()
51 void RayTracing::runGPU(uchar4* ptrDevPixels)
53 #if CURRENT_MEMORY_MODEL == MEMORY_MODEL_GLOBAL or CURRENT_MEMORY_MODEL == MEMORY_MODEL_SHARED
54 rayTracing<<<dg,db>>>(ptrDevPixels, this->ptrDevSpheres, this->w, this->h, this->t);
55 #elif CURRENT_MEMORY_MODEL == MEMORY_MODEL_CONSTANT
56 rayTracing<<<dg,db>>>(ptrDevPixels, this->w, this->h, this->t);
59 HANDLE_ERROR(cudaDeviceSynchronize()); // Pour flusher les 'printf' (pour le DEBUG).
62 void RayTracing::animationStep()
67 int RayTracing::getW()
72 int RayTracing::getH()
77 float RayTracing::getT()
82 string RayTracing::getTitle()
87 Sphere* RayTracing::createSpheres(int n)
89 Sphere* spheres = new Sphere[n];
90 const int RAYON_MIN = 10;
91 const int RAYON_MAX = this->w / 10 - 1;
93 const float bord = RAYON_MAX + 1;
95 for (int i = 0; i < n; ++i)
97 spheres[i].setR(float(this->alea.uniformeAB(RAYON_MIN, RAYON_MAX)));
101 float(this->alea.uniformeAB(double(bord), double(this->w - bord))), // x.
102 float(this->alea.uniformeAB(double(bord), double(this->h - bord))), // y.
103 float(this->alea.uniformeAB(10.0, 2.0 * this->w)) // z.
106 spheres[i].setCentre(centre);
107 spheres[i].setHueInitial(float(this->alea.uniformeAB(0.0, 1.0)));