Implémentation du raytracing pour Global Memory/Shared Memory/Constant Memory
[GPU.git] / WCudaMSE / Student_Cuda_Image / src / cpp / core / 04_RayTracing / moo / host / RayTracing.cu
1 #include <iostream>
2 #include <assert.h>
3 #include <stdio.h>
4 using namespace std;
5
6 #include "AleaTools.h"
7
8 #include "RayTracing.h"
9 #include "Device.h"
10 #include "RayTracingDevice.h"
11 #include "Sphere.h"
12 #include "RayTracingCommon.h"
13
14 RayTracing::RayTracing(int w, int h) :
15     t(0), w(w), h(h),
16     dg(8, 8, 1),
17     db(32, 32, 1),
18     alea(42),
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)")
25 #endif
26     {
27     Device::assertDim(dg, db);
28
29     Sphere* spheres = this->createSpheres(NB_SPHERES);
30
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));
42 #endif
43
44     cout << "sizeof(Sphere): " << sizeof(Sphere) << endl;
45     }
46
47 RayTracing::~RayTracing()
48     {
49     }
50
51 void RayTracing::runGPU(uchar4* ptrDevPixels)
52     {
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);
57 #endif
58
59     HANDLE_ERROR(cudaDeviceSynchronize()); // Pour flusher les 'printf' (pour le DEBUG).
60     }
61
62 void RayTracing::animationStep()
63     {
64     this->t += 0.005;
65     }
66
67 int RayTracing::getW()
68     {
69     return this->w;
70     }
71
72 int RayTracing::getH()
73     {
74     return this->h;
75     }
76
77 float RayTracing::getT()
78     {
79     return this->t;
80     }
81
82 string RayTracing::getTitle()
83     {
84     return this->title;
85     }
86
87 Sphere* RayTracing::createSpheres(int n)
88     {
89     Sphere* spheres = new Sphere[n];
90     const int RAYON_MIN = 10;
91     const int RAYON_MAX = this->w / 10 - 1;
92
93     const float bord = RAYON_MAX + 1;
94
95     for (int i = 0; i < n; ++i)
96         {
97         spheres[i].setR(float(this->alea.uniformeAB(RAYON_MIN, RAYON_MAX)));
98
99         const float3 centre =
100             {
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.
104             };
105
106         spheres[i].setCentre(centre);
107         spheres[i].setHueInitial(float(this->alea.uniformeAB(0.0, 1.0)));
108         }
109
110     return spheres;
111     }