-
Notifications
You must be signed in to change notification settings - Fork 3
/
PathTracer.h
228 lines (174 loc) · 4.39 KB
/
PathTracer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
#include <glm.hpp>
#include <string>
#include <vector>
#include <unordered_map>
class BaseLevelAS;
class DeviceBuffer;
class Texture;
class Cubemap;
class Sampler;
struct GeoCls
{
std::string name;
size_t size_view;
uint32_t binding_view;
const char* fn_intersection = nullptr;
const char* fn_closesthit = nullptr;
};
class Geometry
{
public:
const glm::mat4x4& model() const { return m_model; }
const glm::mat4x4& norm() const { return m_norm_mat; }
const BaseLevelAS& get_blas() const { return *m_blas; }
Geometry(const glm::mat4x4& model);
virtual ~Geometry();
virtual GeoCls cls() const = 0;
virtual void get_view(void* view_buf) const = 0;
protected:
void _blas_create_procedure(DeviceBuffer* aabb_buf);
void _blas_create_indexed_triangles(DeviceBuffer* positionBuffer, DeviceBuffer* indexBuffer);
glm::mat4x4 m_model;
glm::mat4x4 m_norm_mat;
BaseLevelAS* m_blas;
};
class SphereLight : public Geometry
{
public:
const glm::vec3& center() const { return m_center; }
float radius() const { return m_radius; }
const glm::vec3& color() const { return m_color; }
SphereLight(const glm::vec3& center, float r, const glm::vec3& color);
virtual ~SphereLight();
virtual GeoCls cls() const;
virtual void get_view(void* view_buf) const;
private:
glm::vec3 m_center;
float m_radius;
glm::vec3 m_color;
};
struct SkyCls
{
size_t size_view;
const char* fn_missing = nullptr;
};
class Sky
{
public:
Sky() {}
virtual ~Sky() {}
virtual SkyCls cls() const = 0;
virtual void get_view(void* view_buf) const = 0;
};
class GradientSky : public Sky
{
public:
GradientSky(const glm::vec3& color0 = { 1.0f, 1.0f, 1.0f }, const glm::vec3& color1 = { 0.5f, 0.7f, 1.0f });
virtual ~GradientSky();
virtual SkyCls cls() const;
virtual void get_view(void* view_buf) const;
private:
glm::vec3 m_color0;
glm::vec3 m_color1;
};
class RGBATexture
{
public:
Texture* data() const { return m_data; }
RGBATexture(int width, int height, void* data, bool srgb = true);
~RGBATexture();
private:
Texture *m_data;
};
class RGBACubemap
{
public:
Cubemap* data() const { return m_data; }
RGBACubemap(int width, int height, void* data, bool srgb = true);
~RGBACubemap();
private:
Cubemap* m_data;
};
class TexturedSkyBox : public Sky
{
public:
TexturedSkyBox(int texId);
virtual ~TexturedSkyBox();
virtual SkyCls cls() const;
virtual void get_view(void* view_buf) const;
void rotate(float angle, const glm::vec3& v);
private:
int m_texId;
glm::mat4x4 m_transform;
};
class Image
{
public:
DeviceBuffer* data() const { return m_data; }
DeviceBuffer* rng_states();
int width() const { return m_width; }
int height() const { return m_height; }
int batch_size() const { return m_batch_size; }
Image(int width, int height, float* hdata = nullptr, int batch_size = 1 << 18);
~Image();
void clear();
// f32_vec4 - linear
void to_host_raw(float *hdata) const;
// u8_vec3 - srgb
void to_host_srgb(unsigned char* hdata, float boost = 1.0f) const;
private:
DeviceBuffer* m_data;
DeviceBuffer* m_rng_states;
int m_width;
int m_height;
int m_batch_size;
};
struct GeoList
{
GeoCls cls;
std::vector<Geometry*> list;
};
struct RayTrace;
class PathTracer
{
public:
PathTracer();
~PathTracer();
void set_target(Image* target) { m_target = target; }
void set_sky(Sky* sky);
void add_geometry(Geometry* geo);
void add_sunlight(glm::vec3 direction, float radian, glm::vec3 color);
int add_texture(RGBATexture* tex);
int add_cubemap(RGBACubemap* tex);
void set_camera(glm::vec3 lookfrom, glm::vec3 lookat, glm::vec3 vup, float vfov, float aperture = 0.0f, float focus_dist = 1.0f);
void trace(int num_iter = 100, int interval = -1) const;
private:
Image* m_target;
std::unordered_map<std::string, GeoList> m_geo_lists;
std::vector<RGBATexture*> m_textures;
std::vector<RGBACubemap*> m_cubemaps;
Sampler* m_Sampler;
GradientSky* m_default_sky;
Sky* m_current_sky;
// camera
glm::vec3 m_lookfrom;
glm::vec3 m_lookat;
glm::vec3 m_vup;
float m_vfov;
float m_aperture;
float m_focus_dist;
void _tlas_create(RayTrace& rt) const;
void _args_create(RayTrace& rt) const;
void _rt_pipeline_create(RayTrace& rt) const;
void _comp_pipeline_create(RayTrace& rt) const;
void _calc_raygen(RayTrace& rt) const;
void _calc_light_source_dist(RayTrace& rt) const;
void _rt_clean(RayTrace& rt) const;
struct Sunlight
{
glm::vec4 dir_radian;
glm::vec4 color;
};
std::vector<Sunlight> m_sunlights;
};