-
Notifications
You must be signed in to change notification settings - Fork 0
/
RayMarchingTexture.cpp
237 lines (192 loc) · 7.23 KB
/
RayMarchingTexture.cpp
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
229
230
231
232
233
234
235
236
237
#include "RayMarchingTexture.h"
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <cuda_gl_interop.h>
#include "CudaMath.h"
#include "Kernels.h"
using namespace std;
const uint3 DEFAULT_BLOCK_SIZE = make_uint3(256, 1, 1);
const uint3 DEFAULT_NUM_BLOCKS = make_uint3(0, 0, 0);
// const uint2 DEFAULT_SIZE = make_uint2(240, 135);
// const uint2 DEFAULT_SIZE = make_uint2(960, 540);
const uint2 DEFAULT_SIZE = make_uint2(1920, 1080);
const float3 DEFAULT_CAMERA_POSITION = make_float3(0.f, 0.f, -1.f);
const float3 DEFAULT_CAMERA_DIRECTION = make_float3(0.f, 0.f, 1.f);
const float3 DEFAULT_CAMERA_UP = make_float3(0.f, 1.f, 0.f);
const float DEFAULT_DEPTH = 300.f;
const float DEFAULT_WIDTH = 300.f;
const float DEFAULT_HEIGHT = 300.f;
const float3 DEFAULT_LIGHT_POSITION = DEFAULT_CAMERA_POSITION;
const float DEFAULT_DISTANCE_RATIO = 0.0008f;
const uint DEFAULT_MAX_STEPS = 25;
const float INC_FACTOR = 1.1f;
const float DEFAULT_DISTANCE_THRESHOLD = 50.f;
const float DEFAULT_EPSILON_FACTOR = 0.4f;
using RMS = RayMarchingStep;
RayMarchingTexture::RayMarchingTexture()
: NextStep_(RMS::None)
, Texture_(0)
, TexResource_(nullptr)
, TexArray_(nullptr)
, TexDataSize_(0)
{
Param_.BlockSize = DEFAULT_BLOCK_SIZE;
Param_.NumBlocks = DEFAULT_NUM_BLOCKS;
Param_.Size = DEFAULT_SIZE;
Param_.TexCuda = nullptr;
Param_.Distances = nullptr;
// Make sure the texture size uses even numbers!!!
if (Param_.Size.x % 2 != 0) Param_.Size.x++;
if (Param_.Size.y % 2 != 0) Param_.Size.y++;
Param_.TotalPixels = Param_.Size.x * Param_.Size.y;
Param_.CameraPos = DEFAULT_CAMERA_POSITION;
Param_.CameraDir = DEFAULT_CAMERA_DIRECTION;
Param_.CameraUp = DEFAULT_CAMERA_UP;
Param_.CameraLeft = Cross(Param_.CameraUp, Param_.CameraDir);
Param_.CameraRealUp = Cross(Param_.CameraDir, Param_.CameraLeft);
Param_.Depth = DEFAULT_DEPTH;
Param_.Width = DEFAULT_WIDTH;
Param_.Height = DEFAULT_HEIGHT;
Param_.LightPos = DEFAULT_LIGHT_POSITION;
Param_.DistanceRatio = DEFAULT_DISTANCE_RATIO;
Param_.MinDistance = Param_.DistanceRatio;
Param_.MaxSteps = DEFAULT_MAX_STEPS;
Param_.CurrentSubstep = 0;
Param_.DistanceThreshold = DEFAULT_DISTANCE_THRESHOLD;
Param_.EpsilonFactor = DEFAULT_EPSILON_FACTOR;
}
void RayMarchingTexture::Init()
{
const uint BlockSize = Param_.BlockSize.x * Param_.BlockSize.y * Param_.BlockSize.z;
const uint TotalThreads = Param_.TotalPixels;
const uint NumBlocks = static_cast<uint>(ceilf(static_cast<float>(TotalThreads) / BlockSize));
Param_.NumBlocks = make_uint3(NumBlocks, 1, 1);
InitTexture();
Param_.Print();
}
void RayMarchingTexture::InitTexture()
{
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &Texture_);
glBindTexture(GL_TEXTURE_2D, Texture_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)Param_.Size.x, (int)Param_.Size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
TexDataSize_ = Param_.TotalPixels * 4 * sizeof(GLubyte);
CudaCheck(cudaMalloc((void**)&Param_.TexCuda, TexDataSize_));
CudaCheck(cudaMemset(Param_.TexCuda, 0, TexDataSize_));
CudaCheck(cudaGraphicsGLRegisterImage(&TexResource_, Texture_, GL_TEXTURE_2D, cudaGraphicsMapFlagsWriteDiscard));
CudaCheck(cudaMalloc((void**)&Param_.Distances, Param_.TotalPixels * sizeof(float)));
CudaCheck(cudaMemset(Param_.Distances, 0, Param_.TotalPixels * sizeof(float)));
}
void RayMarchingTexture::Update()
{
Param_.MinDistance = Param_.DistanceRatio * fminf(GetDistanceFromCamera(), 1.f);
if (NextStep_ != RMS::None)
{
MarchTimer_.Start();
Param_.CameraLeft = Cross(Param_.CameraUp, Param_.CameraDir);
Param_.CameraRealUp = Cross(Param_.CameraDir, Param_.CameraLeft);
LaunchRayMarching(Param_, NextStep_);
CudaCheck(cudaDeviceSynchronize());
MarchTimer_.Stop();
switch (NextStep_)
{
case RMS::HalfRes:
NextStep_ = RMS::FillRes;
Param_.CurrentSubstep = 1;
break;
case RMS::FillRes:
Param_.CurrentSubstep++;
if (Param_.CurrentSubstep > 3)
{
NextStep_ = RMS::None;
Param_.CurrentSubstep = 0;
}
break;
case RMS::FullRes: NextStep_ = RMS::None; break;
case RMS::ComputeColor: NextStep_ = RMS::None; break;
default: break;
}
MapBuffers();
CopyTimer_.Start();
CudaCheck(cudaMemcpyToArray(TexArray_, 0, 0, Param_.TexCuda, TexDataSize_, cudaMemcpyDeviceToDevice));
CopyTimer_.Stop();
UnmapBuffers();
}
{
if (MarchTimer_.GetCount() >= 60)
{
cout << setprecision(3) << "Average time: " << MarchTimer_.GetAverageTimeMs() << " ms" << endl;
// cout << setprecision(3) << " copy time: " << CopyTimer_.GetAverageTimeMs() << " ms" << endl;
MarchTimer_.Reset();
CopyTimer_.Reset();
cout << "EpsilonFactor = " << Param_.EpsilonFactor << endl;
}
}
{
static bool first = true;
if (first)
{
first = false;
ResetView();
}
}
}
void RayMarchingTexture::SetCameraInfo(const float3 Position, const float3 Direction, const float3 Up)
{
Param_.CameraPos = Position;
Param_.CameraDir = Direction;
Param_.CameraUp = Up;
}
float RayMarchingTexture::GetDistanceFromCamera()
{
return GetDistanceFromPos(Param_.CameraPos);
}
void RayMarchingTexture::SetPerspective(float FOVy, float /*AspectRatio*/, float /*zNear*/, float zFar)
{
Param_.Depth = zFar;
Param_.Height = tanf(FOVy * 3.14159265f / 180.f / 2.f) * Param_.Depth * 2.f;
// Param_.Width = Param_.Height * AspectRatio;
Param_.Width = Param_.Height * (float)Param_.Size.x / Param_.Size.y;
}
void RayMarchingTexture::IncreaseMaxSteps()
{
if (Param_.MaxSteps < numeric_limits<int>::max()) Param_.MaxSteps++;
}
void RayMarchingTexture::DecreaseMaxSteps()
{
if (Param_.MaxSteps > 1) Param_.MaxSteps--;
}
void RayMarchingTexture::IncreaseMinDist()
{
if (Param_.DistanceRatio < numeric_limits<float>::max() / (INC_FACTOR + 1.f)) Param_.DistanceRatio *= INC_FACTOR;
}
void RayMarchingTexture::DecreaseMinDist()
{
if (Param_.DistanceRatio > 0.f) Param_.DistanceRatio *= 1.f / INC_FACTOR;
}
void RayMarchingTexture::PrintMarchingParam()
{
cout << "Min distance = " << Param_.MinDistance << " (ratio " << Param_.DistanceRatio << ")" << endl
<< "Max steps = " << Param_.MaxSteps << endl;
}
void RayMarchingTexture::MapBuffers()
{
if (TexResource_)
{
CudaCheck(cudaGraphicsMapResources(1, &TexResource_));
CudaCheck(cudaGraphicsSubResourceGetMappedArray(&TexArray_, TexResource_, 0, 0));
}
}
void RayMarchingTexture::UnmapBuffers()
{
if (TexResource_)
{
CudaCheck(cudaGraphicsUnmapResources(1, &TexResource_));
}
}